idlehomescreen/widgetmanager/src/wmimageconverter.cpp
branchRCL_3
changeset 93 b01126ce0bec
parent 83 5456b4e8b3a8
child 102 ba63c83f4716
--- a/idlehomescreen/widgetmanager/src/wmimageconverter.cpp	Tue Sep 14 20:58:58 2010 +0300
+++ b/idlehomescreen/widgetmanager/src/wmimageconverter.cpp	Wed Sep 15 12:00:00 2010 +0300
@@ -101,13 +101,14 @@
                             const TSize& aIconSize, 
                             const TDesC& aIconStr,
                             CFbsBitmap*& aBitmap,
-                            CFbsBitmap*& aMask )
+                            CFbsBitmap*& aMask,
+                            TBool aForceScale )
     {
     delete aBitmap; aBitmap = NULL;
     delete aMask; aMask = NULL;
 
     TInt err( KErrNone );
-    TRAP( err, HandleIconStringL( aIconSize, aIconStr ); );
+    TRAP( err, HandleIconStringL( aIconSize, aIconStr, aForceScale ); );
     if ( err == KErrNone && iBitmap && iMask )
         {
         // ownership transferred
@@ -139,7 +140,8 @@
 //
 void CWmImageConverter::HandleIconStringL( 
                             const TSize& aIconSize, 
-                            const TDesC& aIconStr )
+                            const TDesC& aIconStr,
+                            TBool aForceScale )
     {
     if ( aIconStr.Length() )
         {
@@ -160,7 +162,7 @@
             }
         else if ( ResolveUid( aIconStr, appUid ) )
             {
-            CreateIconFromUidL( appUid );
+            CreateIconFromUidL( appUid, aForceScale );
             }
         else if ( EndsWith( aIconStr, KSvgExt ) )
             {
@@ -170,7 +172,7 @@
         else if ( BaflUtils::FileExists( iFs, aIconStr ) )
             {
             // filename_with_full_path.png/jpg
-            CreateIconFromOtherL( aIconStr );
+            CreateIconFromOtherL( aIconStr, aForceScale );
             }
         else
             {
@@ -187,7 +189,8 @@
 // CWmImageConverter::CreateIconFromUidL
 // ---------------------------------------------------------
 //
-void CWmImageConverter::CreateIconFromUidL( const TUid& aUid )
+void CWmImageConverter::CreateIconFromUidL( 
+        const TUid& aUid, TBool aForceScale )
     {
     CFbsBitmap* bitmap = NULL;
     CFbsBitmap* mask = NULL;
@@ -220,21 +223,54 @@
 
         CApaMaskedBitmap* maskedBmp = CApaMaskedBitmap::NewLC();
         User::LeaveIfError( lsSession.GetAppIcon( aUid, size, *maskedBmp ) );
-        
+
         // handle bitmap
-        iBitmap = new ( ELeave ) CFbsBitmap;       
-        CopyBitmapL( *iBitmap, *maskedBmp );
+        iBitmap = new ( ELeave ) CFbsBitmap;
+        if ( aForceScale )
+            {
+            // copy and scale
+            TRect scaledRect = TRect( iSize );
+            iBitmap->Create( scaledRect.Size(), maskedBmp->DisplayMode() );
+            CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL( iBitmap );
+            CleanupStack::PushL( bitmapDevice );
+            CFbsBitGc* bitmapGc = CFbsBitGc::NewL();
+            CleanupStack::PushL( bitmapGc );
+            bitmapGc->Activate( bitmapDevice );
+            bitmapGc->DrawBitmap( scaledRect, maskedBmp );
+            CleanupStack::PopAndDestroy( bitmapGc );
+            CleanupStack::PopAndDestroy( bitmapDevice );
+            }
+        else
+            {
+            CopyBitmapL( *iBitmap, *maskedBmp );
+            }
         
         // handle mask
         if ( maskedBmp->Mask() )
             {
             iMask = new ( ELeave ) CFbsBitmap;
-            CopyBitmapL( *iMask, *maskedBmp->Mask() );
+            if ( aForceScale )
+                {
+                TRect scaledRect = TRect( iSize );
+                iMask->Create( scaledRect.Size(), maskedBmp->Mask()->DisplayMode() );
+                CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL( iMask );
+                CleanupStack::PushL( bitmapDevice );
+                CFbsBitGc* bitmapGc = CFbsBitGc::NewL();
+                CleanupStack::PushL( bitmapGc );
+                bitmapGc->Activate( bitmapDevice );
+                bitmapGc->DrawBitmap( scaledRect, maskedBmp->Mask() );
+                CleanupStack::PopAndDestroy( bitmapGc );
+                CleanupStack::PopAndDestroy( bitmapDevice );
+                }
+            else
+                {
+                CopyBitmapL( *iMask, *maskedBmp->Mask() );
+                }
             }
         
         // cleanup
         CleanupStack::PopAndDestroy( maskedBmp );
-        CleanupStack::PopAndDestroy( sizeArray ); 
+        CleanupStack::PopAndDestroy( sizeArray );
         CleanupStack::PopAndDestroy( &lsSession );
         }
     else if ( aUid.iUid != KNullUid.iUid )
@@ -339,7 +375,8 @@
 // CWmImageConverter::CreateIconFromOtherL
 // ---------------------------------------------------------
 //
-void CWmImageConverter::CreateIconFromOtherL( const TDesC& aFileName )
+void CWmImageConverter::CreateIconFromOtherL( 
+        const TDesC& aFileName, TBool aForceScale )
     {
     if (iBitmap) {delete iBitmap; iBitmap = NULL;}
     if (iMask) {delete iMask; iMask = NULL;}
@@ -372,6 +409,47 @@
         }
     User::LeaveIfError( status.Int() );    
     CleanupStack::PopAndDestroy( imageDecoder );
+    
+    // do scaling
+    if ( aForceScale )
+        {
+        // scale bitmap
+        TRect scaledRect = TRect( iSize );
+        CFbsBitmap* scaledBitmap = new (ELeave) CFbsBitmap();
+        CleanupStack::PushL( scaledBitmap );
+        User::LeaveIfError( 
+                scaledBitmap->Create( scaledRect.Size(), iBitmap->DisplayMode() ) );
+        CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL( scaledBitmap );
+        CleanupStack::PushL( bitmapDevice );
+        CFbsBitGc* bitmapGc = CFbsBitGc::NewL();
+        CleanupStack::PushL( bitmapGc );
+        bitmapGc->Activate( bitmapDevice );
+        bitmapGc->DrawBitmap( scaledRect, iBitmap );
+        CleanupStack::PopAndDestroy( bitmapGc );
+        CleanupStack::PopAndDestroy( bitmapDevice );
+        CleanupStack::Pop( scaledBitmap );
+        // take ownership of scaled bitmap
+        delete iBitmap; iBitmap = NULL;
+        iBitmap = scaledBitmap; scaledBitmap = NULL;
+        
+        // scale mask
+        CFbsBitmap* scaledMask = new (ELeave) CFbsBitmap();
+        CleanupStack::PushL( scaledMask );
+        User::LeaveIfError( 
+                scaledMask->Create( scaledRect.Size(), iMask->DisplayMode() ) );
+        bitmapDevice = CFbsBitmapDevice::NewL( scaledMask );
+        CleanupStack::PushL( bitmapDevice );
+        bitmapGc = CFbsBitGc::NewL();
+        CleanupStack::PushL( bitmapGc );
+        bitmapGc->Activate( bitmapDevice );
+        bitmapGc->DrawBitmap( scaledRect, iMask );
+        CleanupStack::PopAndDestroy( bitmapGc );
+        CleanupStack::PopAndDestroy( bitmapDevice );
+        CleanupStack::Pop( scaledMask );
+        // take ownership of scaled bitmap
+        delete iMask; iMask = NULL;
+        iMask = scaledMask; scaledMask = NULL;        
+        }    
     }
 
 // ---------------------------------------------------------------------------
@@ -508,18 +586,18 @@
 // ---------------------------------------------------------------------------
 //
 TBool CWmImageConverter::ResolveUid( 
-                const TDesC& aPath, TUid& aUid )
+                const TDesC& aStr, TUid& aUid )
     {
     // Syntax: uid(0x12345678)
     TInt error = KErrNotFound;
-    TInt pos = aPath.FindF( KUid );
+    TInt pos = aStr.FindF( KUid );
     if( pos == 0 )
         {
         // Skip uid token
         pos += KUid().Length();
 
         // Initialize lexer
-        TLex lex( aPath.Mid( pos ) );
+        TLex lex( aStr.Mid( pos ) );
         lex.SkipSpaceAndMark();
         
         // Check left parenthesis
@@ -537,18 +615,19 @@
 // ---------------------------------------------------------------------------
 //
 TBool CWmImageConverter::ResolveSkinId( 
-                const TDesC& aPath, TAknsItemID& aItemId )
+                const TDesC& aStr, TAknsItemID& aItemId )
     {
     // Syntax: skin(major minor)
+    aItemId = KAknsIIDNone;
     TInt error = KErrNotFound;
-    TInt pos = aPath.FindF( KSkin );
+    TInt pos = aStr.FindF( KSkin );
     if( pos == 0 )
         {
         // Skip skin token
         pos += KSkin().Length();
 
         // Initialize lexer
-        TLex lex( aPath.Mid( pos ) );
+        TLex lex( aStr.Mid( pos ) );
         lex.SkipSpaceAndMark();
         
         // Check left parenthesis
@@ -570,18 +649,22 @@
 // ---------------------------------------------------------------------------
 //
 TBool CWmImageConverter::ResolveMifId( 
-                const TDesC& aPath, TInt& aBitmapId,
+                const TDesC& aStr, TInt& aBitmapId,
                 TInt& aMaskId, TDes& aFileName )
     {
     // Syntax: mif(filename bimapId maskId)
+   aBitmapId = KErrNotFound;
+   aMaskId = KErrNotFound;
+   aFileName.Copy( KNullDesC );
+   
    TInt error = KErrNotFound;
-   TInt pos = aPath.FindF( KMif );
+   TInt pos = aStr.FindF( KMif );
    if( pos == 0 )
         {
         // Skip mif token
         pos += KMif().Length();
         // Initialize lexer
-        TLex lex( aPath.Mid( pos ) );
+        TLex lex( aStr.Mid( pos ) );
         lex.SkipSpaceAndMark();
         
         // Check left parenthesis
@@ -609,23 +692,23 @@
 // ---------------------------------------------------------------------------
 //
 TBool CWmImageConverter::ResolveSkinIdAndMifId( 
-                const TDesC& aPath, TAknsItemID& aItemId,
+                const TDesC& aStr, TAknsItemID& aItemId,
                 TInt& aBitmapId, TInt& aMaskId, TDes& aFileName )
    {
    // Syntax: skin(major minor):mif(filename bimapId maskId) 
-   TBool result = ResolveSkinId( aPath, aItemId );
+   TBool result = ResolveSkinId( aStr, aItemId );
    if ( result )
 	   {
-	   TInt pos = aPath.FindF( KColon );
+	   TInt pos = aStr.FindF( KColon );
 	   if ( pos != KErrNotFound )
 	       {
-	       TPtrC ptr = aPath.Mid( pos+1 );
+	       TPtrC ptr = aStr.Mid( pos+1 );
 	       result = ResolveMifId( ptr, aBitmapId, aMaskId, aFileName );
 	       }
 	   }
    else
        {
-       result = ResolveMifId( aPath, aBitmapId, aMaskId, aFileName );
+       result = ResolveMifId( aStr, aBitmapId, aMaskId, aFileName );
        }
    return result;
    }
@@ -748,5 +831,20 @@
         }
     }
 
+// ---------------------------------------------------------------------------
+// CWmImageConverter::ParseIconString
+// ---------------------------------------------------------------------------
+//
+TBool CWmImageConverter::ParseIconString( 
+        const TDesC& aIconStr, 
+        TAknsItemID& aItemId,
+        TInt& aBitmapId, 
+        TInt& aMaskId, 
+        TDes& aFileName )
+    {
+    return ResolveSkinIdAndMifId( 
+            aIconStr, aItemId, aBitmapId, aMaskId, aFileName );
+    }
+
 // End of file