yesno.c revision 3950
1/* 2 * yesno.c -- implements the yes/no box 3 * 4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk) 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 21 22#include <dialog.h> 23#include "dialog.priv.h" 24 25 26/* 27 * Display a dialog box with two buttons - Yes and No 28 */ 29int dialog_yesno(unsigned char *title, unsigned char * prompt, int height, int width) 30{ 31 int i, x, y, key = 0, button = 0; 32 WINDOW *dialog; 33 34 /* center dialog box on screen */ 35 x = (COLS - width)/2; 36 y = (LINES - height)/2; 37 38#ifdef HAVE_NCURSES 39 if (use_shadow) 40 draw_shadow(stdscr, y, x, height, width); 41#endif 42 dialog = newwin(height, width, y, x); 43 keypad(dialog, TRUE); 44 45 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 46 wattrset(dialog, border_attr); 47 wmove(dialog, height-3, 0); 48 waddch(dialog, ACS_LTEE); 49 for (i = 0; i < width-2; i++) 50 waddch(dialog, ACS_HLINE); 51 wattrset(dialog, dialog_attr); 52 waddch(dialog, ACS_RTEE); 53 wmove(dialog, height-2, 1); 54 for (i = 0; i < width-2; i++) 55 waddch(dialog, ' '); 56 57 if (title != NULL) { 58 wattrset(dialog, title_attr); 59 wmove(dialog, 0, (width - strlen(title))/2 - 1); 60 waddch(dialog, ' '); 61 waddstr(dialog, title); 62 waddch(dialog, ' '); 63 } 64 wattrset(dialog, dialog_attr); 65 wmove(dialog, 1, 2); 66 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE); 67 68 x = width/2-10; 69 y = height-2; 70 print_button(dialog, " No ", y, x+13, FALSE); 71 print_button(dialog, " Yes ", y, x, TRUE); 72 wrefresh(dialog); 73 74 while (key != ESC) { 75 key = wgetch(dialog); 76 switch (key) { 77 case 'Y': 78 case 'y': 79 delwin(dialog); 80 return 0; 81 case 'N': 82 case 'n': 83 delwin(dialog); 84 return 1; 85 case KEY_BTAB: 86 case TAB: 87 case KEY_UP: 88 case KEY_DOWN: 89 case KEY_LEFT: 90 case KEY_RIGHT: 91 if (!button) { 92 button = 1; /* Indicates "No" button is selected */ 93 print_button(dialog, " Yes ", y, x, FALSE); 94 print_button(dialog, " No ", y, x+13, TRUE); 95 } 96 else { 97 button = 0; /* Indicates "Yes" button is selected */ 98 print_button(dialog, " No ", y, x+13, FALSE); 99 print_button(dialog, " Yes ", y, x, TRUE); 100 } 101 wrefresh(dialog); 102 break; 103 case ' ': 104 case '\r': 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 */ 115} 116/* End of dialog_yesno() */ 117