2 * $Id: buttons.c,v 1.94 2012/12/30 20:51:01 tom Exp $
4 * buttons.c -- draw buttons, e.g., OK/Cancel
6 * Copyright 2000-2011,2012 Thomas E. Dickey
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.
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
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
31 #define MIN_BUTTON (-dialog_state.visit_cols)
34 center_label(char *buffer, int longest, const char *label)
36 int len = dlg_count_columns(label);
37 int left = 0, right = 0;
41 left = (longest - len) / 2;
42 right = (longest - len - left);
44 sprintf(buffer, "%*s", left, " ");
46 strcat(buffer, label);
48 sprintf(buffer + strlen(buffer), "%*s", right, " ");
52 * Parse a multibyte character out of the string, set it past the parsed
56 string_to_char(const char **stringp)
59 #ifdef USE_WIDE_CURSES
60 const char *string = *stringp;
61 size_t have = strlen(string);
67 memset(&state, 0, sizeof(state));
68 len = mbrlen(string, have, &state);
69 if ((int) len > 0 && len <= have) {
70 memset(&state, 0, sizeof(state));
71 memset(cmp2, 0, sizeof(cmp2));
72 check = mbrtowc(cmp2, string, len, &state);
77 cmp2[0] = UCH(*string);
82 const char *string = *stringp;
83 result = UCH(*string);
90 count_labels(const char **labels)
94 while (*labels++ != 0) {
102 * Check if the latest key should be added to the hotkey list.
105 was_hotkey(int this_key, int *used_keys, size_t next)
111 for (n = 0; n < next; ++n) {
112 if (used_keys[n] == this_key) {
122 * Determine the hot-keys for a set of button-labels. Normally these are
123 * the first uppercase character in each label. However, if more than one
124 * button has the same first-uppercase, then we will (attempt to) look for
127 * This allocates data which must be freed by the caller.
130 get_hotkeys(const char **labels)
133 size_t count = count_labels(labels);
136 if ((result = dlg_calloc(int, count + 1)) != 0) {
137 for (n = 0; n < count; ++n) {
138 const char *label = labels[n];
139 const int *indx = dlg_index_wchars(label);
140 int limit = dlg_count_wchars(label);
143 for (i = 0; i < limit; ++i) {
145 int check = UCH(label[first]);
146 #ifdef USE_WIDE_CURSES
147 int last = indx[i + 1];
148 if ((last - first) != 1) {
149 const char *temp = (label + first);
150 check = string_to_char(&temp);
153 if (dlg_isupper(check) && !was_hotkey(check, result, n)) {
167 print_button(WINDOW *win, char *label, int hotkey, int y, int x, int selected)
171 const int *indx = dlg_index_wchars(label);
172 int limit = dlg_count_wchars(label);
173 chtype key_attr = (selected
174 ? button_key_active_attr
175 : button_key_inactive_attr);
176 chtype label_attr = (selected
177 ? button_label_active_attr
178 : button_label_inactive_attr);
180 (void) wmove(win, y, x);
181 (void) wattrset(win, selected
183 : button_inactive_attr);
184 (void) waddstr(win, "<");
185 (void) wattrset(win, label_attr);
186 for (i = 0; i < limit; ++i) {
189 int last = indx[i + 1];
193 check = UCH(label[first]);
194 #ifdef USE_WIDE_CURSES
195 if ((last - first) != 1) {
196 const char *temp = (label + first);
197 check = string_to_char(&temp);
200 if (check == hotkey) {
201 (void) wattrset(win, key_attr);
206 wattrset(win, label_attr);
210 waddnstr(win, label + first, last - first);
212 (void) wattrset(win, selected
214 : button_inactive_attr);
215 (void) waddstr(win, ">");
216 (void) wmove(win, y, x + ((int) strspn(label, " ")) + 1);
220 * Count the buttons in the list.
223 dlg_button_count(const char **labels)
226 while (*labels++ != 0)
232 * Compute the size of the button array in columns. Return the total number of
233 * columns in *length, and the longest button's columns in *longest
236 dlg_button_sizes(const char **labels,
245 for (n = 0; labels[n] != 0; n++) {
250 int len = dlg_count_columns(labels[n]);
257 * If we can, make all of the buttons the same size. This is only optional
258 * for buttons laid out horizontally.
260 if (*longest < 6 - (*longest & 1))
261 *longest = 6 - (*longest & 1);
263 *length = *longest * n;
267 * Compute the size of the button array.
270 dlg_button_x_step(const char **labels, int limit, int *gap, int *margin, int *step)
272 int count = dlg_button_count(labels);
281 dlg_button_sizes(labels, FALSE, &longest, &length);
282 used = (length + (count * 2));
283 unused = limit - used;
285 if ((*gap = unused / (count + 3)) <= 0) {
286 if ((*gap = unused / (count + 1)) <= 0)
292 *step = *gap + (used + count - 1) / count;
293 result = (*gap > 0) && (unused >= 0);
301 * Make sure there is enough space for the buttons
304 dlg_button_layout(const char **labels, int *limit)
307 int gap, margin, step;
309 if (labels != 0 && dlg_button_count(labels)) {
310 while (!dlg_button_x_step(labels, width, &gap, &margin, &step))
312 width += (4 * MARGIN);
321 * Print a list of buttons at the given position.
324 dlg_draw_buttons(WINDOW *win,
331 chtype save = dlg_get_attrs(win);
343 dlg_mouse_setbase(getbegx(win), getbegy(win));
345 getyx(win, final_y, final_x);
347 dlg_button_sizes(labels, vertical, &longest, &length);
353 dlg_button_x_step(labels, limit, &gap, &margin, &step);
358 * Allocate a buffer big enough for any label.
360 need = (size_t) longest;
362 int *hotkeys = get_hotkeys(labels);
363 assert_ptr(hotkeys, "dlg_draw_buttons");
365 for (n = 0; labels[n] != 0; ++n) {
366 need += strlen(labels[n]) + 1;
368 buffer = dlg_malloc(char, need);
369 assert_ptr(buffer, "dlg_draw_buttons");
374 for (n = 0; labels[n] != 0; n++) {
375 center_label(buffer, longest, labels[n]);
376 mouse_mkbutton(y, x, dlg_count_columns(buffer), n);
377 print_button(win, buffer, hotkeys[n], y, x,
378 (selected == n) || (n == 0 && selected < 0));
380 getyx(win, final_y, final_x);
383 if ((y += step) > limit)
386 if ((x += step) > limit)
390 (void) wmove(win, final_y, final_x);
392 (void) wattrset(win, save);
399 * Match a given character against the beginning of the string, ignoring case
400 * of the given character. The matching string must begin with an uppercase
404 dlg_match_char(int ch, const char *string)
407 int cmp2 = string_to_char(&string);
408 #ifdef USE_WIDE_CURSES
409 wint_t cmp1 = dlg_toupper(ch);
410 if (cmp2 != 0 && (wchar_t) cmp1 == (wchar_t) dlg_toupper(cmp2)) {
414 if (ch > 0 && ch < 256) {
415 if (dlg_toupper(ch) == dlg_toupper(cmp2))
424 * Find the first uppercase character in the label, which we may use for an
428 dlg_button_to_char(const char *label)
432 while (*label != 0) {
433 cmp = string_to_char(&label);
434 if (dlg_isupper(cmp)) {
442 * Given a list of button labels, and a character which may be the abbreviation
443 * for one, find it, if it exists. An abbreviation will be the first character
444 * which happens to be capitalized in the label.
447 dlg_char_to_button(int ch, const char **labels)
449 int result = DLG_EXIT_UNKNOWN;
452 int *hotkeys = get_hotkeys(labels);
455 ch = (int) dlg_toupper(dlg_last_getc());
458 for (j = 0; labels[j] != 0; ++j) {
459 if (ch == hotkeys[j]) {
475 return (dialog_vars.yes_label != NULL)
476 ? dialog_vars.yes_label
483 return (dialog_vars.no_label != NULL)
484 ? dialog_vars.no_label
491 return (dialog_vars.ok_label != NULL)
492 ? dialog_vars.ok_label
497 my_cancel_label(void)
499 return (dialog_vars.cancel_label != NULL)
500 ? dialog_vars.cancel_label
507 return (dialog_vars.exit_label != NULL)
508 ? dialog_vars.exit_label
515 return (dialog_vars.extra_label != NULL)
516 ? dialog_vars.extra_label
523 return (dialog_vars.help_label != NULL)
524 ? dialog_vars.help_label
529 * Return a list of button labels.
537 if (dialog_vars.extra_button) {
538 dlg_save_vars(&save);
539 dialog_vars.nocancel = TRUE;
540 result = dlg_ok_labels();
541 dlg_restore_vars(&save);
543 static const char *labels[3];
546 if (!dialog_vars.nook)
547 labels[n++] = my_exit_label();
548 if (dialog_vars.help_button)
549 labels[n++] = my_help_label();
551 labels[n++] = my_exit_label();
560 * Map the given button index for dlg_exit_label() into our exit-code.
563 dlg_exit_buttoncode(int button)
568 dlg_save_vars(&save);
569 dialog_vars.nocancel = TRUE;
571 result = dlg_ok_buttoncode(button);
573 dlg_restore_vars(&save);
581 static const char *labels[4];
584 labels[n++] = my_ok_label();
585 if (dialog_vars.extra_button)
586 labels[n++] = my_extra_label();
587 if (dialog_vars.help_button)
588 labels[n++] = my_help_label();
594 * Return a list of button labels for the OK/Cancel group.
599 static const char *labels[5];
602 if (!dialog_vars.nook)
603 labels[n++] = my_ok_label();
604 if (dialog_vars.extra_button)
605 labels[n++] = my_extra_label();
606 if (!dialog_vars.nocancel)
607 labels[n++] = my_cancel_label();
608 if (dialog_vars.help_button)
609 labels[n++] = my_help_label();
615 * Map the given button index for dlg_ok_labels() into our exit-code
618 dlg_ok_buttoncode(int button)
620 int result = DLG_EXIT_ERROR;
621 int n = !dialog_vars.nook;
623 if (!dialog_vars.nook && (button <= 0)) {
624 result = DLG_EXIT_OK;
625 } else if (dialog_vars.extra_button && (button == n++)) {
626 result = DLG_EXIT_EXTRA;
627 } else if (!dialog_vars.nocancel && (button == n++)) {
628 result = DLG_EXIT_CANCEL;
629 } else if (dialog_vars.help_button && (button == n)) {
630 result = DLG_EXIT_HELP;
632 dlg_trace_msg("# dlg_ok_buttoncode(%d) = %d\n", button, result);
637 * Given that we're using dlg_ok_labels() to list buttons, find the next index
638 * in the list of buttons. The 'extra' parameter if negative provides a way to
639 * enumerate extra active areas on the widget.
642 dlg_next_ok_buttonindex(int current, int extra)
644 int result = current + 1;
647 && dlg_ok_buttoncode(result) < 0)
653 * Similarly, find the previous button index.
656 dlg_prev_ok_buttonindex(int current, int extra)
658 int result = current - 1;
660 if (result < extra) {
661 for (result = 0; dlg_ok_buttoncode(result + 1) >= 0; ++result) {
669 * Find the button-index for the "OK" or "Cancel" button, according to
670 * whether --defaultno is given. If --nocancel was given, we always return
671 * the index for the first button (usually "OK" unless --nook was used).
674 dlg_defaultno_button(void)
678 if (dialog_vars.defaultno && !dialog_vars.nocancel) {
679 while (dlg_ok_buttoncode(result) != DLG_EXIT_CANCEL)
682 dlg_trace_msg("# dlg_defaultno_button() = %d\n", result);
687 * Find the button-index for a button named with --default-button. If the
688 * option was not specified, or if the selected button does not exist, return
689 * the index of the first button (usually "OK" unless --nook was used).
692 dlg_default_button(void)
697 if (dialog_vars.default_button >= 0) {
698 for (i = 0; (n = dlg_ok_buttoncode(i)) >= 0; i++) {
699 if (n == dialog_vars.default_button) {
705 dlg_trace_msg("# dlg_default_button() = %d\n", result);
710 * Return a list of buttons for Yes/No labels.
717 if (dialog_vars.extra_button) {
718 result = dlg_ok_labels();
720 static const char *labels[4];
723 labels[n++] = my_yes_label();
724 labels[n++] = my_no_label();
725 if (dialog_vars.help_button)
726 labels[n++] = my_help_label();
736 * Map the given button index for dlg_yes_labels() into our exit-code.
739 dlg_yes_buttoncode(int button)
741 int result = DLG_EXIT_ERROR;
743 if (dialog_vars.extra_button) {
744 result = dlg_ok_buttoncode(button);
745 } else if (button == 0) {
746 result = DLG_EXIT_OK;
747 } else if (button == 1) {
748 result = DLG_EXIT_CANCEL;
749 } else if (button == 2 && dialog_vars.help_button) {
750 result = DLG_EXIT_HELP;
757 * Return the next index in labels[];
760 dlg_next_button(const char **labels, int button)
765 if (labels[button + 1] != 0) {
774 * Return the previous index in labels[];
777 dlg_prev_button(const char **labels, int button)
779 if (button > MIN_BUTTON) {
785 while (labels[button + 1] != 0)