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