timer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: timer.h
  3. // Purpose: interface of wxTimer
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. // generate notifications periodically until the timer is stopped (default)
  8. #define wxTIMER_CONTINUOUS false
  9. // only send the notification once and then stop the timer
  10. #define wxTIMER_ONE_SHOT true
  11. wxEventType wxEVT_TIMER;
  12. /**
  13. @class wxTimer
  14. The wxTimer class allows you to execute code at specified intervals.
  15. Its precision is platform-dependent, but in general will not be better than
  16. @c 1ms nor worse than @c 1s.
  17. There are three different ways to use this class:
  18. - You may derive a new class from wxTimer and override the
  19. wxTimer::Notify member to perform the required action.
  20. - You may redirect the notifications to any wxEvtHandler derived object by
  21. using the non-default constructor or wxTimer::SetOwner.
  22. Then use the @c EVT_TIMER macro to connect it to the event handler which
  23. will receive wxTimerEvent notifications.
  24. - You may use a derived class and the @c EVT_TIMER macro to connect it to
  25. an event handler defined in the derived class. If the default constructor
  26. is used, the timer object will be its own owner object, since it is
  27. derived from wxEvtHandler.
  28. In any case, you must start the timer with wxTimer::Start() after constructing
  29. it before it actually starts sending notifications.
  30. It can be stopped later with wxTimer::Stop().
  31. @note A timer can only be used from the main thread.
  32. @library{wxbase}
  33. @category{misc}
  34. @see wxStopWatch
  35. */
  36. class wxTimer : public wxEvtHandler
  37. {
  38. public:
  39. /**
  40. Default constructor.
  41. If you use it to construct the object and don't call SetOwner() later,
  42. you must override Notify() method to process the notifications.
  43. */
  44. wxTimer();
  45. /**
  46. Creates a timer and associates it with @a owner.
  47. Please see SetOwner() for the description of parameters.
  48. */
  49. wxTimer(wxEvtHandler* owner, int id = -1);
  50. /**
  51. Destructor. Stops the timer if it is running.
  52. */
  53. virtual ~wxTimer();
  54. /**
  55. Returns the ID of the events generated by this timer.
  56. */
  57. int GetId() const;
  58. /**
  59. Returns the current interval for the timer (in milliseconds).
  60. */
  61. int GetInterval() const;
  62. /**
  63. Returns the current @e owner of the timer.
  64. If non-@NULL this is the event handler which will receive the
  65. timer events (see wxTimerEvent) when the timer is running.
  66. */
  67. wxEvtHandler* GetOwner() const;
  68. /**
  69. Returns @true if the timer is one shot, i.e.\ if it will stop after firing
  70. the first notification automatically.
  71. */
  72. bool IsOneShot() const;
  73. /**
  74. Returns @true if the timer is running, @false if it is stopped.
  75. */
  76. bool IsRunning() const;
  77. /**
  78. This member should be overridden by the user if the default constructor was
  79. used and SetOwner() wasn't called.
  80. Perform whatever action which is to be taken periodically here.
  81. Notice that throwing exceptions from this method is currently not
  82. supported, use event-based timer handling approach if an exception can
  83. be thrown while handling timer notifications.
  84. */
  85. virtual void Notify();
  86. /**
  87. Associates the timer with the given @a owner object.
  88. When the timer is running, the owner will receive timer events (see wxTimerEvent)
  89. with @a id equal to @a id specified here.
  90. */
  91. void SetOwner(wxEvtHandler* owner, int id = -1);
  92. /**
  93. (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
  94. the previous value is used. Returns @false if the timer could not be started,
  95. @true otherwise (in MS Windows timers are a limited resource).
  96. If @a oneShot is @false (the default), the Notify() function will be called
  97. repeatedly until the timer is stopped.
  98. If @true, it will be called only once and the timer will stop automatically.
  99. To make your code more readable you may also use the following symbolic constants:
  100. - wxTIMER_CONTINUOUS: Start a normal, continuously running, timer
  101. - wxTIMER_ONE_SHOT: Start a one shot timer
  102. Alternatively, use StartOnce().
  103. If the timer was already running, it will be stopped by this method before
  104. restarting it.
  105. */
  106. virtual bool Start(int milliseconds = -1, bool oneShot = wxTIMER_CONTINUOUS);
  107. /**
  108. Starts the timer for a once-only notification.
  109. This is a simple wrapper for Start() with @c wxTIMER_ONE_SHOT parameter.
  110. @since 2.9.5
  111. */
  112. bool StartOnce(int milliseconds = -1);
  113. /**
  114. Stops the timer.
  115. */
  116. virtual void Stop();
  117. };
  118. /**
  119. @class wxTimerRunner
  120. Starts the timer in its ctor, stops in the dtor.
  121. */
  122. class wxTimerRunner
  123. {
  124. public:
  125. wxTimerRunner(wxTimer& timer);
  126. wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false);
  127. void Start(int milli, bool oneShot = false);
  128. ~wxTimerRunner();
  129. };
  130. /**
  131. @class wxTimerEvent
  132. wxTimerEvent object is passed to the event handler of timer events
  133. (see wxTimer::SetOwner).
  134. For example:
  135. @code
  136. class MyFrame : public wxFrame
  137. {
  138. public:
  139. ...
  140. void OnTimer(wxTimerEvent& event);
  141. private:
  142. wxTimer m_timer;
  143. wxDECLARE_EVENT_TABLE();
  144. };
  145. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  146. EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
  147. wxEND_EVENT_TABLE()
  148. MyFrame::MyFrame()
  149. : m_timer(this, TIMER_ID)
  150. {
  151. m_timer.Start(1000); // 1 second interval
  152. }
  153. void MyFrame::OnTimer(wxTimerEvent& event)
  154. {
  155. // do whatever you want to do every second here
  156. }
  157. @endcode
  158. @library{wxbase}
  159. @category{events}
  160. @see wxTimer
  161. */
  162. class wxTimerEvent : public wxEvent
  163. {
  164. public:
  165. wxTimerEvent();
  166. wxTimerEvent(wxTimer& timer);
  167. /**
  168. Returns the interval of the timer which generated this event.
  169. */
  170. int GetInterval() const;
  171. /**
  172. Returns the timer object which generated this event.
  173. */
  174. wxTimer& GetTimer() const;
  175. };