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