idlehomescreen/examples/carouselwidgetexample/renderer/src/carouselwidget.cpp
branchRCL_3
changeset 102 ba63c83f4716
parent 93 b01126ce0bec
child 103 966d119a7e67
equal deleted inserted replaced
93:b01126ce0bec 102:ba63c83f4716
     1 /*
       
     2  * Copyright (c) 2010 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:  Carousel widget for Symbian Homescreen
       
    15  *
       
    16  */
       
    17 
       
    18 #include <ecom/implementationproxy.h>
       
    19 #include <aknphysics.h>
       
    20 #include <gulicon.h>
       
    21 #include <AknIconUtils.h>
       
    22 #include <AknsDrawUtils.h>
       
    23 #include <xnexteventhandler.h>
       
    24 #include "carouselwidget.h"
       
    25 
       
    26 const TSize KIconSize(70,70);
       
    27 const TInt KItemPadding( 6 );
       
    28 
       
    29 _LIT( KLaunchByIndex, "LaunchByIndex(");
       
    30 
       
    31 template< class PtrT > inline PtrT* UnpackPtr( 
       
    32     const TDesC8& aBuf )
       
    33     {
       
    34     TAny* result( NULL );
       
    35     
       
    36     if ( aBuf.Size() == sizeof( TAny* ) )
       
    37         {
       
    38         // Effectively writes aBuf contents to result
       
    39         TPckg< TAny* >( result ).Copy( aBuf ); 
       
    40         }
       
    41         
       
    42     return static_cast< PtrT* >( result );
       
    43     }
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 CCarouselItem::CCarouselItem( CFbsBitmap* aBitmap, CFbsBitmap* aMask)
       
    46     :iBitmap( aBitmap ), iMask( aMask )
       
    47     {
       
    48     }
       
    49 
       
    50 CCarouselItem::~CCarouselItem()
       
    51     {
       
    52     delete iBitmap;
       
    53     delete iMask;
       
    54     delete iText;
       
    55     }
       
    56 
       
    57 void CCarouselItem::SetTextL( const TDesC& aText )
       
    58     {
       
    59     delete iText;
       
    60     iText = NULL;
       
    61     iText = aText.AllocL();    
       
    62     }
       
    63 
       
    64 // ============================ MEMBER FUNCTIONS ===============================
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // Symbian static 1st phase constructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CCarouselWidget* CCarouselWidget::NewL()
       
    71     {
       
    72     CCarouselWidget* self = new( ELeave ) CCarouselWidget();
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // C++ destructor
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CCarouselWidget::~CCarouselWidget()
       
    84     {
       
    85     delete iPhysics;
       
    86     iStripeItems.ResetAndDestroy();
       
    87     //delete iCover;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // Symbian 2nd phase constructor can leave.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CCarouselWidget::ConstructL()
       
    95     {
       
    96     
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // Handles key events.
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 TKeyResponse CCarouselWidget::OfferKeyEventL( const TKeyEvent& /*aKeyEvent*/,
       
   104     TEventCode /*aType*/ )
       
   105     {
       
   106     return EKeyWasConsumed;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // Set window for this control
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CCarouselWidget::SetContainerWindowL( const CCoeControl &aContainer )
       
   114     {
       
   115     CCoeControl::SetContainerWindowL( aContainer );
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // (other items were commented in a header).
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CCarouselWidget::HandlePointerEventL( const TPointerEvent& aPointerEvent )
       
   123     {
       
   124     if( !iPhysics )
       
   125         {
       
   126         return;
       
   127         }
       
   128     TPoint stylusPos( aPointerEvent.iPosition );   
       
   129     switch( aPointerEvent.iType )
       
   130         {
       
   131         case TPointerEvent::EButton1Down:
       
   132             {
       
   133             iPhysics->StopPhysics();
       
   134             iPhysics->ResetFriction();
       
   135             iStartPosition = stylusPos;
       
   136             iStartTime.HomeTime();
       
   137             iStylusPosition = stylusPos;
       
   138             iDrawHighlight = ETrue;
       
   139             }
       
   140             break; 
       
   141             
       
   142         case TPointerEvent::EButton1Up:
       
   143             {
       
   144             iDrawHighlight = EFalse;
       
   145             TInt distance = iStartPosition.iX - stylusPos.iX;
       
   146             if( Abs(distance) <= iPhysics->DragThreshold() )
       
   147                 {
       
   148                 LaunchItemL( stylusPos );
       
   149                 }
       
   150             else
       
   151                 {
       
   152                 TPoint drag( distance, 0 );
       
   153                 iPhysics->StartPhysics( drag, iStartTime );
       
   154                 }            }
       
   155             break; 
       
   156         case TPointerEvent::EDrag:
       
   157         case TPointerEvent::EMove:
       
   158             {
       
   159             TPoint dragTh(  iStartPosition - stylusPos );
       
   160             if( Abs(dragTh.iX) > iPhysics->DragThreshold() || 
       
   161                 Abs(dragTh.iY) > iPhysics->DragThreshold())
       
   162                 {
       
   163                 iDrawHighlight = EFalse;
       
   164                 }
       
   165             TInt deltaX( iStylusPosition.iX - stylusPos.iX );
       
   166             iStylusPosition = stylusPos;
       
   167             TPoint deltaPoint( deltaX, 0 );
       
   168             iPhysics->RegisterPanningPosition( deltaPoint );
       
   169             }
       
   170             break;
       
   171         default:                
       
   172             break;                
       
   173         }
       
   174 
       
   175     CCoeControl::HandlePointerEventL( aPointerEvent );
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // Returns the number of component controls
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 TInt CCarouselWidget::CountComponentControls() const
       
   183     {
       
   184     return 0;
       
   185     }    
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // Returns the specified control
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 CCoeControl* CCarouselWidget::ComponentControl( TInt /*aIndex*/ ) const
       
   192     {
       
   193       return NULL;
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // Skin change notification.
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CCarouselWidget::SkinChanged()
       
   201     {
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CCarouselWidget::EnterPowerSaveModeL
       
   206 //
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CCarouselWidget::EnterPowerSaveModeL()
       
   210     {
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CCarouselWidget::ExitPowerSaveModeL
       
   215 //
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CCarouselWidget::ExitPowerSaveModeL()
       
   219     {
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // Called if focus changes
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 void CCarouselWidget::FocusChanged( TDrawNow /*aDrawNow*/ )
       
   227     {
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // Called if position or size changes
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CCarouselWidget::SizeChanged()
       
   235     {
       
   236     iViewPort = Rect();
       
   237     TRAP_IGNORE( InitPhysicEngineL() );
       
   238     DrawNow();
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // Sets the external event handler interface.
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CCarouselWidget::SetEventHandler( MXnExtEventHandler* aEventHandler )
       
   246     {
       
   247     iEventHandler = aEventHandler;
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // Data stream from the publisher
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CCarouselWidget::SetDataL( const TDesC8& aData, const TDesC& aType, TInt aIndex )
       
   255     {
       
   256     /// Unpack the data stream, works because publisher is in same process.
       
   257     CGulIcon* icon( UnpackPtr<CGulIcon>( aData ) );
       
   258     CleanupStack::PushL( icon );
       
   259     AknIconUtils::SetSize( icon->Bitmap(), KIconSize);
       
   260     
       
   261     CCarouselItem* item = new ( ELeave ) CCarouselItem(/* *iEditor,*/ icon->Bitmap(), icon->Mask() );
       
   262     CleanupStack::PopAndDestroy( icon );
       
   263     
       
   264     if( aType == _L("Appstripe/widget"))
       
   265         {
       
   266         if( iStripeItems.Count() <= aIndex )
       
   267             {
       
   268             iStripeItems.AppendL( item );
       
   269             InitPhysicEngineL();
       
   270             }
       
   271         else
       
   272             {
       
   273             CCarouselItem* citem = iStripeItems[ aIndex ];
       
   274             delete citem;
       
   275             iStripeItems.Remove( aIndex );
       
   276             iStripeItems.Insert( item, aIndex );
       
   277             }
       
   278         DrawNow();
       
   279         }
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // Draws the carousel component
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void CCarouselWidget::Draw( const TRect& /*aRect*/ ) const
       
   287     {
       
   288     CWindowGc& gc( SystemGc() );
       
   289     gc.SetClippingRect( iViewPort );    
       
   290     TRect bitmapRect( TPoint(0,0), KIconSize );
       
   291     TPoint point( iViewPort.iTl );
       
   292     point.iX -= iViewPosition.iX;
       
   293     
       
   294     TInt count = iStripeItems.Count();
       
   295     for( TInt i = 0; i < count; i++ )
       
   296         {
       
   297         CFbsBitmap* bitmap = iStripeItems[i]->iBitmap;
       
   298         CFbsBitmap* mask = iStripeItems[i]->iMask;
       
   299 
       
   300         TRect drawRect( point, KIconSize );
       
   301         if( iDrawHighlight && drawRect.Contains( iStylusPosition ))
       
   302             {
       
   303             TRect innerRect( drawRect );
       
   304             innerRect.Shrink( 5, 5 );
       
   305             
       
   306             MAknsSkinInstance* skin( AknsUtils::SkinInstance() );
       
   307             AknsDrawUtils::DrawFrame( skin, gc, drawRect, innerRect,
       
   308                 KAknsIIDQsnFrHomePressed, KAknsIIDDefault );
       
   309             }
       
   310         if( iViewPort.Contains( drawRect.iBr )||
       
   311             iViewPort.Contains( drawRect.iTl ))
       
   312             {
       
   313             gc.BitBltMasked( point, bitmap, bitmapRect, mask, EFalse);
       
   314             }
       
   315         point.iX += KIconSize.iWidth + KItemPadding;
       
   316         }
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // ViewPositionChanged
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void CCarouselWidget::ViewPositionChanged( 
       
   324     const TPoint& aNewPosition, TBool aDrawNow, TUint /*aFlags*/ )
       
   325     {
       
   326     iCurrentPosition = aNewPosition;
       
   327     iViewPosition.iX = aNewPosition.iX - iViewPort.Width()/2;
       
   328     if( aDrawNow )
       
   329         {
       
   330         DrawNow( iViewPort );
       
   331         }
       
   332     }
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // PhysicEmulationEnded
       
   336 // ---------------------------------------------------------------------------
       
   337 //    
       
   338 void CCarouselWidget::PhysicEmulationEnded()
       
   339     {
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // ViewPosition
       
   344 // ---------------------------------------------------------------------------
       
   345 //    
       
   346 TPoint CCarouselWidget::ViewPosition() const
       
   347     {
       
   348     return iCurrentPosition;
       
   349     }
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // InitPhysicEngineL
       
   353 // ---------------------------------------------------------------------------
       
   354 //    
       
   355 void CCarouselWidget::InitPhysicEngineL()
       
   356     {
       
   357     // Init physic engine
       
   358     if ( !iPhysics && CAknPhysics::FeatureEnabled() )
       
   359         {
       
   360         iPhysics = CAknPhysics::NewL( *this, this );
       
   361         }
       
   362     if( !iPhysics )
       
   363         {
       
   364         return;
       
   365         }
       
   366     TSize viewPortSize = iViewPort.Size();
       
   367     TInt totalWidth( iStripeItems.Count() * (KIconSize.iWidth + KItemPadding ));
       
   368     TSize totalSize( totalWidth, viewPortSize.iHeight );
       
   369     iPhysics->InitPhysicsL( totalSize, viewPortSize, ETrue );
       
   370     iCurrentPosition = TPoint( viewPortSize.iWidth / 2, viewPortSize.iHeight / 2 );
       
   371     }
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // 
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 TInt CCarouselWidget::ItemIndex( TPoint& aPoint )
       
   378     {
       
   379     TPoint itemStartPoint( iViewPort.iTl );
       
   380     itemStartPoint.iX -= iViewPosition.iX;
       
   381     
       
   382     TRect dropRect( itemStartPoint, KIconSize );
       
   383     dropRect.Resize( KItemPadding, 0 );
       
   384     TInt index( 0 );
       
   385     do
       
   386         {
       
   387         if( dropRect.Contains( aPoint ))
       
   388             {
       
   389             return index;
       
   390             }
       
   391         dropRect.Move( KIconSize.iWidth+KItemPadding, 0);
       
   392         index++;
       
   393         }
       
   394     while( index < iStripeItems.Count() );
       
   395     return KErrNotFound;
       
   396     }
       
   397 
       
   398 // -----------------------------------------------------------------------------
       
   399 // Launches the item, which is in aPosition
       
   400 // -----------------------------------------------------------------------------
       
   401 //
       
   402 void CCarouselWidget::LaunchItemL( TPoint& aPosition )
       
   403     {
       
   404     TInt index = ItemIndex( aPosition );
       
   405     // carousel plugin expects indexes to start from 1
       
   406     index++;
       
   407     TInt len( KLaunchByIndex().Length()+3 );
       
   408     TBuf<17> event;
       
   409     event.Append( KLaunchByIndex );
       
   410     event.AppendNum( index );
       
   411     event.Append( _L(")"));
       
   412     iEventHandler->HandleEventL( event, _L8("Appstripe"));
       
   413     }
       
   414 
       
   415 // -----------------------------------------------------------------------------
       
   416 // C++ default constructor
       
   417 // -----------------------------------------------------------------------------
       
   418 //
       
   419 CCarouselWidget::CCarouselWidget()
       
   420     {
       
   421     }
       
   422 
       
   423 // ============================ PUBLIC FUNCTIONS ===============================
       
   424 const TImplementationProxy KImplementationTable[] =
       
   425     {
       
   426 #ifdef __EABI__
       
   427     IMPLEMENTATION_PROXY_ENTRY( 0xEabba433, CCarouselWidget::NewL )
       
   428 #else
       
   429     { { 0xEabba433 }, CCarouselWidget::NewL }
       
   430 #endif
       
   431     };
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // Returns the list of implementations provided by the plugin
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(
       
   438     TInt& aTableCount )
       
   439     {
       
   440     aTableCount = sizeof( KImplementationTable ) / sizeof( TImplementationProxy );
       
   441     return KImplementationTable;
       
   442     }
       
   443