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