home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / DIGCLOCK.ZIP / DIGCLOCK.C next >
Text File  |  1989-06-07  |  7KB  |  221 lines

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