Annotation of pmsdk/samples/petzold/chap07/clock.c, revision 1.1.1.1

1.1       root        1: /*-------------------------
                      2:    CLOCK.C -- Analog Clock 
                      3:   -------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: 
                      8: #include <os2.h>
                      9: #include <stddef.h>
                     10: #include <stdlib.h>
                     11: 
                     12: #define ID_TIMER 1
                     13: 
                     14: typedef struct
                     15:      {
                     16:      SHORT cxClient ;
                     17:      SHORT cyClient ;
                     18:      SHORT cxPixelDiam ;
                     19:      SHORT cyPixelDiam ;
                     20:      }
                     21:      WINDOWINFO ;
                     22: 
                     23: typedef WINDOWINFO *PWINDOWINFO ;
                     24: 
                     25: MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
                     26: 
                     27: int main (void)
                     28:      {
                     29:      static CHAR szClientClass[] = "Clock" ;
                     30:      HAB         hab ;
                     31:      HMQ         hmq ;
                     32:      HWND        hwndClient, hwndFrame ;
                     33:      QMSG        qmsg ;
                     34:      ULONG       flFrameFlags = FCF_STANDARD & ~FCF_MENU ;
                     35:      ULONG       flFrameStyle = WS_VISIBLE ;
                     36: 
                     37:      hab = WinInitialize (0) ;
                     38:      hmq = WinCreateMsgQueue (hab, 0) ;
                     39: 
                     40:      WinRegisterClass (hab, szClientClass, ClientWndProc,
                     41:                        CS_SIZEREDRAW | CS_SYNCPAINT, 0) ;
                     42: 
                     43:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                     44:                                      &flFrameFlags, szClientClass,
                     45:                                      szClientClass,
                     46:                                     0L, NULL, 0, &hwndClient) ;
                     47: 
                     48:      if (WinStartTimer (hab, hwndClient, ID_TIMER, 1000))
                     49:           {
                     50:           while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     51:                WinDispatchMsg (hab, &qmsg) ;
                     52: 
                     53:           WinStopTimer (hab, hwndClient, ID_TIMER) ;
                     54:           }
                     55:      else
                     56:           WinMessageBox (HWND_DESKTOP, hwndClient,
                     57:                          "Too many clocks or timers",
                     58:                          szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;
                     59: 
                     60:      WinDestroyWindow (hwndFrame) ;
                     61:      WinDestroyMsgQueue (hmq) ;
                     62:      WinTerminate (hab) ;
                     63: 
                     64:      return 0 ;
                     65:      }
                     66: 
                     67: VOID RotatePoint (POINTL aptl[], SHORT sNum, SHORT sAngle)
                     68:      {
                     69:      static SHORT sSin [60] =
                     70:                     {
                     71:                        0,  105,  208,  309,  407,  500,  588,  669,  743,  809,
                     72:                      866,  914,  951,  978,  995, 1000,  995,  978,  951,  914,
                     73:                      866,  809,  743,  669,  588,  500,  407,  309,  208,  105,
                     74:                        0, -104, -207, -308, -406, -499, -587, -668, -742, -808,
                     75:                     -865, -913, -950, -977, -994, -999, -994, -977, -950, -913,
                     76:                     -865, -808, -742, -668, -587, -499, -406, -308, -207, -104
                     77:                     } ;
                     78:      POINTL       ptlTemp ;
                     79:      SHORT        sIndex ;
                     80: 
                     81:      for (sIndex = 0 ; sIndex < sNum ; sIndex++)
                     82:           {
                     83:           ptlTemp.x = (aptl[sIndex].x * sSin [(sAngle + 15) % 60] +
                     84:                        aptl[sIndex].y * sSin [sAngle]) / 1000 ;
                     85: 
                     86:           ptlTemp.y = (aptl[sIndex].y * sSin [(sAngle + 15) % 60] -
                     87:                        aptl[sIndex].x * sSin [sAngle]) / 1000 ;
                     88: 
                     89:           aptl[sIndex] = ptlTemp ;
                     90:           }
                     91:      }
                     92: 
                     93: VOID ScalePoint (POINTL aptl[], SHORT sNum, PWINDOWINFO pwi)
                     94:      {
                     95:      SHORT sIndex ;
                     96: 
                     97:      for (sIndex = 0 ; sIndex < sNum ; sIndex++)
                     98:           {
                     99:           aptl[sIndex].x = aptl[sIndex].x * pwi->cxPixelDiam / 200 ;
                    100:           aptl[sIndex].y = aptl[sIndex].y * pwi->cyPixelDiam / 200 ;
                    101:           }
                    102:      }
                    103: 
                    104: VOID TranslatePoint (POINTL aptl[], SHORT sNum, PWINDOWINFO pwi)
                    105:      {
                    106:      SHORT sIndex ;
                    107: 
                    108:      for (sIndex = 0 ; sIndex < sNum ; sIndex++)
                    109:           {
                    110:           aptl[sIndex].x += pwi->cxClient / 2 ;
                    111:           aptl[sIndex].y += pwi->cyClient / 2 ;
                    112:           }
                    113:      }
                    114: 
                    115: VOID DrawHand (HPS hps, POINTL aptlIn[], SHORT sNum, SHORT sAngle,
                    116:                PWINDOWINFO pwi)
                    117:      {
                    118:      POINTL aptl [5] ;
                    119:      SHORT  sIndex ;
                    120: 
                    121:      for (sIndex = 0 ; sIndex < sNum ; sIndex++)
                    122:           aptl [sIndex] = aptlIn [sIndex] ;
                    123: 
                    124:      RotatePoint    (aptl, sNum, sAngle) ;
                    125:      ScalePoint     (aptl, sNum, pwi) ;
                    126:      TranslatePoint (aptl, sNum, pwi) ;
                    127: 
                    128:      GpiMove (hps, aptl) ;
                    129: 
                    130:      GpiPolyLine (hps, sNum - 1L, aptl + 1) ;
                    131:      }
                    132: 
                    133: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    134:      {
                    135:      static DATETIME   dtPrevious ;
                    136:      static HDC        hdc ;
                    137:      static LONG       xPixelsPerMeter, yPixelsPerMeter ;
                    138:      static POINTL     aptlHour   [5] = { 0,-15, 10,0, 0,60, -10,0, 0,-15 },
                    139:                        aptlMinute [5] = { 0,-20,  5,0, 0,80,  -5,0, 0,-20 },
                    140:                        aptlSecond [2] = { 0,  0,  0,80 } ;
                    141:      static WINDOWINFO wi ;
                    142:      DATETIME          dt ;
                    143:      HPS               hps ;
                    144:      POINTL            aptl [3] ;
                    145:      SHORT             sDiamMM, sAngle ;
                    146: 
                    147:      switch (msg)
                    148:           {
                    149:           case WM_CREATE:
                    150:                hdc = WinOpenWindowDC (hwnd) ;
                    151: 
                    152:                DevQueryCaps (hdc, CAPS_VERTICAL_RESOLUTION,
                    153:                                   1L, &yPixelsPerMeter) ;
                    154:                DevQueryCaps (hdc, CAPS_HORIZONTAL_RESOLUTION,
                    155:                                   1L, &xPixelsPerMeter) ;
                    156: 
                    157:                DosGetDateTime (&dtPrevious) ;
                    158:                dtPrevious.hours = (dtPrevious.hours * 5) % 60 +
                    159:                                    dtPrevious.minutes / 12 ;
                    160:                return 0 ;
                    161: 
                    162:           case WM_SIZE:
                    163:                wi.cxClient = SHORT1FROMMP (mp2) ;
                    164:                wi.cyClient = SHORT2FROMMP (mp2) ;
                    165: 
                    166:                sDiamMM = (SHORT) min (wi.cxClient * 1000L / xPixelsPerMeter,
                    167:                                       wi.cyClient * 1000L / yPixelsPerMeter) ;
                    168: 
                    169:                wi.cxPixelDiam = (SHORT) (xPixelsPerMeter * sDiamMM / 1000) ;
                    170:                wi.cyPixelDiam = (SHORT) (yPixelsPerMeter * sDiamMM / 1000) ;
                    171:                return 0 ;
                    172: 
                    173:           case WM_TIMER:
                    174:                if (0L == WinQuerySysValue (HWND_DESKTOP, SV_TRACKRECTLEVEL))
                    175:                     break ;
                    176: 
                    177:                DosGetDateTime (&dt) ;
                    178:                dt.hours = (dt.hours * 5) % 60 + dt.minutes / 12 ;
                    179: 
                    180:                hps = WinGetPS (hwnd) ;
                    181: 
                    182:                GpiSetColor (hps, CLR_BACKGROUND) ;
                    183:                DrawHand (hps, aptlSecond, 2, dtPrevious.seconds, &wi) ;
                    184: 
                    185:                if (dt.hours   != dtPrevious.hours ||
                    186:                    dt.minutes != dtPrevious.minutes)
                    187:                     {
                    188:                     DrawHand (hps, aptlHour,   5, dtPrevious.hours,   &wi) ;
                    189:                     DrawHand (hps, aptlMinute, 5, dtPrevious.minutes, &wi) ;
                    190:                     }
                    191: 
                    192:                GpiSetColor (hps, CLR_NEUTRAL) ;
                    193:                DrawHand (hps, aptlHour,   5, dt.hours,   &wi) ;
                    194:                DrawHand (hps, aptlMinute, 5, dt.minutes, &wi) ;
                    195:                DrawHand (hps, aptlSecond, 2, dt.seconds, &wi) ;
                    196: 
                    197:                WinReleasePS (hps) ;
                    198:                dtPrevious = dt ;
                    199:                return 0 ;
                    200: 
                    201:           case WM_PAINT:
                    202:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    203:                GpiErase (hps) ;
                    204: 
                    205:                for (sAngle = 0 ; sAngle < 60 ; sAngle++)
                    206:                     {
                    207:                     aptl[0].x = 0 ;
                    208:                     aptl[0].y = 90 ;
                    209: 
                    210:                     RotatePoint    (aptl, 1, sAngle) ;
                    211:                     ScalePoint     (aptl, 1, &wi) ;
                    212:                     TranslatePoint (aptl, 1, &wi) ;
                    213: 
                    214:                     aptl[2].x = aptl[2].y = sAngle % 5 ? 2 : 10 ;
                    215: 
                    216:                     ScalePoint (aptl + 2, 1, &wi) ;
                    217: 
                    218:                     aptl[0].x -= aptl[2].x / 2 ;
                    219:                     aptl[0].y -= aptl[2].y / 2 ;
                    220: 
                    221:                     aptl[1].x = aptl[0].x + aptl[2].x ;
                    222:                     aptl[1].y = aptl[0].y + aptl[2].y ;
                    223: 
                    224:                     GpiMove (hps, aptl) ;
                    225:                     GpiBox (hps, DRO_OUTLINEFILL, aptl + 1,
                    226:                             aptl[2].x, aptl[2].y) ;
                    227: 
                    228:                     }
                    229:                DrawHand (hps, aptlHour,   5, dtPrevious.hours,   &wi) ;
                    230:                DrawHand (hps, aptlMinute, 5, dtPrevious.minutes, &wi) ;
                    231:                DrawHand (hps, aptlSecond, 2, dtPrevious.seconds, &wi) ;
                    232: 
                    233:                WinEndPaint (hps) ;
                    234:                return 0 ;
                    235:           }
                    236:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    237:      }

unix.superglobalmegacorp.com

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