1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_sd.hxx"
29 
30 #include "ToolBarModule.hxx"
31 #include "ViewShellBase.hxx"
32 #include "DrawController.hxx"
33 #include "framework/FrameworkHelper.hxx"
34 #include "framework/ConfigurationController.hxx"
35 
36 
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::drawing::framework;
40 
41 using ::rtl::OUString;
42 using ::sd::framework::FrameworkHelper;
43 
44 namespace {
45     static const sal_Int32 gnConfigurationUpdateStartEvent(0);
46     static const sal_Int32 gnConfigurationUpdateEndEvent(1);
47     static const sal_Int32 gnResourceActivationRequestEvent(2);
48     static const sal_Int32 gnResourceDeactivationRequestEvent(3);
49 }
50 
51 namespace sd { namespace framework {
52 
53 //===== ToolBarModule =========================================================
54 
55 ToolBarModule::ToolBarModule (
56     const Reference<frame::XController>& rxController)
57     : ToolBarModuleInterfaceBase(m_aMutex),
58       mxConfigurationController(),
59       mpBase(NULL),
60       mpToolBarManagerLock(),
61       mbMainViewSwitchUpdatePending(false)
62 {
63     // Tunnel through the controller to obtain a ViewShellBase.
64     Reference<lang::XUnoTunnel> xTunnel (rxController, UNO_QUERY);
65     if (xTunnel.is())
66     {
67         ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
68             xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
69         if (pController != NULL)
70             mpBase = pController->GetViewShellBase();
71     }
72 
73     Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
74     if (xControllerManager.is())
75     {
76         mxConfigurationController = xControllerManager->getConfigurationController();
77         if (mxConfigurationController.is())
78         {
79             mxConfigurationController->addConfigurationChangeListener(
80                 this,
81                 FrameworkHelper::msConfigurationUpdateStartEvent,
82                 makeAny(gnConfigurationUpdateStartEvent));
83             mxConfigurationController->addConfigurationChangeListener(
84                 this,
85                 FrameworkHelper::msConfigurationUpdateEndEvent,
86                 makeAny(gnConfigurationUpdateEndEvent));
87             mxConfigurationController->addConfigurationChangeListener(
88                 this,
89                 FrameworkHelper::msResourceActivationRequestEvent,
90                 makeAny(gnResourceActivationRequestEvent));
91             mxConfigurationController->addConfigurationChangeListener(
92                 this,
93                 FrameworkHelper::msResourceDeactivationRequestEvent,
94                 makeAny(gnResourceDeactivationRequestEvent));
95         }
96     }
97 }
98 
99 
100 
101 
102 ToolBarModule::~ToolBarModule (void)
103 {
104 }
105 
106 
107 
108 
109 void SAL_CALL ToolBarModule::disposing (void)
110 {
111     if (mxConfigurationController.is())
112         mxConfigurationController->removeConfigurationChangeListener(this);
113 
114     mxConfigurationController = NULL;
115 }
116 
117 
118 
119 
120 void SAL_CALL ToolBarModule::notifyConfigurationChange (
121     const ConfigurationChangeEvent& rEvent)
122     throw (RuntimeException)
123 {
124     if (mxConfigurationController.is())
125     {
126         sal_Int32 nEventType = 0;
127         rEvent.UserData >>= nEventType;
128         switch (nEventType)
129         {
130             case gnConfigurationUpdateStartEvent:
131                 HandleUpdateStart();
132                 break;
133 
134             case gnConfigurationUpdateEndEvent:
135                 HandleUpdateEnd();
136                 break;
137 
138             case gnResourceActivationRequestEvent:
139             case gnResourceDeactivationRequestEvent:
140                 // Remember the request for the activation or deactivation
141                 // of the center pane view.  When that happens then on end
142                 // of the next configuration update the set of visible tool
143                 // bars will be updated.
144                 if ( ! mbMainViewSwitchUpdatePending)
145                     if (rEvent.ResourceId->getResourceURL().match(
146                         FrameworkHelper::msViewURLPrefix)
147                         && rEvent.ResourceId->isBoundToURL(
148                             FrameworkHelper::msCenterPaneURL, AnchorBindingMode_DIRECT))
149                     {
150                         mbMainViewSwitchUpdatePending = true;
151                     }
152                 break;
153         }
154     }
155 }
156 
157 
158 
159 
160 //-----------------------------------------------------------------------------
161 
162 
163 void ToolBarModule::HandleUpdateStart (void)
164 {
165     // Lock the ToolBarManager and tell it to lock the ViewShellManager as
166     // well.  This way the ToolBarManager can optimize the releasing of
167     // locks and arranging of updates of both tool bars and the view shell
168     // stack.
169     if (mpBase != NULL)
170     {
171         ::boost::shared_ptr<ToolBarManager> pToolBarManager (mpBase->GetToolBarManager());
172         mpToolBarManagerLock.reset(new ToolBarManager::UpdateLock(pToolBarManager));
173         pToolBarManager->LockViewShellManager();
174     }
175 }
176 
177 
178 
179 
180 void ToolBarModule::HandleUpdateEnd (void)
181 {
182     if (mbMainViewSwitchUpdatePending)
183     {
184         mbMainViewSwitchUpdatePending = false;
185         // Update the set of visible tool bars and deactivate those that are
186         // no longer visible.  This is done before the old view shell is
187         // destroyed in order to avoid unnecessary updates of those tool
188         // bars.
189         ::boost::shared_ptr<ToolBarManager> pToolBarManager (mpBase->GetToolBarManager());
190         ::boost::shared_ptr<FrameworkHelper> pFrameworkHelper (
191             FrameworkHelper::Instance(*mpBase));
192         ViewShell* pViewShell
193             = pFrameworkHelper->GetViewShell(FrameworkHelper::msCenterPaneURL).get();
194         if (pViewShell != NULL)
195         {
196             pToolBarManager->MainViewShellChanged(*pViewShell);
197             pToolBarManager->SelectionHasChanged(
198                 *pViewShell,
199                 *pViewShell->GetView());
200             pToolBarManager->PreUpdate();
201         }
202         else
203         {
204             pToolBarManager->MainViewShellChanged(ViewShell::ST_NONE);
205             pToolBarManager->PreUpdate();
206         }
207     }
208 
209     // Releasing the update lock of the ToolBarManager  will let the
210     // ToolBarManager with the help of the ViewShellManager take care of
211     // updating tool bars and view shell with the minimal amount of
212     // shell stack modifications and tool bar updates.
213     mpToolBarManagerLock.reset();
214 }
215 
216 
217 
218 
219 void SAL_CALL ToolBarModule::disposing (const lang::EventObject& rEvent)
220     throw (RuntimeException)
221 {
222     if (mxConfigurationController.is()
223         && rEvent.Source == mxConfigurationController)
224     {
225         // Without the configuration controller this class can do nothing.
226         mxConfigurationController = NULL;
227         dispose();
228     }
229 }
230 
231 
232 
233 
234 } } // end of namespace sd::framework
235