emailuis/emailui/src/FreestyleEmailUiAknStatusIndicator.cpp
changeset 3 a4d6f1ea0416
child 4 e7aa27f58ae1
equal deleted inserted replaced
2:5253a20d2a1e 3:a4d6f1ea0416
       
     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 the License "Symbian Foundation License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     8 */ 
       
     9 
       
    10 #include "FreestyleEmailUiAknStatusIndicator.h"
       
    11 
       
    12 #include <biditext.h>
       
    13 #include <akniconutils.h>
       
    14 #include <aknbiditextutils.h>
       
    15 
       
    16 const TInt KImageMargin = 8;
       
    17 const TInt KTextMargin = 4;
       
    18 const TInt KRoundBoundaryEllipse = 5;
       
    19 const TInt KTuncationCharsWidth = 20;    
       
    20 
       
    21 
       
    22 CFreestyleEmailUiAknStatusIndicator* CFreestyleEmailUiAknStatusIndicator::NewL( const TRect& aRect, const CCoeControl* aParent )
       
    23     {
       
    24     CFreestyleEmailUiAknStatusIndicator* self = CFreestyleEmailUiAknStatusIndicator::NewLC( aRect, aParent );
       
    25     CleanupStack::Pop( self );
       
    26     return self;
       
    27     }
       
    28 
       
    29 CFreestyleEmailUiAknStatusIndicator* CFreestyleEmailUiAknStatusIndicator::NewLC( const TRect& aRect, const CCoeControl* aParent )
       
    30     {
       
    31     CFreestyleEmailUiAknStatusIndicator* self = new (ELeave) CFreestyleEmailUiAknStatusIndicator();
       
    32     CleanupStack::PushL( self );
       
    33     self->ConstructL( aRect, aParent );
       
    34     return self;
       
    35     }
       
    36 
       
    37 CFreestyleEmailUiAknStatusIndicator::~CFreestyleEmailUiAknStatusIndicator()
       
    38     {
       
    39     ClearImage();
       
    40     ClearText();
       
    41     
       
    42     delete iTimer;
       
    43     }
       
    44 
       
    45 void CFreestyleEmailUiAknStatusIndicator::ShowIndicatorL( CFbsBitmap* aBitmap, 
       
    46                                                           CFbsBitmap* aMaskBitmap,
       
    47                                                           TDesC* aText,
       
    48                                                           TInt aDuration )
       
    49     {
       
    50     iTimer->Stop();
       
    51 
       
    52     SetImage( aBitmap, aMaskBitmap );
       
    53     SetTextL( aText );
       
    54 
       
    55     MakeVisible( ETrue );
       
    56     DrawNow();
       
    57     
       
    58     if ( aDuration >= 0 )
       
    59         {
       
    60         HideIndicator( aDuration );
       
    61         }
       
    62     }
       
    63 
       
    64 void CFreestyleEmailUiAknStatusIndicator::SetTextL( TDesC* aText )
       
    65     {
       
    66     SetTextFont();
       
    67 
       
    68     ClearText();    
       
    69     iText = aText; 
       
    70     if ( iText )
       
    71         {
       
    72         iVisualText = HBufC::NewL( iText->Length() + KAknBidiExtraSpacePerLine );
       
    73         CalculateVisualText();
       
    74         }    
       
    75     
       
    76     if ( IsVisible() )
       
    77         {
       
    78         DrawNow();
       
    79         }
       
    80     }
       
    81 
       
    82 void CFreestyleEmailUiAknStatusIndicator::HideIndicator( TInt aDelayBeforeHidingInMs )
       
    83     {
       
    84     iTimer->Stop();
       
    85     
       
    86     if ( aDelayBeforeHidingInMs == 0 )
       
    87         {
       
    88         MakeVisible( EFalse );
       
    89         }
       
    90     else
       
    91         {
       
    92         iTimer->Start( aDelayBeforeHidingInMs );
       
    93         }
       
    94     }
       
    95 
       
    96 CCoeControl* CFreestyleEmailUiAknStatusIndicator::CFreestyleEmailUiAknStatusIndicator::ComponentControl( TInt /*aIndex*/ ) const
       
    97     {
       
    98     return NULL;
       
    99     }
       
   100 
       
   101 TInt CFreestyleEmailUiAknStatusIndicator::CountComponentControls() const
       
   102     {
       
   103     return 0;
       
   104     }
       
   105 
       
   106 void CFreestyleEmailUiAknStatusIndicator::Draw( const TRect& aRect ) const
       
   107     {
       
   108     CWindowGc& gc = SystemGc();
       
   109     gc.Clear( aRect );
       
   110     
       
   111     DrawBoundary( aRect );
       
   112     DrawImage( iImageRect );
       
   113     DrawText( iTextRect );
       
   114     }
       
   115 
       
   116 void CFreestyleEmailUiAknStatusIndicator::SizeChanged()
       
   117     {  
       
   118     CalculateLayout( Rect() );
       
   119     ScaleImage();
       
   120     CalculateVisualText();
       
   121     }
       
   122     
       
   123 CFreestyleEmailUiAknStatusIndicator::CFreestyleEmailUiAknStatusIndicator()
       
   124     : iBitmap( NULL ),
       
   125     iMaskBitmap( NULL ),
       
   126     iText( NULL ),
       
   127     iVisualText( NULL ),
       
   128     iTextFont( NULL )
       
   129     {    
       
   130     }
       
   131 
       
   132 void CFreestyleEmailUiAknStatusIndicator::ConstructL( const TRect& aRect, const CCoeControl* aParent )
       
   133     {
       
   134     iTimer = CFSEmailUiGenericTimer::NewL( this ); 
       
   135     
       
   136     if ( !aParent )
       
   137         {
       
   138         CreateWindowL();
       
   139         }
       
   140     else
       
   141         {
       
   142         SetContainerWindowL( *aParent );
       
   143         }
       
   144     
       
   145     SetRect( aRect );
       
   146     ActivateL();
       
   147     
       
   148     MakeVisible( EFalse );
       
   149     }
       
   150 
       
   151 void CFreestyleEmailUiAknStatusIndicator::TimerEventL( CFSEmailUiGenericTimer* /*aTriggeredTimer*/ )
       
   152     {
       
   153     HideIndicator();
       
   154     }
       
   155 
       
   156 void CFreestyleEmailUiAknStatusIndicator::CalculateLayout( const TRect& aRect ) const
       
   157     {
       
   158     TSize imageSize = aRect.Size();
       
   159     imageSize.iHeight = imageSize.iHeight - 2 * KImageMargin;
       
   160     imageSize.iWidth = imageSize.iWidth - 2 * KImageMargin;
       
   161     if ( imageSize.iWidth > imageSize.iHeight )
       
   162         {
       
   163         imageSize.iWidth = imageSize.iHeight;
       
   164         }
       
   165     else
       
   166         {
       
   167         imageSize.iHeight = imageSize.iWidth;
       
   168         }
       
   169     
       
   170     TSize textSize = aRect.Size();
       
   171     textSize.iHeight = textSize.iHeight - 2 * KTextMargin;
       
   172     textSize.iWidth = textSize.iWidth - 2 * KTextMargin - 2 * KImageMargin - imageSize.iWidth;
       
   173     
       
   174     TLanguage language = User::Language();
       
   175     TBidiText::TDirectionality  direction = TBidiText::ScriptDirectionality( language );    
       
   176     if ( direction == TBidiText::ELeftToRight )
       
   177         {
       
   178         TPoint imageLeftTop ( aRect.iTl.iX + KImageMargin, aRect.iTl.iY + KImageMargin );
       
   179         iImageRect.SetRect( imageLeftTop, imageSize );    
       
   180         
       
   181         TPoint textLeftTop ( aRect.iTl.iX + 2*KImageMargin + imageSize.iWidth + KTextMargin, aRect.iTl.iY + KTextMargin );
       
   182         iTextRect.SetRect( textLeftTop, textSize );    
       
   183         }
       
   184     else
       
   185         {
       
   186         TPoint imageLeftTop ( aRect.iBr.iX - KImageMargin - imageSize.iWidth, aRect.iTl.iY + KImageMargin );
       
   187         iImageRect.SetRect( imageLeftTop, imageSize );    
       
   188         
       
   189         TPoint textLeftTop ( aRect.iTl.iX + KTextMargin, aRect.iTl.iY + KTextMargin );
       
   190         iTextRect.SetRect( textLeftTop, textSize );    
       
   191         }
       
   192     }
       
   193 
       
   194 void CFreestyleEmailUiAknStatusIndicator::SetTextFont() const
       
   195     {
       
   196     if ( iTextFont )
       
   197         return;
       
   198     
       
   199     const CFont* font = AknLayoutUtils::FontFromId( EAknLogicalFontPrimarySmallFont );
       
   200     iTextFont = CONST_CAST( CFont*, font );
       
   201     }
       
   202 
       
   203 void CFreestyleEmailUiAknStatusIndicator::CalculateVisualText() const
       
   204     {
       
   205     if ( iVisualText )
       
   206         {
       
   207         TPtr visualText = iVisualText->Des();
       
   208         visualText.Zero();
       
   209         TInt MaxWidthInPixels = iTextRect.Size().iWidth - KTuncationCharsWidth;
       
   210         TInt MaxClippedWidthInPixels = iTextRect.Size().iWidth;
       
   211         AknBidiTextUtils::ConvertToVisualAndClip( *iText,
       
   212                                                   visualText,
       
   213                                                   *iTextFont,
       
   214                                                   MaxWidthInPixels,
       
   215                                                   MaxClippedWidthInPixels );    
       
   216         }
       
   217     }
       
   218 
       
   219 void CFreestyleEmailUiAknStatusIndicator::DrawBoundary( const TRect& aRect ) const
       
   220     {
       
   221     CWindowGc& gc = SystemGc();
       
   222     gc.SetPenColor( KRgbCyan );
       
   223     TSize ellipse( KRoundBoundaryEllipse, KRoundBoundaryEllipse );
       
   224     gc.DrawRoundRect( aRect, ellipse);
       
   225     }
       
   226 
       
   227 void CFreestyleEmailUiAknStatusIndicator::DrawImage( const TRect& aRect ) const
       
   228     {
       
   229     if ( !iBitmap )
       
   230         return;
       
   231     
       
   232     CWindowGc& gc = SystemGc();
       
   233     if ( iMaskBitmap )
       
   234         {
       
   235         TRect bmpPieceRect(TPoint(0,0), aRect.Size() ); 
       
   236         gc.BitBltMasked( aRect.iTl, iBitmap, bmpPieceRect, iMaskBitmap, EFalse );
       
   237         }
       
   238     else
       
   239         {
       
   240         gc.DrawBitmap( aRect, iBitmap );
       
   241         }
       
   242     }
       
   243 
       
   244 void CFreestyleEmailUiAknStatusIndicator::DrawText( const TRect& aRect ) const
       
   245     {
       
   246     if ( !iVisualText )
       
   247         return;
       
   248     if ( iVisualText->Length() == 0 )
       
   249         return;
       
   250     
       
   251     TLanguage language = User::Language();
       
   252     TBidiText::TDirectionality  direction = TBidiText::ScriptDirectionality( language );
       
   253     
       
   254     CGraphicsContext::TTextAlign alignment = CGraphicsContext::ELeft;
       
   255     if ( direction == TBidiText::ERightToLeft )
       
   256         {
       
   257         alignment = CGraphicsContext::ERight;
       
   258         }
       
   259     
       
   260     CWindowGc& gc = SystemGc();
       
   261     gc.UseFont( iTextFont );
       
   262 
       
   263     TInt baseline = aRect.Height() / 2 + iTextFont->FontMaxAscent() / 2; 
       
   264     
       
   265     gc.SetPenColor(KRgbBlack);
       
   266 
       
   267     gc.DrawText( *iVisualText, aRect, baseline, alignment, 0);
       
   268     }
       
   269 
       
   270 void CFreestyleEmailUiAknStatusIndicator::SetImage( CFbsBitmap* aBitmap, CFbsBitmap* aMaskBitmap )
       
   271     {
       
   272     ClearImage();
       
   273     iBitmap = aBitmap;
       
   274     iMaskBitmap = aMaskBitmap;
       
   275     ScaleImage();
       
   276     }
       
   277 
       
   278 void CFreestyleEmailUiAknStatusIndicator::ScaleImage() const
       
   279     {
       
   280     if ( iBitmap )
       
   281         {
       
   282         AknIconUtils::DisableCompression( iBitmap );
       
   283         AknIconUtils::SetSize( iBitmap, iImageRect.Size(), EAspectRatioPreserved );
       
   284         }
       
   285     if ( iMaskBitmap )
       
   286         {
       
   287         AknIconUtils::DisableCompression( iMaskBitmap );
       
   288         AknIconUtils::SetSize( iMaskBitmap, iImageRect.Size(), EAspectRatioPreserved );
       
   289         }    
       
   290     }
       
   291 
       
   292 void CFreestyleEmailUiAknStatusIndicator::ClearImage()
       
   293     {
       
   294     delete iBitmap;
       
   295     iBitmap = NULL;
       
   296     delete iMaskBitmap;
       
   297     iMaskBitmap = NULL;
       
   298     }
       
   299 
       
   300 void CFreestyleEmailUiAknStatusIndicator::ClearText()
       
   301     {
       
   302     delete iText;
       
   303     iText = NULL;
       
   304     
       
   305     delete iVisualText;
       
   306     iVisualText = NULL;
       
   307     }
       
   308