ubit
utimer.hpp
1 /************************************************************************
2  *
3  * utimer.hpp
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _utimer_hpp_
18 #define _utimer_hpp_ 1
19 
20 extern "C" {
21  struct timeval;
22 }
23 
24 #include <ubit/uobject.hpp>
25 namespace ubit {
26 
45  class UTimer : public UNode {
46  public:
47  UCLASS(UTimer)
48 
49  UTimer(bool auto_delete = true);
55  UTimer(unsigned long delay, int ntimes, bool auto_delete = true);
68  virtual ~UTimer();
69 
70  virtual void onAction(UCall& callback);
76  virtual void onTimeout(UCall& callback) {onAction(callback);}
78 
79  virtual void start();
83  virtual void start(unsigned long delay, int ntimes);
88  virtual void stop();
94  unsigned long getDelay() const {return delay;}
95 
96  void setDelay(unsigned long time);
97 
98  bool isRunning() const {return is_running;}
100 
101  // - impl - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 #ifndef NO_DOC
103 
104  int getTimerNo() const {return timer_no;}
105  bool timerCB();
106 
107  private:
108  friend class UAppli;
109  friend class UAppliImpl;
110  friend class UTimerImpl;
111  bool auto_delete, is_running, is_looping;
112  int timer_no, ntimes;
113  unsigned long delay;
114 
115  void removeTimer();
116 
117 #if UBIT_WITH_X11
118  struct timeval& timeout;
119 #elif UBIT_WITH_GDK
120  unsigned int gid; // timer ID when GDK is used
121 #endif
122 #endif
123  };
124 
125  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126 
127  class UTimerImpl {
128  public:
129  static unsigned long getTime();
130  static void getTime(timeval& time);
131  static void minTime(struct timeval& mintime, struct timeval& time);
132  static void addTime(struct timeval& time, unsigned long millisec_delay);
133  static bool lessTime(struct timeval& time, struct timeval& t2);
134 
135  bool resetTimers(struct timeval& delay);
136  void fireTimers();
137 
138  typedef std::vector<UTimer*> Timers;
139  Timers timers;
140  };
141 
142 }
143 #endif
144 
a UTimer object fires callbacks after a given delay.
Definition: utimer.hpp:45
virtual void onTimeout(UCall &callback)
synonym for onAction().
Definition: utimer.hpp:76
virtual void onAction(UCall &callback)
adds a callback that is fired when the time is up.
Definition: utimer.cpp:203
Definition: uappliImpl.hpp:40
Base class of objects that can be added to the UBIT scene graph (SEE DETAILS!).
Definition: unode.hpp:38
Definition: utimer.hpp:127
virtual void start()
(re)starts the timer.
Definition: utimer.cpp:114
The Application Context.
Definition: uappli.hpp:79
UTimer(bool auto_delete=true)
creates a new timer.
Definition: utimer.cpp:175
base class of callback objects for firing functions or methods.
Definition: ucall.hpp:144
virtual void stop()
stops the timer.
Definition: utimer.cpp:161
Definition: uhardfont.hpp:31
bool isRunning() const
returns true if the timer is currently running.
Definition: utimer.hpp:98