]> git.saurik.com Git - wxWidgets.git/blame - include/wx/timer.h
wxHTML src code indentation now conforms (more) to wxWin coding style
[wxWidgets.git] / include / wx / timer.h
CommitLineData
0470b1e6
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/timer.h
ed791986
VZ
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)
0470b1e6
VZ
7// Created: 04/01/98
8// RCS-ID: $Id$
ed791986
VZ
9// Copyright: (c) wxWindows team
10// Licence: wxWindows license
0470b1e6
VZ
11/////////////////////////////////////////////////////////////////////////////
12
34138703
JS
13#ifndef _WX_TIMER_H_BASE_
14#define _WX_TIMER_H_BASE_
c801d85f 15
0470b1e6
VZ
16#ifdef __GNUG__
17 #pragma interface "timerbase.h"
18#endif
19
d895ad7c 20#include "wx/setup.h"
0470b1e6 21#include "wx/object.h"
d895ad7c 22#include "wx/longlong.h"
ed791986 23#include "wx/event.h"
0470b1e6 24
52a07708
VZ
25#if wxUSE_GUI
26
0470b1e6
VZ
27// ----------------------------------------------------------------------------
28// wxTimer
29// ----------------------------------------------------------------------------
30
31// the interface of wxTimer class
32class WXDLLEXPORT wxTimerBase : public wxObject
33{
34public:
ed791986
VZ
35 // ctors and initializers
36 // ----------------------
37
38 // default: if you don't call SetOwner(), your only chance to get timer
39 // notifications is to override Notify() in the derived class
40 wxTimerBase() { Init(); SetOwner(NULL); }
41
42 // ctor which allows to avoid having to override Notify() in the derived
43 // class: the owner will get timer notifications which can be handled with
44 // EVT_TIMER
45 wxTimerBase(wxEvtHandler *owner, int id = -1)
06b466c7 46 { Init(); SetOwner(owner, id); }
ed791986
VZ
47
48 // same as ctor above
49 void SetOwner(wxEvtHandler *owner, int id = -1)
50 { m_owner = owner; m_idTimer = id; }
0470b1e6
VZ
51
52 // working with the timer
53 // ----------------------
54
55 // start the timer: if milliseconds == -1, use the same value as for the
56 // last Start()
99646f7e
VZ
57 //
58 // it is now valid to call Start() multiple times: this just restarts the
59 // timer if it is already running
60 virtual bool Start(int milliseconds = -1, bool oneShot = FALSE);
0470b1e6
VZ
61
62 // stop the timer
63 virtual void Stop() = 0;
64
ed791986
VZ
65 // override this in your wxTimer-derived class if you want to process timer
66 // messages in it, use non default ctor or SetOwner() otherwise
67 virtual void Notify();
0470b1e6
VZ
68
69 // getting info
70 // ------------
71
72 // return TRUE if the timer is running
73 virtual bool IsRunning() const = 0;
74
75 // get the (last) timer interval in the milliseconds
76 int GetInterval() const { return m_milli; }
77
78 // return TRUE if the timer is one shot
79 bool IsOneShot() const { return m_oneShot; }
80
81#if WXWIN_COMPATIBILITY_2
82 // deprecated functions
83 int Interval() const { return GetInterval(); };
84 bool OneShot() const { return IsOneShot(); }
85#endif // WXWIN_COMPATIBILITY_2
86
87protected:
ed791986
VZ
88 // common part of all ctors
89 void Init() { m_oneShot = FALSE; m_milli = 0; }
90
91 wxEvtHandler *m_owner;
92 int m_idTimer;
93
0470b1e6
VZ
94 int m_milli; // the timer interval
95 bool m_oneShot; // TRUE if one shot
96};
97
ed791986
VZ
98// ----------------------------------------------------------------------------
99// wxTimer itself
100// ----------------------------------------------------------------------------
101
2049ba38 102#if defined(__WXMSW__)
0470b1e6 103 #include "wx/msw/timer.h"
2049ba38 104#elif defined(__WXMOTIF__)
0470b1e6 105 #include "wx/motif/timer.h"
2049ba38 106#elif defined(__WXGTK__)
0470b1e6 107 #include "wx/gtk/timer.h"
b4e76e0d 108#elif defined(__WXQT__)
0470b1e6 109 #include "wx/qt/timer.h"
34138703 110#elif defined(__WXMAC__)
0470b1e6 111 #include "wx/mac/timer.h"
1777b9bb 112#elif defined(__WXPM__)
0470b1e6 113 #include "wx/os2/timer.h"
34138703 114#elif defined(__WXSTUBS__)
0470b1e6 115 #include "wx/stubs/timer.h"
c801d85f
KB
116#endif
117
52a07708
VZ
118// ----------------------------------------------------------------------------
119// wxTimerRunner: starts the timer in its ctor, stops in the dtor
120// ----------------------------------------------------------------------------
121
122class WXDLLEXPORT wxTimerRunner
123{
124public:
125 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
126 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE)
127 : m_timer(timer)
128 {
129 m_timer.Start(milli, oneShot);
130 }
131
132 void Start(int milli, bool oneShot = FALSE)
133 {
134 m_timer.Start(milli, oneShot);
135 }
136
137 ~wxTimerRunner()
138 {
139 if ( m_timer.IsRunning() )
140 {
141 m_timer.Stop();
142 }
143 }
144
145private:
146 wxTimer& m_timer;
147};
148
0470b1e6 149// ----------------------------------------------------------------------------
ed791986
VZ
150// wxTimerEvent
151// ----------------------------------------------------------------------------
152
153class WXDLLEXPORT wxTimerEvent : public wxEvent
154{
155public:
156 wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id)
157 {
158 m_eventType = wxEVT_TIMER;
159
160 m_interval = interval;
161 }
162
163 // accessors
164 int GetInterval() const { return m_interval; }
165
166private:
167 int m_interval;
168
169 DECLARE_DYNAMIC_CLASS(wxTimerEvent)
170};
171
172typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
173
174#define EVT_TIMER(id, func) { wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL},
175
52a07708
VZ
176#endif // wxUSE_GUI
177
ed791986
VZ
178// ----------------------------------------------------------------------------
179// wxStopWatch: measure time intervals with up to 1ms resolution
0470b1e6
VZ
180// ----------------------------------------------------------------------------
181
182class WXDLLEXPORT wxStopWatch
f0599ea9 183{
ed791986 184public:
0470b1e6 185 // ctor starts the stop watch
d895ad7c
GRG
186 wxStopWatch() { Start(); }
187 void Start(long t = 0);
92da8bde
VZ
188 void Pause() { m_pause = GetElapsedTime(); }
189 void Resume() { Start(m_pause); }
0470b1e6 190
d895ad7c 191 // get elapsed time since the last Start() or Pause() in milliseconds
0470b1e6 192 long Time() const;
f0599ea9 193
0470b1e6
VZ
194protected:
195 // returns the elapsed time since t0
d895ad7c 196 long GetElapsedTime() const;
ed791986 197
f0599ea9 198private:
d895ad7c
GRG
199 wxLongLong m_t0; // the time of the last Start()
200 long m_pause; // the time of the last Pause() or 0
f0599ea9
SB
201};
202
0470b1e6 203
ed791986 204// Starts a global timer
d895ad7c 205// -- DEPRECATED: use wxStopWatch instead
f0599ea9
SB
206void WXDLLEXPORT wxStartTimer();
207
d895ad7c
GRG
208// Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
209// -- DEPRECATED: use wxStopWatch instead
f0599ea9
SB
210long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE);
211
0470b1e6 212// ----------------------------------------------------------------------------
d895ad7c 213// global time functions
0470b1e6
VZ
214// ----------------------------------------------------------------------------
215
d895ad7c 216// Get number of seconds since local time 00:00:00 Jan 1st 1970.
f6bcfd97 217extern long WXDLLEXPORT wxGetLocalTime();
d895ad7c
GRG
218
219// Get number of seconds since GMT 00:00:00, Jan 1st 1970.
f6bcfd97
BP
220extern long WXDLLEXPORT wxGetUTCTime();
221
222// Get number of milliseconds since local time 00:00:00 Jan 1st 1970
223extern wxLongLong WXDLLEXPORT wxGetLocalTimeMillis();
d895ad7c 224
df0c3226 225#define wxGetCurrentTime() wxGetLocalTime()
0470b1e6 226
c801d85f 227#endif
34138703 228 // _WX_TIMER_H_BASE_