merge the formfield patch from ooo-build
[ooovba.git] / slideshow / test / demoshow.cxx
blob418f08ffb0f4551e72427365f038203dd07cf65b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: demoshow.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/main.h>
32 #include <rtl/ref.hxx>
33 #include <rtl/bootstrap.hxx>
35 #include <cppuhelper/bootstrap.hxx>
36 #include <cppuhelper/servicefactory.hxx>
37 #include <cppuhelper/interfacecontainer.hxx>
38 #include <cppuhelper/compbase1.hxx>
39 #include <cppuhelper/compbase2.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/broadcasthelper.hxx>
43 #include <comphelper/anytostring.hxx>
44 #include <cppuhelper/exc_hlp.hxx>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <com/sun/star/beans/XPropertySet.hpp>
48 #include <com/sun/star/rendering/XCanvas.hpp>
49 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
50 #include <com/sun/star/presentation/XSlideShow.hpp>
51 #include <com/sun/star/presentation/XSlideShowView.hpp>
52 #include "com/sun/star/animations/TransitionType.hpp"
53 #include "com/sun/star/animations/TransitionSubType.hpp"
55 #include <ucbhelper/contentbroker.hxx>
56 #include <ucbhelper/configurationkeys.hxx>
58 #include <basegfx/matrix/b2dhommatrix.hxx>
59 #include <basegfx/tools/canvastools.hxx>
60 #include <basegfx/range/b2drectangle.hxx>
61 #include <basegfx/polygon/b2dpolygon.hxx>
62 #include <basegfx/polygon/b2dpolygontools.hxx>
64 #include <cppcanvas/vclfactory.hxx>
65 #include <cppcanvas/basegfxfactory.hxx>
66 #include <cppcanvas/polypolygon.hxx>
68 #include <canvas/canvastools.hxx>
70 #include <vcl/dialog.hxx>
71 #include <vcl/timer.hxx>
72 #include <vcl/window.hxx>
73 #include <vcl/svapp.hxx>
75 #include <stdio.h>
76 #include <unistd.h>
79 using namespace ::com::sun::star;
81 namespace {
83 typedef ::cppu::WeakComponentImplHelper1< presentation::XSlideShowView > ViewBase;
84 class View : public ::comphelper::OBaseMutex,
85 public ViewBase
87 public:
88 explicit View( const uno::Reference< rendering::XSpriteCanvas >& rCanvas ) :
89 ViewBase( m_aMutex ),
90 mxCanvas( rCanvas ),
91 maPaintListeners( m_aMutex ),
92 maTransformationListeners( m_aMutex ),
93 maMouseListeners( m_aMutex ),
94 maMouseMotionListeners( m_aMutex ),
95 maTransform(),
96 maSize()
100 void resize( const ::Size& rNewSize )
102 maSize = rNewSize;
103 maTransform.identity();
104 const sal_Int32 nSize( std::min( rNewSize.Width(),
105 rNewSize.Height() ) - 10 );
106 maTransform.scale( nSize, nSize );
107 maTransform.translate( (rNewSize.Width() - nSize) / 2,
108 (rNewSize.Height() - nSize) / 2 );
110 lang::EventObject aEvent( *this );
111 maTransformationListeners.notifyEach( &util::XModifyListener::modified,
112 aEvent );
115 void repaint()
117 awt::PaintEvent aEvent( *this,
118 awt::Rectangle(),
119 0 );
120 maPaintListeners.notifyEach( &awt::XPaintListener::windowPaint,
121 aEvent );
124 private:
125 virtual ~View() {}
127 virtual uno::Reference< rendering::XSpriteCanvas > SAL_CALL getCanvas( ) throw (uno::RuntimeException)
129 return mxCanvas;
132 virtual void SAL_CALL clear( ) throw (uno::RuntimeException)
134 ::basegfx::B2DPolygon aPoly( ::basegfx::tools::createPolygonFromRect(
135 ::basegfx::B2DRectangle(0.0,0.0,
136 maSize.Width(),
137 maSize.Height() )));
138 ::cppcanvas::SpriteCanvasSharedPtr pCanvas(
139 ::cppcanvas::VCLFactory::getInstance().createSpriteCanvas( mxCanvas ));
140 if( !pCanvas )
141 return;
143 ::cppcanvas::PolyPolygonSharedPtr pPolyPoly(
144 ::cppcanvas::BaseGfxFactory::getInstance().createPolyPolygon( pCanvas,
145 aPoly ) );
146 if( !pPolyPoly )
147 return;
149 if( pPolyPoly )
151 pPolyPoly->setRGBAFillColor( 0x808080FFU );
152 pPolyPoly->draw();
156 virtual geometry::AffineMatrix2D SAL_CALL getTransformation( ) throw (uno::RuntimeException)
158 geometry::AffineMatrix2D aRes;
159 return basegfx::unotools::affineMatrixFromHomMatrix( aRes,
160 maTransform );
163 virtual void SAL_CALL addTransformationChangedListener( const uno::Reference< util::XModifyListener >& xListener ) throw (uno::RuntimeException)
165 maTransformationListeners.addInterface( xListener );
168 virtual void SAL_CALL removeTransformationChangedListener( const uno::Reference< util::XModifyListener >& xListener ) throw (uno::RuntimeException)
170 maTransformationListeners.removeInterface( xListener );
173 virtual void SAL_CALL addPaintListener( const uno::Reference< awt::XPaintListener >& xListener ) throw (uno::RuntimeException)
175 maPaintListeners.addInterface( xListener );
178 virtual void SAL_CALL removePaintListener( const uno::Reference< awt::XPaintListener >& xListener ) throw (uno::RuntimeException)
180 maPaintListeners.removeInterface( xListener );
183 virtual void SAL_CALL addMouseListener( const uno::Reference< awt::XMouseListener >& xListener ) throw (uno::RuntimeException)
185 maMouseListeners.addInterface( xListener );
188 virtual void SAL_CALL removeMouseListener( const uno::Reference< awt::XMouseListener >& xListener ) throw (uno::RuntimeException)
190 maMouseListeners.removeInterface( xListener );
193 virtual void SAL_CALL addMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener ) throw (uno::RuntimeException)
195 maMouseMotionListeners.addInterface( xListener );
198 virtual void SAL_CALL removeMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener ) throw (uno::RuntimeException)
200 maMouseMotionListeners.removeInterface( xListener );
203 virtual void SAL_CALL setMouseCursor( ::sal_Int16 /*nPointerShape*/ ) throw (uno::RuntimeException)
207 uno::Reference< rendering::XSpriteCanvas > mxCanvas;
208 ::cppu::OInterfaceContainerHelper maPaintListeners;
209 ::cppu::OInterfaceContainerHelper maTransformationListeners;
210 ::cppu::OInterfaceContainerHelper maMouseListeners;
211 ::cppu::OInterfaceContainerHelper maMouseMotionListeners;
212 basegfx::B2DHomMatrix maTransform;
213 Size maSize;
216 typedef ::cppu::WeakComponentImplHelper2< drawing::XDrawPage,
217 beans::XPropertySet > SlideBase;
218 class DummySlide : public ::comphelper::OBaseMutex,
219 public SlideBase
221 public:
222 DummySlide() : SlideBase( m_aMutex ) {}
224 private:
225 // XDrawPage
226 virtual void SAL_CALL add( const uno::Reference< drawing::XShape >& /*xShape*/ ) throw (uno::RuntimeException)
230 virtual void SAL_CALL remove( const uno::Reference< drawing::XShape >& /*xShape*/ ) throw (uno::RuntimeException)
234 virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException)
236 return 0;
239 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 /*Index*/ ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
241 return uno::Any();
244 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException)
246 return uno::Type();
249 virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException)
251 return false;
254 // XPropertySet
255 virtual uno::Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) throw (uno::RuntimeException)
257 return uno::Reference< beans::XPropertySetInfo >();
260 virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& /*aPropertyName*/,
261 const uno::Any& /*aValue*/ ) throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
265 virtual uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
267 typedef ::canvas::tools::ValueMap< sal_Int16 > PropMapT;
269 // fixed PropertyValue map
270 static PropMapT::MapEntry lcl_propertyMap[] =
272 {"Height", 100},
273 {"MinimalFrameNumber", 50},
274 {"TransitionDuration", 10},
275 {"TransitionSubtype", animations::TransitionSubType::FROMTOPLEFT},
276 {"TransitionType", animations::TransitionType::PUSHWIPE},
277 {"Width", 100}
280 static PropMapT aMap( lcl_propertyMap,
281 sizeof(lcl_propertyMap)/sizeof(*lcl_propertyMap),
282 true );
284 sal_Int16 aRes;
285 if( !aMap.lookup( PropertyName, aRes ))
286 return uno::Any();
288 return uno::makeAny(aRes);
291 virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/,
292 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
296 virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/,
297 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
301 virtual void SAL_CALL addVetoableChangeListener( const ::rtl::OUString& /*PropertyName*/,
302 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
306 virtual void SAL_CALL removeVetoableChangeListener( const ::rtl::OUString& /*PropertyName*/,
307 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
313 class DemoApp : public Application
315 public:
316 virtual void Main();
317 virtual USHORT Exception( USHORT nError );
320 class ChildWindow : public Window
322 public:
323 ChildWindow( Window* pParent );
324 virtual ~ChildWindow();
325 virtual void Paint( const Rectangle& rRect );
326 virtual void Resize();
328 void setShow( const uno::Reference< presentation::XSlideShow >& rShow ) { mxShow = rShow; init(); }
330 private:
331 void init();
333 rtl::Reference< View > mpView;
334 uno::Reference< presentation::XSlideShow > mxShow;
337 ChildWindow::ChildWindow( Window* pParent ) :
338 Window(pParent, WB_CLIPCHILDREN | WB_BORDER| WB_3DLOOK ),
339 mpView(),
340 mxShow()
342 EnablePaint( true );
343 Show();
346 ChildWindow::~ChildWindow()
348 if( mxShow.is() && mpView.is() )
349 mxShow->removeView( mpView.get() );
352 void ChildWindow::init()
356 if( !mpView.is() )
358 uno::Reference< rendering::XCanvas > xCanvas( GetCanvas(),
359 uno::UNO_QUERY_THROW );
360 uno::Reference< rendering::XSpriteCanvas > xSpriteCanvas( xCanvas,
361 uno::UNO_QUERY_THROW );
362 mpView = new View( xSpriteCanvas );
363 mpView->resize( GetSizePixel() );
365 if( mxShow.is() )
366 mxShow->addView( mpView.get() );
369 catch (const uno::Exception &e)
371 OSL_TRACE( "Exception '%s' thrown\n" ,
372 (const sal_Char*)::rtl::OUStringToOString( e.Message,
373 RTL_TEXTENCODING_UTF8 ));
377 void ChildWindow::Paint( const Rectangle& /*rRect*/ )
381 if( mpView.is() )
382 mpView->repaint();
384 catch (const uno::Exception &e)
386 OSL_TRACE( "Exception '%s' thrown\n" ,
387 (const sal_Char*)::rtl::OUStringToOString( e.Message,
388 RTL_TEXTENCODING_UTF8 ));
392 void ChildWindow::Resize()
394 if( mpView.is() )
395 mpView->resize( GetSizePixel() );
398 class DemoWindow : public Dialog
400 public:
401 DemoWindow();
402 virtual void Paint( const Rectangle& rRect );
403 virtual void Resize();
405 private:
406 void init();
407 DECL_LINK( updateHdl, Timer* );
409 ChildWindow maLeftChild;
410 ChildWindow maRightTopChild;
411 ChildWindow maRightBottomChild;
412 uno::Reference< presentation::XSlideShow > mxShow;
413 AutoTimer maUpdateTimer;
414 bool mbSlideDisplayed;
417 DemoWindow::DemoWindow() :
418 Dialog((Window*)NULL),
419 maLeftChild( this ),
420 maRightTopChild( this ),
421 maRightBottomChild( this ),
422 mxShow(),
423 maUpdateTimer(),
424 mbSlideDisplayed( false )
426 SetText( rtl::OUString::createFromAscii( "Slideshow Demo" ) );
427 SetSizePixel( Size( 640, 480 ) );
428 EnablePaint( true );
430 maLeftChild.SetPosSizePixel( Point(), Size(320,480) );
431 maRightTopChild.SetPosSizePixel( Point(320,0), Size(320,240) );
432 maRightBottomChild.SetPosSizePixel( Point(320,240), Size(320,240) );
433 Show();
435 maUpdateTimer.SetTimeoutHdl(LINK(this, DemoWindow, updateHdl));
436 maUpdateTimer.SetTimeout( (ULONG)30 );
437 maUpdateTimer.Start();
440 void DemoWindow::init()
444 if( !mxShow.is() )
446 uno::Reference< lang::XMultiServiceFactory > xFactory(
447 ::comphelper::getProcessServiceFactory(),
448 uno::UNO_QUERY_THROW );
450 uno::Reference< uno::XInterface > xInt( xFactory->createInstance(
451 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.presentation.SlideShow")) ));
453 mxShow.set( xInt,
454 uno::UNO_QUERY_THROW );
456 maLeftChild.setShow( mxShow );
457 maRightTopChild.setShow( mxShow );
458 maRightBottomChild.setShow( mxShow );
461 if( mxShow.is() && !mbSlideDisplayed )
463 uno::Reference< drawing::XDrawPage > xSlide( new DummySlide );
464 mxShow->displaySlide( xSlide,
465 uno::Reference< animations::XAnimationNode >(),
466 uno::Sequence< beans::PropertyValue >() );
467 mxShow->setProperty( beans::PropertyValue(
468 rtl::OUString::createFromAscii("RehearseTimings"),
470 uno::makeAny( sal_True ),
471 beans::PropertyState_DIRECT_VALUE ));
472 mbSlideDisplayed = true;
475 catch (const uno::Exception &e)
477 OSL_TRACE( "Exception '%s' thrown\n" ,
478 (const sal_Char*)::rtl::OUStringToOString( e.Message,
479 RTL_TEXTENCODING_UTF8 ));
483 IMPL_LINK( DemoWindow, updateHdl, Timer*, EMPTYARG )
485 init();
487 double nTimeout;
488 if( mxShow.is() )
489 mxShow->update(nTimeout);
491 return 0;
494 void DemoWindow::Paint( const Rectangle& /*rRect*/ )
496 init();
499 void DemoWindow::Resize()
501 // TODO
504 USHORT DemoApp::Exception( USHORT nError )
506 switch( nError & EXC_MAJORTYPE )
508 case EXC_RSCNOTLOADED:
509 Abort( String::CreateFromAscii( "Error: could not load language resources.\nPlease check your installation.\n" ) );
510 break;
512 return 0;
515 void DemoApp::Main()
517 bool bHelp = false;
519 for( USHORT i = 0; i < GetCommandLineParamCount(); i++ )
521 ::rtl::OUString aParam = GetCommandLineParam( i );
523 if( aParam.equalsAscii( "--help" ) ||
524 aParam.equalsAscii( "-h" ) )
525 bHelp = true;
528 if( bHelp )
530 printf( "demoshow - life Slideshow testbed\n" );
531 return;
534 // bootstrap UNO
535 uno::Reference< lang::XMultiServiceFactory > xFactory;
538 uno::Reference< uno::XComponentContext > xCtx = ::cppu::defaultBootstrap_InitialComponentContext();
539 xFactory = uno::Reference< lang::XMultiServiceFactory >( xCtx->getServiceManager(),
540 uno::UNO_QUERY );
541 if( xFactory.is() )
542 ::comphelper::setProcessServiceFactory( xFactory );
544 catch( uno::RuntimeException& )
546 throw;
548 catch( uno::Exception& )
550 OSL_ENSURE( false,
551 rtl::OUStringToOString(
552 comphelper::anyToString( cppu::getCaughtException() ),
553 RTL_TEXTENCODING_UTF8 ).getStr() );
556 if( !xFactory.is() )
558 OSL_TRACE( "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
559 exit( 1 );
562 // Create UCB.
563 uno::Sequence< uno::Any > aArgs( 2 );
564 aArgs[ 0 ] <<= rtl::OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
565 aArgs[ 1 ] <<= rtl::OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
566 ::ucbhelper::ContentBroker::initialize( xFactory, aArgs );
568 DemoWindow pWindow;
569 pWindow.Execute();
571 // clean up UCB
572 ::ucbhelper::ContentBroker::deinitialize();
576 DemoApp aApp;