home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / hdsetup / dialog-0.4-1 / dialog-0 / dialog-0.4-slackware / src / inputbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-09  |  6.2 KB  |  211 lines

  1. /*
  2.  *  inputbox.c -- implements the input box
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21.  
  22. #include "dialog.h"
  23.  
  24.  
  25. /*
  26.  * Display a dialog box for inputing a string
  27.  */
  28. int dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int width)
  29. {
  30.   int i, x, y, box_y, box_x, box_width,
  31.       input_x = 0, scroll = 0, key = 0, button = -1;
  32.   unsigned char instr[MAX_LEN+1];
  33.   WINDOW *dialog;
  34.  
  35.   /* center dialog box on screen */
  36.   x = (COLS - width)/2;
  37.   y = (LINES - height)/2;
  38.   
  39. #ifdef HAVE_NCURSES
  40.   if (use_shadow)
  41.     draw_shadow(stdscr, y, x, height, width);
  42. #endif
  43.   dialog = newwin(height, width, y, x);
  44.   keypad(dialog, TRUE);
  45.  
  46.   draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
  47.   wattrset(dialog, border_attr);
  48.   wmove(dialog, height-3, 0);
  49.   waddch(dialog, ACS_LTEE);
  50.   for (i = 0; i < width-2; i++)
  51.     waddch(dialog, ACS_HLINE);
  52.   waddch(dialog, ACS_RTEE);
  53.   wmove(dialog, height-2, 1);
  54.   for (i = 0; i < width-2; i++)
  55.     waddch(dialog, ' ');
  56.  
  57.   if (title != NULL) {
  58.     wattrset(dialog, title_attr);
  59.     wmove(dialog, 0, (width - strlen(title))/2 - 1);
  60.     waddch(dialog, ' ');
  61.     waddstr(dialog, title);
  62.     waddch(dialog, ' ');
  63.   }
  64.   wattrset(dialog, dialog_attr);
  65.   print_autowrap(dialog, prompt, width, 1, 3);
  66.  
  67.   /* Draw the input field box */
  68.   box_width = width-6;
  69.   getyx(dialog, y, x);
  70.   box_y = y + 2;
  71.   box_x = (width - box_width)/2;
  72.   draw_box(dialog, y+1, box_x-1, 3, box_width+2, inputbox_attr, inputbox_border_attr);
  73.  
  74.   x = width/2-11;
  75.   y = height-2;
  76.   print_button(dialog, "Cancel", y, x+14, FALSE);
  77.   print_button(dialog, "  OK  ", y, x, TRUE);
  78.  
  79.   instr[0] = '\0';
  80.   wmove(dialog, box_y, box_x);
  81.   wrefresh(dialog);
  82.   while (key != ESC) {
  83.     key = wgetch(dialog);
  84.  
  85.     if (button == -1) {    /* Input box selected */
  86.       switch (key) {
  87.         case TAB:
  88.         case KEY_UP:
  89.         case KEY_DOWN:
  90.           break;
  91.         case KEY_LEFT:
  92.           continue;
  93.         case KEY_RIGHT:
  94.           continue;
  95.         case KEY_BACKSPACE:
  96.           if (input_x || scroll) {
  97.             wattrset(dialog, inputbox_attr);
  98.             if (!input_x) {
  99.               scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
  100.               wmove(dialog, box_y, box_x);
  101.               for (i = 0; i < box_width; i++)
  102.                 waddch(dialog, instr[scroll+input_x+i] ? instr[scroll+input_x+i] : ' ');
  103.               input_x = strlen(instr) - scroll;
  104.             }
  105.             else
  106.               input_x--;
  107.             instr[scroll+input_x] = '\0';
  108.             wmove(dialog, box_y, input_x + box_x);
  109.             waddch(dialog, ' ');
  110.             wmove(dialog, box_y, input_x + box_x);
  111.             wrefresh(dialog);
  112.           }
  113.           continue;
  114.         default:
  115.           if (key < 0x100 && isprint(key)) {
  116.             if (scroll+input_x < MAX_LEN) {
  117.               wattrset(dialog, inputbox_attr);
  118.               instr[scroll+input_x] = key;
  119.               instr[scroll+input_x+1] = '\0';
  120.               if (input_x == box_width-1) {
  121.                 scroll++;
  122.                 wmove(dialog, box_y, box_x);
  123.                 for (i = 0; i < box_width-1; i++)
  124.                   waddch(dialog, instr[scroll+i]);
  125.               }
  126.               else {
  127.                 wmove(dialog, box_y, input_x++ + box_x);
  128.                 waddch(dialog, key);
  129.               }
  130.               wrefresh(dialog);
  131.             } else
  132.           flash(); /* Alarm user about overflow */
  133.             continue;
  134.           }
  135.       }
  136.     }
  137.  
  138.     switch (key) {
  139.       case 'O':
  140.       case 'o':
  141.         delwin(dialog);
  142.         fprintf(stderr, instr);
  143.         return 0;
  144.       case 'C':
  145.       case 'c':
  146.         delwin(dialog);
  147.         return 1;
  148.       case KEY_UP:
  149.       case KEY_LEFT:
  150.         switch (button) {
  151.       case -1:
  152.             button = 1;    /* Indicates "Cancel" button is selected */
  153.         print_button(dialog, "  OK  ", y, x, FALSE);
  154.         print_button(dialog, "Cancel", y, x+14, TRUE);
  155.             wrefresh(dialog);
  156.         break;
  157.           case 0:
  158.             button = -1;   /* Indicates input box is selected */
  159.         print_button(dialog, "Cancel", y, x+14, FALSE);
  160.         print_button(dialog, "  OK  ", y, x, TRUE);
  161.             wmove(dialog, box_y, box_x + input_x);
  162.             wrefresh(dialog);
  163.             break;
  164.           case 1:
  165.         button = 0;    /* Indicates "OK" button is selected */
  166.         print_button(dialog, "Cancel", y, x+14, FALSE);
  167.         print_button(dialog, "  OK  ", y, x, TRUE);
  168.             wrefresh(dialog);
  169.             break;
  170.         }
  171.         break;
  172.       case TAB:
  173.       case KEY_DOWN:
  174.       case KEY_RIGHT:
  175.         switch (button) {
  176.       case -1:
  177.         button = 0;    /* Indicates "OK" button is selected */
  178.         print_button(dialog, "Cancel", y, x+14, FALSE);
  179.         print_button(dialog, "  OK  ", y, x, TRUE);
  180.             wrefresh(dialog);
  181.             break;
  182.           case 0:
  183.             button = 1;    /* Indicates "Cancel" button is selected */
  184.         print_button(dialog, "  OK  ", y, x, FALSE);
  185.         print_button(dialog, "Cancel", y, x+14, TRUE);
  186.             wrefresh(dialog);
  187.         break;
  188.           case 1:
  189.             button = -1;   /* Indicates input box is selected */
  190.         print_button(dialog, "Cancel", y, x+14, FALSE);
  191.         print_button(dialog, "  OK  ", y, x, TRUE);
  192.             wmove(dialog, box_y, box_x + input_x);
  193.             wrefresh(dialog);
  194.             break;
  195.         }
  196.         break;
  197.       case ' ':
  198.       case '\n':
  199.         delwin(dialog);
  200.         fprintf(stderr, instr);
  201.         return (button == -1 ? 0 : button);
  202.       case ESC:
  203.         break;
  204.     }
  205.   }
  206.  
  207.   delwin(dialog);
  208.   return -1;    /* ESC pressed */
  209. }
  210. /* End of dialog_inputbox() */
  211.