root/scripts/lxdialog/util.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. attr_clear
  2. dialog_clear
  3. init_dialog
  4. color_setup
  5. end_dialog
  6. print_autowrap
  7. print_button
  8. draw_box
  9. draw_shadow
  10. first_alpha

   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? */
  26 bool use_colors = 1;
  27 
  28 const char *backtitle = NULL;
  29 
  30 const char *dialog_result;
  31 
  32 /* 
  33  * Attribute values, default is for mono display
  34  */
  35 chtype attributes[] =
  36 {
  37     A_NORMAL,                   /* screen_attr */
  38     A_NORMAL,                   /* shadow_attr */
  39     A_REVERSE,                  /* dialog_attr */
  40     A_REVERSE,                  /* title_attr */
  41     A_REVERSE,                  /* border_attr */
  42     A_BOLD,                     /* button_active_attr */
  43     A_DIM,                      /* button_inactive_attr */
  44     A_UNDERLINE,                /* button_key_active_attr */
  45     A_UNDERLINE,                /* button_key_inactive_attr */
  46     A_NORMAL,                   /* button_label_active_attr */
  47     A_NORMAL,                   /* button_label_inactive_attr */
  48     A_REVERSE,                  /* inputbox_attr */
  49     A_REVERSE,                  /* inputbox_border_attr */
  50     A_REVERSE,                  /* searchbox_attr */
  51     A_REVERSE,                  /* searchbox_title_attr */
  52     A_REVERSE,                  /* searchbox_border_attr */
  53     A_REVERSE,                  /* position_indicator_attr */
  54     A_REVERSE,                  /* menubox_attr */
  55     A_REVERSE,                  /* menubox_border_attr */
  56     A_REVERSE,                  /* item_attr */
  57     A_NORMAL,                   /* item_selected_attr */
  58     A_REVERSE,                  /* tag_attr */
  59     A_REVERSE,                  /* tag_selected_attr */
  60     A_NORMAL,                   /* tag_key_attr */
  61     A_BOLD,                     /* tag_key_selected_attr */
  62     A_REVERSE,                  /* check_attr */
  63     A_REVERSE,                  /* check_selected_attr */
  64     A_REVERSE,                  /* uarrow_attr */
  65     A_REVERSE                   /* darrow_attr */
  66 };
  67 
  68 
  69 #include "colors.h"
  70 
  71 /*
  72  * Table of color values
  73  */
  74 int 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  */
 111 void
 112 attr_clear (WINDOW * win, int height, int width, chtype attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 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 
 125 void dialog_clear (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  */
 144 void
 145 init_dialog (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147     initscr ();                 /* Init curses */
 148     keypad (stdscr, TRUE);
 149     cbreak ();
 150     noecho ();
 151 
 152 
 153     if (use_colors)     /* Set up colors */
 154         color_setup ();
 155 
 156 
 157     dialog_clear ();
 158 }
 159 
 160 /*
 161  * Setup for color display
 162  */
 163 void
 164 color_setup (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166     int i;
 167 
 168     if (has_colors ()) {        /* Terminal supports color? */
 169         start_color ();
 170 
 171         /* Initialize color pairs */
 172         for (i = 0; i < ATTRIBUTE_COUNT; i++)
 173             init_pair (i + 1, color_table[i][0], color_table[i][1]);
 174 
 175         /* Setup color attributes */
 176         for (i = 0; i < ATTRIBUTE_COUNT; i++)
 177             attributes[i] = C_ATTR (color_table[i][2], i + 1);
 178     }
 179 }
 180 
 181 /*
 182  * End using dialog functions.
 183  */
 184 void
 185 end_dialog (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187     endwin ();
 188 }
 189 
 190 
 191 /*
 192  * Print a string of text in a window, automatically wrap around to the
 193  * next line if the string is too long to fit on one line. Note that the
 194  * string may contain "\n" to represent a newline character or the real
 195  * newline '\n', but in that case, auto wrap around will be disabled.
 196  */
 197 void
 198 print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
     /* [previous][next][first][last][top][bottom][index][help] */
 199 {
 200     int first = 1, cur_x, cur_y;
 201     int i, prompt_len;
 202     char tempstr[MAX_LEN + 1], *word /*,  *tempptr, *tempptr1*/;
 203 
 204     strcpy (tempstr, prompt);
 205 
 206     prompt_len = strlen(tempstr);
 207         
 208     /*
 209      * Remove newlines
 210      */
 211     for(i=0; i<prompt_len; i++) {
 212         if(tempstr[i] == '\n') tempstr[i] = ' ';
 213     }
 214 
 215     if (strlen (tempstr) <= width - x * 2) {    /* If prompt is short */
 216         wmove (win, y, (width - strlen (tempstr)) / 2);
 217         waddstr (win, tempstr);
 218     } else {
 219         cur_x = x;
 220         cur_y = y;
 221         /* Print prompt word by word, wrap around if necessary */
 222         while ((word = strtok (first ? tempstr : NULL, " ")) != NULL) {
 223             if (first)          /* First iteration */
 224                 first = 0;
 225             if (cur_x + strlen (word) > width) {
 226                 cur_y++;        /* wrap to next line */
 227                 cur_x = x;
 228             }
 229             wmove (win, cur_y, cur_x);
 230             waddstr (win, word);
 231             getyx (win, cur_y, cur_x);
 232             cur_x++;
 233         }
 234     }
 235 }
 236 
 237 /*
 238  * Print a button
 239  */
 240 void
 241 print_button (WINDOW * win, const char *label, int y, int x, int selected)
     /* [previous][next][first][last][top][bottom][index][help] */
 242 {
 243     int i, temp;
 244 
 245     wmove (win, y, x);
 246     wattrset (win, selected ? button_active_attr : button_inactive_attr);
 247     waddstr (win, "<");
 248     temp = strspn (label, " ");
 249     label += temp;
 250     wattrset (win, selected ? button_label_active_attr
 251               : button_label_inactive_attr);
 252     for (i = 0; i < temp; i++)
 253         waddch (win, ' ');
 254     wattrset (win, selected ? button_key_active_attr
 255               : button_key_inactive_attr);
 256     waddch (win, label[0]);
 257     wattrset (win, selected ? button_label_active_attr
 258               : button_label_inactive_attr);
 259     waddstr (win, (char *)label + 1);
 260     wattrset (win, selected ? button_active_attr : button_inactive_attr);
 261     waddstr (win, ">");
 262     wmove (win, y, x + temp + 1);
 263 }
 264 
 265 /*
 266  * Draw a rectangular box with line drawing characters
 267  */
 268 void
 269 draw_box (WINDOW * win, int y, int x, int height, int width,
     /* [previous][next][first][last][top][bottom][index][help] */
 270           chtype box, chtype border)
 271 {
 272     int i, j;
 273 
 274     wattrset (win, 0);
 275     for (i = 0; i < height; i++) {
 276         wmove (win, y + i, x);
 277         for (j = 0; j < width; j++)
 278             if (!i && !j)
 279                 waddch (win, border | ACS_ULCORNER);
 280             else if (i == height - 1 && !j)
 281                 waddch (win, border | ACS_LLCORNER);
 282             else if (!i && j == width - 1)
 283                 waddch (win, box | ACS_URCORNER);
 284             else if (i == height - 1 && j == width - 1)
 285                 waddch (win, box | ACS_LRCORNER);
 286             else if (!i)
 287                 waddch (win, border | ACS_HLINE);
 288             else if (i == height - 1)
 289                 waddch (win, box | ACS_HLINE);
 290             else if (!j)
 291                 waddch (win, border | ACS_VLINE);
 292             else if (j == width - 1)
 293                 waddch (win, box | ACS_VLINE);
 294             else
 295                 waddch (win, box | ' ');
 296     }
 297 }
 298 
 299 /*
 300  * Draw shadows along the right and bottom edge to give a more 3D look
 301  * to the boxes
 302  */
 303 void
 304 draw_shadow (WINDOW * win, int y, int x, int height, int width)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306     int i;
 307 
 308     if (has_colors ()) {        /* Whether terminal supports color? */
 309         wattrset (win, shadow_attr);
 310         wmove (win, y + height, x + 2);
 311         for (i = 0; i < width; i++)
 312             waddch (win, winch (win) & A_CHARTEXT);
 313         for (i = y + 1; i < y + height + 1; i++) {
 314             wmove (win, i, x + width);
 315             waddch (win, winch (win) & A_CHARTEXT);
 316             waddch (win, winch (win) & A_CHARTEXT);
 317         }
 318         wnoutrefresh (win);
 319     }
 320 }
 321 
 322 /*
 323  *  Return the position of the first alphabetic character in a string.
 324  */
 325 int
 326 first_alpha(const char *string)
     /* [previous][next][first][last][top][bottom][index][help] */
 327 {
 328         int i, in_paren=0;
 329 
 330         for (i = 0; i < strlen(string); i++) {
 331                 if (string[i] == '(') ++in_paren;
 332                 if (string[i] == ')') --in_paren;
 333 
 334                 if ((! in_paren) && isalpha(string[i]))
 335                         return i;
 336         }
 337 
 338         return 0;
 339 }

/* [previous][next][first][last][top][bottom][index][help] */