home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / ZINC.ZIP / D_CALC.CPP next >
Encoding:
C/C++ Source or Header  |  1990-07-23  |  4.8 KB  |  162 lines

  1. //    Program name..    Zinc Interface Library
  2. //    Filename......    D_APPS.CPP
  3. //    
  4. //    COPYRIGHT (C) 1990.  All Rights Reserved.
  5. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  6.  
  7. #include <ui_win.hpp>
  8. #include "d_demo.hpp"
  9. #include "d_help.hlh"
  10.  
  11. // Definition of the calculator class.
  12. class CALCULATOR : public UIW_WINDOW
  13. {
  14. public:
  15.     CALCULATOR(int left, int top, char *title);
  16.  
  17. private:
  18.     UIW_NUMBER *numberField;
  19.     long operand1;
  20.     long operand2;
  21.     int operatorLast;
  22.     UCHAR operation;
  23.  
  24.     static void ButtonFunction(void *button, UI_EVENT &event);
  25.     void Display(UIW_BUTTON *button);
  26. };
  27.  
  28. CALCULATOR::CALCULATOR(int left, int top, char *title) :
  29.     UIW_WINDOW(left, top, 19, 10, WOF_NO_FLAGS, WOAF_NO_SIZE | WOAF_NORMAL_HOT_KEYS, INFO_CALCULATOR)
  30. {
  31.     // Initialize the calculator values.
  32.     operand1 = 0L;
  33.     operand2 = 0L;
  34.     operatorLast = TRUE;
  35.     operation = '=';
  36.  
  37.     // Create the number display field.
  38.     numberField = new UIW_NUMBER(2, 1, 12, &operand2, NULL, NMF_COMMAS,
  39.         WOF_BORDER | WOF_JUSTIFY_RIGHT | WOF_VIEW_ONLY | WOF_NO_ALLOCATE_DATA);
  40.  
  41.     // Add the buttons and other objects.
  42.     *this
  43.         + new UIW_BORDER
  44.         + new UIW_MINIMIZE_BUTTON
  45.         + &(*new UIW_SYSTEM_BUTTON
  46.             + new UIW_POP_UP_ITEM("~Move", MNIF_MOVE, BTF_NO_TOGGLE, WOF_NO_FLAGS, 0)
  47.             + new UIW_POP_UP_ITEM("Mi~nimize", MNIF_MINIMIZE, BTF_NO_TOGGLE, WOF_NO_FLAGS, 0)
  48.             + new UIW_POP_UP_ITEM
  49.             + new UIW_POP_UP_ITEM("~Close", MNIF_CLOSE, BTF_NO_TOGGLE, WOF_NO_FLAGS, 0)
  50.             + new HELP_PULL_DOWN_ITEM(" ~About the calculator ", MNF_NO_FLAGS, INFO_CALCULATOR))
  51.         + new UIW_TITLE(title, WOF_JUSTIFY_CENTER)
  52.         + numberField
  53.         + new UIW_BUTTON(1, 3, 4, "~7", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  54.         + new UIW_BUTTON(5, 3, 4, "~8", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  55.         + new UIW_BUTTON(9, 3, 4, "~9", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  56.         + new UIW_BUTTON(13, 3, 4, "~/", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  57.         + new UIW_BUTTON(1, 4, 4, "~4", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  58.         + new UIW_BUTTON(5, 4, 4, "~5", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  59.         + new UIW_BUTTON(9, 4, 4, "~6", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  60.         + new UIW_BUTTON(13, 4, 4, "~-", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  61.         + new UIW_BUTTON(1, 5, 4, "~1", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  62.         + new UIW_BUTTON(5, 5, 4, "~2", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  63.         + new UIW_BUTTON(9, 5, 4, "~3", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  64.         + new UIW_BUTTON(13, 5, 4, "~*", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  65.         + new UIW_BUTTON(1, 6, 4, "~C", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  66.         + new UIW_BUTTON(5, 6, 4, "~0", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  67.         + new UIW_BUTTON(9, 6, 4, "~=", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction)
  68.         + new UIW_BUTTON(13, 6, 4, "~+", BTF_NO_TOGGLE, WOF_JUSTIFY_CENTER, CALCULATOR::ButtonFunction);
  69. }
  70.  
  71. #pragma argsused
  72. void CALCULATOR::ButtonFunction(void *button, UI_EVENT &event)
  73. {
  74.     CALCULATOR *calculator = (CALCULATOR *)((UIW_BUTTON *)button)->parent;
  75.     calculator->Display((UIW_BUTTON *)button);
  76. }
  77.  
  78. void CALCULATOR::Display(UIW_BUTTON *button)
  79. {
  80.     // Switch on the button value.
  81.     switch (button->hotKey)
  82.     {
  83.  
  84.     // Clear the calculator.
  85.     case 'C' :
  86.         operand1 =
  87.             operand2 = 0L;
  88.         operation = '=';
  89.         break;
  90.  
  91.     // Operations.
  92.     case '/' :
  93.     case '*' :
  94.     case '-' :
  95.     case '+' :
  96.     case '=' :
  97.         if (operation != '=' && !operatorLast)
  98.         {
  99.             switch (operation)
  100.             {
  101.             case '/' :
  102.                 if (operand2 != 0L)
  103.                     operand2 = operand1 / operand2;
  104.                 else
  105.                     operand2 = 100000000L;
  106.                 break;
  107.  
  108.             case '*' :
  109.                 operand2 *= operand1;
  110.                 break;
  111.  
  112.             case '-' :
  113.                 operand2 = operand1 - operand2;
  114.                 break;
  115.  
  116.             case '+' :
  117.                 operand2 += operand1;
  118.                 break;
  119.  
  120.             }
  121.             operand1 = 0L;
  122.         }
  123.         operatorLast = TRUE;
  124.         operation = button->hotKey;
  125.         break;
  126.  
  127.     // Digit pressed.
  128.     default:
  129.         if (operatorLast)
  130.         {
  131.             operatorLast = FALSE;
  132.             operand1 = operand2;
  133.             operand2 = 0L;
  134.         }
  135.         if (operand2 < 10000000L)
  136.             operand2 = operand2 * 10 + button->hotKey - '0';
  137.         break;
  138.     }
  139.  
  140.     // Check for out of range numbers.
  141.     if (operand2 >= 100000000L)
  142.     {
  143.         operand2 =
  144.             operand1 = 0L;
  145.         operation = '=';
  146.     }
  147.  
  148.     // Update the displayed number.
  149.     numberField->DataSet(NULL);
  150. }
  151.  
  152. #pragma argsused
  153. void Calculator(void *item, UI_EVENT &event)
  154. {
  155.     // Create the calculator in the screen center.
  156.     int left = _display->columns / _display->cellWidth / 2 - 10;
  157.     int top = _display->lines / _display->cellHeight / 2 - 5;
  158.     *_windowManager + new CALCULATOR(left, top, "Zinc Calc");
  159. }
  160.  
  161.  
  162.