home *** CD-ROM | disk | FTP | other *** search
- /* x11_commandline.c -- Simple prompt for X11
- Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
-
- This file is part of Jade.
-
- Jade is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
-
- Jade is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Jade; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- #include "jade.h"
- #include "jade_protos.h"
-
- #include <string.h>
- #include <X11/Xutil.h>
- #include <X11/keysym.h>
-
- _PR u_char *docmdline(VW *, u_char *, u_char *);
-
- /*
- * returns a string got from the command line
- * it (the string) must be valstrfree()'ed after use.
- * cursor should be off.
- */
- u_char *
- docmdline(VW *vw, u_char *prompt, u_char *original)
- {
- u_char *cmdline;
- int cmdlen;
- int promptlen = strlen(prompt);
- if(!unsleep(vw))
- return(FALSE);
- cmdline = mystrdup(original ? original : (u_char *)"");
- if(cmdline)
- {
- POS oldcursor = vw->vw_CursorPos;
- long oldstartcol = vw->vw_StartCol;
- long prevx;
- char keepgoing = 0;
-
- cmdlen = strlen(cmdline) + 1;
- vw->vw_CursorPos.pos_Col = promptlen + cmdlen - 1;
- vw->vw_CursorPos.pos_Line = vw->vw_StartCol + vw->vw_MaxY - 1;
- prevx = vw->vw_CursorPos.pos_Col;
- if(vw->vw_CursorPos.pos_Col >= vw->vw_MaxX)
- vw->vw_StartCol = vw->vw_CursorPos.pos_Col - vw->vw_MaxX + 1;
- else
- vw->vw_StartCol = 0;
-
- redrawcmdline(vw, prompt, cmdline, promptlen, cmdlen - 1);
- cmdlncursor(vw, CURS_ON);
-
- while(!keepgoing)
- {
- XEvent ev;
- XWindowEvent(XDisplay, vw->vw_Window,
- KeyPressMask | ExposureMask | StructureNotifyMask,
- &ev);
- switch(ev.type)
- {
- KeySym ks;
- u_char buf[256];
- int len;
- case MappingNotify:
- XRefreshKeyboardMapping(&ev.xmapping);
- continue;
- case KeyPress:
- cmdlncursor(vw, CURS_OFF);
- len = XLookupString(&ev.xkey, buf, 255, &ks, NULL);
- buf[len] = 0;
- switch(ks)
- {
- case XK_Delete:
- if(cmdlen > 1)
- {
- u_char *newline;
- vw->vw_CursorPos.pos_Col--;
- cmdlen--;
- newline = mystrdupn(cmdline, cmdlen - 1);
- if(newline)
- {
- mystrfree(cmdline);
- cmdline = newline;
- }
- }
- break;
- case XK_Return:
- keepgoing = 1;
- break;
- case XK_Escape:
- keepgoing = -1;
- break;
- case XK_Up:
- case XK_Down:
- if(LastPrompted)
- {
- u_char *temp = cmdline;
- cmdline = LastPrompted;
- LastPrompted = temp;
- cmdlen = strlen(cmdline) + 1;
- vw->vw_CursorPos.pos_Col = cmdlen + promptlen - 1;
- prevx = 20000;
- }
- break;
- default:
- if(len)
- {
- u_char *newline = mystrdupn(cmdline, cmdlen + len);
- if(newline)
- {
- strcat(newline, buf);
- mystrfree(cmdline);
- cmdline = newline;
- cmdlen += len;
- vw->vw_CursorPos.pos_Col += len;
- }
- }
- break;
- }
- break;
- case ConfigureNotify:
- _updatedimensions(vw, ev.xconfigure.width, ev.xconfigure.height);
- continue;
- case Expose:
- vw->vw_Flags |= VWFF_FORCE_REFRESH;
- drawmsgsepline(vw);
- prevx = 20000;
- cmdlncursor(vw, CURS_OFF);
- break;
- default:
- continue;
- }
- if(vw->vw_CursorPos.pos_Col >= vw->vw_MaxX)
- {
- vw->vw_StartCol = vw->vw_CursorPos.pos_Col - vw->vw_MaxX + 1;
- redrawcmdline(vw, prompt, cmdline, promptlen, cmdlen - 1);
- }
- else
- {
- vw->vw_StartCol = 0;
- if(prevx >= vw->vw_MaxX)
- redrawcmdline(vw, prompt, cmdline, promptlen, cmdlen - 1);
- else if(prevx < vw->vw_CursorPos.pos_Col)
- redrawcmdlinefrom(vw, prompt, cmdline, promptlen, cmdlen - 1, prevx);
- else
- redrawcmdlinefrom(vw, prompt, cmdline, promptlen, cmdlen - 1, vw->vw_CursorPos.pos_Col);
- }
- prevx = vw->vw_CursorPos.pos_Col;
- cmdlncursor(vw, CURS_ON);
- }
- if(keepgoing < 0)
- {
- mystrfree(cmdline);
- cmdline = NULL;
- }
- else
- {
- mystrfree(LastPrompted);
- LastPrompted = mystrdup(cmdline);
- }
- cmdlncursor(vw, CURS_OFF);
- vw->vw_CursorPos = oldcursor;
- vw->vw_StartCol = oldstartcol;
- /* restorecmdline(vw); */
- return(cmdline);
- }
- return(FALSE);
- }
-