mulwidgets/gesturehelper/src/pointercapturer.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     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 * Description:  Alf control group and control for capturing pointer events
       
    15 *
       
    16 */
       
    17 
       
    18 #include "pointercapturer.h"
       
    19 
       
    20 #include <alf/alfcontrol.h>
       
    21 #include <alf/alfcontrolgroup.h>
       
    22 #include <alf/alfdisplay.h>
       
    23 #include <alf/alfenv.h>
       
    24 #include <alf/alfevent.h>
       
    25 #include <alf/alfroster.h>
       
    26 
       
    27 #include "gesturehelperimpl.h"
       
    28 
       
    29 namespace GestureHelper
       
    30 {
       
    31 
       
    32 /**
       
    33  * CPointerCaptureControl
       
    34  * Gesture control own a gesture helper, and captures any pointer events that 
       
    35  * no other control captures. After it captures pointer down event, it will 
       
    36  * capture all pointer events until pointer up. It does this by bringing its
       
    37  * control group to the top upon pointer down, and lowering the control group
       
    38  * to the botton upon pointer up.
       
    39  */
       
    40 NONSHARABLE_CLASS( CPointerCaptureControl ) : public CAlfControl
       
    41     {
       
    42 public: 
       
    43     /** Two-phased constructor */
       
    44     static CPointerCaptureControl* NewLC( CGestureHelperImpl& aHelper, 
       
    45             CAlfEnv& aEnv, CAlfDisplay& aDisplay )
       
    46         {
       
    47         CPointerCaptureControl* self = new ( ELeave ) CPointerCaptureControl( aHelper );
       
    48         CleanupStack::PushL( self );
       
    49         self->ConstructL( aEnv ); // construct base class
       
    50         self->BindDisplay( aDisplay );
       
    51         self->AppendVisualL( EAlfVisualTypeVisual );       
       
    52         return self;
       
    53         }
       
    54     
       
    55     void StartCaptureL()
       
    56         {
       
    57         User::LeaveIfError( Display()->Roster().SetPointerEventObservers( 
       
    58             EAlfPointerEventReportDrag | EAlfPointerEventReportUnhandled, 
       
    59                 *this ) );
       
    60         Display()->Roster().ShowL( *ControlGroup(), KAlfRosterShowAtTop );
       
    61         }
       
    62     
       
    63     void StopCapture()
       
    64         {
       
    65         ( void ) Display()->Roster().SetPointerEventObservers( 0, *this );
       
    66         TRAP_IGNORE( Display()->Roster().ShowL( *ControlGroup(), KAlfRosterShowAtBottom ) );
       
    67         }
       
    68     
       
    69 private:
       
    70     /** Constructor */
       
    71     CPointerCaptureControl( CGestureHelperImpl& aHelper )
       
    72             : iHelper( aHelper )
       
    73         {
       
    74         }
       
    75     
       
    76     // From CAlfControl
       
    77     TBool OfferEventL( const TAlfEvent& aEvent )
       
    78         {
       
    79         TBool consumed = EFalse;
       
    80         if ( aEvent.IsPointerEvent() )
       
    81             {
       
    82             // pointer down for first pointer should never come here if everything is 
       
    83 			// working properly. if pointer event comes here, stop capturing and allow the 
       
    84 			// event to fall to the controls below
       
    85             if ( iHelper.StrayEvent(aEvent.PointerEvent() ))
       
    86                 {
       
    87                 StopCapture();
       
    88                 }
       
    89             else 
       
    90                 {
       
    91                 consumed = iHelper.HandlePointerEventL( aEvent.PointerEvent() );
       
    92                 }
       
    93             }
       
    94         return consumed;
       
    95         }
       
    96     
       
    97 private:    
       
    98     /// gesture helper that analyses pointer sequences. own.
       
    99     CGestureHelperImpl& iHelper;
       
   100     
       
   101     };
       
   102     
       
   103 } // namespace GestureHelper
       
   104 
       
   105 using namespace GestureHelper;
       
   106     
       
   107 // ----------------------------------------------------------------------------
       
   108 // Two phase constructor
       
   109 // ----------------------------------------------------------------------------
       
   110 //
       
   111 CPointerCapturer* CPointerCapturer::NewL()
       
   112     {
       
   113     return new ( ELeave ) CPointerCapturer();
       
   114     }
       
   115 
       
   116 // ----------------------------------------------------------------------------
       
   117 // Two phase constructor
       
   118 // ----------------------------------------------------------------------------
       
   119 //
       
   120 CPointerCapturer::~CPointerCapturer()
       
   121     {
       
   122     if ( iCaptureControl )
       
   123         {
       
   124         // deleting the control group will also delete the control
       
   125         iCaptureControl->Env().DeleteControlGroup( iControlGroupId );
       
   126         }
       
   127     }
       
   128 
       
   129 // ----------------------------------------------------------------------------
       
   130 // InitForAlfredL
       
   131 // ----------------------------------------------------------------------------
       
   132 //
       
   133 void CPointerCapturer::InitForAlfredL( CGestureHelperImpl& aHelper, 
       
   134         CAlfEnv& aEnv, CAlfDisplay& aDisplay, TInt aControlGroupId )
       
   135     {
       
   136     CAlfControlGroup& group = aEnv.NewControlGroupL( aControlGroupId );
       
   137     CPointerCaptureControl* control = CPointerCaptureControl::NewLC( aHelper, 
       
   138         aEnv, aDisplay );
       
   139     group.AppendL( control );
       
   140     CleanupStack::Pop( control );    
       
   141    
       
   142     // store control only after it has been successfully added to group
       
   143     iCaptureControl = control;
       
   144     iControlGroupId = aControlGroupId;
       
   145     }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // StartCaptureL
       
   149 // ----------------------------------------------------------------------------
       
   150 //
       
   151 void CPointerCapturer::StartL()
       
   152     {
       
   153     if ( iCaptureControl )
       
   154         {
       
   155         iCaptureControl->StartCaptureL();
       
   156         }
       
   157     }
       
   158 
       
   159 // ----------------------------------------------------------------------------
       
   160 // StopCapture
       
   161 // ----------------------------------------------------------------------------
       
   162 //
       
   163 void CPointerCapturer::Stop()
       
   164     {
       
   165     if ( iCaptureControl )
       
   166         {
       
   167         iCaptureControl->StopCapture();
       
   168         }
       
   169     }