root/scripts/lxdialog/inputbox.c

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

DEFINITIONS

This source file includes following definitions.
  1. print_buttons
  2. dialog_inputbox

   1 /*
   2  *  inputbox.c -- implements the input box
   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 unsigned char dialog_input_result[MAX_LEN + 1];
  25 
  26 /*
  27  *  Print the termination buttons
  28  */
  29 static void
  30 print_buttons(WINDOW *dialog, int height, int width, int okval, int cancelval)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32     int x = width / 2 - 11;
  33     int y = height - 2;
  34 
  35     print_button (dialog, "  Ok  ", y, x, okval);
  36     print_button (dialog, " Help ", y, x + 14, cancelval);
  37 
  38     wrefresh(dialog);
  39 }
  40 
  41 /*
  42  * Display a dialog box for inputing a string
  43  */
  44 int
  45 dialog_inputbox (const char *title, const char *prompt, int height, int width,
     /* [previous][next][first][last][top][bottom][index][help] */
  46                  const char *init)
  47 {
  48     int i, x, y, box_y, box_x, box_width;
  49     int input_x = 0, scroll = 0, key = 0, button = -1;
  50     unsigned char *instr = dialog_input_result;
  51     WINDOW *dialog;
  52 
  53     /* center dialog box on screen */
  54     x = (COLS - width) / 2;
  55     y = (LINES - height) / 2;
  56 
  57 
  58     draw_shadow (stdscr, y, x, height, width);
  59 
  60     dialog = newwin (height, width, y, x);
  61     keypad (dialog, TRUE);
  62 
  63     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  64     wattrset (dialog, border_attr);
  65     mvwaddch (dialog, height-3, 0, ACS_LTEE);
  66     for (i = 0; i < width - 2; i++)
  67         waddch (dialog, ACS_HLINE);
  68     wattrset (dialog, dialog_attr);
  69     waddch (dialog, ACS_RTEE);
  70 
  71     if (title != NULL) {
  72         wattrset (dialog, title_attr);
  73         mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  74         waddstr (dialog, (char *)title);
  75         waddch (dialog, ' ');
  76     }
  77 
  78     wattrset (dialog, dialog_attr);
  79     print_autowrap (dialog, prompt, width - 2, 1, 3);
  80 
  81     /* Draw the input field box */
  82     box_width = width - 6;
  83     getyx (dialog, y, x);
  84     box_y = y + 2;
  85     box_x = (width - box_width) / 2;
  86     draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
  87               border_attr, dialog_attr);
  88 
  89     print_buttons(dialog, height, width, TRUE, FALSE);
  90 
  91     /* Set up the initial value */
  92     wmove (dialog, box_y, box_x);
  93     wattrset (dialog, inputbox_attr);
  94 
  95     if (!init)
  96         instr[0] = '\0';
  97     else
  98         strcpy (instr, init);
  99 
 100     input_x = strlen (instr);
 101 
 102     if (input_x >= box_width) {
 103         scroll = input_x - box_width + 1;
 104         input_x = box_width - 1;
 105         for (i = 0; i < box_width - 1; i++)
 106             waddch (dialog, instr[scroll + i]);
 107     } else
 108         waddstr (dialog, instr);
 109 
 110     wmove (dialog, box_y, box_x + input_x);
 111 
 112     wrefresh (dialog);
 113 
 114     while (key != ESC) {
 115         key = wgetch (dialog);
 116 
 117         if (button == -1) {     /* Input box selected */
 118             switch (key) {
 119             case TAB:
 120             case KEY_UP:
 121             case KEY_DOWN:
 122                 break;
 123             case KEY_LEFT:
 124                 continue;
 125             case KEY_RIGHT:
 126                 continue;
 127             case KEY_BACKSPACE:
 128             case 127:
 129                 if (input_x || scroll) {
 130                     wattrset (dialog, inputbox_attr);
 131                     if (!input_x) {
 132                         scroll = scroll < box_width - 1 ?
 133                             0 : scroll - (box_width - 1);
 134                         wmove (dialog, box_y, box_x);
 135                         for (i = 0; i < box_width; i++)
 136                             waddch (dialog, instr[scroll + input_x + i] ?
 137                                     instr[scroll + input_x + i] : ' ');
 138                         input_x = strlen (instr) - scroll;
 139                     } else
 140                         input_x--;
 141                     instr[scroll + input_x] = '\0';
 142                     mvwaddch (dialog, box_y, input_x + box_x, ' ');
 143                     wmove (dialog, box_y, input_x + box_x);
 144                     wrefresh (dialog);
 145                 }
 146                 continue;
 147             default:
 148                 if (key < 0x100 && isprint (key)) {
 149                     if (scroll + input_x < MAX_LEN) {
 150                         wattrset (dialog, inputbox_attr);
 151                         instr[scroll + input_x] = key;
 152                         instr[scroll + input_x + 1] = '\0';
 153                         if (input_x == box_width - 1) {
 154                             scroll++;
 155                             wmove (dialog, box_y, box_x);
 156                             for (i = 0; i < box_width - 1; i++)
 157                                 waddch (dialog, instr[scroll + i]);
 158                         } else {
 159                             wmove (dialog, box_y, input_x++ + box_x);
 160                             waddch (dialog, key);
 161                         }
 162                         wrefresh (dialog);
 163                     } else
 164                         flash ();       /* Alarm user about overflow */
 165                     continue;
 166                 }
 167             }
 168         }
 169         switch (key) {
 170         case 'O':
 171         case 'o':
 172             delwin (dialog);
 173             return 0;
 174         case 'H':
 175         case 'h':
 176             delwin (dialog);
 177             return 1;
 178         case KEY_UP:
 179         case KEY_LEFT:
 180             switch (button) {
 181             case -1:
 182                 button = 1;     /* Indicates "Cancel" button is selected */
 183                 print_buttons(dialog, height, width, FALSE, TRUE);
 184                 break;
 185             case 0:
 186                 button = -1;    /* Indicates input box is selected */
 187                 print_buttons(dialog, height, width, TRUE, FALSE);
 188                 wmove (dialog, box_y, box_x + input_x);
 189                 wrefresh (dialog);
 190                 break;
 191             case 1:
 192                 button = 0;     /* Indicates "OK" button is selected */
 193                 print_buttons(dialog, height, width, TRUE, FALSE);
 194                 break;
 195             }
 196             break;
 197         case TAB:
 198         case KEY_DOWN:
 199         case KEY_RIGHT:
 200             switch (button) {
 201             case -1:
 202                 button = 0;     /* Indicates "OK" button is selected */
 203                 print_buttons(dialog, height, width, TRUE, FALSE);
 204                 break;
 205             case 0:
 206                 button = 1;     /* Indicates "Cancel" button is selected */
 207                 print_buttons(dialog, height, width, FALSE, TRUE);
 208                 break;
 209             case 1:
 210                 button = -1;    /* Indicates input box is selected */
 211                 print_buttons(dialog, height, width, TRUE, FALSE);
 212                 wmove (dialog, box_y, box_x + input_x);
 213                 wrefresh (dialog);
 214                 break;
 215             }
 216             break;
 217         case ' ':
 218         case '\n':
 219             delwin (dialog);
 220             return (button == -1 ? 0 : button);
 221         case 'X':
 222         case 'x':
 223             key = ESC;
 224         case ESC:
 225             break;
 226         }
 227     }
 228 
 229     delwin (dialog);
 230     return -1;                  /* ESC pressed */
 231 }

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