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