]> git.saurik.com Git - wxWidgets.git/blob - include/wx/timer.h
30e48fca4453ae08abad3359aadbaae48b624be6
[wxWidgets.git] / include / wx / timer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/timer.h
3 // Purpose: wxTimer, wxStopWatch and global time-related functions
4 // Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch)
5 // Modified by: Vadim Zeitlin (wxTimerBase)
6 // Guillermo Rodriguez (global clean up)
7 // Created: 04/01/98
8 // RCS-ID: $Id$
9 // Copyright: (c) wxWindows team
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
15
16 #ifdef __GNUG__
17 #pragma interface "timerbase.h"
18 #endif
19
20 #include "wx/setup.h"
21 #include "wx/object.h"
22 #include "wx/longlong.h"
23 #include "wx/event.h"
24
25 // ----------------------------------------------------------------------------
26 // wxTimer
27 // ----------------------------------------------------------------------------
28
29 // the interface of wxTimer class
30 class WXDLLEXPORT wxTimerBase : public wxObject
31 {
32 public:
33 // ctors and initializers
34 // ----------------------
35
36 // default: if you don't call SetOwner(), your only chance to get timer
37 // notifications is to override Notify() in the derived class
38 wxTimerBase() { Init(); SetOwner(NULL); }
39
40 // ctor which allows to avoid having to override Notify() in the derived
41 // class: the owner will get timer notifications which can be handled with
42 // EVT_TIMER
43 wxTimerBase(wxEvtHandler *owner, int id = -1)
44 { Init(); SetOwner(owner, -1); }
45
46 // same as ctor above
47 void SetOwner(wxEvtHandler *owner, int id = -1)
48 { m_owner = owner; m_idTimer = id; }
49
50 // working with the timer
51 // ----------------------
52
53 // start the timer: if milliseconds == -1, use the same value as for the
54 // last Start()
55 virtual bool Start(int milliseconds = -1, bool oneShot = FALSE)
56 {
57 if ( milliseconds != -1 )
58 {
59 m_milli = milliseconds;
60 }
61
62 m_oneShot = oneShot;
63
64 return TRUE;
65 }
66
67
68 // stop the timer
69 virtual void Stop() = 0;
70
71 // override this in your wxTimer-derived class if you want to process timer
72 // messages in it, use non default ctor or SetOwner() otherwise
73 virtual void Notify();
74
75 // getting info
76 // ------------
77
78 // return TRUE if the timer is running
79 virtual bool IsRunning() const = 0;
80
81 // get the (last) timer interval in the milliseconds
82 int GetInterval() const { return m_milli; }
83
84 // return TRUE if the timer is one shot
85 bool IsOneShot() const { return m_oneShot; }
86
87 #if WXWIN_COMPATIBILITY_2
88 // deprecated functions
89 int Interval() const { return GetInterval(); };
90 bool OneShot() const { return IsOneShot(); }
91 #endif // WXWIN_COMPATIBILITY_2
92
93 protected:
94 // common part of all ctors
95 void Init() { m_oneShot = FALSE; m_milli = 0; }
96
97 wxEvtHandler *m_owner;
98 int m_idTimer;
99
100 int m_milli; // the timer interval
101 bool m_oneShot; // TRUE if one shot
102 };
103
104 // ----------------------------------------------------------------------------
105 // wxTimer itself
106 // ----------------------------------------------------------------------------
107
108 #if defined(__WXMSW__)
109 #include "wx/msw/timer.h"
110 #elif defined(__WXMOTIF__)
111 #include "wx/motif/timer.h"
112 #elif defined(__WXGTK__)
113 #include "wx/gtk/timer.h"
114 #elif defined(__WXQT__)
115 #include "wx/qt/timer.h"
116 #elif defined(__WXMAC__)
117 #include "wx/mac/timer.h"
118 #elif defined(__WXPM__)
119 #include "wx/os2/timer.h"
120 #elif defined(__WXSTUBS__)
121 #include "wx/stubs/timer.h"
122 #endif
123
124 // ----------------------------------------------------------------------------
125 // wxTimerEvent
126 // ----------------------------------------------------------------------------
127
128 class WXDLLEXPORT wxTimerEvent : public wxEvent
129 {
130 public:
131 wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id)
132 {
133 m_eventType = wxEVT_TIMER;
134
135 m_interval = interval;
136 }
137
138 // accessors
139 int GetInterval() const { return m_interval; }
140
141 private:
142 int m_interval;
143
144 DECLARE_DYNAMIC_CLASS(wxTimerEvent)
145 };
146
147 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
148
149 #define EVT_TIMER(id, func) { wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL},
150
151 // ----------------------------------------------------------------------------
152 // wxStopWatch: measure time intervals with up to 1ms resolution
153 // ----------------------------------------------------------------------------
154
155 class WXDLLEXPORT wxStopWatch
156 {
157 public:
158 // ctor starts the stop watch
159 wxStopWatch() { Start(); }
160 void Start(long t = 0);
161 inline void Pause() { m_pause = GetElapsedTime(); }
162 inline void Resume() { Start(m_pause); }
163
164 // get elapsed time since the last Start() or Pause() in milliseconds
165 long Time() const;
166
167 protected:
168 // returns the elapsed time since t0
169 long GetElapsedTime() const;
170
171 private:
172 wxLongLong m_t0; // the time of the last Start()
173 long m_pause; // the time of the last Pause() or 0
174 };
175
176
177 // Starts a global timer
178 // -- DEPRECATED: use wxStopWatch instead
179 void WXDLLEXPORT wxStartTimer();
180
181 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
182 // -- DEPRECATED: use wxStopWatch instead
183 long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE);
184
185 // ----------------------------------------------------------------------------
186 // global time functions
187 // ----------------------------------------------------------------------------
188
189 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
190 long WXDLLEXPORT wxGetLocalTime();
191
192 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
193 long WXDLLEXPORT wxGetUTCTime();
194
195 #define wxGetCurrentTime() wxGetLocalTime()
196
197 #endif
198 // _WX_TIMER_H_BASE_