home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / TIMELINE.ZIP / TIMELINE.C next >
Text File  |  1989-04-13  |  4KB  |  136 lines

  1. /*-------------------------------------------------------------------
  2.    TIMELINE.C -- Time and Date Display for OS/2 Presentation Manager
  3.                  (c) 1989, Ziff Communications Co.
  4.                  PC Magazine * Charles Petzold, November 1988
  5.   -------------------------------------------------------------------*/
  6.  
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #include <os2.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. #define ID_TIMER 1
  14.  
  15. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  16.  
  17. HAB  hab ;
  18.  
  19. int main (void)
  20.      {
  21.      static CHAR  szClientClass[] = "TimeLine" ;
  22.      static ULONG flFrameFlags = FCF_BORDER | FCF_TASKLIST ;
  23.      HMQ          hmq ;
  24.      HWND         hwndFrame, hwndClient ;
  25.      QMSG         qmsg ;
  26.  
  27.      hab = WinInitialize (0) ;
  28.      hmq = WinCreateMsgQueue (hab, 0) ;
  29.  
  30.      WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0) ;
  31.  
  32.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  33.                                      &flFrameFlags, szClientClass, NULL,
  34.                      0L, NULL, 0, &hwndClient) ;
  35.  
  36.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  37.           WinDispatchMsg (hab, &qmsg) ;
  38.  
  39.      WinDestroyWindow (hwndFrame) ;
  40.      WinDestroyMsgQueue (hmq) ;
  41.      WinTerminate (hab) ;
  42.      return 0 ;
  43.      }
  44.  
  45. VOID SizeWindow (HWND hwnd)
  46.      {
  47.      static CHAR szDummyTime[] = " Wed May 00 00:00:00 0000 " ;
  48.      HPS         hps ;
  49.      HWND        hwndFrame ;
  50.      POINTL      aptl[TXTBOX_COUNT] ;
  51.      RECTL       rcl ;
  52.  
  53.                     // Find width and height of text string
  54.  
  55.      hps = WinGetPS (hwnd) ;
  56.      GpiQueryTextBox (hps, (LONG) strlen (szDummyTime), szDummyTime,
  57.                            TXTBOX_COUNT, aptl) ;
  58.      WinReleasePS (hps) ;
  59.  
  60.                     // Set up rectangle structure based on text dimensions
  61.  
  62.      rcl.xLeft   = 0 ;
  63.      rcl.yBottom = 0 ;
  64.      rcl.xRight  = aptl[TXTBOX_BOTTOMRIGHT].x - aptl[TXTBOX_BOTTOMLEFT].x ;
  65.      rcl.yTop    = aptl[TXTBOX_TOPLEFT].y     - aptl[TXTBOX_BOTTOMLEFT].y ;
  66.  
  67.                     // Set frame window position and size
  68.  
  69.      hwndFrame = WinQueryWindow (hwnd, QW_PARENT, FALSE) ;
  70.  
  71.      WinCalcFrameRect (hwndFrame, &rcl, FALSE) ;
  72.      WinSetWindowPos  (hwndFrame, NULL,
  73.                        (SHORT) rcl.xLeft,  (SHORT) rcl.yBottom,
  74.                        (SHORT) rcl.xRight, (SHORT) rcl.yTop,
  75.                        SWP_MOVE | SWP_SIZE) ;
  76.      }
  77.  
  78. VOID UpdateTime (HWND hwnd, HPS hps)
  79.      {
  80.      CHAR   *szTime ;
  81.      CHAR   szFormattedTime[40] ;
  82.      RECTL  rcl ;
  83.      time_t lTime ;
  84.  
  85.                     // Get ASCII time and date string and format it
  86.  
  87.      time (&lTime) ;
  88.      szTime = ctime (&lTime) ;
  89.      strcpy (szFormattedTime, "   ") ;
  90.      strcat (szFormattedTime, szTime) ;
  91.      szFormattedTime[strlen (szFormattedTime) - 1] = '\0' ;
  92.      strcat (szFormattedTime, "   ") ;
  93.  
  94.                     // Display string in client window
  95.  
  96.      WinQueryWindowRect (hwnd, &rcl) ;
  97.      GpiSetBackMix (hps, BM_OVERPAINT) ;
  98.      WinDrawText (hps, -1, szFormattedTime, &rcl,
  99.                   CLR_NEUTRAL, CLR_BACKGROUND, DT_CENTER | DT_VCENTER) ;
  100.      }
  101.  
  102. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  103.      {
  104.      HPS hps;
  105.  
  106.      switch (msg)
  107.           {
  108.           case WM_CREATE:
  109.                SizeWindow (hwnd) ;
  110.                WinStartTimer (hab, hwnd, ID_TIMER, 1000) ;
  111.                return 0 ;
  112.  
  113.           case WM_TIMER:
  114.                hps = WinGetPS (hwnd) ;
  115.  
  116.                UpdateTime (hwnd, hps) ;
  117.  
  118.                WinReleasePS (hps) ;
  119.                return 0 ;
  120.  
  121.           case WM_PAINT:
  122.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  123.                GpiErase (hps) ;
  124.  
  125.                UpdateTime (hwnd, hps) ;
  126.  
  127.                WinEndPaint (hps) ;
  128.                return 0 ;
  129.  
  130.           case WM_DESTROY:
  131.                WinStopTimer (hab, hwnd, ID_TIMER) ;
  132.                return 0 ;
  133.           }
  134.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  135.      }
  136.