Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / appl / module.cxx
blob47ec441ef1a0120223ebb058943773cdca685e6f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include <tools/rcid.h>
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 #include <sal/log.hxx>
53 #define SfxModule
54 #include "sfxslots.hxx"
56 static SfxModuleArr_Impl* pModules=0;
58 class SfxModule_Impl
60 public:
62 SfxSlotPool* pSlotPool;
63 SfxTbxCtrlFactArr_Impl* pTbxCtrlFac;
64 SfxStbCtrlFactArr_Impl* pStbCtrlFac;
65 SfxMenuCtrlFactArr_Impl* pMenuCtrlFac;
66 SfxChildWinFactArr_Impl* pFactArr;
67 ImageList* pImgListSmall;
68 ImageList* pImgListBig;
70 SfxModule_Impl();
71 ~SfxModule_Impl();
72 ImageList* GetImageList( ResMgr* pResMgr, bool bBig );
75 SfxModule_Impl::SfxModule_Impl()
76 : pSlotPool(0)
80 SfxModule_Impl::~SfxModule_Impl()
82 delete pSlotPool;
83 delete pTbxCtrlFac;
84 delete pStbCtrlFac;
85 delete pMenuCtrlFac;
86 delete pFactArr;
87 delete pImgListSmall;
88 delete pImgListBig;
91 ImageList* SfxModule_Impl::GetImageList( ResMgr* pResMgr, bool bBig )
93 ImageList*& rpList = bBig ? pImgListBig : pImgListSmall;
94 if ( !rpList )
96 ResId aResId( bBig ? ( RID_DEFAULTIMAGELIST_LC ) : ( RID_DEFAULTIMAGELIST_SC ), *pResMgr );
98 aResId.SetRT( RSC_IMAGELIST );
100 DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
102 if ( pResMgr->IsAvailable(aResId) )
103 rpList = new ImageList( aResId );
104 else
105 rpList = new ImageList();
108 return rpList; }
110 TYPEINIT1(SfxModule, SfxShell);
112 //=========================================================================
114 SFX_IMPL_INTERFACE(SfxModule,SfxShell,SfxResId(0))
118 //====================================================================
120 ResMgr* SfxModule::GetResMgr()
122 return pResMgr;
125 //====================================================================
127 SfxModule::SfxModule( ResMgr* pMgrP, sal_Bool bDummyP,
128 SfxObjectFactory* pFactoryP, ... )
129 : pResMgr( pMgrP ), bDummy( bDummyP ), pImpl(0L)
131 Construct_Impl();
132 va_list pVarArgs;
133 va_start( pVarArgs, pFactoryP );
134 for ( SfxObjectFactory *pArg = pFactoryP; pArg;
135 pArg = va_arg( pVarArgs, SfxObjectFactory* ) )
136 pArg->SetModule_Impl( this );
137 va_end(pVarArgs);
140 void SfxModule::Construct_Impl()
142 if( !bDummy )
144 SfxApplication *pApp = SFX_APP();
145 SfxModuleArr_Impl& rArr = GetModules_Impl();
146 SfxModule* pPtr = (SfxModule*)this;
147 rArr.C40_INSERT( SfxModule, pPtr, rArr.Count() );
148 pImpl = new SfxModule_Impl;
149 pImpl->pSlotPool = new SfxSlotPool( &pApp->GetAppSlotPool_Impl(), pResMgr );
151 pImpl->pTbxCtrlFac=0;
152 pImpl->pStbCtrlFac=0;
153 pImpl->pMenuCtrlFac=0;
154 pImpl->pFactArr=0;
155 pImpl->pImgListSmall=0;
156 pImpl->pImgListBig=0;
158 SetPool( &pApp->GetPool() );
162 //====================================================================
164 SfxModule::~SfxModule()
166 if( !bDummy )
168 if ( SFX_APP()->Get_Impl() )
170 // The module will be destroyed before the Deinitialize,
171 // so remove from the array
172 SfxModuleArr_Impl& rArr = GetModules_Impl();
173 for( sal_uInt16 nPos = rArr.Count(); nPos--; )
175 if( rArr[ nPos ] == this )
177 rArr.Remove( nPos );
178 break;
182 delete pImpl;
185 delete pResMgr;
189 //-------------------------------------------------------------------------
191 SfxSlotPool* SfxModule::GetSlotPool() const
193 return pImpl->pSlotPool;
196 //-------------------------------------------------------------------------
198 void SfxModule::RegisterChildWindow(SfxChildWinFactory *pFact)
200 DBG_ASSERT( pImpl, "No real Module!" );
202 if (!pImpl->pFactArr)
203 pImpl->pFactArr = new SfxChildWinFactArr_Impl;
205 for (sal_uInt16 nFactory=0; nFactory<pImpl->pFactArr->Count(); ++nFactory)
207 if (pFact->nId == (*pImpl->pFactArr)[nFactory]->nId)
209 pImpl->pFactArr->Remove( nFactory );
210 SAL_WARN("sfx2.appl", "ChildWindow registered multiple times!");
211 return;
215 pImpl->pFactArr->C40_INSERT(
216 SfxChildWinFactory, pFact, pImpl->pFactArr->Count() );
219 //-------------------------------------------------------------------------
221 void SfxModule::RegisterToolBoxControl( SfxTbxCtrlFactory *pFact )
223 if (!pImpl->pTbxCtrlFac)
224 pImpl->pTbxCtrlFac = new SfxTbxCtrlFactArr_Impl;
226 #ifdef DBG_UTIL
227 for ( sal_uInt16 n=0; n<pImpl->pTbxCtrlFac->Count(); n++ )
229 SfxTbxCtrlFactory *pF = (*pImpl->pTbxCtrlFac)[n];
230 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
231 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
233 DBG_WARNING("TbxController-Registering is not clearly defined!");
236 #endif
238 pImpl->pTbxCtrlFac->C40_INSERT( SfxTbxCtrlFactory, pFact, pImpl->pTbxCtrlFac->Count() );
241 //-------------------------------------------------------------------------
243 void SfxModule::RegisterStatusBarControl( SfxStbCtrlFactory *pFact )
245 if (!pImpl->pStbCtrlFac)
246 pImpl->pStbCtrlFac = new SfxStbCtrlFactArr_Impl;
248 #ifdef DBG_UTIL
249 for ( sal_uInt16 n=0; n<pImpl->pStbCtrlFac->Count(); n++ )
251 SfxStbCtrlFactory *pF = (*pImpl->pStbCtrlFac)[n];
252 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
253 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
255 DBG_WARNING("TbxController-Registering is not clearly defined!");
258 #endif
260 pImpl->pStbCtrlFac->C40_INSERT( SfxStbCtrlFactory, pFact, pImpl->pStbCtrlFac->Count() );
263 //-------------------------------------------------------------------------
265 void SfxModule::RegisterMenuControl( SfxMenuCtrlFactory *pFact )
267 if (!pImpl->pMenuCtrlFac)
268 pImpl->pMenuCtrlFac = new SfxMenuCtrlFactArr_Impl;
270 #ifdef DBG_UTIL
271 for ( sal_uInt16 n=0; n<pImpl->pMenuCtrlFac->Count(); n++ )
273 SfxMenuCtrlFactory *pF = (*pImpl->pMenuCtrlFac)[n];
274 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
275 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
277 DBG_WARNING("MenuController-Registering is not clearly defined!");
280 #endif
282 pImpl->pMenuCtrlFac->C40_INSERT( SfxMenuCtrlFactory, pFact, pImpl->pMenuCtrlFac->Count() );
285 //-------------------------------------------------------------------------
287 SfxTbxCtrlFactArr_Impl* SfxModule::GetTbxCtrlFactories_Impl() const
289 return pImpl->pTbxCtrlFac;
292 //-------------------------------------------------------------------------
294 SfxStbCtrlFactArr_Impl* SfxModule::GetStbCtrlFactories_Impl() const
296 return pImpl->pStbCtrlFac;
299 //-------------------------------------------------------------------------
301 SfxMenuCtrlFactArr_Impl* SfxModule::GetMenuCtrlFactories_Impl() const
303 return pImpl->pMenuCtrlFac;
306 //-------------------------------------------------------------------------
308 SfxChildWinFactArr_Impl* SfxModule::GetChildWinFactories_Impl() const
310 return pImpl->pFactArr;
313 ImageList* SfxModule::GetImageList_Impl( sal_Bool bBig )
315 return pImpl->GetImageList( pResMgr, bBig );
318 SfxTabPage* SfxModule::CreateTabPage( sal_uInt16, Window*, const SfxItemSet& )
320 return NULL;
323 SfxModuleArr_Impl& SfxModule::GetModules_Impl()
325 if( !pModules )
326 pModules = new SfxModuleArr_Impl;
327 return *pModules;
330 void SfxModule::DestroyModules_Impl()
332 if ( pModules )
334 SfxModuleArr_Impl& rModules = *pModules;
335 for( sal_uInt16 nPos = rModules.Count(); nPos--; )
337 SfxModule* pMod = rModules.GetObject(nPos);
338 delete pMod;
340 delete pModules, pModules = 0;
344 void SfxModule::Invalidate( sal_uInt16 nId )
346 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
347 if ( pFrame->GetObjectShell()->GetModule() == this )
348 Invalidate_Impl( pFrame->GetBindings(), nId );
351 bool SfxModule::IsChildWindowAvailable( const sal_uInt16 i_nId, const SfxViewFrame* i_pViewFrame ) const
353 if ( i_nId != SID_TASKPANE )
354 // by default, assume it is
355 return true;
357 const SfxViewFrame* pViewFrame = i_pViewFrame ? i_pViewFrame : GetFrame();
358 ENSURE_OR_RETURN( pViewFrame, "SfxModule::IsChildWindowAvailable: no frame to ask for the module identifier!", false );
359 return ::sfx2::ModuleTaskPane::ModuleHasToolPanels( pViewFrame->GetFrame().GetFrameInterface() );
362 SfxModule* SfxModule::GetActiveModule( SfxViewFrame* pFrame )
364 if ( !pFrame )
365 pFrame = SfxViewFrame::Current();
366 SfxObjectShell* pSh = 0;
367 if( pFrame )
368 pSh = pFrame->GetObjectShell();
369 return pSh ? pSh->GetModule() : 0;
372 FieldUnit SfxModule::GetModuleFieldUnit( ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > const & i_frame )
374 ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FUNIT_100TH_MM );
376 // find SfxViewFrame for the given XFrame
377 SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
378 while ( pViewFrame != NULL )
380 if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
381 break;
382 pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
384 ENSURE_OR_RETURN( pViewFrame != NULL, "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame", FUNIT_100TH_MM );
386 // find the module
387 SfxModule const * pModule = GetActiveModule( pViewFrame );
388 ENSURE_OR_RETURN( pModule != NULL, "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!", FUNIT_100TH_MM );
390 SfxPoolItem const * pItem = pModule->GetItem( SID_ATTR_METRIC );
391 if ( pItem == NULL )
393 SAL_WARN(
394 "sfx2.appl",
395 "SfxModule::GetFieldUnit: no metric item in the module implemented"
396 " by '" << typeid(*pModule).name() << "'!");
397 return FUNIT_100TH_MM;
399 return (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
402 FieldUnit SfxModule::GetCurrentFieldUnit()
404 FieldUnit eUnit = FUNIT_INCH;
405 SfxModule* pModule = GetActiveModule();
406 if ( pModule )
408 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
409 DBG_ASSERT( pItem, "GetFieldUnit(): no item" );
410 if ( pItem )
411 eUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
413 else
414 SAL_WARN( "sfx2.appl", "GetModuleFieldUnit(): no module found" );
415 return eUnit;
418 FieldUnit SfxModule::GetFieldUnit() const
420 FieldUnit eUnit = FUNIT_INCH;
421 const SfxPoolItem* pItem = GetItem( SID_ATTR_METRIC );
422 DBG_ASSERT( pItem, "GetFieldUnit(): no item" );
423 if ( pItem )
424 eUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
425 return eUnit;
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */