home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* Copyright 1988 by Chuck Musciano and Harris Corporation */
- /* */
- /* Permission to use, copy, modify, and distribute this software */
- /* and its documentation for any purpose and without fee is */
- /* hereby granted, provided that the above copyright notice */
- /* appear in all copies and that both that copyright notice and */
- /* this permission notice appear in supporting documentation, and */
- /* that the name of Chuck Musciano and Harris Corporation not be */
- /* used in advertising or publicity pertaining to distribution */
- /* of the software without specific, written prior permission. */
- /* Chuck Musciano and Harris Corporation make no representations */
- /* about the suitability of this software for any purpose. It is */
- /* provided "as is" without express or implied warranty. */
- /************************************************************************/
-
- /************************************************************************/
- /* */
- /* Module: screen.c */
- /* */
- /* Function: Create calculator display */
- /* */
- /* Public Names: create_screen create the screen */
- /* invert_proc invert the keyboard */
- /* blink flash a key */
- /* main entry point */
- /* */
- /* Change History: 11 Nov 86 Creation */
- /* */
- /************************************************************************/
-
- #include <stdio.h>
-
- #include <suntool/sunview.h>
- #include <suntool/panel.h>
- #include <suntool/canvas.h>
-
- #include "manifest.h"
- #include "globals.h"
- #include "keys.h"
-
- #define CLOSE_KEY key[0]
- #define INVERSE_KEY key[1]
- #define CLEAR_KEY key[2]
- #define FIRST_KEY 3
- #define LAST_KEY 37
-
- #define k(s1, s2, s3, s4, o1, o2, o3, o4, p) {{s1, s2, s3, s4}, {o1, o2, o3, o4}, {NULL, NULL, NULL, NULL}, NULL, p}
- #define valid_key(x) ((x) > DIGIT_F || (x) < curr_base)
-
- PRIVATE close_proc();
- PRIVATE event_proc();
- PRIVATE key_proc();
- PRIVATE radix_proc();
- PRIVATE kb_proc();
-
- PUBLIC store_proc();
- PUBLIC recall_proc();
- PUBLIC exchange_proc();
- PUBLIC fix_proc();
- PUBLIC invert_proc();
-
- typedef struct key_rec *key_ptr;
-
- struct key_rec {char *label[4];
- int opcode[4];
- struct pixrect *image[4];
- Panel_item item;
- int (*proc)();
- };
-
- Frame bf;
- Panel keys, sw;
- Canvas display;
- struct pixfont *key_font;
-
- PRIVATE short mask_bits[] = {0xaaaa, 0x5555};
- mpr_static(mask, 16, 2, 1, mask_bits);
-
- PRIVATE struct key_rec key[] = {
- k( "Close", "Quit", "Close", "Quit", CLOSE_OP, QUIT_OP, CLOSE_OP, QUIT_OP, close_proc),
- k( "Inverse", "Inverse", "Inverse", "Inverse", INVERT_OP, INVERT_OP, INVERT_OP, INVERT_OP, invert_proc),
- k( "Erase", "Clear", "Erase", "Clear", ERASE_OP, CLEAR_OP, ERASE_OP, CLEAR_OP, key_proc),
- k( "Sto", "", "Sto", "", STO_OP, NO_OP, STO_OP, NO_OP, store_proc),
- k( "Rcl", "", "Rcl", "", RCL_OP, NO_OP, RCL_OP, NO_OP, recall_proc),
- k( "Exc", "", "Exc", "", EXC_OP, NO_OP, EXC_OP, NO_OP, exchange_proc),
- k( "(", "", "(", "", LPAREN_OP, NO_OP, LPAREN_OP, NO_OP, key_proc),
- k( ")", "", ")", "", RPAREN_OP, NO_OP, RPAREN_OP, NO_OP, key_proc),
- k( "EE", "EE", "", "", DIGIT_EE, INVEE_OP, NO_OP, NO_OP, key_proc),
- k( "X", "", "X", "", MUL_OP, NO_OP, MUL_OP, NO_OP, key_proc),
- k( "log", "10\200", "D", "", LOG_OP, POW_OP, DIGIT_D, NO_OP, key_proc),
- k( "ln", "e\200", "E", "", LN_OP, EXP_OP, DIGIT_E, NO_OP, key_proc),
- k( "y\200", "\203\204", "F", "", Y2X_OP, ROOT_OP, DIGIT_F, NO_OP, key_proc),
- k( "7", "", "7", "", DIGIT_7, NO_OP, DIGIT_7, NO_OP, key_proc),
- k( "8", "", "8", "", DIGIT_8, NO_OP, DIGIT_8, NO_OP, key_proc),
- k( "9", "", "9", "", DIGIT_9, NO_OP, DIGIT_9, NO_OP, key_proc),
- k( "\214", "", "\214", "", DIV_OP, NO_OP, DIV_OP, NO_OP, key_proc),
- k( "sin", "sn\205", "A", "", SIN_OP, ISIN_OP, DIGIT_A, NO_OP, key_proc),
- k( "cos", "cs\205", "B", "", COS_OP, ICOS_OP, DIGIT_B, NO_OP, key_proc),
- k( "tan", "tn\205", "C", "", TAN_OP, ITAN_OP, DIGIT_C, NO_OP, key_proc),
- k( "4", "", "4", "", DIGIT_4, NO_OP, DIGIT_4, NO_OP, key_proc),
- k( "5", "", "5", "", DIGIT_5, NO_OP, DIGIT_5, NO_OP, key_proc),
- k( "6", "", "6", "", DIGIT_6, NO_OP, DIGIT_6, NO_OP, key_proc),
- k( "+", "", "+", "", ADD_OP, NO_OP, ADD_OP, NO_OP, key_proc),
- k( "\202\201", "x\206", "<<", "\217\220", SQRT_OP, SQR_OP, LSL_OP, ROL_OP, key_proc),
- k( "\210\211", "", "Not", "\221>>", OVER_OP, NO_OP, NOT_OP, RSA_OP, key_proc),
- k( "x!", "", ">>", "\215\216", FACT_OP, NO_OP, RSL_OP, ROR_OP, key_proc),
- k( "1", "", "1", "", DIGIT_1, NO_OP, DIGIT_1, NO_OP, key_proc),
- k( "2", "", "2", "", DIGIT_2, NO_OP, DIGIT_2, NO_OP, key_proc),
- k( "3", "", "3", "", DIGIT_3, NO_OP, DIGIT_3, NO_OP, key_proc),
- k( "-", "", "-", "", SUB_OP, NO_OP, SUB_OP, NO_OP, key_proc),
- k( "\207", "e", "And", "", PI_OP, E_OP, AND_OP, NO_OP, key_proc),
- k( "Int", "Trc", "Or", "Xor", INT_OP, TRUNC_OP, OR_OP, XOR_OP, key_proc),
- k( "Fix", "DRG", "Fix", "", FIX_OP, FIX_OP, FIX_OP, NO_OP, fix_proc),
- k( ".", "", "", "", DIGIT_DOT, NO_OP, NO_OP, NO_OP, key_proc),
- k( "0", "", "0", "", DIGIT_0, NO_OP, DIGIT_0, NO_OP, key_proc),
- k( "\212\213", "", "\212\213", "", DIGIT_CHS, NO_OP, DIGIT_CHS, NO_OP, key_proc),
- k( "=", "", "=", "", EQUAL_OP, NO_OP, EQUAL_OP, NO_OP, key_proc)
- };
-
- static short ct_icon_image[] = {
- #include "calc.icon"
- };
- mpr_static(ct_icon_pixrect, 47, 64, 1, ct_icon_image);
-
- PUBLIC create_screen(argc, argv)
-
- int argc;
- char **argv;
-
- { int i, j;
- Icon icon;
-
- icon = icon_create(ICON_IMAGE, &ct_icon_pixrect, ICON_LABEL, NULL, ICON_WIDTH, 47, ICON_HEIGHT, 64, 0);
- bf = window_create(NULL, FRAME,
- FRAME_ARGS, argc, argv,
- FRAME_LABEL, "<< Calctool >>",
- FRAME_ICON, icon,
- FRAME_SUBWINDOWS_ADJUSTABLE, FALSE,
- FRAME_NO_CONFIRM, TRUE,
- 0);
- sw = window_create(bf, PANEL, 0);
- display = window_create(bf, CANVAS, 0);
- keys = window_create(bf, PANEL,
- WIN_WIDTH, 360,
- PANEL_ACCEPT_KEYSTROKE, TRUE,
- PANEL_BACKGROUND_PROC, kb_proc,
- 0);
-
- if ((key_font = pf_open(KEY_FONT)) == NULL) {
- fprintf(stderr, "calctool: could not open font %s\n", KEY_FONT);
- exit(1);
- }
- for (i = 0; i < FIRST_KEY; i++)
- for (j = 0; j < 4; j++)
- key[i].image[j] = panel_button_image((i == 0)? sw : keys, key[i].label[j], 7, key_font);
- for (i = FIRST_KEY; i <= LAST_KEY; i++)
- for (j = 0; j < 4; j += 2)
- if (key[i].opcode[j] != NO_OP) {
- key[i].image[j] = panel_button_image(sw, key[i].label[j], 3, key_font);
- if (key[i].opcode[j + 1] == NO_OP) {
- key[i].image[j + 1] = panel_button_image(sw, key[i].label[j], 3, key_font);
- pr_replrop(key[i].image[j+1], 0, 0, key[i].image[j+1]->pr_width, key[i].image[j+1]->pr_height,
- PIX_SRC & PIX_DST, &mask, 0, 0);
- }
- else
- key[i].image[j+1] = panel_button_image(sw, key[i].label[j+1], 3, key_font);
- }
-
- CLOSE_KEY.item = panel_create_item(sw, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, CLOSE_KEY.image[0],
- PANEL_NOTIFY_PROC, CLOSE_KEY.proc,
- PANEL_EVENT_PROC, event_proc,
- PANEL_CLIENT_DATA, &(CLOSE_KEY),
- 0);
- window_fit(sw);
-
- INVERSE_KEY.item = panel_create_item(keys, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, INVERSE_KEY.image[0],
- PANEL_NOTIFY_PROC, INVERSE_KEY.proc,
- PANEL_EVENT_PROC, event_proc,
- PANEL_CLIENT_DATA, &(INVERSE_KEY),
- 0);
- key[FIRST_KEY].item = panel_create_item(keys, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, key[FIRST_KEY].image[0],
- PANEL_NOTIFY_PROC, key[FIRST_KEY].proc,
- PANEL_EVENT_PROC, event_proc,
- PANEL_ACCEPT_KEYSTROKE, TRUE,
- PANEL_ITEM_X, panel_get(INVERSE_KEY.item, PANEL_ITEM_X),
- PANEL_ITEM_Y, panel_get(INVERSE_KEY.item, PANEL_ITEM_Y) +
- INVERSE_KEY.image[0]->pr_height + 4,
- PANEL_CLIENT_DATA, &(key[FIRST_KEY]),
- 0);
- for (i = FIRST_KEY + 1; i <= LAST_KEY; i++)
- key[i].item = panel_create_item(keys, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, key[i].image[0],
- PANEL_NOTIFY_PROC, key[i].proc,
- PANEL_EVENT_PROC, event_proc,
- PANEL_ACCEPT_KEYSTROKE, TRUE,
- PANEL_CLIENT_DATA, &(key[i]),
- 0);
- CLEAR_KEY.item = panel_create_item(keys, PANEL_BUTTON,
- PANEL_LABEL_IMAGE, CLEAR_KEY.image[0],
- PANEL_NOTIFY_PROC, CLEAR_KEY.proc,
- PANEL_EVENT_PROC, event_proc,
- PANEL_ITEM_X, panel_get(key[LAST_KEY].item, PANEL_ITEM_X) +
- key[LAST_KEY].image[0]->pr_width - CLEAR_KEY.image[0]->pr_width,
- PANEL_ITEM_Y, panel_get(INVERSE_KEY.item, PANEL_ITEM_Y),
- PANEL_CLIENT_DATA, &(CLEAR_KEY),
- 0);
- panel_create_item(keys, PANEL_CHOICE,
- PANEL_LABEL_STRING, "",
- PANEL_DISPLAY_LEVEL, PANEL_CURRENT,
- PANEL_CHOICE_IMAGES, panel_button_image(keys, "Scientific", 13, key_font),
- panel_button_image(keys, "Engineering", 13, key_font),
- panel_button_image(keys, "Binary", 13, key_font),
- panel_button_image(keys, "Octal", 13, key_font),
- panel_button_image(keys, "Hexadecimal", 13, key_font),
- 0,
- PANEL_CHOICE_XS, panel_get(key[FIRST_KEY + 2].item, PANEL_ITEM_X), 0,
- PANEL_CHOICE_YS, panel_get(INVERSE_KEY.item, PANEL_ITEM_Y), 0,
- /* PANEL_CHOICE_FONTS, key_font, 0,*/
- PANEL_NOTIFY_PROC, radix_proc,
- 0);
- window_fit(keys);
-
- window_set(sw,
- WIN_X, 0,
- WIN_Y, 0,
- 0);
- window_set(display,
- WIN_RIGHT_OF, sw,
- WIN_Y, 0,
- WIN_WIDTH, window_get(keys, WIN_WIDTH) - window_get(sw, WIN_WIDTH) - 5,
- WIN_HEIGHT, window_get(sw, WIN_HEIGHT),
- 0);
- window_set(keys,
- WIN_X, 0,
- WIN_BELOW, sw,
- 0);
- window_fit(bf);
- window_set(sw,
- WIN_WIDTH, window_get(sw, WIN_WIDTH),
- WIN_HEIGHT, window_get(sw, WIN_HEIGHT),
- 0);
- window_set(display,
- WIN_WIDTH, window_get(display, WIN_WIDTH),
- WIN_HEIGHT, window_get(display, WIN_HEIGHT),
- 0);
- window_set(keys,
- WIN_WIDTH, window_get(keys, WIN_WIDTH),
- WIN_HEIGHT, window_get(keys, WIN_HEIGHT),
- 0);
- update_display();
- window_main_loop(bf);
- }
-
- PRIVATE close_proc()
-
- {
- if (inverted ^ temp_inverted) {
- window_destroy(bf);
- exit(0);
- }
- else {
- window_set(bf, FRAME_CLOSED, TRUE, 0);
- temp_inverted = FALSE;
- if (inverted)
- invert_proc();
- }
- }
-
- PUBLIC invert_proc()
-
- { int i;
-
- temp_inverted = FALSE;
- inverted = !inverted;
- for (i = 0; i <= LAST_KEY; i++)
- if (key[i].image[curr_mode + (inverted? 1 : 0)] && valid_key(key[i].opcode[curr_mode]))
- panel_set(key[i].item, PANEL_LABEL_IMAGE, key[i].image[curr_mode + (inverted? 1 : 0)], PANEL_SHOW_ITEM, TRUE, 0);
- else
- panel_set(key[i].item, PANEL_SHOW_ITEM, FALSE, 0);
- }
-
- PRIVATE radix_proc(item, value, event)
-
- Panel_item item;
- int value;
- Event *event;
-
- { int i;
- static int bases[] = {DECIMAL, DECIMAL, BINARY, OCTAL, HEXADECIMAL};
-
- convert_display();
- if (value == 0) {
- curr_mode = SCIENTIFIC;
- eng_mode = FALSE;
- }
- else if (value == 1) {
- curr_mode = SCIENTIFIC;
- eng_mode = TRUE;
- }
- else
- curr_mode = PROGRAMMER;
- curr_base = bases[value];
- inverted = temp_inverted = FALSE;
- for (i = FIRST_KEY; i <= LAST_KEY; i++)
- if (key[i].opcode[curr_mode] != NO_OP && valid_key(key[i].opcode[curr_mode]))
- panel_set(key[i].item, PANEL_LABEL_IMAGE, key[i].image[curr_mode], PANEL_SHOW_ITEM, TRUE, 0);
- else
- panel_set(key[i].item, PANEL_SHOW_ITEM, FALSE, 0);
- update_display();
- }
-
- PRIVATE event_proc(item, event)
-
- Panel_item item;
- Event *event;
-
- { key_ptr ky;
- static key_ptr curr_key;
- int op;
-
- if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST) {
- keyboard(event_id(event));
- return;
- }
- ky = (key_ptr) panel_get(item, PANEL_CLIENT_DATA);
- switch (event_id(event)) {
- case MS_LEFT : if (ky->opcode[curr_mode + (inverted? 1 : 0)] != NO_OP)
- if (event_is_down(event)) {
- panel_begin_preview(item, event);
- curr_key = ky;
- }
- else if (curr_key == ky) {
- panel_accept_preview(item, event);
- curr_key = NULL;
- }
- break;
- case MS_MIDDLE : if (ky->opcode[curr_mode + (inverted? 0 : 1)] != NO_OP)
- if (event_is_down(event)) {
- panel_set(item, PANEL_LABEL_IMAGE, ky->image[curr_mode + (inverted? 0 : 1)], 0);
- panel_begin_preview(item, event);
- curr_key = ky;
- }
- else if (curr_key == ky) {
- temp_inverted = TRUE;
- panel_accept_preview(item, event);
- panel_set(item, PANEL_LABEL_IMAGE, ky->image[curr_mode + (inverted? 1 : 0)], 0);
- curr_key = NULL;
- }
- break;
- case MS_RIGHT : if (event_is_down(event))
- if ((op = ky->opcode[curr_mode + (inverted? 1 : 0)]) == STO_OP ||
- op == RCL_OP ||
- op == EXC_OP ||
- op == FIX_OP) {
- panel_begin_preview(item, event);
- panel_accept_preview(item, event);
- }
- break;
- case PANEL_EVENT_CANCEL : if (curr_key == ky) {
- panel_cancel_preview(item, event);
- panel_set(item, PANEL_LABEL_IMAGE, ky->image[curr_mode + (inverted? 1 : 0)], 0);
- curr_key = NULL;
- }
- break;
- }
- }
-
- PRIVATE key_proc(item, event)
-
- Panel_item item;
- Event *event;
-
- { key_ptr ky;
- int op;
-
- ky = (key_ptr) panel_get(item, PANEL_CLIENT_DATA);
- op = ky->opcode[curr_mode + ((inverted? 1 : 0) ^ temp_inverted)];
- if (is_digit(op))
- do_digit(op);
- else if (is_binary(op))
- do_binary(op);
- else
- do_unary(op);
- temp_inverted = FALSE;
- if (inverted)
- invert_proc();
- }
-
- PRIVATE kb_proc(item, event)
-
- Panel_item item;
- Event *event;
-
- {
- if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST)
- keyboard(event_id(event));
- }
-
- PUBLIC int blink(op)
-
- int op;
-
- { int i, j;
- struct pixrect *old, *new;
-
- for (i = FIRST_KEY; i <= LAST_KEY; i++)
- if (op == key[i].opcode[curr_mode])
- break;
- old = (struct pixrect *) panel_get(key[i].item, PANEL_LABEL_IMAGE);
- new = mem_create(old->pr_width, old->pr_height, 1);
- pr_rop(new, 0, 0, new->pr_width, new->pr_height, PIX_NOT(PIX_SRC), old, 0, 0);
- panel_set(key[i].item, PANEL_LABEL_IMAGE, new, 0);
- for (j = 25000; j; j--)
- ;
- panel_set(key[i].item, PANEL_LABEL_IMAGE, old, 0);
- pr_destroy(new);
- return(op);
- }
-
- main(argc, argv)
-
- int argc;
- char **argv;
-
- {
- create_screen(argc, argv);
- }
-