menufw/menufwui/mmwidgets/src/mmgridcontainer.cpp
changeset 0 f72a12da539e
child 1 5315654608de
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     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:   
       
    15 *  Version     : %version: MM_72 % << Don't touch! Updated by Synergy at check-out.
       
    16 *  Version     : %version: MM_72 % << Don't touch! Updated by Synergy at check-out.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include "mmgridcontainer.h"
       
    22 #include "mmgridmodel.h"
       
    23 #include "mmlistboxmodel.h"
       
    24 #include "mmmarqueeadapter.h"
       
    25 #include <AknsLayeredBackgroundControlContext.h>
       
    26 #include <AknsListBoxBackgroundControlContext.h>
       
    27 #include <layoutmetadata.cdl.h>
       
    28 #include "mmhighlighttimer.h"
       
    29 #include "hnsuitemodel.h"
       
    30 #include "mmgrid.h"
       
    31 #include "mmwidgetsconstants.h"
       
    32 #include "hnglobals.h"
       
    33 #include "mmpostevaluationprocessor.h"
       
    34 #include "mmlistboxitemdrawer.h"
       
    35 #include "mmdraweranimator.h"
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CMmGridContainer::NewLC()
       
    39 // Two-phased constructor.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CMmGridContainer* CMmGridContainer::NewLC( const TRect& aRect, 
       
    43         MObjectProvider* aObjectProvider, CMmTemplateLibrary* aLibrary )
       
    44     {        
       
    45     CMmGridContainer* self = new( ELeave ) CMmGridContainer();
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL( aRect, aObjectProvider, aLibrary );
       
    48     return self;
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CMmGridContainer::NewL()
       
    53 // Two-phased constructor.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CMmGridContainer* CMmGridContainer::NewL( const TRect& aRect, 
       
    57         MObjectProvider* aObjectProvider, CMmTemplateLibrary* aLibrary )
       
    58     {        
       
    59     CMmGridContainer* self = NewLC( aRect, aObjectProvider, aLibrary);
       
    60     CleanupStack::Pop( self );
       
    61     return self;
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CMmGridContainer::CMmGridContainer()
       
    66 // C++ default constructor
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CMmGridContainer::CMmGridContainer()
       
    70     {
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // 
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 CAknGrid* CMmGridContainer::Grid()
       
    78     { 
       
    79     return iGrid;
       
    80     }
       
    81 // -----------------------------------------------------------------------------
       
    82 // CMmGridContainer::~CMmGridContainer()
       
    83 // Destructor
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CMmGridContainer::~CMmGridContainer()
       
    87     {  
       
    88     delete iGrid;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 // -----------------------------------------------------------------------------
       
    94 //    
       
    95 THnSuiteWidgetType CMmGridContainer::WidgetType()
       
    96 	{
       
    97 	return EGridWidget;
       
    98 	}
       
    99 // -----------------------------------------------------------------------------
       
   100 // CMmGridContainer::ConstructL()
       
   101 // 2nd phase constructor
       
   102 // -----------------------------------------------------------------------------
       
   103 //    
       
   104 void CMmGridContainer::ConstructL( const TRect& aRect, MObjectProvider* aObjectProvider,
       
   105         CMmTemplateLibrary* aTemplateLibrary )
       
   106     {
       
   107     CMmWidgetContainer::ConstructL();
       
   108     
       
   109     SetMopParent( aObjectProvider );
       
   110     CreateWindowL(); // Creates window.     
       
   111     iWidget = CreateGridL(aTemplateLibrary);
       
   112     iWidget->SetListBoxObserver( this );
       
   113     SetRect( aRect ); // Sets rectangle of frame.
       
   114     ActivateL(); // Activates window. ( Ready to draw )     
       
   115     SetupDrawer();
       
   116     SetHighlightVisibilityL( !AknLayoutUtils::PenEnabled() );	
       
   117     iPostProcessor = CMmPostEvaluationProcessor::NewL( *iDrawer );
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CMmGridContainer::CreateGridL()
       
   122 // Constructs listbox from resource, creates scroll bar and sets empty
       
   123 // list background text. 
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 CMmGrid* CMmGridContainer::CreateGridL( CMmTemplateLibrary* aTemplateLibrary )
       
   127     {
       
   128     iGrid = CMmGrid::NewL( this,
       
   129     		EAknListBoxSelectionList | EAknListBoxLoopScrolling | EAknListBoxDisableHighlight , aTemplateLibrary );
       
   130     iGrid->SetContainerWindowL( *this );
       
   131     iGrid->CreateScrollBarFrameL( ETrue );
       
   132     iGrid->SetPrimaryScrollingType(
       
   133     			CAknGridView::EScrollFollowsItemsAndLoops );
       
   134     iGrid->SetSecondaryScrollingType(
       
   135     			CAknGridView::EScrollFollowsGrid );
       
   136     iGrid->ScrollBarFrame()->SetScrollBarVisibilityL( 
       
   137     		CEikScrollBarFrame::EAuto, CEikScrollBarFrame::EAuto );
       
   138     iGrid->ScrollBarFrame()->DrawBackground( EFalse, EFalse );
       
   139     return iGrid;
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 // -----------------------------------------------------------------------------
       
   145 //     
       
   146 void CMmGridContainer::SizeChanged()
       
   147     {
       
   148     CMmWidgetContainer::SizeChanged();
       
   149     if ( iGrid )
       
   150     	{
       
   151 //    	iGrid->SetRect( Rect() );
       
   152         iGrid->SetupLayout();
       
   153         }
       
   154     }
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CMmGridContainer::SetEditModeL( TBool aIsEditMode )
       
   160     {
       
   161 	if (aIsEditMode && !AknLayoutUtils::PenEnabled() )
       
   162 		{
       
   163 		iGrid->SetPrimaryScrollingType(
       
   164 				CAknGridView::EScrollStops );
       
   165 		iGrid->SetSecondaryScrollingType(
       
   166 				CAknGridView::EScrollStops );
       
   167         }
       
   168     else
       
   169     	{
       
   170     	iGrid->SetPrimaryScrollingType(
       
   171     	    			CAknGridView::EScrollFollowsItemsAndLoops );
       
   172     	iGrid->SetSecondaryScrollingType(
       
   173     	    			CAknGridView::EScrollFollowsGrid );
       
   174     	    
       
   175     	}
       
   176 	
       
   177 	// In EditMode we allow Avkon to redraw scrollbar background
       
   178 	// to prevent scrollbar flicking.
       
   179 	// When edit mode is disabled, this redrawing causes performance problems 
       
   180 	// (and disabling it does not produce erroneous side-effects).
       
   181 	if( aIsEditMode )
       
   182 		{
       
   183 	    iGrid->ScrollBarFrame()->DrawBackground( EFalse, ETrue );
       
   184 		}
       
   185 	else
       
   186 		{
       
   187 	    iGrid->ScrollBarFrame()->DrawBackground( EFalse, EFalse );
       
   188 		}
       
   189 	
       
   190     CMmWidgetContainer::SetEditModeL( aIsEditMode );
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 EXPORT_C void CMmGridContainer::SetEmptyTextL(const TDesC& aText)
       
   198     {
       
   199     iGrid->SetEmptyGridTextL( aText );  
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C void CMmGridContainer::SetDefaultHighlightL(  TBool aRedraw )
       
   207     {  
       
   208     CAknGridView* view = (CAknGridView*) iGrid->View();
       
   209     
       
   210     if ( view )
       
   211         {
       
   212         TInt defaultHighlight( 0 );
       
   213         TSize layout = TSize( view->NumberOfColsInView(),
       
   214         		view->NumberOfRowsInView() );
       
   215         
       
   216         if ( Layout_Meta_Data::IsLandscapeOrientation() || FlipOpen() )
       
   217             {
       
   218             defaultHighlight = LandscapeOrientationDefaultHighlight( layout );
       
   219             }
       
   220         else
       
   221             {
       
   222             defaultHighlight = PortraitOrientationDefaultHighlight( layout );
       
   223             }
       
   224         
       
   225         SetManualHighlightL( defaultHighlight + DefaultHighlightOffset(), aRedraw );
       
   226         }
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 //
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 TInt CMmGridContainer::LandscapeOrientationDefaultHighlight( TSize aLayout )
       
   235 	{
       
   236 	TInt defaultHighlight( 0 );
       
   237 	
       
   238     if ( aLayout == TSize(MmGrid::K4By3LayoutX, MmGrid::K4By3LayoutY) 
       
   239     		&& iGrid->Model()->NumberOfItems() >= MmGrid::K4By3Threshold )
       
   240         {
       
   241         defaultHighlight = MmGrid::K4By3DefaultHighlight;
       
   242         }
       
   243     else if ( aLayout == TSize(MmGrid::K5By4LayoutX, MmGrid::K5By4LayoutY) 
       
   244     		&& iGrid->Model()->NumberOfItems() >= MmGrid::K5By4Threshold )
       
   245         {
       
   246         defaultHighlight = MmGrid::K5By4DefaultHighlight;
       
   247         }
       
   248     // When flip is opened, the phone layout is not changed immediately,
       
   249     // so the phone might still be in portrait mode for a couple of secs.
       
   250     // However, we have to set the correct default item immediately, and
       
   251     // the correct item is the one for landscape mode, even if the phone
       
   252     // is still in portrait mode. On variant switch 3x4 layout will be
       
   253     // replaced by 4x3 layout, while 4x5 layout will change into 5x4.
       
   254     else if ( aLayout == TSize( MmGrid::K3By4LayoutX, MmGrid::K3By4LayoutY )
       
   255             && iGrid->Model()->NumberOfItems() >= MmGrid::K4By3Threshold )
       
   256         {
       
   257         defaultHighlight = MmGrid::K4By3DefaultHighlight;
       
   258         }
       
   259     else if ( aLayout == TSize( MmGrid::K4By5LayoutX, MmGrid::K4By5LayoutY )
       
   260             && iGrid->Model()->NumberOfItems() >= MmGrid::K5By4Threshold )
       
   261         {
       
   262         defaultHighlight = MmGrid::K5By4DefaultHighlight;
       
   263         }
       
   264     
       
   265     return defaultHighlight;
       
   266 	}
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 //
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 TInt CMmGridContainer::PortraitOrientationDefaultHighlight( TSize aLayout )
       
   274 	{
       
   275 	TInt defaultHighlight( 0 );
       
   276 	
       
   277 	if ( aLayout == TSize(MmGrid::K4By5LayoutX, MmGrid::K4By5LayoutY ) 
       
   278 			&& iGrid->Model()->NumberOfItems() >= MmGrid::K4By5Threshold )
       
   279 		{
       
   280 		defaultHighlight = MmGrid::K4By5DefaultHighlight;
       
   281 		}
       
   282 	else if ( aLayout == TSize(MmGrid::K3By4LayoutX, MmGrid::K3By4LayoutY) 
       
   283 			&& iGrid->Model()->NumberOfItems() >= MmGrid::K3By4Threshold )
       
   284 		{
       
   285 		defaultHighlight = MmGrid::K3By4DefaultHighlight;
       
   286 		}
       
   287 	
       
   288     return defaultHighlight;
       
   289 	}
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 //
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 TInt CMmGridContainer::DefaultHighlightOffset()
       
   297 	{
       
   298 	TInt topVisibleItemIndex = iGrid->View()->TopItemIndex();
       
   299 	TInt hiddenPixels = -iGrid->View()->ItemOffsetInPixels();
       
   300 	
       
   301 	if ( iGrid->View()->ItemIsPartiallyVisible( topVisibleItemIndex ) 
       
   302 		&& hiddenPixels > iGrid->View()->ItemSize().iHeight / 2 )
       
   303 		{
       
   304 		topVisibleItemIndex += ColumnsInCurrentView();
       
   305 		}
       
   306 		
       
   307 	return topVisibleItemIndex;
       
   308 	}
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 //
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 CMmListBoxModel* CMmGridContainer::GetMmModel()
       
   316     {
       
   317     return iGrid->MmModel()->MmListBoxModel();
       
   318     }
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 //
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 void CMmGridContainer::SetItemDrawerAndViewBgContext(
       
   326 		CAknsBasicBackgroundControlContext * aBgContext )
       
   327 	{
       
   328 	iGrid->SetItemDrawerAndViewBgContext (aBgContext);
       
   329 	}
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 //
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CMmGridContainer::SetupWidgetLayoutL()
       
   337 	{
       
   338 	User::LeaveIfNull( iGrid );
       
   339 	iGrid->SetupLayout();
       
   340 	}
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 void CMmGridContainer::SetSuiteModelL( CHnSuiteModel* aModel )
       
   347     {
       
   348     CMmWidgetContainer::SetSuiteModelL( aModel );
       
   349     if ( aModel )
       
   350         {
       
   351         aModel->SetActiveL( !AknLayoutUtils::PenEnabled() );
       
   352         }
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 void CMmGridContainer::SetHighlightVisibilityL( TBool aVisible )
       
   360     {    
       
   361     CMmWidgetContainer::SetHighlightVisibilityL( aVisible );
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // 
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 void CMmGridContainer::HandleItemAdditionL()
       
   369 	{
       
   370 	iGrid->HandleItemAdditionL();
       
   371 	}
       
   372 
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // 
       
   376 // ---------------------------------------------------------------------------
       
   377 //
       
   378 void CMmGridContainer::HandleItemRemovalL()
       
   379 	{
       
   380 	
       
   381 	GetMmModel()->HandleSuiteEventL( ESuiteItemsRemoved, GetMmModel()->GetSuiteModel() );
       
   382 	ValidateWidgetCurrentItemIndex();
       
   383 	iDrawer->RemoveFloatingItems();
       
   384 	if( IsEditMode() )
       
   385 		{
       
   386 		iDrawer->GetAnimator()->SetNextRedrawToWholeScreen();
       
   387 		}
       
   388 	
       
   389 	CacheWidgetPosition();
       
   390 	iGrid->HandleItemRemovalL();
       
   391 	RestoreWidgetPosition();
       
   392 	CacheWidgetPosition();
       
   393 	
       
   394 	if ( iCurrentHighlight != iGrid->CurrentItemIndex() )
       
   395 		{
       
   396 		iCurrentHighlight = iGrid->CurrentItemIndex();
       
   397 		if( !iCurrentHighlight && !iGrid->Model()->NumberOfItems() )
       
   398 			{
       
   399 			iCurrentHighlight = KErrNotFound ;
       
   400 			}
       
   401 		CHnSuiteModel* suiteModel = GetSuiteModelL();
       
   402 		if ( suiteModel )
       
   403 		    {
       
   404 		    suiteModel->SetSuiteHighlightL( iCurrentHighlight );
       
   405 		    }
       
   406 		}
       
   407 	static_cast<CMmListBoxItemDrawer*>( iGrid->ItemDrawer() )->TrimCacheSize(
       
   408             GetMmModel()->NumberOfItems() );
       
   409 	}
       
   410 
       
   411 // ---------------------------------------------------------------------------
       
   412 // 
       
   413 // ---------------------------------------------------------------------------
       
   414 //
       
   415 void CMmGridContainer::FlipStateChangedL()
       
   416 	{
       
   417 //	if ( !IsEditMode() )
       
   418 //		{
       
   419 //		if ( FlipOpen() )
       
   420 //			{
       
   421 //			if ( iTimer )
       
   422 //				{
       
   423 //				delete iTimer;
       
   424 //				iTimer = NULL;
       
   425 //				}
       
   426 //			SetHighlightVisibilityL( ETrue );
       
   427 //			if ( iCurrentHighlight == KErrNotFound )
       
   428 //			    {
       
   429 //			    SetDefaultHighlightL( ETrue );
       
   430 //			    }
       
   431 //			}
       
   432 //		else
       
   433 //			{
       
   434 //			if ( AknLayoutUtils::PenEnabled() && !iTimer )
       
   435 //				{
       
   436 //				iTimer = CMmHighlightTimer::NewL( this );
       
   437 //				}
       
   438 //			SetHighlightVisibilityL( EFalse );
       
   439 //			DrawNow();
       
   440 //			}
       
   441 //		}
       
   442 	}
       
   443 
       
   444 // ---------------------------------------------------------------------------
       
   445 // 
       
   446 // ---------------------------------------------------------------------------
       
   447 //
       
   448 TInt CMmGridContainer::ColumnsInCurrentView()
       
   449 	{
       
   450 	CAknGridView* view ( (CAknGridView*) iGrid->View() );
       
   451 	return view->NumberOfColsInView();
       
   452 	}
       
   453 // ---------------------------------------------------------------------------
       
   454 // 
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 TInt CMmGridContainer::RowsInCurrentView()
       
   458     {
       
   459     CAknGridView* view ( (CAknGridView*) iGrid->View() );
       
   460     return view->NumberOfRowsInView();
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // 
       
   465 // ---------------------------------------------------------------------------
       
   466 //
       
   467 void CMmGridContainer::DrawView()
       
   468     {
       
   469     iGrid->DrawView();
       
   470     }
       
   471 
       
   472 // -----------------------------------------------------------------------------
       
   473 //
       
   474 // -----------------------------------------------------------------------------
       
   475 //
       
   476 void CMmGridContainer::SetVerticalItemOffset( TInt aOffset )
       
   477 	{
       
   478 	iGrid->SetVerticalItemOffset( aOffset );
       
   479 	}
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 //
       
   483 // -----------------------------------------------------------------------------
       
   484 //
       
   485 TInt CMmGridContainer::VerticalItemOffset() const
       
   486 	{
       
   487 	return iGrid->VerticalItemOffset();
       
   488 	}
       
   489 
       
   490 //----------------------------------------------------------------------------
       
   491 //
       
   492 // ---------------------------------------------------------------------------
       
   493 //
       
   494 void CMmGridContainer::UpdateViewScrollBarThumbs()
       
   495 	{
       
   496 	iGrid->UpdateScrollBarThumbs();
       
   497 	}
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 //
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 TBool CMmGridContainer::ItemIsVisible( TInt aItemIndex ) const
       
   504     {
       
   505     CListBoxView* v = iGrid->View(); 
       
   506     TRect itemRect( v->ItemPos( aItemIndex ), v->ItemSize( aItemIndex ) );
       
   507     TRect viewRect = v->ViewRect();
       
   508     TBool isVisible = EFalse;
       
   509     if ( itemRect.Intersects( viewRect ) )
       
   510         {
       
   511         TRect intersection = itemRect;
       
   512         intersection.Intersection( viewRect );
       
   513         isVisible = intersection.Height() > 1 && intersection.Width() > 1;
       
   514         }
       
   515     return isVisible;
       
   516     }
       
   517 
       
   518 // End of File
       
   519