xref: /AOO41X/main/canvas/inc/canvas/base/canvascustomspritebase.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 #ifndef INCLUDED_CANVAS_CANVASCUSTOMSPRITEBASE_HXX
29 #define INCLUDED_CANVAS_CANVASCUSTOMSPRITEBASE_HXX
30 
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/rendering/XCustomSprite.hpp>
33 #include <com/sun/star/rendering/XPolyPolygon2D.hpp>
34 #include <basegfx/point/b2dpoint.hxx>
35 #include <basegfx/vector/b2dvector.hxx>
36 #include <basegfx/range/b2drange.hxx>
37 #include <canvas/base/integerbitmapbase.hxx>
38 #include <canvas/base/sprite.hxx>
39 
40 #include <boost/utility.hpp>
41 
42 
43 namespace canvas
44 {
45     /** Helper template to handle XCustomSprite method forwarding to
46         CanvasCustomSpriteHelper
47 
48     	Use this helper to handle the XCustomSprite part of your
49     	implementation.
50 
51         @tpl Base
52         Base class to use, most probably one of the
53         WeakComponentImplHelperN templates with the appropriate
54         interfaces. At least XCustomSprite and Sprite should be among
55         them (why else would you use this template, then?). Base class
56         must have an Base( const Mutex& ) constructor (like the
57         WeakComponentImplHelperN templates have).
58 
59         @tpl SpriteHelper
60         Sprite helper implementation for the backend in question
61 
62         @tpl CanvasHelper
63         Canvas helper implementation for the backend in question
64 
65         @tpl Mutex
66         Lock strategy to use. Defaults to using the
67         OBaseMutex-provided lock.  Everytime one of the methods is
68         entered, an object of type Mutex is created with m_aMutex as
69         the sole parameter, and destroyed again when the method scope
70         is left.
71 
72         @tpl UnambiguousBase
73         Optional unambiguous base class for XInterface of Base. It's
74         sometimes necessary to specify this parameter, e.g. if Base
75         derives from multiple UNO interface (were each provides its
76         own version of XInterface, making the conversion ambiguous)
77 
78         @see CanvasCustomSpriteHelper for further contractual
79         requirements towards the SpriteHelper type, and some examples.
80      */
81     template< class Base,
82               class SpriteHelper,
83               class CanvasHelper,
84               class Mutex=::osl::MutexGuard,
85               class UnambiguousBase=::com::sun::star::uno::XInterface > class CanvasCustomSpriteBase :
86         public IntegerBitmapBase< Base, CanvasHelper, Mutex, UnambiguousBase >
87     {
88     public:
89         typedef IntegerBitmapBase< Base, CanvasHelper, Mutex, UnambiguousBase >	BaseType;
90         typedef SpriteHelper													SpriteHelperType;
91 
92         CanvasCustomSpriteBase() :
93             maSpriteHelper()
94         {
95         }
96 
97         /** Object is being disposed.
98 
99         	Called from the cppu helper base, to notify disposal of
100         	this object. Already releases all internal references.
101 
102             @derive when overriding this method in derived classes,
103             <em>always</em> call the base class' method!
104          */
105         virtual void SAL_CALL disposing()
106         {
107             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
108 
109             maSpriteHelper.disposing();
110 
111             // pass on to base class
112             BaseType::disposing();
113         }
114 
115         // XCanvas: selectively override base's methods here, for opacity tracking
116         virtual void SAL_CALL clear() throw (::com::sun::star::uno::RuntimeException)
117         {
118             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
119 
120             maSpriteHelper.clearingContent( this );
121 
122             // and forward to base class, which handles the actual rendering
123             return BaseType::clear();
124         }
125 
126         virtual ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive > SAL_CALL
127         	drawBitmap( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmap >& xBitmap,
128                         const ::com::sun::star::rendering::ViewState& 									viewState,
129                         const ::com::sun::star::rendering::RenderState& 								renderState ) throw (::com::sun::star::lang::IllegalArgumentException,
130                                                                                                                              ::com::sun::star::uno::RuntimeException)
131         {
132             tools::verifyArgs(xBitmap, viewState, renderState,
133                               BOOST_CURRENT_FUNCTION,
134                               static_cast< typename BaseType::UnambiguousBaseType* >(this));
135 
136             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
137 
138             maSpriteHelper.checkDrawBitmap( this, xBitmap, viewState, renderState );
139 
140             // and forward to base class, which handles the actual rendering
141             return BaseType::drawBitmap( xBitmap,
142                                          viewState,
143                                          renderState );
144         }
145 
146         // TODO(F3): If somebody uses the XIntegerBitmap methods to
147         // clear pixel (setting alpha != 1.0 there), or a compositing
148         // mode results in similar alpha, maSpriteHelper might
149         // errorneously report fully opaque sprites. Effectively, all
150         // render methods must be overridden here; or better,
151         // functionality provided at the baseclass.
152 
153         // XSprite
154         virtual void SAL_CALL setAlpha( double alpha ) throw (::com::sun::star::lang::IllegalArgumentException,
155                                                               ::com::sun::star::uno::RuntimeException)
156         {
157             tools::verifyRange( alpha, 0.0, 1.0 );
158 
159             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
160 
161             maSpriteHelper.setAlpha( this, alpha );
162         }
163 
164         virtual void SAL_CALL move( const ::com::sun::star::geometry::RealPoint2D& 	aNewPos,
165                                     const ::com::sun::star::rendering::ViewState& 	viewState,
166                                     const ::com::sun::star::rendering::RenderState& renderState ) throw (::com::sun::star::lang::IllegalArgumentException,
167                                                                                                          ::com::sun::star::uno::RuntimeException)
168         {
169             tools::verifyArgs(aNewPos, viewState, renderState,
170                               BOOST_CURRENT_FUNCTION,
171                               static_cast< typename BaseType::UnambiguousBaseType* >(this));
172 
173             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
174 
175             maSpriteHelper.move( this, aNewPos, viewState, renderState );
176         }
177 
178         virtual void SAL_CALL transform( const ::com::sun::star::geometry::AffineMatrix2D& aTransformation ) throw (::com::sun::star::lang::IllegalArgumentException,
179                                                                                                                     ::com::sun::star::uno::RuntimeException)
180         {
181             tools::verifyArgs(aTransformation,
182                               BOOST_CURRENT_FUNCTION,
183                               static_cast< typename BaseType::UnambiguousBaseType* >(this));
184 
185             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
186 
187             maSpriteHelper.transform( this, aTransformation );
188         }
189 
190         virtual void SAL_CALL clip( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XPolyPolygon2D >& aClip ) throw (::com::sun::star::uno::RuntimeException)
191         {
192             // NULL xClip explicitely allowed here (to clear clipping)
193 
194             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
195 
196             maSpriteHelper.clip( this, aClip );
197         }
198 
199         virtual void SAL_CALL setPriority( double nPriority ) throw (::com::sun::star::uno::RuntimeException)
200         {
201             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
202 
203             maSpriteHelper.setPriority( this, nPriority );
204         }
205 
206         virtual void SAL_CALL show() throw (::com::sun::star::uno::RuntimeException)
207         {
208             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
209 
210             maSpriteHelper.show( this );
211         }
212 
213         virtual void SAL_CALL hide() throw (::com::sun::star::uno::RuntimeException)
214         {
215             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
216 
217             maSpriteHelper.hide( this );
218         }
219 
220         // XCustomSprite
221         virtual ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvas > SAL_CALL
222         	getContentCanvas() throw (::com::sun::star::uno::RuntimeException)
223         {
224             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
225 
226             return this;
227         }
228 
229         // Sprite
230         virtual bool isAreaUpdateOpaque( const ::basegfx::B2DRange& rUpdateArea ) const
231         {
232             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
233 
234             return maSpriteHelper.isAreaUpdateOpaque( rUpdateArea );
235         }
236 
237         virtual bool isContentChanged() const
238         {
239             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
240 
241             return BaseType::mbSurfaceDirty;
242         }
243 
244         virtual ::basegfx::B2DPoint getPosPixel() const
245         {
246             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
247 
248             return maSpriteHelper.getPosPixel();
249         }
250 
251         virtual ::basegfx::B2DVector getSizePixel() const
252         {
253             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
254 
255             return maSpriteHelper.getSizePixel();
256         }
257 
258         virtual ::basegfx::B2DRange getUpdateArea() const
259         {
260             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
261 
262             return maSpriteHelper.getUpdateArea();
263         }
264 
265         virtual double getPriority() const
266         {
267             typename BaseType::MutexType aGuard( BaseType::m_aMutex );
268 
269             return maSpriteHelper.getPriority();
270         }
271 
272     protected:
273         SpriteHelperType maSpriteHelper;
274     };
275 }
276 
277 #endif /* INCLUDED_CANVAS_CANVASCUSTOMSPRITEBASE_HXX */
278