* updated libkcddb (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / misc / confdialog / util.c
blob440c35b04f4a499513d0780200a290aeee52b01d
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/confdialog/util.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 * util.c
20 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
21 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
22 * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include "dialog.h"
42 /* use colors by default? */
43 bool use_colors = 1;
45 const char *backtitle = NULL;
47 const char *dialog_result;
49 /*
50 * Attribute values, default is for mono display
52 chtype attributes[] =
54 A_NORMAL, /* screen_attr */
55 A_NORMAL, /* shadow_attr */
56 A_NORMAL, /* dialog_attr */
57 A_BOLD, /* title_attr */
58 A_NORMAL, /* border_attr */
59 A_REVERSE, /* button_active_attr */
60 A_DIM, /* button_inactive_attr */
61 A_REVERSE, /* button_key_active_attr */
62 A_BOLD, /* button_key_inactive_attr */
63 A_REVERSE, /* button_label_active_attr */
64 A_NORMAL, /* button_label_inactive_attr */
65 A_NORMAL, /* inputbox_attr */
66 A_NORMAL, /* inputbox_border_attr */
67 A_NORMAL, /* searchbox_attr */
68 A_BOLD, /* searchbox_title_attr */
69 A_NORMAL, /* searchbox_border_attr */
70 A_BOLD, /* position_indicator_attr */
71 A_NORMAL, /* menubox_attr */
72 A_NORMAL, /* menubox_border_attr */
73 A_NORMAL, /* item_attr */
74 A_REVERSE, /* item_selected_attr */
75 A_BOLD, /* tag_attr */
76 A_REVERSE, /* tag_selected_attr */
77 A_BOLD, /* tag_key_attr */
78 A_REVERSE, /* tag_key_selected_attr */
79 A_BOLD, /* check_attr */
80 A_REVERSE, /* check_selected_attr */
81 A_BOLD, /* uarrow_attr */
82 A_BOLD /* darrow_attr */
86 #include "colors.h"
89 * Table of color values
91 int color_table[][3] =
93 {SCREEN_FG, SCREEN_BG, SCREEN_HL},
94 {SHADOW_FG, SHADOW_BG, SHADOW_HL},
95 {DIALOG_FG, DIALOG_BG, DIALOG_HL},
96 {TITLE_FG, TITLE_BG, TITLE_HL},
97 {BORDER_FG, BORDER_BG, BORDER_HL},
98 {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
99 {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
100 {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
101 {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG, BUTTON_KEY_INACTIVE_HL},
102 {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG, BUTTON_LABEL_ACTIVE_HL},
103 {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
104 BUTTON_LABEL_INACTIVE_HL},
105 {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
106 {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
107 {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
108 {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
109 {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
110 {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
111 {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
112 {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
113 {ITEM_FG, ITEM_BG, ITEM_HL},
114 {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
115 {TAG_FG, TAG_BG, TAG_HL},
116 {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
117 {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
118 {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
119 {CHECK_FG, CHECK_BG, CHECK_HL},
120 {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
121 {UARROW_FG, UARROW_BG, UARROW_HL},
122 {DARROW_FG, DARROW_BG, DARROW_HL},
123 }; /* color_table */
126 * Set window to attribute 'attr'
128 void
129 attr_clear (WINDOW * win, int height, int width, chtype attr)
131 int i, j;
133 wattrset (win, attr);
134 for (i = 0; i < height; i++) {
135 wmove (win, i, 0);
136 for (j = 0; j < width; j++)
137 waddch (win, ' ');
139 touchwin (win);
142 void dialog_clear (void)
144 attr_clear (stdscr, LINES, COLS, screen_attr);
145 /* Display background title if it exists ... - SLH */
146 if (backtitle != NULL) {
147 int i;
149 wattrset (stdscr, screen_attr);
150 mvwaddstr (stdscr, 0, 1, (char *)backtitle);
151 wmove (stdscr, 1, 1);
152 for (i = 1; i < COLS - 1; i++)
153 waddch (stdscr, ACS_HLINE);
155 wnoutrefresh (stdscr);
159 * Do some initialization for dialog
161 void
162 init_dialog (void)
164 initscr (); /* Init curses */
165 keypad (stdscr, TRUE);
166 cbreak ();
167 noecho ();
170 if (use_colors) /* Set up colors */
171 color_setup ();
174 dialog_clear ();
178 * Setup for color display
180 void
181 color_setup (void)
183 int i;
185 if (has_colors ()) { /* Terminal supports color? */
186 start_color ();
188 /* Initialize color pairs */
189 for (i = 0; i < ATTRIBUTE_COUNT; i++)
190 init_pair (i + 1, color_table[i][0], color_table[i][1]);
192 /* Setup color attributes */
193 for (i = 0; i < ATTRIBUTE_COUNT; i++)
194 attributes[i] = C_ATTR (color_table[i][2], i + 1);
199 * End using dialog functions.
201 void
202 end_dialog (void)
204 endwin ();
209 * Print a string of text in a window, automatically wrap around to the
210 * next line if the string is too long to fit on one line. Newline
211 * characters '\n' are replaced by spaces. We start on a new line
212 * if there is no room for at least 4 nonblanks following a double-space.
214 void
215 print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
217 int newl, cur_x, cur_y;
218 int i, prompt_len, room, wlen;
219 char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
221 strcpy (tempstr, prompt);
223 prompt_len = strlen(tempstr);
226 * Remove newlines
228 for(i=0; i<prompt_len; i++) {
229 if(tempstr[i] == '\n') tempstr[i] = ' ';
232 if (prompt_len <= width - x * 2) { /* If prompt is short */
233 wmove (win, y, (width - prompt_len) / 2);
234 waddstr (win, tempstr);
235 } else {
236 cur_x = x;
237 cur_y = y;
238 newl = 1;
239 word = tempstr;
240 while (word && *word) {
241 sp = index(word, ' ');
242 if (sp)
243 *sp++ = 0;
245 /* Wrap to next line if either the word does not fit,
246 or it is the first word of a new sentence, and it is
247 short, and the next word does not fit. */
248 room = width - cur_x;
249 wlen = strlen(word);
250 if (wlen > room ||
251 (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
252 && (!(sp2 = index(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
253 cur_y++;
254 cur_x = x;
256 wmove (win, cur_y, cur_x);
257 waddstr (win, word);
258 getyx (win, cur_y, cur_x);
259 cur_x++;
260 if (sp && *sp == ' ') {
261 cur_x++; /* double space */
262 while (*++sp == ' ');
263 newl = 1;
264 } else
265 newl = 0;
266 word = sp;
272 * Print a button
274 void
275 print_button (WINDOW * win, const char *label, int y, int x, int selected)
277 int i, temp;
279 wmove (win, y, x);
280 wattrset (win, selected ? button_active_attr : button_inactive_attr);
281 waddstr (win, "<");
282 temp = strspn (label, " ");
283 label += temp;
284 wattrset (win, selected ? button_label_active_attr
285 : button_label_inactive_attr);
286 for (i = 0; i < temp; i++)
287 waddch (win, ' ');
288 wattrset (win, selected ? button_key_active_attr
289 : button_key_inactive_attr);
290 waddch (win, label[0]);
291 wattrset (win, selected ? button_label_active_attr
292 : button_label_inactive_attr);
293 waddstr (win, (char *)label + 1);
294 wattrset (win, selected ? button_active_attr : button_inactive_attr);
295 waddstr (win, ">");
296 wmove (win, y, x + temp + 1);
300 * Draw a rectangular box with line drawing characters
302 void
303 draw_box (WINDOW * win, int y, int x, int height, int width,
304 chtype box, chtype border)
306 int i, j;
308 wattrset (win, 0);
309 for (i = 0; i < height; i++) {
310 wmove (win, y + i, x);
311 for (j = 0; j < width; j++)
312 if (!i && !j)
313 waddch (win, border | ACS_ULCORNER);
314 else if (i == height - 1 && !j)
315 waddch (win, border | ACS_LLCORNER);
316 else if (!i && j == width - 1)
317 waddch (win, box | ACS_URCORNER);
318 else if (i == height - 1 && j == width - 1)
319 waddch (win, box | ACS_LRCORNER);
320 else if (!i)
321 waddch (win, border | ACS_HLINE);
322 else if (i == height - 1)
323 waddch (win, box | ACS_HLINE);
324 else if (!j)
325 waddch (win, border | ACS_VLINE);
326 else if (j == width - 1)
327 waddch (win, box | ACS_VLINE);
328 else
329 waddch (win, box | ' ');
334 * Draw shadows along the right and bottom edge to give a more 3D look
335 * to the boxes
337 void
338 draw_shadow (WINDOW * win, int y, int x, int height, int width)
340 int i;
342 if (has_colors ()) { /* Whether terminal supports color? */
343 wattrset (win, shadow_attr);
344 wmove (win, y + height, x + 2);
345 for (i = 0; i < width; i++)
346 waddch (win, winch (win) & A_CHARTEXT);
347 for (i = y + 1; i < y + height + 1; i++) {
348 wmove (win, i, x + width);
349 waddch (win, winch (win) & A_CHARTEXT);
350 waddch (win, winch (win) & A_CHARTEXT);
352 wnoutrefresh (win);
357 * Return the position of the first alphabetic character in a string.
360 first_alpha(const char *string, const char *exempt)
362 int i, in_paren=0, c;
364 for (i = 0; i < strlen(string); i++) {
365 c = tolower(string[i]);
367 if (strchr("<[(", c)) ++in_paren;
368 if (strchr(">])", c)) --in_paren;
370 if ((! in_paren) && isalpha(c) &&
371 strchr(exempt, c) == 0)
372 return i;
375 return 0;