]> git.saurik.com Git - wxWidgets.git/blame - include/wx/timer.h
Added event handler macro
[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
c2ca375c 18#if wxUSE_TIMER
2ecf902b 19
0470b1e6 20#include "wx/object.h"
d895ad7c 21#include "wx/longlong.h"
ed791986 22#include "wx/event.h"
5fb98c22 23#include "wx/stopwatch.h" // for backwards compatibility
c2ca375c 24#include "wx/utils.h"
5fb98c22 25
0470b1e6 26
b0a2d8a8
VZ
27// more readable flags for Start():
28
29// generate notifications periodically until the timer is stopped (default)
cb719f2e 30#define wxTIMER_CONTINUOUS false
b0a2d8a8
VZ
31
32// only send the notification once and then stop the timer
cb719f2e 33#define wxTIMER_ONE_SHOT true
b0a2d8a8 34
b5dbe15d 35class WXDLLIMPEXP_FWD_BASE wxTimerImpl;
c2ca375c 36
0470b1e6 37// the interface of wxTimer class
c2ca375c 38class WXDLLIMPEXP_BASE wxTimer : 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
c2ca375c
VZ
46 wxTimer()
47 {
48 Init();
49 SetOwner(this);
50 }
ed791986
VZ
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
c2ca375c 55 wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
9249d38d 56 {
c2ca375c
VZ
57 Init();
58 SetOwner(owner, timerid);
9249d38d
VZ
59 }
60
c2ca375c
VZ
61 // same as ctor above
62 void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
63
64 virtual ~wxTimer();
0470b1e6 65
03e11df5 66
0470b1e6
VZ
67 // working with the timer
68 // ----------------------
69
c2ca375c
VZ
70 // NB: Start() and Stop() are not supposed to be overridden, they are only
71 // virtual for historical reasons, only Notify() can be overridden
72
0470b1e6
VZ
73 // start the timer: if milliseconds == -1, use the same value as for the
74 // last Start()
99646f7e
VZ
75 //
76 // it is now valid to call Start() multiple times: this just restarts the
77 // timer if it is already running
cb719f2e 78 virtual bool Start(int milliseconds = -1, bool oneShot = false);
0470b1e6 79
c2ca375c
VZ
80 // stop the timer, does nothing if the timer is not running
81 virtual void Stop();
0470b1e6 82
ed791986
VZ
83 // override this in your wxTimer-derived class if you want to process timer
84 // messages in it, use non default ctor or SetOwner() otherwise
85 virtual void Notify();
0470b1e6 86
c2ca375c
VZ
87
88 // accessors
89 // ---------
90
91 // get the object notified about the timer events
92 wxEvtHandler *GetOwner() const;
0470b1e6 93
cb719f2e 94 // return true if the timer is running
c2ca375c 95 bool IsRunning() const;
0470b1e6 96
4c286ef6 97 // return the timer ID
c2ca375c 98 int GetId() const;
4c286ef6
DS
99
100 // get the (last) timer interval in milliseconds
c2ca375c 101 int GetInterval() const;
0470b1e6 102
cb719f2e 103 // return true if the timer is one shot
c2ca375c 104 bool IsOneShot() const;
0470b1e6 105
0470b1e6 106protected:
ed791986 107 // common part of all ctors
c2ca375c 108 void Init();
ed791986 109
c2ca375c 110 wxTimerImpl *m_impl;
22f3361e 111
c2ca375c 112 DECLARE_NO_COPY_CLASS(wxTimer)
0470b1e6
VZ
113};
114
52a07708
VZ
115// ----------------------------------------------------------------------------
116// wxTimerRunner: starts the timer in its ctor, stops in the dtor
117// ----------------------------------------------------------------------------
118
163b3ad7 119class WXDLLIMPEXP_BASE wxTimerRunner
52a07708
VZ
120{
121public:
122 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
cb719f2e 123 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
52a07708
VZ
124 : m_timer(timer)
125 {
126 m_timer.Start(milli, oneShot);
127 }
128
cb719f2e 129 void Start(int milli, bool oneShot = false)
52a07708
VZ
130 {
131 m_timer.Start(milli, oneShot);
132 }
133
134 ~wxTimerRunner()
135 {
136 if ( m_timer.IsRunning() )
137 {
138 m_timer.Stop();
139 }
140 }
141
142private:
143 wxTimer& m_timer;
fc7a2a60
VZ
144
145 DECLARE_NO_COPY_CLASS(wxTimerRunner)
52a07708
VZ
146};
147
0470b1e6 148// ----------------------------------------------------------------------------
ed791986
VZ
149// wxTimerEvent
150// ----------------------------------------------------------------------------
151
92c6baaf 152class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
ed791986
VZ
153{
154public:
2cd78b2a
VZ
155 wxTimerEvent(wxTimer& timer)
156 : wxEvent(timer.GetId(), wxEVT_TIMER),
157 m_timer(timer)
ed791986 158 {
2cd78b2a 159 SetEventObject(timer.GetOwner());
ed791986
VZ
160 }
161
162 // accessors
2cd78b2a
VZ
163 int GetInterval() const { return m_timer.GetInterval(); }
164 wxTimer& GetTimer() const { return m_timer; }
ed791986 165
acd15a3f
VZ
166 // implement the base class pure virtual
167 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
168
ed791986 169private:
2cd78b2a 170 wxTimer& m_timer;
ed791986 171
2cd78b2a
VZ
172 DECLARE_ABSTRACT_CLASS(wxTimerEvent)
173 DECLARE_NO_ASSIGN_CLASS(wxTimerEvent)
ed791986
VZ
174};
175
176typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
177
7fa03f04 178#define wxTimerEventHandler(func) \
8bc3ec1f 179 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
7fa03f04 180
c5c5dad5 181#define EVT_TIMER(timerid, func) \
7fa03f04 182 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
ed791986 183
c2ca375c 184#endif // wxUSE_TIMER
52a07708 185
c2ca375c 186#endif // _WX_TIMER_H_BASE_