2 * dialog - Display simple dialog boxes from shell scripts
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * 17/12/93 - Version 0.1 released.
25 * 19/12/93 - menu will now scroll if there are more items than can fit
27 * - added 'checklist', a dialog box with a list of options that
28 * can be turned on or off. A list of options that are on is
31 * 20/12/93 - Version 0.15 released.
33 * 29/12/93 - Incorporated patch from Patrick J. Volkerding
34 * (volkerdi@mhd1.moorhead.msus.edu) that made these changes:
35 * - increased MAX_LEN to 2048
36 * - added 'infobox', equivalent to a message box without pausing
37 * - added option '--clear' that will clear the screen
38 * - Explicit line breaking when printing prompt text can be
39 * invoked by real newline '\n' besides the string "\n"
40 * - an optional parameter '--title <string>' can be used to
41 * specify a title string for the dialog box
43 * 03/01/94 - added 'textbox', a dialog box for displaying text from a file.
44 * - Version 0.2 released.
46 * 04/01/94 - some fixes and improvements for 'textbox':
47 * - fixed a bug that will cause a segmentation violation when a
48 * line is longer than MAX_LEN characters. Lines will now be
49 * truncated if they are longer than MAX_LEN characters.
50 * - removed wrefresh() from print_line(). This will increase
51 * efficiency of print_page() which calls print_line().
52 * - display current position in the form of percentage into file.
53 * - Version 0.21 released.
55 * 05/01/94 - some changes for faster screen update.
57 * 07/01/94 - much more flexible color settings. Can use all 16 colors
58 * (8 normal, 8 highlight) of the Linux console.
60 * 08/01/94 - added run-time configuration using configuration file.
62 * 09/01/94 - some minor bug fixes and cleanups for menubox, checklist and
65 * 11/01/94 - added a man page.
67 * 13/01/94 - some changes for easier porting to other Unix systems (tested
68 * on Ultrix, SunOS and HPUX)
69 * - Version 0.3 released.
71 * 08/06/94 - Patches by Stuart Herbert - S.Herbert@shef.ac.uk
72 * Fixed attr_clear and the textbox stuff to work with ncurses 1.8.5
73 * Fixed the wordwrap routine - it'll actually wrap properly now
74 * Added a more 3D look to everything - having your own rc file could
75 * prove 'interesting' to say the least :-)
76 * Added radiolist option
77 * - Version 0.4 released.
82 #define __DIALOG_MAIN__
86 #include "dialog.priv.h"
91 /* These are two "secret" globals that can be fiddled to make a dialog
92 * come up someplace other than a "centered" calculation for X,Y
96 /* This "secret" global allows you to change the behavior of an input field */
100 * Do some initialization for dialog
102 void init_dialog(void)
106 errx(1, "libdialog is unsafe to use in setugid applications");
110 (void) setlocale(LC_ALL, "");
114 if (parse_rc() == -1) /* Read the configuration file */
118 if (initscr() == NULL) { /* Init curses */
119 fprintf(stderr, "\nCurses initialization error.\n");
122 keypad(stdscr, TRUE);
127 if (use_colors || use_shadow) /* Set up colors */
131 /* Set screen to screen attribute */
132 dialog_clear_norefresh();
133 DialogX = DialogY = 0;
135 /* End of init_dialog() */
140 * Setup for color display
142 void color_setup(void)
146 if (has_colors()) { /* Terminal supports color? */
149 /* Initialize color pairs */
150 for (i = 0; i < ATTRIBUTE_COUNT; i++)
151 init_pair(i+1, color_table[i][0], color_table[i][1]);
153 /* Setup color attributes */
154 for (i = 0; i < ATTRIBUTE_COUNT; i++)
155 attributes[i] = C_ATTR(color_table[i][2], i+1);
158 /* End of color_setup() */
163 * Set window to attribute 'attr'
165 void attr_clear(WINDOW *win, int height, int width, chtype attr)
169 wattrset(win, attr); /* Set window to attribute 'attr' */
170 for (i = 0; i < height; i++) {
172 for (j = 0; j < width; j++)
176 /* End of attr_clear() */
180 * Print a string of text in a window, automatically wrap around to the
181 * next line if the string is too long to fit on one line. Note that the
182 * string may contain "\n" to represent a newline character or the real
183 * newline '\n', but in that case, auto wrap around will be disabled.
185 void print_autowrap(WINDOW *win, unsigned char *prompt, int height, int width, int maxwidth, int y, int x, int center, int rawmode)
188 unsigned char tempstr[MAX_LEN+1], *word, *tempptr, *tempptr1;
189 chtype ostuff[132], attrs = 0, init_bottom = 0;
191 wsetscrreg(win, y, height);
192 getyx(win, cur_y, cur_x);
194 strncpy(tempstr, prompt, MAX_LEN);
195 tempstr[MAX_LEN] = '\0';
196 if ((!rawmode && strstr(tempstr, "\\n") != NULL) ||
197 (strchr(tempstr, '\n') != NULL)) { /* Prompt contains "\n" or '\n' */
200 tempptr = rawmode ? NULL : strstr(word, "\\n");
201 tempptr1 = strchr(word, '\n');
202 if (tempptr == NULL && tempptr1 == NULL)
204 else if (tempptr == NULL) { /* No more "\n" */
208 else if (tempptr1 == NULL) { /* No more '\n' */
212 else { /* Prompt contains both "\n" and '\n' */
213 if (strlen(tempptr)-2 < strlen(tempptr1)-1) {
225 if (++cur_y > height) {
228 for (i = 0; i < x; i++)
229 ostuff[i] = mvwinch(win, cur_y, i);
230 for (i = width; i < maxwidth; i++)
231 ostuff[i] = mvwinch(win, cur_y, i);
232 attrs = getattrs(win);
237 scrollok(win, FALSE);
238 wmove(win, cur_y, 0);
239 for (i = 0; i < x; i++) {
240 wattrset(win, ostuff[i]&A_ATTRIBUTES);
241 waddch(win, ostuff[i]);
243 wattrset(win, attrs);
244 for ( ; i < width; i++)
246 for ( ; i < maxwidth; i++) {
247 wattrset(win, ostuff[i]&A_ATTRIBUTES);
248 waddch(win, ostuff[i]);
250 wattrset(win, attrs);
253 wmove(win, cur_y, cur_x = x);
257 else if (center && strlen(tempstr) <= width-x*2) { /* If prompt is short */
258 wmove(win, cur_y, (width - strlen(tempstr)) / 2);
259 waddstr(win, tempstr);
261 else if (!center && strlen(tempstr) <= width-cur_x) { /* If prompt is short */
262 waddstr(win, tempstr);
267 /* Print prompt word by word, wrap around if necessary */
268 while ((word = strsep(&p, "\t\n ")) != NULL) {
276 if (cur_x+strlen(word) >= width+1) { /* wrap around to next line */
277 if (x+strlen(word) >= width+1) {
278 sc = word[width-cur_x-1];
279 word[width-cur_x-1] = '\0';
280 wmove(win, cur_y, cur_x);
282 word[width-cur_x-1] = sc;
283 word += width-cur_x-1;
284 getyx(win, cur_y, cur_x);
289 if (cur_y > height) {
292 for (i = 0; i < x; i++)
293 ostuff[i] = mvwinch(win, cur_y, i);
294 for (i = width; i < maxwidth; i++)
295 ostuff[i] = mvwinch(win, cur_y, i);
296 attrs = getattrs(win);
301 scrollok(win, FALSE);
302 wmove(win, cur_y, 0);
303 for (i = 0; i < x; i++) {
304 wattrset(win, ostuff[i]&A_ATTRIBUTES);
305 waddch(win, ostuff[i]);
307 wattrset(win, attrs);
308 for ( ; i < width; i++)
310 for ( ; i < maxwidth; i++) {
311 wattrset(win, ostuff[i]&A_ATTRIBUTES);
312 waddch(win, ostuff[i]);
314 wattrset(win, attrs);
320 wmove(win, cur_y, cur_x);
322 getyx(win, cur_y, cur_x);
327 /* End of print_autowrap() */
333 void print_button(WINDOW *win, unsigned char *label, int y, int x, int selected)
338 wattrset(win, selected ? button_active_attr : button_inactive_attr);
339 waddstr(win, selected ? "[" : " ");
340 temp = strspn(label, " ");
342 for (i = 0; i < temp; i++)
344 wattrset(win, selected ? button_key_active_attr : button_key_inactive_attr);
345 waddch(win, label[0]);
346 wattrset(win, selected ? button_active_attr : button_inactive_attr);
347 waddstr(win, label+1);
348 waddstr(win, selected ? "]" : " ");
349 wmove(win, y, x+temp+1);
351 /* End of print_button() */
355 * Draw a rectangular box with line drawing characters
357 void draw_box(WINDOW *win, int y, int x, int height, int width, chtype box, chtype border)
362 for (i = 0; i < height; i++) {
363 wmove(win, y + i, x);
364 for (j = 0; j < width; j++)
366 waddch(win, border | ACS_ULCORNER);
367 else if (i == height-1 && !j)
368 waddch(win, border | ACS_LLCORNER);
369 else if (!i && j == width-1)
370 waddch(win, box | ACS_URCORNER);
371 else if (i == height-1 && j == width-1)
372 waddch(win, box | ACS_LRCORNER);
374 waddch(win, border | ACS_HLINE);
375 else if (i == height-1)
376 waddch(win, box | ACS_HLINE);
378 waddch(win, border | ACS_VLINE);
379 else if (j == width-1)
380 waddch(win, box | ACS_VLINE);
382 waddch(win, box | ' ');
385 /* End of draw_box() */
390 * Draw shadows along the right and bottom edge to give a more 3D look
393 void draw_shadow(WINDOW *win, int y, int x, int height, int width)
398 if (has_colors()) { /* Whether terminal supports color? */
400 attrs = getattrs(win);
401 if (y+height < getmaxy(win)) {
403 wattrset(win, A_INVIS);
404 wmove(win, y + height, x + 2);
405 for (i = 0; i < width; i++)
406 if (i+x+2 < getmaxx(win))
409 wattrset(win, shadow_attr);
410 wmove(win, y + height, x + 2);
411 for (i = 0; i < width; i++)
412 if (i+x+2 < getmaxx(win))
413 waddch(win, mvwinch(newscr, sy+y+height, sx+x+2+i) & A_CHARTEXT);
415 if (x+width < getmaxx(win)) {
416 for (i = y + 1; i < y + height + 1; i++) {
417 if (i < getmaxy(win)) {
419 wattrset(win, A_INVIS);
420 wmove(win, i, x + width);
422 if (x+width+1 < getmaxx(win))
425 wattrset(win, shadow_attr);
426 wmove(win, i, x + width);
427 waddch(win, mvwinch(newscr, sy+i, sx+x+width) & A_CHARTEXT);
428 if (x+width+1 < getmaxx(win))
429 waddch(win, mvwinch(newscr, sy+i, sx+x+width+1) & A_CHARTEXT);
433 wattrset(win, attrs);
437 /* End of draw_shadow() */
440 void dialog_clear_norefresh(void)
442 attr_clear(stdscr, LINES, COLS, screen_attr);
444 wnoutrefresh(stdscr);
447 void dialog_clear(void)
449 dialog_clear_norefresh();
453 void dialog_update(void)
458 void end_dialog(void)
463 int strwidth(const char *p)
465 int i = 0, len, incr;
466 const char *start, *s, *s1, *s2;
468 for (start = s = p; ; start = (s += incr)) {
469 s1 = strchr(s, '\n');
470 s2 = strstr(s, "\\n");
479 incr = 1 + (s == s2);
490 int strheight(const char *p)
493 const char *s, *s1, *s2;
495 for (s = p; ; s += incr) {
496 s1 = strchr(s, '\n');
497 s2 = strstr(s, "\\n");
506 incr = 1 + (s == s2);
512 void print_arrows(WINDOW *dialog, int scroll, int menu_height, int item_no,
513 int box_x, int box_y, int tag_x, int cur_x, int cur_y)
515 wmove(dialog, box_y, box_x + tag_x + 1);
516 wattrset(dialog, scroll ? uarrow_attr : menubox_attr);
517 waddch(dialog, scroll ? ACS_UARROW : ACS_HLINE);
518 wmove(dialog, box_y, box_x + tag_x + 2);
519 waddch(dialog, scroll ? '(' : ACS_HLINE);
520 wmove(dialog, box_y, box_x + tag_x + 3);
521 waddch(dialog, scroll ? '-' : ACS_HLINE);
522 wmove(dialog, box_y, box_x + tag_x + 4);
523 waddch(dialog, scroll ? ')' : ACS_HLINE);
524 wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 1);
525 wattrset(dialog, scroll+menu_height < item_no ? darrow_attr : menubox_border_attr);
526 waddch(dialog, scroll+menu_height < item_no ? ACS_DARROW : ACS_HLINE);
527 wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 2);
528 waddch(dialog, scroll+menu_height < item_no ? '(' : ACS_HLINE);
529 wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 3);
530 waddch(dialog, scroll+menu_height < item_no ? '+' : ACS_HLINE);
531 wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 4);
532 waddch(dialog, scroll+menu_height < item_no ? ')' : ACS_HLINE);
533 wmove(dialog, cur_y, cur_x); /* Restore cursor position */