bump product version to 4.2.0.1
[LibreOffice.git] / sfx2 / source / appl / module.cxx
blob05ebac78ce76b894d57b4e564c623adcaf68476a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <tools/rcid.h>
23 #include <cstdarg>
24 #include <sfx2/module.hxx>
25 #include <sfx2/app.hxx>
26 #include "arrdecl.hxx"
27 #include <sfx2/sfxresid.hxx>
28 #include <sfx2/msgpool.hxx>
29 #include <sfx2/tbxctrl.hxx>
30 #include <sfx2/stbitem.hxx>
31 #include <sfx2/mnuitem.hxx>
32 #include <sfx2/childwin.hxx>
33 #include <sfx2/mnumgr.hxx>
34 #include <sfx2/docfac.hxx>
35 #include <sfx2/objface.hxx>
36 #include <sfx2/viewfrm.hxx>
37 #include <svl/intitem.hxx>
38 #include <sfx2/taskpane.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <rtl/strbuf.hxx>
41 #include <sal/log.hxx>
43 #define SfxModule
44 #include "sfxslots.hxx"
46 static SfxModuleArr_Impl* pModules=0;
48 class SfxModule_Impl
50 public:
52 SfxSlotPool* pSlotPool;
53 SfxTbxCtrlFactArr_Impl* pTbxCtrlFac;
54 SfxStbCtrlFactArr_Impl* pStbCtrlFac;
55 SfxMenuCtrlFactArr_Impl* pMenuCtrlFac;
56 SfxChildWinFactArr_Impl* pFactArr;
57 ImageList* pImgListSmall;
58 ImageList* pImgListBig;
60 SfxModule_Impl();
61 ~SfxModule_Impl();
62 ImageList* GetImageList( ResMgr* pResMgr, bool bBig );
65 SfxModule_Impl::SfxModule_Impl()
66 : pSlotPool(0), pTbxCtrlFac(0), pStbCtrlFac(0), pMenuCtrlFac(0), pFactArr(0), pImgListSmall(0), pImgListBig(0)
70 SfxModule_Impl::~SfxModule_Impl()
72 delete pSlotPool;
73 delete pTbxCtrlFac;
74 delete pStbCtrlFac;
75 delete pMenuCtrlFac;
76 delete pFactArr;
77 delete pImgListSmall;
78 delete pImgListBig;
81 ImageList* SfxModule_Impl::GetImageList( ResMgr* pResMgr, bool bBig )
83 ImageList*& rpList = bBig ? pImgListBig : pImgListSmall;
84 if ( !rpList )
86 ResId aResId( bBig ? ( RID_DEFAULTIMAGELIST_LC ) : ( RID_DEFAULTIMAGELIST_SC ), *pResMgr );
88 aResId.SetRT( RSC_IMAGELIST );
90 DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
92 if ( pResMgr->IsAvailable(aResId) )
93 rpList = new ImageList( aResId );
94 else
95 rpList = new ImageList();
98 return rpList; }
100 TYPEINIT1(SfxModule, SfxShell);
102 //=========================================================================
104 SFX_IMPL_INTERFACE(SfxModule,SfxShell,SfxResId(0))
108 //====================================================================
110 ResMgr* SfxModule::GetResMgr()
112 return pResMgr;
115 //====================================================================
117 SfxModule::SfxModule( ResMgr* pMgrP, sal_Bool bDummyP,
118 SfxObjectFactory* pFactoryP, ... )
119 : pResMgr( pMgrP ), bDummy( bDummyP ), pImpl(0L)
121 Construct_Impl();
122 va_list pVarArgs;
123 va_start( pVarArgs, pFactoryP );
124 for ( SfxObjectFactory *pArg = pFactoryP; pArg;
125 pArg = va_arg( pVarArgs, SfxObjectFactory* ) )
126 pArg->SetModule_Impl( this );
127 va_end(pVarArgs);
130 void SfxModule::Construct_Impl()
132 if( !bDummy )
134 SfxApplication *pApp = SFX_APP();
135 SfxModuleArr_Impl& rArr = GetModules_Impl();
136 SfxModule* pPtr = (SfxModule*)this;
137 rArr.push_back( pPtr );
138 pImpl = new SfxModule_Impl;
139 pImpl->pSlotPool = new SfxSlotPool( &pApp->GetAppSlotPool_Impl(), pResMgr );
141 pImpl->pTbxCtrlFac=0;
142 pImpl->pStbCtrlFac=0;
143 pImpl->pMenuCtrlFac=0;
144 pImpl->pFactArr=0;
145 pImpl->pImgListSmall=0;
146 pImpl->pImgListBig=0;
148 SetPool( &pApp->GetPool() );
152 //====================================================================
154 SfxModule::~SfxModule()
156 if( !bDummy )
158 if ( SFX_APP()->Get_Impl() )
160 // The module will be destroyed before the Deinitialize,
161 // so remove from the array
162 SfxModuleArr_Impl& rArr = GetModules_Impl();
163 for( sal_uInt16 nPos = rArr.size(); nPos--; )
165 if( rArr[ nPos ] == this )
167 rArr.erase( rArr.begin() + nPos );
168 break;
172 delete pImpl;
175 delete pResMgr;
179 //-------------------------------------------------------------------------
181 SfxSlotPool* SfxModule::GetSlotPool() const
183 return pImpl->pSlotPool;
186 //-------------------------------------------------------------------------
188 void SfxModule::RegisterChildWindow(SfxChildWinFactory *pFact)
190 DBG_ASSERT( pImpl, "No real Module!" );
192 if (!pImpl->pFactArr)
193 pImpl->pFactArr = new SfxChildWinFactArr_Impl;
195 for (sal_uInt16 nFactory=0; nFactory<pImpl->pFactArr->size(); ++nFactory)
197 if (pFact->nId == (*pImpl->pFactArr)[nFactory]->nId)
199 pImpl->pFactArr->erase( pImpl->pFactArr->begin() + nFactory );
200 SAL_WARN("sfx.appl", "ChildWindow registered multiple times!");
201 return;
205 pImpl->pFactArr->push_back( pFact );
208 //-------------------------------------------------------------------------
210 void SfxModule::RegisterToolBoxControl( SfxTbxCtrlFactory *pFact )
212 if (!pImpl->pTbxCtrlFac)
213 pImpl->pTbxCtrlFac = new SfxTbxCtrlFactArr_Impl;
215 #ifdef DBG_UTIL
216 for ( sal_uInt16 n=0; n<pImpl->pTbxCtrlFac->size(); n++ )
218 SfxTbxCtrlFactory *pF = (*pImpl->pTbxCtrlFac)[n];
219 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
220 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
222 DBG_WARNING("TbxController-Registering is not clearly defined!");
225 #endif
227 pImpl->pTbxCtrlFac->push_back( pFact );
230 //-------------------------------------------------------------------------
232 void SfxModule::RegisterStatusBarControl( SfxStbCtrlFactory *pFact )
234 if (!pImpl->pStbCtrlFac)
235 pImpl->pStbCtrlFac = new SfxStbCtrlFactArr_Impl;
237 #ifdef DBG_UTIL
238 for ( sal_uInt16 n=0; n<pImpl->pStbCtrlFac->size(); n++ )
240 SfxStbCtrlFactory *pF = (*pImpl->pStbCtrlFac)[n];
241 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
242 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
244 DBG_WARNING("TbxController-Registering is not clearly defined!");
247 #endif
249 pImpl->pStbCtrlFac->push_back( pFact );
252 //-------------------------------------------------------------------------
254 void SfxModule::RegisterMenuControl( SfxMenuCtrlFactory *pFact )
256 if (!pImpl->pMenuCtrlFac)
257 pImpl->pMenuCtrlFac = new SfxMenuCtrlFactArr_Impl;
259 #ifdef DBG_UTIL
260 for ( sal_uInt16 n=0; n<pImpl->pMenuCtrlFac->size(); n++ )
262 SfxMenuCtrlFactory *pF = (*pImpl->pMenuCtrlFac)[n];
263 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
264 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
266 DBG_WARNING("MenuController-Registering is not clearly defined!");
269 #endif
271 pImpl->pMenuCtrlFac->push_back( pFact );
274 //-------------------------------------------------------------------------
276 SfxTbxCtrlFactArr_Impl* SfxModule::GetTbxCtrlFactories_Impl() const
278 return pImpl->pTbxCtrlFac;
281 //-------------------------------------------------------------------------
283 SfxStbCtrlFactArr_Impl* SfxModule::GetStbCtrlFactories_Impl() const
285 return pImpl->pStbCtrlFac;
288 //-------------------------------------------------------------------------
290 SfxMenuCtrlFactArr_Impl* SfxModule::GetMenuCtrlFactories_Impl() const
292 return pImpl->pMenuCtrlFac;
295 //-------------------------------------------------------------------------
297 SfxChildWinFactArr_Impl* SfxModule::GetChildWinFactories_Impl() const
299 return pImpl->pFactArr;
302 ImageList* SfxModule::GetImageList_Impl( sal_Bool bBig )
304 return pImpl->GetImageList( pResMgr, bBig );
307 SfxTabPage* SfxModule::CreateTabPage( sal_uInt16, Window*, const SfxItemSet& )
309 return NULL;
312 SfxModuleArr_Impl& SfxModule::GetModules_Impl()
314 if( !pModules )
315 pModules = new SfxModuleArr_Impl;
316 return *pModules;
319 void SfxModule::DestroyModules_Impl()
321 if ( pModules )
323 SfxModuleArr_Impl& rModules = *pModules;
324 for( sal_uInt16 nPos = rModules.size(); nPos--; )
326 SfxModule* pMod = rModules[nPos];
327 delete pMod;
329 delete pModules, pModules = 0;
333 void SfxModule::Invalidate( sal_uInt16 nId )
335 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
336 if ( pFrame->GetObjectShell()->GetModule() == this )
337 Invalidate_Impl( pFrame->GetBindings(), nId );
340 bool SfxModule::IsChildWindowAvailable( const sal_uInt16 i_nId, const SfxViewFrame* i_pViewFrame ) const
342 if ( i_nId != SID_TASKPANE )
343 // by default, assume it is
344 return true;
346 const SfxViewFrame* pViewFrame = i_pViewFrame ? i_pViewFrame : GetFrame();
347 ENSURE_OR_RETURN( pViewFrame, "SfxModule::IsChildWindowAvailable: no frame to ask for the module identifier!", false );
348 return ::sfx2::ModuleTaskPane::ModuleHasToolPanels( pViewFrame->GetFrame().GetFrameInterface() );
351 SfxModule* SfxModule::GetActiveModule( SfxViewFrame* pFrame )
353 if ( !pFrame )
354 pFrame = SfxViewFrame::Current();
355 SfxObjectShell* pSh = 0;
356 if( pFrame )
357 pSh = pFrame->GetObjectShell();
358 return pSh ? pSh->GetModule() : 0;
361 FieldUnit SfxModule::GetModuleFieldUnit( ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > const & i_frame )
363 ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FUNIT_100TH_MM );
365 // find SfxViewFrame for the given XFrame
366 SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
367 while ( pViewFrame != NULL )
369 if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
370 break;
371 pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
373 ENSURE_OR_RETURN( pViewFrame != NULL, "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame", FUNIT_100TH_MM );
375 // find the module
376 SfxModule const * pModule = GetActiveModule( pViewFrame );
377 ENSURE_OR_RETURN( pModule != NULL, "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!", FUNIT_100TH_MM );
378 if ( pModule )
379 return pModule->GetFieldUnit();
380 return FUNIT_INCH;
383 FieldUnit SfxModule::GetCurrentFieldUnit()
385 FieldUnit eUnit = FUNIT_INCH;
386 SfxModule* pModule = GetActiveModule();
387 if ( pModule )
389 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
390 if ( pItem )
391 eUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
393 else
394 SAL_WARN( "sfx.appl", "GetModuleFieldUnit(): no module found" );
395 return eUnit;
398 FieldUnit SfxModule::GetFieldUnit() const
400 FieldUnit eUnit = FUNIT_INCH;
401 const SfxPoolItem* pItem = GetItem( SID_ATTR_METRIC );
402 if ( pItem )
403 eUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
404 return eUnit;
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */