| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: timer.cpp |
| 3 | // Purpose: wxTimer implementation |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "timer.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/setup.h" |
| 25 | #include "wx/window.h" |
| 26 | #include "wx/list.h" |
| 27 | #include "wx/event.h" |
| 28 | #include "wx/app.h" |
| 29 | #include "wx/intl.h" |
| 30 | #include "wx/log.h" |
| 31 | #endif |
| 32 | |
| 33 | #include "wx/timer.h" |
| 34 | |
| 35 | #include "wx/msw/private.h" |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // private functions |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | wxList wxTimerList(wxKEY_INTEGER); |
| 42 | UINT WINAPI _EXPORT wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD); |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | // macros |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | #ifdef __WIN32__ |
| 49 | #define _EXPORT |
| 50 | #else |
| 51 | #define _EXPORT _export |
| 52 | #endif |
| 53 | |
| 54 | #if !USE_SHARED_LIBRARY |
| 55 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject) |
| 56 | #endif |
| 57 | |
| 58 | // ============================================================================ |
| 59 | // implementation |
| 60 | // ============================================================================ |
| 61 | |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | // wxTimer class |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | wxTimer::wxTimer() |
| 67 | { |
| 68 | m_id = 0; |
| 69 | } |
| 70 | |
| 71 | wxTimer::~wxTimer() |
| 72 | { |
| 73 | wxTimer::Stop(); |
| 74 | |
| 75 | wxTimerList.DeleteObject(this); |
| 76 | } |
| 77 | |
| 78 | bool wxTimer::Start(int milliseconds, bool oneShot) |
| 79 | { |
| 80 | (void)wxTimerBase::Start(milliseconds, oneShot); |
| 81 | |
| 82 | wxCHECK_MSG( m_milli > 0, FALSE, wxT("invalid value for timer timeour") ); |
| 83 | |
| 84 | wxTimerList.DeleteObject(this); |
| 85 | TIMERPROC wxTimerProcInst = (TIMERPROC) |
| 86 | MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance()); |
| 87 | |
| 88 | m_id = SetTimer(NULL, (UINT)(m_id ? m_id : 1), |
| 89 | (UINT)milliseconds, wxTimerProcInst); |
| 90 | if ( m_id > 0 ) |
| 91 | { |
| 92 | wxTimerList.Append(m_id, this); |
| 93 | |
| 94 | return TRUE; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | wxLogSysError(_("Couldn't create a timer")); |
| 99 | |
| 100 | return FALSE; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void wxTimer::Stop() |
| 105 | { |
| 106 | if ( m_id ) |
| 107 | { |
| 108 | KillTimer(NULL, (UINT)m_id); |
| 109 | wxTimerList.DeleteObject(this); |
| 110 | } |
| 111 | |
| 112 | m_id = 0; |
| 113 | } |
| 114 | |
| 115 | // ---------------------------------------------------------------------------- |
| 116 | // private functions |
| 117 | // ---------------------------------------------------------------------------- |
| 118 | |
| 119 | void wxProcessTimer(wxTimer& timer) |
| 120 | { |
| 121 | // Avoid to process spurious timer events |
| 122 | if ( timer.m_id == 0) |
| 123 | return; |
| 124 | |
| 125 | if ( timer.IsOneShot() ) |
| 126 | timer.Stop(); |
| 127 | |
| 128 | timer.Notify(); |
| 129 | } |
| 130 | |
| 131 | UINT WINAPI _EXPORT wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD) |
| 132 | { |
| 133 | wxNode *node = wxTimerList.Find((long)idTimer); |
| 134 | |
| 135 | wxCHECK_MSG( node, 0, wxT("bogus timer id in wxTimerProc") ); |
| 136 | |
| 137 | wxProcessTimer(*(wxTimer *)node->Data()); |
| 138 | |
| 139 | return 0; |
| 140 | } |