xref: /trunk/main/sfx2/source/appl/module.cxx (revision d119d52d)
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_sfx2.hxx"
26 
27 #ifndef GCC
28 #endif
29 
30 #include <stdio.h>
31 #include <tools/rcid.h>
32 
33 #include <cstdarg>
34 #include <sfx2/module.hxx>
35 #include <sfx2/app.hxx>
36 #include "arrdecl.hxx"
37 #include "sfx2/sfxresid.hxx"
38 #include <sfx2/msgpool.hxx>
39 #include <sfx2/tbxctrl.hxx>
40 #include "sfx2/stbitem.hxx"
41 #include <sfx2/mnuitem.hxx>
42 #include <sfx2/childwin.hxx>
43 #include <sfx2/mnumgr.hxx>
44 #include <sfx2/docfac.hxx>
45 #include <sfx2/objface.hxx>
46 #include <sfx2/viewfrm.hxx>
47 #include <svl/intitem.hxx>
48 #include "sfx2/taskpane.hxx"
49 #include <tools/diagnose_ex.h>
50 #include <rtl/strbuf.hxx>
51 
52 #define SfxModule
53 #include "sfxslots.hxx"
54 
55 static SfxModuleArr_Impl* pModules=0;
56 
57 class SfxModule_Impl
58 {
59 public:
60 
61 	SfxSlotPool*				pSlotPool;
62 	SfxTbxCtrlFactArr_Impl* 	pTbxCtrlFac;
63 	SfxStbCtrlFactArr_Impl* 	pStbCtrlFac;
64 	SfxMenuCtrlFactArr_Impl*	pMenuCtrlFac;
65 	SfxChildWinFactArr_Impl*	pFactArr;
66     ImageList*                  pImgListSmall;
67     ImageList*                  pImgListBig;
68 	ImageList*					pImgListHiSmall;
69 	ImageList*					pImgListHiBig;
70 
71 								SfxModule_Impl();
72 								~SfxModule_Impl();
73     ImageList*                  GetImageList( ResMgr*, sal_Bool, sal_Bool bHiContrast = sal_False );
74 };
75 
SfxModule_Impl()76 SfxModule_Impl::SfxModule_Impl()
77  : pSlotPool(0)
78 {
79 }
80 
~SfxModule_Impl()81 SfxModule_Impl::~SfxModule_Impl()
82 {
83     delete pSlotPool;
84     delete pTbxCtrlFac;
85     delete pStbCtrlFac;
86     delete pMenuCtrlFac;
87     delete pFactArr;
88     delete pImgListSmall;
89     delete pImgListBig;
90     delete pImgListHiSmall;
91     delete pImgListHiBig;
92 }
93 
GetImageList(ResMgr * pResMgr,sal_Bool bBig,sal_Bool bHiContrast)94 ImageList* SfxModule_Impl::GetImageList( ResMgr* pResMgr, sal_Bool bBig, sal_Bool bHiContrast )
95 {
96     ImageList*& rpList = bBig ? ( bHiContrast ? pImgListHiBig: pImgListBig ) :
97 								( bHiContrast ? pImgListHiSmall : pImgListSmall );
98     if ( !rpList )
99     {
100         ResId aResId( bBig ? ( bHiContrast ? RID_DEFAULTIMAGELIST_LCH : RID_DEFAULTIMAGELIST_LC ) :
101 							 ( bHiContrast ? RID_DEFAULTIMAGELIST_SCH : RID_DEFAULTIMAGELIST_SC ), *pResMgr );
102         aResId.SetRT( RSC_IMAGELIST );
103 
104         DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
105 
106         if ( pResMgr->IsAvailable(aResId) )
107             rpList = new ImageList( aResId );
108         else
109             rpList = new ImageList();
110     }
111 
112     return rpList; }
113 
114 TYPEINIT1(SfxModule, SfxShell);
115 
116 //=========================================================================
117 
118 SFX_IMPL_INTERFACE(SfxModule,SfxShell,SfxResId(0))
119 {
120 }
121 
122 //====================================================================
123 
GetResMgr()124 ResMgr* SfxModule::GetResMgr()
125 {
126 	return pResMgr;
127 }
128 
129 //====================================================================
130 /*
131 SfxModule::SfxModule( ResMgr* pMgrP, sal_Bool bDummyP,
132 					  SfxObjectFactory* pFactoryP )
133 	: pResMgr( pMgrP ), bDummy( bDummyP ), pImpl(0L)
134 {
135 	Construct_Impl();
136 	if ( pFactoryP )
137 		pFactoryP->SetModule_Impl( this );
138 }
139 */
SfxModule(ResMgr * pMgrP,sal_Bool bDummyP,SfxObjectFactory * pFactoryP,...)140 SfxModule::SfxModule( ResMgr* pMgrP, sal_Bool bDummyP,
141 					  SfxObjectFactory* pFactoryP, ... )
142 	: pResMgr( pMgrP ), bDummy( bDummyP ), pImpl(0L)
143 {
144 	Construct_Impl();
145 	va_list pVarArgs;
146 	va_start( pVarArgs, pFactoryP );
147 	for ( SfxObjectFactory *pArg = pFactoryP; pArg;
148 		 pArg = va_arg( pVarArgs, SfxObjectFactory* ) )
149 		pArg->SetModule_Impl( this );
150 	va_end(pVarArgs);
151 }
152 
Construct_Impl()153 void SfxModule::Construct_Impl()
154 {
155 	if( !bDummy )
156 	{
157 		SfxApplication *pApp = SFX_APP();
158         SfxModuleArr_Impl& rArr = GetModules_Impl();
159 		SfxModule* pPtr = (SfxModule*)this;
160 		rArr.C40_INSERT( SfxModule, pPtr, rArr.Count() );
161 		pImpl = new SfxModule_Impl;
162 		pImpl->pSlotPool = new SfxSlotPool( &pApp->GetAppSlotPool_Impl(), pResMgr );
163 
164 		pImpl->pTbxCtrlFac=0;
165 		pImpl->pStbCtrlFac=0;
166 		pImpl->pMenuCtrlFac=0;
167 		pImpl->pFactArr=0;
168         pImpl->pImgListSmall=0;
169         pImpl->pImgListBig=0;
170         pImpl->pImgListHiSmall=0;
171         pImpl->pImgListHiBig=0;
172 
173 		SetPool( &pApp->GetPool() );
174 	}
175 }
176 
177 //====================================================================
178 
~SfxModule()179 SfxModule::~SfxModule()
180 {
181 	if( !bDummy )
182 	{
183 		if ( SFX_APP()->Get_Impl() )
184 		{
185 			// Das Modul wird noch vor dem DeInitialize zerst"ort, also auis dem Array entfernen
186             SfxModuleArr_Impl& rArr = GetModules_Impl();
187 			for( sal_uInt16 nPos = rArr.Count(); nPos--; )
188 			{
189 				if( rArr[ nPos ] == this )
190 				{
191 					rArr.Remove( nPos );
192 					break;
193 				}
194 			}
195 
196 			delete pImpl;
197 		}
198 
199 		delete pResMgr;
200 	}
201 }
202 
203 //-------------------------------------------------------------------------
204 
GetSlotPool() const205 SfxSlotPool* SfxModule::GetSlotPool() const
206 {
207 	return pImpl->pSlotPool;
208 }
209 
210 //-------------------------------------------------------------------------
211 
RegisterChildWindow(SfxChildWinFactory * pFact)212 void SfxModule::RegisterChildWindow(SfxChildWinFactory *pFact)
213 {
214 	DBG_ASSERT( pImpl, "Kein echtes Modul!" );
215 
216 	if (!pImpl->pFactArr)
217 		pImpl->pFactArr = new SfxChildWinFactArr_Impl;
218 
219 //#ifdef DBG_UTIL
220 	for (sal_uInt16 nFactory=0; nFactory<pImpl->pFactArr->Count(); ++nFactory)
221 	{
222 		if (pFact->nId ==  (*pImpl->pFactArr)[nFactory]->nId)
223 		{
224 			pImpl->pFactArr->Remove( nFactory );
225 			DBG_ERROR("ChildWindow mehrfach registriert!");
226 			return;
227 		}
228 	}
229 //#endif
230 
231 	pImpl->pFactArr->C40_INSERT(
232 		SfxChildWinFactory, pFact, pImpl->pFactArr->Count() );
233 }
234 
235 //-------------------------------------------------------------------------
236 
RegisterChildWindowContext(sal_uInt16 nId,SfxChildWinContextFactory * pFact)237 void SfxModule::RegisterChildWindowContext( sal_uInt16 nId,
238 		SfxChildWinContextFactory *pFact)
239 {
240 	DBG_ASSERT( pImpl, "Kein echtes Modul!" );
241 
242 	sal_uInt16 nCount = pImpl->pFactArr->Count();
243 	for (sal_uInt16 nFactory=0; nFactory<nCount; ++nFactory)
244 	{
245 		SfxChildWinFactory *pF = (*pImpl->pFactArr)[nFactory];
246 		if ( nId == pF->nId )
247 		{
248 			if ( !pF->pArr )
249 				pF->pArr = new SfxChildWinContextArr_Impl;
250 			pF->pArr->C40_INSERT( SfxChildWinContextFactory, pFact, pF->pArr->Count() );
251 			return;
252 		}
253 	}
254 
255 	DBG_ERROR( "Kein ChildWindow fuer diesen Context!" );
256 }
257 
258 //-------------------------------------------------------------------------
259 
RegisterToolBoxControl(SfxTbxCtrlFactory * pFact)260 void SfxModule::RegisterToolBoxControl( SfxTbxCtrlFactory *pFact )
261 {
262 	if (!pImpl->pTbxCtrlFac)
263 		pImpl->pTbxCtrlFac = new SfxTbxCtrlFactArr_Impl;
264 
265 #ifdef DBG_UTIL
266 	for ( sal_uInt16 n=0; n<pImpl->pTbxCtrlFac->Count(); n++ )
267 	{
268 		SfxTbxCtrlFactory *pF = (*pImpl->pTbxCtrlFac)[n];
269 		if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
270 			(pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
271 		{
272 			DBG_WARNING("TbxController-Registrierung ist nicht eindeutig!");
273 		}
274 	}
275 #endif
276 
277 	pImpl->pTbxCtrlFac->C40_INSERT( SfxTbxCtrlFactory, pFact, pImpl->pTbxCtrlFac->Count() );
278 }
279 
280 //-------------------------------------------------------------------------
281 
RegisterStatusBarControl(SfxStbCtrlFactory * pFact)282 void SfxModule::RegisterStatusBarControl( SfxStbCtrlFactory *pFact )
283 {
284 	if (!pImpl->pStbCtrlFac)
285 		pImpl->pStbCtrlFac = new SfxStbCtrlFactArr_Impl;
286 
287 #ifdef DBG_UTIL
288 	for ( sal_uInt16 n=0; n<pImpl->pStbCtrlFac->Count(); n++ )
289 	{
290 		SfxStbCtrlFactory *pF = (*pImpl->pStbCtrlFac)[n];
291 		if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
292 			(pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
293 		{
294 			DBG_WARNING("StbController-Registrierung ist nicht eindeutig!");
295 		}
296 	}
297 #endif
298 
299 	pImpl->pStbCtrlFac->C40_INSERT( SfxStbCtrlFactory, pFact, pImpl->pStbCtrlFac->Count() );
300 }
301 
302 //-------------------------------------------------------------------------
303 
RegisterMenuControl(SfxMenuCtrlFactory * pFact)304 void SfxModule::RegisterMenuControl( SfxMenuCtrlFactory *pFact )
305 {
306 	if (!pImpl->pMenuCtrlFac)
307 		pImpl->pMenuCtrlFac = new SfxMenuCtrlFactArr_Impl;
308 
309 #ifdef DBG_UTIL
310 	for ( sal_uInt16 n=0; n<pImpl->pMenuCtrlFac->Count(); n++ )
311 	{
312 		SfxMenuCtrlFactory *pF = (*pImpl->pMenuCtrlFac)[n];
313 		if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
314 			(pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
315 		{
316 			DBG_WARNING("MenuController-Registrierung ist nicht eindeutig!");
317 		}
318 	}
319 #endif
320 
321 	pImpl->pMenuCtrlFac->C40_INSERT( SfxMenuCtrlFactory, pFact, pImpl->pMenuCtrlFac->Count() );
322 }
323 
324 //-------------------------------------------------------------------------
325 
GetTbxCtrlFactories_Impl() const326 SfxTbxCtrlFactArr_Impl*  SfxModule::GetTbxCtrlFactories_Impl() const
327 {
328 	return pImpl->pTbxCtrlFac;
329 }
330 
331 //-------------------------------------------------------------------------
332 
GetStbCtrlFactories_Impl() const333 SfxStbCtrlFactArr_Impl*  SfxModule::GetStbCtrlFactories_Impl() const
334 {
335 	return pImpl->pStbCtrlFac;
336 }
337 
338 //-------------------------------------------------------------------------
339 
GetMenuCtrlFactories_Impl() const340 SfxMenuCtrlFactArr_Impl* SfxModule::GetMenuCtrlFactories_Impl() const
341 {
342 	return pImpl->pMenuCtrlFac;
343 }
344 
345 //-------------------------------------------------------------------------
346 
GetChildWinFactories_Impl() const347 SfxChildWinFactArr_Impl* SfxModule::GetChildWinFactories_Impl() const
348 {
349 	return pImpl->pFactArr;
350 }
351 
GetImageList_Impl(sal_Bool bBig)352 ImageList* SfxModule::GetImageList_Impl( sal_Bool bBig )
353 {
354 	return pImpl->GetImageList( pResMgr, bBig, sal_False );
355 }
356 
GetImageList_Impl(sal_Bool bBig,sal_Bool bHiContrast)357 ImageList* SfxModule::GetImageList_Impl( sal_Bool bBig, sal_Bool bHiContrast )
358 {
359     return pImpl->GetImageList( pResMgr, bBig, bHiContrast );
360 }
361 
CreateTabPage(sal_uInt16,Window *,const SfxItemSet &)362 SfxTabPage*	SfxModule::CreateTabPage( sal_uInt16, Window*, const SfxItemSet& )
363 {
364 	return NULL;
365 }
366 
GetModules_Impl()367 SfxModuleArr_Impl& SfxModule::GetModules_Impl()
368 {
369     if( !pModules )
370         pModules = new SfxModuleArr_Impl;
371     return *pModules;
372 };
373 
DestroyModules_Impl()374 void SfxModule::DestroyModules_Impl()
375 {
376     if ( pModules )
377     {
378         SfxModuleArr_Impl& rModules = *pModules;
379         for( sal_uInt16 nPos = rModules.Count(); nPos--; )
380 	{
381 	    SfxModule* pMod = rModules.GetObject(nPos);
382 	    delete pMod;
383 	}
384     }
385 }
386 
Invalidate(sal_uInt16 nId)387 void SfxModule::Invalidate( sal_uInt16 nId )
388 {
389     for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
390         if ( pFrame->GetObjectShell()->GetModule() == this )
391             Invalidate_Impl( pFrame->GetBindings(), nId );
392 }
393 
IsActive() const394 sal_Bool SfxModule::IsActive() const
395 {
396 	SfxViewFrame* pFrame = SfxViewFrame::Current();
397 	if ( pFrame && pFrame->GetObjectShell()->GetFactory().GetModule() == this )
398 		return sal_True;
399 	return sal_False;
400 }
401 
IsChildWindowAvailable(const sal_uInt16 i_nId,const SfxViewFrame * i_pViewFrame) const402 bool SfxModule::IsChildWindowAvailable( const sal_uInt16 i_nId, const SfxViewFrame* i_pViewFrame ) const
403 {
404     if ( i_nId != SID_TASKPANE )
405         // by default, assume it is
406         return true;
407 
408     const SfxViewFrame* pViewFrame = i_pViewFrame ? i_pViewFrame : GetFrame();
409     ENSURE_OR_RETURN( pViewFrame, "SfxModule::IsChildWindowAvailable: no frame to ask for the module identifier!", false );
410     return ::sfx2::ModuleTaskPane::ModuleHasToolPanels( pViewFrame->GetFrame().GetFrameInterface() );
411 }
412 
GetActiveModule(SfxViewFrame * pFrame)413 SfxModule* SfxModule::GetActiveModule( SfxViewFrame* pFrame )
414 {
415 	if ( !pFrame )
416 		pFrame = SfxViewFrame::Current();
417 	SfxObjectShell* pSh = 0;
418     if( pFrame )
419         pSh = pFrame->GetObjectShell();
420 	return pSh ? pSh->GetModule() : 0;
421 }
422 
GetModuleFieldUnit(::com::sun::star::uno::Reference<::com::sun::star::frame::XFrame> const & i_frame)423 FieldUnit SfxModule::GetModuleFieldUnit( ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > const & i_frame )
424 {
425     ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FUNIT_100TH_MM );
426 
427     // find SfxViewFrame for the given XFrame
428     SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
429     while ( pViewFrame != NULL )
430     {
431         if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
432             break;
433         pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
434     }
435     ENSURE_OR_RETURN( pViewFrame != NULL, "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame", FUNIT_100TH_MM );
436 
437     // find the module
438     SfxModule const * pModule = GetActiveModule( pViewFrame );
439     ENSURE_OR_RETURN( pModule != NULL, "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!", FUNIT_100TH_MM );
440 	if ( pModule )
441 		return pModule->GetFieldUnit();
442 	return FUNIT_INCH;
443 }
444 
GetCurrentFieldUnit()445 FieldUnit SfxModule::GetCurrentFieldUnit()
446 {
447 	FieldUnit eUnit = FUNIT_INCH;
448 	SfxModule* pModule = GetActiveModule();
449 	if ( pModule )
450 	{
451 		const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
452 		if ( pItem )
453 			eUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
454 	}
455     else
456 	    DBG_ERRORFILE( "GetModuleFieldUnit(): no module found" );
457 	return eUnit;
458 }
459 
GetFieldUnit() const460 FieldUnit SfxModule::GetFieldUnit() const
461 {
462 	FieldUnit eUnit = FUNIT_INCH;
463 	const SfxPoolItem* pItem = GetItem( SID_ATTR_METRIC );
464 	if ( pItem )
465 		eUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
466 	return eUnit;
467 }
468