home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / dos / tools / aurora21 / calcpad.aml < prev    next >
Text File  |  1995-08-10  |  9KB  |  349 lines

  1. /* ------------------------------------------------------------------ */
  2. /* Macro:        CALCPAD.AML                                          */
  3. /* Written by:   nuText Systems                                       */
  4. /*                                                                    */
  5. /* Description:  This macro displays a simple calculator pad with     */
  6. /*               basic arithmetic operations, a memory, and four      */
  7. /*               display lines. Floating point numbers are not        */
  8. /*               supported. The number on the bottommost display      */
  9. /*               line can be entered into the text of the current     */
  10. /*               edit window.                                         */
  11. /*                                                                    */
  12. /* Usage:        Select this macro from the 'Sample Utility Macros'   */
  13. /*               menu or from the macro picklist <shift f12>. Numbers */
  14. /*               and operations can be entered by using the keyboard  */
  15. /*               or the mouse.                                        */
  16. /*                                                                    */
  17. /*               If you use this macro regularly, it may be           */
  18. /*               convenient to assign it to a key. For example:       */
  19. /*                                                                    */
  20. /*                 object edit                                        */
  21. /*                    :                                               */
  22. /*                   key <alt k>                                      */
  23. /*                     runmac "CALCPAD"                               */
  24. /*                   end                                              */
  25. /* ------------------------------------------------------------------ */
  26.  
  27.   include bootpath "define.aml"
  28.  
  29.   // define the colors to use
  30.   define
  31.     set calc_display_color         color darkgray  on green
  32.     set calc_display_curr_color    color black     on green
  33.     set calc_button_color          color black     on green
  34.     set calc_button_hilite_color   color black     on brightgreen
  35.     set calc_client_color          color black     on gray
  36.     set calc_border_color          color white     on gray
  37.     set calc_control_color         color yellow
  38.   end
  39.  
  40.   // keep this object resident
  41.   stayresident
  42.   inheritfrom "win"
  43.  
  44.   // display lines
  45.   var line1
  46.   var line2
  47.   var line3
  48.   var line4
  49.  
  50.   // line number of last lbutton click
  51.   var line
  52.   var bcol
  53.  
  54.   // display operators
  55.   var op0
  56.   var op1
  57.   var op2
  58.   var op3
  59.   var op4
  60.  
  61.   // numeric base
  62.   radix = 10
  63.  
  64.   // clear the calculator display
  65.   function cleardisp
  66.     line1 = ''
  67.     line2 = ''
  68.     line3 = ''
  69.     line4 = 0
  70.     op0 = ' '
  71.     op1 = ' '
  72.     op2 = ' '
  73.     op3 = ' '
  74.     op4 = ' '
  75.   end
  76.  
  77.   // scroll the display upwards
  78.   function scrollup
  79.     op0   = op1
  80.     line1 = line2
  81.     op1   = op2
  82.     line2 = line3
  83.     op2   = op3
  84.     line3 = line4
  85.     op3   = op4
  86.     line4 = 0
  87.     op4   = ' '
  88.   end
  89.  
  90.   // perform the operation currently on the display
  91.   function result
  92.     value = case op3
  93.               when '+'  line3 + line4
  94.               when '-'  line3 - line4
  95.               when '*'  line3 * line4
  96.               when '/'  line3 / line4
  97.               when 'm'  line3 mod line4
  98.               when '%'  (line3 * line4) / 100
  99.             end
  100.     scrollup
  101.     line4 = value
  102.   end
  103.  
  104.  
  105.   // create the calculator window
  106.   createwindow
  107.   setframe ">b"
  108.   setcolor  border_color    calc_border_color
  109.   setcolor  text_color      calc_client_color
  110.   setcolor  control_color   calc_control_color
  111.   setcolor  border_flash_color   (color brightgreen on gray)
  112.   settitle "Calculator"
  113.   setwinctrl '≡'
  114.  
  115.   width = 36
  116.   height = 15
  117.  
  118.   // center the window
  119.   ox = (getvidcols - width) / 2
  120.   oy = (getvidrows - height) / 2
  121.   sizewindow ox oy ox + width oy + height "ad"
  122.  
  123.   setborder "1i"
  124.   setshadow 2 1
  125.  
  126.   // define strings to display in the keypad
  127.   keystr = "  C        %   mod   /    CE   7    8    9    *    MR   4    5    6    -    MS   1    2    3    +    M+   0       +/-   =  "
  128.  
  129.   x = 0
  130.   y = 0
  131.  
  132.   // draw the keypad
  133.   gotoxy 3 7
  134.   while y < 5 do
  135.     while x < 5 do
  136.       writestr keystr [y * 25 + (x * 5) + 1 : 5]  calc_button_color
  137.       writestr "  "
  138.       x = x + 1
  139.     end
  140.     x = 0
  141.     y = y + 1
  142.     if y < 5 then
  143.       writeline
  144.       writeline
  145.       gotoxy 3
  146.     end
  147.   end
  148.  
  149.   y = 0
  150.   memory = ''
  151.  
  152.   // clear the display
  153.   cleardisp
  154.  
  155.   function draw
  156.     // draw the calculator display
  157.     gotoxy 3 2
  158.     writestr op0 + (pad (thousands line1) 32 'r') calc_display_color
  159.     gotoxy 3 3
  160.     writestr op1 + (pad (thousands line2) 32 'r') calc_display_color
  161.     gotoxy 3 4
  162.     writestr op2 + (pad (thousands line3) 32 'r') calc_display_color
  163.     gotoxy 3 5
  164.     writestr (if? op3 == '=' ' ' op3) + (pad (thousands line4) 32 'r')
  165.              calc_display_curr_color
  166.   end
  167.  
  168.   draw
  169.  
  170.   function close
  171.     // call 'close' in object 'win'
  172.     pass
  173.     destroyobject
  174.   end
  175.  
  176.   function "≡"
  177.     close
  178.   end
  179.  
  180.   // exit the calculator
  181.   key <esc>
  182.     close
  183.   end
  184.  
  185.   // mouse click
  186.   function <lbutton>
  187.  
  188.     // first pass on mouse events to the library
  189.     pass
  190.  
  191.     case getregion
  192.  
  193.       // client area
  194.       // translate mouse click to keypress
  195.       when 1
  196.         line = virtorow
  197.         buttonline = case line
  198.                        when 7  "cb%m/"
  199.                        when 9  "e789*"
  200.                        when 11 "r456-"
  201.                        when 13 "s123+"
  202.                        when 15 "z0ti="
  203.                        otherwise ''
  204.                      end
  205.  
  206.         if buttonline then
  207.           column = virtocol
  208.           bcol = if     column >= 3  and column <= 7  then 1
  209.                  elseif column >= 10 and column <= 14 then 2
  210.                  elseif column >= 17 and column <= 21 then 3
  211.                  elseif column >= 24 and column <= 28 then 4
  212.                  elseif column >= 31 and column <= 35 then 5
  213.                  end
  214.  
  215.           if bcol then
  216.             buttonchar = buttonline [bcol]
  217.             hilite 5 1 (calc_button_hilite_color) (bcol * 7) - 4  line
  218.  
  219.             // simulate the appropriate keypress
  220.             if buttonchar then
  221.               queuekey (getkeycode '<'+ buttonchar + '>')
  222.             end
  223.           end
  224.         end
  225.     end
  226.   end
  227.  
  228.   function <lbuttonup>
  229.     if bcol then
  230.       hilite 5 1 (calc_button_color) (bcol * 7) - 4  line
  231.       bcol = ''
  232.     end
  233.     pass
  234.   end
  235.  
  236.   // clear the display
  237.   key <del>
  238.     cleardisp
  239.  
  240.   // enter into text
  241.   key <ctrl enter>
  242.     send "≡"
  243.     queue "write" (thousands line4)
  244.  
  245.   // backspace
  246.   key <backspace>
  247.     line4 = line4 / radix
  248.     op4 = ' '
  249.     op3 = ' '
  250.     draw
  251.   end
  252.  
  253.   key <enter>
  254.     if op3 <> ' ' then
  255.       if op3 == '=' then
  256.         op4 = op2
  257.         scrollup
  258.         line4 = line2
  259.       end
  260.       op4 = '='
  261.       result
  262.       draw
  263.     end
  264.   end
  265.  
  266.   // enter numeric characters
  267.   key <char> (c)
  268.     if pos c "0123456789" then
  269.       if c < radix then
  270.         if op3 == '=' then
  271.           scrollup
  272.         end
  273.         positive = line4 >= 0
  274.         oldline = line4
  275.         line4 = line4 * radix + c
  276.         // test for overflow
  277.         if positive <> (line4 > 0) then
  278.           line4 = oldline
  279.         end
  280.         draw
  281.       end
  282.  
  283.       // operators
  284.     elseif pos c "+-*/%m" then
  285.       if op3 <> ' ' and op3 <> '=' then
  286.         op4 = '='
  287.         result
  288.       end
  289.       op4 = c
  290.       scrollup
  291.       draw
  292.  
  293.     elseif c == '=' then
  294.       send <enter>
  295.  
  296.     // do the current operation
  297.     else
  298.       case c
  299.  
  300.         when 'c'
  301.           cleardisp
  302.  
  303.         // clear the entry line
  304.         when 'e'
  305.           line4 = 0
  306.           op4 = ' '
  307.           op3 = ' '
  308.  
  309.         // store
  310.         when 's'
  311.           memory = line4
  312.  
  313.         // recall
  314.         when 'r'
  315.           line4 = memory
  316.  
  317.         // store + memory
  318.         when 'z'
  319.           memory = memory + line4
  320.  
  321.         // change sign
  322.         when 'i'
  323.           line4 = -line4
  324.           op4 = ' '
  325.           op3 = ' '
  326.  
  327.         // enter into text
  328.         when 't'
  329.           send <ctrl enter>
  330.           return
  331.  
  332.         // backspace
  333.         when 'b'
  334.           send <backspace>
  335.  
  336.         // unrecognized key
  337.         otherwise
  338.           send <otherkey>
  339.       end
  340.       draw
  341.     end
  342.   end
  343.  
  344.   // unrecognized key
  345.   key <otherkey>
  346.     beep 200 70
  347.   end
  348.  
  349.