1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/stream.hxx>
21 #include <rsc/rscsfx.hxx>
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>
34 #include <sfx2/sfx.hrc>
36 SfxSlotPool::SfxSlotPool( SfxSlotPool
*pParent
, ResMgr
* pResManager
)
38 , _pParentPool( pParent
)
39 , _pResMgr( pResManager
)
46 _pResMgr
= SfxApplication::GetOrCreate()->GetOffResManager_Impl();
49 //====================================================================
51 SfxSlotPool::~SfxSlotPool()
54 for ( SfxInterface
*pIF
= FirstInterface(); pIF
; pIF
= FirstInterface() )
60 //====================================================================
61 // registers the availability of the Interface of functions
63 void SfxSlotPool::RegisterInterface( SfxInterface
& rInterface
)
65 // add to the list of SfxObjectInterface instances
66 if ( _pInterfaces
== NULL
)
67 _pInterfaces
= new SfxInterfaceArr_Impl
;
68 _pInterfaces
->push_back(&rInterface
);
70 // Stop at a (single) Null-slot (for syntactic reasons the interfaces
71 // always contain at least one slot)
72 if ( rInterface
.Count() != 0 && !rInterface
[0]->nSlotId
)
75 // possibly add Interface-id and group-ids of funcs to the list of groups
78 _pGroups
= new SfxSlotGroupArr_Impl
;
82 // The Groups in parent Slotpool are also known here
83 _pGroups
->append( *_pParentPool
->_pGroups
);
87 for ( size_t nFunc
= 0; nFunc
< rInterface
.Count(); ++nFunc
)
89 SfxSlot
*pDef
= rInterface
[nFunc
];
90 if ( pDef
->GetGroupId() && /* pDef->GetGroupId() != GID_INTERN && */
91 _pGroups
->find(pDef
->GetGroupId()) == SfxSlotGroupArr_Impl::npos
)
93 if (pDef
->GetGroupId() == GID_INTERN
)
94 _pGroups
->insert(_pGroups
->begin(), pDef
->GetGroupId());
96 _pGroups
->push_back(pDef
->GetGroupId());
101 //====================================================================
103 TypeId
SfxSlotPool::GetSlotType( sal_uInt16 nId
) const
105 const SfxSlot
* pSlot
= (const_cast <SfxSlotPool
*> (this))->GetSlot( nId
);
106 return pSlot
? pSlot
->GetType()->Type() : 0;
109 //====================================================================
110 // unregisters the availability of the Interface of functions
112 void SfxSlotPool::ReleaseInterface( SfxInterface
& rInterface
)
114 DBG_ASSERT( _pInterfaces
, "releasing SfxInterface, but there are none" );
115 // remove from the list of SfxInterface instances
116 SfxInterfaceArr_Impl::iterator i
= std::find(_pInterfaces
->begin(), _pInterfaces
->end(), &rInterface
);
117 if(i
!= _pInterfaces
->end())
118 _pInterfaces
->erase(i
);
121 // get the first SfxMessage for a special Id (e.g. for getting check-mode)
123 const SfxSlot
* SfxSlotPool::GetSlot( sal_uInt16 nId
)
125 DBG_ASSERT( _pInterfaces
!= NULL
, "no Interfaces registered" );
127 // First, search their own interfaces
128 for ( sal_uInt16 nInterf
= 0; nInterf
< _pInterfaces
->size(); ++nInterf
)
130 const SfxSlot
*pDef
= ((*_pInterfaces
)[nInterf
])->GetSlot(nId
);
135 // Then try any of the possible existing parent
136 return _pParentPool
? _pParentPool
->GetSlot( nId
) : 0;
139 //--------------------------------------------------------------------
141 // skips to the next group
143 String
SfxSlotPool::SeekGroup( sal_uInt16 nNo
)
145 DBG_ASSERT( _pInterfaces
!= NULL
, "no Interfaces registered" );
147 // if the group exists, use it
148 if ( _pGroups
&& nNo
< _pGroups
->size() )
153 // In most cases, the order of the IDs agree
154 sal_uInt16 nParentCount
= _pParentPool
->_pGroups
->size();
155 if ( nNo
< nParentCount
&& (*_pGroups
)[nNo
] == (*_pParentPool
->_pGroups
)[nNo
] )
156 _pParentPool
->_nCurGroup
= nNo
;
159 // Otherwise search. If the group is not found in the parent
160 // pool, _nCurGroup is set outside the valid range
162 for ( i
=1; i
<nParentCount
; i
++ )
163 if ( (*_pGroups
)[nNo
] == (*_pParentPool
->_pGroups
)[i
] )
165 _pParentPool
->_nCurGroup
= i
;
169 SfxResId
aResId( (*_pGroups
)[_nCurGroup
] );
170 aResId
.SetRT(RSC_STRING
);
171 if ( !aResId
.GetResMgr()->IsAvailable(aResId
) )
173 OSL_FAIL( "GroupId-Name not defined in SFX!" );
177 return aResId
.toString();
184 //--------------------------------------------------------------------
186 sal_uInt16
SfxSlotPool::GetGroupCount()
188 return _pGroups
->size();
192 //--------------------------------------------------------------------
194 // internal search loop
196 const SfxSlot
* SfxSlotPool::SeekSlot( sal_uInt16 nStartInterface
)
198 DBG_ASSERT( _pInterfaces
!= NULL
, "no Interfaces registered" );
200 // The numbering starts at the interfaces of the parent pool
201 sal_uInt16 nFirstInterface
= _pParentPool
? _pParentPool
->_pInterfaces
->size() : 0;
203 // have reached the end of the Parent-Pools?
204 if ( nStartInterface
< nFirstInterface
&&
205 _pParentPool
->_nCurGroup
>= _pParentPool
->_pGroups
->size() )
206 nStartInterface
= nFirstInterface
;
208 // Is the Interface still in the Parent-Pool?
209 if ( nStartInterface
< nFirstInterface
)
211 DBG_ASSERT( _pParentPool
, "No parent pool!" );
212 _nCurInterface
= nStartInterface
;
213 return _pParentPool
->SeekSlot( nStartInterface
);
216 // find the first func-def with the current group id
217 sal_uInt16 nCount
= _pInterfaces
->size() + nFirstInterface
;
218 for ( _nCurInterface
= nStartInterface
;
219 _nCurInterface
< nCount
;
222 SfxInterface
* pInterface
= (*_pInterfaces
)[_nCurInterface
-nFirstInterface
];
224 _nCurMsg
< pInterface
->Count();
227 const SfxSlot
* pMsg
= (*pInterface
)[_nCurMsg
];
228 if ( pMsg
->GetGroupId() == _pGroups
->at(_nCurGroup
) )
236 //--------------------------------------------------------------------
237 // skips to the next func in the current group
239 const SfxSlot
* SfxSlotPool::NextSlot()
241 DBG_ASSERT( _pInterfaces
!= NULL
, "no Interfaces registered" );
243 // The numbering starts at the interfaces of the parent pool
244 sal_uInt16 nFirstInterface
= _pParentPool
? _pParentPool
->_pInterfaces
->size() : 0;
246 if ( _nCurInterface
< nFirstInterface
&& _nCurGroup
>= _pParentPool
->_pGroups
->size() )
247 _nCurInterface
= nFirstInterface
;
249 if ( _nCurInterface
< nFirstInterface
)
251 DBG_ASSERT( _pParentPool
, "No parent pool!" );
252 const SfxSlot
*pSlot
= _pParentPool
->NextSlot();
253 _nCurInterface
= _pParentPool
->_nCurInterface
;
256 if ( _nCurInterface
== nFirstInterface
)
257 // parent pool is ready
258 return SeekSlot( nFirstInterface
);
261 sal_uInt16 nInterface
= _nCurInterface
- nFirstInterface
;
262 // possibly we are already at the end
263 if ( nInterface
>= _pInterfaces
->size() )
266 // look for further matching func-defs within the same Interface
267 SfxInterface
* pInterface
= (*_pInterfaces
)[nInterface
];
268 while ( ++_nCurMsg
< pInterface
->Count() )
270 SfxSlot
* pMsg
= (*pInterface
)[_nCurMsg
];
271 if ( pMsg
->GetGroupId() == _pGroups
->at(_nCurGroup
) )
275 return SeekSlot(++_nCurInterface
);
279 //--------------------------------------------------------------------
280 // Query SlotName with help text
281 //--------------------------------------------------------------------
283 SfxInterface
* SfxSlotPool::FirstInterface()
286 if ( !_pInterfaces
|| !_pInterfaces
->size() )
288 return _pParentPool
? _pParentPool
->FirstInterface() : (*_pInterfaces
)[0];
292 //--------------------------------------------------------------------
294 const SfxSlot
* SfxSlotPool::GetUnoSlot( const String
& rName
)
296 const SfxSlot
*pSlot
= NULL
;
297 for ( sal_uInt16 nInterface
=0; nInterface
<_pInterfaces
->size(); ++nInterface
)
299 pSlot
= (*_pInterfaces
)[nInterface
]->GetSlot( rName
);
304 if ( !pSlot
&& _pParentPool
)
305 pSlot
= _pParentPool
->GetUnoSlot( rName
);
310 SfxSlotPool
& SfxSlotPool::GetSlotPool( SfxViewFrame
*pFrame
)
312 SfxModule
*pMod
= SfxModule::GetActiveModule( pFrame
);
313 if ( pMod
&& pMod
->GetSlotPool() )
314 return *pMod
->GetSlotPool();
316 return *SFX_APP()->Get_Impl()->pSlotPool
;
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */