1994-10-21 15:42:30 +00:00
|
|
|
/*
|
1995-05-08 01:43:52 +00:00
|
|
|
* Changes Copyright (C) 1995 by Andrey A. Chernov, Moscow
|
|
|
|
*
|
|
|
|
* Original Copyright:
|
1994-10-21 15:42:30 +00:00
|
|
|
*
|
|
|
|
* AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <dialog.h>
|
|
|
|
#include "dialog.priv.h"
|
|
|
|
|
1996-12-14 16:14:21 +00:00
|
|
|
static 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);
|
1994-10-21 15:42:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Line editor
|
|
|
|
*/
|
1996-12-14 16:14:21 +00:00
|
|
|
int 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)
|
1994-10-21 15:42:30 +00:00
|
|
|
{
|
1995-05-08 01:43:52 +00:00
|
|
|
int i, key;
|
1994-11-18 00:30:06 +00:00
|
|
|
chtype old_attr;
|
1994-10-21 15:42:30 +00:00
|
|
|
static int input_x, scroll;
|
|
|
|
static unsigned char instr[MAX_LEN+1];
|
1994-11-10 15:16:46 +00:00
|
|
|
unsigned char erase_char = erasechar();
|
|
|
|
unsigned char kill_char = killchar();
|
|
|
|
#ifdef notyet
|
|
|
|
unsignec char werase_char = cur_term->Ottyb.c_cc[VWERASE];
|
|
|
|
#endif
|
1994-10-21 15:42:30 +00:00
|
|
|
|
1994-11-18 00:30:06 +00:00
|
|
|
old_attr = getattrs(dialog);
|
1994-11-10 15:16:46 +00:00
|
|
|
keypad(dialog, TRUE);
|
1994-11-02 01:40:34 +00:00
|
|
|
|
|
|
|
if (first) {
|
1994-10-21 15:42:30 +00:00
|
|
|
memset(instr, 0, sizeof(instr));
|
1994-11-02 01:40:34 +00:00
|
|
|
strcpy(instr, result);
|
|
|
|
i = strlen(instr);
|
1995-02-15 19:44:08 +00:00
|
|
|
/* input_x = i % box_width;*/
|
|
|
|
input_x = (i > box_width) ? box_width - 1 : i;
|
|
|
|
/* scroll = i - input_x;*/
|
|
|
|
scroll = (i > box_width) ? i - box_width + 1: 0;
|
1994-10-21 15:42:30 +00:00
|
|
|
}
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-10-21 15:42:30 +00:00
|
|
|
|
|
|
|
for (;;) {
|
1994-11-19 09:58:34 +00:00
|
|
|
wattrset(dialog, attr);
|
1994-11-18 01:02:42 +00:00
|
|
|
wrefresh(dialog);
|
1994-10-21 15:42:30 +00:00
|
|
|
key = wgetch(dialog);
|
|
|
|
switch (key) {
|
1995-02-15 19:44:08 +00:00
|
|
|
case ctrl('q'):
|
|
|
|
goto ret;
|
|
|
|
break;
|
|
|
|
case KEY_F(1):
|
|
|
|
display_helpfile();
|
|
|
|
break;
|
1994-10-21 15:42:30 +00:00
|
|
|
case TAB:
|
|
|
|
case KEY_BTAB:
|
|
|
|
case KEY_UP:
|
|
|
|
case KEY_DOWN:
|
|
|
|
case ESC:
|
1994-10-28 03:08:28 +00:00
|
|
|
case '\r':
|
1994-10-21 15:42:30 +00:00
|
|
|
case '\n':
|
|
|
|
for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
|
|
|
|
instr[i] = '\0';
|
1994-10-28 03:08:28 +00:00
|
|
|
if (key == '\r')
|
|
|
|
key = '\n';
|
1994-10-21 15:42:30 +00:00
|
|
|
goto ret;
|
1994-11-10 15:16:46 +00:00
|
|
|
case '\025':
|
1994-11-20 00:21:02 +00:00
|
|
|
case '\030':
|
1994-11-10 15:16:46 +00:00
|
|
|
kill_it:
|
1995-05-08 01:43:52 +00:00
|
|
|
input_x = scroll = 0;
|
|
|
|
/* fall through */
|
|
|
|
case '\013':
|
|
|
|
case KEY_EOL:
|
|
|
|
memset(instr + scroll + input_x, '\0', sizeof(instr) - scroll - input_x);
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1995-05-08 01:43:52 +00:00
|
|
|
continue;
|
1994-11-20 00:21:02 +00:00
|
|
|
case '\001':
|
1994-10-21 15:42:30 +00:00
|
|
|
case KEY_HOME:
|
|
|
|
input_x = scroll = 0;
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-10-21 15:42:30 +00:00
|
|
|
continue;
|
1994-11-20 00:21:02 +00:00
|
|
|
case '\005':
|
1994-10-21 15:42:30 +00:00
|
|
|
case KEY_END:
|
|
|
|
for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
|
|
|
|
instr[i] = '\0';
|
|
|
|
i++;
|
|
|
|
input_x = i % box_width;
|
|
|
|
scroll = i - input_x;
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-10-21 15:42:30 +00:00
|
|
|
continue;
|
1994-11-20 00:21:02 +00:00
|
|
|
case '\002':
|
1994-10-21 15:42:30 +00:00
|
|
|
case KEY_LEFT:
|
|
|
|
if (input_x || scroll) {
|
|
|
|
if (!input_x) {
|
|
|
|
int oldscroll = scroll;
|
|
|
|
scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
|
|
|
|
input_x = oldscroll - 1 - scroll;
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1995-05-08 01:43:52 +00:00
|
|
|
} else {
|
1994-10-21 15:42:30 +00:00
|
|
|
input_x--;
|
1995-05-08 01:43:52 +00:00
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
}
|
1994-11-20 00:21:02 +00:00
|
|
|
} else
|
|
|
|
beep();
|
1994-10-21 15:42:30 +00:00
|
|
|
continue;
|
1994-11-20 00:21:02 +00:00
|
|
|
case '\006':
|
1994-10-21 15:42:30 +00:00
|
|
|
case KEY_RIGHT:
|
1994-11-18 01:02:42 +00:00
|
|
|
if ( scroll+input_x < MAX_LEN
|
|
|
|
&& (flen < 0 || scroll+input_x < flen)
|
|
|
|
) {
|
1994-10-21 15:42:30 +00:00
|
|
|
if (!instr[scroll+input_x])
|
|
|
|
instr[scroll+input_x] = ' ';
|
|
|
|
if (input_x == box_width-1) {
|
|
|
|
scroll++;
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-10-21 15:42:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
waddch(dialog, instr[scroll+input_x]);
|
|
|
|
input_x++;
|
|
|
|
}
|
|
|
|
} else
|
1994-11-18 01:02:42 +00:00
|
|
|
beep(); /* Alarm user about overflow */
|
1994-10-21 15:42:30 +00:00
|
|
|
continue;
|
1994-11-10 15:16:46 +00:00
|
|
|
case '\b':
|
|
|
|
case '\177':
|
1994-10-21 15:42:30 +00:00
|
|
|
case KEY_BACKSPACE:
|
1994-11-10 15:16:46 +00:00
|
|
|
erase_it:
|
1994-10-21 15:42:30 +00:00
|
|
|
if (input_x || scroll) {
|
|
|
|
i = strlen(instr);
|
1994-11-20 00:21:02 +00:00
|
|
|
memmove(instr+scroll+input_x-1, instr+scroll+input_x, i-(scroll+input_x)+1);
|
1994-10-21 15:42:30 +00:00
|
|
|
if (!input_x) {
|
|
|
|
int oldscroll = scroll;
|
|
|
|
scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
|
|
|
|
input_x = oldscroll - 1 - scroll;
|
1995-05-08 01:43:52 +00:00
|
|
|
} else
|
1994-10-21 15:42:30 +00:00
|
|
|
input_x--;
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-11-20 00:21:02 +00:00
|
|
|
} else
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
case '\004':
|
|
|
|
case KEY_DC:
|
|
|
|
for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
|
|
|
|
instr[i] = '\0';
|
|
|
|
i++;
|
|
|
|
if (i == 0) {
|
|
|
|
beep();
|
|
|
|
continue;
|
1994-10-21 15:42:30 +00:00
|
|
|
}
|
1994-11-20 00:21:02 +00:00
|
|
|
memmove(instr+scroll+input_x, instr+scroll+input_x+1, i-(scroll+input_x));
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-10-21 15:42:30 +00:00
|
|
|
continue;
|
|
|
|
default:
|
1994-11-10 15:16:46 +00:00
|
|
|
if (CCEQ(key, erase_char))
|
|
|
|
goto erase_it;
|
|
|
|
if (CCEQ(key, kill_char))
|
|
|
|
goto kill_it;
|
1994-10-21 15:42:30 +00:00
|
|
|
if (key < 0x100 && isprint(key)) {
|
|
|
|
for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
|
|
|
|
instr[i] = '\0';
|
|
|
|
i++;
|
1994-11-19 10:22:12 +00:00
|
|
|
if (i < MAX_LEN && (flen < 0 || scroll+input_x < flen)) {
|
|
|
|
if (flen < 0 || i < flen)
|
1994-11-20 00:21:02 +00:00
|
|
|
memmove(instr+scroll+input_x+1, instr+scroll+input_x, i-(scroll+input_x));
|
1994-10-21 15:42:30 +00:00
|
|
|
instr[scroll+input_x] = key;
|
1994-11-20 00:21:02 +00:00
|
|
|
if (input_x == box_width-1 && (flen < 0 || i < flen))
|
1994-10-21 15:42:30 +00:00
|
|
|
scroll++;
|
1994-11-20 00:21:02 +00:00
|
|
|
else
|
|
|
|
input_x++;
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
1994-10-21 15:42:30 +00:00
|
|
|
} else
|
1994-11-18 01:02:42 +00:00
|
|
|
beep(); /* Alarm user about overflow */
|
1994-10-21 15:42:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret:
|
1996-12-14 16:14:21 +00:00
|
|
|
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, TRUE, attr_mask);
|
1994-11-18 01:46:32 +00:00
|
|
|
wrefresh(dialog);
|
1994-10-21 15:42:30 +00:00
|
|
|
strcpy(result, instr);
|
|
|
|
return key;
|
|
|
|
}
|
1995-05-08 01:43:52 +00:00
|
|
|
|
|
|
|
static void
|
1996-12-14 16:14:21 +00:00
|
|
|
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)
|
1995-05-08 01:43:52 +00:00
|
|
|
{
|
|
|
|
int i, fix_len;
|
|
|
|
|
|
|
|
wattrset(dialog, fexit ? old_attr : attr);
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
fix_len = flen >= 0 ? MIN(flen-scroll,box_width) : box_width;
|
|
|
|
for (i = 0; i < fix_len; i++)
|
1996-12-14 16:14:21 +00:00
|
|
|
waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
|
1995-05-08 01:43:52 +00:00
|
|
|
wattrset(dialog, old_attr);
|
|
|
|
for ( ; i < box_width; i++)
|
1996-12-14 16:14:21 +00:00
|
|
|
waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
|
1995-05-08 01:43:52 +00:00
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
}
|