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