webengine/webkitutils/stmgesturefw/inc/callbacktimer.h
changeset 65 5bfc169077b2
parent 42 d39add9822e2
child 66 cacf6ee57968
equal deleted inserted replaced
42:d39add9822e2 65:5bfc169077b2
     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 the License "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:   Timer implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef CALLBACKTIMER_H_
       
    19 #define CALLBACKTIMER_H_
       
    20 // INCLUDES
       
    21 #include <e32std.h>
       
    22 #include <e32base.h>
       
    23 #include <coemain.h>
       
    24 #include <aknutils.h>
       
    25 #include <e32property.h>
       
    26 #include <w32std.h>
       
    27 
       
    28 namespace stmUiEventEngine
       
    29 {
       
    30 /**
       
    31  * define the method to be called by the timer; this includes the pointer number
       
    32  * the timer is associated with (maybe some other solution could do this, but...)
       
    33  */
       
    34 class CStateMachine ;
       
    35 typedef void (CStateMachine::*CallbackFunctionL)(TInt aPointerNumber);
       
    36 
       
    37 
       
    38 /**
       
    39  * CCallbackTimer to implement the timers.  This needs to be replaced
       
    40  * with something else if cross platform is needed.
       
    41  */
       
    42 NONSHARABLE_CLASS( CCallbackTimer ): public CTimer
       
    43 {
       
    44 public:
       
    45     /** Two-phase constructor */
       
    46     static CCallbackTimer* NewL(CStateMachine& aHelper,
       
    47             CallbackFunctionL aCallbackFunctionL, 
       
    48             TInt aDelay, 
       
    49             TInt aPointerNumber, 
       
    50             TBool aIsEnabled)
       
    51     {
       
    52         CCallbackTimer* self = new (ELeave) CCallbackTimer(aHelper,
       
    53                 aCallbackFunctionL, aDelay, aPointerNumber, aIsEnabled);
       
    54         CleanupStack::PushL(self);
       
    55         self->ConstructL(); // construct base class
       
    56         CActiveScheduler::Add(self);
       
    57         CleanupStack::Pop(self);
       
    58         return self;
       
    59     }
       
    60 
       
    61     /** Destructor */
       
    62     ~CCallbackTimer()
       
    63     {
       
    64         Cancel();
       
    65     }
       
    66 
       
    67     /** Set whether sending events is currently enabled */
       
    68     void SetEnabled(TBool aEnabled)
       
    69     {
       
    70         iIsEnabled = aEnabled;
       
    71         // cancel in case timer is already running
       
    72         Cancel();
       
    73     }
       
    74 
       
    75     /** @return whether sending events is currently enabled */
       
    76     TBool IsEnabled() const
       
    77     {
       
    78         return iIsEnabled;
       
    79     }
       
    80 
       
    81     /** Start the timer. Calls callback upon completion.  It is possible to give temporary delay if needed,
       
    82      * otherwise use the defined delay.
       
    83      */
       
    84     void Start(TInt aNewDelay = 0)
       
    85     {
       
    86         if (iIsEnabled)
       
    87         {
       
    88             Cancel();
       
    89             if (aNewDelay != 0)
       
    90             {
       
    91                 After(aNewDelay);
       
    92             }
       
    93             else
       
    94             {
       
    95                 After(iDelay);
       
    96             }
       
    97         }
       
    98     }
       
    99     void SetDelay(TInt aDelay)
       
   100     {
       
   101         iDelay = aDelay;
       
   102     }
       
   103     TInt GetDelay()
       
   104     {
       
   105         return iDelay;
       
   106     }
       
   107     /**
       
   108      * method for checking are we inside the RunL method...
       
   109      */
       
   110     TBool isTriggered()
       
   111     {
       
   112         return iIsTriggered;
       
   113     }
       
   114 
       
   115 private:
       
   116     /** Constructor */
       
   117     CCallbackTimer(CStateMachine& aHelper, CallbackFunctionL aCallbackFunctionL,
       
   118             TInt aDelay, TInt aPointerNumber, TBool aIsEnabled)
       
   119     :
       
   120                 CTimer(EPriorityRealTime - 1), // handle the timers always first before anything else
       
   121                 iHelper(aHelper), iCallbackFunctionL(aCallbackFunctionL),
       
   122                 iDelay(aDelay), iIsEnabled(aIsEnabled), iPointerNumber(aPointerNumber)
       
   123     {
       
   124         iIsTriggered = EFalse;
       
   125     }
       
   126 
       
   127     void RunL() // From CActive
       
   128     {
       
   129         iIsTriggered = ETrue ;
       
   130         (iHelper .*iCallbackFunctionL)(iPointerNumber);
       
   131         iIsTriggered = EFalse ;
       
   132     }
       
   133 
       
   134 private:
       
   135     /// helper object that will be called back when timer is triggered
       
   136     CStateMachine& iHelper;
       
   137     /// Function in the iHelper object call
       
   138     CallbackFunctionL iCallbackFunctionL;
       
   139     /// How long a time to wait before calling back after Start()
       
   140     TInt iDelay;
       
   141     /// whether sending holding events is currently enabled
       
   142     TBool iIsEnabled;
       
   143     /**
       
   144      * while we are inside RunL iIsTriggered = ETrue
       
   145      */
       
   146     TBool iIsTriggered;
       
   147     /*
       
   148      * in multitouch now we need timers for each pointer
       
   149      * could this be handled in some other way?
       
   150      */
       
   151     TInt iPointerNumber ;
       
   152 };
       
   153 } // namespace
       
   154 
       
   155 #endif /* CALLBACKTIMER_H_ */