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