mobilemessaging/smilui/mediasrc/SmilImageRenderer.cpp
branchRCL_3
changeset 60 7fdbb852d323
parent 0 72b543305e3a
equal deleted inserted replaced
57:ebe688cedc25 60:7fdbb852d323
       
     1 /*
       
     2 * Copyright (c) 2003-2006 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: SmilImageRenderer implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <coemain.h>
       
    22 #include <smilregioninterface.h>
       
    23 #include <smilpresentation.h>
       
    24 #include <smilmediainterface.h>
       
    25 
       
    26 #include <IHLImageFactory.h>
       
    27 #include <IHLViewerFactory.h>
       
    28 #include <MIHLFileImage.h>
       
    29 #include <MIHLBitmap.h>
       
    30 #include <MIHLImageViewer.h>
       
    31 
       
    32 #include <AknUtils.h>
       
    33 #include <aknlayoutscalable_apps.cdl.h>
       
    34 
       
    35 #include "SmilImageRenderer.h" 
       
    36 
       
    37 #ifdef IMAGE_DEBUG
       
    38     #include "SmilMediaLogging.h"
       
    39 #endif
       
    40 // Scrolling is done in 1/4 steps
       
    41 const TInt KScrollStepFraction = 4;
       
    42 
       
    43 #ifdef RD_SCALABLE_UI_V2
       
    44 const TInt KScrollValue = 1;
       
    45 #endif
       
    46 
       
    47 // Minimum frame delay
       
    48 const TInt KMinimumFrameDelay = 100; // 100 ms
       
    49 const TInt KProcessingDelayOffset = 100; // 100 ms
       
    50 
       
    51 // ================= MEMBER FUNCTIONS =========================================
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // CSmilImageRenderer::CSmilImageRenderer
       
    55 // ----------------------------------------------------------------------------
       
    56 //
       
    57 CSmilImageRenderer::CSmilImageRenderer( MSmilMedia* aMedia,
       
    58                                         DRMCommon& aDrmCommon,
       
    59                                         CDRMHelper& aDrmHelper ) :
       
    60     CSmilMediaRendererBase( EMsgMediaImage, aMedia, aDrmCommon, aDrmHelper ),
       
    61     iTotalLength( 0 ),
       
    62     iPrevState( ENotReady ),
       
    63     iCurrState( ENotReady ),
       
    64     iProcessingDelay( TInt64( 0 ) )
       
    65 #ifdef RD_SCALABLE_UI_V2
       
    66     , iNotYetDrawn( ETrue )
       
    67 #endif    
       
    68     {
       
    69     iSmilObject = NULL;
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // CSmilImageRenderer::ConstructL
       
    74 // Checks neccessary parameters and initializes heap member variables. 
       
    75 // Converts first (and in most cases only) frame of the source image and
       
    76 // calculates renderer duration.
       
    77 // ----------------------------------------------------------------------------
       
    78 //
       
    79 void CSmilImageRenderer::ConstructL( RFile& aFileHandle )
       
    80     {
       
    81 #ifdef IMAGE_DEBUG
       
    82     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
    83     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ConstructL" )
       
    84     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
    85 #endif
       
    86 
       
    87     BaseConstructL( aFileHandle );
       
    88     
       
    89     User::LeaveIfError( CheckDRMRights() );
       
    90 
       
    91     iSourceImage = IHLImageFactory::OpenFileImageL( aFileHandle );
       
    92     
       
    93     iSourceSize = iSourceImage->Size();
       
    94     iAnimation = iSourceImage->IsAnimation();
       
    95     
       
    96     iDestinationBitmap = IHLBitmap::CreateL();
       
    97     
       
    98     InitializeViewerL();
       
    99     
       
   100     SetupScrollingL();
       
   101     
       
   102     if ( iSourceImage->IsAnimation() )
       
   103         {              
       
   104         for( TInt frame = 0; frame < iSourceImage->AnimationFrameCount(); frame++ )
       
   105             {
       
   106             // iTotalLength is on millisecond and delay on microseconds.
       
   107             TInt frameDelay = iSourceImage->AnimationFrameDelay( frame ).Int() / 1000;
       
   108             if ( frameDelay < KMinimumFrameDelay )
       
   109                 {
       
   110                 frameDelay = KMinimumFrameDelay;
       
   111                 }
       
   112             
       
   113             // iProvessingDelay is calculated at InitializeViewerL
       
   114             frameDelay = Max( I64INT( iProcessingDelay.Int64() ), frameDelay );
       
   115             iTotalLength = iTotalLength + frameDelay;
       
   116             }
       
   117         iMedia->RendererDurationChangedL();
       
   118         }
       
   119     else
       
   120         {
       
   121         if ( !iIsScrollable )
       
   122             {
       
   123             // Memory optimization. This is not needed
       
   124             // if image is not scrollable or animation.
       
   125             // Releases 1x bitmap size memory + possible mask
       
   126             delete iEngine;
       
   127             iEngine = NULL;
       
   128             
       
   129             delete iSourceImage;
       
   130             iSourceImage = NULL;
       
   131             }        
       
   132         }
       
   133 #ifdef RD_SCALABLE_UI_V2    
       
   134     CCoeControl* parentControl = iMedia->Presentation()->GetPlayer()->GetControl( iMedia->Presentation() );
       
   135     SetContainerWindowL( *parentControl );
       
   136     TRect controlRect = iMedia->GetRegion()->GetRectangle();
       
   137     controlRect.Move( parentControl->Position() );
       
   138     SetRect( controlRect );
       
   139 #endif    
       
   140         
       
   141 #ifdef IMAGE_DEBUG  
       
   142     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ConstructL" )
       
   143     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   144 #endif
       
   145     }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // CSmilImageRenderer::NewL
       
   149 // ----------------------------------------------------------------------------
       
   150 //
       
   151 CSmilImageRenderer* CSmilImageRenderer::NewL( RFile& aFileHandle,
       
   152                                               MSmilMedia* aMedia,
       
   153                                               DRMCommon& aDrmCommon,
       
   154                                               CDRMHelper& aDrmHelper )
       
   155     {
       
   156 #ifdef IMAGE_DEBUG
       
   157     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   158     SMILUILOGGER_ENTERFN( "[SMILUI] Image: NewL" )
       
   159 #endif
       
   160 
       
   161     CSmilImageRenderer* self = new (ELeave) CSmilImageRenderer( aMedia,
       
   162                                                                 aDrmCommon,
       
   163                                                                 aDrmHelper );
       
   164     
       
   165     CleanupStack::PushL( self );
       
   166     self->ConstructL( aFileHandle );
       
   167     CleanupStack::Pop( self );
       
   168 
       
   169 #ifdef IMAGE_DEBUG
       
   170     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: NewL" )
       
   171 #endif
       
   172 
       
   173     return self;
       
   174     }
       
   175 
       
   176 
       
   177 // ----------------------------------------------------------------------------
       
   178 // CSmilImageRenderer::~CSmilImageRenderer
       
   179 // ----------------------------------------------------------------------------
       
   180 //
       
   181 CSmilImageRenderer::~CSmilImageRenderer()
       
   182     {
       
   183 #ifdef IMAGE_DEBUG
       
   184     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ~CSmilImageRenderer" )
       
   185     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   186 #endif
       
   187 
       
   188     delete iEngine;
       
   189     delete iDestinationBitmap;
       
   190     delete iSourceImage;
       
   191 
       
   192     iMedia = NULL; // For LINT
       
   193 #ifdef RD_SCALABLE_UI_V2    
       
   194     delete iSBFrame;
       
   195 #endif    
       
   196     
       
   197 #ifdef IMAGE_DEBUG
       
   198     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ~CSmilImageRenderer" )
       
   199 #endif
       
   200     }
       
   201 
       
   202 // ----------------------------------------------------------------------------
       
   203 // CSmilImageRenderer::IsVisual
       
   204 // ----------------------------------------------------------------------------
       
   205 //
       
   206 TBool CSmilImageRenderer::IsVisual() const
       
   207     {
       
   208     return ETrue;
       
   209     }
       
   210 
       
   211 // ----------------------------------------------------------------------------
       
   212 // CSmilImageRenderer::IsOpaque
       
   213 // Renderer is determined to be opaque (non-transparent) if
       
   214 // mask is not present.
       
   215 // ----------------------------------------------------------------------------
       
   216 //
       
   217 TBool CSmilImageRenderer::IsOpaque() const
       
   218     {
       
   219     return !iDestinationBitmap->HasMask();
       
   220     }
       
   221 
       
   222 // ----------------------------------------------------------------------------
       
   223 // CSmilImageRenderer::IsScrollable
       
   224 // ----------------------------------------------------------------------------
       
   225 //
       
   226 TBool CSmilImageRenderer::IsScrollable() const
       
   227     {
       
   228     return iIsScrollable;
       
   229     }
       
   230 
       
   231 // ----------------------------------------------------------------------------
       
   232 // CSmilImageRenderer::IsControl
       
   233 // All renderer are specified to be control on the base class.
       
   234 // ----------------------------------------------------------------------------
       
   235 //
       
   236 TBool CSmilImageRenderer::IsControl() const
       
   237     {
       
   238     return ETrue;
       
   239     }
       
   240 
       
   241 // ----------------------------------------------------------------------------
       
   242 // CSmilImageRenderer::IntrinsicWidth
       
   243 // ----------------------------------------------------------------------------
       
   244 //
       
   245 TInt CSmilImageRenderer::IntrinsicWidth() const
       
   246     {
       
   247     return iSourceSize.iWidth;
       
   248     }
       
   249 
       
   250 // ----------------------------------------------------------------------------
       
   251 // CSmilImageRenderer::IntrinsicHeight
       
   252 // ----------------------------------------------------------------------------
       
   253 //
       
   254 TInt CSmilImageRenderer::IntrinsicHeight() const
       
   255     {
       
   256     return iSourceSize.iHeight;
       
   257     }
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // CSmilImageRenderer::IntrinsicDuration
       
   261 // Returns zero if still image otherwise returns sum of animation delays.
       
   262 // Duration is specified on milliseconds.
       
   263 // ----------------------------------------------------------------------------
       
   264 //
       
   265 TSmilTime CSmilImageRenderer::IntrinsicDuration() const
       
   266     {
       
   267 #ifdef IMAGE_DEBUG
       
   268     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   269     SMILUILOGGER_ENTERFN( "[SMILUI] Image: IntrinsicDuration" )
       
   270     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   271     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iTotalLength=%d"), iTotalLength );
       
   272 #endif
       
   273 
       
   274     TSmilTime result( 0 );
       
   275     
       
   276     if ( iAnimation )
       
   277         {
       
   278         result = iTotalLength;
       
   279         }
       
   280 
       
   281 #ifdef IMAGE_DEBUG
       
   282     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: IntrinsicDuration" )
       
   283     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   284 #endif
       
   285 
       
   286     return result;
       
   287     }
       
   288 
       
   289 // ----------------------------------------------------------------------------
       
   290 // CSmilImageRenderer::Draw
       
   291 // Performs drawing only if renderer is visible. Transition filter
       
   292 // performs its own drawings. Scroll bar and focus drawing is performed
       
   293 // on functions inherited from base class.
       
   294 // ----------------------------------------------------------------------------
       
   295 //
       
   296 void CSmilImageRenderer::Draw( CGraphicsContext& aGc, 
       
   297                                const TRect& aRect, 
       
   298                                CSmilTransitionFilter* aTransitionFilter, 
       
   299                                const MSmilFocus* aFocus)
       
   300     {
       
   301 #ifdef IMAGE_DEBUG
       
   302     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   303     SMILUILOGGER_ENTERFN( "[SMILUI] Image: Draw" )
       
   304     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   305     if ( iAnimation )
       
   306         {
       
   307         SMILUILOGGER_WRITEF( _L("[SMILUI] Image: currentFrame=%d"), iEngine->AnimationFrame() );       
       
   308         }
       
   309 #endif
       
   310     
       
   311     if ( iCurrState == EPlaying || iCurrState == EPaused )
       
   312         {
       
   313         aGc.Reset();
       
   314         
       
   315         if ( aTransitionFilter )
       
   316             {
       
   317             const CFbsBitmap* bitmap = NULL;
       
   318             const CFbsBitmap* mask = NULL;
       
   319             bitmap = &iDestinationBitmap->Bitmap();
       
   320             if ( iDestinationBitmap->HasMask() )
       
   321                 {
       
   322                 mask = &iDestinationBitmap->Mask();
       
   323                 }
       
   324             aTransitionFilter->Draw( aGc, 
       
   325                                      aRect, 
       
   326                                      bitmap, 
       
   327                                      iMedia->GetRectangle().iTl, 
       
   328                                      mask );
       
   329             }
       
   330         else        
       
   331             {
       
   332             TRect destinationRect( aRect );
       
   333             destinationRect.Intersection( iMedia->GetRectangle() );
       
   334         
       
   335             // Rectangle that indicates what part of the bitmap should be drawn. 
       
   336             TRect sourceRect( destinationRect );
       
   337         
       
   338             // We have to correct the origin as sourceRect is calculated relative
       
   339             // to the bitmap top left corner coordinates and not the graphical context
       
   340             // top left coordinates into which destinationRect is related.
       
   341             sourceRect.Move( -iMedia->GetRectangle().iTl );
       
   342             
       
   343             // Cast graphics context to CFbsBitGc
       
   344             CBitmapContext* tmpBitmapContext = static_cast<CBitmapContext*>( &aGc );
       
   345                 
       
   346             if ( iDestinationBitmap->HasMask() )
       
   347                 {
       
   348                 tmpBitmapContext->SetBrushStyle( CGraphicsContext::ENullBrush );
       
   349                 }
       
   350             
       
   351             iDestinationBitmap->Draw( *tmpBitmapContext, 
       
   352                                       destinationRect.iTl,
       
   353                                       sourceRect );
       
   354             }
       
   355 
       
   356         if ( iIsScrollable )
       
   357             {
       
   358 #ifdef RD_SCALABLE_UI_V2            
       
   359             TRAP_IGNORE( DrawScrollBarL() );
       
   360 #else            
       
   361             DrawScrollBars( aGc, iMedia->GetRectangle(), iImageScrollRect );
       
   362 #endif            
       
   363             }
       
   364             
       
   365         DrawFocus( aGc, aFocus );
       
   366         }
       
   367       
       
   368 #ifdef IMAGE_DEBUG  
       
   369     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: Draw" )
       
   370 #endif
       
   371     }
       
   372 
       
   373 // ---------------------------------------------------------
       
   374 // CSmilImageRenderer::DrawScrollBarL
       
   375 // ---------------------------------------------------------
       
   376 //
       
   377 void CSmilImageRenderer::DrawScrollBarL()
       
   378     {
       
   379 #ifdef RD_SCALABLE_UI_V2    
       
   380     if ( iNotYetDrawn )
       
   381         {
       
   382         TInt regionHeight = iMedia->GetRectangle().Height();
       
   383         TInt regionWidth = iMedia->GetRectangle().Width();
       
   384         TInt imageHeight = iImageScrollRect.Height();
       
   385         TInt imageWidth = iImageScrollRect.Width();
       
   386         if ( !iSBFrame )
       
   387             {
       
   388             iSBFrame = new (ELeave) CEikScrollBarFrame( this, this, ETrue );
       
   389             iSBFrame->CreateDoubleSpanScrollBarsL(ETrue, EFalse, ETrue, ETrue ); // window owning scrollbar            
       
   390          // using this does not draw sbars correctly
       
   391          // most likely it has something to do with drawing mechanism
       
   392          //   iSBFrame->DrawBackground( EFalse, EFalse );
       
   393          
       
   394                  //calculating the correct scrollspans   
       
   395 
       
   396            
       
   397             TInt initialScrollSpan = imageWidth/regionWidth;
       
   398             initialScrollSpan *= KScrollStepFraction;
       
   399             
       
   400             initialScrollSpan += (imageWidth % regionWidth)/(regionWidth/KScrollStepFraction);
       
   401             
       
   402             if ( (initialScrollSpan * regionWidth) < (KScrollStepFraction*imageWidth))        
       
   403                 initialScrollSpan += 1;
       
   404            
       
   405             iScrollSpanX = initialScrollSpan;
       
   406             
       
   407             initialScrollSpan = imageHeight/regionHeight;
       
   408             initialScrollSpan *= KScrollStepFraction;
       
   409             
       
   410             //how many times the remainder fits into the 1/KScrollStepFraction th of a page
       
   411           //  TInt muschi = (imageHeight % regionHeight)/(regionHeight/KScrollStepFraction);
       
   412             
       
   413             initialScrollSpan += (imageHeight % regionHeight)/(regionHeight/KScrollStepFraction);
       
   414             // if it still does not match, add one to round up the result
       
   415             if ( (initialScrollSpan * regionHeight) < (KScrollStepFraction*imageHeight))        
       
   416                 initialScrollSpan += 1;
       
   417             
       
   418             iScrollSpanY = initialScrollSpan;
       
   419             }
       
   420         iSBFrame->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn,CEikScrollBarFrame::EOn );    
       
   421          
       
   422         TEikScrollBarModel vSbarModel;
       
   423         vSbarModel.iThumbPosition = iThumbPosY; 
       
   424         vSbarModel.iScrollSpan = iScrollSpanY; 
       
   425         TInt indicatorHeight = regionHeight * regionHeight / iImageScrollRect.Height();
       
   426         indicatorHeight = Min( indicatorHeight, regionHeight );
       
   427         vSbarModel.iThumbSpan = KScrollStepFraction;//indicatorHeight / KSpanDivider;
       
   428 
       
   429         TEikScrollBarModel hSbarModel;
       
   430         hSbarModel.iThumbPosition = iThumbPosX; 
       
   431         hSbarModel.iScrollSpan = iScrollSpanX; 
       
   432         TInt indicatorWidth = regionWidth * regionWidth / iImageScrollRect.Width();
       
   433         indicatorWidth = Min( indicatorWidth, regionWidth );
       
   434         hSbarModel.iThumbSpan =  KScrollStepFraction;//indicatorWidth / KSpanDivider;
       
   435 
       
   436         // For EDoubleSpan type scrollbar
       
   437         TAknDoubleSpanScrollBarModel hDsSbarModel( hSbarModel );
       
   438         TAknDoubleSpanScrollBarModel vDsSbarModel( vSbarModel );
       
   439 
       
   440         TEikScrollBarFrameLayout layout;
       
   441         layout.iTilingMode = TEikScrollBarFrameLayout::EClientRectConstant;
       
   442                                          
       
   443  //not functional iSBFrame->TileL( &hDsSbarModel, &vDsSbarModel, rect, rect, layout );
       
   444         iSBFrame->Tile( &hDsSbarModel, &vDsSbarModel);
       
   445         
       
   446         TRect controlRect = iMedia->GetRegion()->GetRectangle();
       
   447         controlRect.Move( (iMedia->Presentation()->GetPlayer()->
       
   448                         GetControl( iMedia->Presentation()))->Position() );
       
   449         
       
   450         TAknWindowLineLayout hLayout( AknLayoutScalable_Apps::scroll_pane_cp018( 2 ) ); // laf value
       
   451 		AknLayoutUtils::LayoutHorizontalScrollBar( iSBFrame, controlRect, hLayout );
       
   452 		TAknWindowLineLayout vLayout( AknLayoutScalable_Apps::scroll_pane_cp016( 2 ) ); // laf value
       
   453 		AknLayoutUtils::LayoutVerticalScrollBar( iSBFrame, controlRect, vLayout );
       
   454 		
       
   455 		iSBFrame->SetVFocusPosToThumbPos( vDsSbarModel.FocusPosition() );
       
   456 		iSBFrame->MoveHorizThumbTo( hDsSbarModel.FocusPosition() );
       
   457       
       
   458         iNotYetDrawn = EFalse;
       
   459         }
       
   460 #else
       
   461 
       
   462 #endif
       
   463     }
       
   464 
       
   465 
       
   466 // ---------------------------------------------------------
       
   467 // CSmilImageRenderer::Scroll
       
   468 // Determines if scrolling is possible to specified direction.
       
   469 // If it is calculates how many pixels can be scrolled to
       
   470 // that direction (scrolling is performed on 1/4 of the original
       
   471 // image size steps but can be smaller if available space limits it).
       
   472 // Moving of souce rectangle and scrolling rectangle is done
       
   473 // on mirror image due to they opposite coordinates. Active wait
       
   474 // is used on scrolling. Active scheduler is started to 
       
   475 // wait until asynchronous operation triggered by source rectangle
       
   476 // moving is completed.
       
   477 // ---------------------------------------------------------
       
   478 //
       
   479 void CSmilImageRenderer::Scroll( TInt aDirX, TInt aDirY )
       
   480     {
       
   481 #ifdef IMAGE_DEBUG
       
   482     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   483     SMILUILOGGER_ENTERFN( "[SMILUI] Image: Scroll" )
       
   484 #endif
       
   485 
       
   486         
       
   487     ScrollWithOutThumbUpdate( aDirX, aDirY );
       
   488 
       
   489 #ifdef RD_SCALABLE_UI_V2
       
   490     //first moving in y-direction
       
   491     if ( iThumbPosY > 0 && aDirY < 0 ||
       
   492         iThumbPosY < iScrollSpanY-KScrollStepFraction && aDirY > 0 )
       
   493         {
       
   494         iThumbPosY += aDirY;
       
   495         iSBFrame->MoveVertThumbTo( iThumbPosY );
       
   496         }
       
   497       //then in x-direction
       
   498     if ( iThumbPosX > 0 && aDirX < 0 ||
       
   499         iThumbPosX < iScrollSpanY-KScrollStepFraction && aDirX > 0 )
       
   500         {
       
   501         iThumbPosX += aDirX;
       
   502         iSBFrame->MoveHorizThumbTo( iThumbPosX );
       
   503         }
       
   504 #endif        
       
   505       
       
   506         
       
   507 #ifdef IMAGE_DEBUG  
       
   508     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: Scroll" )
       
   509 #endif
       
   510     }
       
   511 
       
   512 // ----------------------------------------------------------------------------
       
   513 // CSmilImageRenderer::PrepareMediaL
       
   514 // ----------------------------------------------------------------------------
       
   515 //
       
   516 void CSmilImageRenderer::PrepareMediaL()
       
   517     {
       
   518 #ifdef IMAGE_DEBUG
       
   519     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   520     SMILUILOGGER_ENTERFN( "[SMILUI] Image: PrepareMediaL" )
       
   521     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   522 #endif
       
   523 
       
   524     if ( iCurrState == EReady )
       
   525         {
       
   526         SetState( EHidden );
       
   527         }
       
   528 
       
   529 #ifdef IMAGE_DEBUG  
       
   530     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: PrepareMediaL" )
       
   531     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   532 #endif
       
   533     }
       
   534 
       
   535 // ----------------------------------------------------------------------------
       
   536 // CSmilImageRenderer::SeekMediaL
       
   537 // Seeking affects only animations. If animation is currently stopped
       
   538 // to last frame and animation is wanted to start playback from the beginnning
       
   539 // then only starting the engine again is neccessary. Otherwise speficied frame
       
   540 // is converted.
       
   541 // ----------------------------------------------------------------------------
       
   542 //
       
   543 void CSmilImageRenderer::SeekMediaL(const TSmilTime& aTime)
       
   544     {
       
   545 #ifdef IMAGE_DEBUG  
       
   546     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   547     SMILUILOGGER_ENTERFN( "[SMILUI] Image: SeekMediaL" )
       
   548     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   549 #endif
       
   550 
       
   551     if ( iAnimation )
       
   552         {
       
   553         // aTime is in milliseconds
       
   554         TInt time( aTime.Value() );
       
   555 
       
   556         TInt frame( 0 );
       
   557         if ( time > 0 && iSourceImage->IsAnimation() )
       
   558             {
       
   559             TInt cumTime( 0 );
       
   560             while ( cumTime <= time && frame < iSourceImage->AnimationFrameCount() )
       
   561                 {
       
   562                 // cumTime is on millisecond and delay on microseconds.
       
   563                 cumTime += iSourceImage->AnimationFrameDelay( frame ).Int() / 1000 + 
       
   564                            I64INT( iProcessingDelay.Int64() );
       
   565                 frame++;
       
   566                 }
       
   567             frame--;
       
   568             }
       
   569         
       
   570         ConvertFrameL( frame );
       
   571         
       
   572         if( aTime == 0 && iCurrState == EPlaying )
       
   573             {
       
   574 #ifdef IMAGE_DEBUG          
       
   575             SMILUILOGGER_WRITE( "[SMILUI] Image: iEngine->Play() in SeekMediaL()" )
       
   576 #endif      
       
   577             iEngine->Play();
       
   578             }
       
   579         }
       
   580         
       
   581 #ifdef IMAGE_DEBUG  
       
   582     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: SeekMediaL" )
       
   583     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   584 #endif    
       
   585     }
       
   586 
       
   587 // ----------------------------------------------------------------------------
       
   588 // CSmilImageRenderer::ShowMediaL
       
   589 // ----------------------------------------------------------------------------
       
   590 //
       
   591 void CSmilImageRenderer::ShowMediaL()
       
   592     {
       
   593 #ifdef IMAGE_DEBUG      
       
   594     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   595     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ShowMediaL" )
       
   596     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   597 #endif
       
   598 
       
   599     if ( iCurrState == EHidden )
       
   600         {
       
   601         if ( iAnimation )
       
   602             {
       
   603 #ifdef IMAGE_DEBUG          
       
   604             SMILUILOGGER_WRITE( "[SMILUI] Image: iEngine->Play() in ShowMedia" );
       
   605 #endif
       
   606             iEngine->Play();
       
   607             }
       
   608             
       
   609         SetState( EPlaying );
       
   610         
       
   611         iMedia->Redraw();
       
   612         }
       
   613 
       
   614 #ifdef IMAGE_DEBUG          
       
   615     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ShowMediaL" )
       
   616     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   617 #endif    
       
   618     }
       
   619 
       
   620 // ----------------------------------------------------------------------------
       
   621 // CSmilImageRenderer::HideMedia
       
   622 // ----------------------------------------------------------------------------
       
   623 //
       
   624 void CSmilImageRenderer::HideMedia()
       
   625     {
       
   626 #ifdef IMAGE_DEBUG      
       
   627     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   628     SMILUILOGGER_ENTERFN( "[SMILUI] Image: HideMedia" )
       
   629     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   630 #endif
       
   631 
       
   632     if ( iCurrState == EPlaying ||
       
   633          iCurrState == EPaused )
       
   634         {
       
   635         if ( iAnimation )
       
   636             {
       
   637 #ifdef IMAGE_DEBUG          
       
   638             SMILUILOGGER_WRITE( "[SMILUI] Image: iEngine->Stop()" )
       
   639 #endif        
       
   640 
       
   641             iEngine->Stop();    
       
   642             }
       
   643         
       
   644         SetState( EHidden );
       
   645         }
       
   646         
       
   647     iMedia->Redraw();
       
   648 #ifdef RD_SCALABLE_UI_V2    
       
   649     iNotYetDrawn = ETrue;
       
   650     if ( iSBFrame )
       
   651         {
       
   652         TRAP_IGNORE( iSBFrame->SetScrollBarVisibilityL( CEikScrollBarFrame::EOff, 
       
   653                                                         CEikScrollBarFrame::EOff ) );
       
   654         }
       
   655 #endif
       
   656 #ifdef IMAGE_DEBUG  
       
   657     SMILUILOGGER_ENTERFN( "[SMILUI] Image: HideMedia" )
       
   658     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   659 #endif    
       
   660     }
       
   661 
       
   662 // ----------------------------------------------------------------------------
       
   663 // CSmilImageRenderer::FreezeMedia
       
   664 // ----------------------------------------------------------------------------
       
   665 //
       
   666 void CSmilImageRenderer::FreezeMedia()
       
   667     {
       
   668 #ifdef IMAGE_DEBUG      
       
   669     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   670     SMILUILOGGER_ENTERFN( "[SMILUI] Image: FreezeMedia" )
       
   671     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   672 #endif
       
   673 
       
   674     if ( iCurrState == EPlaying )
       
   675         {
       
   676         SetState( EPaused );
       
   677         
       
   678         if ( iAnimation )
       
   679             {
       
   680             iEngine->Stop();
       
   681             }
       
   682         }
       
   683 
       
   684 #ifdef IMAGE_DEBUG  
       
   685     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: FreezeMedia" )
       
   686     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   687 #endif    
       
   688     }
       
   689 
       
   690 // ----------------------------------------------------------------------------
       
   691 // CSmilImageRenderer::ResumeMedia
       
   692 // ----------------------------------------------------------------------------
       
   693 //
       
   694 void CSmilImageRenderer::ResumeMedia()
       
   695     {
       
   696 #ifdef IMAGE_DEBUG      
       
   697     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   698     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ResumeMedia" )
       
   699     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   700 #endif
       
   701 
       
   702     if ( iCurrState == EPaused )
       
   703         {        
       
   704         if ( iAnimation )
       
   705             {
       
   706 #ifdef IMAGE_DEBUG          
       
   707             SMILUILOGGER_WRITE( "[SMILUI] Image: iEngine->Play() in ResumeMedia" )
       
   708 #endif        
       
   709 
       
   710             iEngine->Play();
       
   711             }
       
   712             
       
   713         SetState( EPlaying );
       
   714         
       
   715         iMedia->Redraw();
       
   716         }
       
   717 
       
   718 #ifdef IMAGE_DEBUG  
       
   719     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ResumeMedia" )
       
   720     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   721 #endif    
       
   722     }
       
   723 
       
   724 // ----------------------------------------------------------------------------
       
   725 // CSmilImageRenderer::Close
       
   726 // ----------------------------------------------------------------------------
       
   727 //
       
   728 void CSmilImageRenderer::Close()
       
   729     {
       
   730 #ifdef IMAGE_DEBUG      
       
   731     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   732     SMILUILOGGER_ENTERFN( "[SMILUI] Image: Close()" )
       
   733     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   734 #endif
       
   735     
       
   736     delete this;
       
   737 
       
   738 #ifdef IMAGE_DEBUG  
       
   739     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: Close()" )
       
   740     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   741 #endif    
       
   742     }
       
   743 
       
   744 // ----------------------------------------------------------------------------
       
   745 // CSmilImageRenderer::InitializeViewerL
       
   746 // Specific option is needed to trigger scaling without maintaining
       
   747 // aspect ratio. On this case also zoom ration cannot be set.Source rectangle
       
   748 // position is always set to top left corner. 
       
   749 // ----------------------------------------------------------------------------
       
   750 //
       
   751 void CSmilImageRenderer::InitializeViewerL()
       
   752     {
       
   753 #ifdef IMAGE_DEBUG      
       
   754     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   755     SMILUILOGGER_ENTERFN( "[SMILUI] Image: InitializeViewerL" )
       
   756     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   757 #endif
       
   758 
       
   759     TSize imageSize( iSourceImage->Size() );
       
   760     
       
   761     TSize viewerSize( iSourceImage->Size() );
       
   762     TReal zoomRatio( 1.0 );
       
   763     TUint32 options( 0 );
       
   764     
       
   765     if ( iMedia->GetRegion() )
       
   766         {
       
   767         TSize regionSize( iMedia->GetRegion()->GetRectangle().Size() );
       
   768 
       
   769         switch ( iMedia->GetRegion()->GetFit() )
       
   770             {
       
   771             // FILL: Scale image to fit region exactly.
       
   772             case MSmilRegion::EFill:
       
   773                 {
       
   774                 viewerSize = regionSize;
       
   775                 options = MIHLImageViewer::EOptionIgnoreAspectRatio;
       
   776                 break;
       
   777                 }
       
   778             // MEET: Image fits in region. Aspect ration maintained.
       
   779             case MSmilRegion::EMeet:
       
   780                 {
       
   781                 zoomRatio = CalculateMeetRatio( regionSize, imageSize );
       
   782                 viewerSize = regionSize;
       
   783                 break;
       
   784                 }
       
   785             // SLICE: Image fills region. Aspect ration maintained.
       
   786             case MSmilRegion::ESlice:
       
   787                 {
       
   788                 zoomRatio = CalculateSliceRatio( regionSize, imageSize );
       
   789                 viewerSize = regionSize;
       
   790                 break;
       
   791                 }
       
   792             case MSmilRegion::EScroll:
       
   793             case MSmilRegion::EHidden:
       
   794             default:
       
   795                 {
       
   796                 // Hidden changed to support scrolling.
       
   797                 viewerSize = regionSize;
       
   798                 break;
       
   799                 }
       
   800             }
       
   801         }
       
   802         
       
   803     iEngine = IHLViewerFactory::CreateImageViewerL( viewerSize, 
       
   804                                                     *iSourceImage, 
       
   805                                                     *iDestinationBitmap, 
       
   806                                                     *this, 
       
   807                                                     options );
       
   808     
       
   809     if( options == 0 ) // == keep aspect ration
       
   810         {
       
   811         User::LeaveIfError( iEngine->SetZoomRatio( zoomRatio ) );
       
   812         }
       
   813         
       
   814     User::LeaveIfError( iEngine->SetSourceRectPosition( TPoint( 0, 0 ) ) );
       
   815 
       
   816     TTime startTime;
       
   817     startTime.HomeTime();
       
   818     
       
   819     ConvertFrameL( 0 );
       
   820     
       
   821     TTime stopTime;
       
   822     stopTime.HomeTime();
       
   823     
       
   824     iProcessingDelay = stopTime.MicroSecondsFrom( startTime ).Int64() / 1000 + KProcessingDelayOffset ;
       
   825     
       
   826     
       
   827 #ifdef IMAGE_DEBUG  
       
   828     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: InitializeViewerL" )
       
   829     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   830 #endif    
       
   831     }
       
   832 
       
   833 // ----------------------------------------------------------------------------
       
   834 // CSmilImageRenderer::ConvertFrameL
       
   835 // Triggers active wait (i.e. active scheduler is started to wait
       
   836 // until frame conver has been finished) if current state is set to not ready.
       
   837 // ----------------------------------------------------------------------------
       
   838 //
       
   839 void CSmilImageRenderer::ConvertFrameL( TInt aFrame )
       
   840     {
       
   841 #ifdef IMAGE_DEBUG
       
   842     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   843     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ConvertFrameL" )
       
   844     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   845     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: currentFrame=%d"), 
       
   846                             iEngine->AnimationFrame() );
       
   847 #endif
       
   848 
       
   849     if ( iSourceImage->IsAnimation() &&
       
   850          aFrame <= iSourceImage->AnimationFrameCount())
       
   851         {
       
   852 #ifdef IMAGE_DEBUG
       
   853         SMILUILOGGER_WRITE( "[SMILUI] Image: iEngine->SetAnimationFrame()" )
       
   854 #endif
       
   855         iError = iEngine->SetAnimationFrame( aFrame );            
       
   856         }
       
   857         
       
   858     if ( iCurrState == ENotReady )
       
   859         {
       
   860 #ifdef IMAGE_DEBUG
       
   861         SMILUILOGGER_WRITE( "[SMILUI] Image: BeginActiveWait() starts" )
       
   862 #endif
       
   863 
       
   864         BeginActiveWait(); // Stopped at ViewerBitmapChangedL function
       
   865         
       
   866 #ifdef IMAGE_DEBUG
       
   867         SMILUILOGGER_WRITE( "[SMILUI] Image: BeginActiveWait() ends" )
       
   868         SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iError=%d"), iError );
       
   869 #endif
       
   870 
       
   871         User::LeaveIfError( iError );
       
   872         }
       
   873 
       
   874 
       
   875 #ifdef IMAGE_DEBUG
       
   876     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ConvertFrameL" )
       
   877     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   878     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: currentFrame=%d"), 
       
   879                             iEngine->AnimationFrame() );
       
   880 #endif
       
   881     }
       
   882 
       
   883 // ----------------------------------------------------------------------------
       
   884 // CSmilImageRenderer::SetupScrolling
       
   885 // Initializes the scroll rectangle and determines if scrolling is allowed.
       
   886 // ----------------------------------------------------------------------------
       
   887 //
       
   888 void CSmilImageRenderer::SetupScrollingL()
       
   889     {
       
   890 #ifdef IMAGE_DEBUG
       
   891     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   892     SMILUILOGGER_ENTERFN( "[SMILUI] Image: SetupScrollingL" )
       
   893     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   894 #endif
       
   895 
       
   896     iImageScrollRect.SetRect( iMedia->GetRectangle().iTl, TSize( iSourceSize.iWidth * iEngine->ZoomRatio(),
       
   897                                                                  iSourceSize.iHeight * iEngine->ZoomRatio() ) );
       
   898 
       
   899     if ( iMedia->GetRegion() )
       
   900         {
       
   901         if ( IsScrollingPossible( EScrollDown ) || 
       
   902              IsScrollingPossible( EScrollRight ) )
       
   903             {
       
   904             iIsScrollable = ETrue;
       
   905             User::LeaveIfError( iEngine->SetSourceRectPosition( TPoint( 0,0 ) ) );
       
   906             }
       
   907         }
       
   908 
       
   909 #ifdef IMAGE_DEBUG
       
   910     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: SetupScrollingL" )
       
   911     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   912 #endif
       
   913     }
       
   914 
       
   915 // ----------------------------------------------------------------------------
       
   916 // CSmilImageRenderer::ActivateL
       
   917 // Removes activation and notifies renderer that animation loop has ended.
       
   918 // ----------------------------------------------------------------------------
       
   919 //
       
   920 void CSmilImageRenderer::ActivateL( const TSmilTime& /*aTime*/ )
       
   921     {
       
   922     iMedia->CancelActive( this );
       
   923     iMedia->RendererAtEndL();
       
   924     }
       
   925 
       
   926 // ----------------------------------------------------------------------------
       
   927 // CSmilImageRenderer::ViewerBitmapChangedL
       
   928 // Stops the engine and sets timer to notify the SMIL engine about the end 
       
   929 // if this was the last frame of animation.
       
   930 // State is changed to previous one. Changed bitmap is drawing is
       
   931 // called if renderer is visible and active wait is ended if neccessary.
       
   932 // ----------------------------------------------------------------------------
       
   933 //
       
   934 void CSmilImageRenderer::ViewerBitmapChangedL()
       
   935     {
       
   936 #ifdef IMAGE_DEBUG
       
   937     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
   938     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ViewerBitmapChangedL" )
       
   939     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   940 #endif
       
   941 
       
   942     if ( iSourceImage->IsAnimation() ) 
       
   943         {
       
   944 #ifdef IMAGE_DEBUG
       
   945         SMILUILOGGER_WRITE( "[SMILUI] Image: AnimationFrameChanged " )
       
   946 #endif
       
   947 
       
   948         if ( iEngine->AnimationFrame() == iSourceImage->AnimationFrameCount() - 1 )
       
   949             {
       
   950 #ifdef IMAGE_DEBUG
       
   951             SMILUILOGGER_WRITE( "[SMILUI] Image: iMedia->RendererAtEndL()" )
       
   952 #endif
       
   953             
       
   954             // Last frame's delay should be counted to the animation duration or
       
   955             // else first frame is shown too quickly when looping.
       
   956             TSmilTime delay( iSourceImage->AnimationFrameDelay( iEngine->AnimationFrame() ).Int() / 1000 );
       
   957             
       
   958             // Calls SMIL engine to call ActivateL function after delay time.
       
   959             iMedia->AfterL( this, 
       
   960                             delay, 
       
   961                             EFalse ); 
       
   962       
       
   963 #ifdef IMAGE_DEBUG
       
   964             SMILUILOGGER_WRITE( "[SMILUI] Image: iEngine->Stop()" )
       
   965 #endif
       
   966             iEngine->Stop();
       
   967             }
       
   968         }
       
   969     
       
   970     if ( iCurrState == ENotReady )
       
   971         {
       
   972         SetState( ( iPrevState == ENotReady ) ? EReady : iPrevState );
       
   973         }
       
   974         
       
   975     if ( iCurrState == EPlaying || iCurrState == EPaused )
       
   976         {
       
   977         iMedia->Redraw();
       
   978         }
       
   979 
       
   980 #ifdef IMAGE_DEBUG
       
   981     SMILUILOGGER_WRITE( "[SMILUI] Image: EndActiveWait()" )
       
   982 #endif
       
   983 
       
   984     // Does nothing if there is no waiter.
       
   985     EndActiveWait();
       
   986 
       
   987 #ifdef IMAGE_DEBUG
       
   988     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ViewerBitmapChangedL" )
       
   989     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
   990 #endif
       
   991     }
       
   992 
       
   993 // ----------------------------------------------------------------------------
       
   994 // CSmilImageRenderer::ViewerError
       
   995 // Stores the error and ends active wait if neccessary.
       
   996 // ----------------------------------------------------------------------------
       
   997 //
       
   998 void CSmilImageRenderer::ViewerError( TInt aError )
       
   999     {
       
  1000 #ifdef IMAGE_DEBUG
       
  1001     SMILUILOGGER_WRITE_TIMESTAMP( " --------------------------------" )
       
  1002     SMILUILOGGER_ENTERFN( "[SMILUI] Image: ViewerError" )
       
  1003     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: iCurrState=%d"), iCurrState );
       
  1004     SMILUILOGGER_WRITEF( _L("[SMILUI] Image: aError=%d"), aError );
       
  1005 #endif
       
  1006 
       
  1007     iError = aError;
       
  1008     
       
  1009     // Does nothing if there is no waiter.
       
  1010     EndActiveWait();
       
  1011     
       
  1012 #ifdef IMAGE_DEBUG
       
  1013     SMILUILOGGER_LEAVEFN( "[SMILUI] Image: ViewerError" )
       
  1014 #endif
       
  1015     }
       
  1016 
       
  1017 // ----------------------------------------------------------------------------
       
  1018 // CSmilImageRenderer::CalculateMeetRatio
       
  1019 // Calculates correct zoom ratio for meet fit attribute (i.e. bitmap should preserve
       
  1020 // aspect ratio and be shown fully on region as large as possible). Correct
       
  1021 // zoom ratio is smaller of the width and height original image size and 
       
  1022 // region image size ratios.
       
  1023 // ----------------------------------------------------------------------------
       
  1024 //
       
  1025 TReal CSmilImageRenderer::CalculateMeetRatio( const TSize& aRegion,
       
  1026                                               const TSize& aImage ) const
       
  1027     {
       
  1028     TReal widthRatio = static_cast<TReal>( aRegion.iWidth ) / 
       
  1029                        static_cast<TReal>( aImage.iWidth );
       
  1030 	TReal heightRatio = static_cast<TReal>( aRegion.iHeight ) / 
       
  1031 	                    static_cast<TReal>( aImage.iHeight );
       
  1032 	return ( widthRatio < heightRatio ) ? widthRatio : heightRatio;
       
  1033     }
       
  1034 
       
  1035 // ----------------------------------------------------------------------------
       
  1036 // CSmilImageRenderer::CalculateSliceRatio( const TSize& aRegion, 
       
  1037 //                                          const TSize& aImage ) const
       
  1038 // Calculates correct zoom ratio for slice fit attribute (i.e. bitmap should preserve
       
  1039 // aspect ratio and be shown as big as possible so that atleast either height
       
  1040 // or width is shown fully). Correct zoom ratio is larger of the width and 
       
  1041 // height original image size and region image size ratios.
       
  1042 // ----------------------------------------------------------------------------
       
  1043 //
       
  1044 TReal CSmilImageRenderer::CalculateSliceRatio( const TSize& aRegion,
       
  1045                                                const TSize& aImage ) const
       
  1046     {
       
  1047     TReal widthRatio = static_cast<TReal>( aRegion.iWidth ) / 
       
  1048                        static_cast<TReal>( aImage.iWidth );
       
  1049 	TReal heightRatio = static_cast<TReal>( aRegion.iHeight ) / 
       
  1050 	                    static_cast<TReal>( aImage.iHeight );
       
  1051 	return ( widthRatio > heightRatio ) ? widthRatio : heightRatio;
       
  1052     }
       
  1053 
       
  1054 
       
  1055 // ----------------------------------------------------------------------------
       
  1056 // CSmilImageRenderer::SetState
       
  1057 // Old state is saved so that it can be "reloaded" when neccessary.
       
  1058 // ----------------------------------------------------------------------------
       
  1059 //
       
  1060 void CSmilImageRenderer::SetState( TInt aNewState )
       
  1061     {
       
  1062     iPrevState = iCurrState;
       
  1063     iCurrState = aNewState;
       
  1064     }
       
  1065 
       
  1066 // ----------------------------------------------------------------------------
       
  1067 // CSmilImageRenderer::IsScrollingPossible
       
  1068 // Scrolling determined to be possible if there is atleast one pixel to move
       
  1069 // on specified direction.
       
  1070 // ---------------------------------------------------------------------------- 
       
  1071 //
       
  1072 TBool CSmilImageRenderer::IsScrollingPossible( TDirection aDirection ) const
       
  1073     {
       
  1074     TRect sourceRect( iEngine->SourceRect() );
       
  1075     TSize sourceSize( iEngine->SourceSize() );
       
  1076 
       
  1077     switch ( aDirection )
       
  1078         {
       
  1079         case EScrollUp:
       
  1080             {
       
  1081             return ( sourceRect.iTl.iY > 0 );
       
  1082             }
       
  1083         case EScrollDown:
       
  1084             {
       
  1085             return ( sourceRect.iBr.iY < sourceSize.iHeight );
       
  1086             }
       
  1087         case EScrollRight:
       
  1088             {
       
  1089             return ( sourceRect.iBr.iX < sourceSize.iWidth );
       
  1090             }
       
  1091         case EScrollLeft:
       
  1092             {
       
  1093             return ( sourceRect.iTl.iX > 0 );
       
  1094             }
       
  1095         default:
       
  1096            {
       
  1097            break;
       
  1098            }
       
  1099         }
       
  1100         
       
  1101     return EFalse;
       
  1102     }
       
  1103 
       
  1104 // ----------------------------------------------------------------------------
       
  1105 // CSmilImageRenderer::CalculateScrollStepSize
       
  1106 // Scrolling step (i.e. the amount of pixels to scroll on given direction) is
       
  1107 // calculated to be maximum 1/4 of original image size but no larger than
       
  1108 // available space.
       
  1109 // ---------------------------------------------------------------------------- 
       
  1110 //
       
  1111 void CSmilImageRenderer::CalculateScrollStepSize( TDirection aDirection , 
       
  1112                                                   TSize& aScrollingStep, TInt aStepSize
       
  1113                                                    ) const
       
  1114     {    
       
  1115     TRect sourceRect( iEngine->SourceRect() );
       
  1116     TSize sourceSize( iEngine->SourceSize() );
       
  1117 
       
  1118 	switch ( aDirection )
       
  1119 		{
       
  1120 		case EScrollLeft:
       
  1121 	        {
       
  1122 	        aScrollingStep.iWidth = aStepSize*sourceRect.Size().iWidth  / KScrollStepFraction;
       
  1123 	        if ( sourceRect.iTl.iX - aScrollingStep.iWidth <= 0 )
       
  1124 	            {
       
  1125 	            aScrollingStep.iWidth = sourceRect.iTl.iX;
       
  1126 	            }
       
  1127 	        aScrollingStep.iWidth = -aScrollingStep.iWidth;
       
  1128 			break;
       
  1129 			}
       
  1130 		case EScrollRight:
       
  1131 			{
       
  1132 			aScrollingStep.iWidth = aStepSize*sourceRect.Size().iWidth  / KScrollStepFraction;
       
  1133 			if ( sourceRect.iBr.iX + aScrollingStep.iWidth >= sourceSize.iWidth )
       
  1134 	            {
       
  1135 	            aScrollingStep.iWidth = sourceSize.iWidth - sourceRect.iBr.iX;
       
  1136 	            }
       
  1137 			break;
       
  1138 			}
       
  1139 		case EScrollUp:
       
  1140 			{
       
  1141 			aScrollingStep.iHeight = aStepSize*sourceRect.Size().iHeight  / KScrollStepFraction;
       
  1142 			if ( sourceRect.iTl.iY - aScrollingStep.iHeight <= 0 )
       
  1143 			    {
       
  1144 			    aScrollingStep.iHeight = sourceRect.iTl.iY;
       
  1145 			    }
       
  1146 			aScrollingStep.iHeight = -aScrollingStep.iHeight;
       
  1147 			break;
       
  1148 			}
       
  1149 		default:
       
  1150 		case EScrollDown:
       
  1151 			{
       
  1152 			aScrollingStep.iHeight = aStepSize*sourceRect.Size().iHeight  / KScrollStepFraction;
       
  1153 			if ( sourceRect.iBr.iY + aScrollingStep.iHeight >= sourceSize.iHeight )
       
  1154 			    {
       
  1155 			    aScrollingStep.iHeight = sourceSize.iHeight - sourceRect.iBr.iY;
       
  1156 			    }
       
  1157 			break;
       
  1158 			}
       
  1159 		}
       
  1160     }
       
  1161     
       
  1162 #ifdef RD_SCALABLE_UI_V2    
       
  1163 // -----------------------------------------------------------------------------
       
  1164 // CSmilImageRenderer::HandleScrollEventL
       
  1165 // -----------------------------------------------------------------------------
       
  1166 // 
       
  1167 void CSmilImageRenderer::HandleScrollEventL( 
       
  1168                     CEikScrollBar* aScrollBar, TEikScrollEvent aEventType )
       
  1169     {
       
  1170     //get thumb pos for x and y
       
  1171     TInt oldThumbPos = 0;
       
  1172         
       
  1173         //active wait check here??
       
  1174         
       
  1175     //check which scrollbar is being used        
       
  1176     if ( aScrollBar == iSBFrame->GetScrollBarHandle(CEikScrollBar::EVertical) )
       
  1177         {
       
  1178         oldThumbPos = iThumbPosY;
       
  1179         iThumbPosY = aScrollBar->ThumbPosition();
       
  1180         }
       
  1181     else
       
  1182         {
       
  1183         oldThumbPos = iThumbPosX;
       
  1184         iThumbPosX = aScrollBar->ThumbPosition();
       
  1185         }
       
  1186 
       
  1187     
       
  1188     if ( AknLayoutUtils::PenEnabled() )
       
  1189         {
       
  1190     	switch ( aEventType )
       
  1191     		{
       
  1192        		case EEikScrollUp:
       
  1193    		        
       
  1194        		    ScrollWithOutThumbUpdate( 0, -KScrollValue );
       
  1195        		    break;
       
  1196        		case EEikScrollDown:
       
  1197    			    ScrollWithOutThumbUpdate( 0, KScrollValue );
       
  1198        		    break;
       
  1199    		    case EEikScrollPageUp:
       
  1200    		        {
       
  1201    		        ScrollWithOutThumbUpdate( 0, -KScrollStepFraction*KScrollValue );
       
  1202    		        break;
       
  1203    		        }
       
  1204    		    case EEikScrollPageDown:
       
  1205    		        {
       
  1206    		        ScrollWithOutThumbUpdate( 0, KScrollStepFraction*KScrollValue );   		     
       
  1207    		        break;
       
  1208    		        }
       
  1209         	case EEikScrollThumbDragVert:
       
  1210         	    {
       
  1211                 if ( !iWait.IsStarted() )// -ignoring events if busy
       
  1212                     {
       
  1213    		            ScrollWithOutThumbUpdate( 0, iThumbPosY - oldThumbPos );
       
  1214                     }
       
  1215                 else //restoring old thumb value
       
  1216                     {// in theory the new iThumbPosY might not correspond the the
       
  1217                     //actual position, but in tests this does not seem to cause problems
       
  1218                     iThumbPosY = oldThumbPos;
       
  1219                     }
       
  1220    	    	    break;
       
  1221         	    }
       
  1222         	    
       
  1223       	    case EEikScrollThumbDragHoriz:
       
  1224       	        {
       
  1225       	        if ( !iWait.IsStarted() )// -ignoring events if busy
       
  1226       	            {
       
  1227    		            ScrollWithOutThumbUpdate( iThumbPosX-oldThumbPos, 0 ); 
       
  1228       	            }
       
  1229       	        else //restoring old thumb value
       
  1230       	            {
       
  1231                     iThumbPosX = oldThumbPos;
       
  1232       	            }
       
  1233    		        break;
       
  1234       	        }            	    
       
  1235         	   		        
       
  1236        		case EEikScrollLeft:
       
  1237        		   	{
       
  1238     	        ScrollWithOutThumbUpdate( -KScrollValue, 0 );   		     
       
  1239    		        break;
       
  1240    		        }
       
  1241        		
       
  1242     		case EEikScrollRight:
       
  1243     		   	{
       
  1244     	        ScrollWithOutThumbUpdate( KScrollValue, 0 );   		     
       
  1245    		        break;
       
  1246    		        }
       
  1247         	case EEikScrollPageLeft:
       
  1248         	   	{
       
  1249     	        ScrollWithOutThumbUpdate( -KScrollStepFraction*KScrollValue, 0 );   		     
       
  1250    		        break;
       
  1251    		        }
       
  1252         	case EEikScrollPageRight:
       
  1253         	   	{
       
  1254     	        ScrollWithOutThumbUpdate( KScrollStepFraction*KScrollValue, 0 );   		     
       
  1255    		        break;
       
  1256    		        }
       
  1257 
       
  1258         	case EEikScrollThumbReleaseHoriz:
       
  1259         	case EEikScrollThumbReleaseVert:	
       
  1260             case EEikScrollHome:
       
  1261         	case EEikScrollTop:
       
  1262         	case EEikScrollEnd:
       
  1263         	case EEikScrollBottom:
       
  1264        		default:
       
  1265        		    // Do nothing
       
  1266        			break;
       
  1267        			}
       
  1268         }
       
  1269     }     
       
  1270 #else    
       
  1271 void CSmilImageRenderer::HandleScrollEventL( 
       
  1272                     CEikScrollBar* /*aScrollBar*/, TEikScrollEvent /*aEventType*/ )
       
  1273     {  }
       
  1274 #endif
       
  1275     
       
  1276 // -----------------------------------------------------------------------------
       
  1277 // CSmilImageRenderer::ScrollWithOutThumbUpdate
       
  1278 // -----------------------------------------------------------------------------
       
  1279 // 
       
  1280 void CSmilImageRenderer::ScrollWithOutThumbUpdate( TInt aDirX, TInt aDirY )
       
  1281     {
       
  1282     TSize scrollingStep;
       
  1283 
       
  1284     if ( ( aDirX < 0 ) && IsScrollingPossible( EScrollLeft ) )
       
  1285         {
       
  1286         SetState( ENotReady );
       
  1287 #ifdef RD_SCALABLE_UI_V2        
       
  1288         CalculateScrollStepSize( EScrollLeft, scrollingStep, -aDirX );
       
  1289 #else
       
  1290         CalculateScrollStepSize( EScrollLeft, scrollingStep );
       
  1291 #endif        
       
  1292         }
       
  1293     else if ( ( aDirX > 0 ) && IsScrollingPossible( EScrollRight ) )
       
  1294         {
       
  1295         SetState( ENotReady );
       
  1296 #ifdef RD_SCALABLE_UI_V2          
       
  1297         CalculateScrollStepSize( EScrollRight, scrollingStep, aDirX );
       
  1298 #else        
       
  1299         CalculateScrollStepSize( EScrollRight, scrollingStep );
       
  1300 #endif        
       
  1301         }
       
  1302     
       
  1303     if ( ( aDirY < 0 ) && IsScrollingPossible( EScrollUp ) )
       
  1304         {
       
  1305         SetState( ENotReady );
       
  1306 #ifdef RD_SCALABLE_UI_V2          
       
  1307         CalculateScrollStepSize( EScrollUp, scrollingStep, -aDirY );
       
  1308 #else        
       
  1309         CalculateScrollStepSize( EScrollUp, scrollingStep );
       
  1310 #endif        
       
  1311         }
       
  1312     else if ( ( aDirY > 0 ) && IsScrollingPossible( EScrollDown ) )
       
  1313         {
       
  1314         SetState( ENotReady );
       
  1315 #ifdef RD_SCALABLE_UI_V2          
       
  1316         CalculateScrollStepSize( EScrollDown, scrollingStep, aDirY );
       
  1317 #else        
       
  1318         CalculateScrollStepSize( EScrollDown, scrollingStep );
       
  1319 #endif        
       
  1320         }
       
  1321 
       
  1322     if ( iCurrState == ENotReady && iEngine )
       
  1323         {
       
  1324         TInt result = iEngine->MoveSourceRect( scrollingStep.iWidth, 
       
  1325                                                scrollingStep.iHeight );
       
  1326         if ( result == KErrNone )
       
  1327             {
       
  1328             iImageScrollRect.Move( -scrollingStep.iWidth, 
       
  1329                                    -scrollingStep.iHeight );
       
  1330             BeginActiveWait();
       
  1331             
       
  1332             iMedia->Redraw();
       
  1333             }
       
  1334         }
       
  1335     }
       
  1336     
       
  1337 //  End of File  
       
  1338