]> git.saurik.com Git - wxWidgets.git/blame - include/wx/timer.h
Refactor listbox event sending code to avoid duplication.
[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;
3c778901
VZ
36class WXDLLIMPEXP_FWD_BASE wxTimerEvent;
37
38// timer event type
9b11752c 39wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);
c2ca375c 40
0470b1e6 41// the interface of wxTimer class
c2ca375c 42class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler
0470b1e6
VZ
43{
44public:
ed791986
VZ
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
c2ca375c
VZ
50 wxTimer()
51 {
52 Init();
53 SetOwner(this);
54 }
ed791986
VZ
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
c2ca375c 59 wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
9249d38d 60 {
c2ca375c
VZ
61 Init();
62 SetOwner(owner, timerid);
9249d38d
VZ
63 }
64
c2ca375c
VZ
65 // same as ctor above
66 void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
67
68 virtual ~wxTimer();
0470b1e6 69
03e11df5 70
0470b1e6
VZ
71 // working with the timer
72 // ----------------------
73
c2ca375c
VZ
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
0470b1e6
VZ
77 // start the timer: if milliseconds == -1, use the same value as for the
78 // last Start()
99646f7e
VZ
79 //
80 // it is now valid to call Start() multiple times: this just restarts the
81 // timer if it is already running
cb719f2e 82 virtual bool Start(int milliseconds = -1, bool oneShot = false);
0470b1e6 83
c2ca375c
VZ
84 // stop the timer, does nothing if the timer is not running
85 virtual void Stop();
0470b1e6 86
ed791986
VZ
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();
0470b1e6 90
c2ca375c
VZ
91
92 // accessors
93 // ---------
94
95 // get the object notified about the timer events
96 wxEvtHandler *GetOwner() const;
0470b1e6 97
cb719f2e 98 // return true if the timer is running
c2ca375c 99 bool IsRunning() const;
0470b1e6 100
4c286ef6 101 // return the timer ID
c2ca375c 102 int GetId() const;
4c286ef6
DS
103
104 // get the (last) timer interval in milliseconds
c2ca375c 105 int GetInterval() const;
0470b1e6 106
cb719f2e 107 // return true if the timer is one shot
c2ca375c 108 bool IsOneShot() const;
0470b1e6 109
0470b1e6 110protected:
ed791986 111 // common part of all ctors
c2ca375c 112 void Init();
ed791986 113
c2ca375c 114 wxTimerImpl *m_impl;
22f3361e 115
c0c133e1 116 wxDECLARE_NO_COPY_CLASS(wxTimer);
0470b1e6
VZ
117};
118
52a07708
VZ
119// ----------------------------------------------------------------------------
120// wxTimerRunner: starts the timer in its ctor, stops in the dtor
121// ----------------------------------------------------------------------------
122
163b3ad7 123class WXDLLIMPEXP_BASE wxTimerRunner
52a07708
VZ
124{
125public:
126 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
cb719f2e 127 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
52a07708
VZ
128 : m_timer(timer)
129 {
130 m_timer.Start(milli, oneShot);
131 }
132
cb719f2e 133 void Start(int milli, bool oneShot = false)
52a07708
VZ
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
146private:
147 wxTimer& m_timer;
fc7a2a60 148
c0c133e1 149 wxDECLARE_NO_COPY_CLASS(wxTimerRunner);
52a07708
VZ
150};
151
0470b1e6 152// ----------------------------------------------------------------------------
ed791986
VZ
153// wxTimerEvent
154// ----------------------------------------------------------------------------
155
92c6baaf 156class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
ed791986
VZ
157{
158public:
e47859da
FM
159 wxTimerEvent()
160 : wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; }
161
2cd78b2a
VZ
162 wxTimerEvent(wxTimer& timer)
163 : wxEvent(timer.GetId(), wxEVT_TIMER),
e47859da 164 m_timer(&timer)
ed791986 165 {
2cd78b2a 166 SetEventObject(timer.GetOwner());
ed791986
VZ
167 }
168
169 // accessors
e47859da
FM
170 int GetInterval() const { return m_timer->GetInterval(); }
171 wxTimer& GetTimer() const { return *m_timer; }
ed791986 172
acd15a3f
VZ
173 // implement the base class pure virtual
174 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
d48b06bd 175 virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_TIMER; }
acd15a3f 176
ed791986 177private:
e47859da 178 wxTimer* m_timer;
ed791986 179
e47859da 180 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
ed791986
VZ
181};
182
183typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
184
7fa03f04 185#define wxTimerEventHandler(func) \
3c778901 186 wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
7fa03f04 187
c5c5dad5 188#define EVT_TIMER(timerid, func) \
7fa03f04 189 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
ed791986 190
c2ca375c 191#endif // wxUSE_TIMER
52a07708 192
c2ca375c 193#endif // _WX_TIMER_H_BASE_