1994-10-21 16:42:30 +01:00
|
|
|
/*
|
|
|
|
* inputbox.c -- implements the input box
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Line editor
|
|
|
|
*/
|
1994-11-18 02:02:42 +01:00
|
|
|
int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, chtype attr, int first, unsigned char *result)
|
1994-10-21 16:42:30 +01:00
|
|
|
{
|
1994-11-19 01:45:02 +01:00
|
|
|
int i, key, len;
|
1994-11-18 01:30:06 +01:00
|
|
|
chtype old_attr;
|
1994-10-21 16:42:30 +01:00
|
|
|
static int input_x, scroll;
|
|
|
|
static unsigned char instr[MAX_LEN+1];
|
1994-11-10 16:16:46 +01: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 16:42:30 +01:00
|
|
|
|
1994-11-18 01:30:06 +01:00
|
|
|
old_attr = getattrs(dialog);
|
1994-11-10 16:16:46 +01:00
|
|
|
keypad(dialog, TRUE);
|
1994-11-02 02:40:34 +01:00
|
|
|
|
|
|
|
if (first) {
|
1994-10-21 16:42:30 +01:00
|
|
|
memset(instr, 0, sizeof(instr));
|
1994-11-02 02:40:34 +01:00
|
|
|
strcpy(instr, result);
|
|
|
|
i = strlen(instr);
|
|
|
|
input_x = i % box_width;
|
|
|
|
scroll = i - input_x;
|
1994-10-21 16:42:30 +01:00
|
|
|
}
|
1994-11-18 02:46:32 +01:00
|
|
|
wmove(dialog, box_y, box_x);
|
1994-11-19 01:45:02 +01:00
|
|
|
wattrset(dialog, attr);
|
|
|
|
len = flen >= 0 ? MIN(flen,box_width) : box_width;
|
|
|
|
for (i = 0; i < len; i++)
|
1994-11-18 02:46:32 +01:00
|
|
|
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
1994-11-19 01:45:02 +01:00
|
|
|
wattrset(dialog, old_attr);
|
|
|
|
len = strlen(instr);
|
|
|
|
len = MIN(len,box_width);
|
|
|
|
for ( ; i < len; i++)
|
|
|
|
waddch(dialog, instr[scroll+i]);
|
|
|
|
wattrset(dialog, attr);
|
1994-10-21 16:42:30 +01:00
|
|
|
|
|
|
|
wmove(dialog, box_y, box_x + input_x);
|
|
|
|
for (;;) {
|
1994-11-18 02:02:42 +01:00
|
|
|
wrefresh(dialog);
|
1994-10-21 16:42:30 +01:00
|
|
|
key = wgetch(dialog);
|
|
|
|
switch (key) {
|
|
|
|
case TAB:
|
|
|
|
case KEY_BTAB:
|
|
|
|
case KEY_UP:
|
|
|
|
case KEY_DOWN:
|
|
|
|
case ESC:
|
1994-10-28 04:08:28 +01:00
|
|
|
case '\r':
|
1994-10-21 16:42:30 +01:00
|
|
|
case '\n':
|
|
|
|
for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
|
|
|
|
instr[i] = '\0';
|
1994-10-28 04:08:28 +01:00
|
|
|
if (key == '\r')
|
|
|
|
key = '\n';
|
1994-10-21 16:42:30 +01:00
|
|
|
goto ret;
|
1994-11-10 16:16:46 +01:00
|
|
|
case '\025':
|
|
|
|
kill_it:
|
|
|
|
memset(instr, 0, sizeof(instr));
|
1994-10-21 16:42:30 +01:00
|
|
|
case KEY_HOME:
|
|
|
|
input_x = scroll = 0;
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
for (i = 0; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[i] ? instr[i] : ' ');
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
continue;
|
|
|
|
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;
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
for (i = 0; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
continue;
|
|
|
|
case KEY_LEFT:
|
|
|
|
if (input_x || scroll) {
|
|
|
|
if (!input_x) {
|
|
|
|
int oldscroll = scroll;
|
|
|
|
scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
for (i = 0; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[scroll+input_x+i] ? instr[scroll+input_x+i] : ' ');
|
|
|
|
input_x = oldscroll - 1 - scroll;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
input_x--;
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case KEY_RIGHT:
|
1994-11-18 02:02:42 +01:00
|
|
|
if ( scroll+input_x < MAX_LEN
|
|
|
|
&& (flen < 0 || scroll+input_x < flen)
|
|
|
|
) {
|
1994-10-21 16:42:30 +01:00
|
|
|
if (!instr[scroll+input_x])
|
|
|
|
instr[scroll+input_x] = ' ';
|
|
|
|
if (input_x == box_width-1) {
|
|
|
|
scroll++;
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
for (i = 0; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
|
|
|
wmove(dialog, box_y, box_x + box_width - 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
waddch(dialog, instr[scroll+input_x]);
|
|
|
|
input_x++;
|
|
|
|
}
|
|
|
|
} else
|
1994-11-18 02:02:42 +01:00
|
|
|
beep(); /* Alarm user about overflow */
|
1994-10-21 16:42:30 +01:00
|
|
|
continue;
|
1994-11-10 16:16:46 +01:00
|
|
|
case '\b':
|
|
|
|
case '\177':
|
1994-10-21 16:42:30 +01:00
|
|
|
case KEY_BACKSPACE:
|
|
|
|
case KEY_DC:
|
1994-11-10 16:16:46 +01:00
|
|
|
erase_it:
|
1994-10-21 16:42:30 +01:00
|
|
|
if (input_x || scroll) {
|
|
|
|
i = strlen(instr);
|
|
|
|
memmove(instr+scroll+input_x-1, instr+scroll+input_x, i-scroll+input_x+1);
|
|
|
|
if (!input_x) {
|
|
|
|
int oldscroll = scroll;
|
|
|
|
scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
for (i = 0; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[scroll+input_x+i] ? instr[scroll+input_x+i] : ' ');
|
|
|
|
input_x = oldscroll - 1 - scroll;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
input_x--;
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
for (i = input_x; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
default:
|
1994-11-10 16:16:46 +01:00
|
|
|
if (CCEQ(key, erase_char))
|
|
|
|
goto erase_it;
|
|
|
|
if (CCEQ(key, kill_char))
|
|
|
|
goto kill_it;
|
1994-10-21 16:42:30 +01:00
|
|
|
if (key < 0x100 && isprint(key)) {
|
|
|
|
for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
|
|
|
|
instr[i] = '\0';
|
|
|
|
i++;
|
1994-11-18 02:02:42 +01:00
|
|
|
if ( i < MAX_LEN
|
|
|
|
&& (flen < 0 || i < flen)
|
|
|
|
) {
|
1994-10-21 16:42:30 +01:00
|
|
|
memmove(instr+scroll+input_x+1, instr+scroll+input_x, i-scroll+input_x);
|
|
|
|
instr[scroll+input_x] = key;
|
|
|
|
if (input_x == box_width-1) {
|
|
|
|
scroll++;
|
|
|
|
wmove(dialog, box_y, box_x);
|
|
|
|
for (i = 0; i < box_width-1; i++)
|
|
|
|
waddch(dialog, instr[scroll+i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
for (i = input_x; i < box_width; i++)
|
|
|
|
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
|
|
|
wmove(dialog, box_y, ++input_x + box_x);
|
|
|
|
}
|
|
|
|
} else
|
1994-11-18 02:02:42 +01:00
|
|
|
beep(); /* Alarm user about overflow */
|
1994-10-21 16:42:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret:
|
1994-11-18 01:30:06 +01:00
|
|
|
wattrset(dialog, old_attr);
|
1994-11-18 02:46:32 +01:00
|
|
|
wmove(dialog, box_y, box_x);
|
1994-11-19 01:45:02 +01:00
|
|
|
len = strlen(instr);
|
|
|
|
len = MIN(len,box_width);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
waddch(dialog, instr[scroll+i]);
|
1994-11-18 02:46:32 +01:00
|
|
|
wmove(dialog, box_y, input_x + box_x);
|
|
|
|
wrefresh(dialog);
|
1994-10-21 16:42:30 +01:00
|
|
|
strcpy(result, instr);
|
|
|
|
return key;
|
|
|
|
}
|