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-3d < prev    next >
Encoding:
Text File  |  1994-06-09  |  6.2 KB  |  212 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.   wattrset(dialog, dialog_attr);
  53.   waddch(dialog, ACS_RTEE);
  54.   wmove(dialog, height-2, 1);
  55.   for (i = 0; i < width-2; i++)
  56.     waddch(dialog, ' ');
  57.  
  58.   if (title != NULL) {
  59.     wattrset(dialog, title_attr);
  60.     wmove(dialog, 0, (width - strlen(title))/2 - 1);
  61.     waddch(dialog, ' ');
  62.     waddstr(dialog, title);
  63.     waddch(dialog, ' ');
  64.   }
  65.   wattrset(dialog, dialog_attr);
  66.   print_autowrap(dialog, prompt, width, 1, 3);
  67.  
  68.   /* Draw the input field box */
  69.   box_width = width-6;
  70.   getyx(dialog, y, x);
  71.   box_y = y + 2;
  72.   box_x = (width - box_width)/2;
  73.   draw_box(dialog, y+1, box_x-1, 3, box_width+2, border_attr, dialog_attr);
  74.  
  75.   x = width/2-11;
  76.   y = height-2;
  77.   print_button(dialog, "Cancel", y, x+14, FALSE);
  78.   print_button(dialog, "  OK  ", y, x, TRUE);
  79.  
  80.   instr[0] = '\0';
  81.   wmove(dialog, box_y, box_x);
  82.   wrefresh(dialog);
  83.   while (key != ESC) {
  84.     key = wgetch(dialog);
  85.  
  86.     if (button == -1) {    /* Input box selected */
  87.       switch (key) {
  88.         case TAB:
  89.         case KEY_UP:
  90.         case KEY_DOWN:
  91.           break;
  92.         case KEY_LEFT:
  93.           continue;
  94.         case KEY_RIGHT:
  95.           continue;
  96.         case KEY_BACKSPACE:
  97.           if (input_x || scroll) {
  98.             wattrset(dialog, inputbox_attr);
  99.             if (!input_x) {
  100.               scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
  101.               wmove(dialog, box_y, box_x);
  102.               for (i = 0; i < box_width; i++)
  103.                 waddch(dialog, instr[scroll+input_x+i] ? instr[scroll+input_x+i] : ' ');
  104.               input_x = strlen(instr) - scroll;
  105.             }
  106.             else
  107.               input_x--;
  108.             instr[scroll+input_x] = '\0';
  109.             wmove(dialog, box_y, input_x + box_x);
  110.             waddch(dialog, ' ');
  111.             wmove(dialog, box_y, input_x + box_x);
  112.             wrefresh(dialog);
  113.           }
  114.           continue;
  115.         default:
  116.           if (key < 0x100 && isprint(key)) {
  117.             if (scroll+input_x < MAX_LEN) {
  118.               wattrset(dialog, inputbox_attr);
  119.               instr[scroll+input_x] = key;
  120.               instr[scroll+input_x+1] = '\0';
  121.               if (input_x == box_width-1) {
  122.                 scroll++;
  123.                 wmove(dialog, box_y, box_x);
  124.                 for (i = 0; i < box_width-1; i++)
  125.                   waddch(dialog, instr[scroll+i]);
  126.               }
  127.               else {
  128.                 wmove(dialog, box_y, input_x++ + box_x);
  129.                 waddch(dialog, key);
  130.               }
  131.               wrefresh(dialog);
  132.             } else
  133.           flash(); /* Alarm user about overflow */
  134.             continue;
  135.           }
  136.       }
  137.     }
  138.  
  139.     switch (key) {
  140.       case 'O':
  141.       case 'o':
  142.         delwin(dialog);
  143.         fprintf(stderr, instr);
  144.         return 0;
  145.       case 'C':
  146.       case 'c':
  147.         delwin(dialog);
  148.         return 1;
  149.       case KEY_UP:
  150.       case KEY_LEFT:
  151.         switch (button) {
  152.       case -1:
  153.             button = 1;    /* Indicates "Cancel" button is selected */
  154.         print_button(dialog, "  OK  ", y, x, FALSE);
  155.         print_button(dialog, "Cancel", y, x+14, TRUE);
  156.             wrefresh(dialog);
  157.         break;
  158.           case 0:
  159.             button = -1;   /* Indicates input box is selected */
  160.         print_button(dialog, "Cancel", y, x+14, FALSE);
  161.         print_button(dialog, "  OK  ", y, x, TRUE);
  162.             wmove(dialog, box_y, box_x + input_x);
  163.             wrefresh(dialog);
  164.             break;
  165.           case 1:
  166.         button = 0;    /* Indicates "OK" button is selected */
  167.         print_button(dialog, "Cancel", y, x+14, FALSE);
  168.         print_button(dialog, "  OK  ", y, x, TRUE);
  169.             wrefresh(dialog);
  170.             break;
  171.         }
  172.         break;
  173.       case TAB:
  174.       case KEY_DOWN:
  175.       case KEY_RIGHT:
  176.         switch (button) {
  177.       case -1:
  178.         button = 0;    /* Indicates "OK" button is selected */
  179.         print_button(dialog, "Cancel", y, x+14, FALSE);
  180.         print_button(dialog, "  OK  ", y, x, TRUE);
  181.             wrefresh(dialog);
  182.             break;
  183.           case 0:
  184.             button = 1;    /* Indicates "Cancel" button is selected */
  185.         print_button(dialog, "  OK  ", y, x, FALSE);
  186.         print_button(dialog, "Cancel", y, x+14, TRUE);
  187.             wrefresh(dialog);
  188.         break;
  189.           case 1:
  190.             button = -1;   /* Indicates input box is selected */
  191.         print_button(dialog, "Cancel", y, x+14, FALSE);
  192.         print_button(dialog, "  OK  ", y, x, TRUE);
  193.             wmove(dialog, box_y, box_x + input_x);
  194.             wrefresh(dialog);
  195.             break;
  196.         }
  197.         break;
  198.       case ' ':
  199.       case '\n':
  200.         delwin(dialog);
  201.         fprintf(stderr, instr);
  202.         return (button == -1 ? 0 : button);
  203.       case ESC:
  204.         break;
  205.     }
  206.   }
  207.  
  208.   delwin(dialog);
  209.   return -1;    /* ESC pressed */
  210. }
  211. /* End of dialog_inputbox() */
  212.