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