This source file includes following definitions.
- print_buttons
- dialog_inputbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include "dialog.h"
23
24 unsigned char dialog_input_result[MAX_LEN + 1];
25
26
27
28
29 static void
30 print_buttons(WINDOW *dialog, int height, int width, int okval, int cancelval)
31 {
32 int x = width / 2 - 11;
33 int y = height - 2;
34
35 print_button (dialog, " Ok ", y, x, okval);
36 print_button (dialog, " Help ", y, x + 14, cancelval);
37
38 wrefresh(dialog);
39 }
40
41
42
43
44 int
45 dialog_inputbox (const char *title, const char *prompt, int height, int width,
46 const char *init)
47 {
48 int i, x, y, box_y, box_x, box_width;
49 int input_x = 0, scroll = 0, key = 0, button = -1;
50 unsigned char *instr = dialog_input_result;
51 WINDOW *dialog;
52
53
54 x = (COLS - width) / 2;
55 y = (LINES - height) / 2;
56
57
58 draw_shadow (stdscr, y, x, height, width);
59
60 dialog = newwin (height, width, y, x);
61 keypad (dialog, TRUE);
62
63 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
64 wattrset (dialog, border_attr);
65 mvwaddch (dialog, height-3, 0, ACS_LTEE);
66 for (i = 0; i < width - 2; i++)
67 waddch (dialog, ACS_HLINE);
68 wattrset (dialog, dialog_attr);
69 waddch (dialog, ACS_RTEE);
70
71 if (title != NULL) {
72 wattrset (dialog, title_attr);
73 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
74 waddstr (dialog, (char *)title);
75 waddch (dialog, ' ');
76 }
77
78 wattrset (dialog, dialog_attr);
79 print_autowrap (dialog, prompt, width - 2, 1, 3);
80
81
82 box_width = width - 6;
83 getyx (dialog, y, x);
84 box_y = y + 2;
85 box_x = (width - box_width) / 2;
86 draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
87 border_attr, dialog_attr);
88
89 print_buttons(dialog, height, width, TRUE, FALSE);
90
91
92 wmove (dialog, box_y, box_x);
93 wattrset (dialog, inputbox_attr);
94
95 if (!init)
96 instr[0] = '\0';
97 else
98 strcpy (instr, init);
99
100 input_x = strlen (instr);
101
102 if (input_x >= box_width) {
103 scroll = input_x - box_width + 1;
104 input_x = box_width - 1;
105 for (i = 0; i < box_width - 1; i++)
106 waddch (dialog, instr[scroll + i]);
107 } else
108 waddstr (dialog, instr);
109
110 wmove (dialog, box_y, box_x + input_x);
111
112 wrefresh (dialog);
113
114 while (key != ESC) {
115 key = wgetch (dialog);
116
117 if (button == -1) {
118 switch (key) {
119 case TAB:
120 case KEY_UP:
121 case KEY_DOWN:
122 break;
123 case KEY_LEFT:
124 continue;
125 case KEY_RIGHT:
126 continue;
127 case KEY_BACKSPACE:
128 case 127:
129 if (input_x || scroll) {
130 wattrset (dialog, inputbox_attr);
131 if (!input_x) {
132 scroll = scroll < box_width - 1 ?
133 0 : scroll - (box_width - 1);
134 wmove (dialog, box_y, box_x);
135 for (i = 0; i < box_width; i++)
136 waddch (dialog, instr[scroll + input_x + i] ?
137 instr[scroll + input_x + i] : ' ');
138 input_x = strlen (instr) - scroll;
139 } else
140 input_x--;
141 instr[scroll + input_x] = '\0';
142 mvwaddch (dialog, box_y, input_x + box_x, ' ');
143 wmove (dialog, box_y, input_x + box_x);
144 wrefresh (dialog);
145 }
146 continue;
147 default:
148 if (key < 0x100 && isprint (key)) {
149 if (scroll + input_x < MAX_LEN) {
150 wattrset (dialog, inputbox_attr);
151 instr[scroll + input_x] = key;
152 instr[scroll + input_x + 1] = '\0';
153 if (input_x == box_width - 1) {
154 scroll++;
155 wmove (dialog, box_y, box_x);
156 for (i = 0; i < box_width - 1; i++)
157 waddch (dialog, instr[scroll + i]);
158 } else {
159 wmove (dialog, box_y, input_x++ + box_x);
160 waddch (dialog, key);
161 }
162 wrefresh (dialog);
163 } else
164 flash ();
165 continue;
166 }
167 }
168 }
169 switch (key) {
170 case 'O':
171 case 'o':
172 delwin (dialog);
173 return 0;
174 case 'H':
175 case 'h':
176 delwin (dialog);
177 return 1;
178 case KEY_UP:
179 case KEY_LEFT:
180 switch (button) {
181 case -1:
182 button = 1;
183 print_buttons(dialog, height, width, FALSE, TRUE);
184 break;
185 case 0:
186 button = -1;
187 print_buttons(dialog, height, width, TRUE, FALSE);
188 wmove (dialog, box_y, box_x + input_x);
189 wrefresh (dialog);
190 break;
191 case 1:
192 button = 0;
193 print_buttons(dialog, height, width, TRUE, FALSE);
194 break;
195 }
196 break;
197 case TAB:
198 case KEY_DOWN:
199 case KEY_RIGHT:
200 switch (button) {
201 case -1:
202 button = 0;
203 print_buttons(dialog, height, width, TRUE, FALSE);
204 break;
205 case 0:
206 button = 1;
207 print_buttons(dialog, height, width, FALSE, TRUE);
208 break;
209 case 1:
210 button = -1;
211 print_buttons(dialog, height, width, TRUE, FALSE);
212 wmove (dialog, box_y, box_x + input_x);
213 wrefresh (dialog);
214 break;
215 }
216 break;
217 case ' ':
218 case '\n':
219 delwin (dialog);
220 return (button == -1 ? 0 : button);
221 case 'X':
222 case 'x':
223 key = ESC;
224 case ESC:
225 break;
226 }
227 }
228
229 delwin (dialog);
230 return -1;
231 }