home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / timer.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  3KB  |  147 lines

  1. #if defined(__GNUC__)
  2. #pragma implementation
  3. #endif
  4.  
  5.  
  6.  
  7. #include "base/set.h"
  8.  
  9. #include "ui/timer.h"
  10. #include "ui/applic.h"
  11. #include "ui/cntroler.h"
  12.  
  13. #if defined(__UNIX__)
  14. #include <sys/time.h>
  15. #endif
  16.  
  17. #if defined(__MS_WINDOWS__)
  18. #include <windows.h>
  19. static CL_IntPtrMap    _TimerMap;       // Map of Windows timer indices to
  20.                                         // UI_Timer objects
  21.  
  22. WORD FAR PASCAL TimerFunc (HWND, WORD, int id, DWORD)
  23. {
  24.     UI_Timer* tmr = (UI_Timer*) _TimerMap[id];
  25.     if (tmr)
  26.         tmr->DoAlarm();
  27.     return 0;
  28. }
  29.  
  30. #elif defined(__OS2__)
  31. CL_IntPtrMap UI_Timer::_TimerMap;
  32. #endif
  33.  
  34.  
  35.  
  36.  
  37.  
  38. UI_Timer::UI_Timer (const CL_AbstractBinding& bnd)
  39. {
  40.     _bind = (CL_AbstractBinding*) bnd.Clone();
  41. #if defined(__MS_WINDOWS__)
  42.     _id = 0;
  43. #endif
  44. }
  45.  
  46.  
  47. UI_Timer::~UI_Timer ()
  48. {
  49.     Stop();
  50. #if defined(__OS2__)
  51.     _TimerMap.Remove (_id);
  52. #endif
  53.     delete _bind;
  54. }
  55.  
  56.  
  57.  
  58. bool UI_Timer::Start (long msecs)
  59. {
  60.     _msecs = msecs;
  61. #if defined(__MS_WINDOWS__)
  62.     if (!_TheApplication)
  63.         return FALSE;
  64.     long n = _TimerMap.Size()+1;
  65.     if (SetTimer (_TheApplication->MainWindow()->ViewHandle(),
  66.                   n, msecs, (FARPROC) TimerFunc)) {
  67.         _id = n;
  68.         _TimerMap.Add (n, this);
  69.         _running = TRUE;
  70.         return TRUE;
  71.     }
  72.     return FALSE;
  73. #elif defined(__OS2__)
  74.     _id = WinStartTimer (_TheApplication->Controller().AnchorBlockHandle(),
  75.                          NULLHANDLE, 0, msecs);
  76.     _TimerMap.Add (_id, this);
  77.     _running = TRUE;
  78.     return TRUE;
  79. #elif defined(__UNIX__)
  80.     if (!_TheApplication)
  81.         return FALSE;
  82.     _id = _TheApplication->Controller().RegisterTimeOut
  83.         (msecs, &UI_Timer::DoAlarm, this);
  84.     if (_id) {
  85.         _running = TRUE;
  86.         return TRUE;
  87.     }
  88.     return FALSE;
  89. #endif
  90. }
  91.  
  92.  
  93. bool UI_Timer::Stop ()
  94. {
  95. #if defined(__MS_WINDOWS__)
  96.     if (_id > 0) {
  97.         KillTimer (NULL, _id);
  98.         _TimerMap.Remove (_id);
  99.         _id = 0;
  100.         return TRUE;
  101.     }
  102.     return FALSE;
  103. #elif defined(__OS2__)
  104.     if (_running)
  105.         WinStopTimer (_TheApplication->Controller().AnchorBlockHandle(),
  106.                       NULLHANDLE, _id);
  107.     return TRUE;
  108. #elif defined(__X_MOTIF__)
  109.     if (_TheApplication && _running) {
  110.         _TheApplication->Controller().UnregisterTimeOut (_id);
  111.         _running = FALSE;
  112.     }
  113. #endif
  114. }
  115.  
  116.  
  117.  
  118. #if defined(__MS_WINDOWS__)
  119. void UI_Timer::DoAlarm ()
  120. {
  121.     if (_id >= 1 && _running)
  122.         _bind->Execute (*this, _id);
  123. }
  124. #elif defined(__X_MOTIF__)
  125. void UI_Timer::DoAlarm (void* p, ulong* id)
  126. {
  127.     UI_Timer* tmr = (UI_Timer*) p;
  128.     if (tmr && tmr->_bind && tmr->_running) {
  129.         tmr->_bind->Execute (*tmr, *id);
  130.         tmr->_id = _TheApplication->Controller().RegisterTimeOut
  131.             (tmr->_msecs, &UI_Timer::DoAlarm, tmr);
  132.     }
  133. }
  134.  
  135. #elif defined(__OS2__)
  136. void UI_Timer::DoAlarm (ulong id)
  137. {
  138.     UI_Timer* t = (UI_Timer*) _TimerMap [id];
  139.     if (t)
  140.         t->_bind->Execute (*t, id);
  141. }
  142.  
  143. #endif
  144.  
  145.  
  146.  
  147.