widgetmodel/alfwidgetmodel/src/alfrecyclinggridelementimpl.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     1 /*
       
     2 * Copyright (c) 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:   Private Implementation file for AlfRecyclingGridElement.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "alfrecyclinggridelementimpl.h"
       
    20 #include "autoarrayptr.h"
       
    21 #include <uiacceltk/HuiLayout.h> // Just for CHuiLayout::EExpandVertically
       
    22 #include <alf/alfenv.h>
       
    23 #include <alf/alfconstants.h>
       
    24 #include <alf/alfcommand.h>
       
    25 
       
    26 #include <alf/alfgridlayout.h>
       
    27 #include <alf/alflayout.h>
       
    28 #include <alf/alfcontrol.h>
       
    29 #include <alf/alfevent.h>
       
    30 
       
    31 #include "alf/ialfvisualtemplate.h"
       
    32 #include <alf/alfvarianttype.h>
       
    33 #include <alf/alfwidgetcontrol.h>
       
    34 #include <alf/alfvisualexception.h>
       
    35 #include <alf/alfwidgetevents.h>
       
    36 #include <osn/ustring.h>
       
    37 #include <memory>
       
    38 #include <osn/osnnew.h>
       
    39 
       
    40 using namespace osncore;
       
    41 
       
    42 _LIT8(KMainLayoutName, "MainLayout");
       
    43 _LIT8(KScrollLayoutName, "ScrollLayout");
       
    44 
       
    45 const int KDefaultScrollingTime = 400;
       
    46 
       
    47 using namespace Alf;
       
    48 
       
    49 AlfRecyclingGridElementImpl::AlfRecyclingGridElementImpl(
       
    50     AlfRecyclingGridElement& aOwner,
       
    51     int aNumColumns,
       
    52     int aNumRows) : mOwner( aOwner )
       
    53     {
       
    54     mNumVisibleColumns     = aNumColumns;
       
    55     mNumVisibleRows     = aNumRows;
       
    56     mNumColumns      = 0;
       
    57     mNumRows      = 0;
       
    58     mFillStyle      = AlfRecyclingGridElement::AlfRecyclingGridElement::EColumnsFirst;
       
    59     mFillStyleParam     = 0;
       
    60     mScrollingTime     = KDefaultScrollingTime;
       
    61     mFadeInTime      = KDefaultScrollingTime / 2.f;
       
    62     mFadeInDelay     = KDefaultScrollingTime / 2.f;
       
    63     mFadeOutTime     = KDefaultScrollingTime / 2.f;
       
    64     mFadeOutDelay     = 0;
       
    65     mScrollOffset.iX    = 0;
       
    66     mScrollOffset.iY    = 0;
       
    67     mHorizontalOffset    = false;
       
    68     mVerticalOffset     = false;
       
    69 
       
    70     setFillStyle(AlfRecyclingGridElement::AlfRecyclingGridElement::EColumnsFirst, aNumColumns);
       
    71     }
       
    72 
       
    73 //----------------------------------------------------------------
       
    74 // Class Destructor
       
    75 //----------------------------------------------------------------
       
    76 AlfRecyclingGridElementImpl::~AlfRecyclingGridElementImpl()
       
    77     {
       
    78     }
       
    79 
       
    80 //----------------------------------------------------------------
       
    81 // Sets the fill style for the Recycle grid element
       
    82 //----------------------------------------------------------------
       
    83 void AlfRecyclingGridElementImpl::setFillStyle(AlfRecyclingGridElement::FillStyle aFillStyle, int aFillStyleParam)
       
    84     {
       
    85     mFillStyle = aFillStyle;
       
    86     mFillStyleParam = aFillStyleParam;
       
    87     mNumRows = mNumVisibleRows;
       
    88     mNumColumns = mNumVisibleColumns;
       
    89     if (mFillStyle == AlfRecyclingGridElement::EColumnsFirst ||
       
    90             (mFillStyle == AlfRecyclingGridElement::ERowsFirst && mFillStyleParam > mNumRows))
       
    91         {
       
    92         mNumRows++; // 1 extra row for scrolling
       
    93         }
       
    94     if (mFillStyle == AlfRecyclingGridElement::ERowsFirst ||
       
    95             (mFillStyle == AlfRecyclingGridElement::EColumnsFirst && mFillStyleParam > mNumColumns))
       
    96         {
       
    97         mNumColumns++; // 1 extra column for scrolling
       
    98         }
       
    99     }
       
   100 
       
   101 
       
   102 void AlfRecyclingGridElementImpl::setAnimationTimes(
       
   103     int aScrollingTime, int aFadeInTime, int aFadeInDelay,
       
   104     int aFadeOutTime, int aFadeOutDelay )
       
   105     {
       
   106     mScrollingTime = aScrollingTime;
       
   107     mFadeInTime = aFadeInTime;
       
   108     mFadeInDelay = aFadeInDelay;
       
   109     mFadeOutTime = aFadeOutTime;
       
   110     mFadeOutDelay = aFadeOutDelay;
       
   111     }
       
   112 
       
   113 
       
   114 CAlfVisual* AlfRecyclingGridElementImpl::createVisualTree(
       
   115     IAlfVariantType& aData, uint aDataID, CAlfLayout* aParentLayout, int aLayoutIndex )
       
   116     {
       
   117     CAlfLayout *main = NULL;
       
   118     CAlfGridLayout *grid = NULL;
       
   119     CAlfVisual* visualTree = mOwner.findVisual(aDataID);
       
   120 
       
   121     if ( visualTree )
       
   122         {
       
   123         main = (CAlfLayout *)visualTree;
       
   124         grid = (CAlfGridLayout *)&main->Visual( 0 );
       
   125         }
       
   126     else
       
   127         {
       
   128         main = CAlfLayout::AddNewL((CAlfControl &)mOwner.control());
       
   129         main->SetTagL(KMainLayoutName);
       
   130 
       
   131         // Create grid layout
       
   132         grid = CAlfGridLayout::AddNewL((CAlfControl&)mOwner.control(),mNumColumns, mNumRows, main);
       
   133 
       
   134         // Scroll layout for additional content,
       
   135         // created by the visual template or other elements.
       
   136         CAlfLayout *scroll = CAlfLayout::AddNewL((CAlfControl&)mOwner.control(), main);
       
   137         scroll->SetTagL(KScrollLayoutName);
       
   138 
       
   139         if (mOwner.getVisualTemplate())
       
   140             {
       
   141             mOwner.getVisualTemplate()->createVisualTree(
       
   142                 mOwner.control(), aData.branch()->data(), scroll, scroll->Count());
       
   143             }
       
   144         if (aParentLayout)
       
   145             {
       
   146             aParentLayout->Insert(main, aLayoutIndex );
       
   147             }
       
   148         }
       
   149 
       
   150     TAlfRealPoint size = main->Size().ValueNow();
       
   151     TAlfRealPoint itemSize(size.iX / (TReal)mNumVisibleColumns,size.iY / (TReal)mNumVisibleRows);
       
   152     TAlfRealPoint gridSize(itemSize.iX * mNumColumns, itemSize.iY * mNumRows);
       
   153     grid->SetSize(gridSize);
       
   154 
       
   155     mOwner.addVisualTree(main, aDataID);
       
   156 
       
   157     CAlfLayout *parent = NULL;
       
   158     if (mFillStyle == AlfRecyclingGridElement::EColumnsFirst)
       
   159         {
       
   160         // No reordering needed, add directly to the grid layout
       
   161         parent = grid;
       
   162         }
       
   163     CAlfVisual** array = new( EMM ) CAlfVisual*[mNumColumns * mNumRows];
       
   164 
       
   165     // Ownership transferred to the autoptr.
       
   166     auto_arrayptr<CAlfVisual*> arrayCleanup( array );
       
   167 
       
   168     int childIndex = 0;
       
   169     // Create the children visual trees
       
   170     if ( aData.type() == IAlfVariantType::EBranch &&
       
   171             aData.branch()->childrenCount())
       
   172         {
       
   173         IAlfBranch* branch = aData.branch();
       
   174         TPoint childPos = calculateChildPos(0);
       
   175         int maxColumn = mScrollOffset.iX + mNumColumns;
       
   176         int maxRow = mScrollOffset.iY + mNumRows;
       
   177         for (int i = 0; i < branch->childrenCount() &&
       
   178                 ( childPos.iX < maxColumn || childPos.iY < maxRow); ++i)
       
   179             {
       
   180             childPos = calculateChildPos(i);
       
   181             if (childPos.iX >= mScrollOffset.iX && childPos.iX < maxColumn &&
       
   182                     childPos.iY >= mScrollOffset.iY && childPos.iY < maxRow)
       
   183                 {
       
   184                 IAlfElement* child = mOwner.control().findElement( aData.branch()->childName(i).getUtf8());
       
   185                 uint childID = mOwner.control().elementDataId(*child, i, aDataID);
       
   186                 array[childIndex] = child->createVisualTree(
       
   187                                         *branch->childData( i ), *branch, i, aDataID, parent, childIndex );
       
   188                 childIndex++;
       
   189                 }
       
   190 
       
   191             }
       
   192         }
       
   193 
       
   194     // Reorder and insert to the grid layout
       
   195     if (mFillStyle == AlfRecyclingGridElement::ERowsFirst)
       
   196         {
       
   197         for (int i = 0; i < mNumRows && childIndex >= 0; ++i)
       
   198             {
       
   199             for (int j = 0; j < mNumColumns && childIndex >= 0; ++j)
       
   200                 {
       
   201                 int err=grid->Append(array[j * mNumRows + i]);
       
   202 				if(err!=KErrNone)
       
   203 					{
       
   204 					ALF_THROW ( AlfVisualException, err, "Grid Append Failed" )
       
   205 					}                
       
   206                 childIndex--;
       
   207                 }
       
   208             }
       
   209         }
       
   210 
       
   211     return main;
       
   212     }
       
   213 
       
   214 
       
   215 //----------------------------------------------------------------
       
   216 // Gets the Point of  a child at a given index
       
   217 //----------------------------------------------------------------
       
   218 TPoint AlfRecyclingGridElementImpl::calculateChildPos(int aInd) const
       
   219     {
       
   220     if (mFillStyle == AlfRecyclingGridElement::EColumnsFirst)
       
   221         {
       
   222         return TPoint(aInd % mFillStyleParam, aInd / mFillStyleParam);
       
   223         }
       
   224     else if (mFillStyle == AlfRecyclingGridElement::ERowsFirst)
       
   225         {
       
   226         return TPoint(aInd / mFillStyleParam, aInd % mFillStyleParam);
       
   227         }
       
   228     return TPoint(0, 0);
       
   229     }
       
   230 
       
   231 //----------------------------------------------------------------
       
   232 // Gets the Index of  a child at a given point
       
   233 //----------------------------------------------------------------
       
   234 int AlfRecyclingGridElementImpl::calculateChildInd(TPoint aPos) const
       
   235     {
       
   236     if (aPos.iX < 0 || aPos.iY < 0)
       
   237         {
       
   238         return -1; // Does not exist
       
   239         }
       
   240     if (mFillStyle == AlfRecyclingGridElement::EColumnsFirst)
       
   241         {
       
   242         if (aPos.iX >= mFillStyleParam)
       
   243             {
       
   244             return -1; // Does not exist
       
   245             }
       
   246         return aPos.iY * mFillStyleParam + aPos.iX;
       
   247         }
       
   248     else if (mFillStyle == AlfRecyclingGridElement::ERowsFirst)
       
   249         {
       
   250         if (aPos.iY >= mFillStyleParam)
       
   251             {
       
   252             return -1; // Does not exist
       
   253             }
       
   254         return aPos.iX * mFillStyleParam + aPos.iY;
       
   255         }
       
   256     return 0;
       
   257     }
       
   258 
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // OfferEventL - Handle the Event.
       
   263 // ?implementation_description
       
   264 // ---------------------------------------------------------------------------
       
   265 bool AlfRecyclingGridElementImpl::offerEvent( CAlfWidgetControl& aControl, const TAlfEvent& aEvent )
       
   266     {
       
   267 
       
   268     if ( !accept( aControl, aEvent ) )
       
   269         {
       
   270         return false;
       
   271         }
       
   272     uint aEventID = aEvent.CustomParameter();
       
   273 
       
   274     if ( aEventID == EEventScrollPrimaryNext )
       
   275         {
       
   276         aEventID = ( mFillStyle == AlfRecyclingGridElement::EColumnsFirst ? EEventScrollDown : EEventScrollRight );
       
   277         }
       
   278     else if ( aEventID == EEventScrollPrimaryPrev )
       
   279         {
       
   280         aEventID = ( mFillStyle == AlfRecyclingGridElement::EColumnsFirst ? EEventScrollUp : EEventScrollLeft );
       
   281         }
       
   282     else if ( aEventID == EEventScrollSecondaryNext )
       
   283         {
       
   284         aEventID = ( mFillStyle == AlfRecyclingGridElement::EColumnsFirst ? EEventScrollRight : EEventScrollDown );
       
   285         }
       
   286     else if ( aEventID == EEventScrollSecondaryPrev )
       
   287         {
       
   288         aEventID = ( mFillStyle == AlfRecyclingGridElement::EColumnsFirst ? EEventScrollLeft : EEventScrollUp );
       
   289         }
       
   290 
       
   291     if ((int)aEventID == (int)EEventScrollPageDown )
       
   292         {
       
   293         int iter=0;
       
   294         if ((int)mFillStyle == (int)AlfRecyclingGridElement::EColumnsFirst )
       
   295             {
       
   296             iter = mNumRows-1 ;
       
   297             }
       
   298         else
       
   299             {
       
   300             iter = mNumColumns-1 ;
       
   301             }
       
   302 
       
   303         for (int i=0;i<iter;i++)
       
   304             {
       
   305             moveSingleRowOrColumn(aControl , false);
       
   306             }
       
   307         return true;
       
   308         }
       
   309     else if ((int)aEventID == (int)EEventScrollPageUp )
       
   310         {
       
   311         int iter=0;
       
   312         if ((int)mFillStyle == (int)AlfRecyclingGridElement::EColumnsFirst )
       
   313             {
       
   314             iter = mNumRows-1 ;
       
   315             }
       
   316         else
       
   317             {
       
   318             iter = mNumColumns-1 ;
       
   319             }
       
   320 
       
   321         for (int i=0;i<iter;i++)
       
   322             {
       
   323             moveSingleRowOrColumn(aControl , true);
       
   324             }
       
   325         return true;
       
   326 
       
   327         }
       
   328     else
       
   329         {
       
   330         doPositioningAndScrolling(aControl,aEventID);
       
   331         }
       
   332     return true;
       
   333 
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // ?implementation_description
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void AlfRecyclingGridElementImpl::setActiveStates( unsigned int aStates )
       
   341     {
       
   342     mActiveStates = aStates;
       
   343     }
       
   344 
       
   345 
       
   346 void AlfRecyclingGridElementImpl::doPositioningAndScrolling( CAlfWidgetControl& aControl,uint aEventID)
       
   347     {
       
   348     //unsigned int16 id = aEvent.CustomEventData();//Index of the focus element
       
   349 
       
   350     CAlfLayout *main = (CAlfLayout *)mOwner.findVisual(0);   //(CAlfLayout *)VisualTreeArray()[0];//root element
       
   351     CAlfGridLayout *grid = (CAlfGridLayout *)&main->Visual(0);
       
   352     CAlfLayout *scroll = (CAlfGridLayout *)&main->Visual(1);
       
   353 
       
   354 
       
   355     TSize size;
       
   356 
       
   357     // Grid/scroll layout movement
       
   358     TPoint layoutMovement(0, 0);
       
   359 
       
   360     // New number of the new items to reveal (and old items to hide)
       
   361     int numNewItems = 0;
       
   362 
       
   363     // Starting indices and increment step for recycling visuals
       
   364     int recycleSrcInd = 0, recycleDstInd = 0;
       
   365     int recycleIndStep = 0;
       
   366 
       
   367     // Starting indices and increment step for fading in/out visuals
       
   368     int fadeInInd = 0, fadeOutInd = 0;
       
   369     int fadeIndStep = 0;
       
   370 
       
   371     // Is recycling required?
       
   372     bool recycle = false;
       
   373 
       
   374     // Index of the first new child visual
       
   375     TPoint firstNewChildPos(0, 0);
       
   376 
       
   377     // Increment step for the new child indices
       
   378     TPoint childPosStep(0, 0);
       
   379 
       
   380     if (aEventID == EEventScrollUp) // Up
       
   381         {
       
   382         firstNewChildPos = mScrollOffset + TPoint(mHorizontalOffset ? -1 : 0, -1);
       
   383         childPosStep = TPoint(1, 0);
       
   384         mScrollOffset.iY--;
       
   385         numNewItems = mNumColumns;
       
   386         fadeInInd = mNumColumns - 1;
       
   387         fadeOutInd = grid->Count() - 1;
       
   388         recycleSrcInd = grid->Count() - mNumColumns;
       
   389         recycleIndStep = 1;
       
   390         fadeIndStep = -1;
       
   391         grid->ChildSize(recycleDstInd, size);
       
   392         layoutMovement.iY = -size.iHeight;
       
   393         recycle = !mVerticalOffset;
       
   394         mVerticalOffset = false;
       
   395 
       
   396         }
       
   397     else if (aEventID == EEventScrollDown) // Down
       
   398         {
       
   399         firstNewChildPos = mScrollOffset + TPoint(mHorizontalOffset ? -1 : 0, mNumRows - 1);
       
   400         childPosStep = TPoint(1, 0);
       
   401         mScrollOffset.iY++;
       
   402         numNewItems = mNumColumns;
       
   403         recycleDstInd = grid->Count()- 1;
       
   404         fadeInInd = grid->Count() - mNumColumns;
       
   405         fadeIndStep = 1;
       
   406         grid->ChildSize(recycleDstInd, size);
       
   407         layoutMovement.iY = size.iHeight;
       
   408 
       
   409         recycle = mVerticalOffset;
       
   410         mVerticalOffset = true;
       
   411 
       
   412 
       
   413         }
       
   414     else if (aEventID == EEventScrollLeft) // Left
       
   415         {
       
   416         firstNewChildPos = mScrollOffset + TPoint(-1, mVerticalOffset ? -1 : 0);
       
   417         childPosStep = TPoint(0, 1);
       
   418         mScrollOffset.iX--;
       
   419         numNewItems = mNumRows;
       
   420         fadeOutInd = recycleSrcInd = mNumColumns - 1;
       
   421         fadeIndStep = recycleIndStep = mNumColumns;
       
   422         grid->ChildSize(recycleDstInd, size);
       
   423         layoutMovement.iX = -size.iWidth;
       
   424         recycle = !mHorizontalOffset;
       
   425         mHorizontalOffset = false;
       
   426 
       
   427         }
       
   428 
       
   429     else if (aEventID == EEventScrollRight) // Right
       
   430         {
       
   431         firstNewChildPos = mScrollOffset + TPoint(mNumColumns - 1, mVerticalOffset ? -1 : 0);
       
   432         childPosStep = TPoint(0, 1);
       
   433         mScrollOffset.iX++;
       
   434         numNewItems = mNumRows;
       
   435         fadeInInd = recycleDstInd = mNumColumns - 1;
       
   436         fadeIndStep = recycleIndStep = mNumColumns;
       
   437         grid->ChildSize(recycleDstInd, size);
       
   438         layoutMovement.iX = size.iWidth;
       
   439         recycle = mHorizontalOffset;
       
   440         mHorizontalOffset = true;
       
   441         }
       
   442 
       
   443     IAlfVariantType *recyclingGridData = aControl.elementData(mOwner, 0);
       
   444     if ( recyclingGridData->type()!=  IAlfVariantType::EBranch)
       
   445         {
       
   446         return ;
       
   447         }
       
   448 
       
   449     aControl.Env().PauseRefresh();
       
   450 
       
   451     // Perform scrolling
       
   452     if (aEventID == EEventScrollPageDown || aEventID == EEventScrollPageUp || aEventID == EEventScrollUp || aEventID == EEventScrollDown ||aEventID == EEventScrollLeft || aEventID == EEventScrollRight)
       
   453         {
       
   454         if (recycle)
       
   455             {
       
   456             // Move the layout
       
   457             grid->SetPos(grid->Pos().Target() + layoutMovement);
       
   458             // Recycle visual trees
       
   459             for (int i = 0; i < numNewItems; ++i)
       
   460                 {
       
   461                 CAlfVisual &visual = grid->Visual(recycleSrcInd + i * recycleIndStep);
       
   462                 grid->Reorder(visual, recycleDstInd + i * recycleIndStep);
       
   463                 TPoint childPos = firstNewChildPos + TPoint(i * childPosStep.iX, i * childPosStep.iY);
       
   464                 int childInd = calculateChildInd(childPos);
       
   465                 if (childInd >= 0 && childInd < recyclingGridData->branch()->childrenCount())
       
   466                     {
       
   467                     IAlfVariantType *recyclingGridChildData = recyclingGridData->branch()->childData(childInd);
       
   468 
       
   469                     const UString& childName  = recyclingGridData->branch()->childName(childInd);
       
   470                     IAlfElement *childElement = mOwner.control().findElement(childName.getUtf8());
       
   471                     if (childElement!=NULL)
       
   472                         {
       
   473                         int newDataID= mOwner.control().elementDataId(*childElement,childInd, 0);
       
   474                         childElement->replaceVisualTree(
       
   475                             *recyclingGridChildData,newDataID ,childElement->dataID(visual));
       
   476                         }
       
   477                     }
       
   478                 }
       
   479             }
       
   480         else
       
   481             {
       
   482             scroll->SetPos(scroll->Pos().Target() - layoutMovement, mScrollingTime);
       
   483             }
       
   484         // Fade in / out
       
   485         for (int i = 0; i < numNewItems; ++i)
       
   486             {
       
   487             // Fade out the old items
       
   488             CAlfVisual &oldItem = grid->Visual(fadeOutInd + i * fadeIndStep);
       
   489             if ( mFadeOutDelay == 0 )
       
   490                 {
       
   491                 TAlfTimedValue zero;
       
   492                 zero.SetValueNow(1.f);
       
   493                 zero.SetTarget(0.f, mFadeOutTime);
       
   494                 oldItem.SetOpacity(zero);
       
   495                 }
       
   496             else
       
   497                 {
       
   498                 TAlfValueCommand cmd((void*)&oldItem, EAlfValueCommandVisualSetOpacity, 0.f, mFadeOutTime);
       
   499                 aControl.Env().Send(cmd, mFadeOutDelay);
       
   500                 }
       
   501             // Fade in the new items
       
   502             CAlfVisual &newItem = grid->Visual(fadeInInd + i * fadeIndStep);
       
   503             TAlfTimedValue zero2;
       
   504             zero2.SetValueNow(0.f);
       
   505             zero2.SetTarget(0.f, 0);
       
   506             newItem.SetOpacity(zero2);
       
   507             TPoint childPos = firstNewChildPos + TPoint(i * childPosStep.iX, i * childPosStep.iY);
       
   508             int childInd = calculateChildInd(childPos);
       
   509             IAlfVariantType *recyclingGridChildData = recyclingGridData->branch()->childData(childInd);
       
   510             if (childInd >= 0 && recyclingGridChildData!=NULL)
       
   511                 {
       
   512                 if ( mFadeInDelay == 0 )
       
   513                     {
       
   514                     TAlfTimedValue one;
       
   515                     one.SetValueNow(0.f);
       
   516                     one.SetTarget(1.f, mFadeInTime);
       
   517                     newItem.SetOpacity(one);
       
   518                     }
       
   519                 else
       
   520                     {
       
   521                     TAlfValueCommand cmd((void*)&newItem, EAlfValueCommandVisualSetOpacity, 1.f, mFadeInTime);
       
   522                     aControl.Env().Send(cmd, mFadeInDelay);
       
   523                     }
       
   524                 }
       
   525             else
       
   526                 {
       
   527                 newItem.SetOpacity( TAlfTimedValue(0) );
       
   528                 }
       
   529             }
       
   530         // Animate layout scrolling
       
   531         grid->SetPos(grid->Pos().Target() - layoutMovement, mScrollingTime);
       
   532         }
       
   533 
       
   534     aControl.Env().ContinueRefresh();
       
   535     }
       
   536 
       
   537 // ---------------------------------------------------------------------------
       
   538 // OfferEventL - Handle the Event.
       
   539 // ?implementation_description
       
   540 // ---------------------------------------------------------------------------
       
   541 bool AlfRecyclingGridElementImpl::moveSingleRowOrColumn( CAlfWidgetControl& aControl, bool aPageUp)
       
   542     {
       
   543     uint aEventID;
       
   544 
       
   545     if (!aPageUp)
       
   546         {
       
   547         aEventID = (mFillStyle == AlfRecyclingGridElement::EColumnsFirst ? EEventScrollDown : EEventScrollRight);
       
   548         }
       
   549     else
       
   550         {
       
   551         aEventID = (mFillStyle == AlfRecyclingGridElement::EColumnsFirst ? EEventScrollUp : EEventScrollLeft);
       
   552         }
       
   553 
       
   554     doPositioningAndScrolling(aControl,aEventID);
       
   555     return true;
       
   556     }
       
   557 
       
   558 
       
   559 bool AlfRecyclingGridElementImpl::accept( CAlfWidgetControl& /*aControl*/, const TAlfEvent& aEvent ) const
       
   560     {
       
   561 
       
   562     uint aEventID;
       
   563     if (!aEvent.IsCustomEvent())
       
   564         {
       
   565         return false;
       
   566         }
       
   567     aEventID = aEvent.CustomParameter();
       
   568     if ( aEventID == EEventScrollPrimaryNext || aEventID == EEventScrollPrimaryPrev ||
       
   569             aEventID == EEventScrollSecondaryNext || aEventID == EEventScrollSecondaryPrev ||
       
   570             aEventID == EEventScrollToFirst || aEventID == EEventScrollToLast ||
       
   571             aEventID == EEventScrollUp || aEventID == EEventScrollDown ||
       
   572             aEventID == EEventScrollLeft || aEventID == EEventScrollRight ||
       
   573             aEventID == EEventScrollPageDown || aEventID == EEventScrollPageUp)
       
   574         {
       
   575         return true;
       
   576         }
       
   577 
       
   578     return false;
       
   579     }
       
   580 
       
   581 
       
   582 void AlfRecyclingGridElementImpl::createChildVisualTree(
       
   583     IAlfElement* aElement, IAlfVariantType& aChildData,
       
   584     IAlfBranch& aData, int aIndex, uint aDataID )
       
   585     {
       
   586     int firstVisibleInd=0, lastVisibleInd=0;
       
   587     int firstVisibleDataId=0, lastVisibleDataId=0;
       
   588     CAlfLayout *main = (CAlfLayout *)mOwner.findVisual(0);
       
   589     CAlfGridLayout *grid = (CAlfGridLayout *)&main->Visual(0);
       
   590 
       
   591     if (grid->Count() > 0)
       
   592         {
       
   593         CAlfVisual &first  = grid->Visual( 0 );
       
   594         CAlfVisual &last   = grid->Visual( grid->Count() - 1 );
       
   595 
       
   596         firstVisibleDataId= aElement->dataID(first);
       
   597         lastVisibleDataId= aElement->dataID(last);
       
   598 
       
   599         firstVisibleInd= mOwner.control().dataIdToIndex(*aElement, firstVisibleDataId);
       
   600         lastVisibleInd= mOwner.control().dataIdToIndex(*aElement, lastVisibleDataId);
       
   601 
       
   602         // Replace the visual tree
       
   603         IAlfElement* child = mOwner.control().findElement( aData.branch()->childName(0).getUtf8());
       
   604 
       
   605 
       
   606         if (aData.childrenCount()>= mNumColumns*mNumRows)
       
   607             {
       
   608             // Check that the child index is in the visible area
       
   609             if ( aIndex >= firstVisibleInd && aIndex <= lastVisibleInd )
       
   610                 {
       
   611                 CAlfVisual &last   = grid->Visual( grid->Count()-1 );
       
   612 
       
   613                 // Move the last visual to the right position
       
   614                 grid->Reorder( last, aIndex - firstVisibleInd);
       
   615 
       
   616 
       
   617                 // Update data IDs
       
   618                 child->updateDataIDs( aData, aIndex, 1, aDataID, aDataID );
       
   619 
       
   620                 int oldElementDataID = aElement->dataID( last );
       
   621                 int newElementDataID = mOwner.control().elementDataId( *aElement, aIndex - firstVisibleInd, aDataID );
       
   622 
       
   623                 child->replaceVisualTree( aChildData, newElementDataID ,oldElementDataID );
       
   624                 }
       
   625             }
       
   626         else
       
   627             {
       
   628             int newElementDataID = mOwner.control().elementDataId( *aElement, aIndex - firstVisibleInd, aDataID );
       
   629 
       
   630             //Update data IDs
       
   631             child->updateDataIDs( aData, aIndex, 1, aDataID, aDataID );
       
   632 
       
   633             //CAlfVisual* newItem = child->createVisualTree(aChildData, aData,aData.childrenCount(), aDataID, static_cast<CAlfLayout*>(grid ));
       
   634             CAlfVisual* newItem = child->createVisualTree(aChildData, newElementDataID,static_cast<CAlfLayout*>(grid ),grid->Count()-1);
       
   635 
       
   636             // Move the last visual to the right position
       
   637             grid->Reorder( *newItem, aIndex - firstVisibleInd);
       
   638             }
       
   639         }
       
   640     else
       
   641         {
       
   642         aElement->createVisualTree( aChildData, aData, aIndex, aDataID, grid, aIndex );
       
   643         }
       
   644     }
       
   645 
       
   646 void AlfRecyclingGridElementImpl::removeChildVisualTree(
       
   647     IAlfElement* aElement, IAlfBranch& aData, int aIndex, uint aDataID )
       
   648     {
       
   649     int firstVisibleInd=0, lastVisibleInd=0;
       
   650     int firstVisibleDataId=0, lastVisibleDataId=0;
       
   651     CAlfLayout *main = (CAlfLayout *)mOwner.findVisual(0);
       
   652     CAlfGridLayout *grid = (CAlfGridLayout *)&main->Visual(0);
       
   653 
       
   654     CAlfVisual &first  = grid->Visual( 0 );
       
   655 
       
   656     if (aData.childrenCount()-1 > (grid->Count() - 1) )
       
   657         {
       
   658         CAlfVisual &last   = grid->Visual( grid->Count() - 1 );
       
   659         firstVisibleDataId= aElement->dataID(first);
       
   660         lastVisibleDataId= aElement->dataID(last);
       
   661 
       
   662         firstVisibleInd= mOwner.control().dataIdToIndex(*aElement, firstVisibleDataId);
       
   663         lastVisibleInd= mOwner.control().dataIdToIndex(*aElement, lastVisibleDataId);
       
   664 
       
   665         /*Check that the child index is in the visible area*/
       
   666         if ( aIndex >= firstVisibleInd && aIndex <= lastVisibleInd )
       
   667             {
       
   668             // Find the visual to be removed - it's used to show the next visual tree below the visible area
       
   669             CAlfVisual &removed = grid->Visual( aIndex  - firstVisibleInd );
       
   670 
       
   671             // Move it to the end of the grid
       
   672             grid->Reorder( removed, grid->Count() - 1 );
       
   673 
       
   674 
       
   675             // Replace the visual tree
       
   676             IAlfElement* child = mOwner.control().findElement( aData.branch()->childName(0).getUtf8());
       
   677 
       
   678             IAlfVariantType* data =NULL;
       
   679             data = aData.childData( lastVisibleInd + 1 );
       
   680 
       
   681             int oldElementDataID = aElement->dataID( removed );
       
   682 
       
   683             int newElementDataID ;
       
   684             newElementDataID = mOwner.control().elementDataId( *aElement, lastVisibleInd + 1, aDataID );
       
   685 
       
   686             if (data!=NULL)
       
   687                 {
       
   688                 child->replaceVisualTree(*data, newElementDataID, oldElementDataID);
       
   689                 }
       
   690 
       
   691             if (aData.childrenCount()-1 <= (grid->Count() - 1 ))
       
   692                 {
       
   693                 child->removeVisualTree(aData, aData.childrenCount()-1,aDataID);
       
   694                 }
       
   695 
       
   696             // Update data IDs
       
   697             child->updateDataIDs( aData, aIndex, -1, aDataID, aDataID );
       
   698             }
       
   699         }
       
   700     else
       
   701         {
       
   702         CAlfVisual &last   = grid->Visual(aData.childrenCount()-1);
       
   703         firstVisibleDataId= aElement->dataID(first);
       
   704         lastVisibleDataId= aElement->dataID(last);
       
   705 
       
   706         firstVisibleInd= mOwner.control().dataIdToIndex(*aElement, firstVisibleDataId);
       
   707         lastVisibleInd= mOwner.control().dataIdToIndex(*aElement, lastVisibleDataId);
       
   708 
       
   709 
       
   710         /*Check that the child index is in the visible area*/
       
   711         if ( aIndex >= firstVisibleInd && aIndex <= lastVisibleInd )
       
   712             {
       
   713             // Find the visual to be removed - it's used to show the next visual tree below the visible area
       
   714             CAlfVisual &removed = grid->Visual( aIndex  - firstVisibleInd );
       
   715 
       
   716             grid->Reorder( removed, aData.childrenCount()-1);
       
   717 
       
   718             // Replace the visual tree
       
   719             IAlfElement* child = mOwner.control().findElement( aData.branch()->childName(0).getUtf8());
       
   720 
       
   721             IAlfVariantType* data =NULL;
       
   722             data = aData.childData( aData.childrenCount() -1 );
       
   723 
       
   724             // Replace the visual tree
       
   725 
       
   726             CAlfVisual &last   = grid->Visual( aData.childrenCount()-1 );
       
   727 
       
   728             int oldElementDataID = aElement->dataID( removed );
       
   729 
       
   730             int newElementDataID ;
       
   731             newElementDataID = mOwner.control().elementDataId( *aElement, aData.childrenCount()-1, aDataID );
       
   732 
       
   733             if (data!=NULL)
       
   734                 {
       
   735                 child->replaceVisualTree(*data, newElementDataID, oldElementDataID);
       
   736                 }
       
   737 
       
   738             if (aData.childrenCount()-1 <= (grid->Count() - 1 ))
       
   739                 {
       
   740                 child->removeVisualTree(aData, aData.childrenCount()-1,aDataID);
       
   741                 }
       
   742 
       
   743             // Update data IDs
       
   744             child->updateDataIDs( aData, aIndex, -1, aDataID, aDataID );
       
   745             }
       
   746         }
       
   747     }
       
   748 // ---------------------------------------------------------------------------
       
   749 // Description: Do not destroy visuals, because it's done elsewhere.
       
   750 // ---------------------------------------------------------------------------
       
   751 void AlfRecyclingGridElementImpl::removeAndDestroyVisuals( int /*aTimeMilliseconds*/ )
       
   752     {
       
   753     }
       
   754 
       
   755 // End of File.