Update ooo320-m1
[ooovba.git] / goodies / inc / grfmgr.hxx
blob6ff994ba56117913fd94f176bbfe55f3ce9cd7d5
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: grfmgr.hxx,v $
10 * $Revision: 1.23 $
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 #ifndef _GRFMGR_HXX
32 #define _GRFMGR_HXX
34 #include <vcl/graph.hxx>
36 // -----------
37 // - Defines -
38 // -----------
40 #define GRFMGR_DRAW_NOTCACHED 0x00000000UL
41 #define GRFMGR_DRAW_CACHED 0x00000001UL
42 #define GRFMGR_DRAW_BILINEAR 0x00000002UL
43 #define GRFMGR_DRAW_USE_DRAWMODE_SETTINGS 0x00000004UL
44 #define GRFMGR_DRAW_SUBSTITUTE 0x00000008UL
45 #define GRFMGR_DRAW_NO_SUBSTITUTE 0x00000010UL
46 #define GRFMGR_DRAW_STANDARD (GRFMGR_DRAW_CACHED|GRFMGR_DRAW_BILINEAR)
48 // --------------------
49 // - AutoSwap Defines -
50 // --------------------
52 #define GRFMGR_AUTOSWAPSTREAM_LINK ((SvStream*)0x00000000UL)
53 #define GRFMGR_AUTOSWAPSTREAM_LOADED ((SvStream*)0xfffffffdUL)
54 #define GRFMGR_AUTOSWAPSTREAM_TEMP ((SvStream*)0xfffffffeUL)
55 #define GRFMGR_AUTOSWAPSTREAM_NONE ((SvStream*)0xffffffffUL)
57 // ----------------------
58 // - Adjustment Defines -
59 // ----------------------
61 #define ADJUSTMENT_NONE 0x00000000UL
62 #define ADJUSTMENT_DRAWMODE 0x00000001UL
63 #define ADJUSTMENT_COLORS 0x00000002UL
64 #define ADJUSTMENT_MIRROR 0x00000004UL
65 #define ADJUSTMENT_ROTATE 0x00000008UL
66 #define ADJUSTMENT_TRANSPARENCY 0x00000010UL
67 #define ADJUSTMENT_ALL 0xFFFFFFFFUL
69 // ---------
70 // - Enums -
71 // ---------
73 enum GraphicDrawMode
75 GRAPHICDRAWMODE_STANDARD = 0,
76 GRAPHICDRAWMODE_GREYS = 1,
77 GRAPHICDRAWMODE_MONO = 2,
78 GRAPHICDRAWMODE_WATERMARK = 3
81 // ------------
82 // - Forwards -
83 // ------------
85 class GraphicManager;
86 class SvStream;
87 class BitmapWriteAccess;
88 class GraphicCache;
89 class VirtualDevice;
90 struct GrfSimpleCacheObj;
91 struct ImplTileInfo;
93 // ---------------
94 // - GraphicAttr -
95 // ---------------
97 class GraphicAttr
99 private:
101 long mnDummy1;
102 long mnDummy2;
103 double mfGamma;
104 sal_uInt32 mnMirrFlags;
105 long mnLeftCrop;
106 long mnTopCrop;
107 long mnRightCrop;
108 long mnBottomCrop;
109 USHORT mnRotate10;
110 short mnContPercent;
111 short mnLumPercent;
112 short mnRPercent;
113 short mnGPercent;
114 short mnBPercent;
115 BOOL mbInvert;
116 BYTE mcTransparency;
117 GraphicDrawMode meDrawMode;
119 void* mpDummy;
121 public:
123 GraphicAttr();
124 ~GraphicAttr();
126 BOOL operator==( const GraphicAttr& rAttr ) const;
127 BOOL operator!=( const GraphicAttr& rAttr ) const { return !( *this == rAttr ); }
129 void SetDrawMode( GraphicDrawMode eDrawMode ) { meDrawMode = eDrawMode; }
130 GraphicDrawMode GetDrawMode() const { return meDrawMode; }
132 void SetMirrorFlags( ULONG nMirrFlags ) { mnMirrFlags = nMirrFlags; }
133 ULONG GetMirrorFlags() const { return mnMirrFlags; }
135 void SetCrop( long nLeft_100TH_MM, long nTop_100TH_MM, long nRight_100TH_MM, long nBottom_100TH_MM )
137 mnLeftCrop = nLeft_100TH_MM; mnTopCrop = nTop_100TH_MM;
138 mnRightCrop = nRight_100TH_MM; mnBottomCrop = nBottom_100TH_MM;
140 long GetLeftCrop() const { return mnLeftCrop; }
141 long GetTopCrop() const { return mnTopCrop; }
142 long GetRightCrop() const { return mnRightCrop; }
143 long GetBottomCrop() const { return mnBottomCrop; }
145 void SetRotation( USHORT nRotate10 ) { mnRotate10 = nRotate10; }
146 USHORT GetRotation() const { return mnRotate10; }
148 void SetLuminance( short nLuminancePercent ) { mnLumPercent = nLuminancePercent; }
149 short GetLuminance() const { return mnLumPercent; }
151 void SetContrast( short nContrastPercent ) { mnContPercent = nContrastPercent; }
152 short GetContrast() const { return mnContPercent; }
154 void SetChannelR( short nChannelRPercent ) { mnRPercent = nChannelRPercent; }
155 short GetChannelR() const { return mnRPercent; }
157 void SetChannelG( short nChannelGPercent ) { mnGPercent = nChannelGPercent; }
158 short GetChannelG() const { return mnGPercent; }
160 void SetChannelB( short nChannelBPercent ) { mnBPercent = nChannelBPercent; }
161 short GetChannelB() const { return mnBPercent; }
163 void SetGamma( double fGamma ) { mfGamma = fGamma; }
164 double GetGamma() const { return mfGamma; }
166 void SetInvert( BOOL bInvert ) { mbInvert = bInvert; }
167 BOOL IsInvert() const { return mbInvert; }
169 void SetTransparency( BYTE cTransparency ) { mcTransparency = cTransparency; }
170 BYTE GetTransparency() const { return mcTransparency; }
172 BOOL IsSpecialDrawMode() const { return( meDrawMode != GRAPHICDRAWMODE_STANDARD ); }
173 BOOL IsMirrored() const { return( mnMirrFlags != 0UL ); }
174 BOOL IsCropped() const
176 return( mnLeftCrop != 0 || mnTopCrop != 0 ||
177 mnRightCrop != 0 || mnBottomCrop != 0 );
179 BOOL IsRotated() const { return( ( mnRotate10 % 3600 ) != 0 ); }
180 BOOL IsTransparent() const { return( mcTransparency > 0 ); }
181 BOOL IsAdjusted() const
183 return( mnLumPercent != 0 || mnContPercent != 0 || mnRPercent != 0 ||
184 mnGPercent != 0 || mnBPercent != 0 || mfGamma != 1.0 || mbInvert );
187 friend SvStream& operator<<( SvStream& rOStm, const GraphicAttr& rAttr );
188 friend SvStream& operator>>( SvStream& rIStm, GraphicAttr& rAttr );
191 // -----------------
192 // - GraphicObject -
193 // -----------------
195 class GraphicObject : public SvDataCopyStream
197 friend class GraphicManager;
199 private:
201 static GraphicManager* mpGlobalMgr;
203 Graphic maGraphic;
204 GraphicAttr maAttr;
205 Size maPrefSize;
206 MapMode maPrefMapMode;
207 ULONG mnSizeBytes;
208 GraphicType meType;
209 GraphicManager* mpMgr;
210 String* mpLink;
211 Link* mpSwapStreamHdl;
212 String* mpUserData;
213 Timer* mpSwapOutTimer;
214 GrfSimpleCacheObj* mpSimpleCache;
215 ULONG mnAnimationLoopCount;
216 void* mpDummy1;
217 void* mpDummy2;
218 BOOL mbAutoSwapped : 1;
219 BOOL mbTransparent : 1;
220 BOOL mbAnimated : 1;
221 BOOL mbEPS : 1;
222 BOOL mbIsInSwapIn : 1;
223 BOOL mbIsInSwapOut : 1;
224 BOOL mbAlpha : 1;
225 BOOL mbDummyFlag8 : 1;
227 //#if 0 // _SOLAR__PRIVATE
229 void ImplConstruct();
230 void ImplAssignGraphicData();
231 void ImplSetGraphicManager( const GraphicManager* pMgr,
232 const ByteString* pID = NULL,
233 const GraphicObject* pCopyObj = NULL );
234 void ImplAutoSwapIn();
235 BOOL ImplIsAutoSwapped() const { return mbAutoSwapped; }
236 BOOL ImplGetCropParams( OutputDevice* pOut, Point& rPt, Size& rSz, const GraphicAttr* pAttr,
237 PolyPolygon& rClipPolyPoly, BOOL& bRectClipRegion ) const;
239 /** Render a given number of tiles in an optimized way
241 This method recursively subdivides the tile rendering problem
242 in smaller parts, i.e. rendering output size x with few tiles
243 of size y, which in turn are generated from the original
244 bitmap in a recursive fashion. The subdivision size can be
245 controlled by the exponent argument, which specifies the
246 minimal number of smaller tiles used in one recursion
247 step. The resulting tile size is given as the integer number
248 of repetitions of the original bitmap along x and y. As the
249 exponent need not necessarily divide these numbers without
250 remainder, the repetition counts are effectively converted to
251 base-exponent numbers, where each place denotes the number of
252 times the corresponding tile size is rendered.
254 @param rVDev
255 Virtual device to render everything into
257 @param nExponent
258 Number of repetitions per subdivision step, _must_ be greater than 1
260 @param nNumTilesX
261 Number of original tiles to generate in x direction
263 @param nNumTilesY
264 Number of original tiles to generate in y direction
266 @param rTileSizePixel
267 Size in pixel of the original tile bitmap to render it in
269 @param pAttr
270 Graphic attributes to be used for rendering
272 @param nFlags
273 Graphic flags to be used for rendering
275 @param rCurrPos
276 Current output point for this recursion level (should start with (0,0))
278 @return true, if everything was successfully rendered.
280 bool ImplRenderTempTile( VirtualDevice& rVDev, int nExponent,
281 int nNumTilesX, int nNumTilesY,
282 const Size& rTileSizePixel,
283 const GraphicAttr* pAttr, ULONG nFlags );
285 /// internally called by ImplRenderTempTile()
286 bool ImplRenderTileRecursive( VirtualDevice& rVDev, int nExponent, int nMSBFactor,
287 int nNumOrigTilesX, int nNumOrigTilesY,
288 int nRemainderTilesX, int nRemainderTilesY,
289 const Size& rTileSizePixel, const GraphicAttr* pAttr,
290 ULONG nFlags, ImplTileInfo& rTileInfo );
292 bool ImplDrawTiled( OutputDevice* pOut, const Rectangle& rArea, const Size& rSizePixel,
293 const Size& rOffset, const GraphicAttr* pAttr, ULONG nFlags, int nTileCacheSize1D );
295 bool ImplDrawTiled( OutputDevice& rOut, const Point& rPos,
296 int nNumTilesX, int nNumTilesY,
297 const Size& rTileSize,
298 const GraphicAttr* pAttr, ULONG nFlags );
300 void ImplTransformBitmap( BitmapEx& rBmpEx,
301 const GraphicAttr& rAttr,
302 const Size& rCropLeftTop,
303 const Size& rCropRightBottom,
304 const Rectangle& rCropRect,
305 const Size& rDstSize,
306 BOOL bEnlarge ) const;
308 DECL_LINK( ImplAutoSwapOutHdl, void* );
310 //#endif // _SOLAR__PRIVATE
312 protected:
314 virtual void GraphicManagerDestroyed();
315 virtual SvStream* GetSwapStream() const;
317 // !!! to be removed
318 virtual ULONG GetReleaseFromCache() const;
320 virtual void Load( SvStream& );
321 virtual void Save( SvStream& );
322 virtual void Assign( const SvDataCopyStream& );
324 public:
326 TYPEINFO();
328 GraphicObject( const GraphicManager* pMgr = NULL );
329 GraphicObject( const Graphic& rGraphic, const GraphicManager* pMgr = NULL );
330 GraphicObject( const Graphic& rGraphic, const String& rLink, const GraphicManager* pMgr = NULL );
331 GraphicObject( const GraphicObject& rCacheObj, const GraphicManager* pMgr = NULL );
332 GraphicObject( const ByteString& rUniqueID, const GraphicManager* pMgr = NULL );
333 ~GraphicObject();
335 GraphicObject& operator=( const GraphicObject& rCacheObj );
336 BOOL operator==( const GraphicObject& rCacheObj ) const;
337 BOOL operator!=( const GraphicObject& rCacheObj ) const { return !( *this == rCacheObj ); }
339 BOOL HasSwapStreamHdl() const { return( mpSwapStreamHdl != NULL && mpSwapStreamHdl->IsSet() ); }
340 void SetSwapStreamHdl();
341 void SetSwapStreamHdl( const Link& rHdl, const ULONG nSwapOutTimeout = 0UL );
342 Link GetSwapStreamHdl() const;
343 ULONG GetSwapOutTimeout() const { return( mpSwapOutTimer ? mpSwapOutTimer->GetTimeout() : 0 ); }
345 void FireSwapInRequest();
346 void FireSwapOutRequest();
348 void SetGraphicManager( const GraphicManager& rMgr );
349 GraphicManager& GetGraphicManager() const { return *mpMgr; }
351 BOOL IsCached( OutputDevice* pOut, const Point& rPt, const Size& rSz,
352 const GraphicAttr* pAttr = NULL, ULONG nFlags = GRFMGR_DRAW_STANDARD) const;
353 void ReleaseFromCache();
355 const Graphic& GetGraphic() const;
356 void SetGraphic( const Graphic& rGraphic );
357 void SetGraphic( const Graphic& rGraphic, const String& rLink );
359 /** Get graphic transformed according to given attributes
361 This method returns a Graphic transformed, cropped and scaled
362 to the given parameters, ready to be rendered to printer or
363 display. The returned graphic has the same visual appearance
364 as if it had been drawn via GraphicObject::Draw() to a
365 specific output device.
367 @param rDestSize
368 Desired output size in logical coordinates. The mapmode to
369 interpret these logical coordinates in is given by the second
370 parameter, rDestMap.
372 @param rDestMap
373 Mapmode the output should be interpreted in. This is used to
374 interpret rDestSize, to set the appropriate PrefMapMode on the
375 returned Graphic, and to deal correctly with metafile graphics.
377 @param rAttr
378 Graphic attributes used to transform the graphic. This
379 includes cropping, rotation, mirroring, and various color
380 adjustment parameters.
382 @return the readily transformed Graphic
384 Graphic GetTransformedGraphic( const Size& rDestSize, const MapMode& rDestMap, const GraphicAttr& rAttr ) const;
385 Graphic GetTransformedGraphic( const GraphicAttr* pAttr = NULL ) const; // TODO: Change to Impl
387 void SetAttr( const GraphicAttr& rAttr );
388 const GraphicAttr& GetAttr() const { return maAttr; }
390 BOOL HasLink() const { return( mpLink != NULL && mpLink->Len() > 0 ); }
391 void SetLink();
392 void SetLink( const String& rLink );
393 String GetLink() const;
395 BOOL HasUserData() const { return( mpUserData != NULL && mpUserData->Len() > 0 ); }
396 void SetUserData();
397 void SetUserData( const String& rUserData );
398 String GetUserData() const;
400 ByteString GetUniqueID() const;
402 GraphicType GetType() const { return meType; }
403 const Size& GetPrefSize() const { return maPrefSize; }
404 const MapMode& GetPrefMapMode() const { return maPrefMapMode; }
405 ULONG GetSizeBytes() const { return mnSizeBytes; }
406 ULONG GetChecksum() const;
407 BOOL IsTransparent() const { return mbTransparent; }
408 BOOL IsAlpha() const { return mbAlpha; }
409 BOOL IsAnimated() const { return mbAnimated; }
410 BOOL IsEPS() const { return mbEPS; }
412 void ResetAnimationLoopCount();
413 List* GetAnimationInfoList() const;
414 Link GetAnimationNotifyHdl() const { return maGraphic.GetAnimationNotifyHdl(); }
415 void SetAnimationNotifyHdl( const Link& rLink );
417 BOOL SwapOut();
418 BOOL SwapOut( SvStream* pOStm );
419 BOOL SwapIn();
420 BOOL SwapIn( SvStream* pIStm );
422 BOOL IsInSwapIn() const { return mbIsInSwapIn; }
423 BOOL IsInSwapOut() const { return mbIsInSwapOut; }
424 BOOL IsInSwap() const { return( mbIsInSwapOut || mbIsInSwapOut ); }
425 BOOL IsSwappedOut() const { return( mbAutoSwapped || maGraphic.IsSwapOut() ); }
426 void SetSwapState();
428 BOOL Draw( OutputDevice* pOut, const Point& rPt, const Size& rSz,
429 const GraphicAttr* pAttr = NULL, ULONG nFlags = GRFMGR_DRAW_STANDARD );
431 /** Draw the graphic repeatedly into the given output rectangle
433 @param pOut
434 OutputDevice where the rendering should take place
436 @param rArea
437 The output area that is filled with tiled instances of this graphic
439 @param rSize
440 The actual size of a single tile
442 @param rOffset
443 Offset from the left, top position of rArea, where to start
444 the tiling. The upper left corner of the graphic tilings will
445 virtually start at this position. Concretely, only that many
446 tiles are drawn to completely fill the given output area.
448 @param pAttr
449 Optional GraphicAttr
451 @param nFlags
452 Optional rendering flags
454 @param nTileCacheSize1D
455 Optional dimension of the generated cache tiles. The pOut sees
456 a number of tile draws, which have approximately
457 nTileCacheSize1D times nTileCacheSize1D bitmap sizes if the
458 tile bitmap is smaller. Otherwise, the tile is drawn as
459 is. This is useful if e.g. you want only a few, very large
460 bitmap drawings appear on the outdev.
462 @return TRUE, if drawing completed successfully
464 BOOL DrawTiled( OutputDevice* pOut, const Rectangle& rArea, const Size& rSize,
465 const Size& rOffset, const GraphicAttr* pAttr = NULL,
466 ULONG nFlags = GRFMGR_DRAW_STANDARD, int nTileCacheSize1D=128 );
468 BOOL StartAnimation( OutputDevice* pOut, const Point& rPt, const Size& rSz, long nExtraData = 0L,
469 const GraphicAttr* pAttr = NULL, ULONG nFlags = GRFMGR_DRAW_STANDARD,
470 OutputDevice* pFirstFrameOutDev = NULL );
472 void StopAnimation( OutputDevice* pOut = NULL, long nExtraData = 0L );
474 friend SvStream& operator<<( SvStream& rOStm, const GraphicObject& rGraphicObj );
475 friend SvStream& operator>>( SvStream& rIStm, GraphicObject& rGraphicObj );
478 // ------------------
479 // - GraphicManager -
480 // ------------------
482 class GraphicManager
484 friend class GraphicObject;
485 friend class GraphicDisplayCacheEntry;
487 private:
489 List maObjList;
490 GraphicCache* mpCache;
492 GraphicManager( const GraphicManager& ) {}
493 GraphicManager& operator=( const GraphicManager& ) { return *this; }
495 //#if 0 // _SOLAR__PRIVATE
497 BOOL ImplDraw( OutputDevice* pOut, const Point& rPt,
498 const Size& rSz, GraphicObject& rObj,
499 const GraphicAttr& rAttr,
500 const ULONG nFlags, BOOL& rCached );
502 BOOL ImplCreateOutput( OutputDevice* pOut, const Point& rPt, const Size& rSz,
503 const BitmapEx& rBmpEx, const GraphicAttr& rAttr,
504 const ULONG nFlags, BitmapEx* pBmpEx = NULL );
505 BOOL ImplCreateOutput( OutputDevice* pOut,
506 const Point& rPt, const Size& rSz,
507 const GDIMetaFile& rMtf, const GraphicAttr& rAttr,
508 const ULONG nFlags, GDIMetaFile& rOutMtf, BitmapEx& rOutBmpEx );
510 BOOL ImplCreateScaled( const BitmapEx& rBmpEx,
511 long* pMapIX, long* pMapFX, long* pMapIY, long* pMapFY,
512 long nStartX, long nEndX, long nStartY, long nEndY,
513 BitmapEx& rOutBmpEx );
515 BOOL ImplCreateRotatedScaled( const BitmapEx& rBmpEx,
516 USHORT nRot10, const Size& rOutSzPix, const Size& rUntSzPix,
517 long* pMapIX, long* pMapFX, long* pMapIY, long* pMapFY,
518 long nStartX, long nEndX, long nStartY, long nEndY,
519 BitmapEx& rOutBmpEx );
521 static void ImplAdjust( BitmapEx& rBmpEx, const GraphicAttr& rAttr, ULONG nAdjustmentFlags );
522 static void ImplAdjust( GDIMetaFile& rMtf, const GraphicAttr& rAttr, ULONG nAdjustmentFlags );
523 static void ImplAdjust( Animation& rAnimation, const GraphicAttr& rAttr, ULONG nAdjustmentFlags );
525 static void ImplDraw( OutputDevice* pOut, const Point& rPt, const Size& rSz,
526 const GDIMetaFile& rMtf, const GraphicAttr& rAttr );
528 // Only used by GraphicObject's Ctor's and Dtor's
529 void ImplRegisterObj( const GraphicObject& rObj, Graphic& rSubstitute,
530 const ByteString* pID = NULL,
531 const GraphicObject* pCopyObj = NULL );
532 void ImplUnregisterObj( const GraphicObject& rObj );
533 inline BOOL ImplHasObjects() const { return( maObjList.Count() > 0UL ); }
535 // Only used in swap case by GraphicObject
536 void ImplGraphicObjectWasSwappedOut( const GraphicObject& rObj );
537 BOOL ImplFillSwappedGraphicObject( const GraphicObject& rObj, Graphic& rSubstitute );
538 void ImplGraphicObjectWasSwappedIn( const GraphicObject& rObj );
540 ByteString ImplGetUniqueID( const GraphicObject& rObj ) const;
542 //#endif // _SOLAR__PRIVATE
544 public:
546 GraphicManager( ULONG nCacheSize = 10000000UL, ULONG nMaxObjCacheSize = 2400000UL );
547 ~GraphicManager();
549 void SetMaxCacheSize( ULONG nNewCacheSize );
550 ULONG GetMaxCacheSize() const;
552 void SetMaxObjCacheSize( ULONG nNewMaxObjSize, BOOL bDestroyGreaterCached = FALSE );
553 ULONG GetMaxObjCacheSize() const;
555 ULONG GetUsedCacheSize() const;
556 ULONG GetFreeCacheSize() const;
558 void SetCacheTimeout( ULONG nTimeoutSeconds );
559 ULONG GetCacheTimeout() const;
561 void ClearCache();
563 void ReleaseFromCache( const GraphicObject& rObj );
565 BOOL IsInCache( OutputDevice* pOut, const Point& rPt, const Size& rSz,
566 const GraphicObject& rObj, const GraphicAttr& rAttr ) const;
568 BOOL DrawObj( OutputDevice* pOut, const Point& rPt, const Size& rSz,
569 GraphicObject& rObj, const GraphicAttr& rAttr,
570 const ULONG nFlags, BOOL& rCached );
573 #endif // _GRFMGR_HXX