home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv3_1 / hexcalc / hexcalcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  4.9 KB  |  162 lines

  1. /*
  2.  
  3. Figure 6pm
  4. =========
  5.  
  6. */
  7.  
  8. /*------------------------------------------------------------------
  9.    HEXCALCP.C -- Hexadecimal Calculator (OS/2 Presentation Manager)
  10.   ------------------------------------------------------------------*/
  11.  
  12. #include <os2.h>
  13. #include <limits.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "hexcalcp.h"
  18.  
  19. ULONG EXPENTRY ClientWndProc (HWND, USHORT, ULONG, ULONG) ;
  20.  
  21. main ()
  22.      {
  23.      static CHAR szClassName [] = "HexCalcP" ;
  24.      HAB         hAB ;
  25.      HMQ         hMQ ;
  26.      HWND        hWnd ;
  27.      QMSG        qmsg ;
  28.  
  29.      hAB = WinInitialize () ;
  30.      hMQ = WinCreateMsgQueue (hAB, 0) ;
  31.  
  32.      WinRegisterClass (hAB, szClassName, ClientWndProc, 0L, 0, NULL) ;
  33.  
  34.      hWnd = WinLoadDlg (HWND_DESKTOP, HWND_DESKTOP,
  35.                                       NULL, NULL, ID_HEXCALC, NULL) ;
  36.  
  37.      WinSetFocus (HWND_DESKTOP, WinWindowFromID (hWnd, FID_CLIENT)) ;
  38.  
  39.      while (WinGetMsg (hAB, &qmsg, NULL, 0, 0))
  40.           WinDispatchMsg (hAB, &qmsg) ;
  41.  
  42.      WinDestroyWindow (hWnd) ;
  43.      WinDestroyMsgQueue (hMQ) ;
  44.      WinTerminate (hAB) ;
  45.  
  46.      return 0 ;
  47.      }
  48.  
  49. void ShowNumber (hWnd, lNumber)
  50.      HWND  hWnd ;
  51.      ULONG lNumber ;
  52.      {
  53.      CHAR  szBuffer [20] ;
  54.      HWND  hWndResult ;
  55.  
  56.      hWndResult = WinWindowFromID (hWnd, VK_ESCAPE) ;
  57.  
  58.      WinSetWindowText (hWndResult,
  59.                          strupr (ltoa (lNumber, szBuffer, 16))) ;
  60.      }
  61.  
  62. ULONG CalcIt (lFirstNum, iOperation, lNum)
  63.      ULONG lFirstNum, lNum ;
  64.      SHORT iOperation ;
  65.      {
  66.      switch (iOperation)
  67.           {
  68.           case '=' : return lNum ;
  69.           case '+' : return lFirstNum +  lNum ;
  70.           case '-' : return lFirstNum -  lNum ;
  71.           case '*' : return lFirstNum *  lNum ;
  72.           case '&' : return lFirstNum &  lNum ;
  73.           case '|' : return lFirstNum |  lNum ;
  74.           case '^' : return lFirstNum ^  lNum ;
  75.           case '<' : return lFirstNum << lNum ;
  76.           case '>' : return lFirstNum >> lNum ;
  77.           case '/' : return lNum ? lFirstNum / lNum : ULONG_MAX ;
  78.           case '%' : return lNum ? lFirstNum % lNum : ULONG_MAX ;
  79.           default  : return 0L ;
  80.           }
  81.      }
  82.  
  83. ULONG EXPENTRY ClientWndProc (hWnd, nMessage, lParam1, lParam2)
  84.      HWND         hWnd ;
  85.      USHORT       nMessage ;
  86.      ULONG        lParam1 ;
  87.      ULONG        lParam2 ;
  88.      {
  89.      static BOOL  bNewNumber = TRUE ;
  90.      static ULONG lNumber, lFirstNum ;
  91.      static SHORT iOperation = '=' ;
  92.      HWND         hWndButton ;
  93.      SHORT        idButton ;
  94.  
  95.      switch (nMessage)
  96.           {
  97.           case WM_CHAR:
  98.                if (lParam1 & KC_KEYUP)
  99.                     break ;
  100.  
  101.                if (HIUSHORT (lParam2) == VK_LEFT)   /* left arrow to */
  102.                     LOUSHORT (lParam2) = VK_BACK ;  /*   backspace   */
  103.  
  104.                if (HIUSHORT (lParam2) == VK_RETURN) /* return to     */
  105.                     LOUSHORT (lParam2) = '=' ;      /*   equals      */
  106.  
  107.                if (LOUSHORT (lParam2) == 0)
  108.                     break ;
  109.  
  110.                LOUSHORT (lParam2) = toupper (LOUSHORT (lParam2)) ;
  111.  
  112.                if (hWndButton =
  113.                          WinWindowFromID (hWnd, LOUSHORT (lParam2)))
  114.                     WinSendMsg (hWndButton, BM_CLICK, 0L, 0L) ;
  115.                else
  116.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  117.  
  118.                return 1L ;
  119.  
  120.           case WM_COMMAND:
  121.                idButton = LOUSHORT (lParam1) ;
  122.  
  123.                if (idButton == VK_BACK)                /* backspace */
  124.                     ShowNumber (hWnd, lNumber /= 16) ;
  125.  
  126.                else if (idButton == VK_ESCAPE)         /* escape    */
  127.                     ShowNumber (hWnd, lNumber = 0L) ;
  128.  
  129.                else if (isxdigit (idButton))           /* hex digit */
  130.                     {
  131.                     if (bNewNumber)
  132.                          {
  133.                          lFirstNum = lNumber ;
  134.                          lNumber = 0L ;
  135.                          }
  136.                     bNewNumber = FALSE ;
  137.  
  138.                     if (lNumber <= ULONG_MAX >> 4)
  139.                          ShowNumber (hWnd, lNumber = 
  140.                               16 * lNumber + idButton -
  141.                               (isdigit (idButton) ? '0' : 'A' - 10)) ;
  142.                     else
  143.                          WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  144.                     }
  145.                else                                    /* operation */
  146.                     {
  147.                     if (!bNewNumber)
  148.                          ShowNumber (hWnd, lNumber =
  149.                               CalcIt (lFirstNum, iOperation, lNumber)) ;
  150.                     bNewNumber = TRUE ;
  151.                     iOperation = idButton ;
  152.                     }
  153.                break ;
  154.  
  155.           default :
  156.                return WinDefWindowProc (hWnd, nMessage, lParam1,
  157.                                                         lParam2) ;
  158.           }
  159.      return 0L ;
  160.      }
  161.  
  162.