home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / toolba.zip / ATIMEHDR.CPP < prev    next >
Text File  |  1994-09-08  |  3KB  |  116 lines

  1. /********************************************
  2. * ATimeHdr.cpp - timer class implementation *
  3. ********************************************/
  4.  
  5. #include <ithread.hpp>
  6. #include <iwindow.hpp>
  7.  
  8. #define INCL_WINTIMER
  9. #define INCL_WINSYS
  10. #include <os2.h>
  11.  
  12. #include "atimehdr.hpp"
  13.  
  14. ATimeHandler::ATimeHandler()
  15. {
  16.    timerRunning = false;
  17.    timerDelay = 0;
  18.    timerId = 0;
  19.    generateId = true;
  20. }  /* end ATimeHandler::ATimeHandler() */
  21.  
  22.  
  23. /* -------------------------------------
  24.    override event handler setup routines 
  25.    in order to start and stop timers
  26.    ------------------------------------- */
  27.  
  28. IHandler & ATimeHandler::handleEventsFor( IWindow *window )
  29. {
  30.    if ( window != NULL ) {
  31.       hwndWindow = window->handle();
  32.       if ( timerDelay != 0 ) {
  33.          if ( generateId ) {
  34.             timerId = WinQuerySysValue( HWND_DESKTOP, SV_CTIMERS );
  35.          } /* endif */
  36.          WinStartTimer( (HAB)IThread::current().anchorBlock(),
  37.             hwndWindow, timerId, timerDelay );
  38.       } /* endif */
  39.       timerRunning = true;
  40.    } /* endif */
  41.    return IHandler::handleEventsFor( window );
  42. }  /* end ATimeHandler::handleEventsFor( ... ) */
  43.  
  44. IHandler & ATimeHandler::stopHandlingEventsFor( IWindow *window )
  45. {
  46.    if ( window ) {
  47.       if ( window->isValid() && window->handle() == hwndWindow ) {
  48.          WinStopTimer( (HAB)IThread::current().anchorBlock(),
  49.             (HWND)window->handle(), timerId );
  50.       timerRunning = false;
  51.       } /* endif */
  52.    } /* endif */
  53.    return IHandler::stopHandlingEventsFor( window );
  54. }  /* end ATimeHandler::stopHandlingEventsFor( ... ) */
  55.  
  56.  
  57. /* ----------------------------------------
  58.    allow timer identifier and timeout value
  59.    to be accessed externally
  60.    ---------------------------------------- */
  61.  
  62. void ATimeHandler::setId( unsigned int id )
  63. {
  64.    if ( !timerRunning && id < TID_USERMAX && id > 0 ) {
  65.       timerId = id;
  66.       generateId = false;
  67.    } /* endif */
  68. }  /* end ATimeHandler::setId( ... ) */
  69.  
  70. unsigned int ATimeHandler::id()
  71. {
  72.    return timerId;
  73. }  /* end ATimeHandler::id() */
  74.  
  75. void ATimeHandler::setTimeout( unsigned int timeout )
  76. {
  77.    if ( timeout == 0 && timerDelay != 0 ) {
  78.       WinStopTimer( (HAB)IThread::current().anchorBlock(),
  79.          hwndWindow, timerId );
  80.    } else {
  81.       if ( timerRunning ) {
  82.          if ( generateId ) {
  83.             timerId = WinQuerySysValue( HWND_DESKTOP, SV_CTIMERS );
  84.          } /* endif */
  85.          WinStartTimer( (HAB)IThread::current().anchorBlock(),
  86.             hwndWindow, timerId, timeout );
  87.       } /* endif */
  88.    } /* endif */
  89.    timerDelay = timeout;
  90. }  /* end ATimeHandler::setTimeout( ... ) */
  91.  
  92. unsigned int ATimeHandler::timeout()
  93. {
  94.    return timerDelay;
  95. }  /* end ATimeHandler::timeout() */
  96.  
  97.  
  98. /* ---------------------
  99.    handle WM_TIMER event
  100.    --------------------- */
  101.  
  102. Boolean ATimeHandler::tick( IEvent &evt )
  103. {
  104.    return false;
  105. }  /* end ATimeHandler::tick( ... ) */
  106.  
  107. Boolean ATimeHandler::dispatchHandlerEvent( IEvent &evt )
  108. {
  109.    if ( (evt.eventId() == WM_TIMER) && (evt.parameter1() == timerId) ) {
  110.       timerRunning = true;
  111.       return tick( evt );
  112.    } /* endif */
  113.    return false;
  114. }  /* end ATimeHandler::dispatchHandlerEvent( ... ) */
  115.  
  116.