xref: /trunk/main/sfx2/source/control/msgpool.cxx (revision d119d52d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 #include <tools/stream.hxx>
27 #include <rsc/rscsfx.hxx>
28 #ifndef GCC
29 #endif
30 
31 // wg. pSlotPool
32 #include "appdata.hxx"
33 #include <sfx2/msgpool.hxx>
34 #include <sfx2/minarray.hxx>
35 #include <sfx2/msg.hxx>
36 #include <sfx2/app.hxx>
37 #include <sfx2/objface.hxx>
38 #include "sfxtypes.hxx"
39 #include "sfx2/sfxresid.hxx"
40 #include "arrdecl.hxx"
41 #include <sfx2/module.hxx>
42 
43 #include <sfx2/sfx.hrc>
44 
45 //====================================================================
46 
47 struct SfxSIDRegistration_Impl
48 {
49 	String			_aGroup;
50 	String			_aName;
51 	sal_uInt16			_nSID;
52 };
53 
54 struct SfxSlotType_Impl
55 {
56 	sal_uInt16  nId;
57 	TypeId  nType;
58 
SfxSlotType_ImplSfxSlotType_Impl59 	SfxSlotType_Impl( sal_uInt16 nTheId, TypeId nTheType ):
60 		nId(nTheId), nType(nTheType)
61 	{}
62 };
63 
64 DECL_2BYTEARRAY(SfxSlotGroupArr_Impl, sal_uInt16, 6, 4)
65 DECL_PTRARRAY(SfxInterfaceArr_Impl, SfxInterface*, 6, 3)
66 DECL_PTRARRAY(SfxSlotTypeArr_Impl, SfxSlotType_Impl*, 8, 8)
67 
68 
69 //====================================================================
70 
SfxSlotPool(SfxSlotPool * pParent,ResMgr * pResManager)71 SfxSlotPool::SfxSlotPool( SfxSlotPool *pParent, ResMgr* pResManager )
72  : _pGroups(0)
73  , _pTypes(0)
74  , _pParentPool( pParent )
75  , _pResMgr( pResManager )
76  , _pInterfaces(0)
77  , _nCurGroup(0)
78  , _nCurInterface(0)
79  , _nCurMsg(0)
80  , _pUnoSlots( 0 )
81 {
82 	if ( !_pResMgr )
83 		_pResMgr = SfxApplication::GetOrCreate()->GetOffResManager_Impl();
84 }
85 
86 //====================================================================
87 
~SfxSlotPool()88 SfxSlotPool::~SfxSlotPool()
89 {
90 	_pParentPool = 0;
91 	for ( SfxInterface *pIF = FirstInterface(); pIF; pIF = FirstInterface() )
92 		delete pIF;
93 	delete _pInterfaces;
94 	delete _pGroups;
95 	if ( _pTypes )
96 	{
97 		for ( sal_uInt16 n =_pTypes->Count(); n--; )
98 			delete _pTypes->GetObject(n);
99 		delete _pTypes;
100 	}
101 }
102 
103 //====================================================================
104 
105 // registers the availability of the Interface of functions
106 
RegisterInterface(SfxInterface & rInterface)107 void SfxSlotPool::RegisterInterface( SfxInterface& rInterface )
108 {
109 	DBG_MEMTEST();
110 
111 	// add to the list of SfxObjectInterface instances
112 	if ( _pInterfaces == 0 )
113 		_pInterfaces = new SfxInterfaceArr_Impl;
114 	_pInterfaces->Append(&rInterface);
115 
116 	// bei einem (einzelnen) Null-Slot abbrechen (aus syntaktischen Gr"unden
117 	// enthalten interfaces immer mindestens einen Slot)
118 	if ( rInterface.Count() == 1 && !rInterface[0]->nSlotId )
119 		return;
120 
121 	// possibly add Interface-id and group-ids of funcs to the list of groups
122 	if ( !_pGroups )
123 	{
124 		_pGroups = new SfxSlotGroupArr_Impl;
125 
126 		if ( _pParentPool )
127 		{
128 			// Die Groups im parent Slotpool sind auch hier bekannt
129 			SfxSlotGroupArr_Impl& rGroups = *_pParentPool->_pGroups;
130 			for ( sal_uInt16 n=0; n<rGroups.Count(); n++ )
131 				_pGroups->Append( rGroups[n] );
132 		}
133 	}
134 
135 	if ( !_pTypes )
136 		_pTypes = new SfxSlotTypeArr_Impl;
137 	for ( sal_uInt16 nFunc = 0; nFunc < rInterface.Count(); ++nFunc )
138 	{
139 		SfxSlot *pDef = rInterface[nFunc];
140 		if ( pDef->GetGroupId() && /* pDef->GetGroupId() != GID_INTERN && */
141 			 !_pGroups->Contains(pDef->GetGroupId()) )
142 		{
143 			if (pDef->GetGroupId() == GID_INTERN)
144 				_pGroups->Insert(0, pDef->GetGroupId());
145 			else
146 				_pGroups->Append(pDef->GetGroupId());
147 		}
148 #if 0
149 		const TypeId &rTypeId = pDef->GetType()->Type();
150 		if ( /*rTypeId != TYPE(SfxVoidItem) &&*/ rTypeId != 0 )
151 		{
152 			sal_uInt16 nPos;
153 			for ( nPos = 0; nPos < _pTypes->Count(); ++nPos )
154 			{
155 				if ( _pTypes->GetObject(nPos)->nId == pDef->GetSlotId() )
156 				{
157 					DBG_ASSERT( rTypeId == _pTypes->GetObject(nPos)->nType,
158 								"same slot id with unequal item types" );
159 				}
160 				else if ( _pTypes->GetObject(nPos)->nId > pDef->GetSlotId() )
161 					break;
162 			}
163 			if ( nPos >= _pTypes->Count() ||
164 				 _pTypes->GetObject(nPos)->nId > pDef->GetSlotId() )
165 				_pTypes->Append( new SfxSlotType_Impl( pDef->GetSlotId(), rTypeId ) );
166 		}
167 #endif
168 	}
169 }
170 
171 //====================================================================
172 
GetSlotType(sal_uInt16 nId) const173 TypeId SfxSlotPool::GetSlotType( sal_uInt16 nId ) const
174 {
175 	const SfxSlot* pSlot = (const_cast <SfxSlotPool*> (this))->GetSlot( nId );
176 	return pSlot ? pSlot->GetType()->Type() : 0;
177 /*
178 	for ( sal_uInt16 nPos = 0; nPos < _pTypes->Count(); ++nPos )
179 	{
180 		if ( _pTypes->GetObject(nPos)->nId == nId )
181 			return _pTypes->GetObject(nPos)->nType;
182 	}
183 	return _pParentPool ? _pParentPool->GetSlotType( nId ) : 0;
184  */
185 }
186 
187 //====================================================================
188 
189 // unregisters the availability of the Interface of functions
190 
ReleaseInterface(SfxInterface & rInterface)191 void SfxSlotPool::ReleaseInterface( SfxInterface& rInterface )
192 {
193 	DBG_MEMTEST();
194 	DBG_ASSERT( _pInterfaces, "releasing SfxInterface, but there are none" );
195 	// remove from the list of SfxInterface instances
196 	_pInterfaces->Remove(&rInterface);
197 }
198 
199 //--------------------------------------------------------------------
200 
201 // get the first SfxMessage for a special Id (e.g. for getting check-mode)
202 
GetSlot(sal_uInt16 nId)203 const SfxSlot* SfxSlotPool::GetSlot( sal_uInt16 nId )
204 {
205 	DBG_MEMTEST();
206 	DBG_ASSERT( _pInterfaces != 0, "no Interfaces registered" );
207 
208 	// Zun"achst die eigenen Interfaces absuchen
209 	for ( sal_uInt16 nInterf = 0; nInterf < _pInterfaces->Count(); ++nInterf )
210 	{
211 		const SfxSlot *pDef = _pInterfaces->GetObject(nInterf)->GetSlot(nId);
212 		if ( pDef )
213 			return pDef;
214 	}
215 
216 	// Dann beim eventuell vorhandenen parent versuchen
217 	return _pParentPool ? _pParentPool->GetSlot( nId ) : 0;
218 }
219 
220 //--------------------------------------------------------------------
221 
222 // skips to the next group
223 
SeekGroup(sal_uInt16 nNo)224 String SfxSlotPool::SeekGroup( sal_uInt16 nNo )
225 {
226 	DBG_MEMTEST();
227 	DBG_ASSERT( _pInterfaces != 0, "no Interfaces registered" );
228 
229 	// if the group exists, use it
230 	if ( _pGroups && nNo < _pGroups->Count() )
231 	{
232 		_nCurGroup = nNo;
233 		if ( _pParentPool )
234 		{
235 			// Meistens stimmt die Reihenfolge der Ids "uberein
236 			sal_uInt16 nParentCount = _pParentPool->_pGroups->Count();
237 			if ( nNo < nParentCount && (*_pGroups)[nNo] == (*_pParentPool->_pGroups)[nNo] )
238 				_pParentPool->_nCurGroup = nNo;
239 			else
240 			{
241 				// Ansonsten mu\s gesucht werden
242 				// Wenn die Gruppe im parent pool nicht gefunden wird, wird
243 				// _nCurGroup au\serhalb des g"ultigen Bereiches gesetzt
244 				sal_uInt16 i;
245 				for ( i=1; i<nParentCount; i++ )
246 					if ( (*_pGroups)[nNo] == (*_pParentPool->_pGroups)[i] )
247 						break;
248 				_pParentPool->_nCurGroup = i;
249 			}
250 		}
251 
252 		SfxResId aResId( (*_pGroups)[_nCurGroup] );
253 		aResId.SetRT(RSC_STRING);
254 		if ( !aResId.GetResMgr()->IsAvailable(aResId) )
255 		{
256 			DBG_ERROR( "GroupId-Name nicht im SFX definiert!" );
257 			return String();
258 		}
259 
260 		return String( aResId );
261 	}
262 
263 	return String();
264 }
265 
266 
267 //--------------------------------------------------------------------
268 
GetGroupCount()269 sal_uInt16 SfxSlotPool::GetGroupCount()
270 {
271 	return _pGroups->Count();
272 }
273 
274 
275 //--------------------------------------------------------------------
276 
277 // internal search loop
278 
SeekSlot(sal_uInt16 nStartInterface)279 const SfxSlot* SfxSlotPool::SeekSlot( sal_uInt16 nStartInterface )
280 {
281 	DBG_MEMTEST();
282 	DBG_ASSERT( _pInterfaces != 0, "no Interfaces registered" );
283 
284 	// Die Numerierung der interfaces startet beim parent pool
285 	sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_pInterfaces->Count() : 0;
286 
287 	// sind wir am Ende des Parent-Pools angekommen?
288 	if ( nStartInterface < nFirstInterface &&
289 		 _pParentPool->_nCurGroup >= _pParentPool->_pGroups->Count() )
290 		nStartInterface = nFirstInterface;
291 
292 	// liegt das Interface noch im Parent-Pool?
293 	if ( nStartInterface < nFirstInterface )
294 	{
295 		DBG_ASSERT( _pParentPool, "Kein parent pool!" );
296 		_nCurInterface = nStartInterface;
297 		return _pParentPool->SeekSlot( nStartInterface );
298 	}
299 
300 	// find the first func-def with the current group id
301 	sal_uInt16 nCount = _pInterfaces->Count() + nFirstInterface;
302 	for ( _nCurInterface = nStartInterface;
303 			_nCurInterface < nCount;
304 		  ++_nCurInterface )
305 	{
306 		SfxInterface* pInterface = (*_pInterfaces)[_nCurInterface-nFirstInterface];
307 		for ( _nCurMsg = 0;
308 			  _nCurMsg < pInterface->Count();
309 			  ++_nCurMsg )
310 		{
311 			const SfxSlot* pMsg = (*pInterface)[_nCurMsg];
312 			if ( pMsg->GetGroupId() == _pGroups->GetObject(_nCurGroup) )
313 				return pMsg;
314 		}
315 	}
316 
317 	return 0;
318 }
319 
320 //--------------------------------------------------------------------
321 
322 // skips to the next func in the current group
323 
NextSlot()324 const SfxSlot* SfxSlotPool::NextSlot()
325 {
326 	DBG_MEMTEST();
327 	DBG_ASSERT( _pInterfaces != 0, "no Interfaces registered" );
328 
329 	// Die Numerierung der interfaces startet beim parent pool
330 	sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_pInterfaces->Count() : 0;
331 
332 	if ( _nCurInterface < nFirstInterface && _nCurGroup >= _pParentPool->_pGroups->Count() )
333 		_nCurInterface = nFirstInterface;
334 
335 	if ( _nCurInterface < nFirstInterface )
336 	{
337 		DBG_ASSERT( _pParentPool, "Kein parent pool!" );
338 		const SfxSlot *pSlot = _pParentPool->NextSlot();
339 		_nCurInterface = _pParentPool->_nCurInterface;
340 		if ( pSlot )
341 			return pSlot;
342 		if ( _nCurInterface == nFirstInterface )
343 			// parent pool ist fertig
344 			return SeekSlot( nFirstInterface );
345 	}
346 
347 	sal_uInt16 nInterface = _nCurInterface - nFirstInterface;
348 	// possibly we are already at the end
349 	if ( nInterface >= _pInterfaces->Count() )
350 		return 0;
351 
352 	// look for further matching func-defs within the same Interface
353 	SfxInterface* pInterface = (*_pInterfaces)[nInterface];
354 	while ( ++_nCurMsg < pInterface->Count() )
355 	{
356 		SfxSlot* pMsg = (*pInterface)[_nCurMsg];
357 		if ( pMsg->GetGroupId() == _pGroups->GetObject(_nCurGroup) )
358 			return pMsg;
359 	}
360 
361 	return SeekSlot(++_nCurInterface );
362 }
363 
364 
365 //--------------------------------------------------------------------
366 
367 // SlotName erfragen, gfs. mit HilfeText
368 
369 //--------------------------------------------------------------------
370 
FirstInterface()371 SfxInterface* SfxSlotPool::FirstInterface()
372 {
373 	_nCurInterface = 0;
374 	if ( !_pInterfaces || !_pInterfaces->Count() )
375 		return 0;
376 	return _pParentPool ? _pParentPool->FirstInterface() : (*_pInterfaces)[0];
377 }
378 
379 
380 //--------------------------------------------------------------------
381 
NextInterface()382 SfxInterface* SfxSlotPool::NextInterface()
383 {
384 	_nCurInterface++;
385 	sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_pInterfaces->Count() : 0;
386 	if ( _nCurInterface < nFirstInterface )
387 		return (*_pParentPool->_pInterfaces)[_nCurInterface];
388 	sal_uInt16 nInterface = _nCurInterface - nFirstInterface;
389 	return nInterface < _pInterfaces->Count() ? (*_pInterfaces)[nInterface] : 0;
390 }
391 
GetUnoSlot(const String & rName)392 const SfxSlot* SfxSlotPool::GetUnoSlot( const String& rName )
393 {
394 	const SfxSlot *pSlot = NULL;
395     for ( sal_uInt16 nInterface=0; nInterface<_pInterfaces->Count(); nInterface++ )
396     {
397         pSlot = (*_pInterfaces)[nInterface]->GetSlot( rName );
398         if ( pSlot )
399             break;
400     }
401 
402     if ( !pSlot && _pParentPool )
403         pSlot = _pParentPool->GetUnoSlot( rName );
404 
405 	return pSlot;
406 }
407 
GetSlotPool(SfxViewFrame * pFrame)408 SfxSlotPool& SfxSlotPool::GetSlotPool( SfxViewFrame *pFrame )
409 {
410     SfxModule *pMod = SfxModule::GetActiveModule( pFrame );
411 	if ( pMod && pMod->GetSlotPool() )
412 		return *pMod->GetSlotPool();
413 	else
414         return *SFX_APP()->Get_Impl()->pSlotPool;
415 }
416 
417 
418