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