Back to index

tetex-bin  3.0
inputbox.c
Go to the documentation of this file.
00001 /*
00002  *  inputbox.c -- implements the input box
00003  *
00004  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 
00022 #include "dialog.h"
00023 
00024 
00025 /*
00026  * Display a dialog box for inputing a string
00027  */
00028 int dialog_inputbox(char *title, char *prompt, int height, int width,
00029                   char *init)
00030 {
00031   int i, x, y, box_y, box_x, box_width,
00032       input_x = 0, scroll = 0, key = 0, button = -1;
00033   unsigned char instr[MAX_LEN+1];
00034   WINDOW *dialog;
00035 
00036   /* center dialog box on screen */
00037   x = (COLS - width)/2;
00038   y = (LINES - height)/2;
00039   
00040 #ifdef HAVE_NCURSES
00041   if (use_shadow)
00042     draw_shadow(stdscr, y, x, height, width);
00043 #endif
00044   dialog = newwin(height, width, y, x);
00045   keypad(dialog, TRUE);
00046 
00047   draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
00048   wattrset(dialog, border_attr);
00049   wmove(dialog, height-3, 0);
00050   waddch(dialog, ACS_LTEE);
00051   for (i = 0; i < width-2; i++)
00052     waddch(dialog, ACS_HLINE);
00053   wattrset(dialog, dialog_attr);
00054   waddch(dialog, ACS_RTEE);
00055   wmove(dialog, height-2, 1);
00056   for (i = 0; i < width-2; i++)
00057     waddch(dialog, ' ');
00058 
00059   if (title != NULL) {
00060     wattrset(dialog, title_attr);
00061     wmove(dialog, 0, (width - strlen(title))/2 - 1);
00062     waddch(dialog, ' ');
00063     waddstr(dialog, title);
00064     waddch(dialog, ' ');
00065   }
00066   wattrset(dialog, dialog_attr);
00067   print_autowrap(dialog, prompt, width, 1, 3);
00068 
00069   /* Draw the input field box */
00070   box_width = width-6;
00071   getyx(dialog, y, x);
00072   box_y = y + 2;
00073   box_x = (width - box_width)/2;
00074   draw_box(dialog, y+1, box_x-1, 3, box_width+2, border_attr, dialog_attr);
00075 
00076   x = width/2-11;
00077   y = height-2;
00078   print_button(dialog, "Cancel", y, x+14, FALSE);
00079   print_button(dialog, "  OK  ", y, x, TRUE);
00080 
00081   /* Set up the initial value */
00082   wmove(dialog, box_y, box_x);
00083   wattrset(dialog, inputbox_attr);
00084   if (!init) {
00085       instr[0] = '\0';
00086   } else {
00087       strcpy((char*)instr, init);
00088   }
00089   input_x = strlen((char*)instr);
00090   if (input_x >= box_width) {
00091       scroll = input_x - box_width + 1;
00092       input_x = box_width - 1;
00093       for (i = 0; i < box_width-1; i++) {
00094          waddch(dialog, instr[scroll+i]);
00095       }
00096   } else {
00097       waddstr(dialog, (char*)instr);
00098   }
00099   wmove(dialog, box_y, box_x + input_x);
00100   
00101   wrefresh(dialog);
00102   while (key != ESC) {
00103     key = wgetch(dialog);
00104 
00105     if (button == -1) {    /* Input box selected */
00106       switch (key) {
00107         case TAB:
00108         case KEY_UP:
00109         case KEY_DOWN:
00110           break;
00111         case KEY_LEFT:
00112           continue;
00113         case KEY_RIGHT:
00114           continue;
00115         case KEY_BACKSPACE:
00116           if (input_x || scroll) {
00117             wattrset(dialog, inputbox_attr);
00118             if (!input_x) {
00119               scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
00120               wmove(dialog, box_y, box_x);
00121               for (i = 0; i < box_width; i++)
00122                 waddch(dialog, instr[scroll+input_x+i] ? instr[scroll+input_x+i] : ' ');
00123               input_x = strlen((char*)instr) - scroll;
00124             }
00125             else
00126               input_x--;
00127             instr[scroll+input_x] = '\0';
00128             wmove(dialog, box_y, input_x + box_x);
00129             waddch(dialog, ' ');
00130             wmove(dialog, box_y, input_x + box_x);
00131             wrefresh(dialog);
00132           }
00133           continue;
00134         default:
00135           if (key < 0x100 && isprint(key)) {
00136             if (scroll+input_x < MAX_LEN) {
00137               wattrset(dialog, inputbox_attr);
00138               instr[scroll+input_x] = key;
00139               instr[scroll+input_x+1] = '\0';
00140               if (input_x == box_width-1) {
00141                 scroll++;
00142                 wmove(dialog, box_y, box_x);
00143                 for (i = 0; i < box_width-1; i++)
00144                   waddch(dialog, instr[scroll+i]);
00145               }
00146               else {
00147                 wmove(dialog, box_y, input_x++ + box_x);
00148                 waddch(dialog, key);
00149               }
00150               wrefresh(dialog);
00151             } else
00152              flash(); /* Alarm user about overflow */
00153             continue;
00154           }
00155       }
00156     }
00157 
00158     switch (key) {
00159       case 'O':
00160       case 'o':
00161         delwin(dialog);
00162         fprintf(stderr, (char*)instr);
00163         return 0;
00164       case 'C':
00165       case 'c':
00166         delwin(dialog);
00167         return 1;
00168       case KEY_UP:
00169       case KEY_LEFT:
00170         switch (button) {
00171          case -1:
00172             button = 1;    /* Indicates "Cancel" button is selected */
00173            print_button(dialog, "  OK  ", y, x, FALSE);
00174            print_button(dialog, "Cancel", y, x+14, TRUE);
00175             wrefresh(dialog);
00176            break;
00177           case 0:
00178             button = -1;   /* Indicates input box is selected */
00179            print_button(dialog, "Cancel", y, x+14, FALSE);
00180            print_button(dialog, "  OK  ", y, x, TRUE);
00181             wmove(dialog, box_y, box_x + input_x);
00182             wrefresh(dialog);
00183             break;
00184           case 1:
00185            button = 0;    /* Indicates "OK" button is selected */
00186            print_button(dialog, "Cancel", y, x+14, FALSE);
00187            print_button(dialog, "  OK  ", y, x, TRUE);
00188             wrefresh(dialog);
00189             break;
00190         }
00191         break;
00192       case TAB:
00193       case KEY_DOWN:
00194       case KEY_RIGHT:
00195         switch (button) {
00196          case -1:
00197            button = 0;    /* Indicates "OK" button is selected */
00198            print_button(dialog, "Cancel", y, x+14, FALSE);
00199            print_button(dialog, "  OK  ", y, x, TRUE);
00200             wrefresh(dialog);
00201             break;
00202           case 0:
00203             button = 1;    /* Indicates "Cancel" button is selected */
00204            print_button(dialog, "  OK  ", y, x, FALSE);
00205            print_button(dialog, "Cancel", y, x+14, TRUE);
00206             wrefresh(dialog);
00207            break;
00208           case 1:
00209             button = -1;   /* Indicates input box is selected */
00210            print_button(dialog, "Cancel", y, x+14, FALSE);
00211            print_button(dialog, "  OK  ", y, x, TRUE);
00212             wmove(dialog, box_y, box_x + input_x);
00213             wrefresh(dialog);
00214             break;
00215         }
00216         break;
00217       case ' ':
00218       case '\n':
00219         delwin(dialog);
00220         fprintf(stderr, (char*)instr);
00221         return (button == -1 ? 0 : button);
00222       case ESC:
00223         break;
00224     }
00225   }
00226 
00227   delwin(dialog);
00228   return -1;    /* ESC pressed */
00229 }
00230 /* End of dialog_inputbox() */