idlehomescreen/xmluirendering/renderingplugins/xnclockfactory/src/xndatecontrol.cpp
branchRCL_3
changeset 35 3321d3e205b6
equal deleted inserted replaced
34:5456b4e8b3a8 35:3321d3e205b6
       
     1 /*
       
     2 * Copyright (c) 2006-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:  Clock control
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // System includes
       
    20 #include <e32std.h>
       
    21 #include <debug.h>
       
    22 #include <AknLayoutFont.h>
       
    23 #include <AknUtils.h>
       
    24 #include <AknBidiTextUtils.h>
       
    25 #include <StringLoader.h>
       
    26 #include <bacntf.h>
       
    27 
       
    28 // User includes
       
    29 #include "xndatecontrol.h"
       
    30 #include "xncontroladapter.h"
       
    31 #include "xnclockadapter.h"
       
    32 #include "xnnodepluginif.h"
       
    33 #include "xnproperty.h"
       
    34 #include "xnuienginepluginif.h"
       
    35 #include "c_xnutils.h"
       
    36 
       
    37 // Constants
       
    38 const TInt KMaxDateStringLength = 100;
       
    39 const TInt KMaxDayNumberStringLength = 10;
       
    40 
       
    41 _LIT( KDateFont, "EAknLogicalFontSecondaryFont" );
       
    42 _LIT( KTimeFormat, "%0U %1U" );
       
    43 
       
    44 
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CXnDateControl::CXnDateControl
       
    49 // C++ default constructor can NOT contain any code, that
       
    50 // might leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CXnDateControl::CXnDateControl( CXnClockAdapter& aAdapter,
       
    54                                 TXnDateType aType )
       
    55     : iAdapter( aAdapter ), iDateType( aType )
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CXnDateControl::ConstructL
       
    61 // Symbian 2nd phase constructor can leave.
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 void CXnDateControl::ConstructL()
       
    65     {
       
    66     // Create enviroment notifier
       
    67     iEnvNotifier = CEnvironmentChangeNotifier::NewL( 
       
    68         CActive::EPriorityStandard, TCallBack( HandleCallBackL, this ) );
       
    69     // Start listening notifications
       
    70     iEnvNotifier->Start();
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CXnDateControl::NewL
       
    75 // Two-phased constructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CXnDateControl* CXnDateControl::NewL( CXnClockAdapter& aAdapter,
       
    79                                       TXnDateType aType )
       
    80     {
       
    81     CXnDateControl* self = new (ELeave) CXnDateControl( aAdapter, aType );
       
    82     CleanupStack::PushL( self );    
       
    83     self->ConstructL();
       
    84     CleanupStack::Pop( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // Destructor
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CXnDateControl::~CXnDateControl()
       
    93     {
       
    94     delete iDateText;
       
    95     delete iEnvNotifier;
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CXnDateControl::DrawL
       
   100 // -----------------------------------------------------------------------------
       
   101 //       
       
   102 void CXnDateControl::DrawL( CWindowGc& aGc, CXnNodePluginIf* aNode )
       
   103     {
       
   104     if ( !aNode ||
       
   105          !iDateText ||
       
   106          ( aNode && aNode->Rect().IsEmpty() ) )
       
   107         {
       
   108         return;
       
   109         }
       
   110     
       
   111     const CAknLayoutFont* dateFont( CreateFontL( *aNode ) );
       
   112     if ( !dateFont )
       
   113         {
       
   114         return;
       
   115         }
       
   116     
       
   117     const TRgb& color( CreateColorL( *aNode ) );
       
   118     
       
   119     CGraphicsContext::TTextAlign align = iAdapter.GetTextAlignL( *aNode );
       
   120     
       
   121     const TInt deltaHeight( aNode->Rect().Height() - dateFont->TextPaneHeight() );
       
   122     TInt offset( dateFont->TextPaneTopToBaseline() + deltaHeight / 2 );
       
   123     
       
   124     aGc.SetPenColor( color );
       
   125     aGc.UseFont( dateFont );
       
   126     aGc.DrawText( *iDateText, aNode->Rect(), offset, align );
       
   127     aGc.DiscardFont();
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CXnDateControl::ConstructDateStringL
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 HBufC* CXnDateControl::ConstructDateStringL()
       
   135     {
       
   136     // Construct date string using date format from resource file
       
   137     CCoeEnv* coeEnv = CCoeEnv::Static();
       
   138 
       
   139     if( !coeEnv )
       
   140         {
       
   141         User::Leave( KErrNotReady );
       
   142         }
       
   143 
       
   144     TTime time;
       
   145     time.HomeTime();
       
   146 
       
   147     HBufC* aiDateString = HBufC::NewLC( KMaxDateStringLength );
       
   148     HBufC* aiDateFormatString = HBufC::NewLC( KMaxDateStringLength );
       
   149     aiDateFormatString->Des().Copy( KTimeFormat() );
       
   150     // TODO: when localisation is ready read format string from resource
       
   151     //HBufC* aiDateFormatString = StringLoader::LoadLC( R_ACTIVEIDLE_TIME_FORMAT,
       
   152     //                                                    coeEnv );
       
   153     HBufC* dateStringBuf = HBufC::NewLC( KMaxDateStringLength );
       
   154     HBufC* dateFormatString = StringLoader::LoadLC( R_QTN_DATE_SHORT_WITH_ZERO,
       
   155                                                     coeEnv );
       
   156     TPtr dateString( dateStringBuf->Des() );    
       
   157     time.FormatL( dateString, *dateFormatString );
       
   158     CleanupStack::PopAndDestroy( dateFormatString );
       
   159 
       
   160     //now dateString contains string which is formatted using
       
   161     //R_QTN_DATE_USUAL_WITH_ZERO
       
   162 
       
   163     // To arabic
       
   164     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( dateString );
       
   165 
       
   166     TPtr aiDateStringPtr = aiDateString->Des();
       
   167     
       
   168     TDayNameAbb wkDayAbb = TDayNameAbb();
       
   169     wkDayAbb.Set(time.DayNoInWeek());
       
   170         
       
   171     //add date to string
       
   172     StringLoader::Format( aiDateStringPtr, *aiDateFormatString, 1,dateString );
       
   173 
       
   174     //reuse dateString
       
   175     dateString.Copy( aiDateStringPtr );
       
   176 
       
   177     //add weekday to string
       
   178     StringLoader::Format( aiDateStringPtr, dateString, 0, wkDayAbb );
       
   179 
       
   180     CleanupStack::PopAndDestroy( dateStringBuf );//dateStringBuf, aiDateFormatString
       
   181     CleanupStack::PopAndDestroy( aiDateFormatString );
       
   182 
       
   183     CleanupStack::Pop( aiDateString );
       
   184     return aiDateString;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CXnDateControl::ConstructDayStringL
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 HBufC* CXnDateControl::ConstructDayStringL()
       
   192     {
       
   193     TTime time;
       
   194     time.HomeTime();
       
   195 
       
   196     HBufC* aiDayString = HBufC::NewLC( KMaxDayNumberStringLength );
       
   197     aiDayString->Des().Num( time.DayNoInMonth() + 1 );
       
   198     // To arabic
       
   199     TPtr dayStringPtr = aiDayString->Des();
       
   200     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( dayStringPtr );
       
   201     CleanupStack::Pop( aiDayString );
       
   202     return aiDayString;
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CXnDateControl::CreateFontL
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 const CAknLayoutFont* CXnDateControl::CreateFontL( CXnNodePluginIf& aNode )
       
   210     {    
       
   211     if ( !iDateFont )
       
   212         {
       
   213         iAdapter.CreateFontL( aNode, KDateFont, iDateFont );
       
   214         }
       
   215     return CAknLayoutFont::AsCAknLayoutFontOrNull( iDateFont );
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CXnDateControl::CreateColorL
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 const TRgb& CXnDateControl::CreateColorL( CXnNodePluginIf& aNode )
       
   223     {
       
   224     if ( !iIsColorSet )
       
   225         {
       
   226         iAdapter.CreateColorL( aNode, iDateColor );
       
   227         iIsColorSet = ETrue;
       
   228         }
       
   229     return iDateColor;
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CXnDateControl::ResetFont
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CXnDateControl::ResetFont()
       
   237     {
       
   238     iDateFont = NULL;
       
   239     iIsColorSet = EFalse;
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CXnDateControl::RefreshDateL
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CXnDateControl::RefreshDateL()
       
   247     {
       
   248     delete iDateText;
       
   249     iDateText = NULL;
       
   250     if ( iDateType == EDate )
       
   251         {
       
   252         iDateText = ConstructDateStringL();
       
   253         }
       
   254     else if ( iDateType == EDay )
       
   255         {
       
   256         iDateText = ConstructDayStringL();
       
   257         }
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CXnDateControl::HandleCallBackL
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 TInt CXnDateControl::HandleCallBackL( TAny *aPtr )
       
   265     {
       
   266     CXnDateControl* self = static_cast<CXnDateControl*>( aPtr );
       
   267     
       
   268     if( self )
       
   269         {
       
   270         TInt changes( self->iEnvNotifier->Change() );
       
   271         
       
   272         if ( changes & 
       
   273             ( EChangesLocale | EChangesMidnightCrossover | EChangesSystemTime ) )
       
   274             {
       
   275             self->RefreshDateL();
       
   276             }       
       
   277         }    
       
   278         
       
   279     return KErrNone;
       
   280     }
       
   281 
       
   282 //  End of File