xref: /trunk/main/avmedia/source/xine/window.cxx (revision cdf0e10c)
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 <com/sun/star/awt/SystemPointer.hdl>
29 
30 #include "window.hxx"
31 #include "player.hxx"
32 
33 using namespace ::com::sun::star;
34 
35 namespace avmedia { namespace xine {
36 
37 // -----------
38 // - statics -
39 // -----------
40 
41 static ::osl::Mutex& ImplGetOwnStaticMutex()
42 {
43     static ::osl::Mutex* pMutex = NULL;
44 
45     if( pMutex == NULL )
46     {
47         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
48 
49         if( pMutex == NULL )
50         {
51             static ::osl::Mutex aMutex;
52             pMutex = &aMutex;
53         }
54     }
55 
56     return *pMutex;
57 }
58 
59 // -----------
60 // - WndProc -
61 // -----------
62 
63 /*
64 LRESULT CALLBACK MediaPlayerWndProc( HWND hWnd,UINT nMsg, WPARAM nPar1, LPARAM nPar2 )
65 {
66     Window* pWindow = (Window*) ::GetWindowLong( hWnd, 0 );
67     bool    bProcessed = true;
68 
69     if( pWindow )
70     {
71         switch( nMsg )
72         {
73             case( WM_SETCURSOR ):
74                 pWindow->updatePointer();
75             break;
76 
77             case( WM_GRAPHNOTIFY ):
78                 pWindow->processGraphEvent();
79             break;
80 
81             case( WM_MOUSEMOVE ):
82             case( WM_LBUTTONDOWN ):
83             case( WM_MBUTTONDOWN ):
84             case( WM_RBUTTONDOWN ):
85             case( WM_LBUTTONUP ):
86             case( WM_MBUTTONUP ):
87             case( WM_RBUTTONUP ):
88             {
89                 awt::MouseEvent aUNOEvt;
90                 POINT           aWinPoint;
91 
92                 if( !::GetCursorPos( &aWinPoint ) || !::ScreenToClient( hWnd, &aWinPoint ) )
93                 {
94                     aWinPoint.x = GET_X_LPARAM( nPar2 );
95                     aWinPoint.y = GET_Y_LPARAM( nPar2 );
96                 }
97                 aUNOEvt.Modifiers = 0;
98                 aUNOEvt.Buttons = 0;
99                 aUNOEvt.X = aWinPoint.x;
100                 aUNOEvt.Y = aWinPoint.y;
101                 aUNOEvt.PopupTrigger = false;
102 
103                 // Modifiers
104                 if( nPar1 & MK_SHIFT )
105                     aUNOEvt.Modifiers |= awt::KeyModifier::SHIFT;
106 
107                 if( nPar1 & MK_CONTROL )
108                     aUNOEvt.Modifiers |= awt::KeyModifier::MOD1;
109 
110                 // Buttons
111                 if( WM_LBUTTONDOWN == nMsg || WM_LBUTTONUP == nMsg )
112                     aUNOEvt.Buttons |= awt::MouseButton::LEFT;
113 
114                 if( WM_MBUTTONDOWN == nMsg || WM_MBUTTONUP == nMsg )
115                     aUNOEvt.Buttons |= awt::MouseButton::MIDDLE;
116 
117                 if( WM_RBUTTONDOWN == nMsg || WM_RBUTTONUP == nMsg )
118                     aUNOEvt.Buttons |= awt::MouseButton::RIGHT;
119 
120                 // event type
121                 if( WM_LBUTTONDOWN == nMsg ||
122                     WM_MBUTTONDOWN == nMsg ||
123                     WM_RBUTTONDOWN == nMsg )
124                 {
125                     aUNOEvt.ClickCount = 1;
126                     pWindow->fireMousePressedEvent( aUNOEvt );
127                 }
128                 else if( WM_LBUTTONUP == nMsg ||
129                          WM_MBUTTONUP == nMsg ||
130                          WM_RBUTTONUP == nMsg )
131                 {
132                     aUNOEvt.ClickCount = 1;
133                     pWindow->fireMouseReleasedEvent( aUNOEvt );
134                 }
135                 else if( WM_MOUSEMOVE == nMsg )
136                 {
137                     aUNOEvt.ClickCount = 0;
138                     pWindow->fireMouseMovedEvent( aUNOEvt );
139                     pWindow->updatePointer();
140                 }
141             }
142             break;
143 
144             case( WM_SETFOCUS ):
145             {
146                 const awt::FocusEvent aUNOEvt;
147                 pWindow->fireSetFocusEvent( aUNOEvt );
148             }
149             break;
150 
151             default:
152                 bProcessed = false;
153             break;
154         }
155     }
156     else
157         bProcessed = false;
158 
159     return( bProcessed ? 0 : DefWindowProc( hWnd, nMsg, nPar1, nPar2 ) );
160 }
161 */
162 
163 // ---------------
164 // - Window -
165 // ---------------
166 
167 Window::Window( Player& rPlayer ) :
168     mrPlayer( rPlayer ),
169     maListeners( maMutex ),
170     meZoomLevel( media::ZoomLevel_NOT_AVAILABLE ),
171     mnPointerType( awt::SystemPointer::ARROW )
172 {
173     ::osl::MutexGuard aGuard( ImplGetOwnStaticMutex() );
174 }
175 
176 // ------------------------------------------------------------------------------
177 
178 Window::~Window()
179 {
180 }
181 
182 // ------------------------------------------------------------------------------
183 
184 void Window::implLayoutVideoWindow()
185 {
186     if( media::ZoomLevel_NOT_AVAILABLE != meZoomLevel )
187     {
188         awt::Size           aPrefSize( mrPlayer.getPreferredPlayerWindowSize() );
189         awt::Rectangle      aRect = getPosSize();
190         int                 nW = aRect.Width, nH = aRect.Height;
191         int                 nVideoW = nW, nVideoH = nH;
192         int                 nX = 0, nY = 0, nWidth = 0, nHeight = 0;
193         bool                bDone = false, bZoom = false;
194 
195         if( media::ZoomLevel_ORIGINAL == meZoomLevel )
196         {
197             bZoom = true;
198         }
199         else if( media::ZoomLevel_ZOOM_1_TO_4 == meZoomLevel )
200         {
201             aPrefSize.Width >>= 2;
202             aPrefSize.Height >>= 2;
203             bZoom = true;
204         }
205         else if( media::ZoomLevel_ZOOM_1_TO_2 == meZoomLevel )
206         {
207             aPrefSize.Width >>= 1;
208             aPrefSize.Height >>= 1;
209             bZoom = true;
210         }
211         else if( media::ZoomLevel_ZOOM_2_TO_1 == meZoomLevel )
212         {
213             aPrefSize.Width <<= 1;
214             aPrefSize.Height <<= 1;
215             bZoom = true;
216         }
217         else if( media::ZoomLevel_ZOOM_4_TO_1 == meZoomLevel )
218         {
219             aPrefSize.Width <<= 2;
220             aPrefSize.Height <<= 2;
221             bZoom = true;
222         }
223         else if( media::ZoomLevel_FIT_TO_WINDOW == meZoomLevel )
224         {
225             nWidth = nVideoW;
226             nHeight = nVideoH;
227             bDone = true;
228         }
229 
230         if( bZoom )
231         {
232             if( ( aPrefSize.Width <= nVideoW ) && ( aPrefSize.Height <= nVideoH ) )
233             {
234                 nX = ( nVideoW - aPrefSize.Width ) >> 1;
235                 nY = ( nVideoH - aPrefSize.Height ) >> 1;
236                 nWidth = aPrefSize.Width;
237                 nHeight = aPrefSize.Height;
238                 bDone = true;
239             }
240         }
241 
242         if( !bDone )
243         {
244             if( aPrefSize.Width > 0 && aPrefSize.Height > 0 && nVideoW > 0 && nVideoH > 0 )
245             {
246                 double fPrefWH = (double) aPrefSize.Width / aPrefSize.Height;
247 
248                 if( fPrefWH < ( (double) nVideoW / nVideoH ) )
249                     nVideoW = (int)( nVideoH * fPrefWH );
250                 else
251                     nVideoH = (int)( nVideoW / fPrefWH );
252 
253                 nX = ( nW - nVideoW ) >> 1;
254                 nY = ( nH - nVideoH ) >> 1;
255                 nWidth = nVideoW;
256                 nHeight = nVideoH;
257             }
258             else
259                 nX = nY = nWidth = nHeight = 0;
260         }
261 
262         /*
263 		IVideoWindow* pVideoWindow = const_cast< IVideoWindow* >( mrPlayer.getVideoWindow() );
264 
265         if( pVideoWindow )
266             pVideoWindow->SetWindowPosition( nX, nY, nWidth, nHeight );
267 		*/
268     }
269 }
270 
271 // ------------------------------------------------------------------------------
272 
273 bool Window::create( const uno::Sequence< uno::Any >& /*rArguments*/ )
274 {
275 	bool bRet = false;
276 
277 	return bRet;
278 }
279 
280 // ------------------------------------------------------------------------------
281 
282 void SAL_CALL Window::update(  )
283     throw (uno::RuntimeException)
284 {
285 }
286 
287 // ------------------------------------------------------------------------------
288 
289 sal_Bool SAL_CALL Window::setZoomLevel( media::ZoomLevel eZoomLevel )
290     throw (uno::RuntimeException)
291 {
292 	bool bRet = false;
293 
294 	if( media::ZoomLevel_NOT_AVAILABLE != meZoomLevel &&
295 		media::ZoomLevel_NOT_AVAILABLE != eZoomLevel )
296 	{
297 		if( eZoomLevel != meZoomLevel )
298 		{
299 			meZoomLevel = eZoomLevel;
300 			implLayoutVideoWindow();
301 		}
302 
303 		bRet = true;
304 	}
305 
306 	return bRet;
307 }
308 
309 // ------------------------------------------------------------------------------
310 
311 media::ZoomLevel SAL_CALL Window::getZoomLevel(  )
312     throw (uno::RuntimeException)
313 {
314     return meZoomLevel;
315 }
316 
317 // ------------------------------------------------------------------------------
318 
319 void SAL_CALL Window::setPointerType( sal_Int32 nPointerType )
320 	throw (uno::RuntimeException)
321 {
322     mnPointerType = nPointerType;
323 }
324 
325 // ------------------------------------------------------------------------------
326 
327 void SAL_CALL Window::setPosSize( sal_Int32 /*X*/, sal_Int32 /*Y*/, sal_Int32 /*Width*/, sal_Int32 /*Height*/, sal_Int16 /*Flags*/ )
328     throw (uno::RuntimeException)
329 {
330     implLayoutVideoWindow();
331 }
332 
333 // ------------------------------------------------------------------------------
334 
335 awt::Rectangle SAL_CALL Window::getPosSize()
336     throw (uno::RuntimeException)
337 {
338     awt::Rectangle aRet;
339 
340     return aRet;
341 }
342 
343 // ------------------------------------------------------------------------------
344 
345 void SAL_CALL Window::setVisible( sal_Bool /* bVisible */ )
346     throw (uno::RuntimeException)
347 {
348 }
349 
350 // ------------------------------------------------------------------------------
351 
352 void SAL_CALL Window::setEnable( sal_Bool /* bEnable */ )
353     throw (uno::RuntimeException)
354 {
355 }
356 
357 // ------------------------------------------------------------------------------
358 
359 void SAL_CALL Window::setFocus(  )
360     throw (uno::RuntimeException)
361 {
362 }
363 
364 // ------------------------------------------------------------------------------
365 
366 void SAL_CALL Window::addWindowListener( const uno::Reference< awt::XWindowListener >& xListener )
367     throw (uno::RuntimeException)
368 {
369     maListeners.addInterface( getCppuType( &xListener ), xListener );
370 }
371 
372 // ------------------------------------------------------------------------------
373 
374 void SAL_CALL Window::removeWindowListener( const uno::Reference< awt::XWindowListener >& xListener )
375     throw (uno::RuntimeException)
376 {
377     maListeners.removeInterface( getCppuType( &xListener ), xListener );
378 }
379 
380 // ------------------------------------------------------------------------------
381 
382 void SAL_CALL Window::addFocusListener( const uno::Reference< awt::XFocusListener >& xListener )
383     throw (uno::RuntimeException)
384 {
385     maListeners.addInterface( getCppuType( &xListener ), xListener );
386 }
387 
388 // ------------------------------------------------------------------------------
389 
390 void SAL_CALL Window::removeFocusListener( const uno::Reference< awt::XFocusListener >& xListener )
391     throw (uno::RuntimeException)
392 {
393     maListeners.removeInterface( getCppuType( &xListener ), xListener );
394 }
395 
396 // ------------------------------------------------------------------------------
397 
398 void SAL_CALL Window::addKeyListener( const uno::Reference< awt::XKeyListener >& xListener )
399     throw (uno::RuntimeException)
400 {
401     maListeners.addInterface( getCppuType( &xListener ), xListener );
402 }
403 
404 // ------------------------------------------------------------------------------
405 
406 void SAL_CALL Window::removeKeyListener( const uno::Reference< awt::XKeyListener >& xListener )
407     throw (uno::RuntimeException)
408 {
409     maListeners.removeInterface( getCppuType( &xListener ), xListener );
410 }
411 
412 // ------------------------------------------------------------------------------
413 
414 void SAL_CALL Window::addMouseListener( const uno::Reference< awt::XMouseListener >& xListener )
415     throw (uno::RuntimeException)
416 {
417     maListeners.addInterface( getCppuType( &xListener ), xListener );
418 }
419 
420 // ------------------------------------------------------------------------------
421 
422 void SAL_CALL Window::removeMouseListener( const uno::Reference< awt::XMouseListener >& xListener )
423     throw (uno::RuntimeException)
424 {
425     maListeners.removeInterface( getCppuType( &xListener ), xListener );
426 }
427 
428 // ------------------------------------------------------------------------------
429 
430 void SAL_CALL Window::addMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener )
431     throw (uno::RuntimeException)
432 {
433     maListeners.addInterface( getCppuType( &xListener ), xListener );
434 }
435 
436 // ------------------------------------------------------------------------------
437 
438 void SAL_CALL Window::removeMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener )
439     throw (uno::RuntimeException)
440 {
441     maListeners.removeInterface( getCppuType( &xListener ), xListener );
442 }
443 
444 // ------------------------------------------------------------------------------
445 
446 void SAL_CALL Window::addPaintListener( const uno::Reference< awt::XPaintListener >& xListener )
447     throw (uno::RuntimeException)
448 {
449     maListeners.addInterface( getCppuType( &xListener ), xListener );
450 }
451 
452 // ------------------------------------------------------------------------------
453 
454 void SAL_CALL Window::removePaintListener( const uno::Reference< awt::XPaintListener >& xListener )
455     throw (uno::RuntimeException)
456 {
457     maListeners.removeInterface( getCppuType( &xListener ), xListener );
458 }
459 
460 // ------------------------------------------------------------------------------
461 
462 void SAL_CALL Window::dispose(  )
463     throw (uno::RuntimeException)
464 {
465 }
466 
467 // ------------------------------------------------------------------------------
468 
469 void SAL_CALL Window::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
470     throw (uno::RuntimeException)
471 {
472     maListeners.addInterface( getCppuType( &xListener ), xListener );
473 }
474 
475 // ------------------------------------------------------------------------------
476 
477 void SAL_CALL Window::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
478     throw (uno::RuntimeException)
479 {
480     maListeners.removeInterface( getCppuType( &xListener ), xListener );
481 }
482 
483 // ------------------------------------------------------------------------------
484 
485 void Window::fireMousePressedEvent( const ::com::sun::star::awt::MouseEvent& rEvt )
486 {
487     ::cppu::OInterfaceContainerHelper* pContainer = maListeners.getContainer( getCppuType( (uno::Reference< awt::XMouseListener >*) 0 ) );
488 
489     if( pContainer )
490     {
491         ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
492 
493         while( aIter.hasMoreElements() )
494             uno::Reference< awt::XMouseListener >( aIter.next(), uno::UNO_QUERY )->mousePressed( rEvt );
495     }
496 }
497 
498 // -----------------------------------------------------------------------------
499 
500 void Window::fireMouseReleasedEvent( const ::com::sun::star::awt::MouseEvent& rEvt )
501 {
502     ::cppu::OInterfaceContainerHelper* pContainer = maListeners.getContainer( getCppuType( (uno::Reference< awt::XMouseListener >*) 0 ) );
503 
504     if( pContainer )
505     {
506         ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
507 
508         while( aIter.hasMoreElements() )
509             uno::Reference< awt::XMouseListener >( aIter.next(), uno::UNO_QUERY )->mouseReleased( rEvt );
510     }
511 }
512 
513 // -----------------------------------------------------------------------------
514 
515 void Window::fireMouseMovedEvent( const ::com::sun::star::awt::MouseEvent& rEvt )
516 {
517     ::cppu::OInterfaceContainerHelper* pContainer = maListeners.getContainer( getCppuType( (uno::Reference< awt::XMouseMotionListener >*) 0 ) );
518 
519     if( pContainer )
520     {
521         ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
522 
523         while( aIter.hasMoreElements() )
524             uno::Reference< awt::XMouseMotionListener >( aIter.next(), uno::UNO_QUERY )->mouseMoved( rEvt );
525     }
526 }
527 
528 // -----------------------------------------------------------------------------
529 
530 void Window::fireSetFocusEvent( const ::com::sun::star::awt::FocusEvent& rEvt )
531 {
532     ::cppu::OInterfaceContainerHelper* pContainer = maListeners.getContainer( getCppuType( (uno::Reference< awt::XFocusListener >*) 0 ) );
533 
534     if( pContainer )
535     {
536         ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
537 
538         while( aIter.hasMoreElements() )
539             uno::Reference< awt::XFocusListener >( aIter.next(), uno::UNO_QUERY )->focusGained( rEvt );
540     }
541 }
542 
543 // ------------------------------------------------------------------------------
544 
545 ::rtl::OUString SAL_CALL Window::getImplementationName(  )
546     throw (uno::RuntimeException)
547 {
548     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_XINE_WINDOW_IMPLEMENTATIONNAME ) );
549 }
550 
551 // ------------------------------------------------------------------------------
552 
553 sal_Bool SAL_CALL Window::supportsService( const ::rtl::OUString& ServiceName )
554     throw (uno::RuntimeException)
555 {
556     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_XINE_WINDOW_SERVICENAME ) );
557 }
558 
559 // ------------------------------------------------------------------------------
560 
561 uno::Sequence< ::rtl::OUString > SAL_CALL Window::getSupportedServiceNames(  )
562     throw (uno::RuntimeException)
563 {
564     uno::Sequence< ::rtl::OUString > aRet(1);
565     aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_XINE_WINDOW_SERVICENAME ) );
566 
567     return aRet;
568 }
569 
570 } // namespace xine
571 } // namespace avmedia
572