xref: /trunk/main/sc/source/ui/unoobj/targuno.cxx (revision b63233d8)
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 		throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
109 {
110 	if (pDocShell)
111 	{
112 		String aNameStr(aName);
113 		for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
114 			if ( aNames[i] == aNameStr )
115                 return uno::makeAny(uno::Reference< beans::XPropertySet >(new ScLinkTargetTypeObj( pDocShell, i )));
116 	}
117 
118 	throw container::NoSuchElementException();
119 //    return uno::Any();
120 }
121 
getElementNames(void)122 uno::Sequence<rtl::OUString> SAL_CALL ScLinkTargetTypesObj::getElementNames(void) throw( uno::RuntimeException )
123 {
124 	uno::Sequence<rtl::OUString> aRet(SC_LINKTARGETTYPE_COUNT);
125 	rtl::OUString* pArray = aRet.getArray();
126 	for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
127 		pArray[i] = aNames[i];
128 	return aRet;
129 }
130 
hasByName(const rtl::OUString & aName)131 sal_Bool SAL_CALL ScLinkTargetTypesObj::hasByName(const rtl::OUString& aName) throw( uno::RuntimeException )
132 {
133 	String aNameStr = aName;
134 	for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
135 		if ( aNames[i] == aNameStr )
136 			return sal_True;
137 	return sal_False;
138 }
139 
140 // container::XElementAccess
141 
getElementType(void)142 uno::Type SAL_CALL ScLinkTargetTypesObj::getElementType(void) throw( uno::RuntimeException )
143 {
144 	return ::getCppuType((const uno::Reference< beans::XPropertySet >*)0);
145 }
146 
hasElements(void)147 sal_Bool SAL_CALL ScLinkTargetTypesObj::hasElements(void) throw( uno::RuntimeException )
148 {
149 	return sal_True;
150 }
151 
152 //------------------------------------------------------------------------
153 
ScLinkTargetTypeObj(ScDocShell * pDocSh,sal_uInt16 nT)154 ScLinkTargetTypeObj::ScLinkTargetTypeObj(ScDocShell* pDocSh, sal_uInt16 nT) :
155 	pDocShell( pDocSh ),
156 	nType( nT )
157 {
158 	pDocShell->GetDocument()->AddUnoObject(*this);
159 	aName = String( ScResId( nTypeResIds[nType] ) );	//! on demand?
160 }
161 
~ScLinkTargetTypeObj()162 ScLinkTargetTypeObj::~ScLinkTargetTypeObj()
163 {
164 	if (pDocShell)
165 		pDocShell->GetDocument()->RemoveUnoObject(*this);
166 }
167 
Notify(SfxBroadcaster &,const SfxHint & rHint)168 void ScLinkTargetTypeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
169 {
170 	if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
171 		pDocShell = NULL;		// document gone
172 }
173 
174 // document::XLinkTargetSupplier
175 
getLinks(void)176 uno::Reference< container::XNameAccess > SAL_CALL  ScLinkTargetTypeObj::getLinks(void) throw( uno::RuntimeException )
177 {
178 	uno::Reference< container::XNameAccess >  xCollection;
179 
180 	if ( pDocShell )
181 	{
182 		switch ( nType )
183 		{
184 			case SC_LINKTARGETTYPE_SHEET:
185 				xCollection.set(new ScTableSheetsObj(pDocShell));
186 				break;
187 			case SC_LINKTARGETTYPE_RANGENAME:
188 				xCollection.set(new ScNamedRangesObj(pDocShell));
189 				break;
190 			case SC_LINKTARGETTYPE_DBAREA:
191 				xCollection.set(new ScDatabaseRangesObj(pDocShell));
192 				break;
193 			default:
194 				DBG_ERROR("invalid type");
195 		}
196 	}
197 
198 	//	wrap collection in ScLinkTargetsObj because service document::LinkTargets requires
199 	//	beans::XPropertySet as ElementType in container::XNameAccess.
200 	if ( xCollection.is() )
201 		return new ScLinkTargetsObj( xCollection );
202 	return NULL;
203 }
204 
205 // beans::XPropertySet
206 
getPropertySetInfo(void)207 uno::Reference< beans::XPropertySetInfo > SAL_CALL  ScLinkTargetTypeObj::getPropertySetInfo(void) throw( uno::RuntimeException )
208 {
209 	ScUnoGuard aGuard;
210 	static uno::Reference< beans::XPropertySetInfo >  aRef(new SfxItemPropertySetInfo( lcl_GetLinkTargetMap() ));
211 	return aRef;
212 }
213 
setPropertyValue(const rtl::OUString &,const uno::Any &)214 void SAL_CALL ScLinkTargetTypeObj::setPropertyValue(const rtl::OUString& /* aPropertyName */,
215             const uno::Any& /* aValue */)
216 		throw(	beans::UnknownPropertyException,
217 				beans::PropertyVetoException,
218 				lang::IllegalArgumentException,
219 				lang::WrappedTargetException,
220  				uno::RuntimeException )
221 {
222 	//	everything is read-only
223 	//!	exception?
224 }
225 
226 //	static
SetLinkTargetBitmap(uno::Any & rRet,sal_uInt16 nType)227 void ScLinkTargetTypeObj::SetLinkTargetBitmap( uno::Any& rRet, sal_uInt16 nType )
228 {
229 	sal_uInt16 nImgId = 0;
230 	switch ( nType )
231 	{
232 		case SC_LINKTARGETTYPE_SHEET:
233 			nImgId = SC_CONTENT_TABLE;
234 			break;
235 		case SC_LINKTARGETTYPE_RANGENAME:
236 			nImgId = SC_CONTENT_RANGENAME;
237 			break;
238 		case SC_LINKTARGETTYPE_DBAREA:
239 			nImgId = SC_CONTENT_DBAREA;
240 			break;
241 	}
242 	if (nImgId)
243 	{
244         sal_Bool bHighContrast = Application::GetSettings().GetStyleSettings().GetHighContrastMode();
245         ImageList aEntryImages( ScResId( bHighContrast ? RID_IMAGELIST_H_NAVCONT : RID_IMAGELIST_NAVCONT ) );
246 		const Image& rImage = aEntryImages.GetImage( nImgId );
247 		rRet <<= uno::Reference< awt::XBitmap > (VCLUnoHelper::CreateBitmap( rImage.GetBitmapEx() ));
248 	}
249 }
250 
getPropertyValue(const rtl::OUString & PropertyName)251 uno::Any SAL_CALL ScLinkTargetTypeObj::getPropertyValue(const rtl::OUString& PropertyName)
252 		throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
253 {
254 	uno::Any aRet;
255 	String aNameStr(PropertyName);
256 	if ( aNameStr.EqualsAscii( SC_UNO_LINKDISPBIT ) )
257 		SetLinkTargetBitmap( aRet, nType );
258 	else if ( aNameStr.EqualsAscii( SC_UNO_LINKDISPNAME ) )
259 		aRet <<= rtl::OUString( aName );
260 
261 	return aRet;
262 }
263 
SC_IMPL_DUMMY_PROPERTY_LISTENER(ScLinkTargetTypeObj)264 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScLinkTargetTypeObj )
265 
266 //------------------------------------------------------------------------
267 
268 ScLinkTargetsObj::ScLinkTargetsObj( const uno::Reference< container::XNameAccess > & rColl ) :
269 	xCollection( rColl )
270 {
271 	DBG_ASSERT( xCollection.is(), "ScLinkTargetsObj: NULL" );
272 }
273 
~ScLinkTargetsObj()274 ScLinkTargetsObj::~ScLinkTargetsObj()
275 {
276 }
277 
278 // container::XNameAccess
279 
getByName(const rtl::OUString & aName)280 uno::Any SAL_CALL ScLinkTargetsObj::getByName(const rtl::OUString& aName)
281 		throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
282 {
283 	uno::Reference< beans::XPropertySet >  xProp( ScUnoHelpFunctions::AnyToInterface( xCollection->getByName(aName) ), uno::UNO_QUERY );
284 	if (xProp.is())
285         return uno::makeAny(xProp);
286 
287 	throw container::NoSuchElementException();
288 //    return uno::Any();
289 }
290 
getElementNames(void)291 uno::Sequence<rtl::OUString> SAL_CALL ScLinkTargetsObj::getElementNames(void) throw( uno::RuntimeException )
292 {
293 	return xCollection->getElementNames();
294 }
295 
hasByName(const rtl::OUString & aName)296 sal_Bool SAL_CALL ScLinkTargetsObj::hasByName(const rtl::OUString& aName) throw( uno::RuntimeException )
297 {
298 	return xCollection->hasByName(aName);
299 }
300 
301 // container::XElementAccess
302 
getElementType(void)303 uno::Type SAL_CALL ScLinkTargetsObj::getElementType(void) throw( uno::RuntimeException )
304 {
305 	return ::getCppuType((const uno::Reference< beans::XPropertySet >*)0);
306 }
307 
hasElements(void)308 sal_Bool SAL_CALL ScLinkTargetsObj::hasElements(void) throw( uno::RuntimeException )
309 {
310 	return xCollection->hasElements();
311 }
312 
313 
314 
315