blob: 1f84809773f0b7561730e32dfca509b49a963a78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * util.c
3 *
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "dialog.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* use colors by default? */
25bool use_colors = 1;
26
27const char *backtitle = NULL;
28
29const char *dialog_result;
30
Sam Ravnborgdec69da2005-11-19 21:56:20 +010031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Attribute values, default is for mono display
33 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010034chtype attributes[] = {
35 A_NORMAL, /* screen_attr */
36 A_NORMAL, /* shadow_attr */
37 A_NORMAL, /* dialog_attr */
38 A_BOLD, /* title_attr */
39 A_NORMAL, /* border_attr */
40 A_REVERSE, /* button_active_attr */
41 A_DIM, /* button_inactive_attr */
42 A_REVERSE, /* button_key_active_attr */
43 A_BOLD, /* button_key_inactive_attr */
44 A_REVERSE, /* button_label_active_attr */
45 A_NORMAL, /* button_label_inactive_attr */
46 A_NORMAL, /* inputbox_attr */
47 A_NORMAL, /* inputbox_border_attr */
48 A_NORMAL, /* searchbox_attr */
49 A_BOLD, /* searchbox_title_attr */
50 A_NORMAL, /* searchbox_border_attr */
51 A_BOLD, /* position_indicator_attr */
52 A_NORMAL, /* menubox_attr */
53 A_NORMAL, /* menubox_border_attr */
54 A_NORMAL, /* item_attr */
55 A_REVERSE, /* item_selected_attr */
56 A_BOLD, /* tag_attr */
57 A_REVERSE, /* tag_selected_attr */
58 A_BOLD, /* tag_key_attr */
59 A_REVERSE, /* tag_key_selected_attr */
60 A_BOLD, /* check_attr */
61 A_REVERSE, /* check_selected_attr */
62 A_BOLD, /* uarrow_attr */
63 A_BOLD /* darrow_attr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include "colors.h"
67
68/*
69 * Table of color values
70 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010071int color_table[][3] = {
72 {SCREEN_FG, SCREEN_BG, SCREEN_HL},
73 {SHADOW_FG, SHADOW_BG, SHADOW_HL},
74 {DIALOG_FG, DIALOG_BG, DIALOG_HL},
75 {TITLE_FG, TITLE_BG, TITLE_HL},
76 {BORDER_FG, BORDER_BG, BORDER_HL},
77 {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
78 {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
79 {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
80 {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG,
81 BUTTON_KEY_INACTIVE_HL},
82 {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG,
83 BUTTON_LABEL_ACTIVE_HL},
84 {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
85 BUTTON_LABEL_INACTIVE_HL},
86 {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
87 {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
88 {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
89 {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
90 {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
91 {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
92 {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
93 {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
94 {ITEM_FG, ITEM_BG, ITEM_HL},
95 {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
96 {TAG_FG, TAG_BG, TAG_HL},
97 {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
98 {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
99 {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
100 {CHECK_FG, CHECK_BG, CHECK_HL},
101 {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
102 {UARROW_FG, UARROW_BG, UARROW_HL},
103 {DARROW_FG, DARROW_BG, DARROW_HL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}; /* color_table */
105
106/*
107 * Set window to attribute 'attr'
108 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100109void attr_clear(WINDOW * win, int height, int width, chtype attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100111 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100113 wattrset(win, attr);
114 for (i = 0; i < height; i++) {
115 wmove(win, i, 0);
116 for (j = 0; j < width; j++)
117 waddch(win, ' ');
118 }
119 touchwin(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100122void dialog_clear(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100124 attr_clear(stdscr, LINES, COLS, screen_attr);
125 /* Display background title if it exists ... - SLH */
126 if (backtitle != NULL) {
127 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100129 wattrset(stdscr, screen_attr);
130 mvwaddstr(stdscr, 0, 1, (char *)backtitle);
131 wmove(stdscr, 1, 1);
132 for (i = 1; i < COLS - 1; i++)
133 waddch(stdscr, ACS_HLINE);
134 }
135 wnoutrefresh(stdscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138/*
139 * Do some initialization for dialog
140 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100141void init_dialog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100143 initscr(); /* Init curses */
144 keypad(stdscr, TRUE);
145 cbreak();
146 noecho();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100148 if (use_colors) /* Set up colors */
149 color_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100151 dialog_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/*
155 * Setup for color display
156 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100157void color_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100159 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100161 if (has_colors()) { /* Terminal supports color? */
162 start_color();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100164 /* Initialize color pairs */
165 for (i = 0; i < ATTRIBUTE_COUNT; i++)
166 init_pair(i + 1, color_table[i][0], color_table[i][1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100168 /* Setup color attributes */
169 for (i = 0; i < ATTRIBUTE_COUNT; i++)
170 attributes[i] = C_ATTR(color_table[i][2], i + 1);
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/*
175 * End using dialog functions.
176 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100177void end_dialog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100179 endwin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * Print a string of text in a window, automatically wrap around to the
184 * next line if the string is too long to fit on one line. Newline
185 * characters '\n' are replaced by spaces. We start on a new line
186 * if there is no room for at least 4 nonblanks following a double-space.
187 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100188void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100190 int newl, cur_x, cur_y;
191 int i, prompt_len, room, wlen;
192 char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100194 strcpy(tempstr, prompt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100196 prompt_len = strlen(tempstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100198 /*
199 * Remove newlines
200 */
201 for (i = 0; i < prompt_len; i++) {
202 if (tempstr[i] == '\n')
203 tempstr[i] = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100205
206 if (prompt_len <= width - x * 2) { /* If prompt is short */
207 wmove(win, y, (width - prompt_len) / 2);
208 waddstr(win, tempstr);
209 } else {
210 cur_x = x;
211 cur_y = y;
212 newl = 1;
213 word = tempstr;
214 while (word && *word) {
215 sp = index(word, ' ');
216 if (sp)
217 *sp++ = 0;
218
219 /* Wrap to next line if either the word does not fit,
220 or it is the first word of a new sentence, and it is
221 short, and the next word does not fit. */
222 room = width - cur_x;
223 wlen = strlen(word);
224 if (wlen > room ||
225 (newl && wlen < 4 && sp
226 && wlen + 1 + strlen(sp) > room
227 && (!(sp2 = index(sp, ' '))
228 || wlen + 1 + (sp2 - sp) > room))) {
229 cur_y++;
230 cur_x = x;
231 }
232 wmove(win, cur_y, cur_x);
233 waddstr(win, word);
234 getyx(win, cur_y, cur_x);
235 cur_x++;
236 if (sp && *sp == ' ') {
237 cur_x++; /* double space */
238 while (*++sp == ' ') ;
239 newl = 1;
240 } else
241 newl = 0;
242 word = sp;
243 }
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247/*
248 * Print a button
249 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100250void print_button(WINDOW * win, const char *label, int y, int x, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100252 int i, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100254 wmove(win, y, x);
255 wattrset(win, selected ? button_active_attr : button_inactive_attr);
256 waddstr(win, "<");
257 temp = strspn(label, " ");
258 label += temp;
259 wattrset(win, selected ? button_label_active_attr
260 : button_label_inactive_attr);
261 for (i = 0; i < temp; i++)
262 waddch(win, ' ');
263 wattrset(win, selected ? button_key_active_attr
264 : button_key_inactive_attr);
265 waddch(win, label[0]);
266 wattrset(win, selected ? button_label_active_attr
267 : button_label_inactive_attr);
268 waddstr(win, (char *)label + 1);
269 wattrset(win, selected ? button_active_attr : button_inactive_attr);
270 waddstr(win, ">");
271 wmove(win, y, x + temp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/*
275 * Draw a rectangular box with line drawing characters
276 */
277void
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100278draw_box(WINDOW * win, int y, int x, int height, int width,
279 chtype box, chtype border)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100281 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100283 wattrset(win, 0);
284 for (i = 0; i < height; i++) {
285 wmove(win, y + i, x);
286 for (j = 0; j < width; j++)
287 if (!i && !j)
288 waddch(win, border | ACS_ULCORNER);
289 else if (i == height - 1 && !j)
290 waddch(win, border | ACS_LLCORNER);
291 else if (!i && j == width - 1)
292 waddch(win, box | ACS_URCORNER);
293 else if (i == height - 1 && j == width - 1)
294 waddch(win, box | ACS_LRCORNER);
295 else if (!i)
296 waddch(win, border | ACS_HLINE);
297 else if (i == height - 1)
298 waddch(win, box | ACS_HLINE);
299 else if (!j)
300 waddch(win, border | ACS_VLINE);
301 else if (j == width - 1)
302 waddch(win, box | ACS_VLINE);
303 else
304 waddch(win, box | ' ');
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
309 * Draw shadows along the right and bottom edge to give a more 3D look
310 * to the boxes
311 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100312void draw_shadow(WINDOW * win, int y, int x, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100314 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100316 if (has_colors()) { /* Whether terminal supports color? */
317 wattrset(win, shadow_attr);
318 wmove(win, y + height, x + 2);
319 for (i = 0; i < width; i++)
320 waddch(win, winch(win) & A_CHARTEXT);
321 for (i = y + 1; i < y + height + 1; i++) {
322 wmove(win, i, x + width);
323 waddch(win, winch(win) & A_CHARTEXT);
324 waddch(win, winch(win) & A_CHARTEXT);
325 }
326 wnoutrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/*
331 * Return the position of the first alphabetic character in a string.
332 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100333int first_alpha(const char *string, const char *exempt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100335 int i, in_paren = 0, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 for (i = 0; i < strlen(string); i++) {
338 c = tolower(string[i]);
339
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100340 if (strchr("<[(", c))
341 ++in_paren;
342 if (strchr(">])", c) && in_paren > 0)
343 --in_paren;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100345 if ((!in_paren) && isalpha(c) && strchr(exempt, c) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return i;
347 }
348
349 return 0;
350}