home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch3A / Timer.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.4 KB  |  99 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////
  22. // Timer.C: A clock class for the stopwatch
  23. ///////////////////////////////////////////////////
  24. #include "Timer.h"
  25. #include "Face.h"
  26. #include <Xm/Xm.h>
  27.  
  28. Timer::Timer ( XtAppContext app, int interval )
  29. {
  30.       _id       = NULL;
  31.       _app      = app;
  32.       _counter  = 0;
  33.       _interval = interval;
  34. }
  35.  
  36.  
  37. void Timer::tick()
  38. {
  39.         _counter++;  // Increment a counter for each tick
  40.     
  41.         // Call derived class function to report time
  42.     
  43.         reportTime ( elapsedTime() );
  44.            
  45.         // Reinstall the timeout callback
  46.     
  47.         _id = XtAppAddTimeOut ( _app, 
  48.                                 _interval, 
  49.                                 &Timer::tickCallback, 
  50.                                 (XtPointer) this );
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. void Timer::start()
  58. {
  59.     // Reset the elapsed time
  60.     
  61.     _counter = 0;
  62.     
  63.     if ( _id ) // If a previous callback is still in effect, remove it
  64.     {
  65.     XtRemoveTimeOut ( _id );
  66.     _id = NULL;
  67.     }
  68.     
  69.     // Register a function to be called in _interval milliseconds
  70.     
  71.     _id = XtAppAddTimeOut ( _app, 
  72.                _interval, 
  73.                &Timer::tickCallback, 
  74.                (XtPointer) this );
  75. }
  76. void Timer::stop()
  77. {
  78.     // Remove the current timeout function, if any
  79.     
  80.     if ( _id )
  81.     XtRemoveTimeOut ( _id );
  82.     
  83.     _id = NULL;
  84. }
  85. void Timer::tickCallback ( XtPointer clientData, XtIntervalId * )
  86. {
  87.     // Get the object pointer and call the corresponding tick function
  88.     
  89.     Timer *obj = (Timer *) clientData;
  90.     
  91.     obj->tick();
  92. }
  93.  
  94. float Timer::elapsedTime()
  95. {
  96.     return ( (float) _counter * _interval / 1000.0 );
  97. }
  98.  
  99.