14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin * $Id: yesno.c,v 1.71 2020/11/23 00:48:08 tom Exp $
34c8945a0SNathan Whitehorn *
44c8945a0SNathan Whitehorn * yesno.c -- implements the yes/no box
54c8945a0SNathan Whitehorn *
6*a96ef450SBaptiste Daroussin * Copyright 1999-2019,2020 Thomas E. Dickey
74c8945a0SNathan Whitehorn *
84c8945a0SNathan Whitehorn * This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn * it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn * as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn *
124c8945a0SNathan Whitehorn * This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn * WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
154c8945a0SNathan Whitehorn * Lesser General Public License for more details.
164c8945a0SNathan Whitehorn *
174c8945a0SNathan Whitehorn * You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn * License along with this program; if not, write to
194c8945a0SNathan Whitehorn * Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn * 51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn * Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn *
234c8945a0SNathan Whitehorn * An earlier version of this program lists as authors
244c8945a0SNathan Whitehorn * Savio Lam (lam836@cs.cuhk.hk)
254c8945a0SNathan Whitehorn */
264c8945a0SNathan Whitehorn
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
294c8945a0SNathan Whitehorn
304c8945a0SNathan Whitehorn /*
314c8945a0SNathan Whitehorn * Display a dialog box with two buttons - Yes and No.
324c8945a0SNathan Whitehorn */
334c8945a0SNathan Whitehorn int
dialog_yesno(const char * title,const char * cprompt,int height,int width)344c8945a0SNathan Whitehorn dialog_yesno(const char *title, const char *cprompt, int height, int width)
354c8945a0SNathan Whitehorn {
364c8945a0SNathan Whitehorn /* *INDENT-OFF* */
374c8945a0SNathan Whitehorn static DLG_KEYS_BINDING binding[] = {
38682c9e0fSNathan Whitehorn HELPKEY_BINDINGS,
394c8945a0SNathan Whitehorn ENTERKEY_BINDINGS,
404c8945a0SNathan Whitehorn SCROLLKEY_BINDINGS,
41f4f33ea0SBaptiste Daroussin TRAVERSE_BINDINGS,
424c8945a0SNathan Whitehorn END_KEYS_BINDING
434c8945a0SNathan Whitehorn };
444c8945a0SNathan Whitehorn /* *INDENT-ON* */
454c8945a0SNathan Whitehorn
464c8945a0SNathan Whitehorn int x, y;
47*a96ef450SBaptiste Daroussin int key, fkey;
482a3e3873SBaptiste Daroussin int button = dlg_default_button();
494c8945a0SNathan Whitehorn WINDOW *dialog = 0;
504c8945a0SNathan Whitehorn int result = DLG_EXIT_UNKNOWN;
51f4f33ea0SBaptiste Daroussin char *prompt;
524c8945a0SNathan Whitehorn const char **buttons = dlg_yes_labels();
534c8945a0SNathan Whitehorn int min_width = 25;
544c8945a0SNathan Whitehorn bool show = TRUE;
554c8945a0SNathan Whitehorn int page, last = 0, offset = 0;
564c8945a0SNathan Whitehorn
574c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
584c8945a0SNathan Whitehorn int req_high = height;
594c8945a0SNathan Whitehorn int req_wide = width;
604c8945a0SNathan Whitehorn #endif
614c8945a0SNathan Whitehorn
62f4f33ea0SBaptiste Daroussin DLG_TRACE(("# yesno args:\n"));
63f4f33ea0SBaptiste Daroussin DLG_TRACE2S("title", title);
64f4f33ea0SBaptiste Daroussin DLG_TRACE2S("message", cprompt);
65f4f33ea0SBaptiste Daroussin DLG_TRACE2N("height", height);
66f4f33ea0SBaptiste Daroussin DLG_TRACE2N("width", width);
67f4f33ea0SBaptiste Daroussin
68f4f33ea0SBaptiste Daroussin #ifdef KEY_RESIZE
69f4f33ea0SBaptiste Daroussin restart:
70f4f33ea0SBaptiste Daroussin #endif
71f4f33ea0SBaptiste Daroussin prompt = dlg_strclone(cprompt);
724c8945a0SNathan Whitehorn dlg_tab_correct_str(prompt);
734c8945a0SNathan Whitehorn dlg_button_layout(buttons, &min_width);
744c8945a0SNathan Whitehorn dlg_auto_size(title, prompt, &height, &width, 2, min_width);
754c8945a0SNathan Whitehorn dlg_print_size(height, width);
764c8945a0SNathan Whitehorn dlg_ctl_size(height, width);
774c8945a0SNathan Whitehorn
784c8945a0SNathan Whitehorn x = dlg_box_x_ordinate(width);
794c8945a0SNathan Whitehorn y = dlg_box_y_ordinate(height);
804c8945a0SNathan Whitehorn
814c8945a0SNathan Whitehorn dialog = dlg_new_window(height, width, y, x);
824c8945a0SNathan Whitehorn dlg_register_window(dialog, "yesno", binding);
834c8945a0SNathan Whitehorn dlg_register_buttons(dialog, "yesno", buttons);
844c8945a0SNathan Whitehorn
852a3e3873SBaptiste Daroussin dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
862a3e3873SBaptiste Daroussin dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
874c8945a0SNathan Whitehorn dlg_draw_title(dialog, title);
88682c9e0fSNathan Whitehorn dlg_draw_helpline(dialog, FALSE);
894c8945a0SNathan Whitehorn
90f4f33ea0SBaptiste Daroussin dlg_attrset(dialog, dialog_attr);
914c8945a0SNathan Whitehorn
924c8945a0SNathan Whitehorn page = height - (1 + 3 * MARGIN);
934c8945a0SNathan Whitehorn dlg_draw_buttons(dialog,
944c8945a0SNathan Whitehorn height - 2 * MARGIN, 0,
954c8945a0SNathan Whitehorn buttons, button, FALSE, width);
964c8945a0SNathan Whitehorn
974c8945a0SNathan Whitehorn while (result == DLG_EXIT_UNKNOWN) {
98*a96ef450SBaptiste Daroussin int code;
99*a96ef450SBaptiste Daroussin
1004c8945a0SNathan Whitehorn if (show) {
1014c8945a0SNathan Whitehorn last = dlg_print_scrolled(dialog, prompt, offset,
1024c8945a0SNathan Whitehorn page, width, TRUE);
1032a3e3873SBaptiste Daroussin dlg_trace_win(dialog);
1044c8945a0SNathan Whitehorn show = FALSE;
1054c8945a0SNathan Whitehorn }
1064c8945a0SNathan Whitehorn key = dlg_mouse_wgetch(dialog, &fkey);
107*a96ef450SBaptiste Daroussin if (dlg_result_key(key, fkey, &result)) {
108*a96ef450SBaptiste Daroussin if (!dlg_button_key(result, &button, &key, &fkey))
1094c8945a0SNathan Whitehorn break;
110*a96ef450SBaptiste Daroussin }
1114c8945a0SNathan Whitehorn if ((code = dlg_char_to_button(key, buttons)) >= 0) {
1124c8945a0SNathan Whitehorn result = dlg_ok_buttoncode(code);
1134c8945a0SNathan Whitehorn break;
1144c8945a0SNathan Whitehorn }
1154c8945a0SNathan Whitehorn /* handle function keys */
1164c8945a0SNathan Whitehorn if (fkey) {
1174c8945a0SNathan Whitehorn switch (key) {
1184c8945a0SNathan Whitehorn case DLGK_FIELD_NEXT:
1194c8945a0SNathan Whitehorn button = dlg_next_button(buttons, button);
1204c8945a0SNathan Whitehorn if (button < 0)
1214c8945a0SNathan Whitehorn button = 0;
1224c8945a0SNathan Whitehorn dlg_draw_buttons(dialog,
1234c8945a0SNathan Whitehorn height - 2, 0,
1244c8945a0SNathan Whitehorn buttons, button,
1254c8945a0SNathan Whitehorn FALSE, width);
1264c8945a0SNathan Whitehorn break;
1274c8945a0SNathan Whitehorn case DLGK_FIELD_PREV:
1284c8945a0SNathan Whitehorn button = dlg_prev_button(buttons, button);
1294c8945a0SNathan Whitehorn if (button < 0)
1304c8945a0SNathan Whitehorn button = 0;
1314c8945a0SNathan Whitehorn dlg_draw_buttons(dialog,
1324c8945a0SNathan Whitehorn height - 2, 0,
1334c8945a0SNathan Whitehorn buttons, button,
1344c8945a0SNathan Whitehorn FALSE, width);
1354c8945a0SNathan Whitehorn break;
1364c8945a0SNathan Whitehorn case DLGK_ENTER:
137*a96ef450SBaptiste Daroussin case DLGK_LEAVE:
1384c8945a0SNathan Whitehorn result = dlg_yes_buttoncode(button);
1394c8945a0SNathan Whitehorn break;
1404c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1414c8945a0SNathan Whitehorn case KEY_RESIZE:
142f4f33ea0SBaptiste Daroussin dlg_will_resize(dialog);
1434c8945a0SNathan Whitehorn height = req_high;
1444c8945a0SNathan Whitehorn width = req_wide;
145f4f33ea0SBaptiste Daroussin show = TRUE;
146*a96ef450SBaptiste Daroussin free(prompt);
147*a96ef450SBaptiste Daroussin _dlg_resize_cleanup(dialog);
1484c8945a0SNathan Whitehorn goto restart;
1494c8945a0SNathan Whitehorn #endif
1504c8945a0SNathan Whitehorn default:
1514c8945a0SNathan Whitehorn if (is_DLGK_MOUSE(key)) {
1524c8945a0SNathan Whitehorn result = dlg_yes_buttoncode(key - M_EVENT);
1534c8945a0SNathan Whitehorn if (result < 0)
1544c8945a0SNathan Whitehorn result = DLG_EXIT_OK;
1554c8945a0SNathan Whitehorn } else if (dlg_check_scrolled(key, last, page,
1564c8945a0SNathan Whitehorn &show, &offset) != 0) {
1574c8945a0SNathan Whitehorn beep();
1584c8945a0SNathan Whitehorn }
1594c8945a0SNathan Whitehorn break;
1604c8945a0SNathan Whitehorn }
161*a96ef450SBaptiste Daroussin } else if (key > 0) {
1624c8945a0SNathan Whitehorn beep();
1634c8945a0SNathan Whitehorn }
1644c8945a0SNathan Whitehorn }
165*a96ef450SBaptiste Daroussin dlg_add_last_key(-1);
1664c8945a0SNathan Whitehorn
1674c8945a0SNathan Whitehorn dlg_del_window(dialog);
1684c8945a0SNathan Whitehorn dlg_mouse_free_regions();
1694c8945a0SNathan Whitehorn free(prompt);
1704c8945a0SNathan Whitehorn return result;
1714c8945a0SNathan Whitehorn }
172