base/config/kconfig/lxdialog/util.c

Go to the documentation of this file.
00001 /*
00002  *  util.c
00003  *
00004  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
00005  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
00006  *
00007  *  This program is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU General Public License
00009  *  as published by the Free Software Foundation; either version 2
00010  *  of the License, or (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include "dialog.h"
00023 
00024 
00025 /* use colors by default? */
00026 bool use_colors = 1;
00027 
00028 const char *backtitle = NULL;
00029 
00030 const char *dialog_result;
00031 
00032 /* 
00033  * Attribute values, default is for mono display
00034  */
00035 chtype attributes[] =
00036 {
00037     A_NORMAL,           /* screen_attr */
00038     A_NORMAL,           /* shadow_attr */
00039     A_NORMAL,           /* dialog_attr */
00040     A_BOLD,         /* title_attr */
00041     A_NORMAL,           /* border_attr */
00042     A_REVERSE,          /* button_active_attr */
00043     A_DIM,          /* button_inactive_attr */
00044     A_REVERSE,          /* button_key_active_attr */
00045     A_BOLD,         /* button_key_inactive_attr */
00046     A_REVERSE,          /* button_label_active_attr */
00047     A_NORMAL,           /* button_label_inactive_attr */
00048     A_NORMAL,           /* inputbox_attr */
00049     A_NORMAL,           /* inputbox_border_attr */
00050     A_NORMAL,           /* searchbox_attr */
00051     A_BOLD,         /* searchbox_title_attr */
00052     A_NORMAL,           /* searchbox_border_attr */
00053     A_BOLD,         /* position_indicator_attr */
00054     A_NORMAL,           /* menubox_attr */
00055     A_NORMAL,           /* menubox_border_attr */
00056     A_NORMAL,           /* item_attr */
00057     A_REVERSE,          /* item_selected_attr */
00058     A_BOLD,         /* tag_attr */
00059     A_REVERSE,          /* tag_selected_attr */
00060     A_BOLD,         /* tag_key_attr */
00061     A_REVERSE,          /* tag_key_selected_attr */
00062     A_BOLD,         /* check_attr */
00063     A_REVERSE,          /* check_selected_attr */
00064     A_BOLD,         /* uarrow_attr */
00065     A_BOLD          /* darrow_attr */
00066 };
00067 
00068 
00069 #include "colors.h"
00070 
00071 /*
00072  * Table of color values
00073  */
00074 int color_table[][3] =
00075 {
00076     {SCREEN_FG, SCREEN_BG, SCREEN_HL},
00077     {SHADOW_FG, SHADOW_BG, SHADOW_HL},
00078     {DIALOG_FG, DIALOG_BG, DIALOG_HL},
00079     {TITLE_FG, TITLE_BG, TITLE_HL},
00080     {BORDER_FG, BORDER_BG, BORDER_HL},
00081     {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
00082     {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
00083     {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
00084     {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG, BUTTON_KEY_INACTIVE_HL},
00085     {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG, BUTTON_LABEL_ACTIVE_HL},
00086     {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
00087      BUTTON_LABEL_INACTIVE_HL},
00088     {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
00089     {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
00090     {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
00091     {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
00092     {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
00093     {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
00094     {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
00095     {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
00096     {ITEM_FG, ITEM_BG, ITEM_HL},
00097     {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
00098     {TAG_FG, TAG_BG, TAG_HL},
00099     {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
00100     {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
00101     {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
00102     {CHECK_FG, CHECK_BG, CHECK_HL},
00103     {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
00104     {UARROW_FG, UARROW_BG, UARROW_HL},
00105     {DARROW_FG, DARROW_BG, DARROW_HL},
00106 };              /* color_table */
00107 
00108 /*
00109  * Set window to attribute 'attr'
00110  */
00111 void
00112 attr_clear (WINDOW * win, int height, int width, chtype attr)
00113 {
00114     int i, j;
00115 
00116     wattrset (win, attr);
00117     for (i = 0; i < height; i++) {
00118     wmove (win, i, 0);
00119     for (j = 0; j < width; j++)
00120         waddch (win, ' ');
00121     }
00122     touchwin (win);
00123 }
00124 
00125 void dialog_clear (void)
00126 {
00127     attr_clear (stdscr, LINES, COLS, screen_attr);
00128     /* Display background title if it exists ... - SLH */
00129     if (backtitle != NULL) {
00130         int i;
00131 
00132         wattrset (stdscr, screen_attr);
00133         mvwaddstr (stdscr, 0, 1, (char *)backtitle);
00134         wmove (stdscr, 1, 1);
00135         for (i = 1; i < COLS - 1; i++)
00136             waddch (stdscr, ACS_HLINE);
00137     }
00138     wnoutrefresh (stdscr);
00139 }
00140 
00141 /*
00142  * Do some initialization for dialog
00143  */
00144 void
00145 init_dialog (void)
00146 {
00147     initscr ();         /* Init curses */
00148     keypad (stdscr, TRUE);
00149     cbreak ();
00150     noecho ();
00151 
00152 
00153     if (use_colors) /* Set up colors */
00154     color_setup ();
00155 
00156 
00157     dialog_clear ();
00158 }
00159 
00160 /*
00161  * Setup for color display
00162  */
00163 void
00164 color_setup (void)
00165 {
00166     int i;
00167 
00168     if (has_colors ()) {    /* Terminal supports color? */
00169     start_color ();
00170 
00171     /* Initialize color pairs */
00172     for (i = 0; i < ATTRIBUTE_COUNT; i++)
00173         init_pair (i + 1, color_table[i][0], color_table[i][1]);
00174 
00175     /* Setup color attributes */
00176     for (i = 0; i < ATTRIBUTE_COUNT; i++)
00177         attributes[i] = C_ATTR (color_table[i][2], i + 1);
00178     }
00179 }
00180 
00181 /*
00182  * End using dialog functions.
00183  */
00184 void
00185 end_dialog (void)
00186 {
00187     endwin ();
00188 }
00189 
00190 
00191 /*
00192  * Print a string of text in a window, automatically wrap around to the
00193  * next line if the string is too long to fit on one line. Newline
00194  * characters '\n' are replaced by spaces.  We start on a new line
00195  * if there is no room for at least 4 nonblanks following a double-space.
00196  */
00197 void
00198 print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
00199 {
00200     int newl, cur_x, cur_y;
00201     int i, prompt_len, room, wlen;
00202     char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
00203 
00204     strcpy (tempstr, prompt);
00205 
00206     prompt_len = strlen(tempstr);
00207     
00208     /*
00209      * Remove newlines
00210      */
00211     for(i=0; i<prompt_len; i++) {
00212     if(tempstr[i] == '\n') tempstr[i] = ' ';
00213     }
00214 
00215     if (prompt_len <= width - x * 2) {  /* If prompt is short */
00216     wmove (win, y, (width - prompt_len) / 2);
00217     waddstr (win, tempstr);
00218     } else {
00219     cur_x = x;
00220     cur_y = y;
00221     newl = 1;
00222     word = tempstr;
00223     while (word && *word) {
00224         sp = index(word, ' ');
00225         if (sp)
00226             *sp++ = 0;
00227 
00228         /* Wrap to next line if either the word does not fit,
00229            or it is the first word of a new sentence, and it is
00230            short, and the next word does not fit. */
00231         room = width - cur_x;
00232         wlen = strlen(word);
00233         if (wlen > room ||
00234            (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
00235              && (!(sp2 = index(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
00236         cur_y++;
00237         cur_x = x;
00238         }
00239         wmove (win, cur_y, cur_x);
00240         waddstr (win, word);
00241         getyx (win, cur_y, cur_x);
00242         cur_x++;
00243         if (sp && *sp == ' ') {
00244             cur_x++;    /* double space */
00245         while (*++sp == ' ');
00246         newl = 1;
00247         } else
00248             newl = 0;
00249         word = sp;
00250     }
00251     }
00252 }
00253 
00254 /*
00255  * Print a button
00256  */
00257 void
00258 print_button (WINDOW * win, const char *label, int y, int x, int selected)
00259 {
00260     int i, temp;
00261 
00262     wmove (win, y, x);
00263     wattrset (win, selected ? button_active_attr : button_inactive_attr);
00264     waddstr (win, "<");
00265     temp = strspn (label, " ");
00266     label += temp;
00267     wattrset (win, selected ? button_label_active_attr
00268           : button_label_inactive_attr);
00269     for (i = 0; i < temp; i++)
00270     waddch (win, ' ');
00271     wattrset (win, selected ? button_key_active_attr
00272           : button_key_inactive_attr);
00273     waddch (win, label[0]);
00274     wattrset (win, selected ? button_label_active_attr
00275           : button_label_inactive_attr);
00276     waddstr (win, (char *)label + 1);
00277     wattrset (win, selected ? button_active_attr : button_inactive_attr);
00278     waddstr (win, ">");
00279     wmove (win, y, x + temp + 1);
00280 }
00281 
00282 /*
00283  * Draw a rectangular box with line drawing characters
00284  */
00285 void
00286 draw_box (WINDOW * win, int y, int x, int height, int width,
00287       chtype box, chtype border)
00288 {
00289     int i, j;
00290 
00291     wattrset (win, 0);
00292     for (i = 0; i < height; i++) {
00293     wmove (win, y + i, x);
00294     for (j = 0; j < width; j++)
00295         if (!i && !j)
00296         waddch (win, border | ACS_ULCORNER);
00297         else if (i == height - 1 && !j)
00298         waddch (win, border | ACS_LLCORNER);
00299         else if (!i && j == width - 1)
00300         waddch (win, box | ACS_URCORNER);
00301         else if (i == height - 1 && j == width - 1)
00302         waddch (win, box | ACS_LRCORNER);
00303         else if (!i)
00304         waddch (win, border | ACS_HLINE);
00305         else if (i == height - 1)
00306         waddch (win, box | ACS_HLINE);
00307         else if (!j)
00308         waddch (win, border | ACS_VLINE);
00309         else if (j == width - 1)
00310         waddch (win, box | ACS_VLINE);
00311         else
00312         waddch (win, box | ' ');
00313     }
00314 }
00315 
00316 /*
00317  * Draw shadows along the right and bottom edge to give a more 3D look
00318  * to the boxes
00319  */
00320 void
00321 draw_shadow (WINDOW * win, int y, int x, int height, int width)
00322 {
00323     int i;
00324 
00325     if (has_colors ()) {    /* Whether terminal supports color? */
00326     wattrset (win, shadow_attr);
00327     wmove (win, y + height, x + 2);
00328     for (i = 0; i < width; i++)
00329         waddch (win, winch (win) & A_CHARTEXT);
00330     for (i = y + 1; i < y + height + 1; i++) {
00331         wmove (win, i, x + width);
00332         waddch (win, winch (win) & A_CHARTEXT);
00333         waddch (win, winch (win) & A_CHARTEXT);
00334     }
00335     wnoutrefresh (win);
00336     }
00337 }
00338 
00339 /*
00340  *  Return the position of the first alphabetic character in a string.
00341  */
00342 int
00343 first_alpha(const char *string, const char *exempt)
00344 {
00345     int i, in_paren=0, c;
00346 
00347     for (i = 0; i < strlen(string); i++) {
00348         c = tolower(string[i]);
00349 
00350         if (strchr("<[(", c)) ++in_paren;
00351         if (strchr(">])", c) && in_paren > 0) --in_paren;
00352 
00353         if ((! in_paren) && isalpha(c) && 
00354              strchr(exempt, c) == 0)
00355             return i;
00356     }
00357 
00358     return 0;
00359 }

Generated on Tue Feb 2 17:46:04 2010 for RTAI API by  doxygen 1.4.7