home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume14 / mush6.0 / part06 / fkeys.c < prev    next >
C/C++ Source or Header  |  1988-04-12  |  14KB  |  446 lines

  1. /* @(#)fkeys.c        (c) copyright 10/18/86 (Dan Heller) */
  2.  
  3. #include "mush.h"
  4. #define draw(x1,y1,x2,y2,OP) pw_vector(msg_win, x1,y1,x2,y2,OP,1)
  5. #define box(x1,y1,x2,y2,OP)  \
  6.     draw(x1,y1, x1,y2,OP), draw(x1,y2, x2,y2,OP), \
  7.         draw(x2,y2, x2,y1,OP), draw(x2,y1, x1,y1,OP)
  8.  
  9. struct cmd fkey_cmds[] = {
  10.     { "top", fkey_cmd }, { "close", fkey_cmd }, { "bottom", fkey_cmd },
  11.     { "move", fkey_cmd }, { "stretch", fkey_cmd }, { "repaint", fkey_cmd },
  12.     { "settings", fkey_settings }, { NULL, quit }
  13. };
  14.  
  15. #define L(n)        KEY_LEFTFIRST+(n)-1
  16. #define R(n)        KEY_RIGHTFIRST+(n)-1
  17. #define F(n)        KEY_TOPFIRST+(n)-1
  18. #define BREAK_KEY    KEY_TOPLAST
  19.  
  20. char *leftkey_default_settings[] = {
  21.     "Left Function Key Settings",
  22.     "Unused",        /* L1  */    "next",          /* L2  */
  23.     "undelete",         /* L3  */    "delete",         /* L4  */
  24.     "replyall",      /* L5  */    "replysender",       /* L6  */
  25.     "mail",        /* L7  */    "Unset",        /* L8  */
  26.     "lpr",          /* L9  */    "settings L",      /* L10 */
  27.     0
  28. };
  29.  
  30. char *topkey_default_settings[] = {
  31.     "Top Function Key Settings",
  32.     "top",        /* T1  */     "close",            /* T2  */
  33.     "move",        /* T3  */     "stretch",     /* T4  */
  34.     "bottom",        /* T5  */     "repaint",       /* T6  */
  35.     "Unset",        /* T7  */     "Unset",        /* T8  */
  36.     "settings F",     /* T9  */
  37.     0
  38. };
  39.  
  40. char *rightkey_default_settings[] = {
  41.     "Right Function Key Settings",
  42.     "Unset",        /* R1  */     "Unset",         /* R2  */
  43.     "Unset",        /* R3  */     "Unset",         /* R4  */
  44.     "Unset",        /* R5  */     "Unset",         /* R6  */
  45.     "Unset",        /* R7  */     "Unset",         /* R8  */
  46.     "Unset",        /* R9  */     "Unset",         /* R10 */
  47.     "Unset",        /* R11 */     "Unset",         /* R12 */
  48.     "Unset",        /* R13 */     "Unset",         /* R14 */
  49.     "settings R",       /* R15 */
  50.     0
  51. };
  52.  
  53. /*
  54.  * Execute commands defined by a function key.
  55.  * Left keys:
  56.  * L1 = (null)  can't be set
  57.  * L2 ... L10
  58.  * Top function keys
  59.  * F1 ... F9, BREAK/backspace (key not definable)
  60.  * Right function keys
  61.  * R1 ... R15
  62.  * Usually, the last Function key displays the others' settings.
  63.  */
  64. fkey(key)
  65. register char *key;
  66. {
  67.     register char **argv, *p;
  68.     char buf[256];
  69.     int n;
  70.  
  71.     /* user defined here...        ... default settings here */
  72.     if (!strncmp((p = key_set_to(key)), "Un", 2)) {
  73.     print("Funciton key: %s  %s", key, p);
  74.     return 0;
  75.     }
  76.     /* make_command will screw up "p", so copy it first */
  77.     (void) strcpy(buf, p);
  78.     Debug("(%s) \"%s\": ", key, p), turnon(glob_flags, CONT_PRNT);
  79.     if (argv = make_command(buf, TRPL_NULL, &n))
  80.     (void) do_command(n, argv, msg_list);
  81.     return -1;
  82. }
  83.  
  84. fkey_settings(i, argv)
  85. register char i;
  86. register char **argv;
  87. {
  88.     register char key, *p, **fkey_str;
  89.     char buf[256];
  90.     char *help_args[17];
  91.  
  92.     if (!*++argv) {
  93.     print("Must specify one of L, F or R to identify a function key set");
  94.     return -1;
  95.     }
  96.     key = **argv;
  97.     switch(Upper(key)) {
  98.     when 'L': fkey_str = leftkey_default_settings;
  99.     when 'F': fkey_str = topkey_default_settings;
  100.     when 'R': fkey_str = rightkey_default_settings;
  101.     otherwise: print("Invalid key set: %c (choose L, F or R)", key);
  102.            return -1;
  103.     }
  104.     help_args[0] = fkey_str[0];
  105.     for (i = 1; fkey_str[i]; i++) {
  106.     p = key_set_to(sprintf(buf, "%c%d", key, i));
  107.     help_args[i] = savestr(sprintf(buf, "%c%-2.d       %s", key, i, p));
  108.     }
  109.     help_args[i] = 0;  /* null terminate list */
  110.     (void) help(print_sw->ts_windowfd, help_args, NULL);
  111.  
  112.     free_vec(help_args+1);
  113.     return 0;
  114. }
  115.  
  116. char *
  117. key_set_to(p)
  118. register char *p;
  119. {
  120.     register char *p2, **fkey_str;
  121.  
  122.     switch(*p) {
  123.     when 'L': fkey_str = leftkey_default_settings;
  124.     when 'F': fkey_str = topkey_default_settings;
  125.     when 'R': fkey_str = rightkey_default_settings;
  126.     }
  127.     p2 = do_set(fkeys, p);
  128.     return (p2)? p2: fkey_str[atoi(p+1)];
  129. }
  130.  
  131. fkey_cmd(x, p)
  132. register char **p;
  133. {
  134.     if (!strcmp(*p, "close"))
  135.     toolquit(NO_ITEM, 0, NO_EVENT);
  136.     else if (!strcmp(*p, "top"))
  137.     wmgr_top(tool->tl_windowfd, rootfd);
  138.     else if (!strcmp(*p, "move"))
  139.     wmgr_move(tool->tl_windowfd, rootfd);
  140.     else if (!strcmp(*p, "stretch"))
  141.     wmgr_stretch(tool->tl_windowfd, rootfd);
  142.     else if (!strcmp(*p, "bottom"))
  143.     wmgr_bottom(tool->tl_windowfd, rootfd);
  144.     else if (!strcmp(*p, "repaint"))
  145.     wmgr_refreshwindow(tool->tl_windowfd, rootfd);
  146.     return -1;
  147. }
  148.  
  149. /* execute a command given a function key, if the key is user defined,
  150.  * call fkey() at top of file. Parameter is the key number in "ascii"
  151.  */
  152. func_key(key)
  153. register int key;
  154. {
  155.     register char *p;
  156.     char buf[4];
  157.     int nkey;
  158.  
  159.     if (key >= KEY_LEFTFIRST && key <= KEY_LEFTLAST)
  160.     buf[0] = 'L', nkey = key - KEY_LEFTFIRST;
  161.     else if (key >= KEY_TOPFIRST && key <= KEY_TOPLAST)
  162.     buf[0] = 'F', nkey = key - KEY_TOPFIRST;
  163.     else if (key >= KEY_RIGHTFIRST && key <= KEY_RIGHTLAST)
  164.     buf[0] = 'R', nkey = key - KEY_RIGHTFIRST;
  165.     (void) sprintf(buf+1, "%d", nkey+1);
  166.  
  167.     return fkey(buf);
  168. }
  169.  
  170. void
  171. set_fkeys()
  172. {
  173.     ready(display_keys() + 10);
  174.     print_valid_functions(txt.y+20);
  175.     getting_opts = 2;
  176.     win_setcursor(msg_sw->ts_windowfd, &checkmark);
  177. }
  178.  
  179. char *MSG = "Click the appropriate mouse button over a function key";
  180. ready(Y)
  181. {
  182.     static int y;
  183.     int x = (msg_rect.r_width - strlen(MSG)*l_width(LARGE))/2;
  184.     if (Y)
  185.     y = Y;
  186.     Clrtoeol(msg_win, (txt.x = 0), (txt.y = y), LARGE);
  187.     highlight(msg_win, x, y, LARGE, MSG);
  188. }
  189.  
  190. /* number of pixels in x and y directions describing the size of a graphical
  191.  * function key.  they represent the little keys and big keys respectively.
  192.  */
  193. /* static struct pr_pos fkey_sizes[2] = { { 23, 23 }, { 50, 23 } }; */
  194. static struct pr_pos fkey_sizes[2] = { { 24, 23 }, { 52, 23 } };
  195.  
  196. #define BORDER        4   /* border (distance) between keys */
  197. #define KEYTOP        15  /* distance from top to start drawing */
  198. #define LEFT_START    15  /* pixels from left to start drawing boxes */
  199. #define TOP_START    (LEFT_START+2*fkey_sizes[0].x + fkey_sizes[1].x+BORDER)
  200. #define RIGHT_START    (TOP_START + 5*(fkey_sizes[0].x+BORDER) + \
  201.                      5*(fkey_sizes[1].x+BORDER))
  202.  
  203. /*
  204.  * if !p, we're setting key at location x,y.
  205.  * else Set that key to this string (p).
  206.  */
  207. void
  208. set_key(p, x,y)
  209. register char *p;
  210. register int x,y;
  211. {
  212.     char     buf[256], **argv;
  213.     static char *key;
  214.     int     argc;
  215.  
  216.     static int key_x, key_y;
  217.     if (!p) {
  218.     if (key = find_key(x,y)) {
  219.         print("Type new setting for key: %s", key);
  220.         (void) sprintf(buf, "Function key \"%s\": ", key);
  221.         highlight(msg_win, 20, txt.y, LARGE, buf);
  222.         txt.x = 20 + strlen(buf)*l_width(LARGE);
  223.         Clrtoeol(msg_win, txt.x, txt.y, LARGE);
  224.         type_cursor(PIX_SRC);
  225.     } else
  226.         ready(0);
  227.     key_x = x, key_y = y;
  228.     } else {
  229.     u_long save_bang = ison(glob_flags, IGN_BANG);
  230.     if (!*p)
  231.         (void) sprintf(buf, "unfkey %s", key);
  232.     else
  233.         (void) sprintf(buf, "fkey %s \"%s\"", key, p);
  234.     turnon(glob_flags, IGN_BANG);
  235.     if (argv = make_command(buf, TRPL_NULL, &argc)) {
  236.         (void) do_command(argc, argv, msg_list);
  237.         print("Function key %s:  %s", key, key_set_to(key));
  238.     }
  239.     if (!save_bang)
  240.         turnoff(glob_flags, IGN_BANG);
  241.     ready(0);
  242.     }
  243. }
  244.  
  245. /* passed the x and y coords of a mouse click, return the function key
  246.  * that exists in that position. NULL if no key there.  string will be
  247.  * something like "L6" or "F9" or "R12"
  248.  */
  249. char *
  250. find_key(x,y)
  251. int x, y;
  252. {
  253.     static char buf[6];
  254.     int row, col;
  255.     static int old_left, old_top, old_right, old_bot;
  256.  
  257.     if (!(row = find_y(&y)))
  258.     return NULL;
  259.     if (x < LEFT_START || x > RIGHT_START + 3*(fkey_sizes[0].x+BORDER))
  260.     return NULL;   /* out of range */
  261.     if (x > LEFT_START && x < TOP_START-fkey_sizes[0].x - BORDER) {
  262.     if ((col = (x > LEFT_START + fkey_sizes[0].x + BORDER)+1) == 1)
  263.         x = LEFT_START+1;
  264.     else x = LEFT_START + fkey_sizes[0].x + BORDER + 1;
  265.     if (col == 1 && row == 1)
  266.         return NULL;
  267.     /* unhighlight the old function key image */
  268.     if (old_left)
  269.         box(old_left, old_top, old_right, old_bot, PIX_CLR);
  270.     old_left = x, old_top = y;
  271.     old_right = x+fkey_sizes[(col != 1)].x-2, old_bot = y+fkey_sizes[0].y-2;
  272.     /* highlight most recently selected function key image */
  273.     box(x,y, old_right, old_bot, PIX_SRC);
  274.  
  275.     return sprintf(buf, "L%d", col + 2*(row-1));
  276.     }
  277.     if (x > TOP_START && x < RIGHT_START - fkey_sizes[0].x - BORDER) {
  278.     int which;
  279.     if (row > 1)
  280.         return NULL;
  281.     which = (x - TOP_START) / (fkey_sizes[0].x + BORDER) + 1;
  282.     if (which == 15)
  283.         return NULL;   /* Can't set break key (backspace on a sun3) */
  284.     if (which == 14)
  285.         x = TOP_START + ((which = 9)-2) * (fkey_sizes[1].x+BORDER) -
  286.             fkey_sizes[0].x - BORDER + 1;
  287.     else if (which <= 2)
  288.         x = TOP_START + (which-1) * (fkey_sizes[0].x+BORDER) + 1;
  289.     else {
  290.         which = (which+3)/2;
  291.         x = TOP_START + (which-2) * (fkey_sizes[1].x+BORDER) + 1;
  292.     }
  293.  
  294.     /* unhighlight the old function key image */
  295.     if (old_left)
  296.         box(old_left, old_top, old_right, old_bot, PIX_CLR);
  297.     old_left = x, old_top = y;
  298.     old_right = x+fkey_sizes[(which > 2 && which < 8)].x-2;
  299.     old_bot = y+fkey_sizes[0].y-2;
  300.     /* highlight most recently selected function key image */
  301.     box(x,y, old_right, old_bot, PIX_SRC);
  302.  
  303.     return sprintf(buf, "F%d", which);
  304.     }
  305.     if (x > RIGHT_START) {
  306.     if (x < RIGHT_START + fkey_sizes[0].x)
  307.         x = RIGHT_START+1, col = 1;
  308.     else if (x < RIGHT_START + fkey_sizes[0].x + BORDER)
  309.         return NULL;  /* cursor was clicked between keys */
  310.     else if (x < RIGHT_START + 2*fkey_sizes[0].x + BORDER)
  311.         x = RIGHT_START+fkey_sizes[0].x+BORDER+1, col = 2;
  312.     else if (x < RIGHT_START + 2 * (fkey_sizes[0].x+BORDER))
  313.         return NULL;  /* click between keys again */
  314.     else x = RIGHT_START + 2*(fkey_sizes[0].x+BORDER)+1, col = 3;
  315.  
  316.     /* unhighlight the old function key image */
  317.     if (old_left)
  318.         box(old_left, old_top, old_right, old_bot, PIX_CLR);
  319.     old_left = x, old_top = y;
  320.     old_right = x+fkey_sizes[0].x-2, old_bot = y+fkey_sizes[0].y-2;
  321.     /* highlight most recently selected function key image */
  322.     box(x,y, old_right, old_bot, PIX_SRC);
  323.  
  324.     return sprintf(buf, "R%d", col + 3*(row-1));
  325.     }
  326.     return NULL;
  327. }
  328.  
  329. /* find_y will find which row in a function key pad a y coordinate
  330.  * represents. return 1,2,3,4, or 5   0 if inbetween rows
  331.  */
  332. find_y(y)
  333. register int *y;
  334. {
  335.     int Y, y_incr = fkey_sizes[0].y, ret_value = 0;
  336.     for (Y = KEYTOP; Y <= KEYTOP + 6*y_incr + 4 * BORDER; Y += y_incr + BORDER)
  337.     if (*y < Y) {
  338.         *y = (Y - y_incr - BORDER) + 1;
  339.         return ret_value;
  340.     } else ret_value++;
  341.     return 0;
  342. }
  343.  
  344. char *l_msg = "Specifies which function key for setting value";
  345. char *m_msg = "Display current value for function key";
  346. char *r_msg = "Help setting and viewing function key values";
  347.  
  348. display_keys()
  349. {
  350.     register int i, x,y;
  351.     register char *p;
  352.  
  353.     do_clear();
  354.  
  355.     x = LEFT_START, y = KEYTOP;
  356.     /* print left keys */
  357.     for (i = 0; i < 10; i++) {
  358.     box(x, y, x + fkey_sizes[i%2].x, y + fkey_sizes[i%2].y, PIX_SRC);
  359.     box(x+2, y+2, x+fkey_sizes[i%2].x-2, y+fkey_sizes[i%2].y-2, PIX_SRC);
  360.     if (i && (p = find_key(x+4,y+4)))
  361.         pw_text(msg_win, x+3, y+3+l_height(SMALL), PIX_SRC,fonts[SMALL], p);
  362.     else pw_replrop(msg_win, x+3, y+3, fkey_sizes[0].x-5, fkey_sizes[0].y-5,
  363.          PIX_SRC | PIX_DST, &shade_50, 0,0);
  364.     if (i % 2)
  365.         y += fkey_sizes[0].y + BORDER, x = LEFT_START;
  366.     else
  367.         x += fkey_sizes[0].x + BORDER;
  368.     }
  369.  
  370.     x = TOP_START, y = KEYTOP;
  371.     /* print top keys */
  372.     for (i = 1; i <= 10; i++) {
  373.     register int n = (i >= 3 && i <= 7);
  374.     box(x, y, x + fkey_sizes[n].x, y + fkey_sizes[n].y, PIX_SRC);
  375.     box(x+2, y+2, x + fkey_sizes[n].x-2, y + fkey_sizes[n].y-2, PIX_SRC);
  376.     if (i != 10 && (p = find_key(x+4,y+4)))
  377.         pw_text(msg_win, x+3, y+3+l_height(SMALL), PIX_SRC,fonts[SMALL], p);
  378.     /* shade the break key (backspace on sun3's) -- can't set */
  379.     else if (i == 10)
  380.         pw_replrop(msg_win, x+3, y+3, fkey_sizes[n].x-5, fkey_sizes[n].y-5,
  381.          PIX_SRC | PIX_DST, &shade_50, 0,0);
  382.     x += fkey_sizes[n].x + BORDER;
  383.     }
  384.  
  385.     x = RIGHT_START;
  386.     /* print right keys */
  387.     for (i = 0; i < 15; i++) {
  388.     box(x, y, x + fkey_sizes[0].x, y + fkey_sizes[0].y, PIX_SRC);
  389.     box(x+2, y+2, x + fkey_sizes[0].x-2, y + fkey_sizes[0].y-2, PIX_SRC);
  390.     if (p = find_key(x+4,y+4))
  391.         pw_text(msg_win, x+3, y+3+l_height(SMALL),PIX_SRC, fonts[SMALL], p);
  392.     if (!((i+1) % 3))
  393.         y += fkey_sizes[0].y + BORDER, x -= 2*(fkey_sizes[0].x + BORDER);
  394.     else
  395.         x += fkey_sizes[0].x + BORDER;
  396.     }
  397.     x = TOP_START;
  398.     y = KEYTOP + BORDER + fkey_sizes[0].y + l_height(DEFAULT);
  399.     pw_rop(msg_win, x, y-11, 16,16, PIX_SRC, &mouse_left, 0,0);
  400.     pw_text(msg_win, x+30, y, PIX_SRC, fonts[DEFAULT], l_msg);
  401.  
  402.     y += BORDER + fkey_sizes[0].y;
  403.     pw_rop(msg_win, x,y-11, 16,16, PIX_SRC, &mouse_middle, 0,0);
  404.     pw_text(msg_win, x+30, y, PIX_SRC, fonts[DEFAULT], m_msg);
  405.  
  406.     y += BORDER + fkey_sizes[0].y;
  407.     pw_rop(msg_win, x,y-11, 16,16, PIX_SRC, &mouse_right, 0,0);
  408.     pw_text(msg_win, x+30, y, PIX_SRC, fonts[DEFAULT], r_msg);
  409.  
  410.     x = (msg_rect.r_width - 26*l_width(DEFAULT))/2;
  411.     y += BORDER + fkey_sizes[0].y;
  412.     highlight(msg_win, x, y, DEFAULT, "You may not set shaded keys");
  413.  
  414.     y += BORDER + fkey_sizes[0].y + 15;
  415.     for (i = 0; i < BORDER; i++)
  416.     draw(0, y+i, msg_rect.r_width, y+i, PIX_SRC);
  417.     y += 10;
  418.     for (i = 0; i < BORDER; i++)
  419.     draw(0, y+l_height(LARGE)+i, msg_rect.r_width, y+l_height(LARGE)+i,
  420.          PIX_SRC);
  421.     return y;
  422. }
  423.  
  424. print_valid_functions(y)
  425. register int y;
  426. {
  427.     register int x, n, cmd_len = 12 * l_width(DEFAULT);
  428.     register char *p;
  429.  
  430.     y += 20, x = (msg_rect.r_width - 25*l_width(LARGE))/2;
  431.     highlight (msg_win, x, y, LARGE, "Available Command Names");
  432.     y += 20, x = 30;
  433.     for (n = 0; p = cmds[n].command; n++) {
  434.     if (x + cmd_len > msg_rect.r_width - 5)
  435.         y += l_height(DEFAULT), x = 30;
  436.     pw_text(msg_win, x, y, PIX_SRC, fonts[DEFAULT], p);
  437.     x += cmd_len;
  438.     }
  439.     for (n = 0; p = fkey_cmds[n].command; n++) {
  440.     if (x + cmd_len > msg_rect.r_width - 5)
  441.         y += l_height(DEFAULT), x = 30;
  442.     pw_text(msg_win, x, y, PIX_SRC, fonts[DEFAULT], p);
  443.     x += cmd_len;
  444.     }
  445. }
  446.