LibreOffice Module sfx2 (master) 1
module.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sfx2/module.hxx>
21#include <sfx2/app.hxx>
22#include <sfx2/msgpool.hxx>
23#include <sfx2/tbxctrl.hxx>
24#include <sfx2/stbitem.hxx>
25#include <sfx2/childwin.hxx>
26#include <sfx2/docfac.hxx>
27#include <sfx2/objface.hxx>
28#include <sfx2/viewfrm.hxx>
29#include <sfx2/tabdlg.hxx>
30#include <sfx2/sfxsids.hrc>
31#include <svl/intitem.hxx>
33#include <unotools/resmgr.hxx>
34#include <sal/log.hxx>
35
36#define ShellClass_SfxModule
37#include <sfxslots.hxx>
38#include <optional>
39
41{
42public:
43
44 std::optional<SfxSlotPool> pSlotPool;
45 std::vector<SfxTbxCtrlFactory> maTbxCtrlFactories;
46 std::vector<SfxStbCtrlFactory> maStbCtrlFactories;
47 std::vector<SfxChildWinFactory> maFactories;
48 OString maResName;
49
52};
53
55{
56}
57
59{
60 pSlotPool.reset();
61 maTbxCtrlFactories.clear();
62 maStbCtrlFactories.clear();
63}
64
66
67SfxModule::SfxModule(const OString& rResName, std::initializer_list<SfxObjectFactory*> pFactoryList)
68 : pImpl(nullptr)
69{
70 Construct_Impl(rResName);
71 for (auto pFactory : pFactoryList)
72 {
73 if (pFactory)
74 pFactory->SetModule_Impl( this );
75 }
76}
77
78void SfxModule::Construct_Impl(const OString& rResName)
79{
82 pImpl->pSlotPool.emplace(&pApp->GetAppSlotPool_Impl());
83 pImpl->maResName = rResName;
84
85 SetPool( &pApp->GetPool() );
86}
87
89{
90 //TODO how to silence useuniqueptr
91 if (true)
92 {
93 delete pImpl;
94 }
95}
96
97std::locale SfxModule::GetResLocale() const
98{
100}
101
103{
104 return &*pImpl->pSlotPool;
105}
106
107
109{
110 DBG_ASSERT( pImpl, "No real Module!" );
111
112 for (size_t nFactory=0; nFactory<pImpl->maFactories.size(); ++nFactory)
113 {
114 if (rFact.nId == pImpl->maFactories[nFactory].nId)
115 {
116 pImpl->maFactories.erase( pImpl->maFactories.begin() + nFactory );
117 SAL_WARN("sfx.appl", "ChildWindow registered multiple times!");
118 return;
119 }
120 }
121
122 pImpl->maFactories.push_back( rFact );
123}
124
125
127{
128#ifdef DBG_UTIL
129 for ( size_t n=0; n<pImpl->maTbxCtrlFactories.size(); n++ )
130 {
132 if ( pF->nTypeId == rFact.nTypeId &&
133 (pF->nSlotId == rFact.nSlotId || pF->nSlotId == 0) )
134 {
135 SAL_INFO("sfx.appl", "TbxController-Registering is not clearly defined!");
136 }
137 }
138#endif
139
140 pImpl->maTbxCtrlFactories.push_back( rFact );
141}
142
143
145{
146#ifdef DBG_UTIL
147 for ( size_t n=0; n<pImpl->maStbCtrlFactories.size(); n++ )
148 {
150 if ( pF->nTypeId == rFact.nTypeId &&
151 (pF->nSlotId == rFact.nSlotId || pF->nSlotId == 0) )
152 {
153 SAL_INFO("sfx.appl", "TbxController-Registering is not clearly defined!");
154 }
155 }
156#endif
157
158 pImpl->maStbCtrlFactories.push_back( rFact );
159}
160
161
162SfxTbxCtrlFactory* SfxModule::GetTbxCtrlFactory(const std::type_info& rSlotType, sal_uInt16 nSlotID) const
163{
164 // search for a factory with the given slot id
165 for (auto& rFactory : pImpl->maTbxCtrlFactories)
166 if( rFactory.nTypeId == rSlotType && rFactory.nSlotId == nSlotID )
167 return &rFactory;
168
169 // if no factory exists for the given slot id, see if we
170 // have a generic factory with the correct slot type and slot id == 0
171 for (auto& rFactory : pImpl->maTbxCtrlFactories)
172 if( rFactory.nTypeId == rSlotType && rFactory.nSlotId == 0 )
173 return &rFactory;
174
175 return nullptr;
176}
177
178
179SfxStbCtrlFactory* SfxModule::GetStbCtrlFactory(const std::type_info& rSlotType, sal_uInt16 nSlotID) const
180{
181 for (auto& rFactory : pImpl->maStbCtrlFactories)
182 if ( rFactory.nTypeId == rSlotType &&
183 ( rFactory.nSlotId == 0 || rFactory.nSlotId == nSlotID ) )
184 return &rFactory;
185 return nullptr;
186}
187
189{
190 for (auto& rFactory : pImpl->maFactories)
191 if (rFactory.nId == nId)
192 return &rFactory;
193 return nullptr;
194}
195
196std::unique_ptr<SfxTabPage> SfxModule::CreateTabPage(sal_uInt16, weld::Container*, weld::DialogController*, const SfxItemSet&)
197{
198 return nullptr;
199}
200
201void SfxModule::Invalidate( sal_uInt16 nId )
202{
203 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
204 if ( pFrame->GetObjectShell()->GetModule() == this )
205 Invalidate_Impl( pFrame->GetBindings(), nId );
206}
207
209{
210 if ( !pFrame )
211 pFrame = SfxViewFrame::Current();
212 SfxObjectShell* pSh = nullptr;
213 if( pFrame )
214 pSh = pFrame->GetObjectShell();
215 return pSh ? pSh->GetModule() : nullptr;
216}
217
218FieldUnit SfxModule::GetModuleFieldUnit( css::uno::Reference< css::frame::XFrame > const & i_frame )
219{
220 ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FieldUnit::MM_100TH );
221
222 // find SfxViewFrame for the given XFrame
223 SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
224 while ( pViewFrame != nullptr )
225 {
226 if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
227 break;
228 pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
229 }
231 pViewFrame != nullptr,
232 "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame",
233 FieldUnit::MM_100TH);
234
235 // find the module
236 SfxModule const * pModule = GetActiveModule( pViewFrame );
237 ENSURE_OR_RETURN(pModule != nullptr,
238 "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!",
239 FieldUnit::MM_100TH);
240 return pModule->GetFieldUnit();
241}
242
244{
245 FieldUnit eUnit = FieldUnit::INCH;
246 SfxModule* pModule = GetActiveModule();
247 if ( pModule )
248 {
249 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
250 if ( pItem )
251 eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
252 }
253 else
254 SAL_WARN( "sfx.appl", "GetModuleFieldUnit(): no module found" );
255 return eUnit;
256}
257
259{
260 FieldUnit eUnit = FieldUnit::INCH;
261 const SfxPoolItem* pItem = GetItem( SID_ATTR_METRIC );
262 if ( pItem )
263 eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
264 return eUnit;
265}
266
267/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE SfxSlotPool & GetAppSlotPool_Impl() const
Definition: appmisc.cxx:104
static SfxApplication * GetOrCreate()
Definition: app.cxx:89
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
Definition: frame.cxx:515
std::vector< SfxChildWinFactory > maFactories
Definition: module.cxx:47
OString maResName
Definition: module.cxx:48
std::vector< SfxTbxCtrlFactory > maTbxCtrlFactories
Definition: module.cxx:45
std::vector< SfxStbCtrlFactory > maStbCtrlFactories
Definition: module.cxx:46
std::optional< SfxSlotPool > pSlotPool
Definition: module.cxx:44
virtual void Invalidate(sal_uInt16 nId=0) override
With this method can the slots of the subclasses be invalidated through the slot Id or alternatively ...
Definition: module.cxx:201
static SfxModule * GetActiveModule(SfxViewFrame *pFrame=nullptr)
Definition: module.cxx:208
static FieldUnit GetModuleFieldUnit(css::uno::Reference< css::frame::XFrame > const &i_frame)
retrieves the field unit of the module belonging to the document displayed in the given frame
Definition: module.cxx:218
SfxModule_Impl * pImpl
Definition: module.hxx:54
SAL_DLLPRIVATE SfxStbCtrlFactory * GetStbCtrlFactory(const std::type_info &rSlotType, sal_uInt16 nSlotID) const
Definition: module.cxx:179
SAL_DLLPRIVATE SfxChildWinFactory * GetChildWinFactoryById(sal_uInt16 nId) const
Definition: module.cxx:188
SAL_DLLPRIVATE void Construct_Impl(const OString &rResName)
Definition: module.cxx:78
void RegisterToolBoxControl(const SfxTbxCtrlFactory &)
Definition: module.cxx:126
SAL_DLLPRIVATE SfxTbxCtrlFactory * GetTbxCtrlFactory(const std::type_info &rSlotType, sal_uInt16 nSlotID) const
Definition: module.cxx:162
FieldUnit GetFieldUnit() const
Definition: module.cxx:258
void RegisterStatusBarControl(const SfxStbCtrlFactory &)
Definition: module.cxx:144
virtual std::unique_ptr< SfxTabPage > CreateTabPage(sal_uInt16 nId, weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: module.cxx:196
SfxSlotPool * GetSlotPool() const
Definition: module.cxx:102
static FieldUnit GetCurrentFieldUnit()
Definition: module.cxx:243
std::locale GetResLocale() const
Definition: module.cxx:97
virtual ~SfxModule() override
Definition: module.cxx:88
void RegisterChildWindow(const SfxChildWinFactory &)
Definition: module.cxx:108
SfxModule * GetModule() const
Definition: objmisc.cxx:1317
The class SfxShell is the base class for all classes, which provide the functionality of the form <Sl...
Definition: shell.hxx:128
SfxItemPool & GetPool() const
Each Subclass of SfxShell must reference a pool.
Definition: shell.hxx:511
SAL_DLLPRIVATE void Invalidate_Impl(SfxBindings &rBindings, sal_uInt16 nId)
Definition: shell.cxx:251
const SfxPoolItem * GetItem(sal_uInt16 nSlotId) const
With this method any objects of <SfxPoolItemu> subclasses can be accessed.
Definition: shell.cxx:144
void SetPool(SfxItemPool *pNewPool)
With this method, the subclasses register their special <SfxItemPool> in the SfxShell.
Definition: shell.hxx:525
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
Definition: viewfrm.cxx:1958
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetNext(const SfxViewFrame &rPrev, const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
Definition: viewfrm.cxx:1989
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetFirst(const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
Definition: viewfrm.cxx:1966
SfxFrame & GetFrame() const
Definition: viewfrm.cxx:2765
virtual SfxObjectShell * GetObjectShell() override
Definition: viewfrm.cxx:2201
#define DBG_ASSERT(sCon, aError)
#define ENSURE_OR_RETURN(c, m, r)
FieldUnit
sal_Int64 n
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
std::locale Create(std::string_view aPrefixName, const LanguageTag &rLocale)
sal_Int16 nId
const char GetValue[]
#define SFX_IMPL_SUPERCLASS_INTERFACE(Class, SuperClass)
Definition: shell.hxx:570
sal_uInt16 nId
Definition: childwin.hxx:89
const std::type_info & nTypeId
Definition: stbitem.hxx:42
sal_uInt16 nSlotId
Definition: stbitem.hxx:43
sal_uInt16 nSlotId
Definition: tbxctrl.hxx:47
const std::type_info & nTypeId
Definition: tbxctrl.hxx:46