home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / ownhdr / timehdr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  13.1 KB  |  246 lines

  1. /******************************************************************************
  2. * .FILE:         timehdr.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION:  Your Own Time Handler :  Class Implementation                *
  5. *                                                                             *
  6. * .CLASSES:      ATimeHandler                                                 *
  7. *                                                                             *
  8. * .COPYRIGHT:                                                                 *
  9. *    Licensed Material - Program-Property of IBM                              *
  10. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  11. *                                                                             *
  12. * .DISCLAIMER:                                                                *
  13. *   The following [enclosed] code is sample code created by IBM               *
  14. *   Corporation.  This sample code is not part of any standard IBM product    *
  15. *   and is provided to you solely for the purpose of assisting you in the     *
  16. *   development of your applications.  The code is provided 'AS IS',          *
  17. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  18. *   arising out of your use of the sample code, even if they have been        *
  19. *   advised of the possibility of such damages.                               *
  20. *                                                                             *
  21. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  22. *                                                                             *
  23. ******************************************************************************/
  24. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  25.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  26. #endif                                  //  is defined.
  27. #ifdef IC_PM                            //For OS/2,
  28.   #define TIMER_ID      1               //  Set the timer ID as a constant
  29.   #define INCL_WINTIMER                 //  Include the timer routines
  30.   #include <os2.h>                      //    from the OS/2 libraries
  31.   #include <ithread.hpp>                //  Include IThread class header
  32. #endif
  33.  
  34. #include <iwindow.hpp>                  //Include IWindow class header
  35.  
  36. #ifdef IC_MOTIF                         //For AIX,
  37.   #define WM_TIMER      0xF024          //  Create an event id for a timer
  38.   extern "C" {                          //  Include the X-Window header files
  39.   #include <X11/Intrinsic.h>            //    as C externals
  40.   }
  41.   extern void _System                   //Forward declare for post function
  42.     postATimeHandlerEvent (IWindow *, XtIntervalId *);
  43. #endif
  44.  
  45. #ifdef IC_WIN                           //For Windows,
  46.   #define TIMER_ID      1               //  Set the timer ID as a constant
  47.   #include <windows.h>                  //  From Windows libraries
  48. #endif
  49.  
  50. #include "timehdr.hpp"
  51. #include "ownhdr.h"
  52.  
  53. /**************************************************************************
  54. * ATimeHandler :: handleEventsFor                                         *
  55. *   Begin processing timer events for the window passed in by starting    *
  56. *     the appropriate system timer.                                       *
  57. **************************************************************************/
  58. ATimeHandler
  59.   &ATimeHandler :: handleEventsFor( IWindow *window )
  60. {
  61.  
  62. #ifdef IC_MOTIF
  63. /*--------------------------- Start a Timer ------------------------------|
  64. | The X-Windows application add timeout routine is called by specifying,  |
  65. |   the application context which is obtained using the window handle,    |
  66. |   a time interval in milliseconds which is set to a constant 1000UL,    |
  67. |   an external routine that is called when the timer is up, and          |
  68. |   client_data which is a pointer to application specific data.          |
  69. | The timerId is a unique ID returned from the add timer routine that     |
  70. |   will be used when processing the expired timer.                       |
  71. | When the timer expires, the callback routine will be called with        |
  72. |   two parameters, the client_data and the timer id.                     |
  73. |------------------------------------------------------------------------*/
  74.   timerId = XtAppAddTimeOut (
  75.                    XtWidgetToApplicationContext ((Widget)window->handle()),
  76.                    TIME_INTERVAL,
  77.                    (XtTimerCallbackProc) postATimeHandlerEvent,
  78.                    window);
  79. #endif
  80.  
  81. #ifdef IC_PM
  82. /*--------------------------- Start a Timer ------------------------------|
  83. | The Presentation Manager start timer routine is called by specifying    |
  84. |   the anchor block handle for the current thread,                       |
  85. |   the handle of the window passed in,                                   |
  86. |   a unique timer ID that is below the PM TID_USER_MAX constant, and     |
  87. |   a time interval in milliseconds which is set to a constant 1000UL.    |
  88. | Since this application uses only a single timer, the timer ID is        |
  89. |   specified as a constant.                                              |
  90. | When the timer expires, PM will post a WM_TIMER event to the window     |
  91. |   specified in parameter 2 of the WinStartTimer call.  IParameter1      |
  92. |   of the timer event will contain the timer ID.                         |
  93. |------------------------------------------------------------------------*/
  94.   timerId = TIMER_ID;
  95.   WinStartTimer( IThread::current().anchorBlock(),
  96.                  window->handle(), timerId, TIME_INTERVAL);
  97. #endif
  98.  
  99. #ifdef IC_WIN
  100. /*--------------------------- Start a Timer ------------------------------|
  101. | The Windows start timer routine is called by specifying the handle to   |
  102. |   the window processing messages.                                       |
  103. | Since this application uses only a single timer, the timer ID is        |
  104. |   specified as a constant.                                              |
  105. | When the timer expires, Windows posts a WM_TIMER event to the window    |
  106. |   specified in parameter 1 of the SetTimer call.                        |
  107. |------------------------------------------------------------------------*/
  108.   timerId = TIMER_ID;
  109.   SetTimer( window->handle(), timerId, TIME_INTERVAL, NULL );
  110. #endif
  111.  
  112. /*------------------------- Start the Handler ----------------------------|
  113. | Start the handler by explicitly calling the overridden function.        |
  114. |------------------------------------------------------------------------*/
  115.   Inherited::handleEventsFor(window);
  116.   return (*this);
  117. }
  118.  
  119. #ifdef IC_MOTIF
  120. /**************************************************************************
  121. * external postATimeHandlerEvent - timer callback routine                 *
  122. *   Post a WM_TIMER event.  This is only necessary in MOTIF.              *
  123. *     OS/2 PM timer calls automatically post the WM_TIMER event.          *
  124. **************************************************************************/
  125.   extern void _System
  126.     postATimeHandlerEvent (IWindow * window, XtIntervalId *timerUp)
  127. {
  128. /*-------------------------- Is Window Valid ? ---------------------------|
  129. | This test prevents an exception in IWindow in the case where a timer    |
  130. |   expires while the window is being closed.  If the window is not valid,|
  131. |   do nothing.                                                           |
  132. |------------------------------------------------------------------------*/
  133.   if (window->isValid())
  134. {
  135. /*--------------------------- Add Next Timer -----------------------------|
  136. | When an X-Windows timer is up, it does not restart automatically.       |
  137. |   Therefore, the callback routine starts another timer and registers    |
  138. |   itself as the callback routine.  The new timer ID is passed to the    |
  139. |   time handler in the timer event as parameter 2.                       |
  140. |------------------------------------------------------------------------*/
  141.     IEventParameter2 newTimer = XtAppAddTimeOut (
  142.               XtWidgetToApplicationContext((Widget)window->handle()),
  143.               TIME_INTERVAL,
  144.               (XtTimerCallbackProc)postATimeHandlerEvent,
  145.               window);
  146. /*-------------------------- Post Timer Event ----------------------------|
  147. | The timer event is created by posting an event to the window passed     |
  148. |   in as client_data to the callback routine.  The ID of the timer that  |
  149. |   expired is passed to the time handler as IParameter1 of the event.    |
  150. |------------------------------------------------------------------------*/
  151.     window->postEvent (WM_TIMER, IEventParameter1(*timerUp), newTimer);
  152.   }
  153. }
  154. #endif
  155.  
  156. /**************************************************************************
  157. * ATimeHandler :: dispatchHandlerEvent                                    *
  158. *   Call tick when one of our timer events are dispatched.                *
  159. **************************************************************************/
  160. IBase::Boolean
  161.   ATimeHandler :: dispatchHandlerEvent(IEvent& event)
  162. {
  163.   Boolean eventProcessed(false);        //Assume event will not be proccessed
  164. /*--------------------------- Test the Event -----------------------------|
  165. | This event must be a timer event and parameter 1 must contain the       |
  166. |   ID of the timer that was started by this handler.                     |
  167. |------------------------------------------------------------------------*/
  168.   if ((event.eventId() == WM_TIMER) && (event.parameter1() == timerId))
  169.   {
  170. #ifdef IC_MOTIF
  171. /*--------------------------- Save New Timer ID --------------------------|
  172. | Since the timer ID is not constant in MOTIF, the new timer id, which    |
  173. |   is passed as parameter 2 of the event, must be saved.                 |
  174. |------------------------------------------------------------------------*/
  175.    timerId = event.parameter2();
  176. #endif
  177. /*------------------------- Process Timer Event --------------------------|
  178. | Process the timer event by calling the virtual function tick, which     |
  179. |   should be overridden by an inheriting class.  The returned boolean    |
  180. |   value determines whether the event was actually processed by tick.    |
  181. |   The default ATimeHandler::tick function does not process the event.   |
  182. |------------------------------------------------------------------------*/
  183.     eventProcessed = tick(event);
  184.   }
  185.   return (eventProcessed);
  186. }
  187.  
  188. /**************************************************************************
  189. * ATimeHandler :: tick                                                    *
  190. *   Default tick handler that should be overridden by inheriting classes  *
  191. **************************************************************************/
  192. IBase::Boolean
  193.   ATimeHandler :: tick(IEvent& event)
  194. {
  195.   return (false);                       //The timer event is not processed
  196. }
  197.  
  198. /**************************************************************************
  199. * ATimeHandler :: stopHandlingEventsFor                                   *
  200. *   Stop processing timer events for the window passed in.                *
  201. **************************************************************************/
  202. ATimeHandler
  203.   &ATimeHandler :: stopHandlingEventsFor( IWindow *window )
  204. {
  205.  
  206. #ifdef IC_MOTIF
  207. /*--------------------------- Stop the Timer -----------------------------|
  208. | The X-Windows remove timeout routine is called to stop the outstanding  |
  209. |   timer which is identified by the ID stored in timerId.                |
  210. |------------------------------------------------------------------------*/
  211.   XtRemoveTimeOut (timerId);
  212.   timerId = 0;
  213. #endif
  214.  
  215. #ifdef IC_PM
  216. /*--------------------------- Stop the Timer -----------------------------|
  217. | The Presentation Manager stop timer routine is called to stop the       |
  218. |   outstanding timer which is identified by,                             |
  219. |     the anchor block handle of the current thread,                      |
  220. |     the handle of the window passed in, and                             |
  221. |     the ID stored in timerId.                                           |
  222. |------------------------------------------------------------------------*/
  223.   if ( window->isValid() )
  224.     WinStopTimer( IThread::current().anchorBlock(),
  225.                   window->handle(), timerId);
  226. #endif
  227.  
  228. #ifdef IC_WIN
  229. /*--------------------------- Stop the Timer -----------------------------|
  230. | The Windows stop timer routine is called to stop the                    |
  231. |   outstanding timer which is identified by                              |
  232. |     the window handle processing messages.                              |
  233. |------------------------------------------------------------------------*/
  234.   if ( window->isValid() )
  235.     KillTimer( window->handle(), timerId );
  236. #endif
  237.  
  238. /*-------------------------- Stop the Handler ----------------------------|
  239. | Stop the handler by explicitly calling the overridden function.  Its    |
  240. |   returned value is used to determine the success of this routine.      |
  241. |------------------------------------------------------------------------*/
  242.   Inherited::stopHandlingEventsFor(window);
  243.   return (*this);
  244. }
  245.  
  246.