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