home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / edit / jade / src / x11_commandline.c < prev    next >
C/C++ Source or Header  |  1994-06-29  |  4KB  |  181 lines

  1. /* x11_commandline.c -- Simple prompt for X11
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    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 Jade; see the file COPYING.    If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22.  
  23. #include <string.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/keysym.h>
  26.  
  27. _PR u_char *sys_prompt(VW *, u_char *, u_char *);
  28.  
  29. /*
  30.  * returns a string got from the command line
  31.  * it (the string) must be valstrfree()'ed after use.
  32.  * cursor should be off.
  33.  */
  34. u_char *
  35. sys_prompt(VW *vw, u_char *prompt, u_char *original)
  36. {
  37.     u_char *cmdline;
  38.     int cmdlen;
  39.     int promptlen = strlen(prompt);
  40.     if(!sys_unsleep_vw(vw))
  41.     return(FALSE);
  42.     cmdline = str_dup(original ? original : (u_char *)"");
  43.     if(cmdline)
  44.     {
  45.     POS oldcursor = vw->vw_CursorPos;
  46.     long oldstartcol = vw->vw_StartCol;
  47.     long prevx;
  48.     char keepgoing = 0;
  49.  
  50.     cmdlen = strlen(cmdline) + 1;
  51.     vw->vw_CursorPos.pos_Col = promptlen + cmdlen - 1;
  52.     vw->vw_CursorPos.pos_Line = vw->vw_StartCol + vw->vw_MaxY - 1;
  53.     prevx = vw->vw_CursorPos.pos_Col;
  54.     if(vw->vw_CursorPos.pos_Col >= vw->vw_MaxX)
  55.         vw->vw_StartCol = vw->vw_CursorPos.pos_Col - vw->vw_MaxX + 1;
  56.     else
  57.         vw->vw_StartCol = 0;
  58.  
  59.     redraw_cmd_line(vw, prompt, cmdline, promptlen, cmdlen - 1);
  60.     cmd_line_cursor(vw, CURS_ON);
  61.  
  62.     while(!keepgoing)
  63.     {
  64.         XEvent ev;
  65.         XWindowEvent(x11_display, vw->vw_Window,
  66.              KeyPressMask | ExposureMask | StructureNotifyMask,
  67.              &ev);
  68.         switch(ev.type)
  69.         {
  70.         KeySym ks;
  71.         u_char buf[256];
  72.         int len;
  73.         case MappingNotify:
  74.         XRefreshKeyboardMapping(&ev.xmapping);
  75.         continue;
  76.         case KeyPress:
  77.         cmd_line_cursor(vw, CURS_OFF);
  78.         len = XLookupString(&ev.xkey, buf, 255, &ks, NULL);
  79.         buf[len] = 0;
  80.         switch(ks)
  81.         {
  82.         case XK_Delete:
  83.             if(cmdlen > 1)
  84.             {
  85.             u_char *newline;
  86.             vw->vw_CursorPos.pos_Col--;
  87.             cmdlen--;
  88.             newline = str_dupn(cmdline, cmdlen - 1);
  89.             if(newline)
  90.             {
  91.                 str_free(cmdline);
  92.                 cmdline = newline;
  93.             }
  94.             }
  95.             break;
  96.         case XK_Return:
  97.             keepgoing = 1;
  98.             break;
  99.         case XK_Escape:
  100.             keepgoing = -1;
  101.             break;
  102.         case XK_Up:
  103.         case XK_Down:
  104.             if(last_prompted)
  105.             {
  106.             u_char *temp = cmdline;
  107.             cmdline = last_prompted;
  108.             last_prompted = temp;
  109.             cmdlen = strlen(cmdline) + 1;
  110.             vw->vw_CursorPos.pos_Col = cmdlen + promptlen - 1;
  111.             prevx = 20000;
  112.             }
  113.             break;
  114.         default:
  115.             if(len)
  116.             {
  117.             u_char *newline = str_dupn(cmdline, cmdlen + len);
  118.             if(newline)
  119.             {
  120.                 strcat(newline, buf);
  121.                 str_free(cmdline);
  122.                 cmdline = newline;
  123.                 cmdlen += len;
  124.                 vw->vw_CursorPos.pos_Col += len;
  125.             }
  126.             }
  127.             break;
  128.         }
  129.         break;
  130.         case ConfigureNotify:
  131.         x11_update_dimensions(vw, ev.xconfigure.width,
  132.                   ev.xconfigure.height);
  133.         continue;
  134.         case Expose:
  135.         vw->vw_Flags |= VWFF_FORCE_REFRESH;
  136.         draw_message_line(vw);
  137.         prevx = 20000;
  138.         cmd_line_cursor(vw, CURS_OFF);
  139.         break;
  140.         default:
  141.         continue;
  142.         }
  143.         if(vw->vw_CursorPos.pos_Col >= vw->vw_MaxX)
  144.         {
  145.         vw->vw_StartCol = vw->vw_CursorPos.pos_Col - vw->vw_MaxX + 1;
  146.         redraw_cmd_line(vw, prompt, cmdline, promptlen, cmdlen - 1);
  147.         }
  148.         else
  149.         {
  150.         vw->vw_StartCol = 0;
  151.         if(prevx >= vw->vw_MaxX)
  152.             redraw_cmd_line(vw, prompt, cmdline, promptlen,
  153.                     cmdlen - 1);
  154.         else if(prevx < vw->vw_CursorPos.pos_Col)
  155.             redraw_cmd_line_from(vw, prompt, cmdline, promptlen,
  156.                      cmdlen - 1, prevx);
  157.         else
  158.             redraw_cmd_line_from(vw, prompt, cmdline, promptlen,
  159.                      cmdlen - 1, vw->vw_CursorPos.pos_Col);
  160.         }
  161.         prevx = vw->vw_CursorPos.pos_Col;
  162.         cmd_line_cursor(vw, CURS_ON);
  163.     }
  164.     if(keepgoing < 0)
  165.     {
  166.         str_free(cmdline);
  167.         cmdline = NULL;
  168.     }
  169.     else
  170.     {
  171.         str_free(last_prompted);
  172.         last_prompted = str_dup(cmdline);
  173.     }
  174.     cmd_line_cursor(vw, CURS_OFF);
  175.     vw->vw_CursorPos = oldcursor;
  176.     vw->vw_StartCol = oldstartcol;
  177.     return(cmdline);
  178.     }
  179.     return(FALSE);
  180. }
  181.