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_sfx2.hxx"
23 
24 #include "sfx2/sidebar/SidebarPanelBase.hxx"
25 #include "sfx2/sidebar/Theme.hxx"
26 #include "sfx2/sidebar/ILayoutableWindow.hxx"
27 #include "sfx2/imagemgr.hxx"
28 #include <vcl/ctrl.hxx>
29 #include <comphelper/processfactory.hxx>
30 
31 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
32 #include <com/sun/star/ui/UIElementType.hpp>
33 
34 using namespace css;
35 using namespace cssu;
36 
37 
38 namespace sfx2 { namespace sidebar {
39 
40 Reference<ui::XUIElement> SidebarPanelBase::Create (
41     const ::rtl::OUString& rsResourceURL,
42     const cssu::Reference<css::frame::XFrame>& rxFrame,
43     Window* pWindow,
44     const ::boost::function<void(void)>& rMenuProvider,
45     const css::ui::LayoutSize& rLayoutSize)
46 {
47     Reference<ui::XUIElement> xUIElement (
48         new SidebarPanelBase(
49             rsResourceURL,
50             rxFrame,
51             pWindow,
52             rMenuProvider,
53             rLayoutSize));
54     return xUIElement;
55 }
56 
57 
58 
59 
60 SidebarPanelBase::SidebarPanelBase (
61     const ::rtl::OUString& rsResourceURL,
62     const cssu::Reference<css::frame::XFrame>& rxFrame,
63     Window* pWindow,
64     const ::boost::function<void(void)>& rMenuProvider,
65     const css::ui::LayoutSize& rLayoutSize)
66     : SidebarPanelBaseInterfaceBase(m_aMutex),
67       mxFrame(rxFrame),
68       mpControl(pWindow),
69       msResourceURL(rsResourceURL),
70       maMenuProvider(rMenuProvider),
71       maLayoutSize(rLayoutSize)
72 {
73     OSL_TRACE("SidebarPanelBase created at %x", this);
74 
75     if (mxFrame.is())
76     {
77         cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
78             css::ui::ContextChangeEventMultiplexer::get(
79                 ::comphelper::getProcessComponentContext()));
80         if (xMultiplexer.is())
81             xMultiplexer->addContextChangeEventListener(this, mxFrame->getController());
82     }
83     if (mpControl != NULL)
84         mpControl->Show();
85 }
86 
87 
88 
89 
90 SidebarPanelBase::~SidebarPanelBase (void)
91 {
92     OSL_TRACE("SidebarPanelBase destroyed at %x", this);
93 }
94 
95 
96 
97 
98 void SAL_CALL SidebarPanelBase::disposing (void)
99     throw (cssu::RuntimeException)
100 {
101     OSL_TRACE("SidebarPanelBase disposing at %x", this);
102 
103     if (mpControl != NULL)
104     {
105         delete mpControl;
106         mpControl = NULL;
107     }
108 
109     if (mxFrame.is())
110     {
111         cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
112             css::ui::ContextChangeEventMultiplexer::get(
113                 ::comphelper::getProcessComponentContext()));
114         if (xMultiplexer.is())
115             xMultiplexer->removeAllContextChangeEventListeners(this);
116         mxFrame = NULL;
117     }
118 }
119 
120 
121 
122 
123 void SidebarPanelBase::SetControl (::Window* pControl)
124 {
125     OSL_TRACE("setting control of SidebarPanelBase at %x to %x", this, pControl);
126     mpControl = pControl;
127 }
128 
129 
130 
131 
132 ::Window* SidebarPanelBase::GetControl (void) const
133 {
134     return mpControl;
135 }
136 
137 
138 
139 
140 // XContextChangeEventListener
141 void SAL_CALL SidebarPanelBase::notifyContextChangeEvent (
142     const ui::ContextChangeEventObject& rEvent)
143     throw (cssu::RuntimeException)
144 {
145     OSL_TRACE("SidebarPanelBase notified at %x with control at %x", this, mpControl);
146 
147     ContextChangeReceiverInterface* pContextChangeReceiver
148         = dynamic_cast<ContextChangeReceiverInterface*>(mpControl);
149     if (pContextChangeReceiver != NULL)
150     {
151         const EnumContext aContext(
152             EnumContext::GetApplicationEnum(rEvent.ApplicationName),
153             EnumContext::GetContextEnum(rEvent.ContextName));
154         pContextChangeReceiver->HandleContextChange(aContext);
155     }
156 }
157 
158 
159 
160 
161 void SAL_CALL SidebarPanelBase::disposing (
162     const css::lang::EventObject& rEvent)
163     throw (cssu::RuntimeException)
164 {
165     (void)rEvent;
166 
167     OSL_TRACE("SidebarPanelBase disposing(e) at %x", this);
168 
169     mxFrame = NULL;
170     mpControl = NULL;
171 }
172 
173 
174 
175 
176 cssu::Reference<css::frame::XFrame> SAL_CALL SidebarPanelBase::getFrame (void)
177     throw(cssu::RuntimeException)
178 {
179     return mxFrame;
180 }
181 
182 
183 
184 
185 ::rtl::OUString SAL_CALL SidebarPanelBase::getResourceURL (void)
186     throw(cssu::RuntimeException)
187 {
188     return msResourceURL;
189 }
190 
191 
192 
193 
194 sal_Int16 SAL_CALL SidebarPanelBase::getType (void)
195     throw(cssu::RuntimeException)
196 {
197     return ui::UIElementType::TOOLPANEL;
198 }
199 
200 
201 
202 
203 Reference<XInterface> SAL_CALL SidebarPanelBase::getRealInterface (void)
204     throw(cssu::RuntimeException)
205 {
206     return Reference<XInterface>(static_cast<XWeak*>(this));
207 }
208 
209 
210 
211 
212 Reference<accessibility::XAccessible> SAL_CALL SidebarPanelBase::createAccessible (
213     const Reference<accessibility::XAccessible>& rxParentAccessible)
214     throw(cssu::RuntimeException)
215 {
216     (void)rxParentAccessible;
217 
218     // Not yet implemented.
219     return NULL;
220 }
221 
222 
223 
224 
225 Reference<awt::XWindow> SAL_CALL SidebarPanelBase::getWindow (void)
226     throw(cssu::RuntimeException)
227 {
228     if (mpControl != NULL)
229         return Reference<awt::XWindow>(
230             mpControl->GetComponentInterface(),
231             UNO_QUERY);
232     else
233         return NULL;
234 }
235 
236 
237 
238 
239 ui::LayoutSize SAL_CALL SidebarPanelBase::getHeightForWidth (const sal_Int32 nWidth)
240     throw(cssu::RuntimeException)
241 {
242     if (maLayoutSize.Minimum >= 0)
243         return maLayoutSize;
244     else
245     {
246         ILayoutableWindow* pLayoutableWindow = dynamic_cast<ILayoutableWindow*>(mpControl);
247         if (pLayoutableWindow != NULL)
248             return pLayoutableWindow->GetHeightForWidth(nWidth);
249         else if (mpControl != NULL)
250         {
251             const sal_Int32 nHeight (mpControl->GetSizePixel().Height());
252             return ui::LayoutSize(nHeight,nHeight,nHeight);
253         }
254     }
255 
256     return ui::LayoutSize(0,0,0);
257 }
258 
259 
260 
261 
262 void SAL_CALL SidebarPanelBase::showMenu (void)
263     throw(cssu::RuntimeException)
264 {
265     if (maMenuProvider)
266         maMenuProvider();
267 }
268 
269 
270 
271 
272 sal_Bool SAL_CALL SidebarPanelBase::isContextSupported (
273     const ::rtl::OUString& rsApplicationName,
274     const ::rtl::OUString& rsContextName)
275     throw(cssu::RuntimeException)
276 {
277     (void)rsApplicationName;
278     (void)rsContextName;
279 
280     return sal_True;
281 }
282 
283 
284 } } // end of namespace sfx2::sidebar
285