2 * inputbox.c -- implements the input box
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
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.
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.
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.
23 #include "dialog.priv.h"
27 * Display a dialog box for inputing a string
29 int dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int width, unsigned char *result)
31 int i, j, x, y, box_y, box_x, box_width, first,
33 unsigned char instr[MAX_LEN+1];
37 height = strheight(prompt)+2+4;
40 j = ((title != NULL) ? strwidth(title) : 0);
43 width = MAX(width,24);
49 /* center dialog box on screen */
50 x = DialogX ? DialogX : (COLS - width)/2;
51 y = DialogY ? DialogY : (LINES - height)/2;
55 draw_shadow(stdscr, y, x, height, width);
57 dialog = newwin(height, width, y, x);
60 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
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++)
78 wattrset(dialog, title_attr);
79 wmove(dialog, 0, (width - strlen(title))/2 - 1);
81 waddstr(dialog, title);
84 wattrset(dialog, dialog_attr);
86 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
88 /* Draw the input field box */
92 box_x = (width - box_width)/2;
93 draw_box(dialog, y+1, box_x-1, 3, box_width+2, border_attr, dialog_attr);
95 display_helpline(dialog, height-1, width);
99 print_button(dialog, "Cancel", y, x+14, FALSE);
100 print_button(dialog, " OK ", y, x, TRUE);
103 strcpy(instr, result);
104 wattrset(dialog, dialog_attr);
108 if (button == -1) { /* Input box selected */
109 key = line_edit(dialog, box_y, box_x, -1, box_width, inputbox_attr, first, instr, DialogInputAttrs);
113 key = wgetch(dialog);
119 strcpy(result, instr);
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);
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);
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);
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);
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);
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);
176 strcpy(result, instr);
177 return (button == -1 ? 0 : button);
188 return -1; /* ESC pressed */
190 /* End of dialog_inputbox() */