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
)
38 , _pParentPool( pParent
)
46 SfxSlotPool::~SfxSlotPool()
49 for ( SfxInterface
*pIF
= FirstInterface(); pIF
; pIF
= FirstInterface() )
56 // registers the availability of the Interface of functions
58 void SfxSlotPool::RegisterInterface( SfxInterface
& rInterface
)
60 // add to the list of SfxObjectInterface instances
61 if ( _pInterfaces
== NULL
)
62 _pInterfaces
= new SfxInterfaceArr_Impl
;
63 _pInterfaces
->push_back(&rInterface
);
65 // Stop at a (single) Null-slot (for syntactic reasons the interfaces
66 // always contain at least one slot)
67 if ( rInterface
.Count() != 0 && !rInterface
[0]->nSlotId
)
70 // possibly add Interface-id and group-ids of funcs to the list of groups
73 _pGroups
= new SfxSlotGroupArr_Impl
;
77 // The Groups in parent Slotpool are also known here
78 _pGroups
->append( *_pParentPool
->_pGroups
);
82 for ( size_t nFunc
= 0; nFunc
< rInterface
.Count(); ++nFunc
)
84 SfxSlot
*pDef
= rInterface
[nFunc
];
85 if ( pDef
->GetGroupId() && /* pDef->GetGroupId() != GID_INTERN && */
86 _pGroups
->find(pDef
->GetGroupId()) == SfxSlotGroupArr_Impl::npos
)
88 if (pDef
->GetGroupId() == GID_INTERN
)
89 _pGroups
->insert(_pGroups
->begin(), pDef
->GetGroupId());
91 _pGroups
->push_back(pDef
->GetGroupId());
98 TypeId
SfxSlotPool::GetSlotType( sal_uInt16 nId
) const
100 const SfxSlot
* pSlot
= (const_cast <SfxSlotPool
*> (this))->GetSlot( nId
);
101 return pSlot
? pSlot
->GetType()->Type() : 0;
105 // unregisters the availability of the Interface of functions
107 void SfxSlotPool::ReleaseInterface( SfxInterface
& rInterface
)
109 SAL_WARN_IF(!_pInterfaces
, "sfx.control", "releasing SfxInterface, but there are none");
113 // remove from the list of SfxInterface instances
114 SfxInterfaceArr_Impl::iterator i
= std::find(_pInterfaces
->begin(), _pInterfaces
->end(), &rInterface
);
115 if(i
!= _pInterfaces
->end())
116 _pInterfaces
->erase(i
);
119 // get the first SfxMessage for a special Id (e.g. for getting check-mode)
121 const SfxSlot
* SfxSlotPool::GetSlot( sal_uInt16 nId
)
123 SAL_WARN_IF(!_pInterfaces
, "sfx.control", "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;
141 // skips to the next group
143 OUString
SfxSlotPool::SeekGroup( sal_uInt16 nNo
)
145 SAL_WARN_IF(!_pInterfaces
, "sfx.control", "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 ( !SfxResId::GetResMgr()->IsAvailable(aResId
) )
173 OSL_FAIL( "GroupId-Name not defined in SFX!" );
177 return aResId
.toString();
186 sal_uInt16
SfxSlotPool::GetGroupCount()
188 return _pGroups
->size();
194 // internal search loop
196 const SfxSlot
* SfxSlotPool::SeekSlot( sal_uInt16 nStartInterface
)
198 SAL_WARN_IF(!_pInterfaces
, "sfx.control", "no Interfaces registered");
202 // The numbering starts at the interfaces of the parent pool
203 sal_uInt16 nFirstInterface
= _pParentPool
? _pParentPool
->_pInterfaces
->size() : 0;
205 // have reached the end of the Parent-Pools?
206 if ( nStartInterface
< nFirstInterface
&&
207 _pParentPool
->_nCurGroup
>= _pParentPool
->_pGroups
->size() )
208 nStartInterface
= nFirstInterface
;
210 // Is the Interface still in the Parent-Pool?
211 if ( nStartInterface
< nFirstInterface
)
213 SAL_WARN_IF(!_pParentPool
, "sfx.control", "No parent pool!");
214 _nCurInterface
= nStartInterface
;
215 return _pParentPool
->SeekSlot( nStartInterface
);
218 // find the first func-def with the current group id
219 sal_uInt16 nCount
= _pInterfaces
->size() + nFirstInterface
;
220 for ( _nCurInterface
= nStartInterface
;
221 _nCurInterface
< nCount
;
224 SfxInterface
* pInterface
= (*_pInterfaces
)[_nCurInterface
-nFirstInterface
];
226 _nCurMsg
< pInterface
->Count();
229 const SfxSlot
* pMsg
= (*pInterface
)[_nCurMsg
];
230 if ( pMsg
->GetGroupId() == _pGroups
->at(_nCurGroup
) )
239 // skips to the next func in the current group
241 const SfxSlot
* SfxSlotPool::NextSlot()
243 SAL_WARN_IF(!_pInterfaces
, "sfx.control", "no Interfaces registered");
247 // The numbering starts at the interfaces of the parent pool
248 sal_uInt16 nFirstInterface
= _pParentPool
? _pParentPool
->_pInterfaces
->size() : 0;
250 if ( _nCurInterface
< nFirstInterface
&& _nCurGroup
>= _pParentPool
->_pGroups
->size() )
251 _nCurInterface
= nFirstInterface
;
253 if ( _nCurInterface
< nFirstInterface
)
255 SAL_WARN_IF(!_pParentPool
, "sfx.control", "No parent pool!");
256 const SfxSlot
*pSlot
= _pParentPool
->NextSlot();
257 _nCurInterface
= _pParentPool
->_nCurInterface
;
260 if ( _nCurInterface
== nFirstInterface
)
261 // parent pool is ready
262 return SeekSlot( nFirstInterface
);
265 sal_uInt16 nInterface
= _nCurInterface
- nFirstInterface
;
266 // possibly we are already at the end
267 if ( nInterface
>= _pInterfaces
->size() )
270 // look for further matching func-defs within the same Interface
271 SfxInterface
* pInterface
= (*_pInterfaces
)[nInterface
];
272 while ( ++_nCurMsg
< pInterface
->Count() )
274 SfxSlot
* pMsg
= (*pInterface
)[_nCurMsg
];
275 if ( pMsg
->GetGroupId() == _pGroups
->at(_nCurGroup
) )
279 return SeekSlot(++_nCurInterface
);
284 // Query SlotName with help text
287 SfxInterface
* SfxSlotPool::FirstInterface()
290 if ( !_pInterfaces
|| !_pInterfaces
->size() )
292 return _pParentPool
? _pParentPool
->FirstInterface() : (*_pInterfaces
)[0];
298 const SfxSlot
* SfxSlotPool::GetUnoSlot( const OUString
& rName
)
300 const SfxSlot
*pSlot
= NULL
;
301 for (sal_uInt16 nInterface
= 0; _pInterfaces
&& nInterface
< _pInterfaces
->size(); ++nInterface
)
303 pSlot
= (*_pInterfaces
)[nInterface
]->GetSlot( rName
);
308 if ( !pSlot
&& _pParentPool
)
309 pSlot
= _pParentPool
->GetUnoSlot( rName
);
314 SfxSlotPool
& SfxSlotPool::GetSlotPool( SfxViewFrame
*pFrame
)
316 SfxModule
*pMod
= SfxModule::GetActiveModule( pFrame
);
317 if ( pMod
&& pMod
->GetSlotPool() )
318 return *pMod
->GetSlotPool();
320 return *SfxGetpApp()->Get_Impl()->pSlotPool
;
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */