home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/calc.pev 1.13 20 Jul 1990 16:42:02 ericj $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: calc.pel $: Support for calculator
-
-
-
-
- local CALC_COLOR_TEXT = 0x70
- local CALC_COLOR_BORDER = 0x71
-
-
- local calc_window;
- local calc_result = 0;
- local calc_operand = 0;
- local calc_current_operator = "="
- local calc_keymap
-
- local calc_operand_digits = 0;
-
- local MAX_CALC_DIGITS = 9;
- local CALC_DISPLAY_X = 1
- local CALC_DISPLAY_Y = 16
- local CALC_DISPLAY_WIDTH = 22
- local calc_array[];
- local calc_options[];
- local calc_operators[];
- local calc_initialized = 0;
- local calc_mode = "Dec"; # "Dec" or "Hex" or "Oct"
- local calc_store = 0;
- local calc_x0 = 20;
- local calc_y0 = 4;
-
-
-
- function calc(){
-
- if (!calc_initialized) {
- calc_initialized = 1
-
- # ROW 0
- calc_array[0,0] = " A "
- calc_array[0,1] = " B "
- calc_array[0,2] = " C "
- calc_array[0,3] = " D "
- calc_array[0,4] = " E "
- calc_array[0,5] = " F "
- calc_array[0,6] = "Clr"
-
- # ROW 1
- calc_array[1,0] = "Shf"
- calc_array[1,1] = " 7 "
- calc_array[1,2] = " 8 "
- calc_array[1,3] = " 9 "
- calc_array[1,4] = "sTo"
- calc_array[1,5] = "Rcl"
- calc_array[1,6] = "oFF"
-
- # ROW 2
- calc_array[2,0] = "Oct"
- calc_array[2,1] = " 4 "
- calc_array[2,2] = " 5 "
- calc_array[2,3] = " 6 "
- calc_array[2,4] = " x "
- calc_array[2,5] = " | "
- calc_array[2,6] = " & "
-
- # ROW 3
- calc_array[3,0] = "Hex"
- calc_array[3,1] = " 1 "
- calc_array[3,2] = " 2 "
- calc_array[3,3] = " 3 "
- calc_array[3,4] = " - "
- calc_array[3,5] = "DIV"
- calc_array[3,6] = "MOD"
-
- # ROW 4
- calc_array[4,0] = "Dec"
- calc_array[4,1] = " 0 "
- calc_array[4,2] = " = "
- calc_array[4,3] = "SCP"
- calc_array[4,4] = " + "
- calc_array[4,5] = " ± "
- calc_array[4,6] = "1sC"
-
- calc_operators[ "+" ] = 0;
- calc_operators[ "-" ] = 0;
- calc_operators[ "x" ] = 0;
- calc_operators[ "÷" ] = 0;
- calc_operators[ "&" ] = 0;
- calc_operators[ "|" ] = 0;
- calc_operators[ "=" ] = 0;
- calc_operators[ "DIV" ] = 0;
- calc_operators[ "MOD" ] = 0;
- calc_operators[ "Shf" ] = 0;
-
- calc_options[ "Clr" ] = 0;
- calc_options[ "oFF" ] = 0;
- calc_options[ "Dec" ] = 0;
- calc_options[ "Hex" ] = 0;
- calc_options[ "Oct" ] = 0;
- calc_options[ "sTo" ] = 0;
- calc_options[ "Rcl" ] = 0;
- calc_options[ "1sC" ] = 0;
- calc_options[ "SCP" ] = 0;
- calc_options[ "±" ] = 0;
-
- # initialize keymap here
- #
- push_keymap();
- calc_keymap = current_keymap = create_keymap( empty_keymap );
-
- assign_key( "#61440", "calc_left_press" );
- assign_key( "#62976", "calc_left_click" );
- assign_key( "#62208", "calc_left_release" );
- assign_key( "<ESC>", "process_end" );
-
- assign_key( "a", "process_calc_key A" );
- assign_key( "A", "process_calc_key A" );
- assign_key( "b", "process_calc_key B" );
- assign_key( "B", "process_calc_key B" );
- assign_key( "c", "process_calc_key C" );
- assign_key( "C", "process_calc_key C" );
- assign_key( "d", "process_calc_key D" );
- assign_key( "D", "process_calc_key D" );
- assign_key( "e", "process_calc_key E" );
- assign_key( "E", "process_calc_key E" );
- assign_key( "f", "process_calc_key F" );
- assign_key( "F", "process_calc_key F" );
-
- assign_key( "0", "process_calc_key 0" );
- assign_key( "1", "process_calc_key 1" );
- assign_key( "2", "process_calc_key 2" );
- assign_key( "3", "process_calc_key 3" );
- assign_key( "4", "process_calc_key 4" );
- assign_key( "5", "process_calc_key 5" );
- assign_key( "6", "process_calc_key 6" );
- assign_key( "7", "process_calc_key 7" );
- assign_key( "8", "process_calc_key 8" );
- assign_key( "9", "process_calc_key 9" );
-
- assign_key( "-", "process_calc_key -" );
- assign_key( "+", "process_calc_key +" );
- assign_key( "x", "process_calc_key x" );
- assign_key( "X", "process_calc_key x" );
- assign_key( "*", "process_calc_key x" );
- assign_key( "&", "process_calc_key &" );
- assign_key( "|", "process_calc_key |" );
- assign_key( "<ENTER>", "process_calc_key =" );
- assign_key( "=", "process_calc_key =" );
- assign_key( "/", "process_calc_key DIV" );
- assign_key( "%", "process_calc_key MOD" );
- assign_key( "<INS>", "process_calc_key SCP" );
- assign_key( "<ctrl-s>","process_calc_key Shf" );
- assign_key( "<ctrl-d>","process_calc_key Dec" );
- assign_key( "<ctrl-h>","process_calc_key Hex" );
- assign_key( "<ctrl-o>","process_calc_key Oct" );
- assign_key( "!", "process_calc_key 1sC" );
- assign_key( "#3103", "process_calc_key ±" ); #ctrl-minus
- assign_key( "<ctrl-r>","process_calc_key Rcl" );
- assign_key( "<ctrl-t>","process_calc_key sTo" );
- assign_key( "<ctrl-c>","process_calc_key Clr" );
- assign_key( "<ctrl-f>","process_calc_key oFF" );
-
- assign_key( "<alt-h>", "display_help_item calculator" );
-
- pop_keymap();
- }
-
-
- current_window = calc_window =
- create_factory_window( calc_x0, calc_y0,37,19,
- WINDOW_SYSTEM+WINDOW_BORDER);
- color_text = CALC_COLOR_TEXT;
- color_border = CALC_COLOR_BORDER;
-
- calc_draw();
- calc_display_result()
- push_keymap( calc_keymap );
-
-
- process_begin();
-
-
- pop_keymap()
-
- calc_x0 = window_x0;
- calc_y0 = window_y0;
- delete_window( calc_window );
- }
-
-
- function calc_left_press(){
- local pos;
-
- if (!window_contains( mouse_event_x, mouse_event_y, calc_window) &&
- !window_border_contains( mouse_event_x, mouse_event_y, calc_window))
- return;
-
- pos = mouse_position( 1 );
-
- if ((pos == MOUSE_N+MOUSE_W) || (pos == MOUSE_N))
- left_press(1);
- else
- calc_left_click();
- }
-
-
- local calc_selection = ""
- local calc_x = 0;
- local calc_y = 0;
-
- function calc_left_release(){
- # highlight_window( calc_x * 5, calc_y * 3, 5, 3, color_text );
- }
-
- function calc_left_click(){
- local key = current_key;
-
- if (!window_contains( mouse_event_x, mouse_event_y, calc_window) &&
- !window_border_contains( mouse_event_x, mouse_event_y, calc_window))
- return;
-
- calc_x = (mouse_event_x - window_text_x0) / 5;
- calc_y = (mouse_event_y - window_text_y0) / 3;
- calc_selection = calc_array[ calc_y, calc_x ];
-
- calc_selection = trim(ltrim(calc_selection));
-
- # special handling for the "=" since it takes up two
- # entry locations
- #
- if ((calc_selection == "=") && (calc_x == 3))
- calc_x = 2;
-
- if (calc_y <= 4){
- # highlight_window( calc_x * 5, calc_y * 3, 5, 3,
- # shiftl(and(color_text,0x0F),4) + \
- # shiftr(and(color_text,0xF0),4) );
-
-
- process_calc_key( calc_selection );
-
- if (key == LEFT_CLICK)
- calc_left_release()
- }
-
- }
-
- function process_calc_key( sel ){
-
- if (sel in calc_options) {
- if (sel == "Clr") {
- calc_result = 0;
- calc_operand = 0;
- calc_operand_digits = 0;
- calc_current_operator = "=";
- } else if (sel == "oFF") {
- process_end();
- } else if (sel == "Dec") {
- calc_mode = sel;
- } else if (sel == "Hex") {
- calc_mode = sel;
- } else if (sel == "Oct") {
- calc_mode = sel;
- } else if (sel == "sTo") {
- calc_store = calc_display_value();
- } else if (sel == "Rcl") {
- calc_operand = calc_store;
- calc_operand_digits = 1;
- } else if (sel == "1sC") {
- if (calc_operand_digits)
- calc_operand = xor( calc_operand, 0xFFFFFFFF );
- else
- calc_result = xor( calc_result, 0xFFFFFFFF );
- } else if (sel == "SCP") {
- copy_string_to_scrap( calc_display_result() )
- } else if (sel == "±") {
- if (calc_operand_digits)
- calc_operand = -calc_operand
- else
- calc_result = -calc_result
- }
- calc_display_result();
- } else if (sel in calc_operators) {
- calculate_result( sel )
- calc_operand = calc_operand_digits = 0;
- calc_display_result();
- } else if (sel ~ /[0-9A-F]/) {
- if (++calc_operand_digits > MAX_CALC_DIGITS) {
- beep();
- return;
- }
- if (sel ~ /[0-9]/) {
- if (calc_mode == "Dec")
- calc_operand = (calc_operand * 10) + (sel - "0");
- else if (calc_mode == "Hex")
- calc_operand = (calc_operand * 16) + (sel - "0");
- else if ((calc_mode == "Oct") && (sel ~ /[0-7]/))
- calc_operand = (calc_operand * 8) + (sel - "0");
- else {
- calc_operand_digits--;
- beep();
- }
- } else if (sel ~ /[A-F]/) {
- if (calc_mode == "Hex")
- calc_operand = (calc_operand * 16) + ((ord(sel) - ord("A")) + 10);
- else {
- calc_operand_digits--;
- beep();
- }
- } else {
- calc_operand_digits--;
- beep();
- }
- calc_display_result();
- } else {
- beep();
- return;
- }
- }
-
-
-
- function calc_draw(){
- window_cursor_x = window_cursor_y = 0
- write_window( \
- "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐╔═══╗"\
- "│ A ││ B ││ C ││ D ││ E ││ F │║Clr║"\
- "└───┘└───┘└───┘└───┘└───┘└───┘╚═══╝"\
- "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐╔═══╗"\
- "│Shf││ 7 ││ 8 ││ 9 ││sTo││Rcl│║oFF║"\
- "└───┘└───┘└───┘└───┘└───┘└───┘╚═══╝"\
- "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐"\
- "│Oct││ 4 ││ 5 ││ 6 ││ x ││ | ││ & │"\
- "└───┘└───┘└───┘└───┘└───┘└───┘└───┘"\
- "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐"\
- "│Hex││ 1 ││ 2 ││ 3 ││ - ││DIV││MOD│"\
- "└───┘└───┘└───┘└───┘└───┘└───┘└───┘"\
- "┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐"\
- "│Dec││ 0 ││ = ││SCP││ + ││ ± ││1sC│"\
- "└───┘└───┘└───┘└───┘└───┘└───┘└───┘"\
- " "\
- " ");
-
- }
-
- function calc_display_result(){
- local str;
- local str1;
- local spaces;
- local value = calc_display_value()
- local sprintf_char = (calc_mode == "Dec") \
- ? "ld" \
- : (calc_mode == "Oct") \
- ? "lO"\
- : "lX";
-
- str = str1 = sprintf( "%" sprintf_char, value);
-
- str = sprintf( "%" (CALC_DISPLAY_WIDTH - length(str)) "s%s (%s)", \
- " ", \
- str, \
- calc_mode );
-
- window_cursor_x = CALC_DISPLAY_X;
- window_cursor_y = CALC_DISPLAY_Y;
- write_window( str );
- return str1;
- }
-
-
- function calculate_result( sel ){
- local op = calc_current_operator;
- local res_digits
-
- calc_current_operator = sel;
-
- #
- # perform the pervious operation on the result and operand
- #
- if (!calc_operand_digits){
- return;
- } else if (op == "+") {
- calc_operand = calc_result + calc_operand;
- } else if (op == "-") {
- calc_operand = calc_result - calc_operand;
- } else if (op == "x") {
- calc_operand = calc_result * calc_operand;
- } else if (op == "|") {
- calc_operand = or( calc_operand, calc_result );
- } else if (op == "&") {
- calc_operand = and( calc_operand, calc_result );
- } else if (op == "Shf") {
- if (calc_operand < 0) {
- calc_operand = shiftr( calc_result,-calc_operand);
- } else {
- calc_operand = shiftl( calc_result,calc_operand);
- }
- } else if (op == "DIV") {
- if (calc_result)
- calc_operand = calc_result / calc_operand;
- } else if (op == "MOD") {
- if (calc_result)
- calc_operand = calc_result % calc_operand;
- }
- calc_result = calc_operand;
- }
-
-
- local function calc_display_value(){
- return (calc_operand_digits) ? calc_operand : calc_result;
- }
-
-
-
- local function copy_string_to_scrap( str ){
- current_buffer = create_buffer( "", "", 1 );
- insert_string( str );
- drop_anchor( 1 );
- goto_bol();
- copy_to_scrap();
- buffer_flags = 0;
- delete_buffer();
- }
-
-