source: menu/lxdialog/util.c@ 14a3e90

ablfs
Last change on this file since 14a3e90 was 6e8fc82, checked in by Manuel Canales Esparcia <manuel@…>, 18 years ago

Fixed menuconfig look on UTF-8 based systems.
Thanks to Alexander E. Patrakov for the patch.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
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
24
25/* use colors by default? */
26bool use_colors = 1;
27
28const char *backtitle = NULL;
29
30const char *dialog_result;
31
32/*
33 * Attribute values, default is for mono display
34 */
35chtype attributes[] =
36{
37 A_NORMAL, /* screen_attr */
38 A_NORMAL, /* shadow_attr */
39 A_NORMAL, /* dialog_attr */
40 A_BOLD, /* title_attr */
41 A_NORMAL, /* border_attr */
42 A_REVERSE, /* button_active_attr */
43 A_DIM, /* button_inactive_attr */
44 A_REVERSE, /* button_key_active_attr */
45 A_BOLD, /* button_key_inactive_attr */
46 A_REVERSE, /* button_label_active_attr */
47 A_NORMAL, /* button_label_inactive_attr */
48 A_NORMAL, /* inputbox_attr */
49 A_NORMAL, /* inputbox_border_attr */
50 A_NORMAL, /* searchbox_attr */
51 A_BOLD, /* searchbox_title_attr */
52 A_NORMAL, /* searchbox_border_attr */
53 A_BOLD, /* position_indicator_attr */
54 A_NORMAL, /* menubox_attr */
55 A_NORMAL, /* menubox_border_attr */
56 A_NORMAL, /* item_attr */
57 A_REVERSE, /* item_selected_attr */
58 A_BOLD, /* tag_attr */
59 A_REVERSE, /* tag_selected_attr */
60 A_BOLD, /* tag_key_attr */
61 A_REVERSE, /* tag_key_selected_attr */
62 A_BOLD, /* check_attr */
63 A_REVERSE, /* check_selected_attr */
64 A_BOLD, /* uarrow_attr */
65 A_BOLD /* darrow_attr */
66};
67
68
69#include "colors.h"
70
71/*
72 * Table of color values
73 */
74int color_table[][3] =
75{
76 {SCREEN_FG, SCREEN_BG, SCREEN_HL},
77 {SHADOW_FG, SHADOW_BG, SHADOW_HL},
78 {DIALOG_FG, DIALOG_BG, DIALOG_HL},
79 {TITLE_FG, TITLE_BG, TITLE_HL},
80 {BORDER_FG, BORDER_BG, BORDER_HL},
81 {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
82 {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
83 {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
84 {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG, BUTTON_KEY_INACTIVE_HL},
85 {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG, BUTTON_LABEL_ACTIVE_HL},
86 {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
87 BUTTON_LABEL_INACTIVE_HL},
88 {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
89 {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
90 {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
91 {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
92 {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
93 {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
94 {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
95 {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
96 {ITEM_FG, ITEM_BG, ITEM_HL},
97 {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
98 {TAG_FG, TAG_BG, TAG_HL},
99 {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
100 {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
101 {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
102 {CHECK_FG, CHECK_BG, CHECK_HL},
103 {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
104 {UARROW_FG, UARROW_BG, UARROW_HL},
105 {DARROW_FG, DARROW_BG, DARROW_HL},
106}; /* color_table */
107
108/*
109 * Set window to attribute 'attr'
110 */
111void
112attr_clear (WINDOW * win, int height, int width, chtype attr)
113{
114 int i, j;
115
116 wattrset (win, attr);
117 for (i = 0; i < height; i++) {
118 wmove (win, i, 0);
119 for (j = 0; j < width; j++)
120 waddch (win, ' ');
121 }
122 touchwin (win);
123}
124
125void dialog_clear (void)
126{
127 attr_clear (stdscr, LINES, COLS, screen_attr);
128 /* Display background title if it exists ... - SLH */
129 if (backtitle != NULL) {
130 int i;
131
132 wattrset (stdscr, screen_attr);
133 mvwaddstr (stdscr, 0, 1, (char *)backtitle);
134 wmove (stdscr, 1, 1);
135 for (i = 1; i < COLS - 1; i++)
136 waddch (stdscr, ACS_HLINE);
137 }
138 wnoutrefresh (stdscr);
139}
140
141/*
142 * Do some initialization for dialog
143 */
144void
145init_dialog (void)
146{
147#ifdef LOCALE
148 setlocale(LC_CTYPE, ""); /* required by ncurses on linux UTF-8 console */
149#endif
150 initscr (); /* Init curses */
151 keypad (stdscr, TRUE);
152 cbreak ();
153 noecho ();
154
155
156 if (use_colors) /* Set up colors */
157 color_setup ();
158
159
160 dialog_clear ();
161}
162
163/*
164 * Setup for color display
165 */
166void
167color_setup (void)
168{
169 int i;
170
171 if (has_colors ()) { /* Terminal supports color? */
172 start_color ();
173
174 /* Initialize color pairs */
175 for (i = 0; i < ATTRIBUTE_COUNT; i++)
176 init_pair (i + 1, color_table[i][0], color_table[i][1]);
177
178 /* Setup color attributes */
179 for (i = 0; i < ATTRIBUTE_COUNT; i++)
180 attributes[i] = C_ATTR (color_table[i][2], i + 1);
181 }
182}
183
184/*
185 * End using dialog functions.
186 */
187void
188end_dialog (void)
189{
190 endwin ();
191}
192
193
194/*
195 * Print a string of text in a window, automatically wrap around to the
196 * next line if the string is too long to fit on one line. Newline
197 * characters '\n' are replaced by spaces. We start on a new line
198 * if there is no room for at least 4 nonblanks following a double-space.
199 */
200void
201print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
202{
203 int newl, cur_x, cur_y;
204 int i, prompt_len, room, wlen;
205 char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
206
207 strcpy (tempstr, prompt);
208
209 prompt_len = strlen(tempstr);
210
211 /*
212 * Remove newlines
213 */
214 for(i=0; i<prompt_len; i++) {
215 if(tempstr[i] == '\n') tempstr[i] = ' ';
216 }
217
218 if (prompt_len <= width - x * 2) { /* If prompt is short */
219 wmove (win, y, (width - prompt_len) / 2);
220 waddstr (win, tempstr);
221 } else {
222 cur_x = x;
223 cur_y = y;
224 newl = 1;
225 word = tempstr;
226 while (word && *word) {
227 sp = index(word, ' ');
228 if (sp)
229 *sp++ = 0;
230
231 /* Wrap to next line if either the word does not fit,
232 or it is the first word of a new sentence, and it is
233 short, and the next word does not fit. */
234 room = width - cur_x;
235 wlen = strlen(word);
236 if (wlen > room ||
237 (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
238 && (!(sp2 = index(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
239 cur_y++;
240 cur_x = x;
241 }
242 wmove (win, cur_y, cur_x);
243 waddstr (win, word);
244 getyx (win, cur_y, cur_x);
245 cur_x++;
246 if (sp && *sp == ' ') {
247 cur_x++; /* double space */
248 while (*++sp == ' ');
249 newl = 1;
250 } else
251 newl = 0;
252 word = sp;
253 }
254 }
255}
256
257/*
258 * Print a button
259 */
260void
261print_button (WINDOW * win, const char *label, int y, int x, int selected)
262{
263 int i, temp;
264
265 wmove (win, y, x);
266 wattrset (win, selected ? button_active_attr : button_inactive_attr);
267 waddstr (win, "<");
268 temp = strspn (label, " ");
269 label += temp;
270 wattrset (win, selected ? button_label_active_attr
271 : button_label_inactive_attr);
272 for (i = 0; i < temp; i++)
273 waddch (win, ' ');
274 wattrset (win, selected ? button_key_active_attr
275 : button_key_inactive_attr);
276 waddch (win, label[0]);
277 wattrset (win, selected ? button_label_active_attr
278 : button_label_inactive_attr);
279 waddstr (win, (char *)label + 1);
280 wattrset (win, selected ? button_active_attr : button_inactive_attr);
281 waddstr (win, ">");
282 wmove (win, y, x + temp + 1);
283}
284
285/*
286 * Draw a rectangular box with line drawing characters
287 */
288void
289draw_box (WINDOW * win, int y, int x, int height, int width,
290 chtype box, chtype border)
291{
292 int i, j;
293
294 wattrset (win, 0);
295 for (i = 0; i < height; i++) {
296 wmove (win, y + i, x);
297 for (j = 0; j < width; j++)
298 if (!i && !j)
299 waddch (win, border | ACS_ULCORNER);
300 else if (i == height - 1 && !j)
301 waddch (win, border | ACS_LLCORNER);
302 else if (!i && j == width - 1)
303 waddch (win, box | ACS_URCORNER);
304 else if (i == height - 1 && j == width - 1)
305 waddch (win, box | ACS_LRCORNER);
306 else if (!i)
307 waddch (win, border | ACS_HLINE);
308 else if (i == height - 1)
309 waddch (win, box | ACS_HLINE);
310 else if (!j)
311 waddch (win, border | ACS_VLINE);
312 else if (j == width - 1)
313 waddch (win, box | ACS_VLINE);
314 else
315 waddch (win, box | ' ');
316 }
317}
318
319/*
320 * Draw shadows along the right and bottom edge to give a more 3D look
321 * to the boxes
322 */
323void
324draw_shadow (WINDOW * win, int y, int x, int height, int width)
325{
326 int i;
327
328 if (has_colors ()) { /* Whether terminal supports color? */
329 wattrset (win, shadow_attr);
330 wmove (win, y + height, x + 2);
331 for (i = 0; i < width; i++)
332 waddch (win, winch (win) & A_CHARTEXT);
333 for (i = y + 1; i < y + height + 1; i++) {
334 wmove (win, i, x + width);
335 waddch (win, winch (win) & A_CHARTEXT);
336 waddch (win, winch (win) & A_CHARTEXT);
337 }
338 wnoutrefresh (win);
339 }
340}
341
342/*
343 * Return the position of the first alphabetic character in a string.
344 */
345int
346first_alpha(const char *string, const char *exempt)
347{
348 int i, in_paren=0, c;
349
350 for (i = 0; i < strlen(string); i++) {
351 c = tolower(string[i]);
352
353 if (strchr("<[(", c)) ++in_paren;
354 if (strchr(">])", c) && in_paren > 0) --in_paren;
355
356 if ((! in_paren) && isalpha(c) &&
357 strchr(exempt, c) == 0)
358 return i;
359 }
360
361 return 0;
362}
363
364/*
365 * Get the first selected item in the dialog_list_item list.
366 */
367struct dialog_list_item *
368first_sel_item(int item_no, struct dialog_list_item ** items)
369{
370 int i;
371
372 for (i = 0; i < item_no; i++) {
373 if (items[i]->selected)
374 return items[i];
375 }
376
377 return NULL;
378}
Note: See TracBrowser for help on using the repository browser.