internetradio2.0/uicontrolssrc/irmarqueeclet.cpp
changeset 0 09774dfdd46b
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Controlet to implement marquee functionality for visuals
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <alf/alfcontrol.h>
       
    20 #include <alf/alfenv.h>
       
    21 #include <alf/alfevent.h>
       
    22 #include <alf/alfviewportlayout.h>
       
    23 #include <alf/alftimedvalue.h>
       
    24 #include <alf/alftextvisual.h>
       
    25 
       
    26 #include "irmarqueeclet.h"
       
    27  
       
    28 // Default scroll style.
       
    29 const TIRMarqueeClet::TIRScrollStyle KVRDefaultScrollStyle = TIRMarqueeClet::EIRScrollStyleOnce;
       
    30 // Default scroll direction.
       
    31 const TIRMarqueeClet::TIRScrollDirection KVRDefaultScrollDirection = 
       
    32 										TIRMarqueeClet::EIRScrollDirectionLeft;
       
    33 // Default scroll speed in pixels per second.
       
    34 const TInt KVRDefaultScrollSpeed = 30;
       
    35 //Default Scroll Time
       
    36 const TInt KVRScrollTime=1000;
       
    37 // Spaces needed when creating continuously scrolling text. 
       
    38 _LIT( KVRSpace, "     " );
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Constructor.
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 TIRMarqueeClet::TIRMarqueeClet() :
       
    45     iViewport( NULL ), iTextVisual( NULL ), iScrollSize(), 
       
    46     iScrollSpeed( KVRDefaultScrollSpeed ), iScrollDirection( KVRDefaultScrollDirection ), 
       
    47     iScrollStyle( KVRDefaultScrollStyle ), iSnoozeTime( 0 ), iOrigin(), iOriginalTextWidth( 0 )
       
    48     {
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // Destructor.
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 TIRMarqueeClet::~TIRMarqueeClet()
       
    56     {
       
    57     if ( iTextVisual )
       
    58         {
       
    59         iTextVisual->Owner().Env().CancelCustomCommands( this );
       
    60         }
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Sets the required information for this marquee controlet.
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 void TIRMarqueeClet::Set( CAlfViewportLayout& aViewport, CAlfTextVisual& aTextVisual )
       
    68     {
       
    69     aTextVisual.Owner().Env().CancelCustomCommands( this );
       
    70 
       
    71     iViewport = &aViewport;
       
    72     iTextVisual = &aTextVisual;
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Sets scroll speed in pixels per second.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void TIRMarqueeClet::SetScrollSpeed( TInt aScrollSpeed )
       
    80     {
       
    81     iScrollSpeed = aScrollSpeed;
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Sets the scroll direction.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 void TIRMarqueeClet::SetScrollDirection( TIRScrollDirection aScrollDirection )
       
    89     {
       
    90     iScrollDirection = aScrollDirection; 
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // Sets the scroll style.
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void TIRMarqueeClet::SetScrollStyle( TIRScrollStyle aScrollStyle )
       
    98     {
       
    99     iScrollStyle = aScrollStyle;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Starts scrolling.
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void TIRMarqueeClet::StartL( TInt aDelay, TInt aSnooze )
       
   107     {
       
   108     ASSERT( iViewport );
       
   109     ASSERT( iTextVisual );
       
   110 
       
   111     iViewport->SetViewportSize( TAlfRealPoint( 1.0f, 1.0f ), 0 );
       
   112     iViewport->SetVirtualSize( TAlfRealPoint( 1.0f, 1.0f ), 0 );
       
   113     iViewport->SetViewportPos( TAlfRealPoint( 0.0f, 0.0f ), 0 );
       
   114 
       
   115     iTextVisual->Owner().Env().CancelCustomCommands( this );
       
   116 
       
   117     iSnoozeTime = aSnooze;
       
   118 
       
   119     // Get original text width in pixels.
       
   120     iOriginalTextWidth = iTextVisual->TextExtents().iWidth;
       
   121     TInt targetWidth = iViewport->Size().Target().AsSize().iWidth;
       
   122 
       
   123     // Check if loop scrolling and if the text does not fit to the viewport size.
       
   124     if ( iScrollStyle == TIRMarqueeClet::EIRScrollStyleLoop && iOriginalTextWidth > targetWidth )
       
   125         {
       
   126         // Create continuously scrolling text (text + 5 spaces + text).
       
   127         RBuf buffer;
       
   128         CleanupClosePushL( buffer );
       
   129         buffer.CreateL( 2 * iOriginalTextWidth + KVRSpace().Length() );
       
   130         buffer.Append( iTextVisual->Text() );
       
   131         buffer.Append( KVRSpace );
       
   132         buffer.Append( iTextVisual->Text() );
       
   133         iTextVisual->SetTextL( buffer );
       
   134         CleanupStack::PopAndDestroy(&buffer);// buffer
       
   135         }
       
   136     
       
   137     // Text size to display.
       
   138     iScrollSize = iTextVisual->TextExtents();
       
   139 
       
   140     // Check if text must be scrolled.
       
   141     if ( iScrollSize.iWidth > targetWidth )
       
   142         {
       
   143         // Set line wrapping to truncate: if text does not fit to the viewport width, the text is
       
   144         // truncated with ellipsis (...).
       
   145         iTextVisual->SetWrapping( CAlfTextVisual::ELineWrapTruncate );
       
   146         // Decides the correct directionality for the scroll.
       
   147         TBidiText::TDirectionality directionality = TBidiText::
       
   148         					TextDirectionality( iTextVisual->Text() );
       
   149         if ( directionality == TBidiText::ELeftToRight )
       
   150             {
       
   151             SetScrollDirection( TIRMarqueeClet::EIRScrollDirectionLeft ); 
       
   152             }
       
   153         else
       
   154             {
       
   155             SetScrollDirection( TIRMarqueeClet::EIRScrollDirectionRight ); 
       
   156             }
       
   157         // Align horizontally.
       
   158         iTextVisual->SetAlign( EAlfAlignHLocale, EAlfAlignVCenter );
       
   159         // Start scrolling.
       
   160         iTextVisual->Owner().Env().Send( TAlfCustomEventCommand( 
       
   161             TIRMarqueeClet::EIRCustomEventScrollStarted, this ), aDelay );
       
   162         }
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // From class CAlfControl.
       
   167 // Event handler.
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 TBool TIRMarqueeClet::OfferEventL( const TAlfEvent& aEvent )
       
   171     {
       
   172     TBool eventHandled = EFalse;
       
   173     
       
   174     if ( aEvent.IsCustomEvent() && iViewport )
       
   175         {
       
   176         switch ( aEvent.CustomParameter() )
       
   177             {
       
   178             case TIRMarqueeClet::EIRCustomEventScrollCompleted:
       
   179                 {
       
   180                 // Viewport is reset.
       
   181                 iViewport->SetVirtualSize( TAlfRealPoint( 1.0f, 1.0f ), 0 );
       
   182                 iViewport->SetViewportPos( TAlfRealPoint( 0.0f, 0.0f ), 0 );
       
   183                 // Set line wrapping to truncate.
       
   184                 iTextVisual->SetWrapping( CAlfTextVisual::ELineWrapTruncate );
       
   185                 eventHandled = ETrue;
       
   186                 break;
       
   187                 }
       
   188             case TIRMarqueeClet::EIRCustomEventScrollStarted:
       
   189                 {
       
   190                  // Visible scroll space width.
       
   191                 TReal32 visibleWidth = iViewport->Size().Target().iX;
       
   192  
       
   193                 // One scroll width.
       
   194                 TReal32 overflowWidth( 0.0f );
       
   195                 switch ( iScrollStyle )
       
   196                     {
       
   197                     case TIRMarqueeClet::EIRScrollStyleOnce:
       
   198                         overflowWidth = iScrollSize.iWidth - visibleWidth;
       
   199                         break;
       
   200                     case TIRMarqueeClet::EIRScrollStyleLoop:
       
   201                         // In EIRScrollStyleLoop mode, the scrolling width is 
       
   202                         // the width of the original text + the trailing spaces.
       
   203                         overflowWidth = iScrollSize.iWidth - iOriginalTextWidth;
       
   204                         break;
       
   205                     default:
       
   206                         break;
       
   207                     }
       
   208 
       
   209                 // Width ratio.
       
   210                 TReal32 widthRatio = overflowWidth / visibleWidth;
       
   211                 // Target position.
       
   212                 TAlfRealPoint target( iViewport->Pos().Target() );
       
   213                 // Coordinate space used for scrolling.
       
   214                 TAlfRealPoint virt( widthRatio + 1.0f, 1.0f );
       
   215                 // Start position.
       
   216                 iOrigin = iViewport->Pos().ValueNow();
       
   217                 // Scrolling time in milliseconds.
       
   218                 TInt scrollTime = KVRScrollTime *
       
   219                 				 static_cast<TInt>( overflowWidth / iScrollSpeed );
       
   220                 
       
   221                 switch ( iScrollDirection )
       
   222                     {
       
   223                     case TIRMarqueeClet::EIRScrollDirectionLeft:
       
   224                         iOrigin.iX = 0.0f;
       
   225                         target.iX = widthRatio;
       
   226                         break;
       
   227                     case TIRMarqueeClet::EIRScrollDirectionRight:
       
   228                         iOrigin.iX = widthRatio;
       
   229                         target.iX = 0.0f;
       
   230                         break;
       
   231                     default:
       
   232                         break;
       
   233                     }
       
   234 
       
   235                 // Set virtual size for scrolling.
       
   236                 iViewport->SetVirtualSize( virt, 0 );
       
   237                 // Set viewport to start position.
       
   238                 iViewport->SetViewportPos( iOrigin, 0 );
       
   239                 // Start scrolling to target position.
       
   240                 iViewport->SetViewportPos( target, scrollTime );
       
   241 
       
   242                 // Set line wrapping to manual.
       
   243                 iTextVisual->SetWrapping( CAlfTextVisual::ELineWrapManual );
       
   244 
       
   245                 switch ( iScrollStyle )
       
   246                     {
       
   247                     case TIRMarqueeClet::EIRScrollStyleOnce:
       
   248                         // Send EIRCustomEventScrollCompleted after scrolling is ended.
       
   249                         iTextVisual->Owner().Env().Send( TAlfCustomEventCommand(
       
   250                             TIRMarqueeClet::EIRCustomEventScrollCompleted, this ), 
       
   251                             scrollTime + iSnoozeTime );
       
   252                         break;
       
   253                     case TIRMarqueeClet::EIRScrollStyleLoop:
       
   254                         // Start loop scrolling immediately after previous scrolling has ended.
       
   255                         iTextVisual->Owner().Env().Send( TAlfCustomEventCommand(
       
   256                             TIRMarqueeClet::EIRCustomEventScrollStarted, this ), scrollTime );
       
   257                         break;
       
   258                     default:
       
   259                         break;
       
   260                     }
       
   261 
       
   262                 eventHandled = ETrue;
       
   263                 break;
       
   264                 }
       
   265              default:
       
   266                 break;
       
   267             }
       
   268         }
       
   269 
       
   270 
       
   271     return eventHandled;
       
   272     }
       
   273 
       
   274 // End of file