1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sd.hxx"
23 
24 #include "SidebarShellManager.hxx"
25 
26 #include "ViewShellManager.hxx"
27 #include <tools/diagnose_ex.h>
28 #include <vcl/window.hxx>
29 
30 #include <algorithm>
31 
32 namespace sd { namespace sidebar {
33 
SidebarShellManager(const::boost::shared_ptr<ViewShellManager> & rpViewShellManager,const ViewShell & rViewShell)34 SidebarShellManager::SidebarShellManager (
35     const ::boost::shared_ptr<ViewShellManager>& rpViewShellManager,
36     const ViewShell& rViewShell)
37     : mpViewShellManager(rpViewShellManager),
38       mrViewShell(rViewShell),
39       maSubShells()
40 {
41 }
42 
43 
44 
45 
~SidebarShellManager(void)46 SidebarShellManager::~SidebarShellManager (void)
47 {
48     while ( ! maSubShells.empty())
49         RemoveSubShell(maSubShells.begin()->second.mpShell);
50 }
51 
52 
53 
54 
CreateShell(ShellId nId,::Window *,FrameView *)55 SfxShell* SidebarShellManager::CreateShell( ShellId nId, ::Window* , FrameView* )
56 {
57     SubShells::const_iterator iShell (maSubShells.find(nId));
58     if (iShell != maSubShells.end())
59         return iShell->second.mpShell;
60     else
61         return NULL;
62 }
63 
64 
65 
66 
ReleaseShell(SfxShell *)67 void SidebarShellManager::ReleaseShell (SfxShell* )
68 {
69     // Nothing to do.
70 }
71 
AddSubShell(ShellId nId,SfxShell * pShell,::Window * pWindow)72 void SidebarShellManager::AddSubShell (
73     ShellId nId,
74     SfxShell* pShell,
75     ::Window* pWindow)
76 {
77     if (pShell != NULL)
78     {
79         maSubShells[nId] = ShellDescriptor(pShell,pWindow);
80         if (pWindow != NULL)
81         {
82             pWindow->AddEventListener(LINK(this,SidebarShellManager,WindowCallback));
83             if (pWindow->IsReallyVisible())
84                 mpViewShellManager->ActivateSubShell(mrViewShell, nId);
85         }
86         else
87             mpViewShellManager->ActivateSubShell(mrViewShell, nId);
88     }
89 }
90 
91 
92 
93 
RemoveSubShell(const ShellId i_nShellId)94 void SidebarShellManager::RemoveSubShell (const ShellId i_nShellId)
95 {
96     SubShells::iterator pos = maSubShells.find( i_nShellId );
97     ENSURE_OR_RETURN_VOID( pos != maSubShells.end(), "no shell for this ID" );
98     if ( pos->second.mpWindow != NULL )
99     {
100         pos->second.mpWindow->RemoveEventListener( LINK( this, SidebarShellManager, WindowCallback ) );
101     }
102     mpViewShellManager->DeactivateSubShell( mrViewShell, pos->first );
103     maSubShells.erase( pos );
104 }
105 
106 
107 
108 
RemoveSubShell(const SfxShell * pShell)109 void SidebarShellManager::RemoveSubShell (const SfxShell* pShell)
110 {
111     if (pShell != NULL)
112     {
113         SubShells::iterator iShell;
114         for (iShell=maSubShells.begin(); iShell!=maSubShells.end(); ++iShell)
115             if (iShell->second.mpShell == pShell)
116             {
117                 if (iShell->second.mpWindow != NULL)
118                     iShell->second.mpWindow->RemoveEventListener(
119                         LINK(this,SidebarShellManager,WindowCallback));
120                 mpViewShellManager->DeactivateSubShell(mrViewShell,iShell->first);
121                 maSubShells.erase(iShell);
122                 break;
123             }
124     }
125 }
126 
127 
128 
129 
MoveToTop(SfxShell * pShell)130 void SidebarShellManager::MoveToTop (SfxShell* pShell)
131 {
132     SubShells::const_iterator iShell;
133     for (iShell=maSubShells.begin(); iShell!=maSubShells.end(); ++iShell)
134         if (iShell->second.mpShell == pShell)
135         {
136             ViewShellManager::UpdateLock aLocker (mpViewShellManager);
137             mpViewShellManager->MoveSubShellToTop(mrViewShell,iShell->first);
138             mpViewShellManager->MoveToTop(mrViewShell);
139             break;
140         }
141 }
142 
143 
144 
145 
IMPL_LINK(SidebarShellManager,WindowCallback,VclWindowEvent *,pEvent)146 IMPL_LINK(SidebarShellManager, WindowCallback, VclWindowEvent*, pEvent)
147 {
148     if (pEvent != NULL)
149     {
150         SubShells::const_iterator iShell;
151         ::Window* pWindow = pEvent->GetWindow();
152         for (iShell=maSubShells.begin(); iShell!=maSubShells.end(); ++iShell)
153             if (iShell->second.mpWindow == pWindow)
154                 break;
155         if (iShell != maSubShells.end())
156             switch (pEvent->GetId())
157             {
158                 case VCLEVENT_WINDOW_SHOW:
159                     mpViewShellManager->ActivateSubShell(mrViewShell,iShell->first);
160                     break;
161 
162                 case VCLEVENT_WINDOW_HIDE:
163                     // Do not activate the sub shell.  This leads to
164                     // problems with shapes currently being in text edit
165                     // mode: Deactivating the shell leads to leaving the
166                     // text editing mode.
167                     // mpViewShellManager->DeactivateSubShell(mrViewShell,iShell->first);
168                     break;
169             }
170     }
171 
172     return 0;
173 }
174 
175 
176 } } // end of namespace ::sd::sidebar
177