Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / appl / module.cxx
blobc87e2713581baa292236b359ea55a3fec46b9554
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 <tools/rcid.h>
22 #include <cstdarg>
23 #include <sfx2/module.hxx>
24 #include <sfx2/app.hxx>
25 #include <sfx2/sfxresid.hxx>
26 #include <sfx2/msgpool.hxx>
27 #include <sfx2/tbxctrl.hxx>
28 #include <sfx2/stbitem.hxx>
29 #include <sfx2/mnuitem.hxx>
30 #include <sfx2/childwin.hxx>
31 #include <sfx2/mnumgr.hxx>
32 #include <sfx2/docfac.hxx>
33 #include <sfx2/objface.hxx>
34 #include <sfx2/viewfrm.hxx>
35 #include <sfx2/tabdlg.hxx>
36 #include <svl/intitem.hxx>
37 #include <sfx2/taskpane.hxx>
38 #include <tools/diagnose_ex.h>
39 #include <rtl/strbuf.hxx>
40 #include <sal/log.hxx>
42 #define SfxModule
43 #include "sfxslots.hxx"
44 #include "childwinimpl.hxx"
45 #include <ctrlfactoryimpl.hxx>
47 class SfxModuleArr_Impl
49 typedef ::std::vector<SfxModule*> DataType;
50 DataType maData;
51 public:
53 typedef DataType::iterator iterator;
55 iterator begin()
57 return maData.begin();
60 void erase( iterator it )
62 maData.erase(it);
65 SfxModule* operator[] ( size_t i )
67 return maData[i];
70 void push_back( SfxModule* p )
72 maData.push_back(p);
75 size_t size() const
77 return maData.size();
81 static SfxModuleArr_Impl* pModules=0;
83 class SfxModule_Impl
85 public:
87 SfxSlotPool* pSlotPool;
88 SfxTbxCtrlFactArr_Impl* pTbxCtrlFac;
89 SfxStbCtrlFactArr_Impl* pStbCtrlFac;
90 SfxMenuCtrlFactArr_Impl* pMenuCtrlFac;
91 SfxChildWinFactArr_Impl* pFactArr;
92 ImageList* pImgListSmall;
93 ImageList* pImgListBig;
95 SfxModule_Impl();
96 ~SfxModule_Impl();
97 ImageList* GetImageList( ResMgr* pResMgr, bool bBig );
100 SfxModule_Impl::SfxModule_Impl()
101 : pSlotPool(0), pTbxCtrlFac(0), pStbCtrlFac(0), pMenuCtrlFac(0), pFactArr(0), pImgListSmall(0), pImgListBig(0)
105 SfxModule_Impl::~SfxModule_Impl()
107 delete pSlotPool;
108 delete pTbxCtrlFac;
109 delete pStbCtrlFac;
110 delete pMenuCtrlFac;
111 delete pFactArr;
112 delete pImgListSmall;
113 delete pImgListBig;
116 ImageList* SfxModule_Impl::GetImageList( ResMgr* pResMgr, bool bBig )
118 ImageList*& rpList = bBig ? pImgListBig : pImgListSmall;
119 if ( !rpList )
121 ResId aResId( bBig ? ( RID_DEFAULTIMAGELIST_LC ) : ( RID_DEFAULTIMAGELIST_SC ), *pResMgr );
123 aResId.SetRT( RSC_IMAGELIST );
125 DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
127 if ( pResMgr->IsAvailable(aResId) )
128 rpList = new ImageList( aResId );
129 else
130 rpList = new ImageList();
133 return rpList; }
135 TYPEINIT1(SfxModule, SfxShell);
137 SFX_IMPL_SUPERCLASS_INTERFACE(SfxModule, SfxShell)
139 ResMgr* SfxModule::GetResMgr()
141 return pResMgr;
144 SfxModule::SfxModule( ResMgr* pMgrP, bool bDummyP,
145 SfxObjectFactory* pFactoryP, ... )
146 : pResMgr( pMgrP ), bDummy( bDummyP ), pImpl(0L)
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);
157 void SfxModule::Construct_Impl()
159 if( !bDummy )
161 SfxApplication *pApp = SfxGetpApp();
162 SfxModuleArr_Impl& rArr = GetModules_Impl();
163 SfxModule* pPtr = this;
164 rArr.push_back( pPtr );
165 pImpl = new SfxModule_Impl;
166 pImpl->pSlotPool = new SfxSlotPool(&pApp->GetAppSlotPool_Impl());
168 pImpl->pTbxCtrlFac=0;
169 pImpl->pStbCtrlFac=0;
170 pImpl->pMenuCtrlFac=0;
171 pImpl->pFactArr=0;
172 pImpl->pImgListSmall=0;
173 pImpl->pImgListBig=0;
175 SetPool( &pApp->GetPool() );
181 SfxModule::~SfxModule()
183 if( !bDummy )
185 if ( SfxGetpApp()->Get_Impl() )
187 // The module will be destroyed before the Deinitialize,
188 // so remove from the array
189 SfxModuleArr_Impl& rArr = GetModules_Impl();
190 for( sal_uInt16 nPos = rArr.size(); nPos--; )
192 if( rArr[ nPos ] == this )
194 rArr.erase( rArr.begin() + nPos );
195 break;
199 delete pImpl;
202 delete pResMgr;
208 SfxSlotPool* SfxModule::GetSlotPool() const
210 return pImpl->pSlotPool;
215 void SfxModule::RegisterChildWindow(SfxChildWinFactory *pFact)
217 DBG_ASSERT( pImpl, "No real Module!" );
219 if (!pImpl->pFactArr)
220 pImpl->pFactArr = new SfxChildWinFactArr_Impl;
222 for (sal_uInt16 nFactory=0; nFactory<pImpl->pFactArr->size(); ++nFactory)
224 if (pFact->nId == (*pImpl->pFactArr)[nFactory].nId)
226 pImpl->pFactArr->erase( pImpl->pFactArr->begin() + nFactory );
227 SAL_WARN("sfx.appl", "ChildWindow registered multiple times!");
228 return;
232 pImpl->pFactArr->push_back( pFact );
237 void SfxModule::RegisterToolBoxControl( SfxTbxCtrlFactory *pFact )
239 if (!pImpl->pTbxCtrlFac)
240 pImpl->pTbxCtrlFac = new SfxTbxCtrlFactArr_Impl;
242 #ifdef DBG_UTIL
243 for ( sal_uInt16 n=0; n<pImpl->pTbxCtrlFac->size(); n++ )
245 SfxTbxCtrlFactory *pF = &(*pImpl->pTbxCtrlFac)[n];
246 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
247 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
249 DBG_WARNING("TbxController-Registering is not clearly defined!");
252 #endif
254 pImpl->pTbxCtrlFac->push_back( pFact );
259 void SfxModule::RegisterStatusBarControl( SfxStbCtrlFactory *pFact )
261 if (!pImpl->pStbCtrlFac)
262 pImpl->pStbCtrlFac = new SfxStbCtrlFactArr_Impl;
264 #ifdef DBG_UTIL
265 for ( sal_uInt16 n=0; n<pImpl->pStbCtrlFac->size(); n++ )
267 SfxStbCtrlFactory *pF = &(*pImpl->pStbCtrlFac)[n];
268 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
269 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
271 DBG_WARNING("TbxController-Registering is not clearly defined!");
274 #endif
276 pImpl->pStbCtrlFac->push_back( pFact );
281 void SfxModule::RegisterMenuControl( SfxMenuCtrlFactory *pFact )
283 if (!pImpl->pMenuCtrlFac)
284 pImpl->pMenuCtrlFac = new SfxMenuCtrlFactArr_Impl;
286 #ifdef DBG_UTIL
287 for ( sal_uInt16 n=0; n<pImpl->pMenuCtrlFac->size(); n++ )
289 SfxMenuCtrlFactory *pF = &(*pImpl->pMenuCtrlFac)[n];
290 if ( pF->nTypeId && pF->nTypeId == pFact->nTypeId &&
291 (pF->nSlotId == pFact->nSlotId || pF->nSlotId == 0) )
293 DBG_WARNING("MenuController-Registering is not clearly defined!");
296 #endif
298 pImpl->pMenuCtrlFac->push_back( pFact );
303 SfxTbxCtrlFactArr_Impl* SfxModule::GetTbxCtrlFactories_Impl() const
305 return pImpl->pTbxCtrlFac;
310 SfxStbCtrlFactArr_Impl* SfxModule::GetStbCtrlFactories_Impl() const
312 return pImpl->pStbCtrlFac;
317 SfxMenuCtrlFactArr_Impl* SfxModule::GetMenuCtrlFactories_Impl() const
319 return pImpl->pMenuCtrlFac;
324 SfxChildWinFactArr_Impl* SfxModule::GetChildWinFactories_Impl() const
326 return pImpl->pFactArr;
329 ImageList* SfxModule::GetImageList_Impl( bool bBig )
331 return pImpl->GetImageList( pResMgr, bBig );
334 VclPtr<SfxTabPage> SfxModule::CreateTabPage( sal_uInt16, vcl::Window*, const SfxItemSet& )
336 return VclPtr<SfxTabPage>();
339 SfxModuleArr_Impl& SfxModule::GetModules_Impl()
341 if( !pModules )
342 pModules = new SfxModuleArr_Impl;
343 return *pModules;
346 void SfxModule::DestroyModules_Impl()
348 if ( pModules )
350 SfxModuleArr_Impl& rModules = *pModules;
351 for( sal_uInt16 nPos = rModules.size(); nPos--; )
353 SfxModule* pMod = rModules[nPos];
354 delete pMod;
356 delete pModules, pModules = 0;
360 void SfxModule::Invalidate( sal_uInt16 nId )
362 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
363 if ( pFrame->GetObjectShell()->GetModule() == this )
364 Invalidate_Impl( pFrame->GetBindings(), nId );
367 bool SfxModule::IsChildWindowAvailable( const sal_uInt16 i_nId, const SfxViewFrame* i_pViewFrame ) const
369 if ( i_nId != SID_TASKPANE )
370 // by default, assume it is
371 return true;
373 const SfxViewFrame* pViewFrame = i_pViewFrame ? i_pViewFrame : GetFrame();
374 ENSURE_OR_RETURN( pViewFrame, "SfxModule::IsChildWindowAvailable: no frame to ask for the module identifier!", false );
375 return ::sfx2::ModuleTaskPane::ModuleHasToolPanels( pViewFrame->GetFrame().GetFrameInterface() );
378 SfxModule* SfxModule::GetActiveModule( SfxViewFrame* pFrame )
380 if ( !pFrame )
381 pFrame = SfxViewFrame::Current();
382 SfxObjectShell* pSh = 0;
383 if( pFrame )
384 pSh = pFrame->GetObjectShell();
385 return pSh ? pSh->GetModule() : 0;
388 FieldUnit SfxModule::GetModuleFieldUnit( ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > const & i_frame )
390 ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FUNIT_100TH_MM );
392 // find SfxViewFrame for the given XFrame
393 SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
394 while ( pViewFrame != NULL )
396 if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
397 break;
398 pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
400 ENSURE_OR_RETURN( pViewFrame != NULL, "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame", FUNIT_100TH_MM );
402 // find the module
403 SfxModule const * pModule = GetActiveModule( pViewFrame );
404 ENSURE_OR_RETURN( pModule != NULL, "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!", FUNIT_100TH_MM );
405 return pModule->GetFieldUnit();
408 FieldUnit SfxModule::GetCurrentFieldUnit()
410 FieldUnit eUnit = FUNIT_INCH;
411 SfxModule* pModule = GetActiveModule();
412 if ( pModule )
414 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
415 if ( pItem )
416 eUnit = (FieldUnit) static_cast<const SfxUInt16Item*>(pItem)->GetValue();
418 else
419 SAL_WARN( "sfx.appl", "GetModuleFieldUnit(): no module found" );
420 return eUnit;
423 FieldUnit SfxModule::GetFieldUnit() const
425 FieldUnit eUnit = FUNIT_INCH;
426 const SfxPoolItem* pItem = GetItem( SID_ATTR_METRIC );
427 if ( pItem )
428 eUnit = (FieldUnit) static_cast<const SfxUInt16Item*>(pItem)->GetValue();
429 return eUnit;
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */