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 #include <vcl/imagerepository.hxx>
39 
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using ::rtl::OUString;
43 
44 namespace sd { namespace presenter {
45 
46 //===== Service ===============================================================
47 
48 Reference<XInterface> SAL_CALL PresenterHelperService_createInstance (
49     const Reference<XComponentContext>& rxContext)
50 {
51     return Reference<XInterface>(static_cast<XWeak*>(new PresenterHelper(rxContext)));
52 }
53 
54 
55 
56 
57 ::rtl::OUString PresenterHelperService_getImplementationName (void)
58     throw(RuntimeException)
59 {
60     return OUString::createFromAscii("com.sun.star.comp.Draw.PresenterHelper");
61 }
62 
63 
64 
65 
66 Sequence<rtl::OUString> SAL_CALL PresenterHelperService_getSupportedServiceNames (void)
67     throw (RuntimeException)
68 {
69 	static const ::rtl::OUString sServiceName(
70         ::rtl::OUString::createFromAscii("com.sun.star.drawing.PresenterHelper"));
71 	return Sequence<rtl::OUString>(&sServiceName, 1);
72 }
73 
74 
75 
76 
77 //===== PresenterHelper =======================================================
78 
79 PresenterHelper::PresenterHelper (
80     const Reference<XComponentContext>& rxContext)
81     : PresenterHelperInterfaceBase(m_aMutex),
82       mxComponentContext(rxContext)
83 {
84 }
85 
86 
87 
88 PresenterHelper::~PresenterHelper (void)
89 {
90 }
91 
92 
93 
94 
95 //----- XInitialize -----------------------------------------------------------
96 
97 void SAL_CALL PresenterHelper::initialize (const Sequence<Any>& rArguments)
98     throw(Exception,RuntimeException)
99 {
100     (void)rArguments;
101 }
102 
103 
104 
105 
106 //----- XPaneHelper ----------------------------------------------------
107 
108 Reference<awt::XWindow> SAL_CALL PresenterHelper::createWindow (
109     const Reference<awt::XWindow>& rxParentWindow,
110     sal_Bool bCreateSystemChildWindow,
111     sal_Bool bInitiallyVisible,
112     sal_Bool bEnableChildTransparentMode,
113     sal_Bool bEnableParentClip)
114     throw (css::uno::RuntimeException)
115 {
116     ::Window* pParentWindow = VCLUnoHelper::GetWindow(rxParentWindow);
117 
118     // Create a new window.
119     ::Window* pWindow = NULL;
120     if (bCreateSystemChildWindow)
121     {
122         pWindow = new WorkWindow(pParentWindow, WB_SYSTEMCHILDWINDOW);
123     }
124     else
125     {
126         pWindow = new ::Window(pParentWindow);
127     }
128     Reference<awt::XWindow> xWindow (pWindow->GetComponentInterface(), UNO_QUERY);
129 
130     if (bEnableChildTransparentMode)
131     {
132         // Make the frame window transparent and make the parent able to
133         // draw behind it.
134         if (pParentWindow != NULL)
135             pParentWindow->EnableChildTransparentMode(sal_True);
136     }
137 
138     if (pWindow != NULL)
139     {
140         pWindow->Show(bInitiallyVisible);
141 
142         pWindow->SetMapMode(MAP_PIXEL);
143         pWindow->SetBackground();
144         if ( ! bEnableParentClip)
145         {
146             pWindow->SetParentClipMode(PARENTCLIPMODE_NOCLIP);
147             pWindow->SetPaintTransparent(sal_True);
148         }
149         else
150         {
151             pWindow->SetParentClipMode(PARENTCLIPMODE_CLIP);
152             pWindow->SetPaintTransparent(sal_False);
153         }
154 
155     }
156 
157     return xWindow;
158 }
159 
160 
161 
162 
163 Reference<rendering::XCanvas> SAL_CALL PresenterHelper::createSharedCanvas (
164     const Reference<rendering::XSpriteCanvas>& rxUpdateCanvas,
165     const Reference<awt::XWindow>& rxUpdateWindow,
166     const Reference<rendering::XCanvas>& rxSharedCanvas,
167     const Reference<awt::XWindow>& rxSharedWindow,
168     const Reference<awt::XWindow>& rxWindow)
169     throw (css::uno::RuntimeException)
170 {
171     if ( ! rxSharedCanvas.is()
172         || ! rxSharedWindow.is()
173         || ! rxWindow.is())
174     {
175         throw RuntimeException(
176             OUString::createFromAscii("illegal argument"),
177             Reference<XInterface>(static_cast<XWeak*>(this)));
178     }
179 
180     if (rxWindow == rxSharedWindow)
181         return rxSharedCanvas;
182     else
183         return new PresenterCanvas(
184             rxUpdateCanvas,
185             rxUpdateWindow,
186             rxSharedCanvas,
187             rxSharedWindow,
188             rxWindow);
189 }
190 
191 
192 
193 
194 Reference<rendering::XCanvas> SAL_CALL PresenterHelper::createCanvas (
195     const Reference<awt::XWindow>& rxWindow,
196     sal_Int16 nRequestedCanvasFeatures,
197     const OUString& rsOptionalCanvasServiceName)
198     throw (css::uno::RuntimeException)
199 {
200     (void)nRequestedCanvasFeatures;
201 
202     // No shared window is given or an explicit canvas service name is
203     // specified.  Create a new canvas.
204     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
205     if (pWindow != NULL)
206     {
207         Sequence<Any> aArg (5);
208 
209         // common: first any is VCL pointer to window (for VCL canvas)
210         aArg[0] = makeAny(reinterpret_cast<sal_Int64>(pWindow));
211         aArg[1] = Any();
212         aArg[2] = makeAny(::com::sun::star::awt::Rectangle());
213         aArg[3] = makeAny(sal_False);
214         aArg[4] = makeAny(rxWindow);
215 
216         Reference<lang::XMultiServiceFactory> xFactory (
217             mxComponentContext->getServiceManager(), UNO_QUERY_THROW);
218         return Reference<rendering::XCanvas>(
219             xFactory->createInstanceWithArguments(
220                 rsOptionalCanvasServiceName.getLength()>0
221                     ? rsOptionalCanvasServiceName
222                     : OUString::createFromAscii("com.sun.star.rendering.VCLCanvas"),
223                 aArg),
224             UNO_QUERY);
225     }
226     else
227         throw RuntimeException();
228 }
229 
230 
231 
232 
233 void SAL_CALL PresenterHelper::toTop (
234     const Reference<awt::XWindow>& rxWindow)
235     throw (css::uno::RuntimeException)
236 {
237     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
238     if (pWindow != NULL)
239     {
240         pWindow->ToTop();
241         pWindow->SetZOrder(NULL, WINDOW_ZORDER_LAST);
242     }
243 }
244 
245 
246 
247 
248 Reference<rendering::XBitmap> SAL_CALL PresenterHelper::loadBitmap (
249     const OUString& rsURL,
250     const Reference<rendering::XCanvas>& rxCanvas)
251     throw (RuntimeException)
252 {
253     if ( ! rxCanvas.is())
254         return NULL;
255 
256 	::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
257 
258     if (mpGraphicFilter.get() == NULL)
259         mpGraphicFilter.reset(new GraphicFilter(sal_False));
260 
261     const cppcanvas::CanvasSharedPtr pCanvas (
262         cppcanvas::VCLFactory::getInstance().createCanvas(
263             Reference<css::rendering::XBitmapCanvas>(rxCanvas,UNO_QUERY)));
264 
265     if (pCanvas.get()!=NULL && rsURL.getLength()>0 && mpGraphicFilter.get()!=NULL)
266     {
267         sal_Int32 nIndex = 0;
268         if( rsURL.getToken( 0, '/', nIndex ).equalsAsciiL(
269             RTL_CONSTASCII_STRINGPARAM( "private:graphicrepository" ) ) )
270         {
271             OUString sPathName( rsURL.copy( nIndex ) );
272             BitmapEx aBitmap;
273             if ( ::vcl::ImageRepository::loadImage( sPathName, aBitmap, false )
274                 && !aBitmap.IsEmpty() )
275                 return cppcanvas::VCLFactory::getInstance().createBitmap(
276                     pCanvas,
277                     aBitmap)->getUNOBitmap();
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