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

1.1       root        1: /*----------------------------------
                      2:    FREEMEM.C -- Free Memory Display 
                      3:   ----------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: #define INCL_DOS
                      8: 
                      9: #include <os2.h>
                     10: #include <stddef.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[] = "FreeMem" ;
                     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    = 3 * fm.lMaxBaselineExt / 2 ;
                     71:      rcl.xLeft   = 0 ;
                     72:      rcl.xRight  = 14 * 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 FormatNumber (ULONG ulValue, CHAR *szResult)
                     82:      {
                     83:      BOOL  fDisplay = FALSE ;
                     84:      SHORT sDigit ;
                     85:      ULONG ulQuotient, ulDivisor = 1000000000L ;
                     86: 
                     87:      for (sDigit = 0 ; sDigit < 10 ; sDigit++)
                     88:           {
                     89:           ulQuotient = ulValue / ulDivisor ;
                     90: 
                     91:           if (fDisplay || ulQuotient > 0 || sDigit == 9)
                     92:                {
                     93:                fDisplay = TRUE ;
                     94: 
                     95:                *szResult++ = (CHAR) ('0' + ulQuotient) ;
                     96: 
                     97:                if ((sDigit % 3 == 0) && sDigit != 9)
                     98:                     *szResult++ = ',' ;
                     99:                }
                    100:           ulValue -= ulQuotient * ulDivisor ;
                    101:           ulDivisor /= 10 ;
                    102:           }
                    103:      *szResult = '\0' ;
                    104:      }
                    105: 
                    106: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    107:      {
                    108:      static RECTL rcl ;
                    109:      static ULONG ulFreeMem, ulPrevMem ;
                    110:      CHAR         szBuffer [16] ;
                    111:      HPS          hps;
                    112: 
                    113:      switch (msg)
                    114:           {
                    115:           case WM_SIZE:
                    116:                WinQueryWindowRect (hwnd, &rcl) ;
                    117:                return 0 ;               
                    118: 
                    119:           case WM_TIMER:
                    120:                DosMemAvail (&ulFreeMem) ;
                    121: 
                    122:                if (ulFreeMem != ulPrevMem)
                    123:                     {
                    124:                     WinInvalidateRect (hwnd, NULL, FALSE) ;
                    125:                     ulPrevMem = ulFreeMem ;
                    126:                     }
                    127:                return 0 ;
                    128: 
                    129:           case WM_PAINT:
                    130:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    131: 
                    132:                FormatNumber (ulFreeMem, szBuffer) ;
                    133: 
                    134:                WinDrawText (hps, -1, szBuffer, &rcl, 
                    135:                             CLR_NEUTRAL, CLR_BACKGROUND,
                    136:                             DT_CENTER | DT_VCENTER | DT_ERASERECT) ;
                    137: 
                    138:                WinEndPaint (hps) ;
                    139:                return 0 ;
                    140:           }
                    141:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    142:      }

unix.superglobalmegacorp.com

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