photosgallery/viewframework/uiutilities/src/glxscrollbar.cpp
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-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:    Scrollbar
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "glxscrollbar.h"
       
    22 
       
    23 #include <e32cmn.h>
       
    24 
       
    25 #include <AknsConstants.h>
       
    26 
       
    27 #include <alf/alfcontrol.h>
       
    28 #include <alf/alflayout.h>
       
    29 #include <alf/alfanchorlayout.h>
       
    30 #include <alf/alfimagevisual.h>
       
    31 #include <alf/alftexture.h>
       
    32 #include <alf/alfimage.h>
       
    33 
       
    34 #include <glxtexturemanager.h>
       
    35 
       
    36 #include <glxpanic.h>
       
    37 #include <glxlog.h>
       
    38 
       
    39 #include "mglxscrollbarobserver.h"
       
    40 #include "glxuiutility.h"
       
    41 
       
    42 // S60 standard size for scrollbar
       
    43 const TInt KWidth = 8;
       
    44 
       
    45 const TInt KBackgroundLayoutIndex = 0;
       
    46 const TInt KThumbLayoutIndex = 1;
       
    47 
       
    48 const TInt KTopVisualIndex = 0;
       
    49 const TInt KMiddleVisualIndex = 1;
       
    50 const TInt KBottomVisualIndex = 2;
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // Two phase constructor
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C CGlxScrollbar* CGlxScrollbar::NewLC(CAlfControl& aControl, CAlfLayout& aParentLayout)
       
    57     {
       
    58     GLX_LOG_INFO( "CGlxScrollbar::NewLC" );
       
    59 
       
    60     CGlxScrollbar* self = new (ELeave) CGlxScrollbar();
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aControl, aParentLayout );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // Two phase constructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 EXPORT_C CGlxScrollbar* CGlxScrollbar::NewL(CAlfControl& aControl, CAlfLayout& aParentLayout)
       
    71     {
       
    72     GLX_LOG_INFO( "CGlxScrollbar::NewL" );
       
    73 
       
    74     CGlxScrollbar* self = CGlxScrollbar::NewLC( aControl, aParentLayout );
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // Constructor
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CGlxScrollbar::CGlxScrollbar()
       
    84     {
       
    85     GLX_LOG_INFO( "CGlxScrollbar::CGlxScrollbar" );
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // Destructor
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CGlxScrollbar::~CGlxScrollbar()
       
    93     {
       
    94     GLX_LOG_INFO( "CGlxScrollbar::~CGlxScrollbar" );
       
    95 
       
    96     iObservers.Close();
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // Two phase constructor
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CGlxScrollbar::ConstructL(CAlfControl& aControl, CAlfLayout& aParentLayout)
       
   104     {
       
   105     GLX_LOG_INFO( "CGlxScrollbar::ConstructL" );
       
   106 
       
   107     iLayout = CAlfLayout::AddNewL( aControl, &aParentLayout );
       
   108 
       
   109     // Create the background layout and child visuals
       
   110     CAlfAnchorLayout* backgroundLayout = CAlfAnchorLayout::AddNewL( aControl, iLayout );
       
   111 
       
   112     CAlfImageVisual::AddNewL( aControl, backgroundLayout );
       
   113     CAlfImageVisual::AddNewL( aControl, backgroundLayout );
       
   114     CAlfImageVisual::AddNewL( aControl, backgroundLayout );
       
   115 
       
   116     // Create the thumb layout and child visuals
       
   117     CAlfAnchorLayout* thumbLayout = CAlfAnchorLayout::AddNewL( aControl, iLayout );
       
   118 
       
   119     CAlfImageVisual::AddNewL( aControl, thumbLayout );
       
   120     CAlfImageVisual::AddNewL( aControl, thumbLayout );
       
   121     CAlfImageVisual::AddNewL( aControl, thumbLayout );
       
   122 
       
   123     // Create and set textures for the visuals
       
   124     SetTexturesL();
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // Add an observer to observe scrollbar
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 EXPORT_C void CGlxScrollbar::AddObserverL(MGlxScrollbarObserver* aObserver)
       
   132     {
       
   133     GLX_LOG_INFO( "CGlxScrollbar::AddObserverL" );
       
   134 
       
   135     __ASSERT_DEBUG( iObservers.Find( aObserver ) == KErrNotFound, 
       
   136                     Panic( EGlxPanicIllegalArgument ) );
       
   137 
       
   138     iObservers.AppendL( aObserver );
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // Remove an observer
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C void CGlxScrollbar::RemoveObserver(MGlxScrollbarObserver* aObserver)
       
   146     {
       
   147     GLX_LOG_INFO( "CGlxScrollbar::RemoveObserver" );
       
   148 
       
   149     TInt index = iObservers.Find( aObserver );
       
   150 
       
   151     __ASSERT_DEBUG( index != KErrNotFound, Panic( EGlxPanicIllegalArgument ) );
       
   152 
       
   153     if (index != KErrNotFound) 
       
   154         {
       
   155         iObservers.Remove( index );	
       
   156         }
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // Sets the visible length of the scrollbar
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C void CGlxScrollbar::SetVisibleLength(TUint aVisibleLength)
       
   164     {
       
   165     GLX_LOG_INFO( "CGlxScrollbar::SetVisibleLength" );
       
   166 
       
   167     __ASSERT_DEBUG( aVisibleLength > 0, Panic( EGlxPanicIllegalArgument ) );
       
   168 
       
   169     iVisibleLength = aVisibleLength;
       
   170 
       
   171     NotifyObservers();
       
   172 
       
   173     Update( 0 );
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // Sets the full length of the scrollbar
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 EXPORT_C void CGlxScrollbar::SetFullLength(TUint aFullLength, TUint aTransitionTime)
       
   181     {
       
   182     GLX_LOG_INFO( "CGlxScrollbar::SetFullLength" );
       
   183 
       
   184     __ASSERT_DEBUG( aFullLength > 0, Panic( EGlxPanicIllegalArgument ) );
       
   185 
       
   186     iFullLength = aFullLength;
       
   187 
       
   188     NotifyObservers();
       
   189 
       
   190     Update( aTransitionTime );
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // Sets the position of the first visible row in the full length
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 EXPORT_C void CGlxScrollbar::SetPosition(TUint aPosition, TUint aTransitionTime)
       
   198     {
       
   199     GLX_LOG_INFO( "CGlxScrollbar::SetPosition" );
       
   200 
       
   201     __ASSERT_DEBUG( aPosition < iFullLength, Panic( EGlxPanicIllegalArgument ) );
       
   202 
       
   203     iPosition = aPosition;
       
   204     if ( iPosition > iFullLength - iVisibleLength )
       
   205         {
       
   206         iPosition = iFullLength - iVisibleLength;
       
   207         }
       
   208 
       
   209     Update( aTransitionTime );
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // Set the textures for the visuals
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void CGlxScrollbar::SetTexturesL()
       
   217     {
       
   218     GLX_LOG_INFO( "CGlxScrollbar::SetTexturesL" );
       
   219 
       
   220     CGlxUiUtility* UiUtility = CGlxUiUtility::UtilityL();
       
   221     CleanupClosePushL( *UiUtility );
       
   222 
       
   223     CGlxTextureManager& textureManager = UiUtility->GlxTextureManager();
       
   224 
       
   225     SetBackgroundTexturesL( textureManager );
       
   226     SetThumbTexturesL( textureManager );
       
   227 
       
   228     CleanupStack::PopAndDestroy( UiUtility );
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // Set the textures for the visuals in the background layout
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 void CGlxScrollbar::SetBackgroundTexturesL(CGlxTextureManager& aTextureManager)
       
   236     {
       
   237     GLX_LOG_INFO( "CGlxScrollbar::SetBackgroundTexturesL" );
       
   238 
       
   239     CAlfAnchorLayout& backgroundLayout = 
       
   240         static_cast<CAlfAnchorLayout&>( iLayout->Visual( KBackgroundLayoutIndex ) );
       
   241 
       
   242     CAlfTexture& bgTopTexture = 
       
   243         aTextureManager.CreateAvkonIconTextureL( KAknsIIDQsnCpScrollBgTop,
       
   244                                                  TSize( KWidth, KWidth ) );
       
   245 
       
   246     CAlfImageVisual& bgTop = 
       
   247         static_cast<CAlfImageVisual&>( backgroundLayout.Visual( KTopVisualIndex ) );
       
   248     bgTop.SetImage( TAlfImage( bgTopTexture ) );
       
   249 
       
   250     CAlfTexture& bgMiddleTexture = 
       
   251         aTextureManager.CreateAvkonIconTextureL( KAknsIIDQsnCpScrollBgMiddle,
       
   252                                                  TSize( KWidth, KWidth ) );
       
   253 
       
   254     CAlfImageVisual& bgMiddle = 
       
   255         static_cast<CAlfImageVisual&>( backgroundLayout.Visual( KMiddleVisualIndex ) );
       
   256     bgMiddle.SetImage( TAlfImage( bgMiddleTexture ) );
       
   257 
       
   258     CAlfTexture& bgBottomTexture = 
       
   259         aTextureManager.CreateAvkonIconTextureL( KAknsIIDQsnCpScrollBgBottom,
       
   260                                                  TSize( KWidth, KWidth ) );
       
   261 
       
   262     CAlfImageVisual& bgBottom = 
       
   263         static_cast<CAlfImageVisual&>( backgroundLayout.Visual( KBottomVisualIndex ) );
       
   264     bgBottom.SetImage( TAlfImage( bgBottomTexture ) );
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // Set the textures for the visuals in the thumb layout
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CGlxScrollbar::SetThumbTexturesL(CGlxTextureManager& aTextureManager)
       
   272     {
       
   273     GLX_LOG_INFO( "CGlxScrollbar::SetThumbTexturesL" );
       
   274 
       
   275     CAlfAnchorLayout& thumbLayout = 
       
   276         static_cast<CAlfAnchorLayout&>( iLayout->Visual( KThumbLayoutIndex ) );
       
   277 
       
   278     CAlfTexture& thumbTopTexture = 
       
   279         aTextureManager.CreateAvkonIconTextureL( KAknsIIDQsnCpScrollHandleTop,
       
   280                                                  TSize( KWidth, KWidth ) );
       
   281 
       
   282     CAlfImageVisual& thumbTop = 
       
   283         static_cast<CAlfImageVisual&>( thumbLayout.Visual( KTopVisualIndex ) );
       
   284     thumbTop.SetImage( TAlfImage( thumbTopTexture ) );
       
   285 
       
   286     CAlfTexture& thumbMiddleTexture = 
       
   287         aTextureManager.CreateAvkonIconTextureL( KAknsIIDQsnCpScrollHandleMiddle,
       
   288                                                  TSize( KWidth, KWidth ) );
       
   289 
       
   290     CAlfImageVisual& thumbMiddle = 
       
   291         static_cast<CAlfImageVisual&>( thumbLayout.Visual( KMiddleVisualIndex ) );
       
   292     thumbMiddle.SetImage( TAlfImage( thumbMiddleTexture ) );
       
   293 
       
   294     CAlfTexture& thumbBottomTexture = 
       
   295         aTextureManager.CreateAvkonIconTextureL( KAknsIIDQsnCpScrollHandleBottom,
       
   296                                                  TSize( KWidth, KWidth ) );
       
   297 
       
   298     CAlfImageVisual& thumbBottom = 
       
   299         static_cast<CAlfImageVisual&>( thumbLayout.Visual( KBottomVisualIndex ) );
       
   300     thumbBottom.SetImage( TAlfImage( thumbBottomTexture ) );
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // Set the background or thumb layout anchors
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 void CGlxScrollbar::SetAnchors(CAlfAnchorLayout& aLayout)
       
   308     {
       
   309     GLX_LOG_INFO( "CGlxScrollbar::SetAnchors" );
       
   310 
       
   311     TInt width = aLayout.Size().Target().AsSize().iWidth;
       
   312 
       
   313     aLayout.SetAnchor(EAlfAnchorTopLeft, KTopVisualIndex, 
       
   314                       EAlfAnchorOriginLeft, EAlfAnchorOriginTop, 
       
   315                       EAlfAnchorMetricAbsolute, EAlfAnchorMetricAbsolute, 
       
   316                       TAlfTimedPoint(0, 0));
       
   317     aLayout.SetAnchor(EAlfAnchorBottomRight, KTopVisualIndex, 
       
   318                       EAlfAnchorOriginRight, EAlfAnchorOriginTop, 
       
   319                       EAlfAnchorMetricAbsolute, EAlfAnchorMetricAbsolute, 
       
   320                       TAlfTimedPoint(0, width));
       
   321 
       
   322     aLayout.SetAnchor(EAlfAnchorTopLeft, KMiddleVisualIndex, 
       
   323                       EAlfAnchorOriginLeft, EAlfAnchorOriginTop, 
       
   324                       EAlfAnchorMetricAbsolute, EAlfAnchorMetricAbsolute, 
       
   325                       TAlfTimedPoint(0, width));
       
   326     aLayout.SetAnchor(EAlfAnchorBottomRight, KMiddleVisualIndex, 
       
   327                       EAlfAnchorOriginRight, EAlfAnchorOriginBottom, 
       
   328                       EAlfAnchorMetricAbsolute, EAlfAnchorMetricAbsolute, 
       
   329                       TAlfTimedPoint(0, -width));
       
   330 
       
   331     aLayout.SetAnchor(EAlfAnchorTopLeft, KBottomVisualIndex, 
       
   332                       EAlfAnchorOriginLeft, EAlfAnchorOriginBottom, 
       
   333                       EAlfAnchorMetricAbsolute, EAlfAnchorMetricAbsolute, 
       
   334                       TAlfTimedPoint(0, -width));
       
   335     aLayout.SetAnchor(EAlfAnchorBottomRight, KBottomVisualIndex, 
       
   336                       EAlfAnchorOriginRight, EAlfAnchorOriginBottom, 
       
   337                       EAlfAnchorMetricAbsolute, EAlfAnchorMetricAbsolute, 
       
   338                       TAlfTimedPoint(0, 0));
       
   339 
       
   340     aLayout.UpdateChildrenLayout();
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // Update the scrollbar
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 void CGlxScrollbar::Update(TUint aTransitionTime)
       
   348     {
       
   349     GLX_LOG_INFO( "CGlxScrollbar::Update" );
       
   350 
       
   351     TSize layoutSize = iLayout->Size().Target().AsSize();
       
   352 
       
   353     TSize thumbSize(layoutSize);
       
   354     TPoint thumbPos( 0, 0 );
       
   355     if ( iFullLength > iVisibleLength )
       
   356         {
       
   357         thumbSize.iHeight = layoutSize.iHeight * iVisibleLength / iFullLength;
       
   358         thumbPos.iY = layoutSize.iHeight * iPosition / iFullLength;
       
   359         }
       
   360 
       
   361     CAlfAnchorLayout& backgroundLayout = 
       
   362         static_cast<CAlfAnchorLayout&>( iLayout->Visual( KBackgroundLayoutIndex ) );
       
   363 
       
   364     backgroundLayout.SetPos( TPoint( 0, 0 ) );
       
   365     backgroundLayout.SetSize( layoutSize );
       
   366     SetAnchors( backgroundLayout );
       
   367 
       
   368     CAlfAnchorLayout& thumbLayout = 
       
   369         static_cast<CAlfAnchorLayout&>( iLayout->Visual( KThumbLayoutIndex ) );
       
   370 
       
   371     thumbLayout.SetPos( thumbPos, aTransitionTime );
       
   372     thumbLayout.SetSize( thumbSize, aTransitionTime );
       
   373     SetAnchors( thumbLayout );
       
   374     }
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // Notify observers if the scrollbar is scrollable
       
   378 // -----------------------------------------------------------------------------
       
   379 //
       
   380 void CGlxScrollbar::NotifyObservers()
       
   381     {
       
   382     GLX_LOG_INFO( "CGlxScrollbar::NotifyObservers" );
       
   383 
       
   384     TBool scrollable(EFalse);
       
   385     if ( iFullLength > iVisibleLength )
       
   386         {
       
   387         scrollable = ETrue;
       
   388         }
       
   389 
       
   390     TInt count = iObservers.Count();
       
   391     for ( TInt i = 0; i < count; ++i )
       
   392         {
       
   393         iObservers[i]->HandleScrollable(scrollable);
       
   394         }
       
   395     }