Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / scripts / config / lxdialog / util.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  util.c
4  *
5  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "dialog.h"
24
25
26 /* use colors by default? */
27 bool use_colors = 1;
28
29 const char *backtitle = NULL;
30
31 const char *dialog_result;
32
33 /*
34  * Attribute values, default is for mono display
35  */
36 chtype attributes[] =
37 {
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 */
67 };
68
69
70 #include "colors.h"
71
72 /*
73  * Table of color values
74  */
75 int color_table[][3] =
76 {
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},
107 };                              /* color_table */
108
109 /*
110  * Set window to attribute 'attr'
111  */
112 void
113 attr_clear (WINDOW * win, int height, int width, chtype attr)
114 {
115     int i, j;
116
117     wattrset (win, attr);
118     for (i = 0; i < height; i++) {
119         wmove (win, i, 0);
120         for (j = 0; j < width; j++)
121             waddch (win, ' ');
122     }
123     touchwin (win);
124 }
125
126 void dialog_clear (void)
127 {
128     attr_clear (stdscr, LINES, COLS, screen_attr);
129     /* Display background title if it exists ... - SLH */
130     if (backtitle != NULL) {
131         int i;
132
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);
138     }
139     wnoutrefresh (stdscr);
140 }
141
142 /*
143  * Do some initialization for dialog
144  */
145 void
146 init_dialog (void)
147 {
148     initscr ();                 /* Init curses */
149     keypad (stdscr, TRUE);
150     cbreak ();
151     noecho ();
152
153
154     if (use_colors)     /* Set up colors */
155         color_setup ();
156
157
158     dialog_clear ();
159 }
160
161 /*
162  * Setup for color display
163  */
164 void
165 color_setup (void)
166 {
167     int i;
168
169     if (has_colors ()) {        /* Terminal supports color? */
170         start_color ();
171
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]);
175
176         /* Setup color attributes */
177         for (i = 0; i < ATTRIBUTE_COUNT; i++)
178             attributes[i] = C_ATTR (color_table[i][2], i + 1);
179     }
180 }
181
182 /*
183  * End using dialog functions.
184  */
185 void
186 end_dialog (void)
187 {
188     endwin ();
189 }
190
191
192 /*
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.
197  */
198 void
199 print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
200 {
201     int newl, cur_x, cur_y;
202     int i, prompt_len, room, wlen;
203     char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
204
205     strcpy (tempstr, prompt);
206
207     prompt_len = strlen(tempstr);
208
209     /*
210      * Remove newlines
211      */
212     for(i=0; i<prompt_len; i++) {
213         if(tempstr[i] == '\n') tempstr[i] = ' ';
214     }
215
216     if (prompt_len <= width - x * 2) {  /* If prompt is short */
217         wmove (win, y, (width - prompt_len) / 2);
218         waddstr (win, tempstr);
219     } else {
220         cur_x = x;
221         cur_y = y;
222         newl = 1;
223         word = tempstr;
224         while (word && *word) {
225             sp = strchr(word, ' ');
226             if (sp)
227                 *sp++ = 0;
228
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;
233             wlen = strlen(word);
234             if (wlen > room ||
235                (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
236                      && (!(sp2 = strchr(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
237                 cur_y++;
238                 cur_x = x;
239             }
240             wmove (win, cur_y, cur_x);
241             waddstr (win, word);
242             getyx (win, cur_y, cur_x);
243             cur_x++;
244             if (sp && *sp == ' ') {
245                 cur_x++;        /* double space */
246                 while (*++sp == ' ');
247                 newl = 1;
248             } else
249                 newl = 0;
250             word = sp;
251         }
252     }
253 }
254
255 /*
256  * Print a button
257  */
258 void
259 print_button (WINDOW * win, const char *label, int y, int x, int selected)
260 {
261     int i, temp;
262
263     wmove (win, y, x);
264     wattrset (win, selected ? button_active_attr : button_inactive_attr);
265     waddstr (win, "<");
266     temp = strspn (label, " ");
267     label += temp;
268     wattrset (win, selected ? button_label_active_attr
269               : button_label_inactive_attr);
270     for (i = 0; i < temp; i++)
271         waddch (win, ' ');
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);
279     waddstr (win, ">");
280     wmove (win, y, x + temp + 1);
281 }
282
283 /*
284  * Draw a rectangular box with line drawing characters
285  */
286 void
287 draw_box (WINDOW * win, int y, int x, int height, int width,
288           chtype box, chtype border)
289 {
290     int i, j;
291
292     wattrset (win, 0);
293     for (i = 0; i < height; i++) {
294         wmove (win, y + i, x);
295         for (j = 0; j < width; j++)
296             if (!i && !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);
304             else if (!i)
305                 waddch (win, border | ACS_HLINE);
306             else if (i == height - 1)
307                 waddch (win, box | ACS_HLINE);
308             else if (!j)
309                 waddch (win, border | ACS_VLINE);
310             else if (j == width - 1)
311                 waddch (win, box | ACS_VLINE);
312             else
313                 waddch (win, box | ' ');
314     }
315 }
316
317 /*
318  * Draw shadows along the right and bottom edge to give a more 3D look
319  * to the boxes
320  */
321 void
322 draw_shadow (WINDOW * win, int y, int x, int height, int width)
323 {
324     int i;
325
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);
335         }
336         wnoutrefresh (win);
337     }
338 }
339
340 /*
341  *  Return the position of the first alphabetic character in a string.
342  */
343 int
344 first_alpha(const char *string, const char *exempt)
345 {
346         int i, in_paren=0, c;
347
348         for (i = 0; i < strlen(string); i++) {
349                 c = tolower(string[i]);
350
351                 if (strchr("<[(", c)) ++in_paren;
352                 if (strchr(">])", c) && in_paren > 0) --in_paren;
353
354                 if ((! in_paren) && isalpha(c) &&
355                      strchr(exempt, c) == 0)
356                         return i;
357         }
358
359         return 0;
360 }
361
362 /*
363  * Get the first selected item in the dialog_list_item list.
364  */
365 struct dialog_list_item *
366 first_sel_item(int item_no, struct dialog_list_item ** items)
367 {
368         int i;
369
370         for (i = 0; i < item_no; i++) {
371                 if (items[i]->selected)
372                         return items[i];
373         }
374
375         return NULL;
376 }