Line data Source code
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 <tools/stream.hxx>
21 : #include <rsc/rscsfx.hxx>
22 :
23 : // due to pSlotPool
24 : #include "appdata.hxx"
25 : #include <sfx2/msgpool.hxx>
26 : #include <sfx2/msg.hxx>
27 : #include <sfx2/app.hxx>
28 : #include <sfx2/objface.hxx>
29 : #include "sfxtypes.hxx"
30 : #include <sfx2/sfxresid.hxx>
31 : #include "arrdecl.hxx"
32 : #include <sfx2/module.hxx>
33 :
34 : #include <sfx2/sfx.hrc>
35 :
36 354 : SfxSlotPool::SfxSlotPool(SfxSlotPool *pParent)
37 : : _pGroups(0)
38 : , _pParentPool( pParent )
39 : , _pInterfaces(0)
40 : , _nCurGroup(0)
41 : , _nCurInterface(0)
42 354 : , _nCurMsg(0)
43 : {
44 354 : }
45 :
46 174 : SfxSlotPool::~SfxSlotPool()
47 : {
48 174 : _pParentPool = 0;
49 2142 : for ( SfxInterface *pIF = FirstInterface(); pIF; pIF = FirstInterface() )
50 1968 : delete pIF;
51 174 : delete _pInterfaces;
52 174 : delete _pGroups;
53 174 : }
54 :
55 :
56 : // registers the availability of the Interface of functions
57 :
58 4431 : void SfxSlotPool::RegisterInterface( SfxInterface& rInterface )
59 : {
60 : // add to the list of SfxObjectInterface instances
61 4431 : if ( _pInterfaces == NULL )
62 354 : _pInterfaces = new SfxInterfaceArr_Impl;
63 4431 : _pInterfaces->push_back(&rInterface);
64 :
65 : // Stop at a (single) Null-slot (for syntactic reasons the interfaces
66 : // always contain at least one slot)
67 4431 : if ( rInterface.Count() != 0 && !rInterface[0]->nSlotId )
68 5141 : return;
69 :
70 : // possibly add Interface-id and group-ids of funcs to the list of groups
71 3721 : if ( !_pGroups )
72 : {
73 354 : _pGroups = new SfxSlotGroupArr_Impl;
74 :
75 354 : if ( _pParentPool )
76 : {
77 : // The Groups in parent Slotpool are also known here
78 146 : _pGroups->append( *_pParentPool->_pGroups );
79 : }
80 : }
81 :
82 249092 : for ( size_t nFunc = 0; nFunc < rInterface.Count(); ++nFunc )
83 : {
84 245371 : SfxSlot *pDef = rInterface[nFunc];
85 484945 : if ( pDef->GetGroupId() && /* pDef->GetGroupId() != GID_INTERN && */
86 239574 : _pGroups->find(pDef->GetGroupId()) == SfxSlotGroupArr_Impl::npos )
87 : {
88 4083 : if (pDef->GetGroupId() == GID_INTERN)
89 208 : _pGroups->insert(_pGroups->begin(), pDef->GetGroupId());
90 : else
91 3875 : _pGroups->push_back(pDef->GetGroupId());
92 : }
93 : }
94 : }
95 :
96 :
97 :
98 70472 : TypeId SfxSlotPool::GetSlotType( sal_uInt16 nId ) const
99 : {
100 70472 : const SfxSlot* pSlot = (const_cast <SfxSlotPool*> (this))->GetSlot( nId );
101 70472 : return pSlot ? pSlot->GetType()->Type() : 0;
102 : }
103 :
104 :
105 : // unregisters the availability of the Interface of functions
106 :
107 1968 : void SfxSlotPool::ReleaseInterface( SfxInterface& rInterface )
108 : {
109 : SAL_WARN_IF(!_pInterfaces, "sfx.control", "releasing SfxInterface, but there are none");
110 1968 : if (!_pInterfaces)
111 1968 : return ;
112 :
113 : // remove from the list of SfxInterface instances
114 1968 : SfxInterfaceArr_Impl::iterator i = std::find(_pInterfaces->begin(), _pInterfaces->end(), &rInterface);
115 1968 : if(i != _pInterfaces->end())
116 1968 : _pInterfaces->erase(i);
117 : }
118 :
119 : // get the first SfxMessage for a special Id (e.g. for getting check-mode)
120 :
121 533767 : const SfxSlot* SfxSlotPool::GetSlot( sal_uInt16 nId )
122 : {
123 : SAL_WARN_IF(!_pInterfaces, "sfx.control", "no Interfaces registered");
124 533767 : if (!_pInterfaces)
125 0 : return 0;
126 :
127 : // First, search their own interfaces
128 6348142 : for ( size_t nInterf = 0; nInterf < _pInterfaces->size(); ++nInterf )
129 : {
130 6204764 : const SfxSlot *pDef = ((*_pInterfaces)[nInterf])->GetSlot(nId);
131 6204764 : if ( pDef )
132 390389 : return pDef;
133 : }
134 :
135 : // Then try any of the possible existing parent
136 143378 : return _pParentPool ? _pParentPool->GetSlot( nId ) : 0;
137 : }
138 :
139 :
140 :
141 : // skips to the next group
142 :
143 0 : OUString SfxSlotPool::SeekGroup( sal_uInt16 nNo )
144 : {
145 : SAL_WARN_IF(!_pInterfaces, "sfx.control", "no Interfaces registered");
146 :
147 : // if the group exists, use it
148 0 : if ( _pGroups && nNo < _pGroups->size() )
149 : {
150 0 : _nCurGroup = nNo;
151 0 : if ( _pParentPool )
152 : {
153 : // In most cases, the order of the IDs agree
154 0 : sal_uInt16 nParentCount = _pParentPool->_pGroups->size();
155 0 : if ( nNo < nParentCount && (*_pGroups)[nNo] == (*_pParentPool->_pGroups)[nNo] )
156 0 : _pParentPool->_nCurGroup = nNo;
157 : else
158 : {
159 : // Otherwise search. If the group is not found in the parent
160 : // pool, _nCurGroup is set outside the valid range
161 : sal_uInt16 i;
162 0 : for ( i=1; i<nParentCount; i++ )
163 0 : if ( (*_pGroups)[nNo] == (*_pParentPool->_pGroups)[i] )
164 0 : break;
165 0 : _pParentPool->_nCurGroup = i;
166 : }
167 : }
168 :
169 0 : SfxResId aResId( (*_pGroups)[_nCurGroup] );
170 0 : aResId.SetRT(RSC_STRING);
171 0 : if ( !SfxResId::GetResMgr()->IsAvailable(aResId) )
172 : {
173 : OSL_FAIL( "GroupId-Name not defined in SFX!" );
174 0 : return OUString();
175 : }
176 :
177 0 : return aResId.toString();
178 : }
179 :
180 0 : return OUString();
181 : }
182 :
183 :
184 :
185 :
186 0 : sal_uInt16 SfxSlotPool::GetGroupCount()
187 : {
188 0 : return _pGroups->size();
189 : }
190 :
191 :
192 :
193 :
194 : // internal search loop
195 :
196 0 : const SfxSlot* SfxSlotPool::SeekSlot( sal_uInt16 nStartInterface )
197 : {
198 : SAL_WARN_IF(!_pInterfaces, "sfx.control", "no Interfaces registered");
199 0 : if (!_pInterfaces)
200 0 : return 0;
201 :
202 : // The numbering starts at the interfaces of the parent pool
203 0 : sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_pInterfaces->size() : 0;
204 :
205 : // have reached the end of the Parent-Pools?
206 0 : if ( nStartInterface < nFirstInterface &&
207 0 : _pParentPool->_nCurGroup >= _pParentPool->_pGroups->size() )
208 0 : nStartInterface = nFirstInterface;
209 :
210 : // Is the Interface still in the Parent-Pool?
211 0 : if ( nStartInterface < nFirstInterface )
212 : {
213 : SAL_WARN_IF(!_pParentPool, "sfx.control", "No parent pool!");
214 0 : _nCurInterface = nStartInterface;
215 0 : return _pParentPool->SeekSlot( nStartInterface );
216 : }
217 :
218 : // find the first func-def with the current group id
219 0 : sal_uInt16 nCount = _pInterfaces->size() + nFirstInterface;
220 0 : for ( _nCurInterface = nStartInterface;
221 0 : _nCurInterface < nCount;
222 : ++_nCurInterface )
223 : {
224 0 : SfxInterface* pInterface = (*_pInterfaces)[_nCurInterface-nFirstInterface];
225 0 : for ( _nCurMsg = 0;
226 0 : _nCurMsg < pInterface->Count();
227 : ++_nCurMsg )
228 : {
229 0 : const SfxSlot* pMsg = (*pInterface)[_nCurMsg];
230 0 : if ( pMsg->GetGroupId() == _pGroups->at(_nCurGroup) )
231 0 : return pMsg;
232 : }
233 : }
234 :
235 0 : return 0;
236 : }
237 :
238 :
239 : // skips to the next func in the current group
240 :
241 0 : const SfxSlot* SfxSlotPool::NextSlot()
242 : {
243 : SAL_WARN_IF(!_pInterfaces, "sfx.control", "no Interfaces registered");
244 0 : if (!_pInterfaces)
245 0 : return 0;
246 :
247 : // The numbering starts at the interfaces of the parent pool
248 0 : sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_pInterfaces->size() : 0;
249 :
250 0 : if ( _nCurInterface < nFirstInterface && _nCurGroup >= _pParentPool->_pGroups->size() )
251 0 : _nCurInterface = nFirstInterface;
252 :
253 0 : if ( _nCurInterface < nFirstInterface )
254 : {
255 : SAL_WARN_IF(!_pParentPool, "sfx.control", "No parent pool!");
256 0 : const SfxSlot *pSlot = _pParentPool->NextSlot();
257 0 : _nCurInterface = _pParentPool->_nCurInterface;
258 0 : if ( pSlot )
259 0 : return pSlot;
260 0 : if ( _nCurInterface == nFirstInterface )
261 : // parent pool is ready
262 0 : return SeekSlot( nFirstInterface );
263 : }
264 :
265 0 : sal_uInt16 nInterface = _nCurInterface - nFirstInterface;
266 : // possibly we are already at the end
267 0 : if ( nInterface >= _pInterfaces->size() )
268 0 : return 0;
269 :
270 : // look for further matching func-defs within the same Interface
271 0 : SfxInterface* pInterface = (*_pInterfaces)[nInterface];
272 0 : while ( ++_nCurMsg < pInterface->Count() )
273 : {
274 0 : SfxSlot* pMsg = (*pInterface)[_nCurMsg];
275 0 : if ( pMsg->GetGroupId() == _pGroups->at(_nCurGroup) )
276 0 : return pMsg;
277 : }
278 :
279 0 : return SeekSlot(++_nCurInterface );
280 : }
281 :
282 :
283 :
284 : // Query SlotName with help text
285 :
286 :
287 2142 : SfxInterface* SfxSlotPool::FirstInterface()
288 : {
289 2142 : _nCurInterface = 0;
290 2142 : if ( !_pInterfaces || !_pInterfaces->size() )
291 174 : return 0;
292 1968 : return _pParentPool ? _pParentPool->FirstInterface() : (*_pInterfaces)[0];
293 : }
294 :
295 :
296 :
297 :
298 367370 : const SfxSlot* SfxSlotPool::GetUnoSlot( const OUString& rName )
299 : {
300 367370 : const SfxSlot *pSlot = NULL;
301 3267116 : for (sal_uInt16 nInterface = 0; _pInterfaces && nInterface < _pInterfaces->size(); ++nInterface)
302 : {
303 3234077 : pSlot = (*_pInterfaces)[nInterface]->GetSlot( rName );
304 3234077 : if ( pSlot )
305 334331 : break;
306 : }
307 :
308 367370 : if ( !pSlot && _pParentPool )
309 24559 : pSlot = _pParentPool->GetUnoSlot( rName );
310 :
311 367370 : return pSlot;
312 : }
313 :
314 353132 : SfxSlotPool& SfxSlotPool::GetSlotPool( SfxViewFrame *pFrame )
315 : {
316 353132 : SfxModule *pMod = SfxModule::GetActiveModule( pFrame );
317 353132 : if ( pMod && pMod->GetSlotPool() )
318 293219 : return *pMod->GetSlotPool();
319 : else
320 59913 : return *SfxGetpApp()->Get_Impl()->pSlotPool;
321 : }
322 :
323 :
324 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|