yesno.c revision 4024
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 if (dialog == NULL) { 44 endwin(); 45 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x); 46 exit(1); 47 } 48 keypad(dialog, TRUE); 49 50 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 51 wattrset(dialog, border_attr); 52 wmove(dialog, height-3, 0); 53 waddch(dialog, ACS_LTEE); 54 for (i = 0; i < width-2; i++) 55 waddch(dialog, ACS_HLINE); 56 wattrset(dialog, dialog_attr); 57 waddch(dialog, ACS_RTEE); 58 wmove(dialog, height-2, 1); 59 for (i = 0; i < width-2; i++) 60 waddch(dialog, ' '); 61 62 if (title != NULL) { 63 wattrset(dialog, title_attr); 64 wmove(dialog, 0, (width - strlen(title))/2 - 1); 65 waddch(dialog, ' '); 66 waddstr(dialog, title); 67 waddch(dialog, ' '); 68 } 69 wattrset(dialog, dialog_attr); 70 wmove(dialog, 1, 2); 71 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE); 72 73 x = width/2-10; 74 y = height-2; 75 print_button(dialog, " No ", y, x+13, FALSE); 76 print_button(dialog, " Yes ", y, x, TRUE); 77 wrefresh(dialog); 78 79 while (key != ESC) { 80 key = wgetch(dialog); 81 switch (key) { 82 case 'Y': 83 case 'y': 84 delwin(dialog); 85 return 0; 86 case 'N': 87 case 'n': 88 delwin(dialog); 89 return 1; 90 case KEY_BTAB: 91 case TAB: 92 case KEY_UP: 93 case KEY_DOWN: 94 case KEY_LEFT: 95 case KEY_RIGHT: 96 if (!button) { 97 button = 1; /* Indicates "No" button is selected */ 98 print_button(dialog, " Yes ", y, x, FALSE); 99 print_button(dialog, " No ", y, x+13, TRUE); 100 } 101 else { 102 button = 0; /* Indicates "Yes" button is selected */ 103 print_button(dialog, " No ", y, x+13, FALSE); 104 print_button(dialog, " Yes ", y, x, TRUE); 105 } 106 wrefresh(dialog); 107 break; 108 case ' ': 109 case '\r': 110 case '\n': 111 delwin(dialog); 112 return button; 113 case ESC: 114 break; 115 } 116 } 117 118 delwin(dialog); 119 return -1; /* ESC pressed */ 120} 121/* End of dialog_yesno() */ 122