Annotation of pmsdk/samples/petzold/chap10/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: #include <os2.h>
                      9: #include <string.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[] = "FreeMem" ;
                     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:      static CHAR szText [] = "1,234,567,890 bytes" ;
                     57:      HPS         hps ;
                     58:      POINTL      aptl[TXTBOX_COUNT] ;
                     59:      RECTL       rcl ;
                     60: 
                     61:      hps = WinGetPS (hwndFrame) ;
                     62:      GpiQueryTextBox (hps, sizeof szText - 1L, szText, TXTBOX_COUNT, aptl) ;
                     63:      WinReleasePS (hps) ;
                     64: 
                     65:      rcl.yBottom = 0 ;
                     66:      rcl.yTop    = 3 * (aptl[TXTBOX_TOPLEFT].y -
                     67:                         aptl[TXTBOX_BOTTOMLEFT].y) / 2 ;
                     68:      rcl.xLeft   = 0 ;
                     69:      rcl.xRight  = (sizeof szText + 1L) * (aptl[TXTBOX_BOTTOMRIGHT].x -
                     70:                    aptl[TXTBOX_BOTTOMLEFT].x) / (sizeof szText - 1L) ;
                     71: 
                     72:      WinCalcFrameRect (hwndFrame, &rcl, FALSE) ;
                     73: 
                     74:      WinSetWindowPos (hwndFrame, NULL, (SHORT) rcl.xLeft, (SHORT) rcl.yBottom,
                     75:                       (SHORT) (rcl.xRight - rcl.xLeft),
                     76:                       (SHORT) (rcl.yTop - rcl.yBottom), SWP_SIZE | SWP_MOVE) ;
                     77:      }
                     78: 
                     79: VOID FormatNumber (CHAR *pchResult, ULONG ulValue)
                     80:      {
                     81:      BOOL  fDisplay = FALSE ;
                     82:      SHORT sDigit ;
                     83:      ULONG ulQuotient, ulDivisor = 1000000000L ;
                     84: 
                     85:      for (sDigit = 0 ; sDigit < 10 ; sDigit++)
                     86:           {
                     87:           ulQuotient = ulValue / ulDivisor ;
                     88: 
                     89:           if (fDisplay || ulQuotient > 0 || sDigit == 9)
                     90:                {
                     91:                fDisplay = TRUE ;
                     92: 
                     93:                *pchResult++ = (CHAR) ('0' + ulQuotient) ;
                     94: 
                     95:                if ((sDigit % 3 == 0) && sDigit != 9)
                     96:                     *pchResult++ = ',' ;
                     97:                }
                     98:           ulValue -= ulQuotient * ulDivisor ;
                     99:           ulDivisor /= 10 ;
                    100:           }
                    101:      *pchResult = '\0' ;
                    102:      }
                    103: 
                    104: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    105:      {
                    106:      static RECTL rcl ;
                    107:      static ULONG ulFreeMem, ulPrevMem ;
                    108:      CHAR         szBuffer [24] ;
                    109:      HPS          hps;
                    110: 
                    111:      switch (msg)
                    112:           {
                    113:           case WM_SIZE:
                    114:                WinQueryWindowRect (hwnd, &rcl) ;
                    115:                return 0 ;               
                    116: 
                    117:           case WM_TIMER:
                    118:                DosMemAvail (&ulFreeMem) ;
                    119: 
                    120:                if (ulFreeMem != ulPrevMem)
                    121:                     {
                    122:                     WinInvalidateRect (hwnd, NULL, FALSE) ;
                    123:                     ulPrevMem = ulFreeMem ;
                    124:                     }
                    125:                return 0 ;
                    126: 
                    127:           case WM_PAINT:
                    128:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    129: 
                    130:                FormatNumber (szBuffer, ulFreeMem) ;
                    131:                strcat (szBuffer, " bytes") ;
                    132: 
                    133:                WinDrawText (hps, -1, szBuffer, &rcl, 
                    134:                             CLR_NEUTRAL, CLR_BACKGROUND,
                    135:                             DT_CENTER | DT_VCENTER | DT_ERASERECT) ;
                    136: 
                    137:                WinEndPaint (hps) ;
                    138:                return 0 ;
                    139:           }
                    140:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    141:      }

unix.superglobalmegacorp.com

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