xref: /trunk/main/sc/source/ui/unoobj/targuno.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 #include <vcl/image.hxx>
30 #include <vcl/virdev.hxx>
31 //#include <toolkit/unoiface.hxx>
32 #include <toolkit/helper/vclunohelper.hxx>
33 #include <svl/itemprop.hxx>
34 #include <svl/smplhint.hxx>
35 #include <vcl/svapp.hxx>
36 #include <vcl/settings.hxx>
37 #include <com/sun/star/awt/XBitmap.hpp>
38 
39 #include "targuno.hxx"
40 #include "miscuno.hxx"
41 #include "docuno.hxx"
42 #include "datauno.hxx"
43 #include "nameuno.hxx"
44 #include "docsh.hxx"
45 #include "content.hxx"
46 #include "unoguard.hxx"
47 #include "scresid.hxx"
48 #include "sc.hrc"
49 #include "unonames.hxx"
50 
51 using  namespace ::com::sun::star;
52 
53 //------------------------------------------------------------------------
54 
55 sal_uInt16 nTypeResIds[SC_LINKTARGETTYPE_COUNT] =
56 {
57     SCSTR_CONTENT_TABLE,        // SC_LINKTARGETTYPE_SHEET
58     SCSTR_CONTENT_RANGENAME,    // SC_LINKTARGETTYPE_RANGENAME
59     SCSTR_CONTENT_DBAREA        // SC_LINKTARGETTYPE_DBAREA
60 };
61 
lcl_GetLinkTargetMap()62 const SfxItemPropertyMapEntry* lcl_GetLinkTargetMap()
63 {
64     static SfxItemPropertyMapEntry aLinkTargetMap_Impl[] =
65     {
66         {MAP_CHAR_LEN(SC_UNO_LINKDISPBIT),  0,  &getCppuType((const uno::Reference<awt::XBitmap>*)0),   beans::PropertyAttribute::READONLY, 0 },
67         {MAP_CHAR_LEN(SC_UNO_LINKDISPNAME), 0,  &getCppuType((const ::rtl::OUString*)0),                beans::PropertyAttribute::READONLY, 0 },
68         {0,0,0,0,0,0}
69     };
70     return aLinkTargetMap_Impl;
71 }
72 
73 //------------------------------------------------------------------------
74 
75 // service for ScLinkTargetTypeObj is not defined
76 //  must not support document::LinkTarget because the target type cannot be used as a target
77 
78 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypesObj, "ScLinkTargetTypesObj", "com.sun.star.document.LinkTargets" )
79 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypeObj,  "ScLinkTargetTypeObj",  "com.sun.star.document.LinkTargetSupplier" )
80 SC_SIMPLE_SERVICE_INFO( ScLinkTargetsObj,     "ScLinkTargetsObj",     "com.sun.star.document.LinkTargets" )
81 
82 //------------------------------------------------------------------------
83 
ScLinkTargetTypesObj(ScDocShell * pDocSh)84 ScLinkTargetTypesObj::ScLinkTargetTypesObj(ScDocShell* pDocSh) :
85     pDocShell( pDocSh )
86 {
87     pDocShell->GetDocument()->AddUnoObject(*this);
88 
89     for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
90         aNames[i] = String( ScResId( nTypeResIds[i] ) );
91 }
92 
~ScLinkTargetTypesObj()93 ScLinkTargetTypesObj::~ScLinkTargetTypesObj()
94 {
95     if (pDocShell)
96         pDocShell->GetDocument()->RemoveUnoObject(*this);
97 }
98 
Notify(SfxBroadcaster &,const SfxHint & rHint)99 void ScLinkTargetTypesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
100 {
101     if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
102         pDocShell = NULL;       // document gone
103 }
104 
105 // container::XNameAccess
106 
getByName(const rtl::OUString & aName)107 uno::Any SAL_CALL ScLinkTargetTypesObj::getByName(const rtl::OUString& aName)
108 {
109     if (pDocShell)
110     {
111         String aNameStr(aName);
112         for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
113             if ( aNames[i] == aNameStr )
114                 return uno::makeAny(uno::Reference< beans::XPropertySet >(new ScLinkTargetTypeObj( pDocShell, i )));
115     }
116 
117     throw container::NoSuchElementException();
118 //    return uno::Any();
119 }
120 
getElementNames(void)121 uno::Sequence<rtl::OUString> SAL_CALL ScLinkTargetTypesObj::getElementNames(void)
122 {
123     uno::Sequence<rtl::OUString> aRet(SC_LINKTARGETTYPE_COUNT);
124     rtl::OUString* pArray = aRet.getArray();
125     for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
126         pArray[i] = aNames[i];
127     return aRet;
128 }
129 
hasByName(const rtl::OUString & aName)130 sal_Bool SAL_CALL ScLinkTargetTypesObj::hasByName(const rtl::OUString& aName)
131 {
132     String aNameStr = aName;
133     for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
134         if ( aNames[i] == aNameStr )
135             return sal_True;
136     return sal_False;
137 }
138 
139 // container::XElementAccess
140 
getElementType(void)141 uno::Type SAL_CALL ScLinkTargetTypesObj::getElementType(void)
142 {
143     return ::getCppuType((const uno::Reference< beans::XPropertySet >*)0);
144 }
145 
hasElements(void)146 sal_Bool SAL_CALL ScLinkTargetTypesObj::hasElements(void)
147 {
148     return sal_True;
149 }
150 
151 //------------------------------------------------------------------------
152 
ScLinkTargetTypeObj(ScDocShell * pDocSh,sal_uInt16 nT)153 ScLinkTargetTypeObj::ScLinkTargetTypeObj(ScDocShell* pDocSh, sal_uInt16 nT) :
154     pDocShell( pDocSh ),
155     nType( nT )
156 {
157     pDocShell->GetDocument()->AddUnoObject(*this);
158     aName = String( ScResId( nTypeResIds[nType] ) );    //! on demand?
159 }
160 
~ScLinkTargetTypeObj()161 ScLinkTargetTypeObj::~ScLinkTargetTypeObj()
162 {
163     if (pDocShell)
164         pDocShell->GetDocument()->RemoveUnoObject(*this);
165 }
166 
Notify(SfxBroadcaster &,const SfxHint & rHint)167 void ScLinkTargetTypeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
168 {
169     if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
170         pDocShell = NULL;       // document gone
171 }
172 
173 // document::XLinkTargetSupplier
174 
getLinks(void)175 uno::Reference< container::XNameAccess > SAL_CALL  ScLinkTargetTypeObj::getLinks(void)
176 {
177     uno::Reference< container::XNameAccess >  xCollection;
178 
179     if ( pDocShell )
180     {
181         switch ( nType )
182         {
183             case SC_LINKTARGETTYPE_SHEET:
184                 xCollection.set(new ScTableSheetsObj(pDocShell));
185                 break;
186             case SC_LINKTARGETTYPE_RANGENAME:
187                 xCollection.set(new ScNamedRangesObj(pDocShell));
188                 break;
189             case SC_LINKTARGETTYPE_DBAREA:
190                 xCollection.set(new ScDatabaseRangesObj(pDocShell));
191                 break;
192             default:
193                 DBG_ERROR("invalid type");
194         }
195     }
196 
197     //  wrap collection in ScLinkTargetsObj because service document::LinkTargets requires
198     //  beans::XPropertySet as ElementType in container::XNameAccess.
199     if ( xCollection.is() )
200         return new ScLinkTargetsObj( xCollection );
201     return NULL;
202 }
203 
204 // beans::XPropertySet
205 
getPropertySetInfo(void)206 uno::Reference< beans::XPropertySetInfo > SAL_CALL  ScLinkTargetTypeObj::getPropertySetInfo(void)
207 {
208     ScUnoGuard aGuard;
209     static uno::Reference< beans::XPropertySetInfo >  aRef(new SfxItemPropertySetInfo( lcl_GetLinkTargetMap() ));
210     return aRef;
211 }
212 
setPropertyValue(const rtl::OUString &,const uno::Any &)213 void SAL_CALL ScLinkTargetTypeObj::setPropertyValue(const rtl::OUString& /* aPropertyName */,
214             const uno::Any& /* aValue */)
215 {
216     //  everything is read-only
217     //! exception?
218 }
219 
220 //  static
SetLinkTargetBitmap(uno::Any & rRet,sal_uInt16 nType)221 void ScLinkTargetTypeObj::SetLinkTargetBitmap( uno::Any& rRet, sal_uInt16 nType )
222 {
223     sal_uInt16 nImgId = 0;
224     switch ( nType )
225     {
226         case SC_LINKTARGETTYPE_SHEET:
227             nImgId = SC_CONTENT_TABLE;
228             break;
229         case SC_LINKTARGETTYPE_RANGENAME:
230             nImgId = SC_CONTENT_RANGENAME;
231             break;
232         case SC_LINKTARGETTYPE_DBAREA:
233             nImgId = SC_CONTENT_DBAREA;
234             break;
235     }
236     if (nImgId)
237     {
238         sal_Bool bHighContrast = Application::GetSettings().GetStyleSettings().GetHighContrastMode();
239         ImageList aEntryImages( ScResId( bHighContrast ? RID_IMAGELIST_H_NAVCONT : RID_IMAGELIST_NAVCONT ) );
240         const Image& rImage = aEntryImages.GetImage( nImgId );
241         rRet <<= uno::Reference< awt::XBitmap > (VCLUnoHelper::CreateBitmap( rImage.GetBitmapEx() ));
242     }
243 }
244 
getPropertyValue(const rtl::OUString & PropertyName)245 uno::Any SAL_CALL ScLinkTargetTypeObj::getPropertyValue(const rtl::OUString& PropertyName)
246 {
247     uno::Any aRet;
248     String aNameStr(PropertyName);
249     if ( aNameStr.EqualsAscii( SC_UNO_LINKDISPBIT ) )
250         SetLinkTargetBitmap( aRet, nType );
251     else if ( aNameStr.EqualsAscii( SC_UNO_LINKDISPNAME ) )
252         aRet <<= rtl::OUString( aName );
253 
254     return aRet;
255 }
256 
SC_IMPL_DUMMY_PROPERTY_LISTENER(ScLinkTargetTypeObj)257 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScLinkTargetTypeObj )
258 
259 //------------------------------------------------------------------------
260 
261 ScLinkTargetsObj::ScLinkTargetsObj( const uno::Reference< container::XNameAccess > & rColl ) :
262     xCollection( rColl )
263 {
264     DBG_ASSERT( xCollection.is(), "ScLinkTargetsObj: NULL" );
265 }
266 
~ScLinkTargetsObj()267 ScLinkTargetsObj::~ScLinkTargetsObj()
268 {
269 }
270 
271 // container::XNameAccess
272 
getByName(const rtl::OUString & aName)273 uno::Any SAL_CALL ScLinkTargetsObj::getByName(const rtl::OUString& aName)
274 {
275     uno::Reference< beans::XPropertySet >  xProp( ScUnoHelpFunctions::AnyToInterface( xCollection->getByName(aName) ), uno::UNO_QUERY );
276     if (xProp.is())
277         return uno::makeAny(xProp);
278 
279     throw container::NoSuchElementException();
280 //    return uno::Any();
281 }
282 
getElementNames(void)283 uno::Sequence<rtl::OUString> SAL_CALL ScLinkTargetsObj::getElementNames(void)
284 {
285     return xCollection->getElementNames();
286 }
287 
hasByName(const rtl::OUString & aName)288 sal_Bool SAL_CALL ScLinkTargetsObj::hasByName(const rtl::OUString& aName)
289 {
290     return xCollection->hasByName(aName);
291 }
292 
293 // container::XElementAccess
294 
getElementType(void)295 uno::Type SAL_CALL ScLinkTargetsObj::getElementType(void)
296 {
297     return ::getCppuType((const uno::Reference< beans::XPropertySet >*)0);
298 }
299 
hasElements(void)300 sal_Bool SAL_CALL ScLinkTargetsObj::hasElements(void)
301 {
302     return xCollection->hasElements();
303 }
304