13754Sache/*
28344Sache *  Changes Copyright (C) 1995 by Andrey A. Chernov, Moscow
33754Sache *
48344Sache *  Original Copyright:
58344Sache *
63754Sache *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
73754Sache *
83754Sache *  This program is free software; you can redistribute it and/or
93754Sache *  modify it under the terms of the GNU General Public License
103754Sache *  as published by the Free Software Foundation; either version 2
113754Sache *  of the License, or (at your option) any later version.
123754Sache *
133754Sache *  This program is distributed in the hope that it will be useful,
143754Sache *  but WITHOUT ANY WARRANTY; without even the implied warranty of
153754Sache *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
163754Sache *  GNU General Public License for more details.
173754Sache *
183754Sache *  You should have received a copy of the GNU General Public License
193754Sache *  along with this program; if not, write to the Free Software
203754Sache *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
213754Sache */
223754Sache
233754Sache
243754Sache#include <dialog.h>
253754Sache#include "dialog.priv.h"
263754Sache
2720442Sjkhstatic void redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask);
283754Sache
293754Sache/*
303754Sache * Line editor
313754Sache */
3220442Sjkhint line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, chtype attr, int first, unsigned char *result, int attr_mask)
333754Sache{
348344Sache  int i, key;
354582Sache  chtype old_attr;
363754Sache  static int input_x, scroll;
373754Sache  static unsigned char instr[MAX_LEN+1];
384344Sache  unsigned char erase_char = erasechar();
394344Sache  unsigned char kill_char = killchar();
404344Sache#ifdef notyet
414344Sache  unsignec char werase_char = cur_term->Ottyb.c_cc[VWERASE];
424344Sache#endif
433754Sache
444582Sache  old_attr = getattrs(dialog);
454344Sache  keypad(dialog, TRUE);
464071Sache
473754Sache  if (first) {
483754Sache    memset(instr, 0, sizeof(instr));
494071Sache    strcpy(instr, result);
504071Sache    i = strlen(instr);
516458Sache/*    input_x = i % box_width;*/
526458Sache    input_x = (i > box_width) ? box_width - 1 : i;
536458Sache/*    scroll = i - input_x;*/
546458Sache    scroll = (i > box_width) ? i - box_width + 1: 0;
553754Sache  }
5620442Sjkh  redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
573754Sache
583754Sache  for (;;) {
594666Sache    wattrset(dialog, attr);
604584Sache    wrefresh(dialog);
613754Sache    key = wgetch(dialog);
623754Sache    switch (key) {
636458Sache      case ctrl('q'):
646458Sache	goto ret;
656458Sache	break;
666458Sache      case KEY_F(1):
676458Sache	display_helpfile();
686458Sache	break;
693754Sache      case TAB:
703754Sache      case KEY_BTAB:
713754Sache      case KEY_UP:
723754Sache      case KEY_DOWN:
733754Sache      case ESC:
743950Sache      case '\r':
753754Sache      case '\n':
763754Sache	for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
773754Sache	  instr[i] = '\0';
783950Sache	if (key == '\r')
793950Sache	  key = '\n';
803754Sache	goto ret;
814344Sache      case '\025':
824688Sache      case '\030':
834344Sache      kill_it:
848344Sache	input_x = scroll = 0;
858344Sache	/* fall through */
868344Sache      case '\013':
878344Sache      case KEY_EOL:
888344Sache	memset(instr + scroll + input_x, '\0', sizeof(instr) - scroll - input_x);
8920442Sjkh	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
908344Sache	continue;
914688Sache      case '\001':
923754Sache      case KEY_HOME:
933754Sache	input_x = scroll = 0;
9420442Sjkh	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
953754Sache	continue;
964688Sache      case '\005':
973754Sache      case KEY_END:
983754Sache	for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
993754Sache	  instr[i] = '\0';
1003754Sache	i++;
1013754Sache	input_x = i % box_width;
1023754Sache	scroll = i - input_x;
10320442Sjkh	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
1043754Sache	continue;
1054688Sache      case '\002':
1063754Sache      case KEY_LEFT:
1073754Sache	if (input_x || scroll) {
1083754Sache	  if (!input_x) {
1093754Sache	    int oldscroll = scroll;
1103754Sache	    scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
1113754Sache	    input_x = oldscroll - 1 - scroll;
11220442Sjkh	    redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
1138344Sache	  } else {
1148344Sache	    input_x--;
1158344Sache	    wmove(dialog, box_y, input_x + box_x);
1163754Sache	  }
1174688Sache	} else
1184688Sache	  beep();
1193754Sache	continue;
1204688Sache      case '\006':
1213754Sache      case KEY_RIGHT:
1224584Sache	  if (   scroll+input_x < MAX_LEN
1234584Sache	      && (flen < 0 || scroll+input_x < flen)
1244584Sache	     ) {
1253754Sache	    if (!instr[scroll+input_x])
1263754Sache	      instr[scroll+input_x] = ' ';
1273754Sache	    if (input_x == box_width-1) {
1283754Sache	      scroll++;
12920442Sjkh	      redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
1303754Sache	    }
1313754Sache	    else {
1323754Sache	      wmove(dialog, box_y, input_x + box_x);
1333754Sache	      waddch(dialog, instr[scroll+input_x]);
1343754Sache	      input_x++;
1353754Sache	    }
1363754Sache	  } else
1374584Sache	    beep(); /* Alarm user about overflow */
1383754Sache	continue;
1394344Sache      case '\b':
1404344Sache      case '\177':
1413754Sache      case KEY_BACKSPACE:
1424344Sache      erase_it:
1433754Sache	if (input_x || scroll) {
1443754Sache	  i = strlen(instr);
1454688Sache	  memmove(instr+scroll+input_x-1, instr+scroll+input_x, i-(scroll+input_x)+1);
1463754Sache	  if (!input_x) {
1473754Sache	    int oldscroll = scroll;
1483754Sache	    scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
1493754Sache	    input_x = oldscroll - 1 - scroll;
1508344Sache	  } else
1513754Sache	    input_x--;
15220442Sjkh	  redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
1534688Sache	} else
1544688Sache	  beep();
1554688Sache	continue;
1564688Sache      case '\004':
1574688Sache      case KEY_DC:
1584688Sache	for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
1594688Sache	  instr[i] = '\0';
1604688Sache	i++;
1614688Sache	if (i == 0) {
1624688Sache	  beep();
1634688Sache	  continue;
1643754Sache	}
1654688Sache	memmove(instr+scroll+input_x, instr+scroll+input_x+1, i-(scroll+input_x));
16620442Sjkh	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
1673754Sache	continue;
1683754Sache      default:
1694344Sache	if (CCEQ(key, erase_char))
1704344Sache	  goto erase_it;
1714344Sache	if (CCEQ(key, kill_char))
1724344Sache	  goto kill_it;
1733754Sache	if (key < 0x100 && isprint(key)) {
1743754Sache	  for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
1753754Sache	    instr[i] = '\0';
1763754Sache	  i++;
1774667Sache	  if (i < MAX_LEN && (flen < 0 || scroll+input_x < flen)) {
1784667Sache	    if (flen < 0 || i < flen)
1794688Sache	      memmove(instr+scroll+input_x+1, instr+scroll+input_x, i-(scroll+input_x));
1803754Sache	    instr[scroll+input_x] = key;
1814688Sache	    if (input_x == box_width-1 && (flen < 0 || i < flen))
1823754Sache	      scroll++;
1834688Sache	    else
1844688Sache	      input_x++;
18520442Sjkh	    redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
1863754Sache	  } else
1874584Sache	    beep(); /* Alarm user about overflow */
1883754Sache	  continue;
1893754Sache	}
1903754Sache      }
1913754Sache    }
1923754Sacheret:
19320442Sjkh    redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, TRUE, attr_mask);
1944591Sache    wrefresh(dialog);
1953754Sache    strcpy(result, instr);
1963754Sache    return key;
1973754Sache}
1988344Sache
1998344Sachestatic void
20020442Sjkhredraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask)
2018344Sache{
2028344Sache  int i, fix_len;
2038344Sache
2048344Sache  wattrset(dialog, fexit ? old_attr : attr);
2058344Sache  wmove(dialog, box_y, box_x);
2068344Sache  fix_len = flen >= 0 ? MIN(flen-scroll,box_width) : box_width;
2078344Sache  for (i = 0; i < fix_len; i++)
20820442Sjkh      waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
2098344Sache  wattrset(dialog, old_attr);
2108344Sache  for ( ; i < box_width; i++)
21120442Sjkh      waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
2128344Sache  wmove(dialog, box_y, input_x + box_x);
2138344Sache}
214