timer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef _WX_TIMER_H_BASE_
  12. #define _WX_TIMER_H_BASE_
  13. #include "wx/defs.h"
  14. #if wxUSE_TIMER
  15. #include "wx/object.h"
  16. #include "wx/longlong.h"
  17. #include "wx/event.h"
  18. #include "wx/stopwatch.h" // for backwards compatibility
  19. #include "wx/utils.h"
  20. // more readable flags for Start():
  21. // generate notifications periodically until the timer is stopped (default)
  22. #define wxTIMER_CONTINUOUS false
  23. // only send the notification once and then stop the timer
  24. #define wxTIMER_ONE_SHOT true
  25. class WXDLLIMPEXP_FWD_BASE wxTimerImpl;
  26. class WXDLLIMPEXP_FWD_BASE wxTimerEvent;
  27. // timer event type
  28. wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);
  29. // the interface of wxTimer class
  30. class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler
  31. {
  32. public:
  33. // ctors and initializers
  34. // ----------------------
  35. // default: if you don't call SetOwner(), your only chance to get timer
  36. // notifications is to override Notify() in the derived class
  37. wxTimer()
  38. {
  39. Init();
  40. SetOwner(this);
  41. }
  42. // ctor which allows to avoid having to override Notify() in the derived
  43. // class: the owner will get timer notifications which can be handled with
  44. // EVT_TIMER
  45. wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
  46. {
  47. Init();
  48. SetOwner(owner, timerid);
  49. }
  50. // same as ctor above
  51. void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
  52. virtual ~wxTimer();
  53. // working with the timer
  54. // ----------------------
  55. // NB: Start() and Stop() are not supposed to be overridden, they are only
  56. // virtual for historical reasons, only Notify() can be overridden
  57. // start the timer: if milliseconds == -1, use the same value as for the
  58. // last Start()
  59. //
  60. // it is now valid to call Start() multiple times: this just restarts the
  61. // timer if it is already running
  62. virtual bool Start(int milliseconds = -1, bool oneShot = false);
  63. // start the timer for one iteration only, this is just a simple wrapper
  64. // for Start()
  65. bool StartOnce(int milliseconds = -1) { return Start(milliseconds, true); }
  66. // stop the timer, does nothing if the timer is not running
  67. virtual void Stop();
  68. // override this in your wxTimer-derived class if you want to process timer
  69. // messages in it, use non default ctor or SetOwner() otherwise
  70. virtual void Notify();
  71. // accessors
  72. // ---------
  73. // get the object notified about the timer events
  74. wxEvtHandler *GetOwner() const;
  75. // return true if the timer is running
  76. bool IsRunning() const;
  77. // return the timer ID
  78. int GetId() const;
  79. // get the (last) timer interval in milliseconds
  80. int GetInterval() const;
  81. // return true if the timer is one shot
  82. bool IsOneShot() const;
  83. protected:
  84. // common part of all ctors
  85. void Init();
  86. wxTimerImpl *m_impl;
  87. wxDECLARE_NO_COPY_CLASS(wxTimer);
  88. };
  89. // ----------------------------------------------------------------------------
  90. // wxTimerRunner: starts the timer in its ctor, stops in the dtor
  91. // ----------------------------------------------------------------------------
  92. class WXDLLIMPEXP_BASE wxTimerRunner
  93. {
  94. public:
  95. wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
  96. wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
  97. : m_timer(timer)
  98. {
  99. m_timer.Start(milli, oneShot);
  100. }
  101. void Start(int milli, bool oneShot = false)
  102. {
  103. m_timer.Start(milli, oneShot);
  104. }
  105. ~wxTimerRunner()
  106. {
  107. if ( m_timer.IsRunning() )
  108. {
  109. m_timer.Stop();
  110. }
  111. }
  112. private:
  113. wxTimer& m_timer;
  114. wxDECLARE_NO_COPY_CLASS(wxTimerRunner);
  115. };
  116. // ----------------------------------------------------------------------------
  117. // wxTimerEvent
  118. // ----------------------------------------------------------------------------
  119. class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
  120. {
  121. public:
  122. wxTimerEvent()
  123. : wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; }
  124. wxTimerEvent(wxTimer& timer)
  125. : wxEvent(timer.GetId(), wxEVT_TIMER),
  126. m_timer(&timer)
  127. {
  128. SetEventObject(timer.GetOwner());
  129. }
  130. // accessors
  131. int GetInterval() const { return m_timer->GetInterval(); }
  132. wxTimer& GetTimer() const { return *m_timer; }
  133. // implement the base class pure virtual
  134. virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
  135. virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_TIMER; }
  136. private:
  137. wxTimer* m_timer;
  138. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
  139. };
  140. typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
  141. #define wxTimerEventHandler(func) \
  142. wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
  143. #define EVT_TIMER(timerid, func) \
  144. wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
  145. #endif // wxUSE_TIMER
  146. #endif // _WX_TIMER_H_BASE_