2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Utility functions related to scalable icons.
21 #ifndef AKN_ICON_UTILS_H
22 #define AKN_ICON_UTILS_H
28 // FORWARD DECLARATIONS
30 class MAknIconObserver;
36 * Scales the icon to the maximum size that fits in the given size,
37 * whilst preserving the aspect ratio of the icon. The sizes of the resulting
38 * bitmaps are exactly of the given size. If the aspect ratio of
39 * the given size differs from the aspect ratio of the icon,
40 * the resulting bitmaps will contain an unused area.
42 EAspectRatioPreserved = 0,
45 * Scales the icon to the maximum size that fits in the given size,
46 * whilst preserving the aspect ratio of the icon. The resulting bitmaps
47 * are resized so that any unused portion of the given size is not
50 * This mode should be used when only the height or the width of the icon
51 * is known and the other should be based on the aspect ratio of the icon.
52 * The unknown dimension should be defined high enough (e.g. KMaxTInt) so
53 * that it does not limit the scaling based on the aspect ratio of the icon.
55 EAspectRatioPreservedAndUnusedSpaceRemoved = 1,
58 * Scales the icon exactly to the given size. Does not preserve the aspect
61 EAspectRatioNotPreserved = 2,
64 * Scales the icon to the minimum size that covers the given size,
65 * whilst preserving the aspect ratio of the icon. The sizes of the resulting
66 * bitmaps are exactly of the given size. If the aspect ratio of
67 * the given size differs from the aspect ratio of the icon, some parts of the
68 * icon will be sliced from the resulting bitmaps.
72 EAspectRatioPreservedSlice = 3
78 * Class for storing the content dimensions of icons.
80 class TAknContentDimensions
85 * C++ default constructor.
87 inline TAknContentDimensions();
92 * @param aWidth Icons width.
93 * @param aHeight Icons height.
95 inline TAknContentDimensions(TReal32 aWidth, TReal32 aHeight);
98 * Sets the content dimensions.
100 * @param aWidth Width of the icon.
101 * @param aHeight Height of the icon.
103 inline void SetDimensions(TReal32 aWidth, TReal32 aHeight);
106 * Sets the content size of the icon.
108 * @param aDimensions Two-dimensional size as a width and a height
111 inline void SetDimensions(const TSize& aDimensions);
123 * Cleanup stack helper. Owns the bitmap and the mask.
125 NONSHARABLE_CLASS(CAknIcon) : public CBase
130 * Two-phased constructor.
132 * Creates and returns a pointer to a new @c CAknIcon object.
134 * @return New @c CAknIcon.
136 IMPORT_C static CAknIcon* NewL();
148 * @return The icon's bitmap.
150 IMPORT_C CFbsBitmap* Bitmap() const;
155 * @return The icon's mask.
157 IMPORT_C CFbsBitmap* Mask() const;
162 * @param aBitmap The icon's bitmap.
164 IMPORT_C void SetBitmap( CFbsBitmap* aBitmap );
169 * @param aMask The icon's mask.
171 IMPORT_C void SetMask( CFbsBitmap* aMask );
176 * Default constructor.
182 CFbsBitmap* iBitmap; // owned
183 CFbsBitmap* iMask; // owned
187 * The purpose of this class is for clients to provide opened file handles
188 * to icon files, which reside in private directories,
189 * where AknIcon server has no access.
191 * MIF file is always generated, when building icons with MifConv tool.
192 * The corresponding MBM file is generated only if there are bitmap icons
193 * amongst the source icons.
196 class MAknIconFileProvider
211 virtual ~MAknIconFileProvider() {}
214 * Returns an open file handle to the icon file.
215 * This method should leave if an icon file with specified type does
216 * not exist. That may be the case e.g. with MBM file,
217 * if there are no bitmap icons.
219 * Note! RFs::ShareProtected must be called to the RFs instance used
220 * for opening the file.
222 * @param aFile Icon file should be opened in this file handle, which
223 * is an empty file handle, when the AknIcon framework calls this method.
224 * The AknIcon framework takes care of closing the file handle after
226 * @param aType Icon file type.
228 virtual void RetrieveIconFileHandleL(
229 RFile& aFile, const TIconFileType aType ) = 0;
232 * With this method, AknIcon framework informs that it does not use
233 * this MAknIconFileProvider instance any more. After this call,
234 * it is ok to delete the object. This can be implemented simply
235 * e.g. by deleting self in this callback.
236 * Normally, this callback is invoked when the icon in question
238 * Note, however, that if the same MAknIconFileProvider instance is
239 * supplied in multiple CreateIcon calls, then it must be accessible
240 * by AknIcon framework until it has signalled a matching amount
241 * of these callbacks.
243 virtual void Finished() = 0;
249 * Note: The CFbsBitmap objects for bitmaps and mask returned by this class
250 * may be compressed in background.
251 * Client code directly accessing the bitmap pixel data should not
252 * assume that the bitmap and mask objects are not compressed.
254 * Bitmap compression can be disabled for an icon if desired using
255 * AknIconUtils::DisableCompression().
262 * Creates bitmap and mask for an icon.
263 * Allocates bitmap and mask objects and sets aBitmap and
264 * aMask to point at them. Ownership of those is transferred to the caller.
265 * These bitmaps are not ready for drawing until they are initialized with
266 * SetSize method. Usually, UI controls do this.
268 * Use this method only if aBitmap and aMask are member variables.
269 * Otherwise, use method CreateIconLC.
272 * @param aBitmap icon bitmap is stored here
273 * @param aMask icon mask is stored here
274 * @param aFileName File name. Can be either MBM or MIF file.
275 * Extension is changed based on the given bitmap ID.
276 * @param aBitmapId bitmap Id
277 * @param aMaskId mask Id
279 IMPORT_C static void CreateIconL(
280 CFbsBitmap*& aBitmap,
282 const TDesC& aFileName,
287 * Creates bitmap and mask for an icon.
288 * Allocates bitmap and mask objects and sets aBitmap and
289 * aMask to point at them. Ownership of those is transferred to the caller.
290 * These bitmaps are not ready for drawing until they are initialized with
291 * SetSize method. Usually, UI controls do this.
293 * This method puts both aBitmap and aMask in the cleanup stack.
296 * @param aBitmap icon bitmap is stored here
297 * @param aMask icon mask is stored here
298 * @param aFileName File name. Can be either MBM or MIF file.
299 * Extension is changed based on the given bitmap ID.
300 * @param aBitmapId bitmap ID
301 * @param aMaskId mask ID
303 IMPORT_C static void CreateIconLC(
304 CFbsBitmap*& aBitmap,
306 const TDesC& aFileName,
311 * Creates the bitmap for an icon.
312 * Use this variant when a mask is not needed.
313 * Ownership of the returned object is transferred to the caller.
314 * The bitmap is not ready for drawing until it is initialized with
315 * SetSize method. Usually, UI controls do this.
318 * @param aBitmap icon bitmap is stored here
319 * @param aFileName File name. Can be either MBM or MIF file.
320 * Extension is changed based on the given bitmap ID.
321 * @param aBitmapId bitmap ID
323 IMPORT_C static CFbsBitmap* CreateIconL(
324 const TDesC& aFileName,
328 * Creates bitmap and mask for an icon.
329 * Allocates bitmap and mask objects and sets aBitmap and
330 * aMask to point at them. Ownership of those is transferred to the caller.
331 * These bitmaps are not ready for drawing until they are initialized with
332 * SetSize method. Usually, UI controls do this.
334 * If this method leaves, MAknIconFileProvider::Finished is called for the
335 * supplied aFileProvider.
337 * Use this method only if aBitmap and aMask are member variables.
338 * Otherwise, use method CreateIconLC.
341 * @param aBitmap icon bitmap is stored here
342 * @param aMask icon mask is stored here
343 * @param aFileProvider Icon file handle provider.
344 * @param aBitmapId bitmap Id
345 * @param aMaskId mask Id
347 IMPORT_C static void CreateIconL(
348 CFbsBitmap*& aBitmap,
350 MAknIconFileProvider& aFileProvider,
355 * Creates bitmap and mask for an icon.
356 * Allocates bitmap and mask objects and sets aBitmap and
357 * aMask to point at them. Ownership of those is transferred to the caller.
358 * These bitmaps are not ready for drawing until they are initialized with
359 * SetSize method. Usually, UI controls do this.
361 * This method puts both aBitmap and aMask in the cleanup stack.
363 * If this method leaves, MAknIconFileProvider::Finished is called for the
364 * supplied aFileProvider.
367 * @param aBitmap icon bitmap is stored here
368 * @param aMask icon mask is stored here
369 * @param aFileProvider Icon file handle provider.
370 * @param aBitmapId bitmap Id
371 * @param aMaskId mask Id
373 IMPORT_C static void CreateIconLC(
374 CFbsBitmap*& aBitmap,
376 MAknIconFileProvider& aFileProvider,
381 * Creates the bitmap for an icon.
382 * Use this variant when a mask is not needed.
383 * Ownership of the returned object is transferred to the caller.
384 * The bitmap is not ready for drawing until it is initialized with
385 * SetSize method. Usually, UI controls do this.
387 * If this method leaves, MAknIconFileProvider::Finished is called for the
388 * supplied aFileProvider.
391 * @param aFileProvider Icon file handle provider.
392 * @param aBitmapId bitmap ID
393 * @return The icon bitmap.
395 IMPORT_C static CFbsBitmap* CreateIconL(
396 MAknIconFileProvider& aFileProvider,
400 * Preserves the icon data (e.g. SVG-T data) related to the given icon
401 * in memory. After this, the icon data is destroyed when either
402 * DestroyIconData is explicitly called or the bitmap(s) of the icon are
405 * This method should be called to improve performance if more than one
406 * call to methods SetSize, SetSizeAndRotation or GetContentDimensions
407 * is made successively on a particular icon. It should be called prior
408 * to the above-mentioned calls. Without calling this method,
409 * the icon data is destroyed after any of the method calls mentioned
410 * above and then loaded from the storage medium and parsed from scratch
411 * again in a new operation.
413 * Note! Icon data may consume considerable amounts of memory. In order
414 * to save memory, any code that calls PreserveIconData should also
415 * explicitly call DestroyIconData when the icon data is no longer
419 * @param aBitmap bitmap or mask of the icon.
421 IMPORT_C static void PreserveIconData( CFbsBitmap* aBitmap );
424 * Destroys the icon data related to the given icon,
425 * if it was preserved in memory. Note that this does not affect
426 * the rendered frame buffers (CFbsBitmap objects).
429 * @param aBitmap bitmap or mask of the icon.
431 IMPORT_C static void DestroyIconData( CFbsBitmap* aBitmap );
434 * Initializes the icon to the given size, if the parameter aBitmap is
435 * an instance of CAknBitmap, i.e. created with AknIconUtils methods.
436 * If it is not CAknBitmap, this method does nothing. Note that this call
437 * sets the sizes of both bitmap and mask (if it exists), regardless of
438 * which is given as the parameter.
441 * @param aBitmap bitmap or mask of the icon
442 * @param aSize icon size
443 * @param aMode scale mode
444 * @return Standard Symbian OS error code. In error cases, it is guaranteed
445 * that the returned bitmap is a valid CFbsBitmap, but there is no guarantee
446 * of its size, except that it is non-negative.
448 IMPORT_C static TInt SetSize(
451 TScaleMode aMode = EAspectRatioPreserved );
454 * Initializes the icon to the given size, if the parameter aBitmap is
455 * an instance of CAknBitmap, i.e. created with AknIconUtils methods.
456 * If it is not CAknBitmap, this method does nothing. Note that this call
457 * sets the sizes of both bitmap and mask (if it exists), regardless of
458 * which is given as the parameter. Additionally, this method rotates
459 * the icon according to the given angle.
462 * @param aBitmap bitmap or mask of the icon
463 * @param aSize icon size
464 * @param aAngle Rotation angle in degrees.
465 * @return Standard Symbian OS error code. In error cases, it is guaranteed
466 * that the returned bitmap is a valid CFbsBitmap, but there is no guarantee
467 * of its size, except that it is non-negative.
469 IMPORT_C static TInt SetSizeAndRotation(
476 * Sets observer for change notification.
478 * The method BitmapChanged() will be called when the contents of the CFbsBitmap
479 * are changed. Controls can use this to redraw themselves when icon
480 * animation progresses.
483 * @param aBitmap bitmap
484 * @param aObserver observer
486 IMPORT_C static void SetObserver( CFbsBitmap* aBitmap, MAknIconObserver* aObserver );
489 * Returns the file name of Avkon's icon file, containing full path.
492 * @ret Avkon's icon file name, containing full path.
494 IMPORT_C static const TDesC& AvkonIconFileName();
497 * Validates logical app icon ID so that it can be used as a parameter to
498 * CreateIconL or CreateIconLC. If the given icon file name is .MBM file,
499 * this method does nothing. If it is .MIF file, a predefined offset is
500 * added to the given IDs.
502 * This method is only intended to be used when loading icons from the
503 * icon file retrieved from AppArc.
505 * @param aIconFileName Icon file name retrieved from AppArc.
506 * @param aId Logical bitmap ID used in the app icon file, usually 0.
507 * @param aId Logical mask ID used in the app icon file, usually 1.
509 IMPORT_C static void ValidateLogicalAppIconId(
510 const TDesC& aIconFileName,
515 * Tells whether the given file name is recognized as a MIF file or not.
516 * Only the file name extension is examined, not the contents of the file.
519 * @param aFileName file name
521 * @return ETrue if MIF file, EFalse otherwise.
523 IMPORT_C static TBool IsMifFile( const TDesC& aFileName );
526 * Tells whether the given bitmap is a part of a MIF icon or not.
527 * Accepts any pointer (also NULL) as a parameter.
530 * @param aBitmap bitmap
532 * @return ETrue if the given bitmap is part of a MIF icon,
535 IMPORT_C static TBool IsMifIcon( const CFbsBitmap* aBitmap );
538 * Returns the content dimensions of the given icon.
539 * In case of MBM icons, this is the original size of the bitmap.
540 * In case of SVG icons, this is defined in the icon data.
542 * If SetSize or SetSizeAndRotation is going to be called on the same
543 * icon after this call, the performance can be improved by calling
544 * PreserveIconData before calling this method.
548 * @param aBitmap bitmap
549 * @param aContentDimensions The content dimensions are returned here.
551 * @ret Standard Symbian OS error code.
553 IMPORT_C static TInt GetContentDimensions(
555 TSize& aContentDimensions );
558 * Returns the content dimensions of the given icon.
559 * In case of MBM icons, this is the original size of the bitmap.
560 * In case of SVG icons, this is defined in the icon data.
562 * If SetSize or SetSizeAndRotation is going to be called on the same
563 * icon after this call, the performance can be improved by calling
564 * PreserveIconData before calling this method.
567 * @param aBitmap bitmap
568 * @param aContentDimensions The content dimensions are returned here.
570 * @ret Standard Symbian OS error code.
572 IMPORT_C static TInt GetContentDimensions(
574 TAknContentDimensions& aContentDimensions );
578 * Creates bitmap and mask for an icon.
579 * Allocates bitmap and mask objects and sets aResultIcon to point at them.
581 * When this method returns, the resulting bitmaps are
582 * duplicates of the source bitmaps.
583 * Also the source bitmap instances are preserved in memory.
585 * Ownership of aSourceIcon is transferred from caller.
586 * This method takes the responsibility of deleting the object
587 * even if the method leaves.
589 * The returned icon bitmaps are instances of CAknBitmap,
590 * so AknIconUtils::SetSize is functional for them.
592 * AknIconUtils::SetSize first creates duplicates of the source bitmaps
593 * and after that, if required, creates new bitmaps and performs scaling.
595 * Note that due to the additional memory consumption caused by the source bitmaps,
596 * this method should only be used if it is not possible to use the variant of
597 * CreateIconL that takes icon file name and IDs as parameters.
601 * @param aSourceIcon Contains source bitmap and mask.
602 * Ownership of aSourceIcon is transferred from caller.
603 * This method takes the responsibility of deleting the object
604 * even if the method leaves.
606 * @ret Resulting icon. Ownership transferred to the caller.
608 IMPORT_C static CAknIcon* CreateIconL( CAknIcon* aSourceIcon );
611 * Non-masked variant of CreateIconL.
613 * Ownership of aSourceBitmap is transferred from caller.
614 * This method takes the responsibility of deleting the object
615 * even if the method leaves.
618 * @param aSourceBitmap Source bitmap.
619 * Ownership is transferred from caller.
620 * This method takes the responsibility of deleting the object
621 * even if the method leaves.
623 * @ret Resulting icon bitmap. Ownership transferred to the caller.
625 IMPORT_C static CFbsBitmap* CreateIconL( CFbsBitmap* aSourceBitmap );
628 * Determines the icon color. Only effective if the given bitmap is
629 * an instance of CAknBitmap. This method only needs to be called either
630 * for the bitmap or the mask of an icon, but to be effective, it needs
631 * to be called before calling SetSize or SetSizeAndRotation.
634 * @param aBitmap bitmap
635 * @param aColor icon color
637 IMPORT_C static void SetIconColor( CFbsBitmap* aBitmap, const TRgb aColor );
641 * Excludes the icon form the icon cache.
642 * Only effective if the given bitmap is an instance of CAknBitmap. This call
643 * sets the sizes of both bitmap and mask (if it exists), regardless of
644 * which is given as the parameter.
645 * This method should be called after calling CreateIconL or CreateIconLC
646 * and before calling AknIconUtils::SetSize.
648 * By default icons are being put to icon cache after they are no longer used.
649 * This makes it possible to retrieve recently used icons fast without the need to
651 * The icon cache has a limited size and when the cache is full to cache new icons
652 * the oldest icons from the cache will be removed.
653 * In certain situations it might be feasible to exclude some icons from the
654 * icon cache (e.g. in case of infrequently used large icons) to prevent some more
655 * frequently used icon being kicked out from the cache. Excluding infrequently used
656 * icons from icon cache could improve performance and memory usage of the system.
660 * @param aBitmap bitmap
662 IMPORT_C static void ExcludeFromCache( CFbsBitmap* aBitmap );
665 * Disables bitmap compression for the icon.
666 * Only effective if the given bitmap is an instance of CAknBitmap. This call
667 * prevents AknIcon framework from compressing the CFbsBitmap objects
668 * (bitmap, mask) of the icon.
669 * This method should be called after calling CreateIconL or CreateIconLC
670 * and before calling AknIconUtils::SetSize.
673 * @param aBitmap bitmap
675 IMPORT_C static void DisableCompression( CFbsBitmap* aBitmap );
679 * Performs bitmap scaling.
680 * Draws a source bitmap to fit a given rectangle within a target bitmap.
681 * This is ~15x faster than scaling with Symbian's DrawBitmap. Following
682 * bitmap modes are supported: EGray256, EColor4K, EColor64K, EColor256,
683 * EColor16MU. For all other bitmap modes, Symbian's DrawBitmap API will
684 * be used. Bitmap modes of the source and target bitmap should be the
687 * @ param aTrgRect target rect inside the target bitmap
688 * @ param aTrgBitmap the target bitmap
689 * @ param aSrcBitmap the source bitmap
690 * @leave KErrArgument
691 * If source bitmap mode is not the same as target bitmap mode.
692 * @leave Any other Symbian OS specific error codes.
694 IMPORT_C static void ScaleBitmapL(
695 const TRect& aTrgRect,
696 CFbsBitmap* aTrgBitmap,
697 CFbsBitmap* aSrcBitmap );
700 * Bitmap rotation and scaling. Might be exported later. Scales and
701 * rotates the given bitmap according to the parameters. Supported
702 * bitmap modes are EGray2, EGray256, EColor256, EColor4K, EColor64K and
703 * EColor16MU. All other bitmap depts will cause a leave with
704 * KErrNotSupported. The only supported scalemode is
705 * EAspectRatioPreserved. The unused area in the target bitmap is
706 * filled with black color unless the bitmap depth is EGray8 when the
707 * area is filled with white. Areas that do not fit to the target area
708 * after rotation are clipped out
710 * If the bitmap depth is EGray2, the routine will perform noticeably worse.
712 * @param aTrgRect target rect inside the target bitmap
713 * @param aTrgBitmap the target bitmap
714 * @param aSrcBitmap the source bitmap
715 * @param aAngle the rotation angle in degrees
717 static void RotateAndScaleBitmapL(
718 const TRect& aTrgRect,
719 CFbsBitmap* aTrgBitmap,
720 CFbsBitmap* aSrcBitmap,
726 static TBool DoesScaleBitmapUseFallBack(
727 CFbsBitmap* aSrcBitmap );
732 static void ScaleBitmapExtL(
733 const TRect& aTrgRect,
734 CFbsBitmap* aTrgBitmap,
735 CFbsBitmap* aSrcBitmap,
736 TBool aForceFallBack );
741 * Utility for down-cast of CFbsBitmap objects. It informs whether
742 * the given CFbsBitmap is CAknBitmap or not. A list of CAknBitmap
743 * pointers is managed in TLS for this.
745 * @param aBitmap bitmap
746 * @ret ETrue iff the given CFbsBitmap is an CAknBitmap instance.
748 static TBool IsAknBitmap( const CFbsBitmap* aBitmap );
753 static void CreateIconLC(
754 CFbsBitmap*& aBitmap,
756 const TDesC& aFileName,
759 MAknIconFileProvider* aFileProvider );
765 static void CreateIconLC(
766 CFbsBitmap*& aBitmap,
768 const TDesC& aFileName,
778 #include "AknIconUtils.inl"
779 #endif // AKN_ICON_UTILS_H