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: vclfactory.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_cppcanvas.hxx"
33 #include <rtl/instance.hxx>
34 #include <osl/getglobalmutex.hxx>
35 #include <osl/diagnose.h>
36 #include <com/sun/star/rendering/InterpolationMode.hpp>
37 #include <vcl/window.hxx>
38 #include <vcl/graph.hxx>
39 #include <vcl/canvastools.hxx>
40 #include <basegfx/polygon/b2dpolypolygon.hxx>
42 #include <cppcanvas/vclfactory.hxx>
44 #include <implbitmapcanvas.hxx>
45 #include <implspritecanvas.hxx>
46 #include <implpolypolygon.hxx>
47 #include <implbitmap.hxx>
48 #include <implrenderer.hxx>
49 #include <impltext.hxx>
50 #include <implsprite.hxx>
53 using namespace ::com::sun::star
;
57 /* Singleton handling */
60 VCLFactory
* operator()()
62 return new VCLFactory();
66 VCLFactory
& VCLFactory::getInstance()
68 return *rtl_Instance
< VCLFactory
, InitInstance
, ::osl::MutexGuard
,
69 ::osl::GetGlobalMutex
>::create(
70 InitInstance(), ::osl::GetGlobalMutex());
73 VCLFactory::VCLFactory()
77 VCLFactory::~VCLFactory()
81 BitmapCanvasSharedPtr
VCLFactory::createCanvas( const ::Window
& rVCLWindow
)
83 return BitmapCanvasSharedPtr(
84 new internal::ImplBitmapCanvas(
85 uno::Reference
< rendering::XBitmapCanvas
>(
86 rVCLWindow
.GetCanvas(),
90 BitmapCanvasSharedPtr
VCLFactory::createCanvas( const uno::Reference
< rendering::XBitmapCanvas
>& xCanvas
)
92 return BitmapCanvasSharedPtr(
93 new internal::ImplBitmapCanvas( xCanvas
) );
96 SpriteCanvasSharedPtr
VCLFactory::createSpriteCanvas( const ::Window
& rVCLWindow
) const
98 return SpriteCanvasSharedPtr(
99 new internal::ImplSpriteCanvas(
100 uno::Reference
< rendering::XSpriteCanvas
>(
101 rVCLWindow
.GetSpriteCanvas(),
105 SpriteCanvasSharedPtr
VCLFactory::createSpriteCanvas( const uno::Reference
< rendering::XSpriteCanvas
>& xCanvas
) const
107 return SpriteCanvasSharedPtr(
108 new internal::ImplSpriteCanvas( xCanvas
) );
111 SpriteCanvasSharedPtr
VCLFactory::createFullscreenSpriteCanvas( const ::Window
& rVCLWindow
,
112 const Size
& rFullscreenSize
) const
114 return SpriteCanvasSharedPtr(
115 new internal::ImplSpriteCanvas(
116 uno::Reference
< rendering::XSpriteCanvas
>(
117 rVCLWindow
.GetFullscreenSpriteCanvas( rFullscreenSize
),
121 PolyPolygonSharedPtr
VCLFactory::createPolyPolygon( const CanvasSharedPtr
& rCanvas
,
122 const ::Polygon
& rPoly
) const
124 OSL_ENSURE( rCanvas
.get() != NULL
&&
125 rCanvas
->getUNOCanvas().is(),
126 "VCLFactory::createPolyPolygon(): Invalid canvas" );
128 if( rCanvas
.get() == NULL
)
129 return PolyPolygonSharedPtr();
131 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
133 return PolyPolygonSharedPtr();
135 return PolyPolygonSharedPtr(
136 new internal::ImplPolyPolygon( rCanvas
,
137 ::vcl::unotools::xPolyPolygonFromPolygon(
138 xCanvas
->getDevice(),
142 PolyPolygonSharedPtr
VCLFactory::createPolyPolygon( const CanvasSharedPtr
& rCanvas
,
143 const ::PolyPolygon
& rPolyPoly
) const
145 OSL_ENSURE( rCanvas
.get() != NULL
&&
146 rCanvas
->getUNOCanvas().is(),
147 "VCLFactory::createPolyPolygon(): Invalid canvas" );
149 if( rCanvas
.get() == NULL
)
150 return PolyPolygonSharedPtr();
152 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
154 return PolyPolygonSharedPtr();
156 return PolyPolygonSharedPtr(
157 new internal::ImplPolyPolygon( rCanvas
,
158 ::vcl::unotools::xPolyPolygonFromPolyPolygon(
159 xCanvas
->getDevice(),
163 BitmapSharedPtr
VCLFactory::createBitmap( const CanvasSharedPtr
& rCanvas
,
164 const ::Size
& rSize
) const
166 OSL_ENSURE( rCanvas
.get() != NULL
&&
167 rCanvas
->getUNOCanvas().is(),
168 "VCLFactory::createBitmap(): Invalid canvas" );
170 if( rCanvas
.get() == NULL
)
171 return BitmapSharedPtr();
173 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
175 return BitmapSharedPtr();
177 return BitmapSharedPtr(
178 new internal::ImplBitmap( rCanvas
,
179 xCanvas
->getDevice()->createCompatibleBitmap(
180 ::vcl::unotools::integerSize2DFromSize(rSize
) ) ) );
183 BitmapSharedPtr
VCLFactory::createAlphaBitmap( const CanvasSharedPtr
& rCanvas
,
184 const ::Size
& rSize
) const
186 OSL_ENSURE( rCanvas
.get() != NULL
&&
187 rCanvas
->getUNOCanvas().is(),
188 "VCLFactory::createBitmap(): Invalid canvas" );
190 if( rCanvas
.get() == NULL
)
191 return BitmapSharedPtr();
193 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
195 return BitmapSharedPtr();
197 return BitmapSharedPtr(
198 new internal::ImplBitmap( rCanvas
,
199 xCanvas
->getDevice()->createCompatibleAlphaBitmap(
200 ::vcl::unotools::integerSize2DFromSize(rSize
) ) ) );
203 BitmapSharedPtr
VCLFactory::createBitmap( const CanvasSharedPtr
& rCanvas
,
204 const ::Bitmap
& rBitmap
) const
206 OSL_ENSURE( rCanvas
.get() != NULL
&&
207 rCanvas
->getUNOCanvas().is(),
208 "VCLFactory::createBitmap(): Invalid canvas" );
210 if( rCanvas
.get() == NULL
)
211 return BitmapSharedPtr();
213 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
215 return BitmapSharedPtr();
217 return BitmapSharedPtr( new internal::ImplBitmap( rCanvas
,
218 ::vcl::unotools::xBitmapFromBitmap(
219 xCanvas
->getDevice(),
223 BitmapSharedPtr
VCLFactory::createBitmap( const CanvasSharedPtr
& rCanvas
,
224 const ::BitmapEx
& rBmpEx
) const
226 OSL_ENSURE( rCanvas
.get() != NULL
&&
227 rCanvas
->getUNOCanvas().is(),
228 "VCLFactory::createBitmap(): Invalid canvas" );
230 if( rCanvas
.get() == NULL
)
231 return BitmapSharedPtr();
233 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
235 return BitmapSharedPtr();
237 return BitmapSharedPtr( new internal::ImplBitmap( rCanvas
,
238 ::vcl::unotools::xBitmapFromBitmapEx(
239 xCanvas
->getDevice(),
243 RendererSharedPtr
VCLFactory::createRenderer( const CanvasSharedPtr
& rCanvas
,
244 const ::Graphic
& rGraphic
,
245 const Renderer::Parameters
& rParms
) const
247 OSL_ENSURE( rCanvas
.get() != NULL
&&
248 rCanvas
->getUNOCanvas().is(),
249 "VCLFactory::createRenderer(): Invalid canvas" );
251 if( rCanvas
.get() == NULL
)
252 return RendererSharedPtr();
254 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
256 return RendererSharedPtr();
258 if( rGraphic
.GetType() == GRAPHIC_GDIMETAFILE
)
259 return RendererSharedPtr( new internal::ImplRenderer( rCanvas
,
260 rGraphic
.GetGDIMetaFile(),
263 return RendererSharedPtr( new internal::ImplRenderer( rCanvas
,
264 rGraphic
.GetBitmapEx(),
268 RendererSharedPtr
VCLFactory::createRenderer( const CanvasSharedPtr
& rCanvas
,
269 const ::GDIMetaFile
& rMtf
,
270 const Renderer::Parameters
& rParms
) const
272 return RendererSharedPtr( new internal::ImplRenderer( rCanvas
,
277 SpriteSharedPtr
VCLFactory::createAnimatedSprite( const SpriteCanvasSharedPtr
& rCanvas
, const ::Animation
& rAnim
) const
279 OSL_ENSURE( rCanvas
.get() != NULL
&&
280 rCanvas
->getUNOCanvas().is(),
281 "VCLFactory::createAnimatedSprite(): Invalid canvas" );
283 if( rCanvas
.get() == NULL
)
284 return SpriteSharedPtr();
286 uno::Reference
< rendering::XCanvas
> xCanvas( rCanvas
->getUNOCanvas() );
288 return SpriteSharedPtr();
290 uno::Reference
< rendering::XSpriteCanvas
> xSpriteCanvas( rCanvas
->getUNOSpriteCanvas() );
291 if( !xSpriteCanvas
.is() )
292 return SpriteSharedPtr();
294 if( rAnim
.IsEmpty() )
295 return SpriteSharedPtr();
297 internal::ImplSpriteCanvas
* pSpriteCanvas
= dynamic_cast< internal::ImplSpriteCanvas
* >( rCanvas
.get() );
299 return SpriteSharedPtr();
301 const USHORT
nBitmaps( rAnim
.Count() );
302 uno::Sequence
< uno::Reference
< rendering::XBitmap
> > aBitmapSequence( nBitmaps
);
303 uno::Reference
< rendering::XBitmap
>* pBitmaps
= aBitmapSequence
.getArray();
307 BitmapEx aRestoreBuffer
;
308 aBmpEx
.SetSizePixel( rAnim
.GetDisplaySizePixel() );
309 aRestoreBuffer
.SetSizePixel( rAnim
.GetDisplaySizePixel() );
310 aBmpEx
.Erase( ::Color( 255, 0,0,0 ) ); // clear alpha channel
311 aRestoreBuffer
= aBmpEx
;
312 const Point aEmptyPoint
;
314 for( i
=0; i
<nBitmaps
; ++i
)
316 const AnimationBitmap
& rAnimBmp( rAnim
.Get((USHORT
)i
) );
318 // Handle dispose according to GIF spec: a
319 // DISPOSE_PREVIOUS does _not_ mean to revert to the
320 // previous frame, but to revert to the last frame with
324 if( rAnimBmp
.eDisposal
== DISPOSE_BACK
)
326 // simply clear bitmap to transparent
327 aBmpEx
.Erase( ::Color( 255, 0,0,0 ) );
329 else if( rAnimBmp
.eDisposal
== DISPOSE_PREVIOUS
)
331 // copy in last known full frame
332 aBmpEx
= aRestoreBuffer
;
334 // I have exactly _no_ idea what DISPOSE_FULL is supposed
335 // to do. It's apparently not set anywhere in our code
336 OSL_ENSURE( rAnimBmp
.eDisposal
!=DISPOSE_FULL
,
337 "VCLFactory::createAnimatedSprite(): Somebody set the deprecated DISPOSE_FULL at the Animation" );
340 aBmpEx
.CopyPixel( Rectangle( rAnimBmp
.aPosPix
,
342 Rectangle( aEmptyPoint
,
346 // store last DISPOSE_NOT frame, for later
347 // DISPOSE_PREVIOUS updates
348 if( rAnimBmp
.eDisposal
== DISPOSE_NOT
)
349 aRestoreBuffer
= aBmpEx
;
351 pBitmaps
[i
] = ::vcl::unotools::xBitmapFromBitmapEx(
352 xCanvas
->getDevice(),
356 return pSpriteCanvas
->createSpriteFromBitmaps( aBitmapSequence
,
357 rendering::InterpolationMode::NEAREST_NEIGHBOR
);
360 TextSharedPtr
VCLFactory::createText( const CanvasSharedPtr
& rCanvas
, const ::rtl::OUString
& rText
) const
362 return TextSharedPtr( new internal::ImplText( rCanvas
,