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