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 "framework/ViewShellWrapper.hxx"
27 #include "framework/Pane.hxx"
28 #include "taskpane/ToolPanelViewShell.hxx"
29 #include "ViewShell.hxx"
30 #include "Window.hxx"
31 
32 #include <com/sun/star/drawing/framework/XPane.hpp>
33 #include <com/sun/star/lang/DisposedException.hpp>
34 
35 #include <rtl/uuid.h>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <comphelper/sequence.hxx>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vos/mutex.hxx>
41 #include <tools/diagnose_ex.h>
42 
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::drawing::framework;
47 
48 using ::com::sun::star::awt::XWindow;
49 using ::com::sun::star::rendering::XCanvas;
50 using ::com::sun::star::lang::DisposedException;
51 
52 using ::rtl::OUString;
53 using ::sd::toolpanel::ToolPanelViewShell;
54 
55 namespace sd { namespace framework {
56 
57 ViewShellWrapper::ViewShellWrapper (
58     ::boost::shared_ptr<ViewShell> pViewShell,
59     const Reference<XResourceId>& rxViewId,
60     const Reference<awt::XWindow>& rxWindow)
61     : ViewShellWrapperInterfaceBase(MutexOwner::maMutex),
62       mpViewShell(pViewShell),
63       mxViewId(rxViewId),
64       mxWindow(rxWindow)
65 {
66     if (rxWindow.is())
67     {
68         rxWindow->addWindowListener(this);
69         if (pViewShell != NULL)
70         {
71             pViewShell->Resize();
72         }
73     }
74 }
75 
76 
77 
78 
79 ViewShellWrapper::~ViewShellWrapper (void)
80 {
81 }
82 
83 
84 
85 
86 void SAL_CALL ViewShellWrapper::disposing (void)
87 {
88     ::osl::MutexGuard aGuard( maMutex );
89 
90     OSL_TRACE("disposing ViewShellWrapper %x", this);
91     Reference<awt::XWindow> xWindow (mxWindow);
92     if (xWindow.is())
93     {
94         OSL_TRACE("removing ViewShellWrapper %x from window listener at %x", this, mxWindow.get());
95         xWindow->removeWindowListener(this);
96     }
97 
98     mpViewShell.reset();
99 }
100 
101 
102 
103 
104 ::boost::shared_ptr<ViewShell> ViewShellWrapper::GetViewShell (void)
105 {
106     return mpViewShell;
107 }
108 
109 
110 
111 
112 //----- XResource -------------------------------------------------------------
113 
114 Reference<XResourceId> SAL_CALL ViewShellWrapper::getResourceId (void)
115     throw (RuntimeException)
116 {
117     return mxViewId;
118 }
119 
120 
121 
122 
123 sal_Bool SAL_CALL ViewShellWrapper::isAnchorOnly (void)
124     throw (RuntimeException)
125 {
126     return false;
127 }
128 
129 
130 
131 
132 //----- XRelocatableResource --------------------------------------------------
133 
134 sal_Bool SAL_CALL ViewShellWrapper::relocateToAnchor (
135     const Reference<XResource>& xResource)
136     throw (RuntimeException)
137 {
138     sal_Bool bResult (false);
139 
140     Reference<XPane> xPane (xResource, UNO_QUERY);
141     if (xPane.is())
142     {
143         // Detach from the window of the old pane.
144         Reference<awt::XWindow> xWindow (mxWindow);
145         if (xWindow.is())
146             xWindow->removeWindowListener(this);
147         mxWindow = NULL;
148 
149         if (mpViewShell.get() != NULL)
150         {
151             ::Window* pWindow = VCLUnoHelper::GetWindow(xPane->getWindow());
152             if (pWindow != NULL && mpViewShell->RelocateToParentWindow(pWindow))
153             {
154                 bResult = sal_True;
155 
156                 // Attach to the window of the new pane.
157                 xWindow = Reference<awt::XWindow>(xPane->getWindow(), UNO_QUERY);
158                 if (xWindow.is())
159                 {
160                     xWindow->addWindowListener(this);
161                     mpViewShell->Resize();
162                 }
163             }
164         }
165     }
166 
167     return bResult;
168 }
169 
170 
171 
172 
173 //----- XUnoTunnel ------------------------------------------------------------
174 
175 const Sequence<sal_Int8>& ViewShellWrapper::getUnoTunnelId (void)
176 {
177 	static Sequence<sal_Int8>* pSequence = NULL;
178 	if (pSequence == NULL)
179 	{
180         const ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
181 		if (pSequence == NULL)
182 		{
183 			static ::com::sun::star::uno::Sequence<sal_Int8> aSequence (16);
184 			rtl_createUuid((sal_uInt8*)aSequence.getArray(), 0, sal_True);
185 			pSequence = &aSequence;
186 		}
187 	}
188 	return *pSequence;
189 }
190 
191 
192 
193 
194 sal_Int64 SAL_CALL ViewShellWrapper::getSomething (const Sequence<sal_Int8>& rId)
195     throw (RuntimeException)
196 {
197     sal_Int64 nResult = 0;
198 
199     if (rId.getLength() == 16
200         && rtl_compareMemory(getUnoTunnelId().getConstArray(), rId.getConstArray(), 16) == 0)
201 	{
202 		nResult = reinterpret_cast<sal_Int64>(this);
203 	}
204 
205     return nResult;
206 }
207 
208 
209 
210 
211 //===== awt::XWindowListener ==================================================
212 
213 void SAL_CALL ViewShellWrapper::windowResized (const awt::WindowEvent& rEvent)
214     throw (RuntimeException)
215 {
216     (void)rEvent;
217     ViewShell* pViewShell (mpViewShell.get());
218     if (pViewShell != NULL)
219         pViewShell->Resize();
220 }
221 
222 
223 
224 
225 void SAL_CALL ViewShellWrapper::windowMoved (const awt::WindowEvent& rEvent)
226     throw (RuntimeException)
227 {
228     (void)rEvent;
229 }
230 
231 
232 
233 
234 void SAL_CALL ViewShellWrapper::windowShown (const lang::EventObject& rEvent)
235     throw (RuntimeException)
236 {
237     (void)rEvent;
238     ViewShell* pViewShell (mpViewShell.get());
239     if (pViewShell != NULL)
240         pViewShell->Resize();
241 }
242 
243 
244 
245 
246 void SAL_CALL ViewShellWrapper::windowHidden (const lang::EventObject& rEvent)
247     throw (RuntimeException)
248 {
249     (void)rEvent;
250 }
251 
252 
253 
254 
255 //===== XEventListener ========================================================
256 
257 void SAL_CALL ViewShellWrapper::disposing (const lang::EventObject& rEvent)
258     throw (RuntimeException)
259 {
260     if (rEvent.Source == mxWindow)
261         mxWindow = NULL;
262 }
263 
264 
265 } } // end of namespace sd::framework
266