xref: /aoo41x/main/slideshow/test/demoshow.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 <sal/main.h>
29 #include <rtl/ref.hxx>
30 #include <rtl/bootstrap.hxx>
31 
32 #include <cppuhelper/bootstrap.hxx>
33 #include <cppuhelper/servicefactory.hxx>
34 #include <cppuhelper/interfacecontainer.hxx>
35 #include <cppuhelper/compbase1.hxx>
36 #include <cppuhelper/compbase2.hxx>
37 
38 #include <comphelper/processfactory.hxx>
39 #include <comphelper/broadcasthelper.hxx>
40 #include <comphelper/anytostring.hxx>
41 #include <cppuhelper/exc_hlp.hxx>
42 
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/rendering/XCanvas.hpp>
46 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
47 #include <com/sun/star/presentation/XSlideShow.hpp>
48 #include <com/sun/star/presentation/XSlideShowView.hpp>
49 #include "com/sun/star/animations/TransitionType.hpp"
50 #include "com/sun/star/animations/TransitionSubType.hpp"
51 
52 #include <ucbhelper/contentbroker.hxx>
53 #include <ucbhelper/configurationkeys.hxx>
54 
55 #include <basegfx/matrix/b2dhommatrix.hxx>
56 #include <basegfx/tools/canvastools.hxx>
57 #include <basegfx/range/b2drectangle.hxx>
58 #include <basegfx/polygon/b2dpolygon.hxx>
59 #include <basegfx/polygon/b2dpolygontools.hxx>
60 
61 #include <cppcanvas/vclfactory.hxx>
62 #include <cppcanvas/basegfxfactory.hxx>
63 #include <cppcanvas/polypolygon.hxx>
64 
65 #include <canvas/canvastools.hxx>
66 
67 #include <vcl/dialog.hxx>
68 #include <vcl/timer.hxx>
69 #include <vcl/window.hxx>
70 #include <vcl/svapp.hxx>
71 
72 #include <stdio.h>
73 #include <unistd.h>
74 
75 
76 using namespace ::com::sun::star;
77 
78 namespace {
79 
80 typedef ::cppu::WeakComponentImplHelper1< presentation::XSlideShowView > ViewBase;
81 class View : public ::comphelper::OBaseMutex,
82              public ViewBase
83 {
84 public:
85     explicit View( const uno::Reference< rendering::XSpriteCanvas >& rCanvas ) :
86         ViewBase( m_aMutex ),
87         mxCanvas( rCanvas ),
88         maPaintListeners( m_aMutex ),
89         maTransformationListeners( m_aMutex ),
90         maMouseListeners( m_aMutex ),
91         maMouseMotionListeners( m_aMutex ),
92         maTransform(),
93         maSize()
94     {
95     }
96 
97     void resize( const ::Size& rNewSize )
98     {
99         maSize = rNewSize;
100         const sal_Int32 nSize( std::min( rNewSize.Width(), rNewSize.Height() ) - 10);
101         maTransform = basegfx::tools::createScaleTranslateB2DHomMatrix(
102             nSize, nSize, (rNewSize.Width() - nSize) / 2, (rNewSize.Height() - nSize) / 2);
103 
104         lang::EventObject aEvent( *this );
105         maTransformationListeners.notifyEach( &util::XModifyListener::modified,
106                                               aEvent );
107     }
108 
109     void repaint()
110     {
111         awt::PaintEvent aEvent( *this,
112                                 awt::Rectangle(),
113                                 0 );
114         maPaintListeners.notifyEach( &awt::XPaintListener::windowPaint,
115                                      aEvent );
116     }
117 
118 private:
119     virtual ~View() {}
120 
121     virtual uno::Reference< rendering::XSpriteCanvas > SAL_CALL getCanvas(  ) throw (uno::RuntimeException)
122     {
123         return mxCanvas;
124     }
125 
126     virtual void SAL_CALL clear(  ) throw (uno::RuntimeException)
127     {
128         ::basegfx::B2DPolygon aPoly( ::basegfx::tools::createPolygonFromRect(
129                                          ::basegfx::B2DRectangle(0.0,0.0,
130                                                                  maSize.Width(),
131                                                                  maSize.Height() )));
132         ::cppcanvas::SpriteCanvasSharedPtr pCanvas(
133             ::cppcanvas::VCLFactory::getInstance().createSpriteCanvas( mxCanvas ));
134         if( !pCanvas )
135             return;
136 
137         ::cppcanvas::PolyPolygonSharedPtr pPolyPoly(
138             ::cppcanvas::BaseGfxFactory::getInstance().createPolyPolygon( pCanvas,
139                                                                           aPoly ) );
140         if( !pPolyPoly )
141             return;
142 
143         if( pPolyPoly )
144         {
145             pPolyPoly->setRGBAFillColor( 0x808080FFU );
146             pPolyPoly->draw();
147         }
148     }
149 
150     virtual geometry::AffineMatrix2D SAL_CALL getTransformation(  ) throw (uno::RuntimeException)
151     {
152         geometry::AffineMatrix2D aRes;
153         return basegfx::unotools::affineMatrixFromHomMatrix( aRes,
154                                                              maTransform );
155     }
156 
157     virtual void SAL_CALL addTransformationChangedListener( const uno::Reference< util::XModifyListener >& xListener ) throw (uno::RuntimeException)
158     {
159         maTransformationListeners.addInterface( xListener );
160     }
161 
162     virtual void SAL_CALL removeTransformationChangedListener( const uno::Reference< util::XModifyListener >& xListener ) throw (uno::RuntimeException)
163     {
164         maTransformationListeners.removeInterface( xListener );
165     }
166 
167     virtual void SAL_CALL addPaintListener( const uno::Reference< awt::XPaintListener >& xListener ) throw (uno::RuntimeException)
168     {
169         maPaintListeners.addInterface( xListener );
170     }
171 
172     virtual void SAL_CALL removePaintListener( const uno::Reference< awt::XPaintListener >& xListener ) throw (uno::RuntimeException)
173     {
174         maPaintListeners.removeInterface( xListener );
175     }
176 
177     virtual void SAL_CALL addMouseListener( const uno::Reference< awt::XMouseListener >& xListener ) throw (uno::RuntimeException)
178     {
179         maMouseListeners.addInterface( xListener );
180     }
181 
182     virtual void SAL_CALL removeMouseListener( const uno::Reference< awt::XMouseListener >& xListener ) throw (uno::RuntimeException)
183     {
184         maMouseListeners.removeInterface( xListener );
185     }
186 
187     virtual void SAL_CALL addMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener ) throw (uno::RuntimeException)
188     {
189         maMouseMotionListeners.addInterface( xListener );
190     }
191 
192     virtual void SAL_CALL removeMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener ) throw (uno::RuntimeException)
193     {
194         maMouseMotionListeners.removeInterface( xListener );
195     }
196 
197     virtual void SAL_CALL setMouseCursor( ::sal_Int16 /*nPointerShape*/ ) throw (uno::RuntimeException)
198     {
199     }
200 
201     uno::Reference< rendering::XSpriteCanvas > mxCanvas;
202     ::cppu::OInterfaceContainerHelper          maPaintListeners;
203     ::cppu::OInterfaceContainerHelper          maTransformationListeners;
204     ::cppu::OInterfaceContainerHelper          maMouseListeners;
205     ::cppu::OInterfaceContainerHelper          maMouseMotionListeners;
206     basegfx::B2DHomMatrix                      maTransform;
207     Size                                       maSize;
208 };
209 
210 typedef ::cppu::WeakComponentImplHelper2< drawing::XDrawPage,
211                                           beans::XPropertySet > SlideBase;
212 class DummySlide : public ::comphelper::OBaseMutex,
213                    public SlideBase
214 {
215 public:
216     DummySlide() : SlideBase( m_aMutex ) {}
217 
218 private:
219     // XDrawPage
220     virtual void SAL_CALL add( const uno::Reference< drawing::XShape >& /*xShape*/ ) throw (uno::RuntimeException)
221     {
222     }
223 
224     virtual void SAL_CALL remove( const uno::Reference< drawing::XShape >& /*xShape*/ ) throw (uno::RuntimeException)
225     {
226     }
227 
228     virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
229     {
230         return 0;
231     }
232 
233     virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 /*Index*/ ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
234     {
235         return uno::Any();
236     }
237 
238     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
239     {
240         return uno::Type();
241     }
242 
243     virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
244     {
245         return false;
246     }
247 
248     // XPropertySet
249     virtual uno::Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw (uno::RuntimeException)
250     {
251         return uno::Reference< beans::XPropertySetInfo >();
252     }
253 
254     virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& /*aPropertyName*/,
255                                             const uno::Any& /*aValue*/ ) throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
256     {
257     }
258 
259     virtual uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
260     {
261         typedef ::canvas::tools::ValueMap< sal_Int16 > PropMapT;
262 
263         // fixed PropertyValue map
264         static PropMapT::MapEntry lcl_propertyMap[] =
265             {
266                 {"Height",               100},
267                 {"MinimalFrameNumber",   50},
268                 {"TransitionDuration",   10},
269                 {"TransitionSubtype",    animations::TransitionSubType::FROMTOPLEFT},
270                 {"TransitionType",       animations::TransitionType::PUSHWIPE},
271                 {"Width",                100}
272             };
273 
274         static PropMapT aMap( lcl_propertyMap,
275                               sizeof(lcl_propertyMap)/sizeof(*lcl_propertyMap),
276                               true );
277 
278         sal_Int16 aRes;
279         if( !aMap.lookup( PropertyName, aRes ))
280             return uno::Any();
281 
282         return uno::makeAny(aRes);
283     }
284 
285     virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/,
286                                                      const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
287     {
288     }
289 
290     virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/,
291                                                         const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
292     {
293     }
294 
295     virtual void SAL_CALL addVetoableChangeListener( const ::rtl::OUString& /*PropertyName*/,
296                                                      const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
297     {
298     }
299 
300     virtual void SAL_CALL removeVetoableChangeListener( const ::rtl::OUString& /*PropertyName*/,
301                                                         const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
302     {
303     }
304 };
305 
306 
307 class DemoApp : public Application
308 {
309 public:
310 	virtual void Main();
311 	virtual sal_uInt16	Exception( sal_uInt16 nError );
312 };
313 
314 class ChildWindow : public Window
315 {
316 public:
317     ChildWindow( Window* pParent );
318     virtual ~ChildWindow();
319     virtual void Paint( const Rectangle& rRect );
320     virtual void Resize();
321 
322     void setShow( const uno::Reference< presentation::XSlideShow >& rShow ) { mxShow = rShow; init(); }
323 
324 private:
325     void init();
326 
327     rtl::Reference< View >                     mpView;
328     uno::Reference< presentation::XSlideShow > mxShow;
329 };
330 
331 ChildWindow::ChildWindow( Window* pParent ) :
332     Window(pParent, WB_CLIPCHILDREN | WB_BORDER| WB_3DLOOK ),
333     mpView(),
334     mxShow()
335 {
336     EnablePaint( true );
337     Show();
338 }
339 
340 ChildWindow::~ChildWindow()
341 {
342     if( mxShow.is() && mpView.is() )
343         mxShow->removeView( mpView.get() );
344 }
345 
346 void ChildWindow::init()
347 {
348 	try
349 	{
350         if( !mpView.is() )
351         {
352             uno::Reference< rendering::XCanvas > xCanvas( GetCanvas(),
353                                                           uno::UNO_QUERY_THROW );
354             uno::Reference< rendering::XSpriteCanvas > xSpriteCanvas( xCanvas,
355                                                                       uno::UNO_QUERY_THROW );
356             mpView = new View( xSpriteCanvas );
357             mpView->resize( GetSizePixel() );
358 
359             if( mxShow.is() )
360                 mxShow->addView( mpView.get() );
361         }
362 	}
363 	catch (const uno::Exception &e)
364 	{
365 		OSL_TRACE( "Exception '%s' thrown\n" ,
366                    (const sal_Char*)::rtl::OUStringToOString( e.Message,
367                                                               RTL_TEXTENCODING_UTF8 ));
368 	}
369 }
370 
371 void ChildWindow::Paint( const Rectangle& /*rRect*/ )
372 {
373 	try
374 	{
375 		if( mpView.is() )
376             mpView->repaint();
377 	}
378 	catch (const uno::Exception &e)
379 	{
380 		OSL_TRACE( "Exception '%s' thrown\n" ,
381                    (const sal_Char*)::rtl::OUStringToOString( e.Message,
382                                                               RTL_TEXTENCODING_UTF8 ));
383 	}
384 }
385 
386 void ChildWindow::Resize()
387 {
388     if( mpView.is() )
389         mpView->resize( GetSizePixel() );
390 }
391 
392 class DemoWindow : public Dialog
393 {
394 public:
395     DemoWindow();
396     virtual void Paint( const Rectangle& rRect );
397     virtual void Resize();
398 
399 private:
400     void init();
401 	DECL_LINK( updateHdl, Timer* );
402 
403     ChildWindow                                maLeftChild;
404     ChildWindow                                maRightTopChild;
405     ChildWindow                                maRightBottomChild;
406     uno::Reference< presentation::XSlideShow > mxShow;
407 	AutoTimer                                  maUpdateTimer;
408     bool                                       mbSlideDisplayed;
409 };
410 
411 DemoWindow::DemoWindow() :
412     Dialog((Window*)NULL),
413     maLeftChild( this ),
414     maRightTopChild( this ),
415     maRightBottomChild( this ),
416     mxShow(),
417     maUpdateTimer(),
418     mbSlideDisplayed( false )
419 {
420     SetText( rtl::OUString::createFromAscii( "Slideshow Demo" ) );
421     SetSizePixel( Size( 640, 480 ) );
422     EnablePaint( true );
423 
424     maLeftChild.SetPosSizePixel( Point(), Size(320,480) );
425     maRightTopChild.SetPosSizePixel( Point(320,0), Size(320,240) );
426     maRightBottomChild.SetPosSizePixel( Point(320,240), Size(320,240) );
427     Show();
428 
429 	maUpdateTimer.SetTimeoutHdl(LINK(this, DemoWindow, updateHdl));
430 	maUpdateTimer.SetTimeout( (sal_uLong)30 );
431 	maUpdateTimer.Start();
432 }
433 
434 void DemoWindow::init()
435 {
436 	try
437 	{
438         if( !mxShow.is() )
439         {
440             uno::Reference< lang::XMultiServiceFactory > xFactory(
441                 ::comphelper::getProcessServiceFactory(),
442                 uno::UNO_QUERY_THROW );
443 
444             uno::Reference< uno::XInterface > xInt( xFactory->createInstance(
445                                                         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.presentation.SlideShow")) ));
446 
447             mxShow.set( xInt,
448                         uno::UNO_QUERY_THROW );
449 
450             maLeftChild.setShow( mxShow );
451             maRightTopChild.setShow( mxShow );
452             maRightBottomChild.setShow( mxShow );
453         }
454 
455         if( mxShow.is() && !mbSlideDisplayed )
456         {
457             uno::Reference< drawing::XDrawPage > xSlide( new DummySlide );
458             mxShow->displaySlide( xSlide,
459                                   uno::Reference< animations::XAnimationNode >(),
460                                   uno::Sequence< beans::PropertyValue >() );
461             mxShow->setProperty( beans::PropertyValue(
462                                      rtl::OUString::createFromAscii("RehearseTimings"),
463                                      0,
464                                      uno::makeAny( sal_True ),
465                                      beans::PropertyState_DIRECT_VALUE ));
466             mbSlideDisplayed = true;
467         }
468 	}
469 	catch (const uno::Exception &e)
470 	{
471 		OSL_TRACE( "Exception '%s' thrown\n" ,
472                    (const sal_Char*)::rtl::OUStringToOString( e.Message,
473                                                               RTL_TEXTENCODING_UTF8 ));
474 	}
475 }
476 
477 IMPL_LINK( DemoWindow, updateHdl, Timer*, EMPTYARG )
478 {
479     init();
480 
481     double nTimeout;
482     if( mxShow.is() )
483         mxShow->update(nTimeout);
484 
485     return 0;
486 }
487 
488 void DemoWindow::Paint( const Rectangle& /*rRect*/ )
489 {
490     init();
491 }
492 
493 void DemoWindow::Resize()
494 {
495     // TODO
496 }
497 
498 sal_uInt16 DemoApp::Exception( sal_uInt16 nError )
499 {
500 	switch( nError & EXC_MAJORTYPE )
501 	{
502 		case EXC_RSCNOTLOADED:
503 			Abort( String::CreateFromAscii( "Error: could not load language resources.\nPlease check your installation.\n" ) );
504 			break;
505 	}
506 	return 0;
507 }
508 
509 void DemoApp::Main()
510 {
511 	bool bHelp = false;
512 
513 	for( sal_uInt16 i = 0; i < GetCommandLineParamCount(); i++ )
514 	{
515 		::rtl::OUString aParam = GetCommandLineParam( i );
516 
517 		if( aParam.equalsAscii( "--help" ) ||
518 			aParam.equalsAscii( "-h" ) )
519 				bHelp = true;
520 	}
521 
522 	if( bHelp )
523 	{
524         printf( "demoshow - life Slideshow testbed\n" );
525 		return;
526 	}
527 
528     // bootstrap UNO
529     uno::Reference< lang::XMultiServiceFactory > xFactory;
530     try
531     {
532         uno::Reference< uno::XComponentContext > xCtx = ::cppu::defaultBootstrap_InitialComponentContext();
533         xFactory = uno::Reference< lang::XMultiServiceFactory >(  xCtx->getServiceManager(),
534                                                                   uno::UNO_QUERY );
535         if( xFactory.is() )
536             ::comphelper::setProcessServiceFactory( xFactory );
537     }
538     catch( uno::RuntimeException& )
539     {
540         throw;
541     }
542     catch( uno::Exception& )
543     {
544         OSL_ENSURE( false,
545                     rtl::OUStringToOString(
546                         comphelper::anyToString( cppu::getCaughtException() ),
547                         RTL_TEXTENCODING_UTF8 ).getStr() );
548     }
549 
550     if( !xFactory.is() )
551     {
552         OSL_TRACE( "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
553         exit( 1 );
554     }
555 
556     // Create UCB.
557     uno::Sequence< uno::Any > aArgs( 2 );
558 	aArgs[ 0 ] <<= rtl::OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
559 	aArgs[ 1 ] <<= rtl::OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
560     ::ucbhelper::ContentBroker::initialize( xFactory, aArgs );
561 
562 	DemoWindow pWindow;
563 	pWindow.Execute();
564 
565     // clean up UCB
566 	::ucbhelper::ContentBroker::deinitialize();
567 }
568 }
569 
570 DemoApp aApp;
571