]> git.saurik.com Git - wxWidgets.git/blob - include/wx/timer.h
using Run of base class
[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 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TIMER_H_BASE_
13 #define _WX_TIMER_H_BASE_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_TIMER
18
19 #include "wx/object.h"
20 #include "wx/longlong.h"
21 #include "wx/event.h"
22 #include "wx/stopwatch.h" // for backwards compatibility
23 #include "wx/utils.h"
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 class WXDLLIMPEXP_FWD_BASE wxTimerImpl;
35 class WXDLLIMPEXP_FWD_BASE wxTimerEvent;
36
37 // timer event type
38 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);
39
40 // the interface of wxTimer class
41 class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler
42 {
43 public:
44 // ctors and initializers
45 // ----------------------
46
47 // default: if you don't call SetOwner(), your only chance to get timer
48 // notifications is to override Notify() in the derived class
49 wxTimer()
50 {
51 Init();
52 SetOwner(this);
53 }
54
55 // ctor which allows to avoid having to override Notify() in the derived
56 // class: the owner will get timer notifications which can be handled with
57 // EVT_TIMER
58 wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
59 {
60 Init();
61 SetOwner(owner, timerid);
62 }
63
64 // same as ctor above
65 void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
66
67 virtual ~wxTimer();
68
69
70 // working with the timer
71 // ----------------------
72
73 // NB: Start() and Stop() are not supposed to be overridden, they are only
74 // virtual for historical reasons, only Notify() can be overridden
75
76 // start the timer: if milliseconds == -1, use the same value as for the
77 // last Start()
78 //
79 // it is now valid to call Start() multiple times: this just restarts the
80 // timer if it is already running
81 virtual bool Start(int milliseconds = -1, bool oneShot = false);
82
83 // start the timer for one iteration only, this is just a simple wrapper
84 // for Start()
85 bool StartOnce(int milliseconds = -1) { return Start(milliseconds, true); }
86
87 // stop the timer, does nothing if the timer is not running
88 virtual void Stop();
89
90 // override this in your wxTimer-derived class if you want to process timer
91 // messages in it, use non default ctor or SetOwner() otherwise
92 virtual void Notify();
93
94
95 // accessors
96 // ---------
97
98 // get the object notified about the timer events
99 wxEvtHandler *GetOwner() const;
100
101 // return true if the timer is running
102 bool IsRunning() const;
103
104 // return the timer ID
105 int GetId() const;
106
107 // get the (last) timer interval in milliseconds
108 int GetInterval() const;
109
110 // return true if the timer is one shot
111 bool IsOneShot() const;
112
113 protected:
114 // common part of all ctors
115 void Init();
116
117 wxTimerImpl *m_impl;
118
119 wxDECLARE_NO_COPY_CLASS(wxTimer);
120 };
121
122 // ----------------------------------------------------------------------------
123 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
124 // ----------------------------------------------------------------------------
125
126 class WXDLLIMPEXP_BASE wxTimerRunner
127 {
128 public:
129 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
130 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
131 : m_timer(timer)
132 {
133 m_timer.Start(milli, oneShot);
134 }
135
136 void Start(int milli, bool oneShot = false)
137 {
138 m_timer.Start(milli, oneShot);
139 }
140
141 ~wxTimerRunner()
142 {
143 if ( m_timer.IsRunning() )
144 {
145 m_timer.Stop();
146 }
147 }
148
149 private:
150 wxTimer& m_timer;
151
152 wxDECLARE_NO_COPY_CLASS(wxTimerRunner);
153 };
154
155 // ----------------------------------------------------------------------------
156 // wxTimerEvent
157 // ----------------------------------------------------------------------------
158
159 class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
160 {
161 public:
162 wxTimerEvent()
163 : wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; }
164
165 wxTimerEvent(wxTimer& timer)
166 : wxEvent(timer.GetId(), wxEVT_TIMER),
167 m_timer(&timer)
168 {
169 SetEventObject(timer.GetOwner());
170 }
171
172 // accessors
173 int GetInterval() const { return m_timer->GetInterval(); }
174 wxTimer& GetTimer() const { return *m_timer; }
175
176 // implement the base class pure virtual
177 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
178 virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_TIMER; }
179
180 private:
181 wxTimer* m_timer;
182
183 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
184 };
185
186 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
187
188 #define wxTimerEventHandler(func) \
189 wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
190
191 #define EVT_TIMER(timerid, func) \
192 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
193
194 #endif // wxUSE_TIMER
195
196 #endif // _WX_TIMER_H_BASE_