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