]> git.saurik.com Git - wxWidgets.git/blob - include/wx/timer.h
Committing in .
[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 { m_owner = owner; m_idTimer = timerid; }
55 wxEvtHandler * GetOwner() const { return m_owner; }
56
57 virtual ~wxTimerBase();
58
59 // working with the timer
60 // ----------------------
61
62 // start the timer: if milliseconds == -1, use the same value as for the
63 // last Start()
64 //
65 // it is now valid to call Start() multiple times: this just restarts the
66 // timer if it is already running
67 virtual bool Start(int milliseconds = -1, bool oneShot = false);
68
69 // stop the timer
70 virtual void Stop() = 0;
71
72 // override this in your wxTimer-derived class if you want to process timer
73 // messages in it, use non default ctor or SetOwner() otherwise
74 virtual void Notify();
75
76 // getting info
77 // ------------
78
79 // return true if the timer is running
80 virtual bool IsRunning() const = 0;
81
82 // return the timer ID
83 int GetId() const { return m_idTimer; }
84
85 // get the (last) timer interval in milliseconds
86 int GetInterval() const { return m_milli; }
87
88 // return true if the timer is one shot
89 bool IsOneShot() const { return m_oneShot; }
90
91 protected:
92 // common part of all ctors
93 void Init()
94 { m_owner = NULL; m_idTimer = wxID_ANY; m_milli = 0; m_oneShot = false; }
95
96 wxEvtHandler *m_owner;
97 int m_idTimer;
98 int m_milli; // the timer interval
99 bool m_oneShot; // true if one shot
100
101 DECLARE_NO_COPY_CLASS(wxTimerBase)
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(__WXGTK20__)
113 #include "wx/gtk/timer.h"
114 #elif defined(__WXGTK__)
115 #include "wx/gtk1/timer.h"
116 #elif defined(__WXX11__) || defined(__WXMGL__)
117 #include "wx/generic/timer.h"
118 #elif defined (__WXCOCOA__)
119 #include "wx/cocoa/timer.h"
120 #elif defined(__WXMAC__)
121 #include "wx/mac/timer.h"
122 #elif defined(__WXPM__)
123 #include "wx/os2/timer.h"
124 #endif
125
126 // ----------------------------------------------------------------------------
127 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
128 // ----------------------------------------------------------------------------
129
130 class WXDLLEXPORT wxTimerRunner
131 {
132 public:
133 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
134 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
135 : m_timer(timer)
136 {
137 m_timer.Start(milli, oneShot);
138 }
139
140 void Start(int milli, bool oneShot = false)
141 {
142 m_timer.Start(milli, oneShot);
143 }
144
145 ~wxTimerRunner()
146 {
147 if ( m_timer.IsRunning() )
148 {
149 m_timer.Stop();
150 }
151 }
152
153 private:
154 wxTimer& m_timer;
155
156 DECLARE_NO_COPY_CLASS(wxTimerRunner)
157 };
158
159 // ----------------------------------------------------------------------------
160 // wxTimerEvent
161 // ----------------------------------------------------------------------------
162
163 class WXDLLEXPORT wxTimerEvent : public wxEvent
164 {
165 public:
166 wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid)
167 {
168 m_eventType = wxEVT_TIMER;
169
170 m_interval = interval;
171 }
172
173 // accessors
174 int GetInterval() const { return m_interval; }
175
176 // implement the base class pure virtual
177 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
178
179 private:
180 int m_interval;
181
182 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
183 };
184
185 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
186
187 #define wxTimerEventHandler(func) \
188 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
189
190 #define EVT_TIMER(timerid, func) \
191 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
192
193 #endif // wxUSE_GUI && wxUSE_TIMER
194
195 #endif
196 // _WX_TIMER_H_BASE_