use wxWindow::NewControlId() instead of wxNewId() to avoid clashes with the user...
[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
5 // Modified by: Vadim Zeitlin (wxTimerBase)
6 // Guillermo Rodriguez (global clean up)
7 // Created: 04/01/98
8 // RCS-ID: $Id$
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
15
16 #include "wx/defs.h"
17
18 #if wxUSE_GUI && wxUSE_TIMER
19
20 #include "wx/object.h"
21 #include "wx/longlong.h"
22 #include "wx/event.h"
23 #include "wx/stopwatch.h" // for backwards compatibility
24
25
26 // more readable flags for Start():
27
28 // generate notifications periodically until the timer is stopped (default)
29 #define wxTIMER_CONTINUOUS false
30
31 // only send the notification once and then stop the timer
32 #define wxTIMER_ONE_SHOT true
33
34 // the interface of wxTimer class
35 class WXDLLEXPORT wxTimerBase : public wxEvtHandler
36 {
37 public:
38 // ctors and initializers
39 // ----------------------
40
41 // default: if you don't call SetOwner(), your only chance to get timer
42 // notifications is to override Notify() in the derived class
43 wxTimerBase()
44 { Init(); SetOwner(this); }
45
46 // ctor which allows to avoid having to override Notify() in the derived
47 // class: the owner will get timer notifications which can be handled with
48 // EVT_TIMER
49 wxTimerBase(wxEvtHandler *owner, int timerid = wxID_ANY)
50 { Init(); SetOwner(owner, timerid); }
51
52 // same as ctor above
53 void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY)
54 {
55 m_owner = owner;
56 m_idTimer = timerid == wxID_ANY ? wxWindow::NewControlId() : timerid;
57 }
58
59 wxEvtHandler *GetOwner() const { return m_owner; }
60
61 virtual ~wxTimerBase();
62
63 // working with the timer
64 // ----------------------
65
66 // start the timer: if milliseconds == -1, use the same value as for the
67 // last Start()
68 //
69 // it is now valid to call Start() multiple times: this just restarts the
70 // timer if it is already running
71 virtual bool Start(int milliseconds = -1, bool oneShot = false);
72
73 // stop the timer
74 virtual void Stop() = 0;
75
76 // override this in your wxTimer-derived class if you want to process timer
77 // messages in it, use non default ctor or SetOwner() otherwise
78 virtual void Notify();
79
80 // getting info
81 // ------------
82
83 // return true if the timer is running
84 virtual bool IsRunning() const = 0;
85
86 // return the timer ID
87 int GetId() const { return m_idTimer; }
88
89 // get the (last) timer interval in milliseconds
90 int GetInterval() const { return m_milli; }
91
92 // return true if the timer is one shot
93 bool IsOneShot() const { return m_oneShot; }
94
95 protected:
96 // common part of all ctors
97 void Init()
98 { m_owner = NULL; m_idTimer = wxID_ANY; m_milli = 0; m_oneShot = false; }
99
100 wxEvtHandler *m_owner;
101 int m_idTimer;
102 int m_milli; // the timer interval
103 bool m_oneShot; // true if one shot
104
105 DECLARE_NO_COPY_CLASS(wxTimerBase)
106 };
107
108 // ----------------------------------------------------------------------------
109 // wxTimer itself
110 // ----------------------------------------------------------------------------
111
112 #if defined(__WXMSW__)
113 #include "wx/msw/timer.h"
114 #elif defined(__WXMOTIF__)
115 #include "wx/motif/timer.h"
116 #elif defined(__WXGTK20__)
117 #include "wx/gtk/timer.h"
118 #elif defined(__WXGTK__)
119 #include "wx/gtk1/timer.h"
120 #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXDFB__)
121 #include "wx/generic/timer.h"
122 #elif defined (__WXCOCOA__)
123 #include "wx/cocoa/timer.h"
124 #elif defined(__WXMAC__)
125 #include "wx/mac/timer.h"
126 #elif defined(__WXPM__)
127 #include "wx/os2/timer.h"
128 #endif
129
130 // ----------------------------------------------------------------------------
131 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
132 // ----------------------------------------------------------------------------
133
134 class WXDLLEXPORT wxTimerRunner
135 {
136 public:
137 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
138 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
139 : m_timer(timer)
140 {
141 m_timer.Start(milli, oneShot);
142 }
143
144 void Start(int milli, bool oneShot = false)
145 {
146 m_timer.Start(milli, oneShot);
147 }
148
149 ~wxTimerRunner()
150 {
151 if ( m_timer.IsRunning() )
152 {
153 m_timer.Stop();
154 }
155 }
156
157 private:
158 wxTimer& m_timer;
159
160 DECLARE_NO_COPY_CLASS(wxTimerRunner)
161 };
162
163 // ----------------------------------------------------------------------------
164 // wxTimerEvent
165 // ----------------------------------------------------------------------------
166
167 class WXDLLEXPORT wxTimerEvent : public wxEvent
168 {
169 public:
170 wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid)
171 {
172 m_eventType = wxEVT_TIMER;
173
174 m_interval = interval;
175 }
176
177 // accessors
178 int GetInterval() const { return m_interval; }
179
180 // implement the base class pure virtual
181 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
182
183 private:
184 int m_interval;
185
186 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
187 };
188
189 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
190
191 #define wxTimerEventHandler(func) \
192 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
193
194 #define EVT_TIMER(timerid, func) \
195 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
196
197 #endif // wxUSE_GUI && wxUSE_TIMER
198
199 #endif
200 // _WX_TIMER_H_BASE_