uifw/AvKon/aknphysics/src/aknphysicsconeobserver.cpp
changeset 0 2f259fa3e83a
child 8 71dd06cfe933
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     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:  AknPhysics control environment observer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <aknphysics.h>
       
    20 #include <aknappui.h>
       
    21 #include <aknpointereventmodifier.h>
       
    22 
       
    23 #include "aknphysicsconeobserver.h"
       
    24 #include "aknphysicskeyconsumer.h"
       
    25 #include "aknphysicsrestrictor.h"
       
    26 #include "aknphysicsdragobserver.h"
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CAknPhysicsConeObserver::NewL
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CAknPhysicsConeObserver* CAknPhysicsConeObserver::NewL( 
       
    35     CAknPhysics* aPhysics, CAknPhysicsRestrictor* aRestrictor )
       
    36     {
       
    37     CAknPhysicsConeObserver* self = 
       
    38         CAknPhysicsConeObserver::NewLC( aPhysics, aRestrictor );
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CAknPhysicsConeObserver::NewLC
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CAknPhysicsConeObserver* CAknPhysicsConeObserver::NewLC( 
       
    49     CAknPhysics* aPhysics, CAknPhysicsRestrictor* aRestrictor )
       
    50     {
       
    51     CAknPhysicsConeObserver* self 
       
    52         = new ( ELeave ) CAknPhysicsConeObserver( aPhysics, aRestrictor );
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     return self;
       
    56     }
       
    57 
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CAknPhysicsConeObserver::~CAknPhysicsConeObserver
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CAknPhysicsConeObserver::~CAknPhysicsConeObserver()
       
    64     {
       
    65     if ( iCoeEnv )
       
    66         {
       
    67         iCoeEnv->RemoveForegroundObserver( *this );
       
    68         iCoeEnv->RemoveMessageMonitorObserver( *this );
       
    69 
       
    70         if ( iCoeEnv->AppUi() )
       
    71             {
       
    72             iCoeEnv->AppUi()->RemoveFromStack( iKeyConsumer );
       
    73             }
       
    74         }
       
    75     delete iDragObserver;
       
    76     delete iKeyConsumer;
       
    77     }
       
    78 
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CAknPhysicsConeObserver::EventHandlingAllowed
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 TBool CAknPhysicsConeObserver::EventHandlingAllowed() const
       
    85     {
       
    86     if ( !iEventsAllowed 
       
    87         || OngoingAction() == CAknPhysics::EAknPhysicsActionBouncing )
       
    88         {
       
    89         return EFalse;
       
    90         }
       
    91     return ETrue;
       
    92     }
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CAknPhysicsConeObserver::SetViewWindowControl
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void CAknPhysicsConeObserver::SetViewWindowControl( CCoeControl* aControl )
       
   100     {
       
   101     if ( aControl )
       
   102         {
       
   103         CCoeControl* windowControl( aControl );
       
   104         while ( windowControl && !windowControl->OwnsWindow() )
       
   105             {
       
   106             windowControl = windowControl->Parent();
       
   107             }
       
   108         if ( windowControl )
       
   109             {
       
   110             iWindowControl = windowControl;            
       
   111             }
       
   112         else
       
   113             {
       
   114             iWindowControl = aControl;
       
   115             }
       
   116         iViewControl = aControl;
       
   117         }
       
   118     }
       
   119 
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // CAknPhysicsConeObserver::PhysicsStateChanged
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 void CAknPhysicsConeObserver::PhysicsStateChanged()
       
   126     {
       
   127     // Make sure that if we are foreground, we are monitoring ws events.
       
   128     if ( !iMonitoringWs && iCoeEnv )
       
   129         {
       
   130         CAknAppUi* appUi( AppUi() );
       
   131         if ( appUi && appUi->IsForeground() )
       
   132             {
       
   133             HandleGainingForeground();            
       
   134             }
       
   135         }
       
   136     }
       
   137 
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // CAknPhysicsConeObserver::DragChanged
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 TBool CAknPhysicsConeObserver::DragChanged( const TBool& aLandscape,
       
   144                                             TPoint& aDragDistance, 
       
   145                                             TInt& aMoveTime ) const
       
   146     {
       
   147     if ( iDragObserver && iDragObserver->DragChanged() )
       
   148         {
       
   149         TPoint changedDrag( iDragObserver->ChangedDragDistance() );
       
   150         if ( aLandscape && changedDrag.iX != aDragDistance.iX )
       
   151             {
       
   152             aDragDistance.iX = changedDrag.iX;
       
   153             aMoveTime = iDragObserver->ChangedHorizontalDragMoveTime();
       
   154             return ETrue;
       
   155             }
       
   156         else if ( !aLandscape && changedDrag.iY != aDragDistance.iY )
       
   157             {
       
   158             aDragDistance.iY = changedDrag.iY;
       
   159             aMoveTime = iDragObserver->ChangedVerticalDragMoveTime();
       
   160             return ETrue;
       
   161             }
       
   162         }
       
   163     return EFalse;
       
   164     }
       
   165 
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // CAknPhysicsConeObserver::UpdateViewWindowControl
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 void CAknPhysicsConeObserver::UpdateViewWindowControl( CCoeControl* aControl )
       
   172     {
       
   173     if ( !aControl )
       
   174         {
       
   175         aControl = iViewControl;
       
   176         }
       
   177     SetViewWindowControl( aControl );
       
   178     }
       
   179 
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // CAknPhysicsConeObserver::CAknPhysicsConeObserver
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 CAknPhysicsConeObserver::CAknPhysicsConeObserver( 
       
   186     CAknPhysics* aPhysics, CAknPhysicsRestrictor* aRestrictor )
       
   187     :
       
   188     iPhysics( aPhysics ),
       
   189     iKeyConsumer( NULL ),
       
   190     iCoeEnv( CCoeEnv::Static() ),
       
   191     iEventsAllowed( ETrue ),
       
   192     iMonitoringWs( EFalse ),
       
   193     iWindowControl( NULL ),
       
   194     iViewControl( NULL ),
       
   195     iRestrictor( aRestrictor ),
       
   196     iDragObserver( NULL )
       
   197     {
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CAknPhysicsConeObserver::ConstructL
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CAknPhysicsConeObserver::ConstructL()
       
   206     {
       
   207     iKeyConsumer = CAknPhysicsKeyConsumer::NewL( this );
       
   208     iDragObserver = CAknPhysicsDragObserver::NewL( 
       
   209         iRestrictor->ParameterProvider() );
       
   210     if ( iCoeEnv )
       
   211         {
       
   212         iCoeEnv->AppUi()->AddToStackL( iKeyConsumer, 
       
   213                                        ECoeStackPriorityEnvironmentFilter,
       
   214                                        ECoeStackFlagRefusesFocus );
       
   215         iCoeEnv->AddForegroundObserverL( *this );
       
   216         }
       
   217     }
       
   218 
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CAknPhysicsConeObserver::OngoingAction
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 TInt CAknPhysicsConeObserver::OngoingAction() const
       
   225     {
       
   226     if ( iPhysics )
       
   227         {
       
   228         return iPhysics->OngoingPhysicsAction();
       
   229         }
       
   230     return 0;
       
   231     }
       
   232 
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CAknPhysicsConeObserver::EventTargetedToView
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 TInt CAknPhysicsConeObserver::EventTargetedToView( 
       
   239     const TPointerEvent* aEvent, const CCoeControl* aTargetControl )
       
   240     {
       
   241     TInt target( ETargetUnresolvedControl );
       
   242     
       
   243     if ( aTargetControl && iWindowControl )
       
   244         {
       
   245         // target window different from view window
       
   246         if ( aTargetControl != iWindowControl )
       
   247             {
       
   248             // figure out if target window is iWindowControl's child window
       
   249             CCoeControl* parent = const_cast<CCoeControl*>( aTargetControl );
       
   250             
       
   251             while ( parent )
       
   252                 {
       
   253                 if ( parent == iWindowControl )
       
   254                     {
       
   255                     return ETargetChildControl;
       
   256                     }
       
   257                 
       
   258                 parent = parent->Parent();
       
   259                 }
       
   260             
       
   261             target = ETargetOtherControl;
       
   262             }
       
   263 
       
   264         // target window same as view window
       
   265         else if ( iViewControl )
       
   266             {
       
   267             TRect viewRect( iViewControl->Rect() );
       
   268             
       
   269             // pointerevent position not in control area - view control
       
   270             // not target control
       
   271             if ( viewRect != TRect() 
       
   272                 && !viewRect.Contains( aEvent->iPosition ) )
       
   273                 {
       
   274                 target = ETargetOtherControl;
       
   275                 }
       
   276             else
       
   277                 {
       
   278                 target = ETargetViewControl;
       
   279                 }
       
   280 
       
   281             if ( target != ETargetOtherControl )
       
   282                 {
       
   283                 // check if there is a child control to receive the event
       
   284                 for ( TInt i = 0; i < iViewControl->CountComponentControls();
       
   285                       i++ )
       
   286                     {
       
   287                     CCoeControl* child = iViewControl->ComponentControl( i );
       
   288                     if ( child && child->IsVisible() && !child->OwnsWindow() )
       
   289                         {
       
   290                         if ( child->Rect().Contains( aEvent->iPosition ) )
       
   291                             {
       
   292                             target = ETargetOtherControl;
       
   293                             }
       
   294                         }
       
   295                     }
       
   296                 }
       
   297             }
       
   298         }
       
   299     return target;
       
   300     }
       
   301 
       
   302 
       
   303 // ---------------------------------------------------------------------------
       
   304 // CAknPhysicsConeObserver::MonitorPointerEvent
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 TBool CAknPhysicsConeObserver::MonitorPointerEvent( 
       
   308     TPointerEvent* aEvent, CCoeControl* aTargetControl )
       
   309     {
       
   310     // Check where event is targeted
       
   311     TInt eventTarget( EventTargetedToView( aEvent, aTargetControl ) );
       
   312     
       
   313     // Register event to drag observer
       
   314     if ( iDragObserver 
       
   315         && ( eventTarget == ETargetViewControl 
       
   316             || aEvent->iType == TPointerEvent::EButton1Up ) )
       
   317         {
       
   318         iDragObserver->RegisterEventPosition( aEvent );
       
   319         }
       
   320 
       
   321     if ( aEvent->iType == TPointerEvent::EButton1Up && 
       
   322          iPhysics->IsPanningDrawOmitted() )
       
   323         {
       
   324         // Ensure that the last panning position gets drawn if the last
       
   325         // view redraw was omitted due to too high CPU use.
       
   326         iPhysics->DrawViewInCurrentPosition();
       
   327         }
       
   328     
       
   329     // Stop physics only with pointer down event
       
   330     if ( aEvent->iType != TPointerEvent::EButton1Down )
       
   331         {
       
   332         return EFalse;
       
   333         }
       
   334 
       
   335     iEventsAllowed = ETrue;
       
   336     TBool stopPhysics( EFalse );
       
   337     TInt ongoingAction = OngoingAction();
       
   338     
       
   339     if ( ongoingAction != CAknPhysics::EAknPhysicsActionNone
       
   340         && ongoingAction != CAknPhysics::EAknPhysicsActionDragging )
       
   341         {
       
   342         PointerEventModified( aEvent, aTargetControl );
       
   343         }
       
   344     
       
   345     // Check if flick should be stopped
       
   346     if ( ongoingAction == CAknPhysics::EAknPhysicsActionFlicking )
       
   347         {
       
   348         // Event not targeted to view - stop flick
       
   349         if ( ( eventTarget == ETargetOtherControl || 
       
   350                 eventTarget == ETargetChildControl ) && aTargetControl )
       
   351             {
       
   352             stopPhysics = ETrue;
       
   353             aTargetControl->IgnoreEventsUntilNextPointerUp();
       
   354             }
       
   355         }
       
   356 
       
   357     // No pointer events to view control while bounce is ongoing
       
   358     else if ( ongoingAction == CAknPhysics::EAknPhysicsActionBouncing
       
   359         && eventTarget == ETargetViewControl )
       
   360         {
       
   361         iWindowControl->IgnoreEventsUntilNextPointerUp();
       
   362         }
       
   363     else if ( ongoingAction == CAknPhysics::EAknPhysicsActionBouncing
       
   364         && eventTarget == ETargetChildControl )
       
   365         {
       
   366         aTargetControl->IgnoreEventsUntilNextPointerUp();
       
   367         }
       
   368 
       
   369     return stopPhysics;
       
   370     }
       
   371 
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CAknPhysicsConeObserver::MonitorKeyEvent
       
   375 // --------------------------------------------------------------------------
       
   376 //
       
   377 TBool CAknPhysicsConeObserver::MonitorKeyEvent( TKeyEvent* /*aEvent*/ )
       
   378     {
       
   379     iEventsAllowed = ETrue;
       
   380     if ( OngoingAction() == CAknPhysics::EAknPhysicsActionFlicking )
       
   381         {
       
   382         return ETrue;
       
   383         }
       
   384     return EFalse;
       
   385     }
       
   386 
       
   387 
       
   388 // --------------------------------------------------------------------------
       
   389 // CAknPhysicsConeObserver::PointerEventModified
       
   390 // --------------------------------------------------------------------------
       
   391 //
       
   392 TBool CAknPhysicsConeObserver::PointerEventModified( 
       
   393     TPointerEvent* aEvent, CCoeControl* aTargetControl )
       
   394     {
       
   395     TPoint originalPosition( aEvent->iPosition );
       
   396     CAknAppUi* appUi = AppUi();
       
   397     if ( appUi )
       
   398         {
       
   399         appUi->PointerEventModifier()->HandlePointerEvent( 
       
   400             *aEvent, aTargetControl );
       
   401         }
       
   402     if ( originalPosition != aEvent->iPosition )
       
   403         {
       
   404         return ETrue;
       
   405         }
       
   406     return EFalse;
       
   407     }
       
   408 
       
   409 
       
   410 // --------------------------------------------------------------------------
       
   411 // CAknPhysicsConeObserver::AppUi
       
   412 // --------------------------------------------------------------------------
       
   413 //
       
   414 CAknAppUi* CAknPhysicsConeObserver::AppUi()
       
   415     {
       
   416     if ( iCoeEnv )
       
   417         {
       
   418         return static_cast<CAknAppUi*>( iCoeEnv->AppUi() );        
       
   419         }
       
   420     return NULL;
       
   421     }
       
   422 
       
   423 
       
   424 // --------------------------------------------------------------------------
       
   425 // CAknPhysicsConeObserver::HandleGainingForeground
       
   426 // --------------------------------------------------------------------------
       
   427 //
       
   428 void CAknPhysicsConeObserver::HandleGainingForeground()
       
   429     {
       
   430     iEventsAllowed = ETrue;
       
   431     if ( iCoeEnv && !iMonitoringWs )
       
   432         {
       
   433         TRAP_IGNORE( iCoeEnv->AddMessageMonitorObserverL( *this ) );
       
   434         iMonitoringWs = ETrue;
       
   435         }
       
   436     if ( iPhysics )
       
   437         {
       
   438         iPhysics->SetBounceTactileFeedback( ETrue );
       
   439         }
       
   440     }
       
   441 
       
   442 
       
   443 // --------------------------------------------------------------------------
       
   444 // CAknPhysicsConeObserver::HandleLosingForeground
       
   445 // --------------------------------------------------------------------------
       
   446 //
       
   447 void CAknPhysicsConeObserver::HandleLosingForeground()
       
   448     {
       
   449     if ( iPhysics )
       
   450         {
       
   451         iPhysics->SetBounceTactileFeedback( EFalse );
       
   452         }
       
   453     if ( iPhysics 
       
   454         && OngoingAction() == CAknPhysics::EAknPhysicsActionFlicking )
       
   455         {
       
   456         iPhysics->StopPhysics();
       
   457         iPhysics->ResetFriction();
       
   458         }
       
   459     else if ( OngoingAction() == CAknPhysics::EAknPhysicsActionNone
       
   460         || OngoingAction() == CAknPhysics::EAknPhysicsActionDragging
       
   461         && iPhysics )
       
   462         {
       
   463         iPhysics->UpdateViewPosition();
       
   464         }
       
   465     if ( iCoeEnv && iMonitoringWs )
       
   466         {
       
   467         iCoeEnv->RemoveMessageMonitorObserver( *this );
       
   468         iMonitoringWs = EFalse;
       
   469         }
       
   470     }
       
   471 
       
   472 
       
   473 // --------------------------------------------------------------------------
       
   474 // CAknPhysicsConeObserver::MonitorWsMessage
       
   475 // --------------------------------------------------------------------------
       
   476 //
       
   477 void CAknPhysicsConeObserver::MonitorWsMessage( const TWsEvent &aEvent )
       
   478     {
       
   479     TBool stopPhysics( EFalse );
       
   480 
       
   481     // Check if physics should be stopped with pointer event
       
   482     if ( aEvent.Type() == EEventPointer )
       
   483         {
       
   484         CCoeControl* targetControl = 
       
   485             reinterpret_cast<CCoeControl*>( aEvent.Handle() );
       
   486         if ( targetControl )
       
   487             {
       
   488             stopPhysics = MonitorPointerEvent( 
       
   489                 aEvent.Pointer(), targetControl );
       
   490             }
       
   491         }
       
   492 
       
   493     // Check if physics should be stopped with key event
       
   494     else if ( aEvent.Type() == EEventKeyDown )
       
   495         {
       
   496         stopPhysics = MonitorKeyEvent( aEvent.Key() );
       
   497         }
       
   498 
       
   499     // Stop physics with layout change if flicking        
       
   500     else if ( aEvent.Type() == EEventScreenDeviceChanged
       
   501         && OngoingAction() == CAknPhysics::EAknPhysicsActionFlicking )
       
   502         {
       
   503         stopPhysics = ETrue;
       
   504         }
       
   505 
       
   506     if ( stopPhysics && iPhysics )
       
   507         {
       
   508         iPhysics->StopPhysics();
       
   509         iPhysics->ResetFriction();
       
   510         iEventsAllowed = EFalse;
       
   511         }
       
   512     }