]> git.saurik.com Git - wxWidgets.git/blame - include/wx/timer.h
wxRTC: fixed guidelines overwriting adjacent cell borders; corrected capitalisation...
[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 7// Created: 04/01/98
99d80019 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
0470b1e6
VZ
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_TIMER_H_BASE_
13#define _WX_TIMER_H_BASE_
c801d85f 14
2ecf902b
WS
15#include "wx/defs.h"
16
c2ca375c 17#if wxUSE_TIMER
2ecf902b 18
0470b1e6 19#include "wx/object.h"
d895ad7c 20#include "wx/longlong.h"
ed791986 21#include "wx/event.h"
5fb98c22 22#include "wx/stopwatch.h" // for backwards compatibility
c2ca375c 23#include "wx/utils.h"
5fb98c22 24
0470b1e6 25
b0a2d8a8
VZ
26// more readable flags for Start():
27
28// generate notifications periodically until the timer is stopped (default)
cb719f2e 29#define wxTIMER_CONTINUOUS false
b0a2d8a8
VZ
30
31// only send the notification once and then stop the timer
cb719f2e 32#define wxTIMER_ONE_SHOT true
b0a2d8a8 33
b5dbe15d 34class WXDLLIMPEXP_FWD_BASE wxTimerImpl;
3c778901
VZ
35class WXDLLIMPEXP_FWD_BASE wxTimerEvent;
36
37// timer event type
9b11752c 38wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);
c2ca375c 39
0470b1e6 40// the interface of wxTimer class
c2ca375c 41class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler
0470b1e6
VZ
42{
43public:
ed791986
VZ
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
c2ca375c
VZ
49 wxTimer()
50 {
51 Init();
52 SetOwner(this);
53 }
ed791986
VZ
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
c2ca375c 58 wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
9249d38d 59 {
c2ca375c
VZ
60 Init();
61 SetOwner(owner, timerid);
9249d38d
VZ
62 }
63
c2ca375c
VZ
64 // same as ctor above
65 void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
66
67 virtual ~wxTimer();
0470b1e6 68
03e11df5 69
0470b1e6
VZ
70 // working with the timer
71 // ----------------------
72
c2ca375c
VZ
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
0470b1e6
VZ
76 // start the timer: if milliseconds == -1, use the same value as for the
77 // last Start()
99646f7e
VZ
78 //
79 // it is now valid to call Start() multiple times: this just restarts the
80 // timer if it is already running
cb719f2e 81 virtual bool Start(int milliseconds = -1, bool oneShot = false);
0470b1e6 82
7c602225
VZ
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
c2ca375c
VZ
87 // stop the timer, does nothing if the timer is not running
88 virtual void Stop();
0470b1e6 89
ed791986
VZ
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();
0470b1e6 93
c2ca375c
VZ
94
95 // accessors
96 // ---------
97
98 // get the object notified about the timer events
99 wxEvtHandler *GetOwner() const;
0470b1e6 100
cb719f2e 101 // return true if the timer is running
c2ca375c 102 bool IsRunning() const;
0470b1e6 103
4c286ef6 104 // return the timer ID
c2ca375c 105 int GetId() const;
4c286ef6
DS
106
107 // get the (last) timer interval in milliseconds
c2ca375c 108 int GetInterval() const;
0470b1e6 109
cb719f2e 110 // return true if the timer is one shot
c2ca375c 111 bool IsOneShot() const;
0470b1e6 112
0470b1e6 113protected:
ed791986 114 // common part of all ctors
c2ca375c 115 void Init();
ed791986 116
c2ca375c 117 wxTimerImpl *m_impl;
22f3361e 118
c0c133e1 119 wxDECLARE_NO_COPY_CLASS(wxTimer);
0470b1e6
VZ
120};
121
52a07708
VZ
122// ----------------------------------------------------------------------------
123// wxTimerRunner: starts the timer in its ctor, stops in the dtor
124// ----------------------------------------------------------------------------
125
163b3ad7 126class WXDLLIMPEXP_BASE wxTimerRunner
52a07708
VZ
127{
128public:
129 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
cb719f2e 130 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
52a07708
VZ
131 : m_timer(timer)
132 {
133 m_timer.Start(milli, oneShot);
134 }
135
cb719f2e 136 void Start(int milli, bool oneShot = false)
52a07708
VZ
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
149private:
150 wxTimer& m_timer;
fc7a2a60 151
c0c133e1 152 wxDECLARE_NO_COPY_CLASS(wxTimerRunner);
52a07708
VZ
153};
154
0470b1e6 155// ----------------------------------------------------------------------------
ed791986
VZ
156// wxTimerEvent
157// ----------------------------------------------------------------------------
158
92c6baaf 159class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
ed791986
VZ
160{
161public:
e47859da
FM
162 wxTimerEvent()
163 : wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; }
164
2cd78b2a
VZ
165 wxTimerEvent(wxTimer& timer)
166 : wxEvent(timer.GetId(), wxEVT_TIMER),
e47859da 167 m_timer(&timer)
ed791986 168 {
2cd78b2a 169 SetEventObject(timer.GetOwner());
ed791986
VZ
170 }
171
172 // accessors
e47859da
FM
173 int GetInterval() const { return m_timer->GetInterval(); }
174 wxTimer& GetTimer() const { return *m_timer; }
ed791986 175
acd15a3f
VZ
176 // implement the base class pure virtual
177 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
d48b06bd 178 virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_TIMER; }
acd15a3f 179
ed791986 180private:
e47859da 181 wxTimer* m_timer;
ed791986 182
e47859da 183 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
ed791986
VZ
184};
185
186typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
187
7fa03f04 188#define wxTimerEventHandler(func) \
3c778901 189 wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
7fa03f04 190
c5c5dad5 191#define EVT_TIMER(timerid, func) \
7fa03f04 192 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
ed791986 193
c2ca375c 194#endif // wxUSE_TIMER
52a07708 195
c2ca375c 196#endif // _WX_TIMER_H_BASE_