widgetmodel/alfwidgetmodel/src/alfgridlayoutmanagerimpl.cpp
branchRCL_3
changeset 19 4ea6f81c838a
parent 17 514d98f21c43
child 20 0e9bb658ef58
equal deleted inserted replaced
17:514d98f21c43 19:4ea6f81c838a
     1 /*
       
     2 * Copyright (c) 2007 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:  layoutmanager implementation class with focus handling in 1D (next/previous)
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <alf/ialfwidgetcontrol.h>
       
    20 #include <alf/alfvisual.h>
       
    21 #include <alf/alfgridlayout.h>
       
    22 #include "alf/alfwidget.h"
       
    23 #include <alf/alfvisualfactory.h>
       
    24 #include <alf/alfexceptions.h>
       
    25 #include <alf/ialflayoutpreferences.h>
       
    26 #include <osn/osnnew.h>
       
    27 
       
    28 //stl
       
    29 #include <algorithm> //for min & max
       
    30 
       
    31 #include "alfgridlayoutmanagerimpl.h"
       
    32 
       
    33 using namespace Alf;
       
    34 
       
    35 namespace Alf
       
    36     {
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38     
       
    39 // ---------------------------------------------------------------------------
       
    40 // layoutmanagerimpl constructor
       
    41 // ---------------------------------------------------------------------------
       
    42 //    
       
    43 AlfGridLayoutManagerImpl::AlfGridLayoutManagerImpl(
       
    44     AlfGridLayoutManager& aGridLayoutManager) : 
       
    45     mGridLayoutManager(aGridLayoutManager)
       
    46     {
       
    47     }
       
    48     
       
    49 // ---------------------------------------------------------------------------
       
    50 // creates the layout used by the layoutmanager.
       
    51 // ---------------------------------------------------------------------------
       
    52 //    
       
    53 AlfGridLayoutManagerImpl::~AlfGridLayoutManagerImpl()
       
    54     {
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // from IAlfLayoutPreferences
       
    59 // Returns the minimum area that this layout manager can occupy by observing
       
    60 // the minimum sizes of the child UI elements in the layout
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 bool AlfGridLayoutManagerImpl::getMinimumSize( TAlfXYMetric& /*aMinSize*/ ) const
       
    64 	{
       
    65 	return false;
       
    66 	}
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // from IAlfLayoutPreferences
       
    70 // Returns the maximum area that this layout manager can occupy by observing
       
    71 // the maximum sizes of the child UI elements in the layout
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 bool AlfGridLayoutManagerImpl::getMaximumSize( TAlfXYMetric& /*aMaxSize*/ ) const
       
    75 	{
       
    76 	return false;
       
    77 	}
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Combines and returns the preferred sizes of all child UI elements according
       
    81 // to the layouting rules.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 bool AlfGridLayoutManagerImpl::getPreferredSize(TAlfXYMetric& aPreferredSize) const
       
    85 	{	
       
    86 	bool hasPreferredSize = false;
       
    87 	TAlfXYMetric resultSize = TAlfXYMetric(TAlfMetric(0), TAlfMetric(0));
       
    88 		
       
    89     const CAlfGridLayout& gridlayout = layout();
       
    90     const int childCount = mGridLayoutManager.count();
       
    91     const int gridColCount = gridlayout.ColumnCount();
       
    92     
       
    93     //iterate the columns to calculate the preferred width.
       
    94     const int colCount = std::min<int>(gridColCount, childCount);
       
    95     for (int i = 0; i < colCount; i++)
       
    96         {
       
    97         CAlfWidgetControl* childWidgetControl = mGridLayoutManager.getControl(i);
       
    98         //getControl may return null, but getLayoutPreferences checks that.
       
    99         const IAlfLayoutPreferences* layoutPrefs = 
       
   100             mGridLayoutManager.getLayoutPreferences(childWidgetControl);        
       
   101         
       
   102         if (layoutPrefs)
       
   103             {
       
   104             TAlfXYMetric prefSize;
       
   105             if (layoutPrefs->getPreferredSize(prefSize))
       
   106                 {
       
   107                 if(prefSize.iX.iUnit == EAlfUnitPixel)
       
   108                     {
       
   109                     resultSize.iX.iMagnitude += prefSize.iX.iMagnitude;
       
   110                     hasPreferredSize = true;
       
   111                     }
       
   112                 }
       
   113             }
       
   114         }
       
   115     
       
   116     //iterate the rows to calculate the preferred height  
       
   117     if (hasPreferredSize)
       
   118         {
       
   119         //count rows, that have at least one visual.
       
   120         int usedRows = childCount / gridColCount;
       
   121         if (childCount % gridColCount)
       
   122             {
       
   123             //last wor not filled completely, but still valid to get preferred height
       
   124             //for this row also.
       
   125             usedRows++;
       
   126             }
       
   127             
       
   128         const int rowCount = std::min<int>(gridlayout.RowCount(), usedRows);
       
   129         for (int i = 0; i < rowCount; i+= gridColCount)
       
   130             {
       
   131             CAlfWidgetControl* childWidgetControl = mGridLayoutManager.getControl(i);
       
   132             //getControl may return null, but getLayoutPreferences checks that.
       
   133             const IAlfLayoutPreferences* layoutPrefs = 
       
   134                 mGridLayoutManager.getLayoutPreferences(childWidgetControl);        
       
   135             
       
   136             if (layoutPrefs)
       
   137                 {
       
   138                 TAlfXYMetric prefSize;
       
   139                 if (layoutPrefs->getPreferredSize(prefSize))
       
   140                     {
       
   141                     if(prefSize.iY.iUnit == EAlfUnitPixel)
       
   142                         {
       
   143                         resultSize.iY.iMagnitude += prefSize.iY.iMagnitude;
       
   144                         hasPreferredSize = true;
       
   145                         }
       
   146                     }
       
   147                 }
       
   148             }
       
   149         }
       
   150         
       
   151     aPreferredSize = resultSize;
       
   152     return hasPreferredSize;
       
   153 	}
       
   154  
       
   155 // ---------------------------------------------------------------------------
       
   156 // from IAlfLayoutPreferences
       
   157 // At the moment doesn't do anything since preferred size is being queried
       
   158 // from the child UI elements added to this layout manager.
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void AlfGridLayoutManagerImpl::setPreferredSize( const TAlfXYMetric& aPreferredSize)
       
   162 	{
       
   163 	TAlfTimedPoint size((float)aPreferredSize.iX.iMagnitude,(float)aPreferredSize.iY.iMagnitude);
       
   164 	layout().SetSize(size);
       
   165 	mGridLayoutManager.owner().updateParentLayout();
       
   166 	}
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // From class IAlfInterfaceBase.
       
   170 // Getter for interfaces provided by the element.
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 IAlfInterfaceBase* AlfGridLayoutManagerImpl::makeInterface(const IfId& aType)
       
   174     {
       
   175     UString param(aType.mImplementationId);
       
   176     if (param == IAlfLayoutPreferences::type().mImplementationId)
       
   177         {
       
   178         return this;
       
   179         }      
       
   180     
       
   181     return NULL;
       
   182     }
       
   183     
       
   184 // ---------------------------------------------------------------------------
       
   185 // returns the gridlayout used by the layoutmanager
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 CAlfGridLayout& AlfGridLayoutManagerImpl::layout() const
       
   189     {
       
   190     return dynamic_cast<CAlfGridLayout&>(mGridLayoutManager.getLayout());
       
   191     }
       
   192 
       
   193  } // Alf