root/scripts/lxdialog/menubox.c

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

DEFINITIONS

This source file includes following definitions.
  1. print_item
  2. print_arrows
  3. print_buttons
  4. dialog_menu

   1 /*
   2  *  menubox.c -- implements the menu box
   3  *
   4  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
   5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcapw@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 static int menu_width, item_x;
  25 
  26 /*
  27  * Print menu item
  28  */
  29 static void
  30 print_item (WINDOW * win, const char *item, int choice, int selected, int hotkey)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32     int i, j;
  33     char menu_item[menu_width+1];
  34 
  35     strncpy(menu_item, item, menu_width);
  36     menu_item[menu_width] = 0;
  37     j = first_alpha(menu_item);
  38 
  39     /* Clear 'residue' of last item */
  40     wattrset (win, menubox_attr);
  41     wmove (win, choice, 0);
  42     for (i = 0; i < menu_width; i++)
  43         waddch (win, ' ');
  44     wattrset (win, selected ? item_selected_attr : item_attr);
  45     mvwaddstr (win, choice, item_x, menu_item);
  46     if (hotkey) {
  47         wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);
  48         mvwaddch(win, choice, item_x+j, menu_item[j]);
  49     }
  50 }
  51 
  52 /*
  53  * Print the scroll indicators.
  54  */
  55 static void
  56 print_arrows (WINDOW * win, int item_no, int scroll,
     /* [previous][next][first][last][top][bottom][index][help] */
  57                 int y, int x, int height)
  58 {
  59     int cur_y, cur_x;
  60 
  61     getyx(win, cur_y, cur_x);
  62 
  63     wmove(win, y, x);
  64 
  65     if (scroll > 0) {
  66         wattrset (win, uarrow_attr);
  67         waddch (win, ACS_UARROW);
  68         waddstr (win, "(-)");
  69     }
  70     else {
  71         wattrset (win, menubox_attr);
  72         waddch (win, ACS_HLINE);
  73         waddch (win, ACS_HLINE);
  74         waddch (win, ACS_HLINE);
  75         waddch (win, ACS_HLINE);
  76     }
  77 
  78    y = y + height + 1;
  79    wmove(win, y, x);
  80 
  81    if ((height < item_no) && (scroll + height < item_no)) {
  82         wattrset (win, darrow_attr);
  83         waddch (win, ACS_DARROW);
  84         waddstr (win, "(+)");
  85     }
  86     else {
  87         wattrset (win, menubox_border_attr);
  88         waddch (win, ACS_HLINE);
  89         waddch (win, ACS_HLINE);
  90         waddch (win, ACS_HLINE);
  91         waddch (win, ACS_HLINE);
  92    }
  93 
  94    wmove(win, cur_y, cur_x);
  95 }
  96 
  97 /*
  98  * Display the termination buttons.
  99  */
 100 static void
 101 print_buttons (WINDOW *win, int height, int width, int selected)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103     int x = width / 2 - 16;
 104     int y = height - 2;
 105 
 106     print_button (win, "Select", y, x, selected == 0);
 107     print_button (win, " Exit ", y, x + 12, selected == 1);
 108     print_button (win, " Help ", y, x + 24, selected == 2);
 109 
 110     wmove(win, y, x+1+12*selected);
 111     wrefresh (win);
 112 }
 113 
 114 /*
 115  * Display a menu for choosing among a number of options
 116  */
 117 int
 118 dialog_menu (const char *title, const char *prompt, int height, int width,
     /* [previous][next][first][last][top][bottom][index][help] */
 119                 int menu_height, const char *current, int item_no,
 120                 const char * const * items)
 121 
 122 {
 123     int i, j, x, y, box_x, box_y;
 124     int key = 0, button = 0, scroll = 0, choice = 0, first_item = 0, max_choice;
 125     WINDOW *dialog, *menu;
 126 
 127     max_choice = MIN (menu_height, item_no);
 128 
 129     /* center dialog box on screen */
 130     x = (COLS - width) / 2;
 131     y = (LINES - height) / 2;
 132 
 133     draw_shadow (stdscr, y, x, height, width);
 134 
 135     dialog = newwin (height, width, y, x);
 136     keypad (dialog, TRUE);
 137 
 138     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
 139     wattrset (dialog, border_attr);
 140     mvwaddch (dialog, height - 3, 0, ACS_LTEE);
 141     for (i = 0; i < width - 2; i++)
 142         waddch (dialog, ACS_HLINE);
 143     wattrset (dialog, dialog_attr);
 144     waddch (dialog, ACS_RTEE);
 145 
 146     if (title != NULL) {
 147         wattrset (dialog, title_attr);
 148         mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
 149         waddstr (dialog, (char *)title);
 150         waddch (dialog, ' ');
 151     }
 152 
 153     wattrset (dialog, dialog_attr);
 154     print_autowrap (dialog, prompt, width - 2, 1, 3);
 155 
 156     menu_width = width - 6;
 157     box_y = height - menu_height - 5;
 158     box_x = (width - menu_width) / 2 - 1;
 159 
 160     /* create new window for the menu */
 161     menu = subwin (dialog, menu_height, menu_width,
 162                 y + box_y + 1, x + box_x + 1);
 163     keypad (menu, TRUE);
 164 
 165     /* draw a box around the menu items */
 166     draw_box (dialog, box_y, box_x, menu_height + 2, menu_width + 2,
 167               menubox_border_attr, menubox_attr);
 168 
 169     /*
 170      * Find length of longest item in order to center menu.
 171      * Set 'choice' to default item. 
 172      */
 173     item_x = 0;
 174     for (i = 0; i < item_no; i++) {
 175         item_x = MAX (item_x, MIN(menu_width, strlen (items[i * 2 + 1]) + 2));
 176         if (strcmp(current, items[i*2]) == 0) choice = i;
 177     }
 178 
 179     item_x = (menu_width - item_x) / 2;
 180 
 181     if (choice >= max_choice){
 182         scroll = first_item = choice - max_choice + 1;
 183         choice = max_choice-1;
 184     }
 185 
 186     /* Print the menu */
 187     for (i=0; i < max_choice; i++) {
 188         print_item (menu, items[(first_item + i) * 2 + 1], i, i == choice,
 189                     (items[(first_item + i)*2][0] != ':'));
 190     }
 191 
 192     wnoutrefresh (menu);
 193 
 194     print_arrows(dialog, item_no, scroll,
 195                  box_y, box_x+item_x+1, menu_height);
 196 
 197     print_buttons (dialog, height, width, 0);
 198 
 199     while (key != ESC) {
 200         key = wgetch (dialog);
 201 
 202         for (i = choice+1; i < max_choice; i++) {
 203                 j = first_alpha(items[(scroll+i)*2+1]);
 204                 if (toupper(key) == toupper(items[(scroll+i)*2+1][j]))
 205                         break;
 206         }
 207         if (i == max_choice)
 208                 for (i = 0; i < max_choice; i++) {
 209                         j = first_alpha(items[(scroll+i)*2+1]);
 210                         if (toupper(key) == toupper(items[(scroll+i)*2+1][j]))
 211                                 break;
 212                 }
 213 
 214         if (i < max_choice || 
 215             key == KEY_UP || key == KEY_DOWN ||
 216             key == '-' || key == '+' ||
 217             key == KEY_PPAGE || key == KEY_NPAGE) {
 218 
 219             print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
 220                        (items[(scroll+choice)*2][0] != ':'));
 221 
 222             if (key == KEY_UP || key == '-') {
 223                 if (choice < 6 && scroll) {
 224                     /* Scroll menu down */
 225                     scrollok (menu, TRUE);
 226                     wscrl (menu, -1);
 227                     scrollok (menu, FALSE);
 228 
 229                     scroll--;
 230 
 231                     print_item (menu, items[scroll * 2 + 1], 0, FALSE,
 232                                (items[scroll*2][0] != ':'));
 233                 } else
 234                     choice = MAX(choice - 1, 0);
 235 
 236             } else if (key == KEY_DOWN || key == '+')  {
 237 
 238                 print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
 239                                 (items[(scroll+choice)*2][0] != ':'));
 240 
 241                 if ((choice > 4) && (scroll + max_choice < item_no)) {
 242                     /* Scroll menu up */
 243                     scrollok (menu, TRUE);
 244                     scroll (menu);
 245                     scrollok (menu, FALSE);
 246 
 247                     scroll++;
 248 
 249                     print_item (menu, items[(scroll+max_choice-1)*2+1],
 250                                max_choice-1, FALSE,
 251                                (items[(scroll+max_choice-1)*2][0] != ':'));
 252                 } else
 253                     choice = MIN(choice+1, max_choice-1);
 254 
 255             } else if (key == KEY_PPAGE) {
 256                 scrollok (menu, TRUE);
 257                 for (i=0; (i < max_choice) && (scroll > 0); i++) {
 258                     wscrl (menu, -1);
 259                     scroll--;
 260                     print_item (menu, items[scroll * 2 + 1], 0, FALSE,
 261                                (items[scroll*2][0] != ':'));
 262                 }
 263                 scrollok (menu, FALSE);
 264                 choice = 0;
 265 
 266             } else if (key == KEY_NPAGE) {
 267                 scrollok (menu, TRUE);
 268                 for (i=0; (i < max_choice) && (scroll+max_choice < item_no); i++) {
 269                     scroll(menu);
 270                     scroll++;
 271                     print_item (menu, items[(scroll+max_choice-1)*2+1],
 272                                 max_choice-1, FALSE,
 273                                 (items[(scroll+max_choice-1)*2][0] != ':'));
 274                 }
 275                 scrollok (menu, FALSE);
 276                 choice = 0;
 277 
 278             } else
 279                 choice = i;
 280 
 281             print_item (menu, items[(scroll+choice)*2+1], choice, TRUE,
 282                        (items[(scroll+choice)*2][0] != ':'));
 283 
 284             print_arrows(dialog, item_no, scroll,
 285                          box_y, box_x+item_x+1, menu_height);
 286 
 287             wnoutrefresh (menu);
 288             wrefresh (dialog);
 289 
 290             continue;           /* wait for another key press */
 291         }
 292 
 293         switch (key) {
 294         case KEY_LEFT:
 295         case TAB:
 296         case KEY_RIGHT:
 297             button = ((key == KEY_LEFT ? --button : ++button) < 0)
 298                         ? 2 : (button > 2 ? 0 : button);
 299 
 300             print_buttons(dialog, height, width, button);
 301             wrefresh (dialog);
 302             break;
 303         case ' ':
 304         case 'S':
 305         case 's':
 306             delwin (dialog);
 307             fprintf(stderr, items[(scroll + choice) * 2]);
 308             return 0;
 309         case 'H':
 310         case 'h':
 311         case '?':
 312             button = 2;
 313         case '\n':
 314             delwin (dialog);
 315             if (button == 2) 
 316                 fprintf(stderr, "%s \"%s\"", 
 317                         items[(scroll + choice) * 2],
 318                         items[(scroll + choice) * 2 + 1] +
 319                         first_alpha(items[(scroll + choice) * 2 + 1]));
 320             else
 321                 fprintf(stderr, items[(scroll + choice) * 2]);
 322 
 323             return button;
 324         case 'E':
 325         case 'e':
 326         case 'X':
 327         case 'x':
 328             key = ESC;
 329         case ESC:
 330             break;
 331         }
 332     }
 333 
 334     delwin (dialog);
 335     return -1;                  /* ESC pressed */
 336 }

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