uiacceltk/hitchcock/coretoolkit/src/HuiImageLoaderUtil.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Implements CHuiImageLoaderUtil, a utility to load skin-able image/icon.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "uiacceltk/HuiImageLoaderUtil.h"
       
    21 #include "uiacceltk/HuiBitmapProvider.h"
       
    22 #include "uiacceltk/HuiTextureManager.h"
       
    23 #include "uiacceltk/HuiStatic.h"
       
    24 #include "uiacceltk/HuiEnv.h"
       
    25 
       
    26 struct THuiProvidedBitmap
       
    27     {
       
    28     TInt iId;
       
    29     THuiRealSize iPreferredSize;        
       
    30     };
       
    31 
       
    32 /**
       
    33  * Base class of the image loder classes
       
    34  */
       
    35 NONSHARABLE_CLASS(CHuiImageLoader): public CBase, public MHuiBitmapProvider, public MHuiTextureAutoSizeObserver
       
    36 	{
       
    37 public:
       
    38 	CHuiImageLoader();
       
    39 
       
    40     ~CHuiImageLoader();
       
    41     /**
       
    42      * Set size of the icon. This must be called before creating texture.
       
    43      * This call only sets information and dees *NOT* cause any scalable icon rasterization.
       
    44      *
       
    45      * @param aSize     Size in pixel.
       
    46      */
       
    47 	void SetSize(const TSize& aSize) {iSize = aSize;}
       
    48 
       
    49 	void SetScaleMode(TScaleMode aScaleMode) {iScaleMode = aScaleMode;}
       
    50 
       
    51     /**
       
    52      * Set skin instance, which is acquired from AknsUtils::SkinInstance().
       
    53      *
       
    54      * @param aSkinInstance     An Avkon skin instamce.
       
    55      */
       
    56 	void SetSkinInstance(MAknsSkinInstance *aSkinInstance) {iSkinInstance = aSkinInstance;}
       
    57 
       
    58 public:
       
    59     // From MHuiBitmapProvider
       
    60 	virtual void ProvideBitmapL (TInt aId, CFbsBitmap *& aBitmap, CFbsBitmap *& aMaskBitmap) ;
       
    61 
       
    62     // From MHuiTextureAutoSizeObserver
       
    63     virtual TBool PreferredSizeChanged(const CHuiTexture& aTexture, TInt aTextureId, const THuiRealSize& aPreferredSize);
       
    64 
       
    65     // From MHuiTextureAutoSizeObserver
       
    66     virtual void PreferredSizeReportCompleted();
       
    67 
       
    68 protected:
       
    69 	MAknsSkinInstance* GetValidSkinInstance();
       
    70     TSize PreferredSize(TInt aId);
       
    71 
       
    72 protected:
       
    73 	// common source info data
       
    74 	MAknsSkinInstance *iSkinInstance;
       
    75 	TSize iSize;
       
    76 	TScaleMode iScaleMode;
       
    77 	RArray<THuiProvidedBitmap> iProvidedBitmaps;
       
    78 	};
       
    79 
       
    80 
       
    81 /**
       
    82  * Image loader utility for skin item image loading.
       
    83  *
       
    84  */
       
    85 NONSHARABLE_CLASS(CHuiSkinItemImageLoader): public CHuiImageLoader
       
    86 {
       
    87 public:
       
    88     /**
       
    89      * Construct an icon source information with Avkon skin item ID. AknsUtils::CreateIconL()
       
    90      * will be called in RHuiImageLoaderUtil::CreateIconL().
       
    91      *
       
    92      * @param aInstance     Skin instance.
       
    93      * @param aID  An ID of the graphical item (e.g. an icon).
       
    94      * @param aFileName  File name of graphics file e.g. mif file) when sepcified item is not found in the current skin.
       
    95      * @param aBitmapId  ID of the bitmap when fallback is used.
       
    96      * @param aMaskId  ID of the mask bitmap when fallback is used.
       
    97      */
       
    98 	CHuiSkinItemImageLoader(const TAknsItemID &aID, const TDesC &aFileName, TInt aBitmapId, TInt aMaskId);
       
    99 
       
   100     /**
       
   101      * Implementation of MHuiBitmapProvider interface.
       
   102      *
       
   103      * @param aId     A unique ID of the texture needed to provide bitmaps.
       
   104      * @param aBitmap     A bitmap.
       
   105      * @param aMaskBitmap     A mask bitmap.
       
   106      */
       
   107 	virtual void ProvideBitmapL (TInt aId, CFbsBitmap *& aBitmap, CFbsBitmap *& aMaskBitmap) ;
       
   108 
       
   109 private:
       
   110 	TAknsItemID iId;
       
   111 	TPtrC iFileName;
       
   112 	TInt iBitmapId;
       
   113 	TInt iMaskId;
       
   114 };
       
   115 
       
   116 
       
   117 /**
       
   118  * Image loader utility for application icon loading.
       
   119  *
       
   120  */
       
   121 NONSHARABLE_CLASS(CHuiAppIconImageLoader): public CHuiImageLoader
       
   122 {
       
   123 public:
       
   124     /**
       
   125      * Construct an icon source information to create application icon. AknsUtils::CreateAppIconLC()
       
   126      * will be called in RHuiImageLoaderUtil::CreateIconL().
       
   127      *
       
   128      * @param aInstance     Skin instance.
       
   129      * @param aAppUid  Application UID of the icon to be created.
       
   130      * @param aType  Type of the application icon. Likely EAknsAppIconTypeContext is used...
       
   131      */
       
   132 	CHuiAppIconImageLoader(TUid aAppUid, TAknsAppIconType aType);
       
   133     
       
   134     /**
       
   135      * Implementation of MHuiBitmapProvider interface.
       
   136      *
       
   137      * @param aId     A unique ID of the texture needed to provide bitmaps.
       
   138      * @param aBitmap     A bitmap.
       
   139      * @param aMaskBitmap     A mask bitmap.
       
   140      */
       
   141 	virtual void ProvideBitmapL (TInt aId, CFbsBitmap *& aBitmap, CFbsBitmap *& aMaskBitmap) ;
       
   142 private:
       
   143 	TUid iAppUid;
       
   144 	TAknsAppIconType iType;
       
   145 };
       
   146 
       
   147 // Image loader util
       
   148 EXPORT_C CHuiImageLoaderUtil::CHuiImageLoaderUtil()
       
   149 		:iSize(4,4), iScaleMode(EAspectRatioPreserved)
       
   150 	{
       
   151 	}
       
   152 
       
   153 
       
   154 EXPORT_C CHuiImageLoaderUtil::~CHuiImageLoaderUtil()
       
   155 	{
       
   156 	for (TInt i=0; i<iImageLoaderList.Count(); i++)
       
   157 	    {
       
   158 	    delete iImageLoaderList[i];
       
   159 	    }
       
   160 	iImageLoaderList.Close();
       
   161 	iSkinInstance = NULL;
       
   162 	}
       
   163 
       
   164 
       
   165 EXPORT_C MHuiBitmapProvider* CHuiImageLoaderUtil::CreateImageLoaderL(
       
   166 				const TAknsItemID &aID, 
       
   167 				const TDesC &aFileName, 
       
   168 				TInt aBitmapId, 
       
   169 				TInt aMaskId)
       
   170 	{
       
   171 	CHuiSkinItemImageLoader* imgLoader = new (ELeave) CHuiSkinItemImageLoader(
       
   172 			aID, aFileName, aBitmapId, aMaskId);
       
   173     CleanupStack::PushL( imgLoader );
       
   174 	imgLoader->SetSize(iSize);
       
   175 	imgLoader->SetScaleMode(iScaleMode);
       
   176 	imgLoader->SetSkinInstance(iSkinInstance);
       
   177 	iImageLoaderList.AppendL(imgLoader);
       
   178 	CleanupStack::Pop( imgLoader );
       
   179 	return imgLoader;
       
   180 	}
       
   181 
       
   182 
       
   183 EXPORT_C MHuiBitmapProvider* CHuiImageLoaderUtil::CreateImageLoaderL(
       
   184 				TUid aAppUid, 
       
   185 				TAknsAppIconType aType)
       
   186 	{
       
   187 	CHuiAppIconImageLoader* imgLoader = new (ELeave) CHuiAppIconImageLoader(
       
   188 			aAppUid, aType);
       
   189     CleanupStack::PushL( imgLoader );
       
   190 	imgLoader->SetSize(iSize);
       
   191 	imgLoader->SetScaleMode(iScaleMode);
       
   192 	imgLoader->SetSkinInstance(iSkinInstance);
       
   193 	iImageLoaderList.AppendL(imgLoader);
       
   194 	CleanupStack::Pop( imgLoader );
       
   195 	return imgLoader;
       
   196 	}
       
   197 
       
   198 
       
   199 EXPORT_C void CHuiImageLoaderUtil::SetSize(TSize aSize, TScaleMode aMode)
       
   200 	{
       
   201 	iSize = aSize;
       
   202 	iScaleMode = aMode;
       
   203 	}
       
   204 
       
   205 
       
   206 EXPORT_C void CHuiImageLoaderUtil::SetSkinInstance(MAknsSkinInstance *aSkinInstance)
       
   207 	{
       
   208 	iSkinInstance = aSkinInstance;
       
   209 	}
       
   210 
       
   211 
       
   212 
       
   213 // Image loaders
       
   214 MAknsSkinInstance* CHuiImageLoader::GetValidSkinInstance()
       
   215 	{
       
   216 	if (iSkinInstance == NULL && CCoeEnv::Static())
       
   217 		{
       
   218 		return AknsUtils::SkinInstance();
       
   219 		}
       
   220 	else
       
   221 		{
       
   222 		return iSkinInstance;
       
   223 		}
       
   224 	}
       
   225 
       
   226 TBool CHuiImageLoader::PreferredSizeChanged(const CHuiTexture& /*aTexture*/, TInt aTextureId, const THuiRealSize& aPreferredSize)    {
       
   227     for(TInt ii = 0; ii < iProvidedBitmaps.Count(); ii++)
       
   228         {
       
   229         if (iProvidedBitmaps[ii].iId == aTextureId)
       
   230             {
       
   231             iProvidedBitmaps[ii].iPreferredSize = aPreferredSize;     
       
   232             return ETrue;
       
   233             }
       
   234         }
       
   235     return EFalse;            
       
   236     }
       
   237 
       
   238 void CHuiImageLoader::PreferredSizeReportCompleted()
       
   239     {
       
   240     // Nothing to do    
       
   241     }
       
   242 
       
   243 void CHuiImageLoader::ProvideBitmapL (TInt aId, CFbsBitmap *& /*aBitmap*/, CFbsBitmap *& /*aMaskBitmap*/)
       
   244     {
       
   245     for(TInt i = 0; i < iProvidedBitmaps.Count(); i++)
       
   246         {
       
   247         if (iProvidedBitmaps[i].iId == aId)
       
   248             {            
       
   249             // Already in list, do nothing
       
   250             return;   
       
   251             }
       
   252         }
       
   253     
       
   254     // If provided bitmap is not yet in the list, add it.    
       
   255     THuiProvidedBitmap providedBitmap;
       
   256     providedBitmap.iId = aId;
       
   257     providedBitmap.iPreferredSize = iSize;        
       
   258     iProvidedBitmaps.AppendL(providedBitmap);            
       
   259     }
       
   260 
       
   261 CHuiImageLoader::CHuiImageLoader():iSize(4,4), iScaleMode(EAspectRatioPreserved)
       
   262     {
       
   263     }
       
   264 
       
   265 CHuiImageLoader::~CHuiImageLoader()
       
   266     {
       
   267     iProvidedBitmaps.Close();
       
   268     };
       
   269 
       
   270 TSize CHuiImageLoader::PreferredSize(TInt aId)
       
   271     {
       
   272     for(TInt i = 0; i < iProvidedBitmaps.Count(); i++)
       
   273         {
       
   274         if (iProvidedBitmaps[i].iId == aId)
       
   275             {
       
   276             // Already in list, do nothing
       
   277             return iProvidedBitmaps[i].iPreferredSize;   
       
   278             }
       
   279         }
       
   280     return iSize;    
       
   281     }
       
   282 
       
   283 
       
   284 CHuiSkinItemImageLoader::CHuiSkinItemImageLoader(
       
   285 		const TAknsItemID &aID, 
       
   286 		const TDesC &aFileName, 
       
   287 		TInt aBitmapId, 
       
   288 		TInt aMaskId):
       
   289 		iId(aID),
       
   290 		iBitmapId(aBitmapId),
       
   291 		iMaskId(aMaskId)
       
   292 	{
       
   293 	iFileName.Set(aFileName);
       
   294 	}
       
   295 
       
   296 
       
   297 void CHuiSkinItemImageLoader::ProvideBitmapL (TInt aId, CFbsBitmap *& aBitmap, CFbsBitmap *& aMaskBitmap)
       
   298 	{
       
   299 	CHuiImageLoader::ProvideBitmapL( aId, aBitmap, aMaskBitmap);
       
   300 	MAknsSkinInstance* skininstance = GetValidSkinInstance();
       
   301 	
       
   302 	if (skininstance)
       
   303 	    {
       
   304 	    TRAPD(err, AknsUtils::CreateIconL(
       
   305 					skininstance, iId,
       
   306 					aBitmap, aMaskBitmap, 
       
   307 					iFileName, iBitmapId, iMaskId););
       
   308 	    if (err == KErrNotFound)
       
   309 	        {
       
   310 	        AknsUtils::CreateIconL(skininstance,
       
   311 	                iId,
       
   312 	                aBitmap,
       
   313 	                iFileName,
       
   314 	                iBitmapId );
       
   315 	        }
       
   316 	    else
       
   317 	        {
       
   318 	        User::LeaveIfError(err);
       
   319 	        }
       
   320 	    }
       
   321 	else // proxy from other process 
       
   322 	    {
       
   323 	    CHuiStatic::GetMaskedBitmapL(iId,
       
   324                 aBitmap, aMaskBitmap, iFileName, iBitmapId, iMaskId, PreferredSize(aId), iScaleMode );
       
   325 	    return;
       
   326 	    }
       
   327 	    
       
   328 
       
   329     // Leave if bitmap or mask is NULL.
       
   330     User::LeaveIfNull( aBitmap );
       
   331     //User::LeaveIfNull( aMaskBitmap );
       
   332 
       
   333 	// Disable compression so that the bitmap may be able to be duplicated inside
       
   334 	// HUITK when BITGDI renderer is in use.
       
   335 #ifndef __SERIES60_30__
       
   336 	AknIconUtils::DisableCompression( aBitmap );
       
   337 #else
       
   338 #pragma message ("S60R30: FBS Compression can't be disabled, graphics may look crappy, deal with it")
       
   339 #endif
       
   340 
       
   341     // Set icon sizes and render them on the bitmaps.
       
   342     // This does the actual rendering to specified size. (SVG -> bitmap)
       
   343     AknIconUtils::SetSize( aBitmap, PreferredSize(aId), iScaleMode );
       
   344 	}
       
   345 
       
   346 
       
   347 CHuiAppIconImageLoader::CHuiAppIconImageLoader(
       
   348 		TUid aAppUid, 
       
   349 		TAknsAppIconType aType):
       
   350 		iAppUid(aAppUid),
       
   351 		iType(aType)
       
   352 	{
       
   353 	}
       
   354 
       
   355 
       
   356 void CHuiAppIconImageLoader::ProvideBitmapL (
       
   357 		TInt aId, 
       
   358 		CFbsBitmap *& aBitmap, 
       
   359 		CFbsBitmap *& aMaskBitmap)
       
   360 	{
       
   361 	CHuiImageLoader::ProvideBitmapL( aId, aBitmap, aMaskBitmap);
       
   362     MAknsSkinInstance* skin = GetValidSkinInstance();
       
   363     if (skin)
       
   364         {
       
   365         AknsUtils::CreateAppIconLC( 
       
   366 					skin, 
       
   367 					iAppUid, iType,
       
   368 					aBitmap, aMaskBitmap );
       
   369         }
       
   370     // Leave if bitmap or mask is NULL.
       
   371     User::LeaveIfNull( aBitmap );
       
   372     //User::LeaveIfNull( aMaskBitmap );
       
   373 
       
   374 	// diable compression so that the bitmap may be able to be duplicated inside
       
   375 	// HUITK when BITGDI renderer is in use.
       
   376 #ifndef __SERIES60_30__
       
   377 	AknIconUtils::DisableCompression( aBitmap );
       
   378 #else
       
   379 #pragma message ("S60R30: FBS Compression can't be disabled, graphics may look crappy, deal with it")
       
   380 #endif
       
   381 
       
   382     // Set icon sizes and render them on the bitmaps.
       
   383     // This does the actual rendering to specified size. (SVG -> bitmap)
       
   384     AknIconUtils::SetSize( aBitmap, PreferredSize(aId), iScaleMode );
       
   385     
       
   386 	// pop the passed bitmap and mask 
       
   387 	CleanupStack::Pop(2);
       
   388 	}
       
   389 
       
   390 EXPORT_C void CHuiImageLoaderUtil::ImageLoaderExtension(const TUid& /*aExtensionUid*/, TAny** /*aExtensionParamters*/ )
       
   391     {
       
   392     
       
   393     }
       
   394 
       
   395 EXPORT_C TBool CHuiAutoSizeImageLoaderUtil::PreferredSizeChanged(const CHuiTexture& aTexture, TInt aTextureId, const THuiRealSize& aPreferredSize)
       
   396     {
       
   397     TBool ret = EFalse;
       
   398     TInt count = iImageLoaderList.Count();
       
   399     for (TInt i=0; i<count;i++)
       
   400         {
       
   401         ret |= iImageLoaderList[i]->PreferredSizeChanged(aTexture, aTextureId, aPreferredSize);    
       
   402         }
       
   403     return ret;    
       
   404         
       
   405     }
       
   406     
       
   407 EXPORT_C void CHuiAutoSizeImageLoaderUtil::PreferredSizeReportCompleted()
       
   408     {
       
   409     // Do nothing    
       
   410     }