blob: ce411474d72979bb66beb6097f48025e66af711c [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
Sam Ravnborgdec69da2005-11-19 21:56:20 +010029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Attribute values, default is for mono display
31 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010032chtype attributes[] = {
33 A_NORMAL, /* screen_attr */
34 A_NORMAL, /* shadow_attr */
35 A_NORMAL, /* dialog_attr */
36 A_BOLD, /* title_attr */
37 A_NORMAL, /* border_attr */
38 A_REVERSE, /* button_active_attr */
39 A_DIM, /* button_inactive_attr */
40 A_REVERSE, /* button_key_active_attr */
41 A_BOLD, /* button_key_inactive_attr */
42 A_REVERSE, /* button_label_active_attr */
43 A_NORMAL, /* button_label_inactive_attr */
44 A_NORMAL, /* inputbox_attr */
45 A_NORMAL, /* inputbox_border_attr */
46 A_NORMAL, /* searchbox_attr */
47 A_BOLD, /* searchbox_title_attr */
48 A_NORMAL, /* searchbox_border_attr */
49 A_BOLD, /* position_indicator_attr */
50 A_NORMAL, /* menubox_attr */
51 A_NORMAL, /* menubox_border_attr */
52 A_NORMAL, /* item_attr */
53 A_REVERSE, /* item_selected_attr */
54 A_BOLD, /* tag_attr */
55 A_REVERSE, /* tag_selected_attr */
56 A_BOLD, /* tag_key_attr */
57 A_REVERSE, /* tag_key_selected_attr */
58 A_BOLD, /* check_attr */
59 A_REVERSE, /* check_selected_attr */
60 A_BOLD, /* uarrow_attr */
61 A_BOLD /* darrow_attr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "colors.h"
65
66/*
67 * Table of color values
68 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010069int color_table[][3] = {
70 {SCREEN_FG, SCREEN_BG, SCREEN_HL},
71 {SHADOW_FG, SHADOW_BG, SHADOW_HL},
72 {DIALOG_FG, DIALOG_BG, DIALOG_HL},
73 {TITLE_FG, TITLE_BG, TITLE_HL},
74 {BORDER_FG, BORDER_BG, BORDER_HL},
75 {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
76 {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
77 {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
78 {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG,
79 BUTTON_KEY_INACTIVE_HL},
80 {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG,
81 BUTTON_LABEL_ACTIVE_HL},
82 {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
83 BUTTON_LABEL_INACTIVE_HL},
84 {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
85 {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
86 {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
87 {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
88 {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
89 {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
90 {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
91 {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
92 {ITEM_FG, ITEM_BG, ITEM_HL},
93 {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
94 {TAG_FG, TAG_BG, TAG_HL},
95 {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
96 {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
97 {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
98 {CHECK_FG, CHECK_BG, CHECK_HL},
99 {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
100 {UARROW_FG, UARROW_BG, UARROW_HL},
101 {DARROW_FG, DARROW_BG, DARROW_HL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}; /* color_table */
103
104/*
105 * Set window to attribute 'attr'
106 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100107void attr_clear(WINDOW * win, int height, int width, chtype attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100109 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100111 wattrset(win, attr);
112 for (i = 0; i < height; i++) {
113 wmove(win, i, 0);
114 for (j = 0; j < width; j++)
115 waddch(win, ' ');
116 }
117 touchwin(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100120void dialog_clear(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100122 attr_clear(stdscr, LINES, COLS, screen_attr);
123 /* Display background title if it exists ... - SLH */
124 if (backtitle != NULL) {
125 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100127 wattrset(stdscr, screen_attr);
128 mvwaddstr(stdscr, 0, 1, (char *)backtitle);
129 wmove(stdscr, 1, 1);
130 for (i = 1; i < COLS - 1; i++)
131 waddch(stdscr, ACS_HLINE);
132 }
133 wnoutrefresh(stdscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136/*
137 * Do some initialization for dialog
138 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100139void init_dialog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100141 initscr(); /* Init curses */
142 keypad(stdscr, TRUE);
143 cbreak();
144 noecho();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100146 if (use_colors) /* Set up colors */
147 color_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100149 dialog_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/*
153 * Setup for color display
154 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100155void color_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100157 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100159 if (has_colors()) { /* Terminal supports color? */
160 start_color();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100162 /* Initialize color pairs */
163 for (i = 0; i < ATTRIBUTE_COUNT; i++)
164 init_pair(i + 1, color_table[i][0], color_table[i][1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100166 /* Setup color attributes */
167 for (i = 0; i < ATTRIBUTE_COUNT; i++)
168 attributes[i] = C_ATTR(color_table[i][2], i + 1);
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * End using dialog functions.
174 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100175void end_dialog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100177 endwin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * Print a string of text in a window, automatically wrap around to the
182 * next line if the string is too long to fit on one line. Newline
183 * characters '\n' are replaced by spaces. We start on a new line
184 * if there is no room for at least 4 nonblanks following a double-space.
185 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100186void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100188 int newl, cur_x, cur_y;
189 int i, prompt_len, room, wlen;
190 char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100192 strcpy(tempstr, prompt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100194 prompt_len = strlen(tempstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100196 /*
197 * Remove newlines
198 */
199 for (i = 0; i < prompt_len; i++) {
200 if (tempstr[i] == '\n')
201 tempstr[i] = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100203
204 if (prompt_len <= width - x * 2) { /* If prompt is short */
205 wmove(win, y, (width - prompt_len) / 2);
206 waddstr(win, tempstr);
207 } else {
208 cur_x = x;
209 cur_y = y;
210 newl = 1;
211 word = tempstr;
212 while (word && *word) {
213 sp = index(word, ' ');
214 if (sp)
215 *sp++ = 0;
216
217 /* Wrap to next line if either the word does not fit,
218 or it is the first word of a new sentence, and it is
219 short, and the next word does not fit. */
220 room = width - cur_x;
221 wlen = strlen(word);
222 if (wlen > room ||
223 (newl && wlen < 4 && sp
224 && wlen + 1 + strlen(sp) > room
225 && (!(sp2 = index(sp, ' '))
226 || wlen + 1 + (sp2 - sp) > room))) {
227 cur_y++;
228 cur_x = x;
229 }
230 wmove(win, cur_y, cur_x);
231 waddstr(win, word);
232 getyx(win, cur_y, cur_x);
233 cur_x++;
234 if (sp && *sp == ' ') {
235 cur_x++; /* double space */
236 while (*++sp == ' ') ;
237 newl = 1;
238 } else
239 newl = 0;
240 word = sp;
241 }
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * Print a button
247 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100248void print_button(WINDOW * win, const char *label, int y, int x, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100250 int i, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100252 wmove(win, y, x);
253 wattrset(win, selected ? button_active_attr : button_inactive_attr);
254 waddstr(win, "<");
255 temp = strspn(label, " ");
256 label += temp;
257 wattrset(win, selected ? button_label_active_attr
258 : button_label_inactive_attr);
259 for (i = 0; i < temp; i++)
260 waddch(win, ' ');
261 wattrset(win, selected ? button_key_active_attr
262 : button_key_inactive_attr);
263 waddch(win, label[0]);
264 wattrset(win, selected ? button_label_active_attr
265 : button_label_inactive_attr);
266 waddstr(win, (char *)label + 1);
267 wattrset(win, selected ? button_active_attr : button_inactive_attr);
268 waddstr(win, ">");
269 wmove(win, y, x + temp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * Draw a rectangular box with line drawing characters
274 */
275void
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100276draw_box(WINDOW * win, int y, int x, int height, int width,
277 chtype box, chtype border)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100279 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100281 wattrset(win, 0);
282 for (i = 0; i < height; i++) {
283 wmove(win, y + i, x);
284 for (j = 0; j < width; j++)
285 if (!i && !j)
286 waddch(win, border | ACS_ULCORNER);
287 else if (i == height - 1 && !j)
288 waddch(win, border | ACS_LLCORNER);
289 else if (!i && j == width - 1)
290 waddch(win, box | ACS_URCORNER);
291 else if (i == height - 1 && j == width - 1)
292 waddch(win, box | ACS_LRCORNER);
293 else if (!i)
294 waddch(win, border | ACS_HLINE);
295 else if (i == height - 1)
296 waddch(win, box | ACS_HLINE);
297 else if (!j)
298 waddch(win, border | ACS_VLINE);
299 else if (j == width - 1)
300 waddch(win, box | ACS_VLINE);
301 else
302 waddch(win, box | ' ');
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/*
307 * Draw shadows along the right and bottom edge to give a more 3D look
308 * to the boxes
309 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100310void draw_shadow(WINDOW * win, int y, int x, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100314 if (has_colors()) { /* Whether terminal supports color? */
315 wattrset(win, shadow_attr);
316 wmove(win, y + height, x + 2);
317 for (i = 0; i < width; i++)
318 waddch(win, winch(win) & A_CHARTEXT);
319 for (i = y + 1; i < y + height + 1; i++) {
320 wmove(win, i, x + width);
321 waddch(win, winch(win) & A_CHARTEXT);
322 waddch(win, winch(win) & A_CHARTEXT);
323 }
324 wnoutrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
329 * Return the position of the first alphabetic character in a string.
330 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100331int first_alpha(const char *string, const char *exempt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100333 int i, in_paren = 0, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 for (i = 0; i < strlen(string); i++) {
336 c = tolower(string[i]);
337
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100338 if (strchr("<[(", c))
339 ++in_paren;
340 if (strchr(">])", c) && in_paren > 0)
341 --in_paren;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100343 if ((!in_paren) && isalpha(c) && strchr(exempt, c) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return i;
345 }
346
347 return 0;
348}