1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sd.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "ViewShellManager.hxx"
28cdf0e10cSrcweir #include "ViewShell.hxx"
29cdf0e10cSrcweir #include "ViewShellBase.hxx"
30cdf0e10cSrcweir #include "Window.hxx"
31cdf0e10cSrcweir #include "DrawDocShell.hxx"
32cdf0e10cSrcweir #include "FormShellManager.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
35cdf0e10cSrcweir #include <svx/svxids.hrc>
36cdf0e10cSrcweir #include <svx/fmshell.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <hash_map>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #undef VERBOSE
41cdf0e10cSrcweir //#define VERBOSE 2
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace sd {
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir /** The ShellDescriptor class is used to shells together with their ids and
48cdf0e10cSrcweir     the factory that was used to create the shell.
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     The shell pointer may be NULL.  In that case the shell is created on
51cdf0e10cSrcweir     demand by a factory.
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     The factory pointer may be NULL.  In that case the shell pointer is
54cdf0e10cSrcweir     given to the ViewShellManager.
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     Shell pointer and factory pointer can but should not be NULL at the same
57cdf0e10cSrcweir     time.
58cdf0e10cSrcweir */
59cdf0e10cSrcweir class ShellDescriptor {
60cdf0e10cSrcweir public:
61cdf0e10cSrcweir     SfxShell* mpShell;
62cdf0e10cSrcweir     ShellId mnId;
63cdf0e10cSrcweir     ViewShellManager::SharedShellFactory mpFactory;
64cdf0e10cSrcweir     ShellDescriptor ();
65cdf0e10cSrcweir     ShellDescriptor (SfxShell* pShell, ShellId nId);
66cdf0e10cSrcweir     ShellDescriptor (const ShellDescriptor& rDescriptor);
67cdf0e10cSrcweir     ShellDescriptor& operator= (const ShellDescriptor& rDescriptor);
68cdf0e10cSrcweir     bool IsMainViewShell (void) const;
69cdf0e10cSrcweir     ::Window* GetWindow (void) const;
70cdf0e10cSrcweir };
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
75cdf0e10cSrcweir /** This functor can be used to search for a shell in an STL container when the
76cdf0e10cSrcweir     shell pointer is given.
77cdf0e10cSrcweir */
78cdf0e10cSrcweir class IsShell : public ::std::unary_function<ShellDescriptor,bool>
79cdf0e10cSrcweir {
80cdf0e10cSrcweir public:
81cdf0e10cSrcweir     IsShell (const SfxShell* pShell) : mpShell(pShell) {}
82cdf0e10cSrcweir     bool operator() (const ShellDescriptor& rDescriptor)
83cdf0e10cSrcweir     { return rDescriptor.mpShell == mpShell; }
84cdf0e10cSrcweir private:
85cdf0e10cSrcweir     const SfxShell* mpShell;
86cdf0e10cSrcweir };
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 
91cdf0e10cSrcweir /** This functor can be used to search for a shell in an STL container when the
92cdf0e10cSrcweir     id of the shell is given.
93cdf0e10cSrcweir */
94cdf0e10cSrcweir class IsId : public ::std::unary_function<ShellDescriptor,bool>
95cdf0e10cSrcweir {
96cdf0e10cSrcweir public:
97cdf0e10cSrcweir     IsId (ShellId nId) : mnId(nId) {}
98cdf0e10cSrcweir     bool operator() (const ShellDescriptor& rDescriptor)
99cdf0e10cSrcweir     { return rDescriptor.mnId == mnId; }
100cdf0e10cSrcweir private:
101cdf0e10cSrcweir     ShellId mnId;
102cdf0e10cSrcweir };
103cdf0e10cSrcweir 
104cdf0e10cSrcweir } // end of anonymous namespace
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
109cdf0e10cSrcweir class ViewShellManager::Implementation
110cdf0e10cSrcweir {
111cdf0e10cSrcweir public:
112cdf0e10cSrcweir     Implementation (
113cdf0e10cSrcweir         ViewShellManager& rManager,
114cdf0e10cSrcweir         ViewShellBase& rBase);
115cdf0e10cSrcweir     ~Implementation (void);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     void AddShellFactory (
118cdf0e10cSrcweir         const SfxShell* pViewShell,
119cdf0e10cSrcweir         const SharedShellFactory& rpFactory);
120cdf0e10cSrcweir     void RemoveShellFactory (
121cdf0e10cSrcweir         const SfxShell* pViewShell,
122cdf0e10cSrcweir         const SharedShellFactory& rpFactory);
123cdf0e10cSrcweir     void ActivateViewShell (
124cdf0e10cSrcweir         ViewShell* pViewShell);
125cdf0e10cSrcweir     void DeactivateViewShell (const ViewShell& rShell);
126cdf0e10cSrcweir     void ActivateShell (SfxShell& rShell);
127cdf0e10cSrcweir     void DeactivateShell (const SfxShell& rShell);
128cdf0e10cSrcweir     void ActivateShell (const ShellDescriptor& rDescriptor);
129cdf0e10cSrcweir     void SetFormShell (const ViewShell* pViewShell, FmFormShell* pFormShell, bool bAbove);
130cdf0e10cSrcweir     void ActivateSubShell (const SfxShell& rParentShell, ShellId nId);
131cdf0e10cSrcweir     void DeactivateSubShell (const SfxShell& rParentShell, ShellId nId);
132cdf0e10cSrcweir     void MoveSubShellToTop (const SfxShell& rParentShell, ShellId nId);
133cdf0e10cSrcweir     void MoveToTop (const SfxShell& rParentShell);
134cdf0e10cSrcweir     SfxShell* GetShell (ShellId nId) const;
135cdf0e10cSrcweir     SfxShell* GetTopShell (void) const;
136cdf0e10cSrcweir     void Shutdown (void);
137cdf0e10cSrcweir     void InvalidateAllSubShells (const SfxShell* pParentShell);
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /** Remove all shells from the SFX stack above and including the given
140cdf0e10cSrcweir         shell.
141cdf0e10cSrcweir     */
142cdf0e10cSrcweir     void TakeShellsFromStack (const SfxShell* pShell);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     class UpdateLock
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir     public:
147cdf0e10cSrcweir         UpdateLock (Implementation& rImpl) : mrImpl(rImpl) {mrImpl.LockUpdate();}
148cdf0e10cSrcweir         ~UpdateLock (void) {mrImpl.UnlockUpdate();};
149cdf0e10cSrcweir     private:
150cdf0e10cSrcweir         Implementation& mrImpl;
151cdf0e10cSrcweir     };
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     /** Prevent updates of the shell stack.  While the sub shell manager is
156cdf0e10cSrcweir         locked it will update its internal data structures but not alter the
157cdf0e10cSrcweir         shell stack.  Use this method when there are several modifications
158cdf0e10cSrcweir         to the shell stack to prevent multiple rebuilds of the shell stack
159cdf0e10cSrcweir         and resulting broadcasts.
160cdf0e10cSrcweir     */
161cdf0e10cSrcweir     void LockUpdate (void);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     /** Allow updates of the shell stack.  This method has to be called the
164cdf0e10cSrcweir         same number of times as LockUpdate() to really allow a rebuild of
165cdf0e10cSrcweir         the shell stack.
166cdf0e10cSrcweir     */
167cdf0e10cSrcweir     void UnlockUpdate (void);
168cdf0e10cSrcweir 
169cdf0e10cSrcweir private:
170cdf0e10cSrcweir     ViewShellBase& mrBase;
171cdf0e10cSrcweir     mutable ::osl::Mutex maMutex;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     class ShellHash{public: size_t operator()(const SfxShell* p) const { return (size_t)p;} };
174cdf0e10cSrcweir     typedef ::std::hash_multimap<const SfxShell*,SharedShellFactory,ShellHash>
175cdf0e10cSrcweir         FactoryList;
176cdf0e10cSrcweir     FactoryList maShellFactories;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     /** List of the active view shells.  In order to create gather all shells
179cdf0e10cSrcweir         to put on the shell stack each view shell in this list is asked for
180cdf0e10cSrcweir         its sub-shells (typically toolbars).
181cdf0e10cSrcweir     */
182cdf0e10cSrcweir     typedef ::std::list<ShellDescriptor> ActiveShellList;
183cdf0e10cSrcweir     ActiveShellList maActiveViewShells;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     typedef ::std::list<ShellDescriptor> SubShellSubList;
186cdf0e10cSrcweir     typedef ::std::hash_map<const SfxShell*,SubShellSubList,ShellHash> SubShellList;
187cdf0e10cSrcweir     SubShellList maActiveSubShells;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     /** In this member we remember what shells we have pushed on the shell
190cdf0e10cSrcweir         stack.
191cdf0e10cSrcweir     */
192cdf0e10cSrcweir     typedef ::std::vector<SfxShell*> ShellStack;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     int mnUpdateLockCount;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     /** When this flag is set then the main view shell is always kept at the
197cdf0e10cSrcweir         top of the shell stack.
198cdf0e10cSrcweir     */
199cdf0e10cSrcweir     bool mbKeepMainViewShellOnTop;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     /** The UpdateShellStack() method can be called recursively.  This flag
202cdf0e10cSrcweir         is used to communicate between different levels of invocation: if
203cdf0e10cSrcweir         the stack has been updated in an inner call the outer call can (has
204cdf0e10cSrcweir         to) stop and return immediately.
205cdf0e10cSrcweir     */
206cdf0e10cSrcweir     bool mbShellStackIsUpToDate;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     SfxShell* mpFormShell;
209cdf0e10cSrcweir     const ViewShell* mpFormShellParent;
210cdf0e10cSrcweir     bool mbFormShellAboveParent;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     SfxShell* mpTopShell;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     void GatherActiveShells (ShellStack& rShellList);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     void UpdateShellStack (void);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     void CreateShells (void);
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     /** This method rebuilds the stack of shells that are stacked upon the
221cdf0e10cSrcweir         view shell base.
222cdf0e10cSrcweir     */
223cdf0e10cSrcweir     void CreateTargetStack (ShellStack& rStack) const;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     DECL_LINK(WindowEventHandler, VclWindowEvent*);
226cdf0e10cSrcweir 
227cdf0e10cSrcweir #ifdef VERBOSE
228cdf0e10cSrcweir     void DumpShellStack (const ShellStack& rStack);
229cdf0e10cSrcweir     void DumpSfxShellStack (void);
230cdf0e10cSrcweir #endif
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     /** To be called before a shell is taken fom the SFX shell stack.  This
233cdf0e10cSrcweir         method deactivates an active text editing to avoid problems with
234cdf0e10cSrcweir         undo managers.
235cdf0e10cSrcweir         Afterwards the Deactivate() of the shell is called.
236cdf0e10cSrcweir     */
237cdf0e10cSrcweir     void Deactivate (SfxShell* pShell);
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     ShellDescriptor CreateSubShell (
240cdf0e10cSrcweir         SfxShell* pShell,
241cdf0e10cSrcweir         ShellId nShellId,
242cdf0e10cSrcweir         ::Window* pParentWindow,
243cdf0e10cSrcweir         FrameView* pFrameView);
244cdf0e10cSrcweir     void DestroyViewShell (const ShellDescriptor& rDescriptor);
245cdf0e10cSrcweir     void DestroySubShell (
246cdf0e10cSrcweir         const SfxShell& rViewShell,
247cdf0e10cSrcweir         const ShellDescriptor& rDescriptor);
248cdf0e10cSrcweir };
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 
253cdf0e10cSrcweir //===== ViewShellManager ======================================================
254cdf0e10cSrcweir 
255cdf0e10cSrcweir ViewShellManager::ViewShellManager (ViewShellBase& rBase)
256cdf0e10cSrcweir     : mpImpl(new Implementation(*this,rBase)),
257cdf0e10cSrcweir       mbValid(true)
258cdf0e10cSrcweir {
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 
264cdf0e10cSrcweir ViewShellManager::~ViewShellManager (void)
265cdf0e10cSrcweir {
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
271cdf0e10cSrcweir void ViewShellManager::AddSubShellFactory (
272cdf0e10cSrcweir     ViewShell* pViewShell,
273cdf0e10cSrcweir     const SharedShellFactory& rpFactory)
274cdf0e10cSrcweir {
275cdf0e10cSrcweir     if (mbValid)
276cdf0e10cSrcweir         mpImpl->AddShellFactory(pViewShell, rpFactory);
277cdf0e10cSrcweir }
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 
282cdf0e10cSrcweir void ViewShellManager::RemoveSubShellFactory (
283cdf0e10cSrcweir     ViewShell* pViewShell,
284cdf0e10cSrcweir     const SharedShellFactory& rpFactory)
285cdf0e10cSrcweir {
286cdf0e10cSrcweir     if (mbValid)
287cdf0e10cSrcweir         mpImpl->RemoveShellFactory(pViewShell, rpFactory);
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 
293cdf0e10cSrcweir void ViewShellManager::ActivateViewShell (ViewShell* pViewShell)
294cdf0e10cSrcweir {
295cdf0e10cSrcweir     if (mbValid)
296cdf0e10cSrcweir         return mpImpl->ActivateViewShell(pViewShell);
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 
302cdf0e10cSrcweir void ViewShellManager::DeactivateViewShell (const ViewShell* pShell)
303cdf0e10cSrcweir {
304cdf0e10cSrcweir     if (mbValid && pShell!=NULL)
305cdf0e10cSrcweir         mpImpl->DeactivateViewShell(*pShell);
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 
311cdf0e10cSrcweir void ViewShellManager::MoveSubShellToTop (
312cdf0e10cSrcweir     const ViewShell& rParentShell,
313cdf0e10cSrcweir     ShellId nId)
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     if (mbValid)
316cdf0e10cSrcweir         mpImpl->MoveSubShellToTop(rParentShell, nId);
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 
322cdf0e10cSrcweir void ViewShellManager::SetFormShell (
323cdf0e10cSrcweir     const ViewShell* pParentShell,
324cdf0e10cSrcweir     FmFormShell* pFormShell,
325cdf0e10cSrcweir     bool bAbove)
326cdf0e10cSrcweir {
327cdf0e10cSrcweir     if (mbValid)
328cdf0e10cSrcweir         mpImpl->SetFormShell(pParentShell,pFormShell,bAbove);
329cdf0e10cSrcweir }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 
334cdf0e10cSrcweir void ViewShellManager::ActivateSubShell (const ViewShell& rViewShell, ShellId nId)
335cdf0e10cSrcweir {
336cdf0e10cSrcweir     if (mbValid)
337cdf0e10cSrcweir         mpImpl->ActivateSubShell(rViewShell,nId);
338cdf0e10cSrcweir }
339cdf0e10cSrcweir 
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 
343cdf0e10cSrcweir void ViewShellManager::DeactivateSubShell (const ViewShell& rViewShell, ShellId nId)
344cdf0e10cSrcweir {
345cdf0e10cSrcweir     if (mbValid)
346cdf0e10cSrcweir         mpImpl->DeactivateSubShell(rViewShell,nId);
347cdf0e10cSrcweir }
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 
352cdf0e10cSrcweir void ViewShellManager::InvalidateAllSubShells (ViewShell* pViewShell)
353cdf0e10cSrcweir {
354cdf0e10cSrcweir     if (mbValid)
355cdf0e10cSrcweir         mpImpl->InvalidateAllSubShells(pViewShell);
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 
360cdf0e10cSrcweir 
361cdf0e10cSrcweir void ViewShellManager::ActivateShell (SfxShell* pShell)
362cdf0e10cSrcweir {
363cdf0e10cSrcweir     if (mbValid && pShell!=NULL)
364cdf0e10cSrcweir         mpImpl->ActivateShell(*pShell);
365cdf0e10cSrcweir }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 
370cdf0e10cSrcweir void ViewShellManager::DeactivateShell (const SfxShell* pShell)
371cdf0e10cSrcweir {
372cdf0e10cSrcweir     if (mbValid && pShell!=NULL)
373cdf0e10cSrcweir         mpImpl->DeactivateShell(*pShell);
374cdf0e10cSrcweir }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 
378cdf0e10cSrcweir 
379cdf0e10cSrcweir void ViewShellManager::MoveToTop (const ViewShell& rParentShell)
380cdf0e10cSrcweir {
381cdf0e10cSrcweir     if (mbValid)
382cdf0e10cSrcweir         mpImpl->MoveToTop(rParentShell);
383cdf0e10cSrcweir }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 
387cdf0e10cSrcweir 
388cdf0e10cSrcweir SfxShell* ViewShellManager::GetShell (ShellId nId) const
389cdf0e10cSrcweir {
390cdf0e10cSrcweir     if (mbValid)
391cdf0e10cSrcweir         return mpImpl->GetShell(nId);
392cdf0e10cSrcweir     else
393cdf0e10cSrcweir         return NULL;
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 
399cdf0e10cSrcweir SfxShell* ViewShellManager::GetTopShell (void) const
400cdf0e10cSrcweir {
401cdf0e10cSrcweir     if (mbValid)
402cdf0e10cSrcweir         return mpImpl->GetTopShell();
403cdf0e10cSrcweir     else
404cdf0e10cSrcweir         return NULL;
405cdf0e10cSrcweir }
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 
410cdf0e10cSrcweir void ViewShellManager::Shutdown (void)
411cdf0e10cSrcweir {
412cdf0e10cSrcweir     if (mbValid)
413cdf0e10cSrcweir     {
414cdf0e10cSrcweir         mpImpl->Shutdown();
415cdf0e10cSrcweir         mbValid = false;
416cdf0e10cSrcweir     }
417cdf0e10cSrcweir }
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 
421cdf0e10cSrcweir void ViewShellManager::LockUpdate (void)
422cdf0e10cSrcweir {
423cdf0e10cSrcweir     mpImpl->LockUpdate();
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir void ViewShellManager::UnlockUpdate (void)
430cdf0e10cSrcweir {
431cdf0e10cSrcweir     mpImpl->UnlockUpdate();
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 
437cdf0e10cSrcweir //===== ViewShellManager::Implementation ======================================
438cdf0e10cSrcweir 
439cdf0e10cSrcweir ViewShellManager::Implementation::Implementation (
440cdf0e10cSrcweir     ViewShellManager& rManager,
441cdf0e10cSrcweir     ViewShellBase& rBase)
442cdf0e10cSrcweir     : mrBase(rBase),
443cdf0e10cSrcweir       maMutex(),
444cdf0e10cSrcweir       maShellFactories(),
445cdf0e10cSrcweir       maActiveViewShells(),
446cdf0e10cSrcweir       mnUpdateLockCount(0),
447cdf0e10cSrcweir       mbKeepMainViewShellOnTop(false),
448cdf0e10cSrcweir       mbShellStackIsUpToDate(true),
449cdf0e10cSrcweir       mpFormShell(NULL),
450cdf0e10cSrcweir       mpFormShellParent(NULL),
451cdf0e10cSrcweir       mbFormShellAboveParent(true),
452cdf0e10cSrcweir       mpTopShell(NULL)
453cdf0e10cSrcweir {
454cdf0e10cSrcweir     (void)rManager;
455cdf0e10cSrcweir }
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 
460cdf0e10cSrcweir ViewShellManager::Implementation::~Implementation (void)
461cdf0e10cSrcweir {
462cdf0e10cSrcweir     Shutdown();
463cdf0e10cSrcweir }
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 
467cdf0e10cSrcweir 
468cdf0e10cSrcweir void ViewShellManager::Implementation::AddShellFactory (
469cdf0e10cSrcweir     const SfxShell* pViewShell,
470cdf0e10cSrcweir     const SharedShellFactory& rpFactory)
471cdf0e10cSrcweir {
472cdf0e10cSrcweir     bool bAlreadyAdded (false);
473cdf0e10cSrcweir 
474cdf0e10cSrcweir     // Check that the given factory has not already been added.
475cdf0e10cSrcweir     ::std::pair<FactoryList::iterator,FactoryList::iterator> aRange(
476cdf0e10cSrcweir         maShellFactories.equal_range(pViewShell));
477cdf0e10cSrcweir     for (FactoryList::const_iterator iFactory=aRange.first; iFactory!=aRange.second; ++iFactory)
478cdf0e10cSrcweir         if (iFactory->second == rpFactory)
479cdf0e10cSrcweir         {
480cdf0e10cSrcweir             bAlreadyAdded = true;
481cdf0e10cSrcweir             break;
482cdf0e10cSrcweir         }
483cdf0e10cSrcweir 
484cdf0e10cSrcweir     // Add the factory if it is not already present.
485cdf0e10cSrcweir     if ( ! bAlreadyAdded)
486cdf0e10cSrcweir         maShellFactories.insert(FactoryList::value_type(pViewShell, rpFactory));
487cdf0e10cSrcweir }
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 
490cdf0e10cSrcweir 
491cdf0e10cSrcweir 
492cdf0e10cSrcweir void ViewShellManager::Implementation::RemoveShellFactory (
493cdf0e10cSrcweir     const SfxShell* pViewShell,
494cdf0e10cSrcweir     const SharedShellFactory& rpFactory)
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     ::std::pair<FactoryList::iterator,FactoryList::iterator> aRange(
497cdf0e10cSrcweir         maShellFactories.equal_range(pViewShell));
498cdf0e10cSrcweir     for (FactoryList::iterator iFactory=aRange.first; iFactory!=aRange.second; ++iFactory)
499cdf0e10cSrcweir         if (iFactory->second == rpFactory)
500cdf0e10cSrcweir         {
501cdf0e10cSrcweir             maShellFactories.erase(iFactory);
502cdf0e10cSrcweir             break;
503cdf0e10cSrcweir         }
504cdf0e10cSrcweir }
505cdf0e10cSrcweir 
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 
509cdf0e10cSrcweir void ViewShellManager::Implementation::ActivateViewShell (ViewShell* pViewShell)
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
512cdf0e10cSrcweir 
513cdf0e10cSrcweir     ShellDescriptor aResult;
514cdf0e10cSrcweir     aResult.mpShell = pViewShell;
515cdf0e10cSrcweir 
516cdf0e10cSrcweir     // Register as window listener so that the shells of the current
517cdf0e10cSrcweir     // window can be moved to the top of the shell stack.
518cdf0e10cSrcweir     if (aResult.mpShell != NULL)
519cdf0e10cSrcweir     {
520cdf0e10cSrcweir         ::Window* pWindow = aResult.GetWindow();
521cdf0e10cSrcweir         if (pWindow != NULL)
522cdf0e10cSrcweir             pWindow->AddEventListener(
523cdf0e10cSrcweir                 LINK(this, ViewShellManager::Implementation, WindowEventHandler));
524cdf0e10cSrcweir         else
525cdf0e10cSrcweir         {
526cdf0e10cSrcweir             DBG_ASSERT(false,
527cdf0e10cSrcweir                 "ViewShellManager::ActivateViewShell: "
528cdf0e10cSrcweir                 "new view shell has no active window");
529cdf0e10cSrcweir         }
530cdf0e10cSrcweir     }
531cdf0e10cSrcweir 
532cdf0e10cSrcweir     ActivateShell(aResult);
533cdf0e10cSrcweir }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir 
536cdf0e10cSrcweir 
537cdf0e10cSrcweir 
538cdf0e10cSrcweir void ViewShellManager::Implementation::DeactivateViewShell (const ViewShell& rShell)
539cdf0e10cSrcweir {
540cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
541cdf0e10cSrcweir 
542cdf0e10cSrcweir     ActiveShellList::iterator iShell (::std::find_if (
543cdf0e10cSrcweir         maActiveViewShells.begin(),
544cdf0e10cSrcweir         maActiveViewShells.end(),
545cdf0e10cSrcweir         IsShell(&rShell)));
546cdf0e10cSrcweir     if (iShell != maActiveViewShells.end())
547cdf0e10cSrcweir     {
548cdf0e10cSrcweir         UpdateLock aLocker (*this);
549cdf0e10cSrcweir 
550cdf0e10cSrcweir         ShellDescriptor aDescriptor(*iShell);
551cdf0e10cSrcweir         mrBase.GetDocShell()->Disconnect(dynamic_cast<ViewShell*>(aDescriptor.mpShell));
552cdf0e10cSrcweir         maActiveViewShells.erase(iShell);
553cdf0e10cSrcweir         TakeShellsFromStack(aDescriptor.mpShell);
554cdf0e10cSrcweir 
555cdf0e10cSrcweir         // Deactivate sub shells.
556cdf0e10cSrcweir         SubShellList::iterator iList (maActiveSubShells.find(&rShell));
557cdf0e10cSrcweir         if (iList != maActiveSubShells.end())
558cdf0e10cSrcweir         {
559cdf0e10cSrcweir             SubShellSubList& rList (iList->second);
560cdf0e10cSrcweir             while ( ! rList.empty())
561cdf0e10cSrcweir                 DeactivateSubShell(rShell, rList.front().mnId);
562cdf0e10cSrcweir         }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir         DestroyViewShell(aDescriptor);
565cdf0e10cSrcweir     }
566cdf0e10cSrcweir }
567cdf0e10cSrcweir 
568cdf0e10cSrcweir 
569cdf0e10cSrcweir 
570cdf0e10cSrcweir 
571cdf0e10cSrcweir void ViewShellManager::Implementation::ActivateShell (SfxShell& rShell)
572cdf0e10cSrcweir {
573cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
574cdf0e10cSrcweir 
575cdf0e10cSrcweir     // Create a new shell or recycle on in the cache.
576cdf0e10cSrcweir     ShellDescriptor aDescriptor;
577cdf0e10cSrcweir     aDescriptor.mpShell = &rShell;
578cdf0e10cSrcweir 
579cdf0e10cSrcweir     ActivateShell(aDescriptor);
580cdf0e10cSrcweir }
581cdf0e10cSrcweir 
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 
585cdf0e10cSrcweir void ViewShellManager::Implementation::ActivateShell (const ShellDescriptor& rDescriptor)
586cdf0e10cSrcweir {
587cdf0e10cSrcweir     // Put shell on top of the active view shells.
588cdf0e10cSrcweir     if (rDescriptor.mpShell != NULL)
589cdf0e10cSrcweir     {
590cdf0e10cSrcweir         // Determine where to put the view shell on the stack.  By default
591cdf0e10cSrcweir         // it is put on top of the stack.  When the view shell of the center
592cdf0e10cSrcweir         // pane is to be kept top most and the new view shell is not
593cdf0e10cSrcweir         // displayed in the center pane then it is inserted at the position
594cdf0e10cSrcweir         // one below the top.
595cdf0e10cSrcweir         ActiveShellList::iterator iInsertPosition (maActiveViewShells.begin());
596cdf0e10cSrcweir         if (iInsertPosition != maActiveViewShells.end()
597cdf0e10cSrcweir             && mbKeepMainViewShellOnTop
598cdf0e10cSrcweir             && ! rDescriptor.IsMainViewShell()
599cdf0e10cSrcweir             && iInsertPosition->IsMainViewShell())
600cdf0e10cSrcweir         {
601cdf0e10cSrcweir             ++iInsertPosition;
602cdf0e10cSrcweir         }
603cdf0e10cSrcweir         maActiveViewShells.insert(
604cdf0e10cSrcweir             iInsertPosition,
605cdf0e10cSrcweir             rDescriptor);
606cdf0e10cSrcweir     }
607cdf0e10cSrcweir }
608cdf0e10cSrcweir 
609cdf0e10cSrcweir 
610cdf0e10cSrcweir 
611cdf0e10cSrcweir 
612cdf0e10cSrcweir void ViewShellManager::Implementation::DeactivateShell (const SfxShell& rShell)
613cdf0e10cSrcweir {
614cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
615cdf0e10cSrcweir 
616cdf0e10cSrcweir     ActiveShellList::iterator iShell (::std::find_if (
617cdf0e10cSrcweir         maActiveViewShells.begin(),
618cdf0e10cSrcweir         maActiveViewShells.end(),
619cdf0e10cSrcweir         IsShell(&rShell)));
620cdf0e10cSrcweir     if (iShell != maActiveViewShells.end())
621cdf0e10cSrcweir     {
622cdf0e10cSrcweir         UpdateLock aLocker (*this);
623cdf0e10cSrcweir 
624cdf0e10cSrcweir         ShellDescriptor aDescriptor(*iShell);
625cdf0e10cSrcweir         mrBase.GetDocShell()->Disconnect(dynamic_cast<ViewShell*>(aDescriptor.mpShell));
626cdf0e10cSrcweir         maActiveViewShells.erase(iShell);
627cdf0e10cSrcweir         TakeShellsFromStack(aDescriptor.mpShell);
628cdf0e10cSrcweir 
629cdf0e10cSrcweir         // Deactivate sub shells.
630cdf0e10cSrcweir         SubShellList::iterator iList (maActiveSubShells.find(&rShell));
631cdf0e10cSrcweir         if (iList != maActiveSubShells.end())
632cdf0e10cSrcweir         {
633cdf0e10cSrcweir             SubShellSubList& rList (iList->second);
634cdf0e10cSrcweir             while ( ! rList.empty())
635cdf0e10cSrcweir                 DeactivateSubShell(rShell, rList.front().mnId);
636cdf0e10cSrcweir         }
637cdf0e10cSrcweir 
638cdf0e10cSrcweir         DestroyViewShell(aDescriptor);
639cdf0e10cSrcweir     }
640cdf0e10cSrcweir }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 
644cdf0e10cSrcweir 
645cdf0e10cSrcweir void ViewShellManager::Implementation::ActivateSubShell (
646cdf0e10cSrcweir     const SfxShell& rParentShell,
647cdf0e10cSrcweir     ShellId nId)
648cdf0e10cSrcweir {
649cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
650cdf0e10cSrcweir 
651cdf0e10cSrcweir     do
652cdf0e10cSrcweir     {
653cdf0e10cSrcweir         // Check that the given view shell is active.
654cdf0e10cSrcweir         ActiveShellList::iterator iShell (::std::find_if (
655cdf0e10cSrcweir             maActiveViewShells.begin(),
656cdf0e10cSrcweir             maActiveViewShells.end(),
657cdf0e10cSrcweir             IsShell(&rParentShell)));
658cdf0e10cSrcweir         if (iShell == maActiveViewShells.end())
659cdf0e10cSrcweir             break;
660cdf0e10cSrcweir 
661cdf0e10cSrcweir         // Create the sub shell list if it does not yet exist.
662cdf0e10cSrcweir         SubShellList::iterator iList (maActiveSubShells.find(&rParentShell));
663cdf0e10cSrcweir         if (iList == maActiveSubShells.end())
664cdf0e10cSrcweir             iList = maActiveSubShells.insert(
665cdf0e10cSrcweir                 SubShellList::value_type(&rParentShell,SubShellSubList())).first;
666cdf0e10cSrcweir 
667cdf0e10cSrcweir         // Do not activate an object bar that is already active.  Requesting
668cdf0e10cSrcweir         // this is not exactly an error but may be an indication of one.
669cdf0e10cSrcweir         SubShellSubList& rList (iList->second);
670cdf0e10cSrcweir         if (::std::find_if(rList.begin(),rList.end(), IsId(nId)) != rList.end())
671cdf0e10cSrcweir             break;
672cdf0e10cSrcweir 
673cdf0e10cSrcweir         // Add just the id of the sub shell. The actual shell is created
674cdf0e10cSrcweir         // later in CreateShells().
675cdf0e10cSrcweir         UpdateLock aLock (*this);
676cdf0e10cSrcweir         rList.push_back(ShellDescriptor(NULL, nId));
677cdf0e10cSrcweir     }
678cdf0e10cSrcweir     while (false);
679cdf0e10cSrcweir }
680cdf0e10cSrcweir 
681cdf0e10cSrcweir 
682cdf0e10cSrcweir 
683cdf0e10cSrcweir 
684cdf0e10cSrcweir void ViewShellManager::Implementation::DeactivateSubShell (
685cdf0e10cSrcweir     const SfxShell& rParentShell,
686cdf0e10cSrcweir     ShellId nId)
687cdf0e10cSrcweir {
688cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
689cdf0e10cSrcweir 
690cdf0e10cSrcweir     do
691cdf0e10cSrcweir     {
692cdf0e10cSrcweir         // Check that the given view shell is active.
693cdf0e10cSrcweir         SubShellList::iterator iList (maActiveSubShells.find(&rParentShell));
694cdf0e10cSrcweir         if (iList == maActiveSubShells.end())
695cdf0e10cSrcweir             break;
696cdf0e10cSrcweir 
697cdf0e10cSrcweir         // Look up the sub shell.
698cdf0e10cSrcweir         SubShellSubList& rList (iList->second);
699cdf0e10cSrcweir         SubShellSubList::iterator iShell (
700cdf0e10cSrcweir             ::std::find_if(rList.begin(),rList.end(), IsId(nId)));
701cdf0e10cSrcweir         if (iShell == rList.end())
702cdf0e10cSrcweir             break;
703cdf0e10cSrcweir         SfxShell* pShell = iShell->mpShell;
704cdf0e10cSrcweir         if (pShell == NULL)
705cdf0e10cSrcweir             break;
706cdf0e10cSrcweir 
707cdf0e10cSrcweir         UpdateLock aLock (*this);
708cdf0e10cSrcweir 
709cdf0e10cSrcweir         ShellDescriptor aDescriptor(*iShell);
710cdf0e10cSrcweir         // Remove the sub shell from both the internal structure as well as the
711cdf0e10cSrcweir         // SFX shell stack above and including the sub shell.
712cdf0e10cSrcweir         rList.erase(iShell);
713cdf0e10cSrcweir         TakeShellsFromStack(pShell);
714cdf0e10cSrcweir 
715cdf0e10cSrcweir         DestroySubShell(rParentShell, aDescriptor);
716cdf0e10cSrcweir     }
717cdf0e10cSrcweir     while (false);
718cdf0e10cSrcweir }
719cdf0e10cSrcweir 
720cdf0e10cSrcweir 
721cdf0e10cSrcweir 
722cdf0e10cSrcweir 
723cdf0e10cSrcweir void ViewShellManager::Implementation::MoveSubShellToTop (
724cdf0e10cSrcweir     const SfxShell& rParentShell,
725cdf0e10cSrcweir     ShellId nId)
726cdf0e10cSrcweir {
727cdf0e10cSrcweir     SubShellList::iterator iList (maActiveSubShells.find(&rParentShell));
728cdf0e10cSrcweir     if (iList != maActiveSubShells.end())
729cdf0e10cSrcweir     {
730cdf0e10cSrcweir         // Look up the sub shell.
731cdf0e10cSrcweir         SubShellSubList& rList (iList->second);
732cdf0e10cSrcweir         SubShellSubList::iterator iShell (
733cdf0e10cSrcweir             ::std::find_if(rList.begin(),rList.end(), IsId(nId)));
734cdf0e10cSrcweir         if (iShell!=rList.end() && iShell!=rList.begin())
735cdf0e10cSrcweir         {
736cdf0e10cSrcweir             SubShellSubList::value_type aEntry (*iShell);
737cdf0e10cSrcweir             rList.erase(iShell);
738cdf0e10cSrcweir             rList.push_front(aEntry);
739cdf0e10cSrcweir         }
740cdf0e10cSrcweir     }
741cdf0e10cSrcweir     else
742cdf0e10cSrcweir     {
743cdf0e10cSrcweir         // Ignore this call when there are no sub shells for the given
744cdf0e10cSrcweir         // parent shell.  We could remember the sub shell to move to the top
745cdf0e10cSrcweir         // but we do not.  Do call this method at a later time instead.
746cdf0e10cSrcweir     }
747cdf0e10cSrcweir }
748cdf0e10cSrcweir 
749cdf0e10cSrcweir 
750cdf0e10cSrcweir 
751cdf0e10cSrcweir void ViewShellManager::Implementation::MoveToTop (const SfxShell& rShell)
752cdf0e10cSrcweir {
753cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
754cdf0e10cSrcweir 
755cdf0e10cSrcweir     // Check that we have access to a dispatcher.  If not, then we are
756cdf0e10cSrcweir     // (probably) called while the view shell is still being created or
757cdf0e10cSrcweir     // initialized.  Without dispatcher we can not rebuild the shell stack
758cdf0e10cSrcweir     // to move the requested shell to the top.  So return right away instead
759cdf0e10cSrcweir     // of making a mess without being able to clean up afterwards.
760cdf0e10cSrcweir     if (mrBase.GetDispatcher() == NULL)
761cdf0e10cSrcweir         return;
762cdf0e10cSrcweir 
763cdf0e10cSrcweir     ActiveShellList::iterator iShell (::std::find_if (
764cdf0e10cSrcweir         maActiveViewShells.begin(),
765cdf0e10cSrcweir         maActiveViewShells.end(),
766cdf0e10cSrcweir         IsShell(&rShell)));
767cdf0e10cSrcweir     bool bMove = true;
768cdf0e10cSrcweir     if (iShell != maActiveViewShells.end())
769cdf0e10cSrcweir     {
770cdf0e10cSrcweir         // Is the shell already at the top of the stack?  We have to keep
771cdf0e10cSrcweir         // the case in mind that mbKeepMainViewShellOnTop is true.  Shells
772cdf0e10cSrcweir         // that are not the main view shell are placed on the second-to-top
773cdf0e10cSrcweir         // position in this case.
774cdf0e10cSrcweir             if (iShell == maActiveViewShells.begin()
775cdf0e10cSrcweir             && (iShell->IsMainViewShell() || ! mbKeepMainViewShellOnTop))
776cdf0e10cSrcweir         {
777cdf0e10cSrcweir             // The shell is at the top position and is either a) the main
778cdf0e10cSrcweir             // view shell or b) another shell but the main view shell is not
779cdf0e10cSrcweir             // kept at the top position.  We do not have to move the shell.
780cdf0e10cSrcweir             bMove = false;
781cdf0e10cSrcweir         }
782cdf0e10cSrcweir         else if (iShell == ++maActiveViewShells.begin()
783cdf0e10cSrcweir             && ! iShell->IsMainViewShell()
784cdf0e10cSrcweir             && mbKeepMainViewShellOnTop)
785cdf0e10cSrcweir         {
786cdf0e10cSrcweir             // The shell is a the second-to-top position, not the main view
787cdf0e10cSrcweir             // shell and the main view shell is kept at the top position.
788cdf0e10cSrcweir             // Therefore we do not have to move the shell.
789cdf0e10cSrcweir             bMove = false;
790cdf0e10cSrcweir         }
791cdf0e10cSrcweir     }
792cdf0e10cSrcweir     else
793cdf0e10cSrcweir     {
794cdf0e10cSrcweir         // The shell is not on the stack.  Therefore it can not be moved.
795cdf0e10cSrcweir         // We could insert it but we don't.  Use ActivateViewShell() for
796cdf0e10cSrcweir         // that.
797cdf0e10cSrcweir         bMove = false;
798cdf0e10cSrcweir     }
799cdf0e10cSrcweir 
800cdf0e10cSrcweir     // When the shell is not at the right position it is removed from the
801cdf0e10cSrcweir     // internal list of shells and inserted at the correct position.
802cdf0e10cSrcweir     if (bMove)
803cdf0e10cSrcweir     {
804cdf0e10cSrcweir         UpdateLock aLock (*this);
805cdf0e10cSrcweir 
806cdf0e10cSrcweir         ShellDescriptor aDescriptor(*iShell);
807cdf0e10cSrcweir 
808cdf0e10cSrcweir         TakeShellsFromStack(&rShell);
809cdf0e10cSrcweir         maActiveViewShells.erase(iShell);
810cdf0e10cSrcweir 
811cdf0e10cSrcweir         // Find out whether to insert at the top or one below.
812cdf0e10cSrcweir         ActiveShellList::iterator aInsertPosition (maActiveViewShells.begin());
813cdf0e10cSrcweir         if (mbKeepMainViewShellOnTop && ! aDescriptor.IsMainViewShell())
814cdf0e10cSrcweir         {
815cdf0e10cSrcweir             if (maActiveViewShells.back().IsMainViewShell())
816cdf0e10cSrcweir                 aInsertPosition++;
817cdf0e10cSrcweir         }
818cdf0e10cSrcweir 
819cdf0e10cSrcweir         maActiveViewShells.insert(aInsertPosition, aDescriptor);
820cdf0e10cSrcweir     }
821cdf0e10cSrcweir }
822cdf0e10cSrcweir 
823cdf0e10cSrcweir 
824cdf0e10cSrcweir 
825cdf0e10cSrcweir 
826cdf0e10cSrcweir SfxShell* ViewShellManager::Implementation::GetShell (ShellId nId) const
827cdf0e10cSrcweir {
828cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
829cdf0e10cSrcweir 
830cdf0e10cSrcweir     SfxShell* pShell = NULL;
831cdf0e10cSrcweir 
832cdf0e10cSrcweir     // First search the active view shells.
833cdf0e10cSrcweir     ActiveShellList::const_iterator iShell (
834cdf0e10cSrcweir         ::std::find_if (
835cdf0e10cSrcweir         maActiveViewShells.begin(),
836cdf0e10cSrcweir         maActiveViewShells.end(),
837cdf0e10cSrcweir         IsId(nId)));
838cdf0e10cSrcweir     if (iShell != maActiveViewShells.end())
839cdf0e10cSrcweir         pShell = iShell->mpShell;
840cdf0e10cSrcweir     else
841cdf0e10cSrcweir     {
842cdf0e10cSrcweir         // Now search the active sub shells of every active view shell.
843cdf0e10cSrcweir         SubShellList::const_iterator iList;
844cdf0e10cSrcweir         for (iList=maActiveSubShells.begin(); iList!=maActiveSubShells.end(); ++iList)
845cdf0e10cSrcweir         {
846cdf0e10cSrcweir             const SubShellSubList& rList (iList->second);
847cdf0e10cSrcweir             SubShellSubList::const_iterator iSubShell(
848cdf0e10cSrcweir                 ::std::find_if(rList.begin(),rList.end(), IsId(nId)));
849cdf0e10cSrcweir             if (iSubShell != rList.end())
850cdf0e10cSrcweir             {
851cdf0e10cSrcweir                 pShell = iSubShell->mpShell;
852cdf0e10cSrcweir                 break;
853cdf0e10cSrcweir             }
854cdf0e10cSrcweir         }
855cdf0e10cSrcweir     }
856cdf0e10cSrcweir 
857cdf0e10cSrcweir     return pShell;
858cdf0e10cSrcweir }
859cdf0e10cSrcweir 
860cdf0e10cSrcweir 
861cdf0e10cSrcweir 
862cdf0e10cSrcweir 
863cdf0e10cSrcweir SfxShell* ViewShellManager::Implementation::GetTopShell (void) const
864cdf0e10cSrcweir {
865cdf0e10cSrcweir     OSL_ASSERT(mpTopShell == mrBase.GetSubShell(0));
866cdf0e10cSrcweir     return mpTopShell;
867cdf0e10cSrcweir }
868cdf0e10cSrcweir 
869cdf0e10cSrcweir 
870cdf0e10cSrcweir 
871cdf0e10cSrcweir 
872cdf0e10cSrcweir void ViewShellManager::Implementation::LockUpdate (void)
873cdf0e10cSrcweir {
874cdf0e10cSrcweir     mnUpdateLockCount++;
875cdf0e10cSrcweir }
876cdf0e10cSrcweir 
877cdf0e10cSrcweir 
878cdf0e10cSrcweir 
879cdf0e10cSrcweir 
880cdf0e10cSrcweir void ViewShellManager::Implementation::UnlockUpdate (void)
881cdf0e10cSrcweir {
882cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
883cdf0e10cSrcweir 
884cdf0e10cSrcweir     mnUpdateLockCount--;
885cdf0e10cSrcweir     if (mnUpdateLockCount < 0)
886cdf0e10cSrcweir     {
887cdf0e10cSrcweir         // This should not happen.
888cdf0e10cSrcweir         OSL_ASSERT (mnUpdateLockCount>=0);
889cdf0e10cSrcweir         mnUpdateLockCount = 0;
890cdf0e10cSrcweir     }
891cdf0e10cSrcweir     if (mnUpdateLockCount == 0)
892cdf0e10cSrcweir         UpdateShellStack();
893cdf0e10cSrcweir }
894cdf0e10cSrcweir 
895cdf0e10cSrcweir 
896cdf0e10cSrcweir 
897cdf0e10cSrcweir 
898cdf0e10cSrcweir /** Update the SFX shell stack (the portion that is visible to us) so that
899cdf0e10cSrcweir     it matches the internal shell stack.  This is done in six steps:
900cdf0e10cSrcweir     1. Create the missing view shells and sub shells.
901cdf0e10cSrcweir     2. Set up the internal shell stack.
902cdf0e10cSrcweir     3. Get the SFX shell stack.
903cdf0e10cSrcweir     4. Find the lowest shell in which the two stacks differ.
904cdf0e10cSrcweir     5. Remove all shells above and including that shell from the SFX stack.
905cdf0e10cSrcweir     6. Push all shells of the internal stack on the SFX shell stack that are
906cdf0e10cSrcweir     not already present on the later.
907cdf0e10cSrcweir */
908cdf0e10cSrcweir void ViewShellManager::Implementation::UpdateShellStack (void)
909cdf0e10cSrcweir {
910cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
911cdf0e10cSrcweir 
912cdf0e10cSrcweir     // Remember the undo manager from the top-most shell on the stack.
913cdf0e10cSrcweir     SfxShell* pTopMostShell = mrBase.GetSubShell(0);
914cdf0e10cSrcweir     ::svl::IUndoManager* pUndoManager = (pTopMostShell!=NULL)
915cdf0e10cSrcweir         ? pTopMostShell->GetUndoManager()
916cdf0e10cSrcweir         : NULL;
917cdf0e10cSrcweir 
918cdf0e10cSrcweir     // 1. Create the missing shells.
919cdf0e10cSrcweir     CreateShells();
920cdf0e10cSrcweir 
921cdf0e10cSrcweir 
922cdf0e10cSrcweir     // 2. Create the internal target stack.
923cdf0e10cSrcweir     ShellStack aTargetStack;
924cdf0e10cSrcweir     CreateTargetStack(aTargetStack);
925cdf0e10cSrcweir 
926cdf0e10cSrcweir 
927cdf0e10cSrcweir     // 3. Get SFX shell stack.
928cdf0e10cSrcweir     ShellStack aSfxShellStack;
929cdf0e10cSrcweir     sal_uInt16 nIndex (0);
930cdf0e10cSrcweir     while (mrBase.GetSubShell(nIndex)!=NULL)
931cdf0e10cSrcweir         ++nIndex;
932cdf0e10cSrcweir     aSfxShellStack.reserve(nIndex);
933cdf0e10cSrcweir     while (nIndex-- > 0)
934cdf0e10cSrcweir         aSfxShellStack.push_back(mrBase.GetSubShell(nIndex));
935cdf0e10cSrcweir 
936cdf0e10cSrcweir 
937cdf0e10cSrcweir #ifdef VERBOSE
938cdf0e10cSrcweir     OSL_TRACE("Current SFX Stack\r");
939cdf0e10cSrcweir     DumpShellStack(aSfxShellStack);
940cdf0e10cSrcweir     OSL_TRACE("Target Stack\r");
941cdf0e10cSrcweir     DumpShellStack(aTargetStack);
942cdf0e10cSrcweir #endif
943cdf0e10cSrcweir 
944cdf0e10cSrcweir 
945cdf0e10cSrcweir     // 4. Find the lowest shell in which the two stacks differ.
946cdf0e10cSrcweir     ShellStack::const_iterator iSfxShell (aSfxShellStack.begin());
947cdf0e10cSrcweir     ShellStack::iterator iTargetShell (aTargetStack.begin());
948cdf0e10cSrcweir     while (iSfxShell != aSfxShellStack.end()
949cdf0e10cSrcweir         && iTargetShell!=aTargetStack.end()
950cdf0e10cSrcweir         && (*iSfxShell)==(*iTargetShell))
951cdf0e10cSrcweir     {
952cdf0e10cSrcweir         ++iSfxShell;
953cdf0e10cSrcweir         ++iTargetShell;
954cdf0e10cSrcweir     }
955cdf0e10cSrcweir 
956cdf0e10cSrcweir 
957cdf0e10cSrcweir     // 5. Remove all shells above and including the differing shell from the
958cdf0e10cSrcweir     // SFX stack starting with the shell on top of the stack.
959cdf0e10cSrcweir     while (iSfxShell != aSfxShellStack.end())
960cdf0e10cSrcweir     {
961cdf0e10cSrcweir         SfxShell* pShell = aSfxShellStack.back();
962cdf0e10cSrcweir         aSfxShellStack.pop_back();
963cdf0e10cSrcweir #ifdef VERBOSE
964cdf0e10cSrcweir         OSL_TRACE("removing shell %p from stack\r", pShell);
965cdf0e10cSrcweir #endif
966cdf0e10cSrcweir         mrBase.RemoveSubShell(pShell);
967cdf0e10cSrcweir     }
968cdf0e10cSrcweir 
969cdf0e10cSrcweir 
970cdf0e10cSrcweir     // 6. Push shells from the given stack onto the SFX stack.
971cdf0e10cSrcweir     mbShellStackIsUpToDate = false;
972cdf0e10cSrcweir     while (iTargetShell != aTargetStack.end())
973cdf0e10cSrcweir     {
974cdf0e10cSrcweir #ifdef VERBOSE
975cdf0e10cSrcweir         OSL_TRACE("pushing shell %p on stack\r", *iTargetShell);
976cdf0e10cSrcweir #endif
977cdf0e10cSrcweir         mrBase.AddSubShell(**iTargetShell);
978cdf0e10cSrcweir         ++iTargetShell;
979cdf0e10cSrcweir 
980cdf0e10cSrcweir         // The pushing of the shell on to the shell stack may have lead to
981cdf0e10cSrcweir         // another invocation of this method.  In this case we have to abort
982cdf0e10cSrcweir         // pushing shells on the stack and return immediately.
983cdf0e10cSrcweir         if (mbShellStackIsUpToDate)
984cdf0e10cSrcweir             break;
985cdf0e10cSrcweir     }
986cdf0e10cSrcweir     if (mrBase.GetDispatcher() != NULL)
987cdf0e10cSrcweir         mrBase.GetDispatcher()->Flush();
988cdf0e10cSrcweir 
989cdf0e10cSrcweir     // Update the pointer to the top-most shell and set its undo manager
990cdf0e10cSrcweir     // to the one of the previous top-most shell.
991cdf0e10cSrcweir     mpTopShell = mrBase.GetSubShell(0);
992cdf0e10cSrcweir     if (mpTopShell!=NULL && pUndoManager!=NULL && mpTopShell->GetUndoManager()==NULL)
993cdf0e10cSrcweir         mpTopShell->SetUndoManager(pUndoManager);
994cdf0e10cSrcweir 
995cdf0e10cSrcweir     // Finally tell an invocation of this method on a higher level that it can (has
996cdf0e10cSrcweir     // to) abort and return immediately.
997cdf0e10cSrcweir     mbShellStackIsUpToDate = true;
998cdf0e10cSrcweir 
999cdf0e10cSrcweir #ifdef VERBOSE
1000cdf0e10cSrcweir     OSL_TRACE("New current stack\r");
1001cdf0e10cSrcweir     DumpSfxShellStack();
1002cdf0e10cSrcweir #endif
1003cdf0e10cSrcweir }
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir 
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir void ViewShellManager::Implementation::TakeShellsFromStack (const SfxShell* pShell)
1009cdf0e10cSrcweir {
1010cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
1011cdf0e10cSrcweir 
1012cdf0e10cSrcweir     // Remember the undo manager from the top-most shell on the stack.
1013cdf0e10cSrcweir     SfxShell* pTopMostShell = mrBase.GetSubShell(0);
1014cdf0e10cSrcweir     ::svl::IUndoManager* pUndoManager = (pTopMostShell!=NULL)
1015cdf0e10cSrcweir         ? pTopMostShell->GetUndoManager()
1016cdf0e10cSrcweir         : NULL;
1017cdf0e10cSrcweir 
1018cdf0e10cSrcweir #ifdef VERBOSE
1019cdf0e10cSrcweir     OSL_TRACE("TakeShellsFromStack(%p)\r", pShell);
1020cdf0e10cSrcweir     DumpSfxShellStack();
1021cdf0e10cSrcweir #endif
1022cdf0e10cSrcweir 
1023cdf0e10cSrcweir     // 0.Make sure that the given shell is on the stack.  This is a
1024cdf0e10cSrcweir     // preparation for the following assertion.
1025cdf0e10cSrcweir     for (sal_uInt16 nIndex=0; true; nIndex++)
1026cdf0e10cSrcweir     {
1027cdf0e10cSrcweir         SfxShell* pShellOnStack = mrBase.GetSubShell(nIndex);
1028cdf0e10cSrcweir         if (pShellOnStack == NULL)
1029cdf0e10cSrcweir         {
1030cdf0e10cSrcweir             // Set pShell to NULL to indicate the following code that the
1031cdf0e10cSrcweir             // shell is not on the stack.
1032cdf0e10cSrcweir             pShell = NULL;
1033cdf0e10cSrcweir             break;
1034cdf0e10cSrcweir         }
1035cdf0e10cSrcweir         else if (pShellOnStack == pShell)
1036cdf0e10cSrcweir             break;
1037cdf0e10cSrcweir     }
1038cdf0e10cSrcweir 
1039cdf0e10cSrcweir     if (pShell != NULL)
1040cdf0e10cSrcweir     {
1041cdf0e10cSrcweir         // 1. Deactivate our shells on the stack before they are removed so
1042cdf0e10cSrcweir         // that during the Deactivation() calls the stack is still intact.
1043cdf0e10cSrcweir         for (sal_uInt16 nIndex=0; true; nIndex++)
1044cdf0e10cSrcweir         {
1045cdf0e10cSrcweir             SfxShell* pShellOnStack = mrBase.GetSubShell(nIndex);
1046cdf0e10cSrcweir             Deactivate(pShellOnStack);
1047cdf0e10cSrcweir             if (pShellOnStack == pShell)
1048cdf0e10cSrcweir                 break;
1049cdf0e10cSrcweir         }
1050cdf0e10cSrcweir 
1051cdf0e10cSrcweir         // 2. Remove the shells from the stack.
1052cdf0e10cSrcweir         while (true)
1053cdf0e10cSrcweir         {
1054cdf0e10cSrcweir             SfxShell* pShellOnStack = mrBase.GetSubShell(0);
1055cdf0e10cSrcweir #ifdef VERBOSE
1056cdf0e10cSrcweir             OSL_TRACE("removing shell %p from stack\r", pShellOnStack);
1057cdf0e10cSrcweir #endif
1058cdf0e10cSrcweir             mrBase.RemoveSubShell(pShellOnStack);
1059cdf0e10cSrcweir             if (pShellOnStack == pShell)
1060cdf0e10cSrcweir                 break;
1061cdf0e10cSrcweir         }
1062cdf0e10cSrcweir 
1063cdf0e10cSrcweir         // 3. Update the stack.
1064cdf0e10cSrcweir         if (mrBase.GetDispatcher() != NULL)
1065cdf0e10cSrcweir             mrBase.GetDispatcher()->Flush();
1066cdf0e10cSrcweir 
1067cdf0e10cSrcweir         // Update the pointer to the top-most shell and set its undo manager
1068cdf0e10cSrcweir         // to the one of the previous top-most shell.
1069cdf0e10cSrcweir         mpTopShell = mrBase.GetSubShell(0);
1070cdf0e10cSrcweir         if (mpTopShell!=NULL && pUndoManager!=NULL && mpTopShell->GetUndoManager()==NULL)
1071cdf0e10cSrcweir             mpTopShell->SetUndoManager(pUndoManager);
1072cdf0e10cSrcweir     }
1073cdf0e10cSrcweir 
1074cdf0e10cSrcweir #ifdef VERBOSE
1075cdf0e10cSrcweir     OSL_TRACE("Sfx shell stack is:\r");
1076cdf0e10cSrcweir     DumpSfxShellStack();
1077cdf0e10cSrcweir #endif
1078cdf0e10cSrcweir }
1079cdf0e10cSrcweir 
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir 
1082cdf0e10cSrcweir 
1083cdf0e10cSrcweir void ViewShellManager::Implementation::CreateShells (void)
1084cdf0e10cSrcweir {
1085cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
1086cdf0e10cSrcweir 
1087cdf0e10cSrcweir     // Iterate over all view shells.
1088cdf0e10cSrcweir     ShellStack aShellStack;
1089cdf0e10cSrcweir     ActiveShellList::reverse_iterator iShell;
1090cdf0e10cSrcweir     for (iShell=maActiveViewShells.rbegin(); iShell!=maActiveViewShells.rend(); ++iShell)
1091cdf0e10cSrcweir     {
1092cdf0e10cSrcweir         // Get the list of associated sub shells.
1093cdf0e10cSrcweir         SubShellList::iterator iList (maActiveSubShells.find(iShell->mpShell));
1094cdf0e10cSrcweir         if (iList != maActiveSubShells.end())
1095cdf0e10cSrcweir         {
1096cdf0e10cSrcweir             SubShellSubList& rList (iList->second);
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir             // Iterate over all sub shells of the current view shell.
1099cdf0e10cSrcweir             SubShellSubList::iterator iSubShell;
1100cdf0e10cSrcweir             for (iSubShell=rList.begin(); iSubShell!=rList.end(); ++iSubShell)
1101cdf0e10cSrcweir             {
1102cdf0e10cSrcweir                 if (iSubShell->mpShell == NULL)
1103cdf0e10cSrcweir                 {
1104cdf0e10cSrcweir                     *iSubShell = CreateSubShell(iShell->mpShell,iSubShell->mnId,NULL,NULL);
1105cdf0e10cSrcweir                 }
1106cdf0e10cSrcweir             }
1107cdf0e10cSrcweir         }
1108cdf0e10cSrcweir    }
1109cdf0e10cSrcweir }
1110cdf0e10cSrcweir 
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir 
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir void ViewShellManager::Implementation::CreateTargetStack (ShellStack& rStack) const
1115cdf0e10cSrcweir {
1116cdf0e10cSrcweir     // Create a local stack of the shells that are to push on the shell
1117cdf0e10cSrcweir     // stack.  We can thus safly create the required shells wile still
1118cdf0e10cSrcweir     // having a valid shell stack.
1119cdf0e10cSrcweir     for (ActiveShellList::const_reverse_iterator iViewShell (maActiveViewShells.rbegin());
1120cdf0e10cSrcweir          iViewShell != maActiveViewShells.rend();
1121cdf0e10cSrcweir          ++iViewShell)
1122cdf0e10cSrcweir     {
1123cdf0e10cSrcweir         // Possibly place the form shell below the current view shell.
1124cdf0e10cSrcweir         if ( ! mbFormShellAboveParent
1125cdf0e10cSrcweir             && mpFormShell!=NULL
1126cdf0e10cSrcweir             && iViewShell->mpShell==mpFormShellParent)
1127cdf0e10cSrcweir         {
1128cdf0e10cSrcweir             rStack.push_back(mpFormShell);
1129cdf0e10cSrcweir         }
1130cdf0e10cSrcweir 
1131cdf0e10cSrcweir         // Put the view shell itself on the local stack.
1132cdf0e10cSrcweir         rStack.push_back (iViewShell->mpShell);
1133cdf0e10cSrcweir 
1134cdf0e10cSrcweir         // Possibly place the form shell above the current view shell.
1135cdf0e10cSrcweir         if (mbFormShellAboveParent
1136cdf0e10cSrcweir             && mpFormShell!=NULL
1137cdf0e10cSrcweir             && iViewShell->mpShell==mpFormShellParent)
1138cdf0e10cSrcweir         {
1139cdf0e10cSrcweir             rStack.push_back(mpFormShell);
1140cdf0e10cSrcweir         }
1141cdf0e10cSrcweir 
1142cdf0e10cSrcweir         // Add all other sub shells.
1143cdf0e10cSrcweir         SubShellList::const_iterator iList (maActiveSubShells.find(iViewShell->mpShell));
1144cdf0e10cSrcweir         if (iList != maActiveSubShells.end())
1145cdf0e10cSrcweir         {
1146cdf0e10cSrcweir             const SubShellSubList& rList (iList->second);
1147cdf0e10cSrcweir             SubShellSubList::const_reverse_iterator iSubShell;
1148cdf0e10cSrcweir             for (iSubShell=rList.rbegin(); iSubShell!=rList.rend(); ++iSubShell)
1149cdf0e10cSrcweir                 if (iSubShell->mpShell != mpFormShell)
1150cdf0e10cSrcweir                     rStack.push_back(iSubShell->mpShell);
1151cdf0e10cSrcweir         }
1152cdf0e10cSrcweir     }
1153cdf0e10cSrcweir }
1154cdf0e10cSrcweir 
1155cdf0e10cSrcweir 
1156cdf0e10cSrcweir 
1157cdf0e10cSrcweir 
1158cdf0e10cSrcweir IMPL_LINK(ViewShellManager::Implementation, WindowEventHandler, VclWindowEvent*, pEvent)
1159cdf0e10cSrcweir {
1160cdf0e10cSrcweir     if (pEvent != NULL)
1161cdf0e10cSrcweir     {
1162cdf0e10cSrcweir         ::Window* pEventWindow
1163cdf0e10cSrcweir             = static_cast<VclWindowEvent*>(pEvent)->GetWindow();
1164cdf0e10cSrcweir 
1165cdf0e10cSrcweir         switch (pEvent->GetId())
1166cdf0e10cSrcweir         {
1167cdf0e10cSrcweir             case VCLEVENT_WINDOW_GETFOCUS:
1168cdf0e10cSrcweir             {
1169cdf0e10cSrcweir                 for (ActiveShellList::iterator aI(maActiveViewShells.begin());
1170cdf0e10cSrcweir                      aI!=maActiveViewShells.end();
1171cdf0e10cSrcweir                      aI++)
1172cdf0e10cSrcweir                 {
1173cdf0e10cSrcweir                     if (pEventWindow == static_cast< ::Window*>(aI->GetWindow()))
1174cdf0e10cSrcweir                     {
1175cdf0e10cSrcweir                         MoveToTop(*aI->mpShell);
1176cdf0e10cSrcweir                         break;
1177cdf0e10cSrcweir                     }
1178cdf0e10cSrcweir                 }
1179cdf0e10cSrcweir             }
1180cdf0e10cSrcweir             break;
1181cdf0e10cSrcweir 
1182cdf0e10cSrcweir             case VCLEVENT_WINDOW_LOSEFOCUS:
1183cdf0e10cSrcweir                 break;
1184cdf0e10cSrcweir         }
1185cdf0e10cSrcweir     }
1186cdf0e10cSrcweir     return sal_True;
1187cdf0e10cSrcweir }
1188cdf0e10cSrcweir 
1189cdf0e10cSrcweir 
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir 
1192cdf0e10cSrcweir ShellDescriptor ViewShellManager::Implementation::CreateSubShell (
1193cdf0e10cSrcweir     SfxShell* pParentShell,
1194cdf0e10cSrcweir     ShellId nShellId,
1195cdf0e10cSrcweir     ::Window* pParentWindow,
1196cdf0e10cSrcweir     FrameView* pFrameView)
1197cdf0e10cSrcweir {
1198cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
1199cdf0e10cSrcweir     ShellDescriptor aResult;
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir     // Look up the factories for the parent shell.
1202cdf0e10cSrcweir     ::std::pair<FactoryList::iterator,FactoryList::iterator> aRange(
1203cdf0e10cSrcweir         maShellFactories.equal_range(pParentShell));
1204cdf0e10cSrcweir 
1205cdf0e10cSrcweir     // Try all factories to create the shell.
1206cdf0e10cSrcweir     for (FactoryList::const_iterator iFactory=aRange.first; iFactory!=aRange.second; ++iFactory)
1207cdf0e10cSrcweir     {
1208cdf0e10cSrcweir         SharedShellFactory pFactory = iFactory->second;
1209cdf0e10cSrcweir         if (pFactory != NULL)
1210cdf0e10cSrcweir             aResult.mpShell = pFactory->CreateShell(nShellId, pParentWindow, pFrameView);
1211cdf0e10cSrcweir 
1212cdf0e10cSrcweir         // Exit the loop when the shell has been successfully created.
1213cdf0e10cSrcweir         if (aResult.mpShell != NULL)
1214cdf0e10cSrcweir         {
1215cdf0e10cSrcweir             aResult.mpFactory = pFactory;
1216cdf0e10cSrcweir             aResult.mnId = nShellId;
1217cdf0e10cSrcweir             break;
1218cdf0e10cSrcweir         }
1219cdf0e10cSrcweir     }
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir     return aResult;
1222cdf0e10cSrcweir }
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir 
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir void ViewShellManager::Implementation::DestroyViewShell (
1228cdf0e10cSrcweir     const ShellDescriptor& rDescriptor)
1229cdf0e10cSrcweir {
1230cdf0e10cSrcweir     OSL_ASSERT(rDescriptor.mpShell != NULL);
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir     ::Window* pWindow = rDescriptor.GetWindow();
1233cdf0e10cSrcweir     if (pWindow != NULL)
1234cdf0e10cSrcweir     {
1235cdf0e10cSrcweir         pWindow->RemoveEventListener(
1236cdf0e10cSrcweir             LINK(this, ViewShellManager::Implementation, WindowEventHandler));
1237cdf0e10cSrcweir     }
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir     // Destroy the sub shell factories.
1240cdf0e10cSrcweir     ::std::pair<FactoryList::iterator,FactoryList::iterator> aRange(
1241cdf0e10cSrcweir         maShellFactories.equal_range(rDescriptor.mpShell));
1242cdf0e10cSrcweir     if (aRange.first != maShellFactories.end())
1243cdf0e10cSrcweir         maShellFactories.erase(aRange.first, aRange.second);
1244cdf0e10cSrcweir 
1245cdf0e10cSrcweir     // Release the shell.
1246cdf0e10cSrcweir     if (rDescriptor.mpFactory.get() != NULL)
1247cdf0e10cSrcweir         rDescriptor.mpFactory->ReleaseShell(rDescriptor.mpShell);
1248cdf0e10cSrcweir }
1249cdf0e10cSrcweir 
1250cdf0e10cSrcweir 
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir 
1253cdf0e10cSrcweir void ViewShellManager::Implementation::DestroySubShell (
1254cdf0e10cSrcweir     const SfxShell& rParentShell,
1255cdf0e10cSrcweir     const ShellDescriptor& rDescriptor)
1256cdf0e10cSrcweir {
1257cdf0e10cSrcweir     (void)rParentShell;
1258cdf0e10cSrcweir     OSL_ASSERT(rDescriptor.mpFactory.get() != NULL);
1259cdf0e10cSrcweir     rDescriptor.mpFactory->ReleaseShell(rDescriptor.mpShell);
1260cdf0e10cSrcweir }
1261cdf0e10cSrcweir 
1262cdf0e10cSrcweir 
1263cdf0e10cSrcweir 
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir void ViewShellManager::Implementation::InvalidateAllSubShells (const SfxShell* pParentShell)
1266cdf0e10cSrcweir {
1267cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir     SubShellList::iterator iList (maActiveSubShells.find(pParentShell));
1270cdf0e10cSrcweir     if (iList != maActiveSubShells.end())
1271cdf0e10cSrcweir     {
1272cdf0e10cSrcweir         SubShellSubList& rList (iList->second);
1273cdf0e10cSrcweir         SubShellSubList::iterator iShell;
1274cdf0e10cSrcweir         for (iShell=rList.begin(); iShell!=rList.end(); ++iShell)
1275cdf0e10cSrcweir             if (iShell->mpShell != NULL)
1276cdf0e10cSrcweir                 iShell->mpShell->Invalidate();
1277cdf0e10cSrcweir     }
1278cdf0e10cSrcweir }
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir 
1282cdf0e10cSrcweir 
1283cdf0e10cSrcweir void ViewShellManager::Implementation::Shutdown (void)
1284cdf0e10cSrcweir {
1285cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
1286cdf0e10cSrcweir 
1287cdf0e10cSrcweir     // Take stacked shells from stack.
1288cdf0e10cSrcweir     if ( ! maActiveViewShells.empty())
1289cdf0e10cSrcweir     {
1290cdf0e10cSrcweir         UpdateLock aLock (*this);
1291cdf0e10cSrcweir 
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir         while ( ! maActiveViewShells.empty())
1294cdf0e10cSrcweir         {
1295cdf0e10cSrcweir             SfxShell* pShell = maActiveViewShells.front().mpShell;
1296cdf0e10cSrcweir             if (pShell != NULL)
1297cdf0e10cSrcweir             {
1298cdf0e10cSrcweir                 ViewShell* pViewShell = dynamic_cast<ViewShell*>(pShell);
1299cdf0e10cSrcweir                 if (pViewShell != NULL)
1300cdf0e10cSrcweir                     DeactivateViewShell(*pViewShell);
1301cdf0e10cSrcweir                 else
1302cdf0e10cSrcweir                     DeactivateShell(*pShell);
1303cdf0e10cSrcweir             }
1304cdf0e10cSrcweir             else
1305cdf0e10cSrcweir             {
1306cdf0e10cSrcweir                 DBG_ASSERT(false,
1307cdf0e10cSrcweir                     "ViewShellManager::Implementation::Shutdown(): empty active shell descriptor");
1308cdf0e10cSrcweir                 maActiveViewShells.pop_front();
1309cdf0e10cSrcweir             }
1310cdf0e10cSrcweir         }
1311cdf0e10cSrcweir     }
1312cdf0e10cSrcweir     mrBase.RemoveSubShell (NULL);
1313cdf0e10cSrcweir 
1314cdf0e10cSrcweir     maShellFactories.clear();
1315cdf0e10cSrcweir }
1316cdf0e10cSrcweir 
1317cdf0e10cSrcweir 
1318cdf0e10cSrcweir 
1319cdf0e10cSrcweir 
1320cdf0e10cSrcweir #ifdef VERBOSE
1321cdf0e10cSrcweir void ViewShellManager::Implementation::DumpShellStack (const ShellStack& rStack)
1322cdf0e10cSrcweir {
1323cdf0e10cSrcweir     ShellStack::const_reverse_iterator iEntry;
1324cdf0e10cSrcweir     for (iEntry=rStack.rbegin(); iEntry!=rStack.rend(); ++iEntry)
1325cdf0e10cSrcweir         if (*iEntry != NULL)
1326cdf0e10cSrcweir             OSL_TRACE ("    %p : %s\r",
1327cdf0e10cSrcweir                 *iEntry,
1328cdf0e10cSrcweir                 ::rtl::OUStringToOString((*iEntry)->GetName(),RTL_TEXTENCODING_UTF8).getStr());
1329cdf0e10cSrcweir         else
1330cdf0e10cSrcweir             OSL_TRACE("     null\r");
1331cdf0e10cSrcweir }
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir 
1334cdf0e10cSrcweir 
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir void ViewShellManager::Implementation::DumpSfxShellStack (void)
1337cdf0e10cSrcweir {
1338cdf0e10cSrcweir     ShellStack aSfxShellStack;
1339cdf0e10cSrcweir     sal_uInt16 nIndex (0);
1340cdf0e10cSrcweir     while (mrBase.GetSubShell(nIndex)!=NULL)
1341cdf0e10cSrcweir         ++nIndex;
1342cdf0e10cSrcweir     aSfxShellStack.reserve(nIndex);
1343cdf0e10cSrcweir     while (nIndex-- > 0)
1344cdf0e10cSrcweir         aSfxShellStack.push_back(mrBase.GetSubShell(nIndex));
1345cdf0e10cSrcweir     DumpShellStack(aSfxShellStack);
1346cdf0e10cSrcweir }
1347cdf0e10cSrcweir #endif
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir 
1350cdf0e10cSrcweir 
1351cdf0e10cSrcweir void ViewShellManager::Implementation::Deactivate (SfxShell* pShell)
1352cdf0e10cSrcweir {
1353cdf0e10cSrcweir     OSL_ASSERT(pShell!=NULL);
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir     // We have to end a text edit for view shells that are to be taken from
1356cdf0e10cSrcweir     // the shell stack.
1357cdf0e10cSrcweir     ViewShell* pViewShell = dynamic_cast<ViewShell*>(pShell);
1358cdf0e10cSrcweir     if (pViewShell != NULL)
1359cdf0e10cSrcweir     {
1360cdf0e10cSrcweir 		sd::View* pView = pViewShell->GetView();
1361cdf0e10cSrcweir         if (pView!=NULL && pView->IsTextEdit())
1362cdf0e10cSrcweir         {
1363cdf0e10cSrcweir             pView->SdrEndTextEdit();
1364cdf0e10cSrcweir             pView->UnmarkAll();
1365cdf0e10cSrcweir             pViewShell->GetViewFrame()->GetDispatcher()->Execute(
1366cdf0e10cSrcweir                 SID_OBJECT_SELECT,
1367cdf0e10cSrcweir                 SFX_CALLMODE_ASYNCHRON);
1368cdf0e10cSrcweir         }
1369cdf0e10cSrcweir     }
1370cdf0e10cSrcweir 
1371cdf0e10cSrcweir     // Now we can deactivate the shell.
1372cdf0e10cSrcweir     pShell->Deactivate(sal_True);
1373cdf0e10cSrcweir }
1374cdf0e10cSrcweir 
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir 
1377cdf0e10cSrcweir 
1378cdf0e10cSrcweir void ViewShellManager::Implementation::SetFormShell (
1379cdf0e10cSrcweir     const ViewShell* pFormShellParent,
1380cdf0e10cSrcweir     FmFormShell* pFormShell,
1381cdf0e10cSrcweir     bool bFormShellAboveParent)
1382cdf0e10cSrcweir {
1383cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
1384cdf0e10cSrcweir 
1385cdf0e10cSrcweir     mpFormShellParent = pFormShellParent;
1386cdf0e10cSrcweir     mpFormShell = pFormShell;
1387cdf0e10cSrcweir     mbFormShellAboveParent = bFormShellAboveParent;
1388cdf0e10cSrcweir }
1389cdf0e10cSrcweir 
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir 
1392cdf0e10cSrcweir 
1393cdf0e10cSrcweir namespace {
1394cdf0e10cSrcweir 
1395cdf0e10cSrcweir ShellDescriptor::ShellDescriptor (void)
1396cdf0e10cSrcweir     : mpShell(NULL),
1397cdf0e10cSrcweir       mnId(0),
1398cdf0e10cSrcweir       mpFactory()
1399cdf0e10cSrcweir {
1400cdf0e10cSrcweir }
1401cdf0e10cSrcweir 
1402cdf0e10cSrcweir 
1403cdf0e10cSrcweir 
1404cdf0e10cSrcweir 
1405cdf0e10cSrcweir ShellDescriptor::ShellDescriptor (
1406cdf0e10cSrcweir     SfxShell* pShell,
1407cdf0e10cSrcweir     ShellId nId)
1408cdf0e10cSrcweir     : mpShell(pShell),
1409cdf0e10cSrcweir       mnId(nId),
1410cdf0e10cSrcweir       mpFactory()
1411cdf0e10cSrcweir {
1412cdf0e10cSrcweir }
1413cdf0e10cSrcweir 
1414cdf0e10cSrcweir 
1415cdf0e10cSrcweir 
1416cdf0e10cSrcweir 
1417cdf0e10cSrcweir ShellDescriptor::ShellDescriptor (const ShellDescriptor& rDescriptor)
1418cdf0e10cSrcweir         : mpShell(rDescriptor.mpShell),
1419cdf0e10cSrcweir           mnId(rDescriptor.mnId),
1420cdf0e10cSrcweir           mpFactory(rDescriptor.mpFactory)
1421cdf0e10cSrcweir {
1422cdf0e10cSrcweir }
1423cdf0e10cSrcweir 
1424cdf0e10cSrcweir 
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir 
1427cdf0e10cSrcweir ShellDescriptor& ShellDescriptor::operator= (const ShellDescriptor& rDescriptor)
1428cdf0e10cSrcweir {
1429cdf0e10cSrcweir     mpShell = rDescriptor.mpShell;
1430cdf0e10cSrcweir     mnId = rDescriptor.mnId;
1431cdf0e10cSrcweir     mpFactory = rDescriptor.mpFactory;
1432cdf0e10cSrcweir     return *this;
1433cdf0e10cSrcweir }
1434cdf0e10cSrcweir 
1435cdf0e10cSrcweir 
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir 
1438cdf0e10cSrcweir bool ShellDescriptor::IsMainViewShell (void) const
1439cdf0e10cSrcweir {
1440cdf0e10cSrcweir     ViewShell* pViewShell = dynamic_cast<ViewShell*>(mpShell);
1441cdf0e10cSrcweir     if (pViewShell != NULL)
1442cdf0e10cSrcweir         return pViewShell->IsMainViewShell();
1443cdf0e10cSrcweir     else
1444cdf0e10cSrcweir         return false;
1445cdf0e10cSrcweir }
1446cdf0e10cSrcweir 
1447cdf0e10cSrcweir 
1448cdf0e10cSrcweir 
1449cdf0e10cSrcweir 
1450cdf0e10cSrcweir ::Window* ShellDescriptor::GetWindow (void) const
1451cdf0e10cSrcweir {
1452cdf0e10cSrcweir     ViewShell* pViewShell = dynamic_cast<ViewShell*>(mpShell);
1453cdf0e10cSrcweir     if (pViewShell != NULL)
1454cdf0e10cSrcweir         return pViewShell->GetActiveWindow();
1455cdf0e10cSrcweir     else
1456cdf0e10cSrcweir         return NULL;
1457cdf0e10cSrcweir }
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir 
1460cdf0e10cSrcweir 
1461cdf0e10cSrcweir } // end of anonymous namespace
1462cdf0e10cSrcweir 
1463cdf0e10cSrcweir } // end of namespace sd
1464