src/screensaverindicatorarray.cpp
changeset 0 040fcad49f44
equal deleted inserted replaced
-1:000000000000 0:040fcad49f44
       
     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:   CScreensaverIndicatorArray implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <barsread.h>
       
    21 #include <screensaver.rsg>
       
    22 #include <AknUtils.h>
       
    23 
       
    24 #include "screensaverindicatorarray.h"
       
    25 #include "screensaverindicatornumberandicon.h"
       
    26 #include "screensaverindicatortext.h"
       
    27 #include "screensaverindicatoricon.h"
       
    28 #include "screensaverutility.h"
       
    29 
       
    30 const TInt KInvertedColors = 1;
       
    31 
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CScreensaverIndicatorArray::NewL
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CScreensaverIndicatorArray* CScreensaverIndicatorArray::NewL( TBool aColorInversionUsed )
       
    38     {
       
    39     CScreensaverIndicatorArray* array = new(ELeave) CScreensaverIndicatorArray();
       
    40     CleanupStack::PushL(array);
       
    41     array->ConstructL( aColorInversionUsed );
       
    42     CleanupStack::Pop(); // array
       
    43 
       
    44     return array;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CScreensaverIndicatorArray::~CScreensaverIndicatorArray
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CScreensaverIndicatorArray::~CScreensaverIndicatorArray()
       
    52     {
       
    53     if (iIndicators)
       
    54         {
       
    55         iIndicators->ResetAndDestroy();
       
    56         }
       
    57     delete iIndicators;
       
    58     delete iDependencyRecord;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CScreensaverIndicatorArray::NewIndicatorLC
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CScreensaverIndicator *CScreensaverIndicatorArray::NewIndicatorLC(
       
    66         TScreensaverIndicatorType aId)
       
    67     {
       
    68     CScreensaverIndicator *result= NULL;
       
    69 
       
    70     switch (aId)
       
    71         {
       
    72         case ESsIconIndicator:
       
    73             result = new( ELeave ) CScreensaverIndicatorIcon();
       
    74             break;
       
    75         case ESsTextIndicator:
       
    76             result = new( ELeave ) CScreensaverIndicatorText();
       
    77             break;
       
    78         case ESsNumberAndIconIndicator:
       
    79             result = new( ELeave ) CScreensaverIndicatorNumberAndIcon();
       
    80             break;
       
    81         default:
       
    82             User::Leave(KErrCorrupt);
       
    83         }
       
    84 
       
    85     CleanupStack::PushL( result );
       
    86     result->SetType(aId);
       
    87 
       
    88     return result;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CScreensaverIndicatorArray::ConstructL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CScreensaverIndicatorArray::ConstructL( TBool aColorInversionUsed )
       
    96     {
       
    97     if (aColorInversionUsed == KInvertedColors)
       
    98         {
       
    99         iBgColor = TRgb(KRgbBlack).Value();
       
   100         iTextColor = TRgb(KRgbWhite).Value();
       
   101         }
       
   102     else //aColorInversionUsed == KNormalColors
       
   103         {
       
   104         iBgColor = TRgb(KRgbWhite).Value();
       
   105         iTextColor = TRgb(KRgbBlack).Value();
       
   106         }
       
   107     
       
   108     ConstructFromResourceL();
       
   109 
       
   110     }
       
   111 
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CScreensaverIndicatorArray::ConstructFromResourceL
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void CScreensaverIndicatorArray::ConstructFromResourceL()
       
   118     {
       
   119     ConstructParamFromResourceL();
       
   120 
       
   121     ConstructIndArrayL();
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CScreensaverIndicatorArray::ConstructParamFromResourceL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CScreensaverIndicatorArray::ConstructParamFromResourceL()
       
   129     {
       
   130     TResourceReader reader;
       
   131 
       
   132     CEikonEnv::Static()->CreateResourceReaderLC( reader, R_SCREENSAVER_PARAMETERS );
       
   133     iMargin = reader.ReadInt16();
       
   134     iGap = reader.ReadInt16();
       
   135     iTopMargin = reader.ReadInt16();
       
   136     iTextLevel = reader.ReadInt16();
       
   137     iHeight = reader.ReadInt16();
       
   138     CleanupStack::PopAndDestroy(); // reader
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CScreensaverIndicatorArray::ConstructIndArrayL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CScreensaverIndicatorArray::ConstructIndArrayL()
       
   146     {
       
   147     if( AknLayoutUtils::ScalableLayoutInterfaceAvailable() )
       
   148         {//use the new layout
       
   149         ConstructIndicatorsL( R_SCREENSAVER_INDICATOR_ARRAY_NEW_LAYOUT );
       
   150         }
       
   151     else
       
   152         {//use the old layout
       
   153         ConstructIndicatorsL( R_SCREENSAVER_INDICATOR_ARRAY );
       
   154         }
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CScreensaverIndicatorArray::ConstructIndicatorsL
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CScreensaverIndicatorArray::ConstructIndicatorsL( TInt aResourceId )
       
   162     {
       
   163     TResourceReader reader;
       
   164 
       
   165     CEikonEnv::Static()->CreateResourceReaderLC(
       
   166         reader, aResourceId);
       
   167     
       
   168     iCount = reader.ReadInt16();
       
   169 
       
   170     iIndicators = new( ELeave ) CArrayPtrFlat<CScreensaverIndicator>( iCount );
       
   171     iDependencyRecord = new( ELeave ) CArrayFixFlat<TBool>( iCount );
       
   172 
       
   173     CScreensaverIndicator *nInd;
       
   174     TInt32 type;
       
   175     for (TInt i = 0; i < iCount; i++)
       
   176         {
       
   177         iDependencyRecord->AppendL( ETrue );
       
   178         type = reader.ReadInt32();
       
   179         nInd = NewIndicatorLC( ( TScreensaverIndicatorType ) type );
       
   180         nInd->ConstructL( reader, iBgColor, iTextColor );
       
   181         iIndicators->InsertL( 0, nInd );
       
   182         CleanupStack::Pop(); // nInd
       
   183         }
       
   184 
       
   185     CleanupStack::PopAndDestroy(); // reader
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CScreensaverIndicatorArray::Dependency
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TBool CScreensaverIndicatorArray::Dependency(TInt aIndex) const
       
   193     {
       
   194     return (*iDependencyRecord)[aIndex];
       
   195     }
       
   196 // -----------------------------------------------------------------------------
       
   197 // CScreensaverIndicatorArray::Setup
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CScreensaverIndicatorArray::Setup( TAknLayoutRect& aLayoutRect,
       
   201         TAknLayoutText& aLayoutText, TAknLayoutRect& aLayoutInds )
       
   202     {
       
   203     // Reset drawing parameters (do not use those defined in resources)
       
   204     iRect = aLayoutRect.Rect();
       
   205 
       
   206     TInt rightMargin = 0;
       
   207     // Left margin = indicators left edge - indicator area left edge
       
   208     // Right margin = 0, the AM/PM indicator needs to be on the edge
       
   209     // Except when mirrored the other way around. Right margin can be reduced
       
   210     // by 1 when mirrored, because the icons contain a border on top & right 
       
   211     if ( AknLayoutUtils::LayoutMirrored() )
       
   212         {
       
   213         iMargin = 0;
       
   214         rightMargin = aLayoutInds.Rect().iTl.iX - iRect.iTl.iX - 1;
       
   215         }
       
   216     else
       
   217         {
       
   218         iMargin = aLayoutInds.Rect().iTl.iX - iRect.iTl.iX;
       
   219         rightMargin = 0;
       
   220         }
       
   221 
       
   222     TInt currentX = iRect.iBr.iX - rightMargin;
       
   223 
       
   224     TInt leftmostRightAlignedBorder = iRect.iBr.iX - rightMargin; // iMargin;
       
   225 
       
   226     // This is done in two passes, first we set up right-aligned indicators
       
   227     // and then left-aligned. If any left-aligned indicator overlaps with leftmost
       
   228     // right-aligned indicator the the process is stopped (right-aligned
       
   229     // indicators have higher priority).
       
   230     for ( TInt i = 0; i < iIndicators->Count(); i++ )
       
   231         {
       
   232         if (iIndicators->At(i)->Alignment() != ESsAlignRight)
       
   233             {
       
   234             continue;
       
   235             }
       
   236         
       
   237         SetIndicatorsAttribute( i, aLayoutText, aLayoutInds, currentX );
       
   238 
       
   239         if (iIndicators->At(i)->Visible())
       
   240             {
       
   241             currentX -= iIndicators->At(i)->Width() + iGap;
       
   242             leftmostRightAlignedBorder = currentX;
       
   243             if (currentX < iMargin)
       
   244                 {
       
   245                 break;
       
   246                 }
       
   247             }
       
   248         }
       
   249 
       
   250     // Start second round from left edge
       
   251     currentX = iRect.iTl.iX + iMargin;
       
   252     for ( TInt i = 0; i < iIndicators->Count(); i++ )
       
   253         {
       
   254         if (iIndicators->At(i)->Alignment() != ESsAlignLeft)
       
   255             {
       
   256             continue;
       
   257             }
       
   258         
       
   259         SetIndicatorsAttribute( i, aLayoutText, aLayoutInds, currentX );
       
   260 
       
   261         if (iIndicators->At(i)->Visible())
       
   262             {
       
   263             if (currentX + iIndicators->At(i)->Width() + iGap >= leftmostRightAlignedBorder)
       
   264                 {
       
   265                 iIndicators->At(i)->SetVisibility(EFalse);
       
   266                 break;
       
   267                 }
       
   268             currentX += iIndicators->At(i)->Width() + iGap;
       
   269             if (currentX > iRect.Width() - iMargin)
       
   270                 {
       
   271                 break;
       
   272                 }
       
   273             }
       
   274         }
       
   275     }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CScreensaverIndicatorArray::SetIndicatorsAttribute
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void CScreensaverIndicatorArray::SetIndicatorsAttribute( 
       
   282         TInt& Ind,
       
   283         TAknLayoutText& aLayoutText,
       
   284         TAknLayoutRect& aLayoutInds,
       
   285         TInt& aCurrentX )
       
   286     {
       
   287 
       
   288     // Set text layout for text containing indicators
       
   289     iIndicators->At(Ind)->SetTextLayout(aLayoutText, aCurrentX);
       
   290 
       
   291     // Set icon layout for icon containing indicators
       
   292     iIndicators->At(Ind)->SetIconLayout(aLayoutInds, aCurrentX);
       
   293 
       
   294     // Not visible, if conditions (e.g. there's icon & payload) are not met,
       
   295     // or there's no reason to show the indicator
       
   296     if (!iIndicators->At(Ind)->CheckVisibilityConditions() || Dependency(iIndicators->At(Ind)->Id()))
       
   297         {
       
   298         iIndicators->At(Ind)->SetVisibility(EFalse);        
       
   299         }
       
   300     }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CScreensaverIndicatorArray::Draw
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 void CScreensaverIndicatorArray::Draw(CWindowGc& aGc) const
       
   307     {
       
   308     aGc.SetBrushColor(iBgColor);
       
   309     aGc.SetPenColor(iTextColor);
       
   310     aGc.Clear(iRect);
       
   311 
       
   312     for (TInt i = 0; i < iIndicators->Count(); i++)
       
   313         {
       
   314         if (iIndicators->At(i)->Visible())
       
   315             {
       
   316             iIndicators->At(i)->Draw(aGc);
       
   317             }
       
   318         }
       
   319     }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CScreensaverIndicatorArray::CheckDependencyConflict
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 TBool CScreensaverIndicatorArray::CheckDependencyConflict(
       
   326         TScreensaverIndicatorId aId) const
       
   327     {
       
   328     if ((iDependencyRecord) && (aId < iCount))
       
   329         {
       
   330         return Dependency(aId);
       
   331         }
       
   332 
       
   333     return EFalse;
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CScreensaverIndicatorArray::SetDependencyStatus
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 void CScreensaverIndicatorArray::SetDependencyStatus(
       
   341         TScreensaverIndicatorId aId, TBool aValue)
       
   342     {
       
   343     if ((iDependencyRecord) && (aId < iCount))
       
   344         {
       
   345         (*iDependencyRecord)[aId] = aValue;
       
   346         }
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CScreensaverIndicatorArray::GetIndicatorPayload
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 TInt CScreensaverIndicatorArray::GetIndicatorPayload(
       
   354         TScreensaverIndicatorId aId, TIndicatorPayload& aPayload) const
       
   355     {
       
   356     if (aId < iCount)
       
   357         {
       
   358         for (TInt i = 0; i < iIndicators->Count(); i++)
       
   359             {
       
   360             if (aId == iIndicators->At(i)->Id())
       
   361                 {
       
   362                 iIndicators->At(i)->Payload(aPayload);
       
   363                 return KErrNone;
       
   364                 }
       
   365             }
       
   366         }
       
   367 
       
   368     return KErrCorrupt;
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CScreensaverIndicatorArray::SetIndicatorPayload
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 TInt CScreensaverIndicatorArray::SetIndicatorPayload(
       
   376         TScreensaverIndicatorId aId, const TIndicatorPayload& aPayload)
       
   377     {
       
   378     if (aId < iCount)
       
   379         {
       
   380         for (TInt i = 0; i < iIndicators->Count(); i++)
       
   381             {
       
   382             if (aId == iIndicators->At(i)->Id())
       
   383                 {
       
   384                 iIndicators->At(i)->SetPayload(aPayload);
       
   385                 return KErrNone;
       
   386                 }
       
   387             }
       
   388         }
       
   389 
       
   390     return KErrCorrupt;
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CScreensaverIndicatorArray::PreferredDrawingMode
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 TScreensaverDrawType CScreensaverIndicatorArray::PreferredDrawingMode() const
       
   398     {
       
   399     TScreensaverDrawType result = ESsDrawTypeNotDrawn; // Default mode.
       
   400 
       
   401     for (TInt i = 0; i < iIndicators->Count(); i++)
       
   402         {
       
   403         if ((iIndicators->At(i)->CheckVisibilityConditions() == EFalse)
       
   404                 || (CheckDependencyConflict(iIndicators->At(i)->Id())))
       
   405             {
       
   406             // This indicator can't be visible or there is 
       
   407             // dependency conflict -> continue.
       
   408             continue;
       
   409             }
       
   410 
       
   411         switch (iIndicators->At(i)->DrawType())
       
   412             {
       
   413             case ESsDrawTypePrimary:
       
   414                 // Primary draw type overides everything,
       
   415                 // so we can return immediately. 
       
   416                 return ESsDrawTypePrimary;
       
   417             case ESsDrawTypeSecondary:
       
   418                 result = ESsDrawTypeSecondary;
       
   419                 break;
       
   420             default:
       
   421                 break;
       
   422             }
       
   423         }
       
   424 
       
   425     return result;
       
   426     }
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 // CScreensaverIndicatorArray::ActualDrawingMode
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 TScreensaverDrawType CScreensaverIndicatorArray::ActualDrawingMode() const
       
   433     {
       
   434     TScreensaverDrawType result = ESsDrawTypeNotDrawn; // Default mode.
       
   435     
       
   436     for (TInt i = 0; i < iIndicators->Count(); i++)
       
   437         {
       
   438         if ((iIndicators->At(i)->CheckVisibilityConditions() == EFalse)
       
   439                 || !( iIndicators->At(i)->Visible() ) )
       
   440             {
       
   441             // This indicator can't be visible or it is visible
       
   442             continue;
       
   443             }
       
   444         
       
   445         switch (iIndicators->At(i)->DrawType())
       
   446             {
       
   447             case ESsDrawTypePrimary:
       
   448                 return ESsDrawTypePrimary; // Primary draw type overides everything.
       
   449             case ESsDrawTypeSecondary:
       
   450                 result = ESsDrawTypeSecondary;
       
   451                 break;
       
   452             default:
       
   453                 break;
       
   454             }
       
   455         }
       
   456 
       
   457     return result;
       
   458     }
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CScreensaverIndicatorArray::Height
       
   462 // -----------------------------------------------------------------------------
       
   463 //
       
   464 TInt CScreensaverIndicatorArray::Height() const
       
   465     {
       
   466     return iHeight;
       
   467     }
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // CScreensaverIndicatorArray::SetVisibilityForIndicators
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 void CScreensaverIndicatorArray::SetVisibilityForIndicators()
       
   474     {
       
   475     for (int i = 0; i < iIndicators->Count(); i++)
       
   476         {
       
   477         // Not visible, if conditions (e.g. there's icon & payload) are not met,
       
   478         // or there's no reason to show the indicator
       
   479         if (!iIndicators->At(i)->CheckVisibilityConditions() || Dependency(iIndicators->At(i)->Id()))
       
   480             {
       
   481             iIndicators->At(i)->SetVisibility(EFalse);
       
   482             }
       
   483         else
       
   484             iIndicators->At(i)->SetVisibility(ETrue);
       
   485         }
       
   486 
       
   487     }
       
   488 
       
   489 // -----------------------------------------------------------------------------
       
   490 // CScreensaverIndicatorArray::CScreensaverIndicatorArray
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 CScreensaverIndicatorArray::CScreensaverIndicatorArray()
       
   494     {
       
   495     
       
   496     }
       
   497 
       
   498 // End of files