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 "PresenterHelper.hxx"
31 #include "CanvasUpdateRequester.hxx"
32 #include "PresenterCanvas.hxx"
33 #include <cppcanvas/vclfactory.hxx>
34 #include <com/sun/star/awt/WindowAttribute.hpp>
35 #include <com/sun/star/awt/WindowClass.hpp>
36 #include <com/sun/star/awt/WindowDescriptor.hpp>
37 #include <osl/file.hxx>
38 #include <toolkit/helper/vclunohelper.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/window.hxx>
41 #include <vcl/wrkwin.hxx>
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 using ::rtl::OUString;
46 
47 namespace sd { namespace presenter {
48 
49 //===== Service ===============================================================
50 
51 Reference<XInterface> SAL_CALL PresenterHelperService_createInstance (
52     const Reference<XComponentContext>& rxContext)
53 {
54     return Reference<XInterface>(static_cast<XWeak*>(new PresenterHelper(rxContext)));
55 }
56 
57 
58 
59 
60 ::rtl::OUString PresenterHelperService_getImplementationName (void)
61     throw(RuntimeException)
62 {
63     return OUString::createFromAscii("com.sun.star.comp.Draw.PresenterHelper");
64 }
65 
66 
67 
68 
69 Sequence<rtl::OUString> SAL_CALL PresenterHelperService_getSupportedServiceNames (void)
70     throw (RuntimeException)
71 {
72 	static const ::rtl::OUString sServiceName(
73         ::rtl::OUString::createFromAscii("com.sun.star.drawing.PresenterHelper"));
74 	return Sequence<rtl::OUString>(&sServiceName, 1);
75 }
76 
77 
78 
79 
80 //===== PresenterHelper =======================================================
81 
82 PresenterHelper::PresenterHelper (
83     const Reference<XComponentContext>& rxContext)
84     : PresenterHelperInterfaceBase(m_aMutex),
85       mxComponentContext(rxContext)
86 {
87 }
88 
89 
90 
91 PresenterHelper::~PresenterHelper (void)
92 {
93 }
94 
95 
96 
97 
98 //----- XInitialize -----------------------------------------------------------
99 
100 void SAL_CALL PresenterHelper::initialize (const Sequence<Any>& rArguments)
101     throw(Exception,RuntimeException)
102 {
103     (void)rArguments;
104 }
105 
106 
107 
108 
109 //----- XPaneHelper ----------------------------------------------------
110 
111 Reference<awt::XWindow> SAL_CALL PresenterHelper::createWindow (
112     const Reference<awt::XWindow>& rxParentWindow,
113     sal_Bool bCreateSystemChildWindow,
114     sal_Bool bInitiallyVisible,
115     sal_Bool bEnableChildTransparentMode,
116     sal_Bool bEnableParentClip)
117     throw (css::uno::RuntimeException)
118 {
119     ::Window* pParentWindow = VCLUnoHelper::GetWindow(rxParentWindow);
120 
121     // Create a new window.
122     ::Window* pWindow = NULL;
123     if (bCreateSystemChildWindow)
124     {
125         pWindow = new WorkWindow(pParentWindow, WB_SYSTEMCHILDWINDOW);
126     }
127     else
128     {
129         pWindow = new ::Window(pParentWindow);
130     }
131     Reference<awt::XWindow> xWindow (pWindow->GetComponentInterface(), UNO_QUERY);
132 
133     if (bEnableChildTransparentMode)
134     {
135         // Make the frame window transparent and make the parent able to
136         // draw behind it.
137         if (pParentWindow != NULL)
138             pParentWindow->EnableChildTransparentMode(sal_True);
139     }
140 
141     if (pWindow != NULL)
142     {
143         pWindow->Show(bInitiallyVisible);
144 
145         pWindow->SetMapMode(MAP_PIXEL);
146         pWindow->SetBackground();
147         if ( ! bEnableParentClip)
148         {
149             pWindow->SetParentClipMode(PARENTCLIPMODE_NOCLIP);
150             pWindow->SetPaintTransparent(sal_True);
151         }
152         else
153         {
154             pWindow->SetParentClipMode(PARENTCLIPMODE_CLIP);
155             pWindow->SetPaintTransparent(sal_False);
156         }
157 
158     }
159 
160     return xWindow;
161 }
162 
163 
164 
165 
166 Reference<rendering::XCanvas> SAL_CALL PresenterHelper::createSharedCanvas (
167     const Reference<rendering::XSpriteCanvas>& rxUpdateCanvas,
168     const Reference<awt::XWindow>& rxUpdateWindow,
169     const Reference<rendering::XCanvas>& rxSharedCanvas,
170     const Reference<awt::XWindow>& rxSharedWindow,
171     const Reference<awt::XWindow>& rxWindow)
172     throw (css::uno::RuntimeException)
173 {
174     if ( ! rxSharedCanvas.is()
175         || ! rxSharedWindow.is()
176         || ! rxWindow.is())
177     {
178         throw RuntimeException(
179             OUString::createFromAscii("illegal argument"),
180             Reference<XInterface>(static_cast<XWeak*>(this)));
181     }
182 
183     if (rxWindow == rxSharedWindow)
184         return rxSharedCanvas;
185     else
186         return new PresenterCanvas(
187             rxUpdateCanvas,
188             rxUpdateWindow,
189             rxSharedCanvas,
190             rxSharedWindow,
191             rxWindow);
192 }
193 
194 
195 
196 
197 Reference<rendering::XCanvas> SAL_CALL PresenterHelper::createCanvas (
198     const Reference<awt::XWindow>& rxWindow,
199     sal_Int16 nRequestedCanvasFeatures,
200     const OUString& rsOptionalCanvasServiceName)
201     throw (css::uno::RuntimeException)
202 {
203     (void)nRequestedCanvasFeatures;
204 
205     // No shared window is given or an explicit canvas service name is
206     // specified.  Create a new canvas.
207     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
208     if (pWindow != NULL)
209     {
210         Sequence<Any> aArg (5);
211 
212         // common: first any is VCL pointer to window (for VCL canvas)
213         aArg[0] = makeAny(reinterpret_cast<sal_Int64>(pWindow));
214         aArg[1] = Any();
215         aArg[2] = makeAny(::com::sun::star::awt::Rectangle());
216         aArg[3] = makeAny(sal_False);
217         aArg[4] = makeAny(rxWindow);
218 
219         Reference<lang::XMultiServiceFactory> xFactory (
220             mxComponentContext->getServiceManager(), UNO_QUERY_THROW);
221         return Reference<rendering::XCanvas>(
222             xFactory->createInstanceWithArguments(
223                 rsOptionalCanvasServiceName.getLength()>0
224                     ? rsOptionalCanvasServiceName
225                     : OUString::createFromAscii("com.sun.star.rendering.VCLCanvas"),
226                 aArg),
227             UNO_QUERY);
228     }
229     else
230         throw RuntimeException();
231 }
232 
233 
234 
235 
236 void SAL_CALL PresenterHelper::toTop (
237     const Reference<awt::XWindow>& rxWindow)
238     throw (css::uno::RuntimeException)
239 {
240     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
241     if (pWindow != NULL)
242     {
243         pWindow->ToTop();
244         pWindow->SetZOrder(NULL, WINDOW_ZORDER_LAST);
245     }
246 }
247 
248 
249 
250 
251 Reference<rendering::XBitmap> SAL_CALL PresenterHelper::loadBitmap (
252     const OUString& rsURL,
253     const Reference<rendering::XCanvas>& rxCanvas)
254     throw (RuntimeException)
255 {
256     if ( ! rxCanvas.is())
257         return NULL;
258 
259 	::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
260 
261     if (mpGraphicFilter.get() == NULL)
262         mpGraphicFilter.reset(new GraphicFilter(sal_False));
263 
264     const cppcanvas::CanvasSharedPtr pCanvas (
265         cppcanvas::VCLFactory::getInstance().createCanvas(
266             Reference<css::rendering::XBitmapCanvas>(rxCanvas,UNO_QUERY)));
267 
268     if (pCanvas.get()!=NULL && rsURL.getLength()>0 && mpGraphicFilter.get()!=NULL)
269     {
270         Graphic aGraphic;
271         OUString sFileName;
272         if (osl::FileBase::getSystemPathFromFileURL(rsURL, sFileName)
273             == osl::FileBase::E_None)
274         {
275             if (mpGraphicFilter->ImportGraphic(aGraphic, rsURL) == GRFILTER_OK)
276             {
277                 BitmapEx aBitmapEx (aGraphic.GetBitmapEx());
278                 return cppcanvas::VCLFactory::getInstance().createBitmap(
279                     pCanvas,
280                     aBitmapEx)->getUNOBitmap();
281             }
282         }
283     }
284 
285     return NULL;
286 }
287 
288 
289 
290 
291 
292 void SAL_CALL PresenterHelper::captureMouse (
293     const Reference<awt::XWindow>& rxWindow)
294     throw (RuntimeException)
295 {
296 	::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
297 
298     // Capture the mouse (if not already done.)
299     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
300     if (pWindow != NULL && ! pWindow->IsMouseCaptured())
301     {
302         pWindow->CaptureMouse();
303     }
304 }
305 
306 
307 
308 
309 void SAL_CALL PresenterHelper::releaseMouse (const Reference<awt::XWindow>& rxWindow)
310     throw (RuntimeException)
311 {
312 	::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
313 
314     // Release the mouse (if not already done.)
315     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
316     if (pWindow != NULL && pWindow->IsMouseCaptured())
317     {
318         pWindow->ReleaseMouse();
319     }
320 }
321 
322 
323 
324 
325 awt::Rectangle PresenterHelper::getWindowExtentsRelative (
326     const Reference<awt::XWindow>& rxChildWindow,
327     const Reference<awt::XWindow>& rxParentWindow)
328     throw (RuntimeException)
329 {
330     ::Window* pChildWindow = VCLUnoHelper::GetWindow(rxChildWindow);
331     ::Window* pParentWindow = VCLUnoHelper::GetWindow(rxParentWindow);
332     if (pChildWindow!=NULL && pParentWindow!=NULL)
333     {
334         Rectangle aBox (pChildWindow->GetWindowExtentsRelative(pParentWindow));
335         return awt::Rectangle(aBox.Left(),aBox.Top(),aBox.GetWidth(),aBox.GetHeight());
336     }
337     else
338         return awt::Rectangle();
339 }
340 
341 
342 
343 } } // end of namespace ::sd::presenter
344