1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: userpaintoverlay.cxx,v $
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
;
65 class PaintOverlayHandler
: public MouseEventHandler
,
66 public ViewEventHandler
,
67 public UserPaintEventHandler
70 PaintOverlayHandler( const RGBColor
& rStrokeColor
,
72 ActivitiesQueue
& rActivitiesQueue
,
73 ScreenUpdater
& rScreenUpdater
,
74 const UnoViewContainer
& rViews
) :
75 mrActivitiesQueue( rActivitiesQueue
),
76 mrScreenUpdater( rScreenUpdater
),
78 maStrokeColor( rStrokeColor
),
79 mnStrokeWidth( nStrokeWidth
),
82 mbIsLastPointValid( false ),
83 mbIsLastMouseDownPosValid( false )
85 std::for_each( rViews
.begin(),
87 boost::bind( &PaintOverlayHandler::viewAdded
,
92 virtual void dispose()
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(),
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
)
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
137 virtual bool handleMouseReleased( const awt::MouseEvent
& e
)
139 if (e
.Buttons
!= awt::MouseButton::LEFT
)
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
148 if( mbIsLastMouseDownPosValid
&&
149 ::basegfx::B2DPoint( e
.X
,
150 e
.Y
) == maLastMouseDownPos
)
152 mbIsLastMouseDownPosValid
= false;
156 // invalidate, next downpress will have to start a new
158 mbIsLastPointValid
= false;
160 // eat mouse click (though we don't process it
161 // _directly_, it enables the drag mode
165 virtual bool handleMouseEntered( const awt::MouseEvent
& e
)
167 mbIsLastPointValid
= true;
168 maLastPoint
.setX( e
.X
);
169 maLastPoint
.setY( e
.Y
);
174 virtual bool handleMouseExited( const awt::MouseEvent
& )
176 mbIsLastPointValid
= false;
177 mbIsLastMouseDownPosValid
= false;
182 virtual bool handleMouseDragged( const awt::MouseEvent
& e
)
184 if( !mbIsLastPointValid
)
186 mbIsLastPointValid
= true;
187 maLastPoint
.setX( e
.X
);
188 maLastPoint
.setY( e
.Y
);
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();
205 ::cppcanvas::PolyPolygonSharedPtr
pPolyPoly(
206 ::cppcanvas::BaseGfxFactory::getInstance().createPolyPolygon( (*aIter
)->getCanvas(),
211 pPolyPoly
->setRGBALineColor( maStrokeColor
.getIntegerColor() );
212 pPolyPoly
->setStrokeWidth(mnStrokeWidth
);
217 // screen update necessary to show painting
218 mrScreenUpdater
.notifyUpdate();
221 // mouse events captured
225 virtual bool handleMouseMoved( const awt::MouseEvent
& /*e*/ )
228 return false; // did not handle the event
232 bool colorChanged( RGBColor
const& rUserColor
){
233 this->maStrokeColor
= rUserColor
;
237 bool widthChanged( double nUserStrokeWidth
){
238 this->mnStrokeWidth
= nUserStrokeWidth
;
243 //this->maStrokeColor = *(new RGBColor(255,255,255));
244 //this->mnStrokeWidth = 4.0;
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
,
263 const SlideShowContext
& rContext
)
265 UserPaintOverlaySharedPtr
pRet( new UserPaintOverlay( rStrokeColor
,
272 UserPaintOverlay::UserPaintOverlay( const RGBColor
& rStrokeColor
,
274 const SlideShowContext
& rContext
) :
275 mpHandler( new PaintOverlayHandler( rStrokeColor
,
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() );