commondrm/drmencryptor/src/DRMEncryptorContainer.cpp
changeset 48 c68061d56109
parent 47 5ed7931150e9
child 49 69d8e75812b7
equal deleted inserted replaced
47:5ed7931150e9 48:c68061d56109
     1 /*
       
     2 * Copyright (c) 2002-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 "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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <eiksbfrm.h>
       
    22 #include <eikscrlb.h>
       
    23 #include <eikrted.h>
       
    24 #include <txtrich.h>
       
    25 #include <barsread.h>
       
    26 #include <eikenv.h>
       
    27 #include <aknenv.h>
       
    28 #include <AknUtils.h>
       
    29 #include <aknconsts.h>
       
    30 #include <txtfrmat.h>
       
    31 #include <AknBidiTextUtils.h>
       
    32 
       
    33 #include <DRMEncryptor.rsg>
       
    34 #include "DRMEncryptorContainer.h"
       
    35 #include "DRMEncryptorImage.h"
       
    36 #include "DRMEncryptor.hrh"
       
    37 
       
    38 // CONSTANTS
       
    39 
       
    40 _LIT( KDRMEncryptorPanicCategory, "DRMEncryptor" );
       
    41 
       
    42 enum TDRMEncryptorPanic
       
    43     {
       
    44     EDRMEncryptorNotSupported = 0
       
    45     };
       
    46 
       
    47 // constructors
       
    48 
       
    49 CDRMEncryptorContainer::CDRMEncryptorContainer()
       
    50     {
       
    51     }
       
    52 
       
    53 void CDRMEncryptorContainer::ConstructL( const TRect& aRect )
       
    54     {
       
    55     CreateWindowL();
       
    56 
       
    57     // In case of APAC layout, use APAC font
       
    58     TAknLayoutId layoutId;
       
    59     iAvkonEnv->GetCurrentLayoutId( layoutId );
       
    60 
       
    61     if ( layoutId == EAknLayoutIdAPAC )
       
    62         {
       
    63         iFont = ApacPlain12();
       
    64         }
       
    65     else
       
    66         {
       
    67         iFont = LatinPlain12();
       
    68         }
       
    69 
       
    70     // Calculate various text positioning parameters
       
    71     iBaseLineDelta = iFont->HeightInPixels() * 4 / 3;
       
    72 
       
    73     TInt mainPaneWidth( aRect.iBr.iX - aRect.iTl.iX );
       
    74     TInt mainPaneHeight( aRect.iBr.iY - aRect.iTl.iY );
       
    75     // Line width is 87% of client rect, horizontal margins 13%
       
    76     iLineWidth = mainPaneWidth * 87 / 100;
       
    77 
       
    78     iTopBaseLineX = ( mainPaneWidth - iLineWidth ) / 2;
       
    79 
       
    80     // top margin is 6.5% of the client rect
       
    81     TInt topMargin = mainPaneHeight * 65 / 1000;
       
    82     iTopBaseLineY = topMargin + iFont->AscentInPixels();
       
    83 
       
    84     // minimum bottom margin is 3% of the client rect
       
    85     TInt bottomMargin = mainPaneHeight * 3 / 100;
       
    86     iLinesPerScreen =
       
    87         ( mainPaneHeight - topMargin - bottomMargin ) / iBaseLineDelta;
       
    88 
       
    89     iTextAlign = CGraphicsContext::ELeft;
       
    90 
       
    91     // Every text line on screen is one entry in this array
       
    92     iText = new( ELeave ) CArrayPtrFlat<HBufC>( 20 );
       
    93     // Every image on screen is one entry in this array
       
    94     iImages = new( ELeave ) CArrayPtrFlat<CDRMEncryptorImage>( 1 );
       
    95     // This array contains indices for lines that start the subsequent
       
    96     // screens, for custom scrolling
       
    97     iScreenStarts = new( ELeave ) CArrayFixFlat<TInt>( 5 );
       
    98     // Initialisation: first screen starts at line 0.
       
    99     iScreenStarts->AppendL( 0 );
       
   100 
       
   101     // Read text and image items to be shown on the screen from a resource file.
       
   102 
       
   103     // real resource
       
   104     TResourceReader reader;
       
   105     iEikonEnv->CreateResourceReaderLC( reader, R_DRMENCRYPTOR_MAIN_TEXT );
       
   106 
       
   107     TInt numItems( reader.ReadInt16() );
       
   108 
       
   109     for ( TInt i = 0 ; i < numItems ; i++ )
       
   110         {
       
   111         TInt type = reader.ReadInt8();
       
   112 
       
   113         if ( type == EDRMEncryptorTextItem )
       
   114             {
       
   115             HBufC* text = iEikonEnv->AllocReadResourceLC( reader.ReadInt32() );
       
   116     SetTextL( *text );
       
   117 
       
   118 
       
   119             CleanupStack::PopAndDestroy(); // text
       
   120             }
       
   121         else if ( type == EDRMEncryptorImageItem )
       
   122             {
       
   123             TPtrC bitmapFile = reader.ReadTPtrC();
       
   124             TInt bitmapId = reader.ReadInt16();
       
   125             SetImageL( bitmapFile, bitmapId );
       
   126             }
       
   127         else
       
   128             {
       
   129             User::Panic( KDRMEncryptorPanicCategory, EDRMEncryptorNotSupported );
       
   130             }
       
   131         }
       
   132 
       
   133     CleanupStack::PopAndDestroy(); // reader
       
   134 
       
   135     UpdateScrollIndicatorL();
       
   136     SetRect( aRect );
       
   137     ActivateL();
       
   138     }
       
   139 
       
   140 // destructor
       
   141 
       
   142 CDRMEncryptorContainer::~CDRMEncryptorContainer()
       
   143     {
       
   144     delete iSBFrame;
       
   145     delete iScreenStarts;
       
   146 
       
   147     if ( iText )
       
   148         {
       
   149         iText->ResetAndDestroy();
       
   150         delete iText;
       
   151         }
       
   152 
       
   153     if ( iImages )
       
   154         {
       
   155         iImages->ResetAndDestroy();
       
   156         delete iImages;
       
   157         }
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CDRMEncryptorContainer::Draw()
       
   162 // -----------------------------------------------------------------------------
       
   163 
       
   164 void CDRMEncryptorContainer::Draw( const TRect& aRect ) const
       
   165     {
       
   166     CWindowGc& gc = SystemGc();
       
   167 
       
   168     //  clear the area
       
   169 
       
   170     gc.SetBrushColor( iEikonEnv->ControlColor( EColorWindowBackground, *this ) );
       
   171     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   172     gc.Clear( aRect );
       
   173 
       
   174     // draw text
       
   175     gc.UseFont( iFont );
       
   176 
       
   177     // index of the first line on the screen in the text array
       
   178     TInt firstLine( (*iScreenStarts)[ iCurrentScreen ] );
       
   179     // index of the last line on the screen in the text array
       
   180     TInt lastLine( firstLine + iLinesPerScreen - 1 );
       
   181 
       
   182     gc.SetBrushStyle( CGraphicsContext::ENullBrush );
       
   183     TPoint position( iTopBaseLineX, iTopBaseLineY );
       
   184     TPoint topLeft;
       
   185     TSize rectSize( iLineWidth, iBaseLineDelta +iFont->DescentInPixels() );
       
   186 
       
   187     for ( TInt index = firstLine ;
       
   188           index < iText->Count() && index <= lastLine ;
       
   189           index++, position.iY += iBaseLineDelta )
       
   190         {
       
   191         HBufC* text = (*iText)[ index ];
       
   192 
       
   193         if ( text )
       
   194             {
       
   195             topLeft = TPoint( position.iX, position.iY-iBaseLineDelta );
       
   196             gc.DrawText( *text,
       
   197                          TRect( topLeft, rectSize ),
       
   198                          iBaseLineDelta,
       
   199                          iTextAlign );
       
   200             }
       
   201         }
       
   202 
       
   203     gc.DiscardFont();
       
   204 
       
   205     // draw images
       
   206 
       
   207     for ( TInt i = 0 ; i < iImages->Count() ; i++ )
       
   208         {
       
   209         CDRMEncryptorImage* image = (*iImages)[ i ];
       
   210 
       
   211         // If part of the image resides in visible lines, draw it.
       
   212         if ( image->StartLine() <= lastLine && image->EndLine() >= firstLine )
       
   213             {
       
   214             position.SetXY( iTopBaseLineX, iTopBaseLineY );
       
   215             position.iY += ( image->StartLine() - firstLine ) * iBaseLineDelta;
       
   216 
       
   217             position.iY -= iBaseLineDelta - iFont->DescentInPixels();
       
   218             // Now iY is the top line of rectangle where the picture is
       
   219             // centered in.
       
   220             position.iY += ( (image->Lines()+1) * iBaseLineDelta -
       
   221                              iFont->HeightInPixels() -
       
   222                              image->HeightInPixels() ) / 2;
       
   223 
       
   224             // If text is right-aligned, also align images to the right.
       
   225 
       
   226             if ( iTextAlign == CGraphicsContext::ERight )
       
   227                 {
       
   228                 position.iX += ( iLineWidth - image->WidthInPixels() );
       
   229                 }
       
   230 
       
   231             gc.BitBlt( position, image->Bitmap(), aRect );
       
   232             }
       
   233         }
       
   234 
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CDRMEncryptorContainer::ActivateL()
       
   239 // -----------------------------------------------------------------------------
       
   240 
       
   241 void CDRMEncryptorContainer::ActivateL()
       
   242     {
       
   243     CCoeControl::ActivateL();
       
   244     UpdateScrollIndicatorL();
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CDRMEncryptorContainer::SetTextL()
       
   249 // -----------------------------------------------------------------------------
       
   250 
       
   251 void CDRMEncryptorContainer::SetTextL( const TDesC& aText )
       
   252     {
       
   253     CArrayFix<TPtrC>* wrappedArray =
       
   254         new( ELeave ) CArrayFixFlat<TPtrC>( 10 );
       
   255 
       
   256     CleanupStack::PushL( wrappedArray );
       
   257 
       
   258     HBufC* dataToDestroy =
       
   259         AknBidiTextUtils::ConvertToVisualAndWrapToArrayL(
       
   260             aText, iLineWidth, *iFont, *wrappedArray
       
   261         );
       
   262 
       
   263     TInt numLines( wrappedArray->Count() );
       
   264     for ( TInt i = 0 ; i < numLines ; i++ )
       
   265         {
       
   266         HBufC* line = (*wrappedArray)[i].AllocLC();
       
   267 
       
   268         if(!line->Length())
       
   269             {
       
   270             iText->AppendL( NULL );
       
   271 
       
   272             CleanupStack::PopAndDestroy();  // line
       
   273             }
       
   274         else
       
   275             {
       
   276             iText->AppendL( line );
       
   277             CleanupStack::Pop();  // line
       
   278             }
       
   279         }
       
   280     iText->AppendL( NULL );
       
   281 
       
   282     // If the last char was newline, add one extra, since
       
   283     // wrapping automatically removes it.
       
   284     if ( aText[ aText.Length() - 1 ] == '\n' )
       
   285         {
       
   286         iText->AppendL( NULL );
       
   287         }
       
   288 
       
   289     CleanupStack::PopAndDestroy(); // wrappedArray
       
   290     delete dataToDestroy;
       
   291 
       
   292     // update screen scrolling info array
       
   293 
       
   294     TInt lastLine( iText->Count() - 1 );
       
   295     TInt screenStart( (*iScreenStarts)[ iScreenStarts->Count() - 1 ] );
       
   296 
       
   297     TBool firstNewScreenHandled( EFalse );
       
   298 
       
   299     while ( lastLine >= screenStart + iLinesPerScreen )
       
   300         {
       
   301         if ( !firstNewScreenHandled && iDoNotShowLastLineAgain )
       
   302             {
       
   303             screenStart++;
       
   304             firstNewScreenHandled = ETrue;
       
   305             }
       
   306 
       
   307         screenStart += iLinesPerScreen - 1;
       
   308         iScreenStarts->AppendL( screenStart );
       
   309         }
       
   310 
       
   311     // if text, last line is shown again in next screen
       
   312     iDoNotShowLastLineAgain = EFalse;
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CDRMEncryptorContainer::SetImageL()
       
   317 // -----------------------------------------------------------------------------
       
   318 
       
   319 void CDRMEncryptorContainer::SetImageL( const TDesC& aFileName, TInt aBitmapId )
       
   320     {
       
   321     TInt firstLineOfImage( iText->Count() );
       
   322 
       
   323     CDRMEncryptorImage* image =
       
   324     CDRMEncryptorImage::NewLC( aFileName, aBitmapId, firstLineOfImage, iBaseLineDelta );
       
   325 
       
   326     // new lines to make room for the picture
       
   327 
       
   328     for ( TInt i = 0 ; i < image->Lines() ; i++ )
       
   329         {
       
   330         iText->AppendL( NULL );
       
   331         }
       
   332 
       
   333     iImages->AppendL( image );
       
   334     CleanupStack::Pop(); // image
       
   335 
       
   336     // update screen scrolling info array
       
   337 
       
   338     TInt lastLineOfImage( iText->Count() - 1 );
       
   339     TInt screenStart( (*iScreenStarts)[ iScreenStarts->Count() - 1 ] );
       
   340 
       
   341     TBool firstNewScreenHandled( EFalse );
       
   342 
       
   343     // If the image was not fully shown in the first screen,
       
   344     // start the next screen with the image.
       
   345 
       
   346     if ( firstLineOfImage < screenStart + iLinesPerScreen &&
       
   347          lastLineOfImage >= screenStart + iLinesPerScreen )
       
   348         {
       
   349         screenStart = firstLineOfImage;
       
   350         iScreenStarts->AppendL( screenStart );
       
   351         firstNewScreenHandled = ETrue;
       
   352         }
       
   353 
       
   354     while ( lastLineOfImage >= screenStart + iLinesPerScreen )
       
   355         {
       
   356         if ( !firstNewScreenHandled && iDoNotShowLastLineAgain )
       
   357             {
       
   358             screenStart++;
       
   359             firstNewScreenHandled = ETrue;
       
   360             }
       
   361 
       
   362         screenStart += iLinesPerScreen - 1;
       
   363         iScreenStarts->AppendL( screenStart );
       
   364         }
       
   365 
       
   366     if ( lastLineOfImage == screenStart + iLinesPerScreen - 1 )
       
   367         {
       
   368         iDoNotShowLastLineAgain = ETrue;
       
   369         }
       
   370     else
       
   371         {
       
   372         iDoNotShowLastLineAgain = EFalse;
       
   373         }
       
   374     }
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // CDRMEncryptorContainer::OfferKeyEventL()
       
   378 // -----------------------------------------------------------------------------
       
   379 
       
   380 TKeyResponse CDRMEncryptorContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent,
       
   381                                               TEventCode aType )
       
   382     {
       
   383     if ( aType == EEventKey && iScreenStarts->Count() > 1 )
       
   384         {
       
   385         switch ( aKeyEvent.iCode )
       
   386             {
       
   387             case EKeyUpArrow:
       
   388                 if ( iCurrentScreen > 0 )
       
   389                     {
       
   390                     iCurrentScreen--;
       
   391                     DrawNow();
       
   392                     UpdateScrollIndicatorL();
       
   393                     }
       
   394                 break;
       
   395 
       
   396             case EKeyDownArrow:
       
   397                 if ( iCurrentScreen < iScreenStarts->Count() - 1 )
       
   398                     {
       
   399                     iCurrentScreen++;
       
   400                     DrawNow();
       
   401                     UpdateScrollIndicatorL();
       
   402                     }
       
   403                 break;
       
   404 
       
   405             default:
       
   406                 break;
       
   407             }
       
   408         }
       
   409 
       
   410     return EKeyWasConsumed;
       
   411     }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CDRMEncryptorContainer::UpdateScrollIndicatorL()
       
   415 // -----------------------------------------------------------------------------
       
   416 
       
   417 void CDRMEncryptorContainer::UpdateScrollIndicatorL()
       
   418     {
       
   419     if ( iScreenStarts->Count() <= 1 )
       
   420         {
       
   421         return;
       
   422         }
       
   423 
       
   424     if ( !iSBFrame )
       
   425         {
       
   426         iSBFrame = new( ELeave ) CEikScrollBarFrame( this, NULL, ETrue );
       
   427         iSBFrame->SetScrollBarVisibilityL( CEikScrollBarFrame::EOff,
       
   428                                            CEikScrollBarFrame::EAuto );
       
   429         iSBFrame->SetTypeOfVScrollBar( CEikScrollBarFrame::EArrowHead );
       
   430         }
       
   431 
       
   432     TEikScrollBarModel hSbarModel;
       
   433     TEikScrollBarModel vSbarModel;
       
   434     vSbarModel.iThumbPosition = iCurrentScreen;
       
   435     vSbarModel.iScrollSpan = iScreenStarts->Count();
       
   436     vSbarModel.iThumbSpan = 1;
       
   437 
       
   438     TEikScrollBarFrameLayout layout;
       
   439     TRect rect( Rect() );
       
   440     iSBFrame->TileL( &hSbarModel, &vSbarModel, rect, rect, layout );
       
   441     iSBFrame->SetVFocusPosToThumbPos( vSbarModel.iThumbPosition );
       
   442     }
       
   443 
       
   444 // End of File