0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "dialog.h"
0010
0011
0012
0013
0014 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
0015 {
0016 int x = width / 2 - 10;
0017 int y = height - 2;
0018
0019 print_button(dialog, " Yes ", y, x, selected == 0);
0020 print_button(dialog, " No ", y, x + 13, selected == 1);
0021
0022 wmove(dialog, y, x + 1 + 13 * selected);
0023 wrefresh(dialog);
0024 }
0025
0026
0027
0028
0029 int dialog_yesno(const char *title, const char *prompt, int height, int width)
0030 {
0031 int i, x, y, key = 0, button = 0;
0032 WINDOW *dialog;
0033
0034 do_resize:
0035 if (getmaxy(stdscr) < (height + YESNO_HEIGTH_MIN))
0036 return -ERRDISPLAYTOOSMALL;
0037 if (getmaxx(stdscr) < (width + YESNO_WIDTH_MIN))
0038 return -ERRDISPLAYTOOSMALL;
0039
0040
0041 x = (getmaxx(stdscr) - width) / 2;
0042 y = (getmaxy(stdscr) - height) / 2;
0043
0044 draw_shadow(stdscr, y, x, height, width);
0045
0046 dialog = newwin(height, width, y, x);
0047 keypad(dialog, TRUE);
0048
0049 draw_box(dialog, 0, 0, height, width,
0050 dlg.dialog.atr, dlg.border.atr);
0051 wattrset(dialog, dlg.border.atr);
0052 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
0053 for (i = 0; i < width - 2; i++)
0054 waddch(dialog, ACS_HLINE);
0055 wattrset(dialog, dlg.dialog.atr);
0056 waddch(dialog, ACS_RTEE);
0057
0058 print_title(dialog, title, width);
0059
0060 wattrset(dialog, dlg.dialog.atr);
0061 print_autowrap(dialog, prompt, width - 2, 1, 3);
0062
0063 print_buttons(dialog, height, width, 0);
0064
0065 while (key != KEY_ESC) {
0066 key = wgetch(dialog);
0067 switch (key) {
0068 case 'Y':
0069 case 'y':
0070 delwin(dialog);
0071 return 0;
0072 case 'N':
0073 case 'n':
0074 delwin(dialog);
0075 return 1;
0076
0077 case TAB:
0078 case KEY_LEFT:
0079 case KEY_RIGHT:
0080 button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
0081
0082 print_buttons(dialog, height, width, button);
0083 wrefresh(dialog);
0084 break;
0085 case ' ':
0086 case '\n':
0087 delwin(dialog);
0088 return button;
0089 case KEY_ESC:
0090 key = on_key_esc(dialog);
0091 break;
0092 case KEY_RESIZE:
0093 delwin(dialog);
0094 on_key_resize();
0095 goto do_resize;
0096 }
0097 }
0098
0099 delwin(dialog);
0100 return key;
0101 }