Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * yesno.c -- implements the yes/no box |
| 3 | * |
| 4 | * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk) |
| 5 | * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "dialog.h" |
| 23 | |
| 24 | /* |
| 25 | * Display termination buttons |
| 26 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 27 | static void print_buttons(WINDOW * dialog, int height, int width, int selected) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 29 | int x = width / 2 - 10; |
| 30 | int y = height - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 32 | print_button(dialog, " Yes ", y, x, selected == 0); |
| 33 | print_button(dialog, " No ", y, x + 13, selected == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 35 | wmove(dialog, y, x + 1 + 13 * selected); |
| 36 | wrefresh(dialog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Display a dialog box with two buttons - Yes and No |
| 41 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 42 | int dialog_yesno(const char *title, const char *prompt, int height, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 44 | int i, x, y, key = 0, button = 0; |
| 45 | WINDOW *dialog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 47 | /* center dialog box on screen */ |
| 48 | x = (COLS - width) / 2; |
| 49 | y = (LINES - height) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 51 | draw_shadow(stdscr, y, x, height, width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 53 | dialog = newwin(height, width, y, x); |
| 54 | keypad(dialog, TRUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 56 | draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); |
| 57 | wattrset(dialog, border_attr); |
| 58 | mvwaddch(dialog, height - 3, 0, ACS_LTEE); |
| 59 | for (i = 0; i < width - 2; i++) |
| 60 | waddch(dialog, ACS_HLINE); |
| 61 | wattrset(dialog, dialog_attr); |
| 62 | waddch(dialog, ACS_RTEE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 64 | if (title != NULL && strlen(title) >= width - 2) { |
| 65 | /* truncate long title -- mec */ |
| 66 | char *title2 = malloc(width - 2 + 1); |
| 67 | memcpy(title2, title, width - 2); |
| 68 | title2[width - 2] = '\0'; |
| 69 | title = title2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 72 | if (title != NULL) { |
| 73 | wattrset(dialog, title_attr); |
| 74 | mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' '); |
| 75 | waddstr(dialog, (char *)title); |
| 76 | waddch(dialog, ' '); |
| 77 | } |
| 78 | |
| 79 | wattrset(dialog, dialog_attr); |
| 80 | print_autowrap(dialog, prompt, width - 2, 1, 3); |
| 81 | |
| 82 | print_buttons(dialog, height, width, 0); |
| 83 | |
| 84 | while (key != ESC) { |
| 85 | key = wgetch(dialog); |
| 86 | switch (key) { |
| 87 | case 'Y': |
| 88 | case 'y': |
| 89 | delwin(dialog); |
| 90 | return 0; |
| 91 | case 'N': |
| 92 | case 'n': |
| 93 | delwin(dialog); |
| 94 | return 1; |
| 95 | |
| 96 | case TAB: |
| 97 | case KEY_LEFT: |
| 98 | case KEY_RIGHT: |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame^] | 99 | button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 100 | |
| 101 | print_buttons(dialog, height, width, button); |
| 102 | wrefresh(dialog); |
| 103 | break; |
| 104 | case ' ': |
| 105 | case '\n': |
| 106 | delwin(dialog); |
| 107 | return button; |
| 108 | case ESC: |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | delwin(dialog); |
| 114 | return -1; /* ESC pressed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |