]> git.saurik.com Git - wxWidgets.git/blame - include/wx/timer.h
Theme fix for Mac
[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 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
0470b1e6 34// the interface of wxTimer class
01ebf752 35class WXDLLEXPORT wxTimerBase : public wxEvtHandler
0470b1e6
VZ
36{
37public:
ed791986
VZ
38 // ctors and initializers
39 // ----------------------
40
41 // default: if you don't call SetOwner(), your only chance to get timer
42 // notifications is to override Notify() in the derived class
4c286ef6
DS
43 wxTimerBase()
44 { Init(); SetOwner(this); }
ed791986
VZ
45
46 // ctor which allows to avoid having to override Notify() in the derived
47 // class: the owner will get timer notifications which can be handled with
48 // EVT_TIMER
cb719f2e 49 wxTimerBase(wxEvtHandler *owner, int timerid = wxID_ANY)
c5c5dad5 50 { Init(); SetOwner(owner, timerid); }
ed791986
VZ
51
52 // same as ctor above
cb719f2e 53 void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY)
2dc3af50 54 { m_owner = owner; m_idTimer = timerid == wxID_ANY ? wxNewId() : timerid; }
4c286ef6 55 wxEvtHandler * GetOwner() const { return m_owner; }
0470b1e6 56
799ea011 57 virtual ~wxTimerBase();
03e11df5 58
0470b1e6
VZ
59 // working with the timer
60 // ----------------------
61
62 // start the timer: if milliseconds == -1, use the same value as for the
63 // last Start()
99646f7e
VZ
64 //
65 // it is now valid to call Start() multiple times: this just restarts the
66 // timer if it is already running
cb719f2e 67 virtual bool Start(int milliseconds = -1, bool oneShot = false);
0470b1e6
VZ
68
69 // stop the timer
70 virtual void Stop() = 0;
71
ed791986
VZ
72 // override this in your wxTimer-derived class if you want to process timer
73 // messages in it, use non default ctor or SetOwner() otherwise
74 virtual void Notify();
0470b1e6
VZ
75
76 // getting info
77 // ------------
78
cb719f2e 79 // return true if the timer is running
0470b1e6
VZ
80 virtual bool IsRunning() const = 0;
81
4c286ef6
DS
82 // return the timer ID
83 int GetId() const { return m_idTimer; }
84
85 // get the (last) timer interval in milliseconds
0470b1e6
VZ
86 int GetInterval() const { return m_milli; }
87
cb719f2e 88 // return true if the timer is one shot
0470b1e6
VZ
89 bool IsOneShot() const { return m_oneShot; }
90
0470b1e6 91protected:
ed791986 92 // common part of all ctors
4c286ef6
DS
93 void Init()
94 { m_owner = NULL; m_idTimer = wxID_ANY; m_milli = 0; m_oneShot = false; }
ed791986
VZ
95
96 wxEvtHandler *m_owner;
97 int m_idTimer;
0470b1e6 98 int m_milli; // the timer interval
cb719f2e 99 bool m_oneShot; // true if one shot
22f3361e
VZ
100
101 DECLARE_NO_COPY_CLASS(wxTimerBase)
0470b1e6
VZ
102};
103
ed791986
VZ
104// ----------------------------------------------------------------------------
105// wxTimer itself
106// ----------------------------------------------------------------------------
107
2049ba38 108#if defined(__WXMSW__)
0470b1e6 109 #include "wx/msw/timer.h"
2049ba38 110#elif defined(__WXMOTIF__)
0470b1e6 111 #include "wx/motif/timer.h"
1be7a35c 112#elif defined(__WXGTK20__)
0470b1e6 113 #include "wx/gtk/timer.h"
1be7a35c
MR
114#elif defined(__WXGTK__)
115 #include "wx/gtk1/timer.h"
b3c86150 116#elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXDFB__)
c331ac2e 117 #include "wx/generic/timer.h"
2fcd7f64
RN
118#elif defined (__WXCOCOA__)
119 #include "wx/cocoa/timer.h"
34138703 120#elif defined(__WXMAC__)
0470b1e6 121 #include "wx/mac/timer.h"
1777b9bb 122#elif defined(__WXPM__)
0470b1e6 123 #include "wx/os2/timer.h"
c801d85f
KB
124#endif
125
52a07708
VZ
126// ----------------------------------------------------------------------------
127// wxTimerRunner: starts the timer in its ctor, stops in the dtor
128// ----------------------------------------------------------------------------
129
130class WXDLLEXPORT wxTimerRunner
131{
132public:
133 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
cb719f2e 134 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
52a07708
VZ
135 : m_timer(timer)
136 {
137 m_timer.Start(milli, oneShot);
138 }
139
cb719f2e 140 void Start(int milli, bool oneShot = false)
52a07708
VZ
141 {
142 m_timer.Start(milli, oneShot);
143 }
144
145 ~wxTimerRunner()
146 {
147 if ( m_timer.IsRunning() )
148 {
149 m_timer.Stop();
150 }
151 }
152
153private:
154 wxTimer& m_timer;
fc7a2a60
VZ
155
156 DECLARE_NO_COPY_CLASS(wxTimerRunner)
52a07708
VZ
157};
158
0470b1e6 159// ----------------------------------------------------------------------------
ed791986
VZ
160// wxTimerEvent
161// ----------------------------------------------------------------------------
162
163class WXDLLEXPORT wxTimerEvent : public wxEvent
164{
165public:
c5c5dad5 166 wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid)
ed791986
VZ
167 {
168 m_eventType = wxEVT_TIMER;
169
170 m_interval = interval;
171 }
172
173 // accessors
174 int GetInterval() const { return m_interval; }
175
acd15a3f
VZ
176 // implement the base class pure virtual
177 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
178
ed791986
VZ
179private:
180 int m_interval;
181
fc7a2a60 182 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
ed791986
VZ
183};
184
185typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
186
7fa03f04 187#define wxTimerEventHandler(func) \
8bc3ec1f 188 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
7fa03f04 189
c5c5dad5 190#define EVT_TIMER(timerid, func) \
7fa03f04 191 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
ed791986 192
1e6feb95 193#endif // wxUSE_GUI && wxUSE_TIMER
52a07708 194
c801d85f 195#endif
34138703 196 // _WX_TIMER_H_BASE_