yesno.c revision 72986
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#ifndef lint 22static const char rcsid[] = "$FreeBSD: head/gnu/lib/libdialog/yesno.c 72986 2001-02-24 18:52:56Z jkh $"; 23#endif 24 25#include <dialog.h> 26#include "dialog.priv.h" 27 28/* Actual work function */ 29static int dialog_yesno_proc(unsigned char *title, unsigned char *prompt, 30 int height, int width, int yesdefault); 31 32/* 33 * Display a dialog box with two buttons - Yes and No 34 */ 35int 36dialog_yesno(unsigned char *title, unsigned char *prompt, int height, int width) 37{ 38 return dialog_yesno_proc(title, prompt, height, width, TRUE); 39} 40 41/* 42 * Display a dialog box with two buttons - No and Yes 43 */ 44int 45dialog_noyes(unsigned char *title, unsigned char *prompt, int height, int width) 46{ 47 return dialog_yesno_proc(title, prompt, height, width, FALSE); 48} 49 50static int 51dialog_yesno_proc(unsigned char *title, unsigned char *prompt, int height, int width, int yesdefault) 52{ 53 int i, j, x, y, key, button; 54 WINDOW *dialog; 55 char *tmphlp; 56 57 /* disable helpline */ 58 tmphlp = get_helpline(); 59 use_helpline(NULL); 60 61 if (height < 0) 62 height = strheight(prompt)+4; 63 if (width < 0) { 64 i = strwidth(prompt); 65 j = ((title != NULL) ? strwidth(title) : 0); 66 width = MAX(i,j)+4; 67 } 68 width = MAX(width,23); 69 70 if (width > COLS) 71 width = COLS; 72 if (height > LINES) 73 height = LINES; 74 /* center dialog box on screen */ 75 x = DialogX ? DialogX : (COLS - width)/2; 76 y = DialogY ? DialogY : (LINES - height)/2; 77 78#ifdef HAVE_NCURSES 79 if (use_shadow) 80 draw_shadow(stdscr, y, x, height, width); 81#endif 82 dialog = newwin(height, width, y, x); 83 if (dialog == NULL) { 84 endwin(); 85 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x); 86 exit(1); 87 } 88 keypad(dialog, TRUE); 89 90 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 91 wattrset(dialog, border_attr); 92 wmove(dialog, height-3, 0); 93 waddch(dialog, ACS_LTEE); 94 for (i = 0; i < width-2; i++) 95 waddch(dialog, ACS_HLINE); 96 wattrset(dialog, dialog_attr); 97 waddch(dialog, ACS_RTEE); 98 wmove(dialog, height-2, 1); 99 for (i = 0; i < width-2; i++) 100 waddch(dialog, ' '); 101 102 if (title != NULL) { 103 wattrset(dialog, title_attr); 104 wmove(dialog, 0, (width - strlen(title))/2 - 1); 105 waddch(dialog, ' '); 106 waddstr(dialog, title); 107 waddch(dialog, ' '); 108 } 109 wattrset(dialog, dialog_attr); 110 wmove(dialog, 1, 2); 111 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE); 112 113 display_helpline(dialog, height-1, width); 114 115 x = width/2-10; 116 y = height-2; 117 118 /* preset button 0 or 1 for YES or NO as the default */ 119 key = 0; 120 button = !yesdefault; 121 while (key != ESC) { 122 print_button(dialog, " No ", y, x+13, button); 123 print_button(dialog, " Yes " , y, x, !button); 124 wrefresh(dialog); 125 126 key = wgetch(dialog); 127 switch (key) { 128 case 'Y': 129 case 'y': 130 delwin(dialog); 131 restore_helpline(tmphlp); 132 return 0; 133 case 'N': 134 case 'n': 135 delwin(dialog); 136 restore_helpline(tmphlp); 137 return 1; 138 case KEY_BTAB: 139 case TAB: 140 case KEY_UP: 141 case KEY_DOWN: 142 case KEY_LEFT: 143 case KEY_RIGHT: 144 button = !button; 145 /* redrawn at the loop's entry */ 146 break; 147 case ' ': 148 case '\r': 149 case '\n': 150 delwin(dialog); 151 restore_helpline(tmphlp); 152 return button; 153 case ESC: 154 break; 155 case KEY_F(1): 156 case '?': 157 display_helpfile(); 158 break; 159 } 160 } 161 162 delwin(dialog); 163 restore_helpline(tmphlp); 164 return -1; /* ESC pressed */ 165} 166/* End of dialog_yesno() */ 167