1/* 2 * inputbox.c -- implements the input 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 for inputing a string 28 */ 29int dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int width, unsigned char *result) 30{ 31 int i, j, x, y, box_y, box_x, box_width, first, 32 key = 0, button = -1; 33 unsigned char instr[MAX_LEN+1]; 34 WINDOW *dialog; 35 36 if (height < 0) 37 height = strheight(prompt)+2+4; 38 if (width < 0) { 39 i = strwidth(prompt); 40 j = ((title != NULL) ? strwidth(title) : 0); 41 width = MAX(i,j) + 4; 42 } 43 width = MAX(width,24); 44 45 if (width > COLS) 46 width = COLS; 47 if (height > LINES) 48 height = LINES; 49 /* center dialog box on screen */ 50 x = DialogX ? DialogX : (COLS - width)/2; 51 y = DialogY ? DialogY : (LINES - height)/2; 52 53#ifdef HAVE_NCURSES 54 if (use_shadow) 55 draw_shadow(stdscr, y, x, height, width); 56#endif 57 dialog = newwin(height, width, y, x); 58 if (dialog == NULL) { 59 endwin(); 60 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x); 61 exit(1); 62 } 63 keypad(dialog, TRUE); 64 65 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 66 wattrset(dialog, border_attr); 67 wmove(dialog, height-3, 0); 68 waddch(dialog, ACS_LTEE); 69 for (i = 0; i < width-2; i++) 70 waddch(dialog, ACS_HLINE); 71 wattrset(dialog, dialog_attr); 72 waddch(dialog, ACS_RTEE); 73 wmove(dialog, height-2, 1); 74 for (i = 0; i < width-2; i++) 75 waddch(dialog, ' '); 76 77 if (title != NULL) { 78 wattrset(dialog, title_attr); 79 wmove(dialog, 0, (width - strlen(title))/2 - 1); 80 waddch(dialog, ' '); 81 waddstr(dialog, title); 82 waddch(dialog, ' '); 83 } 84 wattrset(dialog, dialog_attr); 85 wmove(dialog, 1, 2); 86 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE); 87 88 /* Draw the input field box */ 89 box_width = width-6; 90 getyx(dialog, y, x); 91 box_y = y + 2; 92 box_x = (width - box_width)/2; 93 draw_box(dialog, y+1, box_x-1, 3, box_width+2, border_attr, dialog_attr); 94 95 display_helpline(dialog, height-1, width); 96 97 x = width/2-11; 98 y = height-2; 99 print_button(dialog, "Cancel", y, x+14, FALSE); 100 print_button(dialog, " OK ", y, x, TRUE); 101 102 first = 1; 103 strcpy(instr, result); 104 wattrset(dialog, dialog_attr); 105 106 while (key != ESC) { 107 108 if (button == -1) { /* Input box selected */ 109 key = line_edit(dialog, box_y, box_x, -1, box_width, inputbox_attr, first, instr, DialogInputAttrs); 110 first = 0; 111 } 112 else 113 key = wgetch(dialog); 114 115 switch (key) { 116 case 'O': 117 case 'o': 118 delwin(dialog); 119 strcpy(result, instr); 120 return 0; 121 case 'C': 122 case 'c': 123 delwin(dialog); 124 return 1; 125 case KEY_UP: 126 case KEY_LEFT: 127 case KEY_BTAB: 128 switch (button) { 129 case -1: 130 button = 1; /* Indicates "Cancel" button is selected */ 131 print_button(dialog, " OK ", y, x, FALSE); 132 print_button(dialog, "Cancel", y, x+14, TRUE); 133 wrefresh(dialog); 134 break; 135 case 0: 136 button = -1; /* Indicates input box is selected */ 137 print_button(dialog, "Cancel", y, x+14, FALSE); 138 print_button(dialog, " OK ", y, x, TRUE); 139 break; 140 case 1: 141 button = 0; /* Indicates "OK" button is selected */ 142 print_button(dialog, "Cancel", y, x+14, FALSE); 143 print_button(dialog, " OK ", y, x, TRUE); 144 wrefresh(dialog); 145 break; 146 } 147 break; 148 case TAB: 149 case KEY_DOWN: 150 case KEY_RIGHT: 151 switch (button) { 152 case -1: 153 button = 0; /* Indicates "OK" button is selected */ 154 print_button(dialog, "Cancel", y, x+14, FALSE); 155 print_button(dialog, " OK ", y, x, TRUE); 156 wrefresh(dialog); 157 break; 158 case 0: 159 button = 1; /* Indicates "Cancel" button is selected */ 160 print_button(dialog, " OK ", y, x, FALSE); 161 print_button(dialog, "Cancel", y, x+14, TRUE); 162 wrefresh(dialog); 163 break; 164 case 1: 165 button = -1; /* Indicates input box is selected */ 166 print_button(dialog, "Cancel", y, x+14, FALSE); 167 print_button(dialog, " OK ", y, x, TRUE); 168 break; 169 } 170 break; 171 case ' ': 172 case '\n': 173 case '\r': 174 delwin(dialog); 175 if (button < 1) 176 strcpy(result, instr); 177 return (button == -1 ? 0 : button); 178 case ESC: 179 break; 180 case KEY_F(1): 181 case '?': 182 display_helpfile(); 183 break; 184 } 185 } 186 187 delwin(dialog); 188 return -1; /* ESC pressed */ 189} 190/* End of dialog_inputbox() */ 191