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 
23 
24 #include "precompiled_sd.hxx"
25 
26 #include "ToolBarModule.hxx"
27 #include "ViewShellBase.hxx"
28 #include "DrawController.hxx"
29 #include "framework/FrameworkHelper.hxx"
30 #include "framework/ConfigurationController.hxx"
31 
32 
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::drawing::framework;
36 
37 using ::rtl::OUString;
38 using ::sd::framework::FrameworkHelper;
39 
40 namespace {
41     static const sal_Int32 gnConfigurationUpdateStartEvent(0);
42     static const sal_Int32 gnConfigurationUpdateEndEvent(1);
43     static const sal_Int32 gnResourceActivationRequestEvent(2);
44     static const sal_Int32 gnResourceDeactivationRequestEvent(3);
45 }
46 
47 namespace sd { namespace framework {
48 
49 //===== ToolBarModule =========================================================
50 
ToolBarModule(const Reference<frame::XController> & rxController)51 ToolBarModule::ToolBarModule (
52     const Reference<frame::XController>& rxController)
53     : ToolBarModuleInterfaceBase(m_aMutex),
54       mxConfigurationController(),
55       mpBase(NULL),
56       mpToolBarManagerLock(),
57       mbMainViewSwitchUpdatePending(false)
58 {
59     // Tunnel through the controller to obtain a ViewShellBase.
60     Reference<lang::XUnoTunnel> xTunnel (rxController, UNO_QUERY);
61     if (xTunnel.is())
62     {
63         ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
64             xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
65         if (pController != NULL)
66             mpBase = pController->GetViewShellBase();
67     }
68 
69     Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
70     if (xControllerManager.is())
71     {
72         mxConfigurationController = xControllerManager->getConfigurationController();
73         if (mxConfigurationController.is())
74         {
75             mxConfigurationController->addConfigurationChangeListener(
76                 this,
77                 FrameworkHelper::msConfigurationUpdateStartEvent,
78                 makeAny(gnConfigurationUpdateStartEvent));
79             mxConfigurationController->addConfigurationChangeListener(
80                 this,
81                 FrameworkHelper::msConfigurationUpdateEndEvent,
82                 makeAny(gnConfigurationUpdateEndEvent));
83             mxConfigurationController->addConfigurationChangeListener(
84                 this,
85                 FrameworkHelper::msResourceActivationRequestEvent,
86                 makeAny(gnResourceActivationRequestEvent));
87             mxConfigurationController->addConfigurationChangeListener(
88                 this,
89                 FrameworkHelper::msResourceDeactivationRequestEvent,
90                 makeAny(gnResourceDeactivationRequestEvent));
91         }
92     }
93 }
94 
95 
96 
97 
~ToolBarModule(void)98 ToolBarModule::~ToolBarModule (void)
99 {
100 }
101 
102 
103 
104 
disposing(void)105 void SAL_CALL ToolBarModule::disposing (void)
106 {
107     if (mxConfigurationController.is())
108         mxConfigurationController->removeConfigurationChangeListener(this);
109 
110     mxConfigurationController = NULL;
111 }
112 
113 
114 
115 
notifyConfigurationChange(const ConfigurationChangeEvent & rEvent)116 void SAL_CALL ToolBarModule::notifyConfigurationChange (
117     const ConfigurationChangeEvent& rEvent)
118     throw (RuntimeException)
119 {
120     if (mxConfigurationController.is())
121     {
122         sal_Int32 nEventType = 0;
123         rEvent.UserData >>= nEventType;
124         switch (nEventType)
125         {
126             case gnConfigurationUpdateStartEvent:
127                 HandleUpdateStart();
128                 break;
129 
130             case gnConfigurationUpdateEndEvent:
131                 HandleUpdateEnd();
132                 break;
133 
134             case gnResourceActivationRequestEvent:
135             case gnResourceDeactivationRequestEvent:
136                 // Remember the request for the activation or deactivation
137                 // of the center pane view.  When that happens then on end
138                 // of the next configuration update the set of visible tool
139                 // bars will be updated.
140                 if ( ! mbMainViewSwitchUpdatePending)
141                     if (rEvent.ResourceId->getResourceURL().match(
142                         FrameworkHelper::msViewURLPrefix)
143                         && rEvent.ResourceId->isBoundToURL(
144                             FrameworkHelper::msCenterPaneURL, AnchorBindingMode_DIRECT))
145                     {
146                         mbMainViewSwitchUpdatePending = true;
147                     }
148                 break;
149         }
150     }
151 }
152 
153 
154 
155 
156 //-----------------------------------------------------------------------------
157 
158 
HandleUpdateStart(void)159 void ToolBarModule::HandleUpdateStart (void)
160 {
161     // Lock the ToolBarManager and tell it to lock the ViewShellManager as
162     // well.  This way the ToolBarManager can optimize the releasing of
163     // locks and arranging of updates of both tool bars and the view shell
164     // stack.
165     if (mpBase != NULL)
166     {
167         ::boost::shared_ptr<ToolBarManager> pToolBarManager (mpBase->GetToolBarManager());
168         mpToolBarManagerLock.reset(new ToolBarManager::UpdateLock(pToolBarManager));
169         pToolBarManager->LockViewShellManager();
170     }
171 }
172 
173 
174 
175 
HandleUpdateEnd(void)176 void ToolBarModule::HandleUpdateEnd (void)
177 {
178     if (mbMainViewSwitchUpdatePending)
179     {
180         mbMainViewSwitchUpdatePending = false;
181         // Update the set of visible tool bars and deactivate those that are
182         // no longer visible.  This is done before the old view shell is
183         // destroyed in order to avoid unnecessary updates of those tool
184         // bars.
185         ::boost::shared_ptr<ToolBarManager> pToolBarManager (mpBase->GetToolBarManager());
186         ::boost::shared_ptr<FrameworkHelper> pFrameworkHelper (
187             FrameworkHelper::Instance(*mpBase));
188         ViewShell* pViewShell
189             = pFrameworkHelper->GetViewShell(FrameworkHelper::msCenterPaneURL).get();
190         if (pViewShell != NULL)
191         {
192             pToolBarManager->MainViewShellChanged(*pViewShell);
193             pToolBarManager->SelectionHasChanged(
194                 *pViewShell,
195                 *pViewShell->GetView());
196             pToolBarManager->PreUpdate();
197         }
198         else
199         {
200             pToolBarManager->MainViewShellChanged(ViewShell::ST_NONE);
201             pToolBarManager->PreUpdate();
202         }
203     }
204 
205     // Releasing the update lock of the ToolBarManager  will let the
206     // ToolBarManager with the help of the ViewShellManager take care of
207     // updating tool bars and view shell with the minimal amount of
208     // shell stack modifications and tool bar updates.
209     mpToolBarManagerLock.reset();
210 }
211 
212 
213 
214 
disposing(const lang::EventObject & rEvent)215 void SAL_CALL ToolBarModule::disposing (const lang::EventObject& rEvent)
216     throw (RuntimeException)
217 {
218     if (mxConfigurationController.is()
219         && rEvent.Source == mxConfigurationController)
220     {
221         // Without the configuration controller this class can do nothing.
222         mxConfigurationController = NULL;
223         dispose();
224     }
225 }
226 
227 
228 
229 
230 } } // end of namespace sd::framework
231