]> git.saurik.com Git - wxWidgets.git/blob - include/wx/timer.h
Quick and dirty fix for building with COMPATIBILITY_2_4 off.
[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 (wxTimer), Sylvain Bougnoux (wxStopWatch)
5 // Modified by: Vadim Zeitlin (wxTimerBase)
6 // Guillermo Rodriguez (global clean up)
7 // Created: 04/01/98
8 // RCS-ID: $Id$
9 // Copyright: (c) wxWindows team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
15
16 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "timerbase.h"
18 #endif
19
20 #include "wx/setup.h"
21 #include "wx/object.h"
22 #include "wx/longlong.h"
23 #include "wx/event.h"
24
25 #include "wx/stopwatch.h" // for backwards compatibility
26
27 #if wxUSE_GUI && wxUSE_TIMER
28
29 // ----------------------------------------------------------------------------
30 // wxTimer
31 // ----------------------------------------------------------------------------
32
33 // more readable flags for Start():
34
35 // generate notifications periodically until the timer is stopped (default)
36 #define wxTIMER_CONTINUOUS FALSE
37
38 // only send the notification once and then stop the timer
39 #define wxTIMER_ONE_SHOT TRUE
40
41 // the interface of wxTimer class
42 class WXDLLEXPORT wxTimerBase : 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 wxTimerBase() { Init(); SetOwner(this); }
51
52 // ctor which allows to avoid having to override Notify() in the derived
53 // class: the owner will get timer notifications which can be handled with
54 // EVT_TIMER
55 wxTimerBase(wxEvtHandler *owner, int id = -1)
56 { Init(); SetOwner(owner, id); }
57
58 // same as ctor above
59 void SetOwner(wxEvtHandler *owner, int id = -1)
60 { m_owner = owner; m_idTimer = id; }
61
62 virtual ~wxTimerBase();
63
64 // working with the timer
65 // ----------------------
66
67 // start the timer: if milliseconds == -1, use the same value as for the
68 // last Start()
69 //
70 // it is now valid to call Start() multiple times: this just restarts the
71 // timer if it is already running
72 virtual bool Start(int milliseconds = -1, bool oneShot = FALSE);
73
74 // stop the timer
75 virtual void Stop() = 0;
76
77 // override this in your wxTimer-derived class if you want to process timer
78 // messages in it, use non default ctor or SetOwner() otherwise
79 virtual void Notify();
80
81 // getting info
82 // ------------
83
84 // return TRUE if the timer is running
85 virtual bool IsRunning() const = 0;
86
87 // get the (last) timer interval in the milliseconds
88 int GetInterval() const { return m_milli; }
89
90 // return TRUE if the timer is one shot
91 bool IsOneShot() const { return m_oneShot; }
92
93 #if WXWIN_COMPATIBILITY_2
94 // deprecated functions
95 int Interval() const { return GetInterval(); };
96 bool OneShot() const { return IsOneShot(); }
97 #endif // WXWIN_COMPATIBILITY_2
98
99 protected:
100 // common part of all ctors
101 void Init() { m_oneShot = FALSE; m_milli = 0; }
102
103 wxEvtHandler *m_owner;
104 int m_idTimer;
105
106 int m_milli; // the timer interval
107 bool m_oneShot; // TRUE if one shot
108
109 DECLARE_NO_COPY_CLASS(wxTimerBase)
110 };
111
112 // ----------------------------------------------------------------------------
113 // wxTimer itself
114 // ----------------------------------------------------------------------------
115
116 #if defined(__WXMSW__)
117 #include "wx/msw/timer.h"
118 #elif defined(__WXMOTIF__)
119 #include "wx/motif/timer.h"
120 #elif defined(__WXGTK__)
121 #include "wx/gtk/timer.h"
122 #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXCOCOA__)
123 #include "wx/generic/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
161 // ----------------------------------------------------------------------------
162 // wxTimerEvent
163 // ----------------------------------------------------------------------------
164
165 class WXDLLEXPORT wxTimerEvent : public wxEvent
166 {
167 public:
168 wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id)
169 {
170 m_eventType = wxEVT_TIMER;
171
172 m_interval = interval;
173 }
174
175 // accessors
176 int GetInterval() const { return m_interval; }
177
178 // implement the base class pure virtual
179 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
180
181 private:
182 int m_interval;
183
184 DECLARE_DYNAMIC_CLASS(wxTimerEvent)
185 };
186
187 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
188
189 #define EVT_TIMER(id, func) \
190 DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
191
192 #endif // wxUSE_GUI && wxUSE_TIMER
193
194 #endif
195 // _WX_TIMER_H_BASE_