dlg_keys.c (217309) | dlg_keys.c (241818) |
---|---|
1/* | 1/* |
2 * $Id: dlg_keys.c,v 1.26 2009/02/22 16:19:51 tom Exp $ | 2 * $Id: dlg_keys.c,v 1.34 2011/10/14 00:41:08 tom Exp $ |
3 * | 3 * |
4 * dlg_keys.c -- runtime binding support for dialog | 4 * dlg_keys.c -- runtime binding support for dialog |
5 * | 5 * |
6 * Copyright 2006-2007,2009 Thomas E. Dickey | 6 * Copyright 2006-2009,2011 Thomas E. Dickey |
7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License, version 2.1 10 * as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --- 14 unchanged lines hidden (view full) --- 29LIST_BINDINGS { 30 LIST_BINDINGS *link; 31 WINDOW *win; /* window on which widget gets input */ 32 const char *name; /* widget name */ 33 bool buttons; /* true only for dlg_register_buttons() */ 34 DLG_KEYS_BINDING *binding; /* list of bindings */ 35}; 36 | 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License, version 2.1 10 * as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --- 14 unchanged lines hidden (view full) --- 29LIST_BINDINGS { 30 LIST_BINDINGS *link; 31 WINDOW *win; /* window on which widget gets input */ 32 const char *name; /* widget name */ 33 bool buttons; /* true only for dlg_register_buttons() */ 34 DLG_KEYS_BINDING *binding; /* list of bindings */ 35}; 36 |
37#define WILDNAME "*" |
|
37static LIST_BINDINGS *all_bindings; 38static const DLG_KEYS_BINDING end_keys_binding = END_KEYS_BINDING; 39 40/* 41 * For a given named widget's window, associate a binding table. 42 */ 43void 44dlg_register_window(WINDOW *win, const char *name, DLG_KEYS_BINDING * binding) --- 11 unchanged lines hidden (view full) --- 56 p->win = win; 57 p->name = name; 58 p->binding = binding; 59 if (q != 0) 60 q->link = p; 61 else 62 all_bindings = p; 63 } | 38static LIST_BINDINGS *all_bindings; 39static const DLG_KEYS_BINDING end_keys_binding = END_KEYS_BINDING; 40 41/* 42 * For a given named widget's window, associate a binding table. 43 */ 44void 45dlg_register_window(WINDOW *win, const char *name, DLG_KEYS_BINDING * binding) --- 11 unchanged lines hidden (view full) --- 57 p->win = win; 58 p->name = name; 59 p->binding = binding; 60 if (q != 0) 61 q->link = p; 62 else 63 all_bindings = p; 64 } |
65#if defined(HAVE_DLG_TRACE) && defined(HAVE_RC_FILE) 66 /* 67 * Trace the binding information assigned to this window. For most widgets 68 * there is only one binding table. forms have two, so the trace will be 69 * longer. Since compiled-in bindings are only visible when the widget is 70 * registered, there is no other way to see what bindings are available, 71 * than by running dialog and tracing it. 72 */ 73 dlg_trace_msg("# dlg_register_window %s\n", name); 74 dlg_dump_window_keys(dialog_state.trace_output, win); 75#endif |
|
64} 65 66/* 67 * Unlike dlg_lookup_key(), this looks for either widget-builtin or rc-file 68 * definitions, depending on whether 'win' is null. 69 */ 70static int 71key_is_bound(WINDOW *win, const char *name, int curses_key, int function_key) --- 112 unchanged lines hidden (view full) --- 184 * curses_key is the value returned by wgetch(). 185 * fkey in/out (on input, it is true if curses_key is a function key, 186 * and on output, it is true if the result is a function key). 187 */ 188int 189dlg_lookup_key(WINDOW *win, int curses_key, int *fkey) 190{ 191 LIST_BINDINGS *p; | 76} 77 78/* 79 * Unlike dlg_lookup_key(), this looks for either widget-builtin or rc-file 80 * definitions, depending on whether 'win' is null. 81 */ 82static int 83key_is_bound(WINDOW *win, const char *name, int curses_key, int function_key) --- 112 unchanged lines hidden (view full) --- 196 * curses_key is the value returned by wgetch(). 197 * fkey in/out (on input, it is true if curses_key is a function key, 198 * and on output, it is true if the result is a function key). 199 */ 200int 201dlg_lookup_key(WINDOW *win, int curses_key, int *fkey) 202{ 203 LIST_BINDINGS *p; |
192 int n; | 204 DLG_KEYS_BINDING *q; |
193 194 /* 195 * Ignore mouse clicks, since they are already encoded properly. 196 */ 197#ifdef KEY_MOUSE 198 if (*fkey != 0 && curses_key == KEY_MOUSE) { 199 ; 200 } else 201#endif 202 /* 203 * Ignore resize events, since they are already encoded properly. 204 */ 205#ifdef KEY_RESIZE 206 if (*fkey != 0 && curses_key == KEY_RESIZE) { 207 ; 208 } else 209#endif 210 if (*fkey == 0 || curses_key < KEY_MAX) { | 205 206 /* 207 * Ignore mouse clicks, since they are already encoded properly. 208 */ 209#ifdef KEY_MOUSE 210 if (*fkey != 0 && curses_key == KEY_MOUSE) { 211 ; 212 } else 213#endif 214 /* 215 * Ignore resize events, since they are already encoded properly. 216 */ 217#ifdef KEY_RESIZE 218 if (*fkey != 0 && curses_key == KEY_RESIZE) { 219 ; 220 } else 221#endif 222 if (*fkey == 0 || curses_key < KEY_MAX) { |
223 const char *name = WILDNAME; 224 if (win != 0) { 225 for (p = all_bindings; p != 0; p = p->link) { 226 if (p->win == win) { 227 name = p->name; 228 break; 229 } 230 } 231 } |
|
211 for (p = all_bindings; p != 0; p = p->link) { | 232 for (p = all_bindings; p != 0; p = p->link) { |
212 if (p->win == win || p->win == 0) { | 233 if (p->win == win || (p->win == 0 && !strcmp(p->name, name))) { |
213 int function_key = (*fkey != 0); | 234 int function_key = (*fkey != 0); |
214 for (n = 0; p->binding[n].is_function_key >= 0; ++n) { | 235 for (q = p->binding; q->is_function_key >= 0; ++q) { |
215 if (p->buttons 216 && !function_key | 236 if (p->buttons 237 && !function_key |
217 && p->binding[n].curses_key == (int) dlg_toupper(curses_key)) { | 238 && q->curses_key == (int) dlg_toupper(curses_key)) { |
218 *fkey = 0; | 239 *fkey = 0; |
219 return p->binding[n].dialog_key; | 240 return q->dialog_key; |
220 } | 241 } |
221 if (p->binding[n].curses_key == curses_key 222 && p->binding[n].is_function_key == function_key) { 223 *fkey = p->binding[n].dialog_key; | 242 if (q->curses_key == curses_key 243 && q->is_function_key == function_key) { 244 *fkey = q->dialog_key; |
224 return *fkey; 225 } 226 } 227 } 228 } 229 } 230 return curses_key; 231} --- 58 unchanged lines hidden (view full) --- 290} 291 292#ifdef HAVE_RC_FILE 293typedef struct { 294 const char *name; 295 int code; 296} CODENAME; 297 | 245 return *fkey; 246 } 247 } 248 } 249 } 250 } 251 return curses_key; 252} --- 58 unchanged lines hidden (view full) --- 311} 312 313#ifdef HAVE_RC_FILE 314typedef struct { 315 const char *name; 316 int code; 317} CODENAME; 318 |
319#define ASCII_NAME(name,code) { #name, code } |
|
298#define CURSES_NAME(upper) { #upper, KEY_ ## upper } 299#define COUNT_CURSES sizeof(curses_names)/sizeof(curses_names[0]) 300static const CODENAME curses_names[] = 301{ | 320#define CURSES_NAME(upper) { #upper, KEY_ ## upper } 321#define COUNT_CURSES sizeof(curses_names)/sizeof(curses_names[0]) 322static const CODENAME curses_names[] = 323{ |
324 ASCII_NAME(ESC, '\033'), 325 ASCII_NAME(CR, '\r'), 326 ASCII_NAME(LF, '\n'), 327 ASCII_NAME(FF, '\f'), 328 ASCII_NAME(TAB, '\t'), 329 ASCII_NAME(DEL, '\177'), 330 |
|
302 CURSES_NAME(DOWN), 303 CURSES_NAME(UP), 304 CURSES_NAME(LEFT), 305 CURSES_NAME(RIGHT), 306 CURSES_NAME(HOME), 307 CURSES_NAME(BACKSPACE), 308 CURSES_NAME(F0), 309 CURSES_NAME(DL), --- 93 unchanged lines hidden (view full) --- 403 DIALOG_NAME(ITEM_FIRST), 404 DIALOG_NAME(ITEM_LAST), 405 DIALOG_NAME(ITEM_NEXT), 406 DIALOG_NAME(ITEM_PREV), 407 DIALOG_NAME(FIELD_FIRST), 408 DIALOG_NAME(FIELD_LAST), 409 DIALOG_NAME(FIELD_NEXT), 410 DIALOG_NAME(FIELD_PREV), | 331 CURSES_NAME(DOWN), 332 CURSES_NAME(UP), 333 CURSES_NAME(LEFT), 334 CURSES_NAME(RIGHT), 335 CURSES_NAME(HOME), 336 CURSES_NAME(BACKSPACE), 337 CURSES_NAME(F0), 338 CURSES_NAME(DL), --- 93 unchanged lines hidden (view full) --- 432 DIALOG_NAME(ITEM_FIRST), 433 DIALOG_NAME(ITEM_LAST), 434 DIALOG_NAME(ITEM_NEXT), 435 DIALOG_NAME(ITEM_PREV), 436 DIALOG_NAME(FIELD_FIRST), 437 DIALOG_NAME(FIELD_LAST), 438 DIALOG_NAME(FIELD_NEXT), 439 DIALOG_NAME(FIELD_PREV), |
440 DIALOG_NAME(FORM_FIRST), 441 DIALOG_NAME(FORM_LAST), 442 DIALOG_NAME(FORM_NEXT), 443 DIALOG_NAME(FORM_PREV), |
|
411 DIALOG_NAME(GRID_UP), 412 DIALOG_NAME(GRID_DOWN), 413 DIALOG_NAME(GRID_LEFT), 414 DIALOG_NAME(GRID_RIGHT), 415 DIALOG_NAME(DELETE_LEFT), 416 DIALOG_NAME(DELETE_RIGHT), 417 DIALOG_NAME(DELETE_ALL), 418 DIALOG_NAME(ENTER), 419 DIALOG_NAME(BEGIN), 420 DIALOG_NAME(FINAL), | 444 DIALOG_NAME(GRID_UP), 445 DIALOG_NAME(GRID_DOWN), 446 DIALOG_NAME(GRID_LEFT), 447 DIALOG_NAME(GRID_RIGHT), 448 DIALOG_NAME(DELETE_LEFT), 449 DIALOG_NAME(DELETE_RIGHT), 450 DIALOG_NAME(DELETE_ALL), 451 DIALOG_NAME(ENTER), 452 DIALOG_NAME(BEGIN), 453 DIALOG_NAME(FINAL), |
421 DIALOG_NAME(SELECT) | 454 DIALOG_NAME(SELECT), 455 DIALOG_NAME(HELPFILE), 456 DIALOG_NAME(TRACE) |
422}; 423 424static char * 425skip_white(char *s) 426{ 427 while (*s != '\0' && isspace(UCH(*s))) 428 ++s; 429 return s; --- 37 unchanged lines hidden (view full) --- 467 */ 468static int 469compare_bindings(LIST_BINDINGS * a, LIST_BINDINGS * b) 470{ 471 int result = 0; 472 if (a->win == b->win) { 473 if (!strcmp(a->name, b->name)) { 474 result = a->binding[0].curses_key - b->binding[0].curses_key; | 457}; 458 459static char * 460skip_white(char *s) 461{ 462 while (*s != '\0' && isspace(UCH(*s))) 463 ++s; 464 return s; --- 37 unchanged lines hidden (view full) --- 502 */ 503static int 504compare_bindings(LIST_BINDINGS * a, LIST_BINDINGS * b) 505{ 506 int result = 0; 507 if (a->win == b->win) { 508 if (!strcmp(a->name, b->name)) { 509 result = a->binding[0].curses_key - b->binding[0].curses_key; |
475 } else if (!strcmp(b->name, "*")) { | 510 } else if (!strcmp(b->name, WILDNAME)) { |
476 result = -1; | 511 result = -1; |
477 } else if (!strcmp(a->name, "*")) { | 512 } else if (!strcmp(a->name, WILDNAME)) { |
478 result = 1; 479 } else { 480 result = dlg_strcmp(a->name, b->name); 481 } 482 } else if (b->win) { 483 result = -1; 484 } else { 485 result = 1; --- 81 unchanged lines hidden (view full) --- 567 568 curses_key = -1; 569 dialog_key = -1; 570 widget = p; 571 572 p = skip_black(p); 573 if (p != widget && *p != '\0') { 574 *p++ = '\0'; | 513 result = 1; 514 } else { 515 result = dlg_strcmp(a->name, b->name); 516 } 517 } else if (b->win) { 518 result = -1; 519 } else { 520 result = 1; --- 81 unchanged lines hidden (view full) --- 602 603 curses_key = -1; 604 dialog_key = -1; 605 widget = p; 606 607 p = skip_black(p); 608 if (p != widget && *p != '\0') { 609 *p++ = '\0'; |
610 p = skip_white(p); |
|
575 q = p; 576 while (*p != '\0' && curses_key < 0) { 577 if (escaped) { 578 escaped = FALSE; 579 curses_key = *p; 580 } else if (*p == '\\') { 581 escaped = TRUE; 582 } else if (modified) { --- 25 unchanged lines hidden (view full) --- 608 int keynumber; 609 if (sscanf(q, "%[Ff]%d%c", fprefix, &keynumber, check) == 2) { 610 curses_key = KEY_F(keynumber); 611 is_function = TRUE; 612 } else { 613 for (xx = 0; xx < COUNT_CURSES; ++xx) { 614 if (!dlg_strcmp(curses_names[xx].name, q)) { 615 curses_key = curses_names[xx].code; | 611 q = p; 612 while (*p != '\0' && curses_key < 0) { 613 if (escaped) { 614 escaped = FALSE; 615 curses_key = *p; 616 } else if (*p == '\\') { 617 escaped = TRUE; 618 } else if (modified) { --- 25 unchanged lines hidden (view full) --- 644 int keynumber; 645 if (sscanf(q, "%[Ff]%d%c", fprefix, &keynumber, check) == 2) { 646 curses_key = KEY_F(keynumber); 647 is_function = TRUE; 648 } else { 649 for (xx = 0; xx < COUNT_CURSES; ++xx) { 650 if (!dlg_strcmp(curses_names[xx].name, q)) { 651 curses_key = curses_names[xx].code; |
616 is_function = TRUE; | 652 is_function = (curses_key >= KEY_MIN); |
617 break; 618 } 619 } 620 } 621 } 622 } 623 q = skip_white(p); 624 p = skip_black(q); --- 70 unchanged lines hidden (view full) --- 695{ 696 fprintf(fp, "bindkey %s ", widget); 697 dump_curses_key(fp, binding->curses_key); 698 fputc(' ', fp); 699 dump_dialog_key(fp, binding->dialog_key); 700 fputc('\n', fp); 701} 702 | 653 break; 654 } 655 } 656 } 657 } 658 } 659 q = skip_white(p); 660 p = skip_black(q); --- 70 unchanged lines hidden (view full) --- 731{ 732 fprintf(fp, "bindkey %s ", widget); 733 dump_curses_key(fp, binding->curses_key); 734 fputc(' ', fp); 735 dump_dialog_key(fp, binding->dialog_key); 736 fputc('\n', fp); 737} 738 |
739/* 740 * Dump bindings for the given window. If it is a null, then this dumps the 741 * initial bindings which were loaded from the rc-file that are used as 742 * overall defaults. 743 */ |
|
703void | 744void |
704dlg_dump_keys(FILE *fp) | 745dlg_dump_window_keys(FILE *fp, WINDOW *win) |
705{ | 746{ |
706 LIST_BINDINGS *p; 707 const char *last = ""; 708 unsigned n; 709 unsigned count = 0; | 747 if (fp != 0) { 748 LIST_BINDINGS *p; 749 DLG_KEYS_BINDING *q; 750 const char *last = ""; |
710 | 751 |
711 for (p = all_bindings; p != 0; p = p->link) { 712 if (p->win == 0) { 713 ++count; 714 } 715 } 716 if (count != 0) { 717 for (p = all_bindings, n = 0; p != 0; p = p->link) { 718 if (p->win == 0) { | 752 for (p = all_bindings; p != 0; p = p->link) { 753 if (p->win == win) { |
719 if (dlg_strcmp(last, p->name)) { 720 fprintf(fp, "\n# key bindings for %s widgets\n", | 754 if (dlg_strcmp(last, p->name)) { 755 fprintf(fp, "\n# key bindings for %s widgets\n", |
721 !strcmp(p->name, "*") ? "all" : p->name); | 756 !strcmp(p->name, WILDNAME) ? "all" : p->name); |
722 last = p->name; 723 } | 757 last = p->name; 758 } |
724 dump_one_binding(fp, p->name, p->binding); | 759 for (q = p->binding; q->is_function_key >= 0; ++q) { 760 dump_one_binding(fp, p->name, q); 761 } |
725 } 726 } 727 } 728} | 762 } 763 } 764 } 765} |
766 767/* 768 * Dump all of the bindings which are not specific to a given widget, i.e., 769 * the "win" member is null. 770 */ 771void 772dlg_dump_keys(FILE *fp) 773{ 774 if (fp != 0) { 775 LIST_BINDINGS *p; 776 unsigned count = 0; 777 778 for (p = all_bindings; p != 0; p = p->link) { 779 if (p->win == 0) { 780 ++count; 781 } 782 } 783 if (count != 0) { 784 dlg_dump_window_keys(fp, 0); 785 } 786 } 787} |
|
729#endif /* HAVE_RC_FILE */ | 788#endif /* HAVE_RC_FILE */ |