src/screensaverindicatortext.cpp
changeset 14 8a173132b0aa
parent 2 058b1fc1663a
equal deleted inserted replaced
2:058b1fc1663a 14:8a173132b0aa
     1 /*
       
     2 * Copyright (c) 2003 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:   CScreensaverIndicatorText implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <AknLayoutFont.h>
       
    21 #include <barsread.h>
       
    22 
       
    23 #include "screensaverindicatortext.h"
       
    24 
       
    25 
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CScreensaverTextIndicator::Draw
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 void CScreensaverIndicatorText::Draw( CWindowGc& aGc ) const
       
    32     {
       
    33     if ( iFont )
       
    34         {
       
    35         aGc.UseFont( iFont );
       
    36         }
       
    37     aGc.SetPenColor( iTextColor );
       
    38 
       
    39     // Draw normal bidi text
       
    40     TBidiText* bidi= NULL;
       
    41     TRAPD( err, bidi = TBidiText::NewL( iText, 1 ) );
       
    42     if ( err == KErrNone )
       
    43         {
       
    44         bidi->WrapText( Width(), *iFont, NULL );
       
    45         if ( iTextRect.IsEmpty() )
       
    46             {
       
    47             bidi->DrawText( aGc, iTopLeft );
       
    48             }
       
    49         else
       
    50             {
       
    51             TPoint pt = iTextRect.iTl;
       
    52             pt.iY += iTextOffset;
       
    53             bidi->DrawText( aGc, pt );
       
    54             }
       
    55         delete bidi;
       
    56         }
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CScreensaverTextIndicator::ConstructL
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CScreensaverIndicatorText::ConstructL( TResourceReader &aReader,
       
    64         TUint32 aBgColor, TUint aTextColor )
       
    65     {
       
    66     CScreensaverIndicator::ConstructL( aReader, aBgColor, aTextColor );
       
    67 
       
    68     iFont = AknLayoutUtils::FontFromId( aReader.ReadInt32() );
       
    69     if ( !iFont )
       
    70         {
       
    71         iFont = LatinPlain12();
       
    72         }
       
    73 
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CScreensaverTextIndicator::SetupDrawingParameters
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CScreensaverIndicatorText::SetupDrawingParameters( const TPoint& aCorner,
       
    81         const TRect& aParentRect )
       
    82     {
       
    83     iVisible = EFalse;
       
    84 
       
    85     if ( iText.Length() <= 0 )
       
    86         {
       
    87         return;
       
    88         }
       
    89 
       
    90     TInt textWidth = ( iFont ? iFont->TextWidthInPixels(iText) + 1
       
    91             : iText.Length() * 11 ); // Should be enough for APAC too
       
    92 
       
    93     iRect = TRect( 0, 0, textWidth, aParentRect.Size().iHeight );
       
    94 
       
    95     iTopLeft.iX = aCorner.iX;
       
    96 
       
    97     if ( iAlignment == ESsAlignRight )
       
    98         {
       
    99         // Change topright to topleft.      
       
   100         iTopLeft.iX -= iRect.Size().iWidth;
       
   101         }
       
   102 
       
   103     iTopLeft.iY = aCorner.iY + ( aParentRect.Size().iHeight / 2 ) + ( ( aParentRect.Size().iHeight - iFont->HeightInPixels() ) / 2 ) + 2;
       
   104 
       
   105     // Make sure text rect is not used
       
   106     iTextRect.SetRect( 0, 0, 0, 0 );
       
   107 
       
   108     iVisible = ETrue;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CScreensaverTextIndicator::ConstructL
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 TBool CScreensaverIndicatorText::CheckVisibilityConditions() const
       
   116     {
       
   117     return ( iText.Length() > 0 ? ETrue : EFalse );
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CScreensaverTextIndicator::ConstructL
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CScreensaverIndicatorText::Payload( TIndicatorPayload &aPayload ) const
       
   125     {
       
   126     aPayload.iType = EPayloadTypeText;
       
   127     aPayload.iText = iText;
       
   128     aPayload.iInteger = -1;
       
   129     aPayload.iIsDisplayed = Visible();
       
   130     aPayload.iIcon = NULL;
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CScreensaverTextIndicator::ConstructL
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void CScreensaverIndicatorText::SetPayload( const TIndicatorPayload& aPayload )
       
   138     {
       
   139     if ( aPayload.iType != EPayloadTypeText )
       
   140         {
       
   141         return;
       
   142         }
       
   143 
       
   144     if ( aPayload.iText.Length() > iText.MaxLength() )
       
   145         {
       
   146         return; // Don't panic.      
       
   147         }
       
   148 
       
   149     iText = aPayload.iText;
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CScreensaverTextIndicator::SetTextLayout
       
   154 // Sets text position and font
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void CScreensaverIndicatorText::SetTextLayout( TAknLayoutText& aLayout, TInt aX )
       
   158     {
       
   159     // Font from layout
       
   160     iFont = aLayout.Font();
       
   161     // Layout MUST have a font
       
   162     ASSERT( iFont != NULL );
       
   163 
       
   164     // Vertical dimension of text rect from layout
       
   165     iTextRect = aLayout.TextRect();
       
   166 
       
   167     // Calculate text offset from rect top
       
   168     const CAknLayoutFont* layoutFont =
       
   169             CAknLayoutFont::AsCAknLayoutFontOrNull( iFont );
       
   170 
       
   171     if ( layoutFont )
       
   172         {
       
   173         iTextOffset = layoutFont->TextPaneTopToBaseline();
       
   174         }
       
   175     else
       
   176         {
       
   177         iTextOffset = iFont->AscentInPixels();
       
   178         }
       
   179 
       
   180     // X-pos from parameter
       
   181     iTextRect.iTl.iX = aX;
       
   182 
       
   183     // Initially set indicator rect (this may later be overridden by SetWidth())
       
   184     iRect = TRect( 0, 0, PreferredWidth(), aLayout.TextRect().Height() );
       
   185 
       
   186     // If right-aligned, the X-pos indicates right edge - switch to left
       
   187     if ( iAlignment == ESsAlignRight )
       
   188         {
       
   189         // Change topright to topleft.      
       
   190         iTextRect.iTl.iX -= Width(); // iRect.Size().iWidth;  
       
   191         }
       
   192 
       
   193     // Indicator width = text rect width
       
   194     iTextRect.SetWidth( Width() );
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CScreensaverTextIndicator::PreferredWidth
       
   199 // Gets preferred indicator width
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TInt CScreensaverIndicatorText::PreferredWidth()
       
   203     {
       
   204     TInt textWidth = ( iFont ? iFont->TextWidthInPixels( iText ) + 1
       
   205             : iText.Length() * 11 ); // Should be enough for APAC too
       
   206     return textWidth;
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CScreensaverTextIndicator::SetWidth
       
   211 // Sets indicator width
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CScreensaverIndicatorText::SetWidth( TInt iWidth )
       
   215     {
       
   216     iRect.SetWidth( iWidth );
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CScreensaverTextIndicator::SetXPos
       
   221 // Sets indicator X-position (new layout)
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CScreensaverIndicatorText::SetXPos( TInt aX )
       
   225     {
       
   226     iTopLeft.iX = aX;
       
   227     if ( iAlignment == ESsAlignRight )
       
   228         {
       
   229         // Change topright to topleft.      
       
   230         iTopLeft.iX -= iRect.Size().iWidth;
       
   231         }
       
   232     }
       
   233 
       
   234 // End of file