home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume7 / my_wgetstr / example.c next >
Encoding:
C/C++ Source or Header  |  1989-06-03  |  2.2 KB  |  103 lines

  1. /*
  2.  * Example program --
  3.  * A possible replacement for curses' wgetstr(). Allows
  4.  * limited editing. Recognizes the erase and kill chars.
  5.  *
  6.  * By Hung Le (mott@ucscb.ucsc.edu  ...!ucbvax!ucscc!ucscb!mott)
  7.  * History:
  8.  *    01/18/89 - Initial version 
  9.  *    03/20/89 - add control-w to delete word
  10.  *
  11.  * Release to Public Domain 
  12.  */
  13.  
  14. #include <curses.h>
  15. #include <signal.h>
  16. /* REMEMBER to include "my_wgetstr.h" */
  17. #include "my_wgetstr.h"
  18.  
  19. /* test my_wgetstr() */
  20. main()
  21. {
  22.     char buffer[30];
  23.     static char *header[] =
  24.     {
  25.      "INPUT WINDOW",
  26.      "Look Ma ... Line editing in curses",
  27.      "Erase char to erase, Kill char to delete the whole line",
  28.      "control-w to delete a word, empty input line to quit",
  29.     };
  30.     char ender[81], *name, *getlogin(), *getenv();
  31.     int clean_up(), n;
  32.     int x_pos, y_pos;
  33.     WINDOW *inwin;
  34.  
  35.     initscr();
  36.     clear();
  37.     refresh();
  38.  
  39.     /* set up interupt handler */
  40.     signal(SIGINT, clean_up);
  41.  
  42.     /* set up input window */
  43.     inwin = newwin(8, COLS, 3, 0);
  44.     if (inwin == (WINDOW *) NULL)
  45.         clean_up();
  46.  
  47.     /* print out header */
  48.     box(inwin, '|', '-');
  49.     for (n = 0; n < 4; n++)
  50.         mvwaddstr(inwin, n + 1, COLS / 2 - strlen(header[n]) / 2, header[n]);
  51.     mvwaddstr(inwin, 6, 15, "Enter input --> ");
  52.     /* get the prompt positions so we can return at later time */
  53.     getyx(inwin, y_pos, x_pos);
  54.     wrefresh(inwin);
  55.  
  56.     /* now get string */
  57.     do
  58.     {
  59.         /* clear last input */
  60.         wmove(inwin, y_pos, x_pos);
  61.         wclrtoeol(inwin);
  62.         box(inwin, '|', '-');
  63.         wrefresh(inwin);
  64.  
  65.         /*
  66.          * get string. NO_RET=0 and RET=1 are defined in
  67.          * "my_wgetstr.h" 
  68.          */
  69.         n = my_wgetstr(inwin, buffer, sizeof(buffer), NO_RET);
  70.  
  71.         /* print out the result of my_wgetstr */
  72.         move(15, 15);
  73.         clrtoeol();
  74.         mvprintw(15, 15, "Received %d chars -- \"%s\"", n, buffer);
  75.         refresh();
  76.  
  77.         /* move back to the input prompt */
  78.         wmove(inwin, y_pos, x_pos);
  79.         wrefresh(inwin);
  80.     }
  81.     while (n != 0);        /* while input is not empty */
  82.  
  83.     name = getenv("NAME");
  84.     if (name == (char *) NULL)    /* use login if NAME is not set */
  85.         name = getlogin();
  86.     /* say good bye */
  87.     move(15, 15);
  88.     clrtoeol();
  89.     sprintf(ender, "GOOD BYE, \"%s\"", name);
  90.     mvprintw(15, COLS / 2 - strlen(ender) / 2, "%s", ender);
  91.  
  92.     /* move to last line ... looks cleaner this way */
  93.     move(LINES - 1, 0);
  94.     refresh();
  95.     clean_up();
  96. }
  97.  
  98. clean_up()
  99. {
  100.     endwin();
  101.     exit();
  102. }
  103.