1 /* vi: set sw=4 ts=4: */
5 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* use colors by default? */
29 const char *backtitle = NULL;
31 const char *dialog_result;
34 * Attribute values, default is for mono display
38 A_NORMAL, /* screen_attr */
39 A_NORMAL, /* shadow_attr */
40 A_NORMAL, /* dialog_attr */
41 A_BOLD, /* title_attr */
42 A_NORMAL, /* border_attr */
43 A_REVERSE, /* button_active_attr */
44 A_DIM, /* button_inactive_attr */
45 A_REVERSE, /* button_key_active_attr */
46 A_BOLD, /* button_key_inactive_attr */
47 A_REVERSE, /* button_label_active_attr */
48 A_NORMAL, /* button_label_inactive_attr */
49 A_NORMAL, /* inputbox_attr */
50 A_NORMAL, /* inputbox_border_attr */
51 A_NORMAL, /* searchbox_attr */
52 A_BOLD, /* searchbox_title_attr */
53 A_NORMAL, /* searchbox_border_attr */
54 A_BOLD, /* position_indicator_attr */
55 A_NORMAL, /* menubox_attr */
56 A_NORMAL, /* menubox_border_attr */
57 A_NORMAL, /* item_attr */
58 A_REVERSE, /* item_selected_attr */
59 A_BOLD, /* tag_attr */
60 A_REVERSE, /* tag_selected_attr */
61 A_BOLD, /* tag_key_attr */
62 A_REVERSE, /* tag_key_selected_attr */
63 A_BOLD, /* check_attr */
64 A_REVERSE, /* check_selected_attr */
65 A_BOLD, /* uarrow_attr */
66 A_BOLD /* darrow_attr */
73 * Table of color values
75 int color_table[][3] =
77 {SCREEN_FG, SCREEN_BG, SCREEN_HL},
78 {SHADOW_FG, SHADOW_BG, SHADOW_HL},
79 {DIALOG_FG, DIALOG_BG, DIALOG_HL},
80 {TITLE_FG, TITLE_BG, TITLE_HL},
81 {BORDER_FG, BORDER_BG, BORDER_HL},
82 {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
83 {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
84 {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
85 {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG, BUTTON_KEY_INACTIVE_HL},
86 {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG, BUTTON_LABEL_ACTIVE_HL},
87 {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
88 BUTTON_LABEL_INACTIVE_HL},
89 {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
90 {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
91 {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
92 {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
93 {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
94 {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
95 {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
96 {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
97 {ITEM_FG, ITEM_BG, ITEM_HL},
98 {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
99 {TAG_FG, TAG_BG, TAG_HL},
100 {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
101 {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
102 {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
103 {CHECK_FG, CHECK_BG, CHECK_HL},
104 {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
105 {UARROW_FG, UARROW_BG, UARROW_HL},
106 {DARROW_FG, DARROW_BG, DARROW_HL},
110 * Set window to attribute 'attr'
113 attr_clear (WINDOW * win, int height, int width, chtype attr)
117 wattrset (win, attr);
118 for (i = 0; i < height; i++) {
120 for (j = 0; j < width; j++)
126 void dialog_clear (void)
128 attr_clear (stdscr, LINES, COLS, screen_attr);
129 /* Display background title if it exists ... - SLH */
130 if (backtitle != NULL) {
133 wattrset (stdscr, screen_attr);
134 mvwaddstr (stdscr, 0, 1, (char *)backtitle);
135 wmove (stdscr, 1, 1);
136 for (i = 1; i < COLS - 1; i++)
137 waddch (stdscr, ACS_HLINE);
139 wnoutrefresh (stdscr);
143 * Do some initialization for dialog
148 initscr (); /* Init curses */
149 keypad (stdscr, TRUE);
154 if (use_colors) /* Set up colors */
162 * Setup for color display
169 if (has_colors ()) { /* Terminal supports color? */
172 /* Initialize color pairs */
173 for (i = 0; i < ATTRIBUTE_COUNT; i++)
174 init_pair (i + 1, color_table[i][0], color_table[i][1]);
176 /* Setup color attributes */
177 for (i = 0; i < ATTRIBUTE_COUNT; i++)
178 attributes[i] = C_ATTR (color_table[i][2], i + 1);
183 * End using dialog functions.
193 * Print a string of text in a window, automatically wrap around to the
194 * next line if the string is too long to fit on one line. Newline
195 * characters '\n' are replaced by spaces. We start on a new line
196 * if there is no room for at least 4 nonblanks following a double-space.
199 print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
201 int newl, cur_x, cur_y;
202 int i, prompt_len, room, wlen;
203 char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
205 strcpy (tempstr, prompt);
207 prompt_len = strlen(tempstr);
212 for(i=0; i<prompt_len; i++) {
213 if(tempstr[i] == '\n') tempstr[i] = ' ';
216 if (prompt_len <= width - x * 2) { /* If prompt is short */
217 wmove (win, y, (width - prompt_len) / 2);
218 waddstr (win, tempstr);
224 while (word && *word) {
225 sp = strchr(word, ' ');
229 /* Wrap to next line if either the word does not fit,
230 or it is the first word of a new sentence, and it is
231 short, and the next word does not fit. */
232 room = width - cur_x;
235 (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
236 && (!(sp2 = strchr(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
240 wmove (win, cur_y, cur_x);
242 getyx (win, cur_y, cur_x);
244 if (sp && *sp == ' ') {
245 cur_x++; /* double space */
246 while (*++sp == ' ');
259 print_button (WINDOW * win, const char *label, int y, int x, int selected)
264 wattrset (win, selected ? button_active_attr : button_inactive_attr);
266 temp = strspn (label, " ");
268 wattrset (win, selected ? button_label_active_attr
269 : button_label_inactive_attr);
270 for (i = 0; i < temp; i++)
272 wattrset (win, selected ? button_key_active_attr
273 : button_key_inactive_attr);
274 waddch (win, label[0]);
275 wattrset (win, selected ? button_label_active_attr
276 : button_label_inactive_attr);
277 waddstr (win, (char *)label + 1);
278 wattrset (win, selected ? button_active_attr : button_inactive_attr);
280 wmove (win, y, x + temp + 1);
284 * Draw a rectangular box with line drawing characters
287 draw_box (WINDOW * win, int y, int x, int height, int width,
288 chtype box, chtype border)
293 for (i = 0; i < height; i++) {
294 wmove (win, y + i, x);
295 for (j = 0; j < width; j++)
297 waddch (win, border | ACS_ULCORNER);
298 else if (i == height - 1 && !j)
299 waddch (win, border | ACS_LLCORNER);
300 else if (!i && j == width - 1)
301 waddch (win, box | ACS_URCORNER);
302 else if (i == height - 1 && j == width - 1)
303 waddch (win, box | ACS_LRCORNER);
305 waddch (win, border | ACS_HLINE);
306 else if (i == height - 1)
307 waddch (win, box | ACS_HLINE);
309 waddch (win, border | ACS_VLINE);
310 else if (j == width - 1)
311 waddch (win, box | ACS_VLINE);
313 waddch (win, box | ' ');
318 * Draw shadows along the right and bottom edge to give a more 3D look
322 draw_shadow (WINDOW * win, int y, int x, int height, int width)
326 if (has_colors ()) { /* Whether terminal supports color? */
327 wattrset (win, shadow_attr);
328 wmove (win, y + height, x + 2);
329 for (i = 0; i < width; i++)
330 waddch (win, winch (win) & A_CHARTEXT);
331 for (i = y + 1; i < y + height + 1; i++) {
332 wmove (win, i, x + width);
333 waddch (win, winch (win) & A_CHARTEXT);
334 waddch (win, winch (win) & A_CHARTEXT);
341 * Return the position of the first alphabetic character in a string.
344 first_alpha(const char *string, const char *exempt)
346 int i, in_paren=0, c;
348 for (i = 0; i < strlen(string); i++) {
349 c = tolower(string[i]);
351 if (strchr("<[(", c)) ++in_paren;
352 if (strchr(">])", c) && in_paren > 0) --in_paren;
354 if ((! in_paren) && isalpha(c) &&
355 strchr(exempt, c) == 0)
363 * Get the first selected item in the dialog_list_item list.
365 struct dialog_list_item *
366 first_sel_item(int item_no, struct dialog_list_item ** items)
370 for (i = 0; i < item_no; i++) {
371 if (items[i]->selected)