meetingrequest/mrgui/src/cmrattachmentindicator.cpp
branchRCL_3
changeset 12 4ce476e64c59
child 16 b5fbb9b25d57
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2 * Copyright (c) 2008 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: Attachment indicator implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cmrattachmentindicator.h"
       
    19 
       
    20 #include <biditext.h>
       
    21 #include <aknutils.h>
       
    22 #include <akniconutils.h>
       
    23 #include <aknbiditextutils.h>
       
    24 
       
    25 namespace
       
    26 {
       
    27 const TInt KImageMargin = 8;
       
    28 const TInt KTextMargin = 4;
       
    29 const TInt KRoundBoundaryEllipse = 5;
       
    30 const TInt KTruncationCharsWidth = 20;
       
    31 }
       
    32 
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CMRAttachmentIndicator::CMRAttachmentIndicator
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CMRAttachmentIndicator::CMRAttachmentIndicator()
       
    39     : iBitmap( NULL ),
       
    40     iMaskBitmap( NULL ),
       
    41     iText( NULL ),
       
    42     iVisualText( NULL ),
       
    43     iTextFont( NULL )
       
    44     {
       
    45     }
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CMRAttachmentIndicator::~CMRAttachmentIndicator
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CMRAttachmentIndicator::~CMRAttachmentIndicator()
       
    52     {
       
    53     ClearImage();
       
    54     ClearText();
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CMRAttachmentIndicator::CMRAttachmentIndicator
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CMRAttachmentIndicator* CMRAttachmentIndicator::NewL(
       
    62         const TRect& aRect,
       
    63         const CCoeControl* aParent )
       
    64     {
       
    65     CMRAttachmentIndicator* self = new (ELeave) CMRAttachmentIndicator();
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL( aRect, aParent );
       
    68     CleanupStack::Pop( self );
       
    69     return self;
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CMRAttachmentIndicator::ConstructL
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 void CMRAttachmentIndicator::ConstructL(
       
    77         const TRect& aRect,
       
    78         const CCoeControl* aParent )
       
    79     {
       
    80     CreateWindowL( aParent );
       
    81 
       
    82     SetRect( aRect );
       
    83     ActivateL();
       
    84 
       
    85     MakeVisible( EFalse );
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CMRAttachmentIndicator::ShowIndicatorL
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CMRAttachmentIndicator::ShowIndicatorL(
       
    93         CFbsBitmap* aBitmap,
       
    94         CFbsBitmap* aMaskBitmap,
       
    95         const TDesC& aText,
       
    96         TInt /*aDuration*/ )
       
    97     {
       
    98     SetImage( aBitmap, aMaskBitmap );
       
    99     SetTextL( aText );
       
   100 
       
   101     MakeVisible( ETrue );
       
   102     DrawNow();
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // CMRAttachmentIndicator::SetTextL
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 void CMRAttachmentIndicator::SetTextL( const TDesC& aText )
       
   110     {
       
   111     SetTextFont();
       
   112 
       
   113     ClearText();
       
   114 
       
   115     iText = aText.AllocL();
       
   116     if ( aText.Length() )
       
   117         {
       
   118         iVisualText = HBufC::NewL( aText.Length() + KAknBidiExtraSpacePerLine );
       
   119         CalculateVisualText();
       
   120         }
       
   121 
       
   122     if ( IsVisible() )
       
   123         {
       
   124         DrawNow();
       
   125         }
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CMRAttachmentIndicator::SetImage
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CMRAttachmentIndicator::SetImage(
       
   133         CFbsBitmap* aBitmap,
       
   134         CFbsBitmap* aMaskBitmap )
       
   135     {
       
   136     ClearImage();
       
   137     iBitmap = aBitmap;
       
   138     iMaskBitmap = aMaskBitmap;
       
   139     ScaleImage();
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CMRAttachmentIndicator::HideIndicator
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CMRAttachmentIndicator::HideIndicator( TInt aDelayBeforeHidingInMs )
       
   147     {
       
   148     if ( aDelayBeforeHidingInMs == 0 )
       
   149         {
       
   150         MakeVisible( EFalse );
       
   151         }
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // CMRAttachmentIndicator::ComponentControl
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 CCoeControl* CMRAttachmentIndicator::CMRAttachmentIndicator::ComponentControl( TInt /*aIndex*/ ) const
       
   159     {
       
   160     return NULL;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // CMRAttachmentIndicator::CountComponentControls
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 TInt CMRAttachmentIndicator::CountComponentControls() const
       
   168     {
       
   169     return 0;
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CMRAttachmentIndicator::Draw
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CMRAttachmentIndicator::Draw( const TRect& aRect ) const
       
   177     {
       
   178     CWindowGc& gc = SystemGc();
       
   179     gc.Clear( aRect );
       
   180 
       
   181     DrawBoundary( aRect );
       
   182     DrawImage( iImageRect );
       
   183     DrawText( iTextRect );
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CMRAttachmentIndicator::SizeChanged
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CMRAttachmentIndicator::SizeChanged()
       
   191     {
       
   192     CalculateLayout( Rect() );
       
   193     ScaleImage();
       
   194     CalculateVisualText();
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CMRAttachmentIndicator::Image
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 const CFbsBitmap* CMRAttachmentIndicator::Image() const
       
   202     {
       
   203     return iBitmap;
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CMRAttachmentIndicator::ImageMask
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 const CFbsBitmap* CMRAttachmentIndicator::ImageMask() const
       
   211     {
       
   212     return iMaskBitmap;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // CMRAttachmentIndicator::Text
       
   217 // ---------------------------------------------------------------------------
       
   218 //
       
   219 const TDesC& CMRAttachmentIndicator::Text() const
       
   220     {
       
   221     return *iText;
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // CMRAttachmentIndicator::CalculateLayout
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 void CMRAttachmentIndicator::CalculateLayout( const TRect& aRect ) const
       
   229     {
       
   230     TSize imageSize = aRect.Size();
       
   231     imageSize.iHeight = imageSize.iHeight - 2 * KImageMargin;
       
   232     imageSize.iWidth = imageSize.iWidth - 2 * KImageMargin;
       
   233     if ( imageSize.iWidth > imageSize.iHeight )
       
   234         {
       
   235         imageSize.iWidth = imageSize.iHeight;
       
   236         }
       
   237     else
       
   238         {
       
   239         imageSize.iHeight = imageSize.iWidth;
       
   240         }
       
   241 
       
   242     TSize textSize = aRect.Size();
       
   243     textSize.iHeight = textSize.iHeight - 2 * KTextMargin;
       
   244     textSize.iWidth = textSize.iWidth - 2 * KTextMargin - 2 * KImageMargin - imageSize.iWidth;
       
   245 
       
   246     TLanguage language = User::Language();
       
   247     TBidiText::TDirectionality  direction = TBidiText::ScriptDirectionality( language );
       
   248     if ( direction == TBidiText::ELeftToRight )
       
   249         {
       
   250         TPoint imageLeftTop ( aRect.iTl.iX + KImageMargin, aRect.iTl.iY + KImageMargin );
       
   251         iImageRect.SetRect( imageLeftTop, imageSize );
       
   252 
       
   253         TPoint textLeftTop ( aRect.iTl.iX + 2*KImageMargin + imageSize.iWidth + KTextMargin, aRect.iTl.iY + KTextMargin );
       
   254         iTextRect.SetRect( textLeftTop, textSize );
       
   255         }
       
   256     else
       
   257         {
       
   258         TPoint imageLeftTop ( aRect.iBr.iX - KImageMargin - imageSize.iWidth, aRect.iTl.iY + KImageMargin );
       
   259         iImageRect.SetRect( imageLeftTop, imageSize );
       
   260 
       
   261         TPoint textLeftTop ( aRect.iTl.iX + KTextMargin, aRect.iTl.iY + KTextMargin );
       
   262         iTextRect.SetRect( textLeftTop, textSize );
       
   263         }
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // CMRAttachmentIndicator::SetTextFont
       
   268 // ---------------------------------------------------------------------------
       
   269 //
       
   270 void CMRAttachmentIndicator::SetTextFont() const
       
   271     {
       
   272     if ( iTextFont )
       
   273         return;
       
   274 
       
   275     const CFont* font = AknLayoutUtils::FontFromId( EAknLogicalFontPrimarySmallFont );
       
   276     iTextFont = CONST_CAST( CFont*, font );
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CMRAttachmentIndicator::CalculateVisualText
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 void CMRAttachmentIndicator::CalculateVisualText() const
       
   284     {
       
   285     if ( iVisualText )
       
   286         {
       
   287         TPtr visualText = iVisualText->Des();
       
   288         visualText.Zero();
       
   289         TInt MaxWidthInPixels = iTextRect.Size().iWidth - KTruncationCharsWidth;
       
   290         TInt MaxClippedWidthInPixels = iTextRect.Size().iWidth;
       
   291         AknBidiTextUtils::ConvertToVisualAndClip( *iText,
       
   292                                                   visualText,
       
   293                                                   *iTextFont,
       
   294                                                   MaxWidthInPixels,
       
   295                                                   MaxClippedWidthInPixels );
       
   296         }
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // CMRAttachmentIndicator::DrawBoundary
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CMRAttachmentIndicator::DrawBoundary( const TRect& aRect ) const
       
   304     {
       
   305     CWindowGc& gc = SystemGc();
       
   306     gc.SetPenColor( KRgbCyan );
       
   307     TSize ellipse( KRoundBoundaryEllipse, KRoundBoundaryEllipse );
       
   308     gc.DrawRoundRect( aRect, ellipse);
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // CMRAttachmentIndicator::DrawImage
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 void CMRAttachmentIndicator::DrawImage( const TRect& aRect ) const
       
   316     {
       
   317     if ( !iBitmap )
       
   318         return;
       
   319 
       
   320     CWindowGc& gc = SystemGc();
       
   321     if ( iMaskBitmap )
       
   322         {
       
   323         TRect bmpPieceRect(TPoint(0,0), aRect.Size() );
       
   324         gc.BitBltMasked( aRect.iTl, iBitmap, bmpPieceRect, iMaskBitmap, EFalse );
       
   325         }
       
   326     else
       
   327         {
       
   328         gc.DrawBitmap( aRect, iBitmap );
       
   329         }
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // CMRAttachmentIndicator::DrawText
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void CMRAttachmentIndicator::DrawText( const TRect& aRect ) const
       
   337     {
       
   338     if ( !iVisualText )
       
   339         return;
       
   340     if ( iVisualText->Length() == 0 )
       
   341         return;
       
   342 
       
   343     TLanguage language = User::Language();
       
   344     TBidiText::TDirectionality  direction = TBidiText::ScriptDirectionality( language );
       
   345 
       
   346     CGraphicsContext::TTextAlign alignment = CGraphicsContext::ELeft;
       
   347     if ( direction == TBidiText::ERightToLeft )
       
   348         {
       
   349         alignment = CGraphicsContext::ERight;
       
   350         }
       
   351 
       
   352     CWindowGc& gc = SystemGc();
       
   353     gc.UseFont( iTextFont );
       
   354 
       
   355     TInt baseline = aRect.Height() / 2 + iTextFont->FontMaxAscent() / 2;
       
   356 
       
   357     gc.SetPenColor(KRgbBlack);
       
   358 
       
   359     gc.DrawText( *iVisualText, aRect, baseline, alignment, 0);
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 // CMRAttachmentIndicator::ScaleImage
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 void CMRAttachmentIndicator::ScaleImage() const
       
   367     {
       
   368     if ( iBitmap )
       
   369         {
       
   370         AknIconUtils::DisableCompression( iBitmap );
       
   371         AknIconUtils::SetSize( iBitmap, iImageRect.Size(), EAspectRatioPreserved );
       
   372         }
       
   373     if ( iMaskBitmap )
       
   374         {
       
   375         AknIconUtils::DisableCompression( iMaskBitmap );
       
   376         AknIconUtils::SetSize( iMaskBitmap, iImageRect.Size(), EAspectRatioPreserved );
       
   377         }
       
   378     }
       
   379 
       
   380 // ---------------------------------------------------------------------------
       
   381 // CMRAttachmentIndicator::ClearImage
       
   382 // ---------------------------------------------------------------------------
       
   383 //
       
   384 void CMRAttachmentIndicator::ClearImage()
       
   385     {
       
   386     delete iBitmap;
       
   387     iBitmap = NULL;
       
   388     delete iMaskBitmap;
       
   389     iMaskBitmap = NULL;
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // CMRAttachmentIndicator::ClearText
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 void CMRAttachmentIndicator::ClearText()
       
   397     {
       
   398     delete iText;
       
   399     iText = NULL;
       
   400 
       
   401     delete iVisualText;
       
   402     iVisualText = NULL;
       
   403     }
       
   404 
       
   405 // EOF