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