Update ooo320-m1
[ooovba.git] / slideshow / source / engine / slide / userpaintoverlay.cxx
blob0dcefb49193cae02e313073ee477604166f21a3d
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: userpaintoverlay.cxx,v $
10 * $Revision: 1.3 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 #include <canvas/debug.hxx>
36 #include <comphelper/anytostring.hxx>
37 #include <cppuhelper/exc_hlp.hxx>
39 #include <com/sun/star/awt/MouseButton.hpp>
40 #include <com/sun/star/presentation/XSlideShowView.hpp>
42 #include <basegfx/point/b2dpoint.hxx>
43 #include <basegfx/polygon/b2dpolygon.hxx>
44 #include <cppcanvas/basegfxfactory.hxx>
46 #include "activity.hxx"
47 #include "activitiesqueue.hxx"
48 #include "slideshowcontext.hxx"
49 #include "userpaintoverlay.hxx"
50 #include "mouseeventhandler.hxx"
51 #include "eventmultiplexer.hxx"
52 #include "screenupdater.hxx"
53 #include "vieweventhandler.hxx"
55 #include <boost/bind.hpp>
56 #include <boost/noncopyable.hpp>
59 using namespace ::com::sun::star;
61 namespace slideshow
63 namespace internal
65 class PaintOverlayHandler : public MouseEventHandler,
66 public ViewEventHandler,
67 public UserPaintEventHandler
69 public:
70 PaintOverlayHandler( const RGBColor& rStrokeColor,
71 double nStrokeWidth,
72 ActivitiesQueue& rActivitiesQueue,
73 ScreenUpdater& rScreenUpdater,
74 const UnoViewContainer& rViews ) :
75 mrActivitiesQueue( rActivitiesQueue ),
76 mrScreenUpdater( rScreenUpdater ),
77 maViews(),
78 maStrokeColor( rStrokeColor ),
79 mnStrokeWidth( nStrokeWidth ),
80 maLastPoint(),
81 maLastMouseDownPos(),
82 mbIsLastPointValid( false ),
83 mbIsLastMouseDownPosValid( false )
85 std::for_each( rViews.begin(),
86 rViews.end(),
87 boost::bind( &PaintOverlayHandler::viewAdded,
88 this,
89 _1 ));
92 virtual void dispose()
94 maViews.clear();
97 // ViewEventHandler methods
98 virtual void viewAdded( const UnoViewSharedPtr& rView )
100 maViews.push_back( rView );
103 virtual void viewRemoved( const UnoViewSharedPtr& rView )
105 maViews.erase( ::std::remove( maViews.begin(),
106 maViews.end(),
107 rView ) );
110 virtual void viewChanged( const UnoViewSharedPtr& /*rView*/ )
112 // TODO(F2): for persistent drawings, need to store
113 // polygon and repaint here.
116 virtual void viewsChanged()
118 // TODO(F2): for persistent drawings, need to store
119 // polygon and repaint here.
122 // MouseEventHandler methods
123 virtual bool handleMousePressed( const awt::MouseEvent& e )
125 if (e.Buttons != awt::MouseButton::LEFT)
126 return false;
128 maLastMouseDownPos.setX( e.X );
129 maLastMouseDownPos.setY( e.Y );
130 mbIsLastMouseDownPosValid = true;
132 // eat mouse click (though we don't process it
133 // _directly_, it enables the drag mode
134 return true;
137 virtual bool handleMouseReleased( const awt::MouseEvent& e )
139 if (e.Buttons != awt::MouseButton::LEFT)
140 return false;
142 // check, whether up- and down press are on exactly
143 // the same pixel. If that's the case, ignore the
144 // click, and pass on the event to low-prio
145 // handlers. This effectively permits effect
146 // advancements via clicks also when user paint is
147 // enabled.
148 if( mbIsLastMouseDownPosValid &&
149 ::basegfx::B2DPoint( e.X,
150 e.Y ) == maLastMouseDownPos )
152 mbIsLastMouseDownPosValid = false;
153 return false;
156 // invalidate, next downpress will have to start a new
157 // polygon.
158 mbIsLastPointValid = false;
160 // eat mouse click (though we don't process it
161 // _directly_, it enables the drag mode
162 return true;
165 virtual bool handleMouseEntered( const awt::MouseEvent& e )
167 mbIsLastPointValid = true;
168 maLastPoint.setX( e.X );
169 maLastPoint.setY( e.Y );
171 return true;
174 virtual bool handleMouseExited( const awt::MouseEvent& )
176 mbIsLastPointValid = false;
177 mbIsLastMouseDownPosValid = false;
179 return true;
182 virtual bool handleMouseDragged( const awt::MouseEvent& e )
184 if( !mbIsLastPointValid )
186 mbIsLastPointValid = true;
187 maLastPoint.setX( e.X );
188 maLastPoint.setY( e.Y );
190 else
192 ::basegfx::B2DPolygon aPoly;
193 aPoly.append( maLastPoint );
195 maLastPoint.setX( e.X );
196 maLastPoint.setY( e.Y );
198 aPoly.append( maLastPoint );
200 // paint to all views
201 for( UnoViewVector::iterator aIter=maViews.begin(), aEnd=maViews.end();
202 aIter!=aEnd;
203 ++aIter )
205 ::cppcanvas::PolyPolygonSharedPtr pPolyPoly(
206 ::cppcanvas::BaseGfxFactory::getInstance().createPolyPolygon( (*aIter)->getCanvas(),
207 aPoly ) );
209 if( pPolyPoly )
211 pPolyPoly->setRGBALineColor( maStrokeColor.getIntegerColor() );
212 pPolyPoly->setStrokeWidth(mnStrokeWidth);
213 pPolyPoly->draw();
217 // screen update necessary to show painting
218 mrScreenUpdater.notifyUpdate();
221 // mouse events captured
222 return true;
225 virtual bool handleMouseMoved( const awt::MouseEvent& /*e*/ )
227 // not used here
228 return false; // did not handle the event
232 bool colorChanged( RGBColor const& rUserColor ){
233 this->maStrokeColor = rUserColor;
234 return true;
237 bool widthChanged( double nUserStrokeWidth ){
238 this->mnStrokeWidth = nUserStrokeWidth;
239 return true;
242 bool disable(){
243 //this->maStrokeColor = *(new RGBColor(255,255,255));
244 //this->mnStrokeWidth = 4.0;
245 return true;
249 private:
250 ActivitiesQueue& mrActivitiesQueue;
251 ScreenUpdater& mrScreenUpdater;
252 UnoViewVector maViews;
253 RGBColor maStrokeColor;
254 double mnStrokeWidth;
255 basegfx::B2DPoint maLastPoint;
256 basegfx::B2DPoint maLastMouseDownPos;
257 bool mbIsLastPointValid;
258 bool mbIsLastMouseDownPosValid;
261 UserPaintOverlaySharedPtr UserPaintOverlay::create( const RGBColor& rStrokeColor,
262 double nStrokeWidth,
263 const SlideShowContext& rContext )
265 UserPaintOverlaySharedPtr pRet( new UserPaintOverlay( rStrokeColor,
266 nStrokeWidth,
267 rContext ));
269 return pRet;
272 UserPaintOverlay::UserPaintOverlay( const RGBColor& rStrokeColor,
273 double nStrokeWidth,
274 const SlideShowContext& rContext ) :
275 mpHandler( new PaintOverlayHandler( rStrokeColor,
276 nStrokeWidth,
277 rContext.mrActivitiesQueue,
278 rContext.mrScreenUpdater,
279 rContext.mrViewContainer )),
280 mrMultiplexer( rContext.mrEventMultiplexer )
282 mrMultiplexer.addClickHandler( mpHandler, 3.0 );
283 mrMultiplexer.addMouseMoveHandler( mpHandler, 3.0 );
284 mrMultiplexer.addViewHandler( mpHandler );
285 mrMultiplexer.addUserPaintHandler(mpHandler);
288 UserPaintOverlay::~UserPaintOverlay()
292 mrMultiplexer.removeMouseMoveHandler( mpHandler );
293 mrMultiplexer.removeClickHandler( mpHandler );
294 mrMultiplexer.removeViewHandler( mpHandler );
295 mpHandler->dispose();
297 catch (uno::Exception &) {
298 OSL_ENSURE( false, rtl::OUStringToOString(
299 comphelper::anyToString(
300 cppu::getCaughtException() ),
301 RTL_TEXTENCODING_UTF8 ).getStr() );