/src/wxwidgets/include/wx/timer.h
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /////////////////////////////////////////////////////////////////////////////  | 
2  |  | // Name:        wx/timer.h  | 
3  |  | // Purpose:     wxTimer, wxStopWatch and global time-related functions  | 
4  |  | // Author:      Julian Smart  | 
5  |  | // Modified by: Vadim Zeitlin (wxTimerBase)  | 
6  |  | //              Guillermo Rodriguez (global clean up)  | 
7  |  | // Created:     04/01/98  | 
8  |  | // Copyright:   (c) Julian Smart  | 
9  |  | // Licence:     wxWindows licence  | 
10  |  | /////////////////////////////////////////////////////////////////////////////  | 
11  |  |  | 
12  |  | #ifndef _WX_TIMER_H_BASE_  | 
13  |  | #define _WX_TIMER_H_BASE_  | 
14  |  |  | 
15  |  | #include "wx/defs.h"  | 
16  |  |  | 
17  |  | #if wxUSE_TIMER  | 
18  |  |  | 
19  |  | #include "wx/object.h"  | 
20  |  | #include "wx/longlong.h"  | 
21  |  | #include "wx/event.h"  | 
22  |  | #include "wx/stopwatch.h" // for backwards compatibility  | 
23  |  | #include "wx/utils.h"  | 
24  |  |  | 
25  |  |  | 
26  |  | // more readable flags for Start():  | 
27  |  |  | 
28  |  | // generate notifications periodically until the timer is stopped (default)  | 
29  |  | #define wxTIMER_CONTINUOUS false  | 
30  |  |  | 
31  |  | // only send the notification once and then stop the timer  | 
32  |  | #define wxTIMER_ONE_SHOT true  | 
33  |  |  | 
34  |  | class WXDLLIMPEXP_FWD_BASE wxTimerImpl;  | 
35  |  | class WXDLLIMPEXP_FWD_BASE wxTimerEvent;  | 
36  |  |  | 
37  |  | // timer event type  | 
38  |  | wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);  | 
39  |  |  | 
40  |  | // the interface of wxTimer class  | 
41  |  | class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler  | 
42  |  | { | 
43  |  | public:  | 
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  | 
49  |  |     wxTimer()  | 
50  | 0  |     { | 
51  | 0  |         Init();  | 
52  | 0  |         SetOwner(this);  | 
53  | 0  |     }  | 
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  | 
58  |  |     wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)  | 
59  | 0  |     { | 
60  | 0  |         Init();  | 
61  | 0  |         SetOwner(owner, timerid);  | 
62  | 0  |     }  | 
63  |  |  | 
64  |  |     // same as ctor above  | 
65  |  |     void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);  | 
66  |  |  | 
67  |  |     virtual ~wxTimer();  | 
68  |  |  | 
69  |  |  | 
70  |  |     // working with the timer  | 
71  |  |     // ----------------------  | 
72  |  |  | 
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  |  |  | 
76  |  |     // start the timer: if milliseconds == -1, use the same value as for the  | 
77  |  |     // last Start()  | 
78  |  |     //  | 
79  |  |     // it is now valid to call Start() multiple times: this just restarts the  | 
80  |  |     // timer if it is already running  | 
81  |  |     virtual bool Start(int milliseconds = -1, bool oneShot = false);  | 
82  |  |  | 
83  |  |     // start the timer for one iteration only, this is just a simple wrapper  | 
84  |  |     // for Start()  | 
85  | 0  |     bool StartOnce(int milliseconds = -1) { return Start(milliseconds, true); } | 
86  |  |  | 
87  |  |     // stop the timer, does nothing if the timer is not running  | 
88  |  |     virtual void Stop();  | 
89  |  |  | 
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();  | 
93  |  |  | 
94  |  |  | 
95  |  |     // accessors  | 
96  |  |     // ---------  | 
97  |  |  | 
98  |  |     // get the object notified about the timer events  | 
99  |  |     wxEvtHandler *GetOwner() const;  | 
100  |  |  | 
101  |  |     // return true if the timer is running  | 
102  |  |     bool IsRunning() const;  | 
103  |  |  | 
104  |  |     // return the timer ID  | 
105  |  |     int GetId() const;  | 
106  |  |  | 
107  |  |     // get the (last) timer interval in milliseconds  | 
108  |  |     int GetInterval() const;  | 
109  |  |  | 
110  |  |     // return true if the timer is one shot  | 
111  |  |     bool IsOneShot() const;  | 
112  |  |  | 
113  |  | protected:  | 
114  |  |     // common part of all ctors  | 
115  |  |     void Init();  | 
116  |  |  | 
117  |  |     wxTimerImpl *m_impl;  | 
118  |  |  | 
119  |  |     wxDECLARE_NO_COPY_CLASS(wxTimer);  | 
120  |  | };  | 
121  |  |  | 
122  |  | // ----------------------------------------------------------------------------  | 
123  |  | // wxTimerRunner: starts the timer in its ctor, stops in the dtor  | 
124  |  | // ----------------------------------------------------------------------------  | 
125  |  |  | 
126  |  | class WXDLLIMPEXP_BASE wxTimerRunner  | 
127  |  | { | 
128  |  | public:  | 
129  | 0  |     wxTimerRunner(wxTimer& timer) : m_timer(timer) { } | 
130  |  |     wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)  | 
131  |  |         : m_timer(timer)  | 
132  | 0  |     { | 
133  | 0  |         m_timer.Start(milli, oneShot);  | 
134  | 0  |     }  | 
135  |  |  | 
136  |  |     void Start(int milli, bool oneShot = false)  | 
137  | 0  |     { | 
138  | 0  |         m_timer.Start(milli, oneShot);  | 
139  | 0  |     }  | 
140  |  |  | 
141  |  |     ~wxTimerRunner()  | 
142  | 0  |     { | 
143  | 0  |         if ( m_timer.IsRunning() )  | 
144  | 0  |         { | 
145  | 0  |             m_timer.Stop();  | 
146  | 0  |         }  | 
147  | 0  |     }  | 
148  |  |  | 
149  |  | private:  | 
150  |  |     wxTimer& m_timer;  | 
151  |  |  | 
152  |  |     wxDECLARE_NO_COPY_CLASS(wxTimerRunner);  | 
153  |  | };  | 
154  |  |  | 
155  |  | // ----------------------------------------------------------------------------  | 
156  |  | // wxTimerEvent  | 
157  |  | // ----------------------------------------------------------------------------  | 
158  |  |  | 
159  |  | class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent  | 
160  |  | { | 
161  |  | public:  | 
162  |  |     wxTimerEvent(wxTimer& timer)  | 
163  | 0  |         : wxEvent(timer.GetId(), wxEVT_TIMER),  | 
164  | 0  |           m_timer(&timer)  | 
165  | 0  |     { | 
166  | 0  |         SetEventObject(timer.GetOwner());  | 
167  | 0  |     }  | 
168  |  |  | 
169  |  |     // accessors  | 
170  | 0  |     int GetInterval() const { return m_timer->GetInterval(); } | 
171  | 0  |     wxTimer& GetTimer() const { return *m_timer; } | 
172  |  |  | 
173  |  |     // implement the base class pure virtual  | 
174  | 0  |     wxNODISCARD virtual wxEvent *Clone() const override { return new wxTimerEvent(*this); } | 
175  | 0  |     virtual wxEventCategory GetEventCategory() const override { return wxEVT_CATEGORY_TIMER; } | 
176  |  |  | 
177  |  |     // default ctor creates an unusable event object and should not be used (in  | 
178  |  |     // fact, no code outside wxWidgets is supposed to create event objects)  | 
179  |  | #if WXWIN_COMPATIBILITY_3_0  | 
180  |  |     wxDEPRECATED_MSG("wxTimerEvent not supposed to be created by user code") | 
181  |  |     wxTimerEvent()  | 
182  |  |         : wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=nullptr; } | 
183  |  | #endif // WXWIN_COMPATIBILITY_3_0  | 
184  |  |  | 
185  |  | private:  | 
186  |  |     wxTimer* m_timer;  | 
187  |  |  | 
188  |  |     wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN_DEF_COPY(wxTimerEvent);  | 
189  |  | };  | 
190  |  |  | 
191  |  | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);  | 
192  |  |  | 
193  |  | #define wxTimerEventHandler(func) \  | 
194  |  |     wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)  | 
195  |  |  | 
196  |  | #define EVT_TIMER(timerid, func) \  | 
197  |  |     wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))  | 
198  |  |  | 
199  |  | #endif // wxUSE_TIMER  | 
200  |  |  | 
201  |  | #endif // _WX_TIMER_H_BASE_  |