Annotation of pmsdk/samples/petzold/chap07/digclock.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.