home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / CALC.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  11.7 KB  |  442 lines

  1. # $Header:   P:/source/ppee/macros/calc.pev   1.13   20 Jul 1990 16:42:02   ericj  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   calc.pel  $: Support for calculator
  20.  
  21.  
  22.  
  23.  
  24. local CALC_COLOR_TEXT = 0x70
  25. local CALC_COLOR_BORDER = 0x71
  26.  
  27.  
  28. local calc_window;
  29. local calc_result = 0;
  30. local calc_operand = 0;
  31. local calc_current_operator = "="
  32. local calc_keymap
  33.  
  34. local calc_operand_digits = 0;
  35.  
  36. local MAX_CALC_DIGITS = 9;
  37. local CALC_DISPLAY_X = 1
  38. local CALC_DISPLAY_Y = 16
  39. local CALC_DISPLAY_WIDTH = 22
  40. local calc_array[];
  41. local calc_options[];
  42. local calc_operators[];
  43. local calc_initialized = 0;
  44. local calc_mode = "Dec";        # "Dec" or "Hex" or "Oct"
  45. local calc_store = 0;
  46. local calc_x0 = 20;
  47. local calc_y0 = 4;
  48.  
  49.  
  50.  
  51. function calc(){
  52.  
  53.     if (!calc_initialized) {
  54.         calc_initialized = 1
  55.  
  56.         # ROW 0
  57.         calc_array[0,0] = " A "
  58.         calc_array[0,1] = " B "
  59.         calc_array[0,2] = " C "
  60.         calc_array[0,3] = " D "
  61.         calc_array[0,4] = " E "
  62.         calc_array[0,5] = " F "
  63.         calc_array[0,6] = "Clr"
  64.  
  65.         # ROW 1
  66.         calc_array[1,0] = "Shf"
  67.         calc_array[1,1] = " 7 "
  68.         calc_array[1,2] = " 8 "
  69.         calc_array[1,3] = " 9 "
  70.         calc_array[1,4] = "sTo"
  71.         calc_array[1,5] = "Rcl"
  72.         calc_array[1,6] = "oFF"
  73.  
  74.         # ROW 2 
  75.         calc_array[2,0] = "Oct"
  76.         calc_array[2,1] = " 4 "
  77.         calc_array[2,2] = " 5 "
  78.         calc_array[2,3] = " 6 "
  79.         calc_array[2,4] = " x "
  80.         calc_array[2,5] = " | "
  81.         calc_array[2,6] = " & "
  82.  
  83.         # ROW 3 
  84.         calc_array[3,0] = "Hex"
  85.         calc_array[3,1] = " 1 "
  86.         calc_array[3,2] = " 2 "
  87.         calc_array[3,3] = " 3 "
  88.         calc_array[3,4] = " - "
  89.         calc_array[3,5] = "DIV"
  90.         calc_array[3,6] = "MOD"
  91.  
  92.         # ROW 4
  93.         calc_array[4,0] = "Dec"
  94.         calc_array[4,1] = " 0 "
  95.         calc_array[4,2] = " = "
  96.         calc_array[4,3] = "SCP"
  97.         calc_array[4,4] = " + "
  98.         calc_array[4,5] = " ± "
  99.         calc_array[4,6] = "1sC"
  100.  
  101.         calc_operators[ "+" ]   = 0;
  102.         calc_operators[ "-" ]   = 0;
  103.         calc_operators[ "x" ]   = 0;
  104.         calc_operators[ "÷" ]   = 0;
  105.         calc_operators[ "&" ]   = 0;
  106.         calc_operators[ "|" ]   = 0;
  107.         calc_operators[ "=" ]   = 0;
  108.         calc_operators[ "DIV" ] = 0;
  109.         calc_operators[ "MOD" ] = 0;
  110.         calc_operators[ "Shf" ] = 0;
  111.  
  112.         calc_options[ "Clr" ] = 0;
  113.         calc_options[ "oFF" ] = 0;
  114.         calc_options[ "Dec" ] = 0;
  115.         calc_options[ "Hex" ] = 0;
  116.         calc_options[ "Oct" ] = 0;
  117.         calc_options[ "sTo" ] = 0;
  118.         calc_options[ "Rcl" ] = 0;
  119.         calc_options[ "1sC" ] = 0;
  120.         calc_options[ "SCP" ] = 0;
  121.         calc_options[ "±" ]   = 0;
  122.  
  123.         # initialize keymap here
  124.         #
  125.         push_keymap();
  126.         calc_keymap = current_keymap = create_keymap( empty_keymap );
  127.  
  128.         assign_key( "#61440", "calc_left_press" );
  129.         assign_key( "#62976", "calc_left_click" );
  130.         assign_key( "#62208", "calc_left_release" );
  131.         assign_key( "<ESC>",  "process_end" );
  132.  
  133.         assign_key( "a",      "process_calc_key A" );
  134.         assign_key( "A",      "process_calc_key A" );
  135.         assign_key( "b",      "process_calc_key B" );
  136.         assign_key( "B",      "process_calc_key B" );
  137.         assign_key( "c",      "process_calc_key C" );
  138.         assign_key( "C",      "process_calc_key C" );
  139.         assign_key( "d",      "process_calc_key D" );
  140.         assign_key( "D",      "process_calc_key D" );
  141.         assign_key( "e",      "process_calc_key E" );
  142.         assign_key( "E",      "process_calc_key E" );
  143.         assign_key( "f",      "process_calc_key F" );
  144.         assign_key( "F",      "process_calc_key F" );
  145.  
  146.         assign_key( "0",      "process_calc_key 0" );
  147.         assign_key( "1",      "process_calc_key 1" );
  148.         assign_key( "2",      "process_calc_key 2" );
  149.         assign_key( "3",      "process_calc_key 3" );
  150.         assign_key( "4",      "process_calc_key 4" );
  151.         assign_key( "5",      "process_calc_key 5" );
  152.         assign_key( "6",      "process_calc_key 6" );
  153.         assign_key( "7",      "process_calc_key 7" );
  154.         assign_key( "8",      "process_calc_key 8" );
  155.         assign_key( "9",      "process_calc_key 9" );
  156.  
  157.         assign_key( "-",       "process_calc_key -" );
  158.         assign_key( "+",       "process_calc_key +" );
  159.         assign_key( "x",       "process_calc_key x" );
  160.         assign_key( "X",       "process_calc_key x" );
  161.         assign_key( "*",       "process_calc_key x" );
  162.         assign_key( "&",       "process_calc_key &" );
  163.         assign_key( "|",       "process_calc_key |" );
  164.         assign_key( "<ENTER>", "process_calc_key =" );
  165.         assign_key( "=",       "process_calc_key =" );
  166.         assign_key( "/",       "process_calc_key DIV" );
  167.         assign_key( "%",       "process_calc_key MOD" );
  168.         assign_key( "<INS>",   "process_calc_key SCP" );
  169.         assign_key( "<ctrl-s>","process_calc_key Shf" );
  170.         assign_key( "<ctrl-d>","process_calc_key Dec" );
  171.         assign_key( "<ctrl-h>","process_calc_key Hex" );
  172.         assign_key( "<ctrl-o>","process_calc_key Oct" );
  173.         assign_key( "!",       "process_calc_key 1sC" );
  174.         assign_key( "#3103",   "process_calc_key ±" );        #ctrl-minus
  175.         assign_key( "<ctrl-r>","process_calc_key Rcl" );
  176.         assign_key( "<ctrl-t>","process_calc_key sTo" );
  177.         assign_key( "<ctrl-c>","process_calc_key Clr" );
  178.         assign_key( "<ctrl-f>","process_calc_key oFF" );
  179.  
  180.         assign_key( "<alt-h>", "display_help_item calculator" );
  181.  
  182.         pop_keymap();
  183.     }
  184.  
  185.  
  186.     current_window = calc_window = 
  187.         create_factory_window( calc_x0, calc_y0,37,19,
  188.             WINDOW_SYSTEM+WINDOW_BORDER);
  189.     color_text   = CALC_COLOR_TEXT;
  190.     color_border = CALC_COLOR_BORDER;
  191.  
  192.     calc_draw();
  193.     calc_display_result()
  194.     push_keymap( calc_keymap );
  195.  
  196.     
  197.     process_begin();
  198.  
  199.  
  200.     pop_keymap()
  201.  
  202.     calc_x0 = window_x0;
  203.     calc_y0 = window_y0;
  204.     delete_window( calc_window );
  205. }
  206.  
  207.  
  208. function calc_left_press(){
  209.     local    pos;
  210.  
  211.     if (!window_contains( mouse_event_x, mouse_event_y, calc_window) &&
  212.         !window_border_contains( mouse_event_x, mouse_event_y, calc_window))
  213.         return;
  214.  
  215.     pos = mouse_position( 1 );
  216.  
  217.     if ((pos == MOUSE_N+MOUSE_W) || (pos == MOUSE_N))
  218.         left_press(1);
  219.     else
  220.         calc_left_click();
  221. }
  222.  
  223.  
  224. local calc_selection = ""
  225. local calc_x = 0;
  226. local calc_y = 0;
  227.  
  228. function calc_left_release(){
  229.     # highlight_window( calc_x * 5, calc_y * 3, 5, 3, color_text );
  230. }
  231.  
  232. function calc_left_click(){
  233.     local    key = current_key;
  234.  
  235.     if (!window_contains( mouse_event_x, mouse_event_y, calc_window) &&
  236.         !window_border_contains( mouse_event_x, mouse_event_y, calc_window))
  237.         return;
  238.  
  239.     calc_x = (mouse_event_x - window_text_x0) / 5;
  240.     calc_y = (mouse_event_y - window_text_y0) / 3;
  241.     calc_selection = calc_array[ calc_y, calc_x ];
  242.  
  243.     calc_selection = trim(ltrim(calc_selection));
  244.  
  245.     # special handling for the "=" since it takes up two 
  246.     # entry locations
  247.     #
  248.     if ((calc_selection == "=") && (calc_x == 3))
  249.         calc_x = 2;
  250.  
  251.     if (calc_y <= 4){
  252. #        highlight_window( calc_x * 5, calc_y * 3, 5, 3, 
  253. #            shiftl(and(color_text,0x0F),4) + \
  254. #            shiftr(and(color_text,0xF0),4) );
  255.  
  256.  
  257.         process_calc_key( calc_selection );
  258.  
  259.         if (key == LEFT_CLICK)
  260.             calc_left_release()
  261.     }
  262.     
  263. }
  264.  
  265. function process_calc_key( sel ){
  266.  
  267.     if (sel in calc_options) {
  268.         if (sel == "Clr") {
  269.             calc_result  = 0;
  270.             calc_operand = 0;
  271.             calc_operand_digits = 0;
  272.             calc_current_operator = "=";
  273.         } else if (sel == "oFF") {
  274.             process_end();
  275.         } else if (sel == "Dec") {
  276.             calc_mode = sel;
  277.         } else if (sel == "Hex") {
  278.             calc_mode = sel;
  279.         } else if (sel == "Oct") {
  280.             calc_mode = sel;
  281.         } else if (sel == "sTo") {
  282.             calc_store = calc_display_value();
  283.         } else if (sel == "Rcl") {
  284.             calc_operand = calc_store;
  285.             calc_operand_digits = 1;
  286.         } else if (sel == "1sC") {
  287.             if (calc_operand_digits)
  288.                 calc_operand = xor( calc_operand, 0xFFFFFFFF );
  289.             else
  290.                 calc_result = xor( calc_result, 0xFFFFFFFF );
  291.         } else if (sel == "SCP") {
  292.             copy_string_to_scrap( calc_display_result() )
  293.         } else if (sel == "±") {
  294.             if (calc_operand_digits)
  295.                 calc_operand = -calc_operand
  296.             else
  297.                 calc_result  = -calc_result
  298.         }
  299.         calc_display_result();
  300.     } else if (sel in calc_operators) {
  301.         calculate_result( sel )
  302.         calc_operand = calc_operand_digits = 0;
  303.         calc_display_result();
  304.     } else if (sel ~ /[0-9A-F]/) {
  305.         if (++calc_operand_digits > MAX_CALC_DIGITS) {
  306.             beep();
  307.             return;
  308.         }
  309.         if (sel ~ /[0-9]/) {
  310.             if (calc_mode == "Dec")
  311.                 calc_operand = (calc_operand * 10) + (sel - "0");
  312.             else if (calc_mode == "Hex")
  313.                 calc_operand = (calc_operand * 16) + (sel - "0");
  314.             else if ((calc_mode == "Oct") && (sel ~ /[0-7]/))
  315.                 calc_operand = (calc_operand * 8) + (sel - "0");
  316.             else {
  317.                 calc_operand_digits--;
  318.                 beep();
  319.             }
  320.         } else if (sel ~ /[A-F]/) {
  321.             if (calc_mode == "Hex")
  322.                 calc_operand = (calc_operand * 16) + ((ord(sel) - ord("A")) + 10);
  323.             else {
  324.                 calc_operand_digits--;
  325.                 beep();
  326.             }
  327.         } else {
  328.             calc_operand_digits--;
  329.             beep();
  330.         }
  331.         calc_display_result();
  332.     } else {
  333.         beep();
  334.         return;
  335.     }
  336. }
  337.  
  338.  
  339.  
  340. function calc_draw(){
  341.     window_cursor_x = window_cursor_y = 0
  342.     write_window(    \
  343.             "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐╔═══╗"\
  344.             "│ A ││ B ││ C ││ D ││ E ││ F │║Clr║"\
  345.             "└───┘└───┘└───┘└───┘└───┘└───┘╚═══╝"\
  346.             "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐╔═══╗"\
  347.             "│Shf││ 7 ││ 8 ││ 9 ││sTo││Rcl│║oFF║"\
  348.             "└───┘└───┘└───┘└───┘└───┘└───┘╚═══╝"\
  349.             "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐"\
  350.             "│Oct││ 4 ││ 5 ││ 6 ││ x ││ | ││ & │"\
  351.             "└───┘└───┘└───┘└───┘└───┘└───┘└───┘"\
  352.             "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐"\
  353.             "│Hex││ 1 ││ 2 ││ 3 ││ - ││DIV││MOD│"\
  354.             "└───┘└───┘└───┘└───┘└───┘└───┘└───┘"\
  355.             "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐"\
  356.             "│Dec││ 0 ││ = ││SCP││ + ││ ± ││1sC│"\
  357.             "└───┘└───┘└───┘└───┘└───┘└───┘└───┘"\
  358.             "                                   "\
  359.             "                                   ");
  360.  
  361. }
  362.  
  363. function calc_display_result(){
  364.     local str;
  365.     local str1;
  366.     local spaces;
  367.     local value        = calc_display_value()
  368.     local sprintf_char = (calc_mode == "Dec") \
  369.                 ? "ld" \
  370.                 : (calc_mode == "Oct") \
  371.                     ? "lO"\
  372.                     : "lX";
  373.  
  374.     str = str1 = sprintf( "%" sprintf_char, value);
  375.  
  376.     str = sprintf( "%" (CALC_DISPLAY_WIDTH - length(str)) "s%s  (%s)", \
  377.         " ", \
  378.         str, \
  379.         calc_mode );
  380.  
  381.     window_cursor_x = CALC_DISPLAY_X;
  382.     window_cursor_y = CALC_DISPLAY_Y;
  383.     write_window( str );
  384.     return str1;
  385. }
  386.  
  387.  
  388. function calculate_result( sel ){
  389.     local op = calc_current_operator;
  390.     local res_digits
  391.     
  392.     calc_current_operator = sel;
  393.  
  394.     #
  395.     # perform the pervious operation on the result and operand
  396.     #
  397.     if (!calc_operand_digits){
  398.         return;
  399.     } else if (op == "+") {
  400.         calc_operand  = calc_result + calc_operand;
  401.     } else if (op == "-") {
  402.         calc_operand  = calc_result - calc_operand;
  403.     } else if (op == "x") {
  404.         calc_operand  = calc_result * calc_operand;
  405.     } else if (op == "|") {
  406.         calc_operand = or( calc_operand, calc_result );
  407.     } else if (op == "&") {
  408.         calc_operand = and( calc_operand, calc_result );
  409.     } else if (op == "Shf") {
  410.         if (calc_operand < 0) {
  411.             calc_operand = shiftr( calc_result,-calc_operand);
  412.         } else {
  413.             calc_operand = shiftl( calc_result,calc_operand);
  414.         }
  415.     } else if (op == "DIV") {
  416.         if (calc_result)
  417.             calc_operand = calc_result / calc_operand;
  418.     } else if (op == "MOD") {
  419.         if (calc_result)
  420.             calc_operand = calc_result % calc_operand;
  421.     }
  422.     calc_result = calc_operand;
  423. }
  424.  
  425.  
  426. local function calc_display_value(){
  427.     return  (calc_operand_digits) ? calc_operand : calc_result;
  428. }
  429.  
  430.  
  431.  
  432. local function copy_string_to_scrap( str ){
  433.     current_buffer = create_buffer( "", "", 1 );
  434.     insert_string( str );
  435.     drop_anchor( 1 );
  436.     goto_bol();
  437.     copy_to_scrap();
  438.     buffer_flags = 0;
  439.     delete_buffer();
  440. }
  441.  
  442.