This source file includes following definitions.
- print_item
- print_arrows
- print_buttons
- dialog_checklist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include "dialog.h"
25
26 static int list_width, check_x, item_x, checkflag;
27
28
29
30
31 static void
32 print_item (WINDOW * win, const char *item, int status,
33 int choice, int selected)
34 {
35 int i;
36
37
38 wattrset (win, menubox_attr);
39 wmove (win, choice, 0);
40 for (i = 0; i < list_width; i++)
41 waddch (win, ' ');
42
43 wmove (win, choice, check_x);
44 wattrset (win, selected ? check_selected_attr : check_attr);
45 if (checkflag == FLAG_CHECK)
46 wprintw (win, "[%c]", status ? 'X' : ' ');
47 else
48 wprintw (win, "(%c)", status ? 'X' : ' ');
49
50 wattrset (win, selected ? tag_selected_attr : tag_attr);
51 mvwaddch(win, choice, item_x, item[0]);
52 wattrset (win, selected ? item_selected_attr : item_attr);
53 waddstr (win, (char *)item+1);
54 }
55
56
57
58
59 static void
60 print_arrows (WINDOW * win, int choice, int item_no, int scroll,
61 int y, int x, int height)
62 {
63 wmove(win, y, x);
64
65 if (scroll > 0) {
66 wattrset (win, uarrow_attr);
67 waddch (win, ACS_UARROW);
68 waddstr (win, "(-)");
69 }
70 else {
71 wattrset (win, menubox_attr);
72 waddch (win, ACS_HLINE);
73 waddch (win, ACS_HLINE);
74 waddch (win, ACS_HLINE);
75 waddch (win, ACS_HLINE);
76 }
77
78 y = y + height + 1;
79 wmove(win, y, x);
80
81 if ((height < item_no) && (scroll + choice < item_no - 1)) {
82 wattrset (win, darrow_attr);
83 waddch (win, ACS_DARROW);
84 waddstr (win, "(+)");
85 }
86 else {
87 wattrset (win, menubox_border_attr);
88 waddch (win, ACS_HLINE);
89 waddch (win, ACS_HLINE);
90 waddch (win, ACS_HLINE);
91 waddch (win, ACS_HLINE);
92 }
93 }
94
95
96
97
98 static void
99 print_buttons( WINDOW *dialog, int height, int width, int okval, int cancelval)
100 {
101 int x = width / 2 - 11;
102 int y = height - 2;
103
104 print_button (dialog, "Select", y, x, okval);
105 print_button (dialog, " Help ", y, x + 14, cancelval);
106
107 wrefresh (dialog);
108 }
109
110
111
112
113
114 int
115 dialog_checklist (const char *title, const char *prompt, int height, int width,
116 int list_height, int item_no, const char * const * items, int flag,
117 int separate_output)
118 {
119 int i, x, y, cur_x, cur_y, box_x, box_y;
120 int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
121 WINDOW *dialog, *list;
122
123 checkflag = flag;
124
125
126 if ((status = malloc (sizeof (int) * item_no)) == NULL) {
127 endwin ();
128 fprintf (stderr,
129 "\nCan't allocate memory in dialog_checklist().\n");
130 exit (-1);
131 }
132
133
134 for (i = 0; i < item_no; i++) {
135 status[i] = !strcasecmp (items[i * 3 + 2], "on");
136 if (!choice && status[i])
137 choice = i;
138 }
139
140 max_choice = MIN (list_height, item_no);
141
142
143 x = (COLS - width) / 2;
144 y = (LINES - height) / 2;
145
146 draw_shadow (stdscr, y, x, height, width);
147
148 dialog = newwin (height, width, y, x);
149 keypad (dialog, TRUE);
150
151 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
152 wattrset (dialog, border_attr);
153 mvwaddch (dialog, height-3, 0, ACS_LTEE);
154 for (i = 0; i < width - 2; i++)
155 waddch (dialog, ACS_HLINE);
156 wattrset (dialog, dialog_attr);
157 waddch (dialog, ACS_RTEE);
158
159 if (title != NULL) {
160 wattrset (dialog, title_attr);
161 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
162 waddstr (dialog, (char *)title);
163 waddch (dialog, ' ');
164 }
165
166 wattrset (dialog, dialog_attr);
167 print_autowrap (dialog, prompt, width - 2, 1, 3);
168
169 list_width = width - 6;
170 getyx (dialog, cur_y, cur_x);
171 box_y = height - list_height - 5;
172 box_x = (width - list_width) / 2 - 1;
173
174
175 list = subwin (dialog, list_height, list_width, y+box_y+1, x+box_x+1);
176
177 keypad (list, TRUE);
178
179
180 draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
181 menubox_border_attr, menubox_attr);
182
183
184 check_x = 0;
185 for (i = 0; i < item_no; i++)
186 check_x = MAX (check_x, + strlen (items[i * 3 + 1]) + 4);
187
188 check_x = (list_width - check_x) / 2;
189 item_x = check_x + 4;
190
191
192 for (i = 0; i < max_choice; i++)
193 print_item (list, items[i * 3 + 1], status[i], i, i == choice);
194
195 wnoutrefresh (list);
196
197 print_arrows(dialog, choice, item_no, scroll,
198 box_y, box_x + check_x + 5, list_height);
199
200 print_buttons(dialog, height, width, TRUE, FALSE);
201
202 while (key != ESC) {
203 key = wgetch (dialog);
204
205 for (i = 0; i < max_choice; i++)
206 if (toupper(key) == toupper(items[(scroll+i)*3+1][0]))
207 break;
208
209
210 if ( i < max_choice || key == KEY_UP || key == KEY_DOWN ||
211 key == '+' || key == '-' ) {
212 if (key == KEY_UP || key == '-') {
213 if (!choice) {
214 if (!scroll)
215 continue;
216
217 getyx (dialog, cur_y, cur_x);
218 if (list_height > 1) {
219
220 print_item (list, items[scroll * 3 + 1],
221 status[scroll], 0, FALSE);
222 scrollok (list, TRUE);
223 wscrl (list, -1);
224 scrollok (list, FALSE);
225 }
226 scroll--;
227 print_item (list, items[scroll * 3 + 1],
228 status[scroll], 0, TRUE);
229 wnoutrefresh (list);
230
231 print_arrows(dialog, choice, item_no, scroll,
232 box_y, box_x + check_x + 5, list_height);
233
234 wmove (dialog, cur_y, cur_x);
235 wrefresh (dialog);
236
237 continue;
238 } else
239 i = choice - 1;
240 } else if (key == KEY_DOWN || key == '+') {
241 if (choice == max_choice - 1) {
242 if (scroll + choice >= item_no - 1)
243 continue;
244
245 getyx (dialog, cur_y, cur_x);
246 if (list_height > 1) {
247
248 print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
249 status[scroll + max_choice - 1],
250 max_choice - 1, FALSE);
251 scrollok (list, TRUE);
252 scroll (list);
253 scrollok (list, FALSE);
254 }
255 scroll++;
256 print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
257 status[scroll + max_choice - 1],
258 max_choice - 1, TRUE);
259 wnoutrefresh (list);
260
261 print_arrows(dialog, choice, item_no, scroll,
262 box_y, box_x + check_x + 5, list_height);
263
264 wmove (dialog, cur_y, cur_x);
265 wrefresh (dialog);
266
267 continue;
268 } else
269 i = choice + 1;
270 }
271 if (i != choice) {
272
273 getyx (dialog, cur_y, cur_x);
274 print_item (list, items[(scroll + choice) * 3 + 1],
275 status[scroll + choice], choice, FALSE);
276
277 choice = i;
278 print_item (list, items[(scroll + choice) * 3 + 1],
279 status[scroll + choice], choice, TRUE);
280 wnoutrefresh (list);
281 wmove (dialog, cur_y, cur_x);
282 wrefresh (dialog);
283 }
284 continue;
285 }
286 switch (key) {
287 case 'H':
288 case 'h':
289 case '?':
290 delwin (dialog);
291 free (status);
292 return 1;
293 return 1;
294 case TAB:
295 case KEY_LEFT:
296 case KEY_RIGHT:
297 if (!button) {
298 button = 1;
299 print_buttons(dialog, height, width, FALSE, TRUE);
300 } else {
301 button = 0;
302 print_buttons(dialog, height, width, TRUE, FALSE);
303 }
304 wrefresh (dialog);
305 break;
306 case 'S':
307 case 's':
308 case ' ':
309 case '\n':
310 if (!button) {
311 if (flag == FLAG_CHECK) {
312 status[scroll + choice] = !status[scroll + choice];
313 getyx (dialog, cur_y, cur_x);
314 wmove (list, choice, check_x);
315 wattrset (list, check_selected_attr);
316 wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
317 } else {
318 if (!status[scroll + choice]) {
319 for (i = 0; i < item_no; i++)
320 status[i] = 0;
321 status[scroll + choice] = 1;
322 getyx (dialog, cur_y, cur_x);
323 for (i = 0; i < max_choice; i++)
324 print_item (list, items[(scroll + i) * 3 + 1],
325 status[scroll + i], i, i == choice);
326 }
327 }
328 wnoutrefresh (list);
329 wmove (dialog, cur_y, cur_x);
330 wrefresh (dialog);
331
332 for (i = 0; i < item_no; i++) {
333 if (status[i]) {
334 if (flag == FLAG_CHECK) {
335 fprintf (stderr, "\"%s\" ", items[i * 3]);
336 } else {
337 fprintf (stderr, "%s", items[i * 3]);
338 }
339
340 }
341 }
342 }
343 delwin (dialog);
344 free (status);
345 return button;
346 case 'X':
347 case 'x':
348 key = ESC;
349 case ESC:
350 break;
351 }
352 }
353
354 delwin (dialog);
355 free (status);
356 return -1;
357 }