textinput/peninputarc/src/peninputlayoutcontrol/peninputlayoutsmiley.cpp
changeset 0 eb1f2e154e89
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2  * Copyright (c) 2009 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: implementation smiley classes 
       
    15  *
       
    16  */
       
    17 
       
    18 #include <AknIconUtils.h> 
       
    19 #include <barsread.h>
       
    20 #include <coemain.h>
       
    21 #include <smiley.mbg>
       
    22 #include <smiley.rsg>
       
    23 #include "peninputlayoutsmiley.h"
       
    24 
       
    25 //resource file
       
    26 _LIT( KSmileyMifFile, "z:\\resource\\apps\\smiley.mif" );
       
    27 _LIT( KSmileyResFile, "z:\\resource\\smiley.rsc" );
       
    28 
       
    29 //string to calculate smiley width
       
    30 _LIT( KSmileySizeTemplate, "\xf8e4" );
       
    31 
       
    32 //invisible place holder for smiley character
       
    33 const TUint16 KSmileyPlaceHolder = 0x3000;
       
    34 const TInt KDefaultSmileySize = 24;
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // Symbian Constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CPeninputSmileyImage* CPeninputSmileyImage::NewL( TInt aSmileyId, TInt aBitmapId, 
       
    41                                                   TInt aMaskId, const TSize& aImageSize )
       
    42     {
       
    43     CPeninputSmileyImage* self = CPeninputSmileyImage::NewLC( aSmileyId, aBitmapId, 
       
    44                                                               aMaskId, aImageSize );
       
    45     CleanupStack::Pop( self );
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // Symbian Constructor
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CPeninputSmileyImage* CPeninputSmileyImage::NewLC( TInt aSmileyId, TInt aBitmapId, 
       
    54                                                    TInt aMaskId, const TSize& aImageSize )
       
    55     {
       
    56     CPeninputSmileyImage* self = new ( ELeave ) CPeninputSmileyImage( aSmileyId, 
       
    57                                                    aBitmapId, aMaskId,aImageSize );
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL();
       
    60     return self;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // c++ destructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CPeninputSmileyImage::~CPeninputSmileyImage()
       
    68     {
       
    69     delete iImageBitmap;
       
    70     delete iMaskBitmap;
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Create smiley image
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void CPeninputSmileyImage::CreateImageL()
       
    78     {
       
    79     DestroyImage();
       
    80                 
       
    81     TInt maskid = iMaskBitmapId != 0 ? iMaskBitmapId : iImageBitmapId;
       
    82     
       
    83     AknIconUtils::CreateIconL( iImageBitmap, iMaskBitmap, 
       
    84                                KSmileyMifFile, iImageBitmapId, maskid );
       
    85    
       
    86     AknIconUtils::SetSize( iImageBitmap, iImageSize );
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // Destroy smiley image
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 void CPeninputSmileyImage::DestroyImage()
       
    94     {
       
    95     if ( iImageBitmap )
       
    96         {
       
    97         delete iImageBitmap;
       
    98         iImageBitmap = NULL;
       
    99         }
       
   100     
       
   101     if ( iMaskBitmap )
       
   102         {
       
   103         delete iMaskBitmap;
       
   104         iMaskBitmap = NULL;
       
   105         }    
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // Get image bitmap of smiley
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 CFbsBitmap* CPeninputSmileyImage::ImageBitmap()
       
   113     {
       
   114     return iImageBitmap;
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // Get mask bitmap of smiley
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 CFbsBitmap* CPeninputSmileyImage::MaskBitmap()
       
   122     {
       
   123     return iMaskBitmap;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // Get smiley id
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TInt CPeninputSmileyImage::SmileyId()
       
   131     {
       
   132     return iSmileyId;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // Get smiley size
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 TSize CPeninputSmileyImage::SmileyImageSize()
       
   140     {
       
   141     return iImageSize;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // Set smiley size
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CPeninputSmileyImage::SetImageSize( const TSize& aSize )
       
   149     {
       
   150     if ( aSize != iImageSize )
       
   151         {
       
   152         iImageSize = aSize;
       
   153         if ( iImageBitmap )
       
   154             {
       
   155             AknIconUtils::SetSize( iImageBitmap, aSize );
       
   156             }     
       
   157         }
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // c++ constructor
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 CPeninputSmileyImage::CPeninputSmileyImage( TInt aSmileyId, TInt aBitmapId, 
       
   165                                             TInt aMaskId, const TSize& aImageSize ) 
       
   166     : iSmileyId( aSmileyId ) 
       
   167     , iImageBitmapId( aBitmapId ) 
       
   168     , iMaskBitmapId( aMaskId )
       
   169     , iImageSize( aImageSize )
       
   170     {
       
   171     }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // Symbian second-phase constructor
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CPeninputSmileyImage::ConstructL()
       
   179     {
       
   180     }
       
   181 
       
   182 //////////////////////////////////////////////////////////////////////////////
       
   183 //CPeninputSmileyManager
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // Symbian Constructor
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 CPeninputSmileyManager* CPeninputSmileyManager::NewL()
       
   190     {
       
   191     CPeninputSmileyManager* self = CPeninputSmileyManager::NewLC();
       
   192     CleanupStack::Pop( self );
       
   193     return self;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // Symbian Constructor
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 CPeninputSmileyManager* CPeninputSmileyManager::NewLC()
       
   201     {
       
   202     CPeninputSmileyManager* self = new ( ELeave ) CPeninputSmileyManager;
       
   203     CleanupStack::PushL( self );
       
   204     self->ConstructL();
       
   205     return self;
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // C++ destructor
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 CPeninputSmileyManager::~CPeninputSmileyManager()
       
   213     {
       
   214     iSmileyArray.ResetAndDestroy();
       
   215     iSmileyArray.Close();
       
   216     
       
   217     iSmileyHash.Close();
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // Get smiley object by id
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 CPeninputSmileyImage* CPeninputSmileyManager::SmileyImage( TInt aSmileyId )
       
   225     {
       
   226     TInt key = aSmileyId;
       
   227     CPeninputSmileyImage** ptr = iSmileyHash.Find( key );
       
   228     if ( ptr )
       
   229         {
       
   230         return *ptr;
       
   231         }
       
   232     return NULL;
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // Detech if Specified character is smiley
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 TBool CPeninputSmileyManager::IsSmileyCode( TUint16 aCharCode )
       
   240     {
       
   241     return ( aCharCode >= 0xf880 && aCharCode <= 0xf8e4 );
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // Get smiley template
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 const TDesC& CPeninputSmileyManager::SmileySizeTemplate()
       
   249     {
       
   250     return KSmileySizeTemplate;
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // Get invisible character as place holder
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 TUint16 CPeninputSmileyManager::SmileyPlaceHolderCharacter()
       
   258     {
       
   259     return KSmileyPlaceHolder;
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------------------------
       
   263 // C++ constructor
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 CPeninputSmileyManager::CPeninputSmileyManager()
       
   267     {
       
   268     }
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // Symbian second-phase constructor
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 void CPeninputSmileyManager::ConstructL()
       
   275     {
       
   276     TInt id = CCoeEnv::Static()->AddResourceFileL( KSmileyResFile );
       
   277     ReadSmileyInfoL( R_SMILEY_ICONS_INFO );
       
   278     CCoeEnv::Static()->DeleteResourceFile( id );
       
   279 
       
   280     PreLoadSmileyImageL();
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // Read smiley info from resource
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CPeninputSmileyManager::ReadSmileyInfoL( TInt aResourceId )
       
   288     {
       
   289     TResourceReader reader;
       
   290     CCoeEnv::Static()->CreateResourceReaderLC( reader, aResourceId );//resId
       
   291 
       
   292     TInt count = reader.ReadInt16();
       
   293 
       
   294     CPeninputSmileyImage* smiley = NULL;
       
   295     for ( TInt i = 0; i < count; i++ )
       
   296         {
       
   297         TUint16 animation = reader.ReadUint16(); // read animation
       
   298         
       
   299         TUint16 smileyId = reader.ReadUint16();
       
   300         
       
   301         TInt32 bitmapId = reader.ReadInt32();
       
   302         TInt32 maskId = reader.ReadInt32();
       
   303         TInt32 staticBitmapId = reader.ReadInt32();
       
   304         TInt32 staticMaskId = reader.ReadInt32();
       
   305         
       
   306         TPtrC smileycode = reader.ReadTPtrC(); // read string
       
   307 
       
   308         TSize size( KDefaultSmileySize, KDefaultSmileySize );
       
   309         if ( animation )
       
   310             {
       
   311             smiley = CPeninputSmileyImage::NewL( smileyId, staticBitmapId, 
       
   312                                                  staticMaskId, size );
       
   313             }
       
   314         else
       
   315             {
       
   316             smiley = CPeninputSmileyImage::NewL( smileyId, bitmapId, maskId,
       
   317                                                  size );            
       
   318             }
       
   319         iSmileyArray.Append( smiley );
       
   320         
       
   321         iSmileyHash.Insert( smileyId, smiley );
       
   322         }
       
   323 
       
   324     CleanupStack::PopAndDestroy(); // reader
       
   325     }
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // Initalize all smily images
       
   329 // ---------------------------------------------------------------------------
       
   330 //
       
   331 void CPeninputSmileyManager::PreLoadSmileyImageL()
       
   332     {
       
   333     for ( TInt i = 0; i < iSmileyArray.Count(); i++ )
       
   334         {
       
   335         iSmileyArray[i]->CreateImageL();
       
   336         }
       
   337     }