home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / petzold / chap10 / digclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  6.6 KB  |  196 lines

  1. /*-----------------------------
  2.    DIGCLOCK.C -- Digital Clock 
  3.   -----------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #define INCL_DOS
  8. #include <os2.h>
  9. #include <stdio.h>
  10.  
  11. #define ID_TIMER 1
  12.  
  13. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  14. VOID    SizeTheWindow (HWND) ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR  szClientClass[] = "DigClock" ;
  19.      static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU  |
  20.                                  FCF_BORDER   | FCF_TASKLIST ;
  21.      HAB          hab ;
  22.      HMQ          hmq ;
  23.      HWND         hwndFrame, hwndClient ;
  24.      QMSG         qmsg ;
  25.  
  26.      hab = WinInitialize (0) ;
  27.      hmq = WinCreateMsgQueue (hab, 0) ;
  28.  
  29.      WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0) ;
  30.  
  31.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  32.                                      &flFrameFlags, szClientClass, NULL,
  33.                      0L, NULL, 0, &hwndClient) ;
  34.      SizeTheWindow (hwndFrame) ;
  35.  
  36.      if (WinStartTimer (hab, hwndClient, ID_TIMER, 1000))
  37.           {
  38.           while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  39.                WinDispatchMsg (hab, &qmsg) ;
  40.  
  41.           WinStopTimer (hab, hwndClient, ID_TIMER) ;
  42.           }
  43.      else
  44.           WinMessageBox (HWND_DESKTOP, hwndClient,
  45.                          "Too many clocks or timers",
  46.                          szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;
  47.  
  48.      WinDestroyWindow (hwndFrame) ;
  49.      WinDestroyMsgQueue (hmq) ;
  50.      WinTerminate (hab) ;
  51.      return 0 ;
  52.      }
  53.  
  54. VOID SizeTheWindow (HWND hwndFrame)
  55.      {
  56.      FONTMETRICS fm ;
  57.      HPS         hps ;
  58.      RECTL       rcl ;
  59.  
  60.      hps = WinGetPS (hwndFrame) ;
  61.      GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  62.      WinReleasePS (hps) ;
  63.  
  64.      rcl.yBottom = 0 ;
  65.      rcl.yTop    = 11 * fm.lMaxBaselineExt / 4 ;
  66.      rcl.xRight  = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN) ;
  67.      rcl.xLeft   = rcl.xRight - 16 * fm.lEmInc ;
  68.  
  69.      WinCalcFrameRect (hwndFrame, &rcl, FALSE) ;
  70.  
  71.      WinSetWindowPos (hwndFrame, NULL, (SHORT) rcl.xLeft, (SHORT) rcl.yBottom,
  72.                       (SHORT) (rcl.xRight - rcl.xLeft),
  73.                       (SHORT) (rcl.yTop - rcl.yBottom), SWP_SIZE | SWP_MOVE) ;
  74.      }
  75.  
  76. VOID UpdateTime (HWND hwnd, HPS hps)
  77.      {
  78.      static BOOL        fHaveCtryInfo = FALSE ;
  79.      static CHAR        *szDayName [] = { "Sun", "Mon", "Tue", "Wed",
  80.                                           "Thu", "Fri", "Sat" } ;
  81.      static CHAR        szDateFormat [] = " %s  %d%s%02d%s%02d " ;
  82.      static COUNTRYCODE ctryc = { 0, 0 } ;
  83.      static COUNTRYINFO ctryi ;
  84.      CHAR               szBuffer [20] ;
  85.      DATETIME           dt ;
  86.      RECTL              rcl ;
  87.      USHORT             usDataLength ;
  88.  
  89.                /*----------------------------------------
  90.                   Get Country Information, Date and Time
  91.                  ----------------------------------------*/
  92.  
  93.      if (!fHaveCtryInfo)
  94.           {
  95.           DosGetCtryInfo (sizeof ctryi, &ctryc, &ctryi, &usDataLength) ;
  96.           fHaveCtryInfo = TRUE ;
  97.           }
  98.      DosGetDateTime (&dt) ;
  99.      dt.year %= 100 ;
  100.  
  101.                /*------------- 
  102.                   Format Date
  103.                  -------------*/
  104.                                    /*-----------------
  105.                                       mm/dd/yy format
  106.                                      -----------------*/
  107.      if (ctryi.fsDateFmt == 0)
  108.  
  109.           sprintf (szBuffer, szDateFormat, szDayName [dt.weekday],
  110.                              dt.month, ctryi.szDateSeparator,
  111.                              dt.day,   ctryi.szDateSeparator, dt.year) ;
  112.  
  113.                                    /*-----------------
  114.                                       dd/mm/yy format
  115.                                      -----------------*/
  116.      else if (ctryi.fsDateFmt == 1)
  117.  
  118.           sprintf (szBuffer, szDateFormat, szDayName [dt.weekday],
  119.                              dt.day,   ctryi.szDateSeparator,
  120.                              dt.month, ctryi.szDateSeparator, dt.year) ;
  121.  
  122.                                    /*-----------------
  123.                                       yy/mm/dd format
  124.                                      -----------------*/
  125.      else
  126.           sprintf (szBuffer, szDateFormat, szDayName [dt.weekday],
  127.                              dt.year,  ctryi.szDateSeparator,
  128.                              dt.month, ctryi.szDateSeparator, dt.day) ;
  129.  
  130.                /*-------------- 
  131.                   Display Date
  132.                  --------------*/
  133.  
  134.      WinQueryWindowRect (hwnd, &rcl) ;
  135.      rcl.yBottom += 5 * rcl.yTop / 11 ;
  136.      WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  137.                   DT_CENTER | DT_VCENTER) ;
  138.  
  139.                /*------------- 
  140.                   Format Time
  141.                  -------------*/
  142.                                    /*----------------
  143.                                       12-hour format
  144.                                      ----------------*/
  145.      if ((ctryi.fsTimeFmt & 1) == 0)
  146.  
  147.           sprintf (szBuffer, " %d%s%02d%s%02d %cm ",
  148.                              (dt.hours + 11) % 12 + 1, ctryi.szTimeSeparator,
  149.                              dt.minutes, ctryi.szTimeSeparator,
  150.                              dt.seconds, dt.hours / 12 ? 'p' : 'a') ;
  151.  
  152.                                    /*----------------
  153.                                       24-hour format
  154.                                      ----------------*/
  155.      else
  156.           sprintf (szBuffer, " %02d%s%02d%s%02d ",
  157.                              dt.hours,   ctryi.szTimeSeparator,
  158.                              dt.minutes, ctryi.szTimeSeparator, dt.seconds) ;
  159.  
  160.                /*-------------- 
  161.                   Display Time
  162.                  --------------*/
  163.  
  164.      WinQueryWindowRect (hwnd, &rcl) ;
  165.      rcl.yTop -= 5 * rcl.yTop / 11 ;
  166.      WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  167.                   DT_CENTER | DT_VCENTER) ;
  168.      }
  169.  
  170. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  171.      {
  172.      HPS  hps;
  173.  
  174.      switch (msg)
  175.           {
  176.           case WM_TIMER:
  177.                hps = WinGetPS (hwnd) ;
  178.                GpiSetBackMix (hps, BM_OVERPAINT) ;
  179.  
  180.                UpdateTime (hwnd, hps) ;
  181.  
  182.                WinReleasePS (hps) ;
  183.                return 0 ;
  184.  
  185.           case WM_PAINT:
  186.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  187.                GpiErase (hps) ;
  188.  
  189.                UpdateTime (hwnd, hps) ;
  190.  
  191.                WinEndPaint (hps) ;
  192.                return 0 ;
  193.           }
  194.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  195.      }
  196.