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 | */ |
| 27 | static void |
| 28 | print_buttons(WINDOW *dialog, int height, int width, int selected) |
| 29 | { |
| 30 | int x = width / 2 - 10; |
| 31 | int y = height - 2; |
| 32 | |
| 33 | print_button (dialog, " Yes ", y, x, selected == 0); |
| 34 | print_button (dialog, " No ", y, x + 13, selected == 1); |
| 35 | |
| 36 | wmove(dialog, y, x+1 + 13*selected ); |
| 37 | wrefresh (dialog); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Display a dialog box with two buttons - Yes and No |
| 42 | */ |
| 43 | int |
| 44 | dialog_yesno (const char *title, const char *prompt, int height, int width) |
| 45 | { |
| 46 | int i, x, y, key = 0, button = 0; |
| 47 | WINDOW *dialog; |
| 48 | |
| 49 | /* center dialog box on screen */ |
| 50 | x = (COLS - width) / 2; |
| 51 | y = (LINES - height) / 2; |
| 52 | |
| 53 | draw_shadow (stdscr, y, x, height, width); |
| 54 | |
| 55 | dialog = newwin (height, width, y, x); |
| 56 | keypad (dialog, TRUE); |
| 57 | |
| 58 | draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr); |
| 59 | wattrset (dialog, border_attr); |
| 60 | mvwaddch (dialog, height-3, 0, ACS_LTEE); |
| 61 | for (i = 0; i < width - 2; i++) |
| 62 | waddch (dialog, ACS_HLINE); |
| 63 | wattrset (dialog, dialog_attr); |
| 64 | waddch (dialog, ACS_RTEE); |
| 65 | |
| 66 | if (title != NULL && strlen(title) >= width-2 ) { |
| 67 | /* truncate long title -- mec */ |
| 68 | char * title2 = malloc(width-2+1); |
| 69 | memcpy( title2, title, width-2 ); |
| 70 | title2[width-2] = '\0'; |
| 71 | title = title2; |
| 72 | } |
| 73 | |
| 74 | if (title != NULL) { |
| 75 | wattrset (dialog, title_attr); |
| 76 | mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' '); |
| 77 | waddstr (dialog, (char *)title); |
| 78 | waddch (dialog, ' '); |
| 79 | } |
| 80 | |
| 81 | wattrset (dialog, dialog_attr); |
| 82 | print_autowrap (dialog, prompt, width - 2, 1, 3); |
| 83 | |
| 84 | print_buttons(dialog, height, width, 0); |
| 85 | |
| 86 | while (key != ESC) { |
| 87 | key = wgetch (dialog); |
| 88 | switch (key) { |
| 89 | case 'Y': |
| 90 | case 'y': |
| 91 | delwin (dialog); |
| 92 | return 0; |
| 93 | case 'N': |
| 94 | case 'n': |
| 95 | delwin (dialog); |
| 96 | return 1; |
| 97 | |
| 98 | case TAB: |
| 99 | case KEY_LEFT: |
| 100 | case KEY_RIGHT: |
| 101 | button = ((key == KEY_LEFT ? --button : ++button) < 0) |
| 102 | ? 1 : (button > 1 ? 0 : button); |
| 103 | |
| 104 | print_buttons(dialog, height, width, button); |
| 105 | wrefresh (dialog); |
| 106 | break; |
| 107 | case ' ': |
| 108 | case '\n': |
| 109 | delwin (dialog); |
| 110 | return button; |
| 111 | case ESC: |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | delwin (dialog); |
| 117 | return -1; /* ESC pressed */ |
| 118 | } |