idlehomescreen/xmluirendering/uiengine/src/xnscrollablecontroladapter.cpp
branchRCL_3
changeset 83 5456b4e8b3a8
child 88 3321d3e205b6
equal deleted inserted replaced
82:5f0182e07bfb 83:5456b4e8b3a8
       
     1 /*
       
     2 * Copyright (c) 2008 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 */
       
    15 
       
    16 //  System includes
       
    17 #include <e32base.h>
       
    18 #include <e32const.h>
       
    19 #include <coecntrl.h>
       
    20 #include <coemain.h>
       
    21 #include <aknphysics.h>
       
    22 
       
    23 // User includes
       
    24 #include "xncontroladapter.h"
       
    25 #include "xnuiengine.h"
       
    26 #include "xnnode.h"
       
    27 #include "xnnodepluginif.h"
       
    28 #include "xnproperty.h"
       
    29 
       
    30 #include "xnscrollablecontroladapter.h"
       
    31 
       
    32 // Constants
       
    33 const TInt KOffset( 25 );
       
    34 
       
    35 _LIT8(KViewportHeight, "viewport-height");
       
    36 _LIT8(KViewportWidth, "viewport-width");
       
    37 _LIT8(KViewportTop, "viewport-top");
       
    38 _LIT8(KViewportLeft, "viewport-left");
       
    39 _LIT8(KScrollDirection, "scroll-direction");                         
       
    40 _LIT8(KHorizontal, "horizontal");
       
    41 
       
    42 const TInt EVertical( 0 );
       
    43 const TInt EHorizontal( 1 );
       
    44 
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CXnScrollableControlAdapter::NewL
       
    49 // Two-phased constructor. Can leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CXnScrollableControlAdapter* CXnScrollableControlAdapter::NewL( 
       
    53     CXnNodePluginIf& aNode )
       
    54     {
       
    55     CXnScrollableControlAdapter* self = 
       
    56         new ( ELeave ) CXnScrollableControlAdapter( aNode );
       
    57     
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop( self );
       
    61     return self;
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CXnScrollableControlAdapter::~CXnScrollableControlAdapter
       
    66 // Destructor.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CXnScrollableControlAdapter::~CXnScrollableControlAdapter()
       
    70     {
       
    71     delete iPhysics;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CXnScrollableControlAdapter::CXnScrollableControlAdapter
       
    76 // C++ default constructor. Must not leave.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CXnScrollableControlAdapter::CXnScrollableControlAdapter( CXnNodePluginIf& aNode )
       
    80     : iNode( aNode ), iStartPosition( 0, 0), iPreviousPosition( 0, 0 ), 
       
    81     iCurrentPosition( 0, 0 ), iLayoutChanged( EFalse )
       
    82     {
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CXnScrollableControlAdapter::ConstructL
       
    87 // 2nd phase constructor. Can leave.
       
    88 // -----------------------------------------------------------------------------
       
    89 //    
       
    90 void CXnScrollableControlAdapter::ConstructL()
       
    91     {
       
    92     CXnControlAdapter::ConstructL( iNode );
       
    93     SetControlContext( this );
       
    94     SetHitTest( this );
       
    95     iUiEngine = iNode.Node().UiEngine();
       
    96     
       
    97     iNode.Node().SetScrollableControl( this );
       
    98     
       
    99     // Default
       
   100     iDirection = EVertical;
       
   101     
       
   102     CXnProperty* prop( iNode.GetPropertyL( KScrollDirection ) );
       
   103     
       
   104     if ( prop && prop->StringValue() == KHorizontal )
       
   105         {
       
   106         iDirection = EHorizontal;
       
   107         }    
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CXnScrollableControlAdapter::HandlePointerEventL
       
   112 // 
       
   113 // -----------------------------------------------------------------------------
       
   114 //    
       
   115 void CXnScrollableControlAdapter::HandlePointerEventL( 
       
   116     const TPointerEvent& aPointerEvent )
       
   117     {
       
   118     if( !iPhysics )
       
   119         {
       
   120         return;
       
   121         }
       
   122     
       
   123     TPoint stylusPos( aPointerEvent.iPosition );
       
   124     
       
   125     switch( aPointerEvent.iType )
       
   126         {
       
   127         case TPointerEvent::EButton1Down:
       
   128             {
       
   129             iPhysics->StopPhysics();
       
   130             iPhysics->ResetFriction();
       
   131             iStartPosition = stylusPos;
       
   132             iStartTime.HomeTime();
       
   133             iPreviousPosition = iCurrentPosition;
       
   134             iStylusPosition = stylusPos;
       
   135             }
       
   136             break;            
       
   137         case TPointerEvent::EButton1Up:
       
   138             {                                                        
       
   139             TInt distanceY( iStartPosition.iY - stylusPos.iY );                                              
       
   140             TInt distanceX( iStartPosition.iX - stylusPos.iX );
       
   141             
       
   142             TPoint drag( distanceX, distanceY );
       
   143             iPhysics->StartPhysics( drag, iStartTime );                                
       
   144             }
       
   145             break; 
       
   146         case TPointerEvent::EDrag:        
       
   147             {
       
   148             TPoint distanceFromStart( iStartPosition - stylusPos );
       
   149 
       
   150             if( ( Abs( distanceFromStart.iY ) > KOffset && iDirection == EVertical ) || 
       
   151                 ( Abs( distanceFromStart.iX ) > KOffset && iDirection == EHorizontal ) )                 
       
   152                 {
       
   153                 CXnNode* focused( iUiEngine->FocusedNode() );
       
   154                 
       
   155                 if ( focused )
       
   156                     {
       
   157                     // Remove pressed down
       
   158                     focused->UnsetStateL( 
       
   159                         XnPropertyNames::style::common::KPressedDown );
       
   160                     focused->HideTooltipsL();                                
       
   161                     }
       
   162                 }
       
   163             
       
   164             TInt deltaY( iStylusPosition.iY - stylusPos.iY );
       
   165             TInt deltaX( iStylusPosition.iX - stylusPos.iX );
       
   166             
       
   167             iStylusPosition = stylusPos;
       
   168             
       
   169             TPoint deltaPoint( deltaX, deltaY );
       
   170             
       
   171             iPhysics->RegisterPanningPosition( deltaPoint );
       
   172             }
       
   173             break;
       
   174         default:                
       
   175             break;                
       
   176         }
       
   177  
       
   178     CXnControlAdapter::HandlePointerEventL( aPointerEvent );
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CXnScrollableControlAdapter::Draw
       
   183 // 
       
   184 // -----------------------------------------------------------------------------
       
   185 //    
       
   186 void CXnScrollableControlAdapter::Draw( const TRect& aRect ) const
       
   187     {
       
   188     CXnControlAdapter::Draw( aRect );           
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CXnScrollableControlAdapter::SizeChanged
       
   193 // 
       
   194 // -----------------------------------------------------------------------------
       
   195 void CXnScrollableControlAdapter::SizeChanged()
       
   196     {
       
   197     TRAP_IGNORE(         
       
   198         ReadPropertiesL();
       
   199         InitPhysicEngineL(); ); 
       
   200         
       
   201     CXnControlAdapter::SizeChanged();
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CXnScrollableControlAdapter::MakeVisible
       
   206 // 
       
   207 // -----------------------------------------------------------------------------
       
   208 // 
       
   209 void CXnScrollableControlAdapter::MakeVisible( TBool aVisible )
       
   210     {
       
   211     TBool visible( IsVisible() ? ETrue : EFalse );
       
   212     
       
   213     if ( visible == aVisible )
       
   214         {
       
   215         return;
       
   216         }
       
   217     
       
   218     if( aVisible )
       
   219         {
       
   220         TRAP_IGNORE( InitPhysicEngineL() );
       
   221         }
       
   222     
       
   223     CCoeControl::MakeVisible( aVisible );            
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CXnScrollableControlAdapter::HitRegionContains
       
   228 // 
       
   229 // -----------------------------------------------------------------------------
       
   230 // 
       
   231 TBool CXnScrollableControlAdapter::HitRegionContains( const TPoint& aPoint, 
       
   232     const CCoeControl& /*aControl*/) const
       
   233     {
       
   234     return iViewPort.Contains( aPoint );
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CXnScrollableControlAdapter::HandleScreenDeviceChangedL
       
   239 // 
       
   240 // -----------------------------------------------------------------------------
       
   241 void CXnScrollableControlAdapter::HandleScreenDeviceChangedL()
       
   242     {
       
   243     // The new layout has not been calculated yet. 
       
   244     // Therefore we need to read new propertues and initialise engine when LayoutChagedL() is called next time.
       
   245     iLayoutChanged = ETrue;
       
   246     CXnControlAdapter::HandleScreenDeviceChangedL();
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CXnScrollableControlAdapter::DeltaPosition
       
   251 // 
       
   252 // -----------------------------------------------------------------------------
       
   253 const TPoint CXnScrollableControlAdapter::DeltaPosition() const
       
   254     {
       
   255     return iStartViewPosition - iCurrentPosition;
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CXnScrollableControlAdapter::ResetState
       
   260 // 
       
   261 // -----------------------------------------------------------------------------
       
   262 void CXnScrollableControlAdapter::ResetState()
       
   263     {
       
   264     iStartViewPosition = TPoint( 0, 0 );
       
   265     iCurrentPosition = TPoint( 0, 0 );
       
   266     iPreviousPosition = TPoint( 0, 0 );
       
   267     }
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CXnScrollableControlAdapter::ShowItem
       
   271 // 
       
   272 // -----------------------------------------------------------------------------
       
   273 void CXnScrollableControlAdapter::ShowItem( CXnNode& aNode )
       
   274     {
       
   275     TRect rect( aNode.MarginRect() );
       
   276     
       
   277     if( !iViewPort.Contains( rect.iTl ) || !iViewPort.Contains( rect.iBr ) )
       
   278         {
       
   279         if ( iDirection == EVertical )
       
   280             {                            
       
   281             TInt delta( 0 );
       
   282 
       
   283             if( rect.iTl.iY < iViewPort.iTl.iY )
       
   284                 {
       
   285                 delta = rect.iTl.iY - iViewPort.iTl.iY;
       
   286                 }
       
   287             else if( rect.iBr.iY > iViewPort.iBr.iY )
       
   288                 {
       
   289                 delta = rect.iBr.iY - iViewPort.iBr.iY;
       
   290                 }
       
   291             
       
   292             TPoint newPosition( iPreviousPosition + TPoint( 0, delta ) );
       
   293             
       
   294             ViewPositionChanged( newPosition, ETrue, 0 );
       
   295             }
       
   296         else
       
   297             {
       
   298             TInt delta( 0 );
       
   299             
       
   300             if( rect.iTl.iX < iViewPort.iTl.iX )
       
   301                 {
       
   302                 delta = rect.iTl.iX - iViewPort.iTl.iX;
       
   303                 }
       
   304             else if( rect.iBr.iX > iViewPort.iBr.iX )
       
   305                 {
       
   306                 delta = rect.iBr.iX - iViewPort.iBr.iX;
       
   307                 }
       
   308             
       
   309             TPoint newPosition( iPreviousPosition + TPoint( delta, 0 ) );
       
   310             
       
   311             ViewPositionChanged( newPosition, ETrue, 0 );            
       
   312             }
       
   313         }
       
   314     }
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CXnScrollableControlAdapter::LayoutChangedL
       
   318 // 
       
   319 // -----------------------------------------------------------------------------
       
   320 void CXnScrollableControlAdapter::LayoutChangedL()
       
   321     {
       
   322     if( iLayoutChanged )
       
   323         {
       
   324         iLayoutChanged = EFalse;
       
   325         ReadPropertiesL();
       
   326         InitPhysicEngineL();
       
   327         }
       
   328     }
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 // CXnScrollableControlAdapter::ViewPositionChanged
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void CXnScrollableControlAdapter::ViewPositionChanged( 
       
   335     const TPoint& aNewPosition, TBool aDrawNow, TUint /*aFlags*/ )
       
   336     {
       
   337     TPoint diff;
       
   338     
       
   339     if ( iDirection == EVertical )
       
   340         {
       
   341         diff = TPoint( 0, aNewPosition.iY - iPreviousPosition.iY );
       
   342         }
       
   343     else
       
   344         {
       
   345         diff = TPoint( aNewPosition.iX - iPreviousPosition.iX, 0 );
       
   346         }
       
   347     
       
   348     iPreviousPosition = aNewPosition;
       
   349     iCurrentPosition += diff;
       
   350     TPoint tmpPos = iNode.Control()->Position();
       
   351     tmpPos -= diff;
       
   352     iNode.Control()->SetPosition( tmpPos );
       
   353     if( aDrawNow )
       
   354         {
       
   355         DrawNow( iViewPort );    
       
   356         }
       
   357     }
       
   358     
       
   359 // ---------------------------------------------------------------------------
       
   360 // CXnScrollableControlAdapter::PhysicEmulationEnded
       
   361 // ---------------------------------------------------------------------------
       
   362 //    
       
   363 void CXnScrollableControlAdapter::PhysicEmulationEnded()
       
   364     {
       
   365     }
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // CXnScrollableControlAdapter::ViewPosition
       
   369 // ---------------------------------------------------------------------------
       
   370 //    
       
   371 TPoint CXnScrollableControlAdapter::ViewPosition() const
       
   372     {
       
   373     return iCurrentPosition;
       
   374     }
       
   375 
       
   376 // ---------------------------------------------------------------------------
       
   377 // CXnScrollableControlAdapter::ResetContext
       
   378 // ---------------------------------------------------------------------------
       
   379 //    
       
   380 void CXnScrollableControlAdapter::ResetContext( CWindowGc& aGc ) const
       
   381     {
       
   382     aGc.Reset();
       
   383     aGc.SetClippingRect( iViewPort );
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 // CXnScrollableControlAdapter::InitPhysicEngineL
       
   388 // ---------------------------------------------------------------------------
       
   389 //    
       
   390 void CXnScrollableControlAdapter::InitPhysicEngineL()
       
   391     {
       
   392     // Init physic engine
       
   393     if ( !iPhysics && CAknPhysics::FeatureEnabled() )
       
   394         {
       
   395         iPhysics = CAknPhysics::NewL( *this, this );
       
   396         }
       
   397     if( !iPhysics )
       
   398         {
       
   399         return;
       
   400         }
       
   401     TSize viewPortSize = iViewPort.Size();
       
   402     TSize totalSize( iNode.Rect().Size() );
       
   403     
       
   404     if ( iDirection == EVertical )
       
   405         {
       
   406         iPhysics->InitPhysicsL( totalSize, viewPortSize, EFalse );
       
   407         }
       
   408     else
       
   409         {
       
   410         iPhysics->InitPhysicsL( totalSize, viewPortSize, ETrue );
       
   411         }
       
   412     
       
   413     
       
   414     iCurrentPosition = 
       
   415         TPoint( viewPortSize.iWidth / 2, viewPortSize.iHeight / 2 ); 
       
   416                   
       
   417     iPreviousPosition = iStartViewPosition =  iCurrentPosition;
       
   418     }
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // CXnScrollableControlAdapter::ReadPropertiesL
       
   422 // ---------------------------------------------------------------------------
       
   423 //    
       
   424 void CXnScrollableControlAdapter::ReadPropertiesL()
       
   425     {
       
   426     TInt width( 0 );
       
   427     TInt height( 0 );
       
   428     TInt top( 0 );
       
   429     TInt left( 0 );
       
   430 
       
   431     CXnNode& node( iNode.Node() );
       
   432     
       
   433     // Width
       
   434     CXnProperty* prop = iNode.GetPropertyL( KViewportWidth );
       
   435     if( prop )
       
   436         {
       
   437         width = iUiEngine->HorizontalPixelValueL(
       
   438             prop, iNode.Rect().Width() );
       
   439         }
       
   440     if( width == 0 )
       
   441         {
       
   442         width = node.Parent()->Rect().Width();
       
   443         }
       
   444 
       
   445     // Height
       
   446     prop = iNode.GetPropertyL( KViewportHeight );
       
   447     if( prop )
       
   448         {
       
   449         height = iUiEngine->VerticalPixelValueL(
       
   450             prop, iNode.Rect().Height() );
       
   451         }
       
   452     if( height == 0 )
       
   453         {
       
   454         height = node.Parent()->Rect().Height();
       
   455         }
       
   456 
       
   457     // Top
       
   458     prop = iNode.GetPropertyL( KViewportTop );
       
   459     if( prop )
       
   460         {
       
   461         top = iUiEngine->VerticalPixelValueL(
       
   462             prop, iNode.Rect().Height() );
       
   463         }
       
   464 
       
   465     // Left
       
   466     prop = iNode.GetPropertyL( KViewportLeft );
       
   467     if( prop )
       
   468         {
       
   469         left = iUiEngine->HorizontalPixelValueL(
       
   470             prop, iNode.Rect().Width() );
       
   471         }
       
   472 
       
   473     iViewPort = TRect( iNode.Rect().iTl, TSize( width, height ) );
       
   474     iViewPort.Move( left, top );
       
   475     }
       
   476 
       
   477 //  End of File