Commit 87727d45 authored by Wang YanQing's avatar Wang YanQing Committed by Yann E. MORIN

menuconfig:inputbox: support navigate input position

This patch add support navigate input position *inside* the input
field with LEFT/RIGHT, so it is possible to modify the text in place.
Signed-off-by: default avatarWang YanQing <udknight@gmail.com>
Tested-by: default avatar"Yann E. MORIN" <yann.morin.1998@free.fr>
Signed-off-by: default avatar"Yann E. MORIN" <yann.morin.1998@free.fr>
parent 16974326
...@@ -45,7 +45,8 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width ...@@ -45,7 +45,8 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
const char *init) const char *init)
{ {
int i, x, y, box_y, box_x, box_width; int i, x, y, box_y, box_x, box_width;
int input_x = 0, scroll = 0, key = 0, button = -1; int input_x = 0, key = 0, button = -1;
int show_x, len, pos;
char *instr = dialog_input_result; char *instr = dialog_input_result;
WINDOW *dialog; WINDOW *dialog;
...@@ -97,14 +98,17 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width ...@@ -97,14 +98,17 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
wmove(dialog, box_y, box_x); wmove(dialog, box_y, box_x);
wattrset(dialog, dlg.inputbox.atr); wattrset(dialog, dlg.inputbox.atr);
input_x = strlen(instr); len = strlen(instr);
pos = len;
if (input_x >= box_width) { if (len >= box_width) {
scroll = input_x - box_width + 1; show_x = len - box_width + 1;
input_x = box_width - 1; input_x = box_width - 1;
for (i = 0; i < box_width - 1; i++) for (i = 0; i < box_width - 1; i++)
waddch(dialog, instr[scroll + i]); waddch(dialog, instr[show_x + i]);
} else { } else {
show_x = 0;
input_x = len;
waddstr(dialog, instr); waddstr(dialog, instr);
} }
...@@ -121,45 +125,104 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width ...@@ -121,45 +125,104 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
case KEY_UP: case KEY_UP:
case KEY_DOWN: case KEY_DOWN:
break; break;
case KEY_LEFT:
continue;
case KEY_RIGHT:
continue;
case KEY_BACKSPACE: case KEY_BACKSPACE:
case 127: case 127:
if (input_x || scroll) { if (pos) {
wattrset(dialog, dlg.inputbox.atr); wattrset(dialog, dlg.inputbox.atr);
if (!input_x) { if (input_x == 0) {
scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1); show_x--;
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 = strlen(instr) - scroll;
} else } else
input_x--; input_x--;
instr[scroll + input_x] = '\0';
mvwaddch(dialog, box_y, input_x + box_x, ' '); if (pos < len) {
for (i = pos - 1; i < len; i++) {
instr[i] = instr[i+1];
}
}
pos--;
len--;
instr[len] = '\0';
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, input_x + box_x); wmove(dialog, box_y, input_x + box_x);
wrefresh(dialog); wrefresh(dialog);
} }
continue; continue;
case KEY_LEFT:
if (pos > 0) {
if (input_x > 0) {
wmove(dialog, box_y, --input_x + box_x);
} else if (input_x == 0) {
show_x--;
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, box_x);
}
pos--;
}
continue;
case KEY_RIGHT:
if (pos < len) {
if (input_x < box_width - 1) {
wmove(dialog, box_y, ++input_x + box_x);
} else if (input_x == box_width - 1) {
show_x++;
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, input_x + box_x);
}
pos++;
}
continue;
default: default:
if (key < 0x100 && isprint(key)) { if (key < 0x100 && isprint(key)) {
if (scroll + input_x < MAX_LEN) { if (len < MAX_LEN) {
wattrset(dialog, dlg.inputbox.atr); wattrset(dialog, dlg.inputbox.atr);
instr[scroll + input_x] = key; if (pos < len) {
instr[scroll + input_x + 1] = '\0'; for (i = len; i > pos; i--)
instr[i] = instr[i-1];
instr[pos] = key;
} else {
instr[len] = key;
}
pos++;
len++;
instr[len] = '\0';
if (input_x == box_width - 1) { if (input_x == box_width - 1) {
scroll++; show_x++;
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width - 1; i++)
waddch(dialog, instr [scroll + i]);
} else { } else {
wmove(dialog, box_y, input_x++ + box_x); input_x++;
waddch(dialog, key); }
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
} }
wmove(dialog, box_y, input_x + box_x);
wrefresh(dialog); wrefresh(dialog);
} else } else
flash(); /* Alarm user about overflow */ flash(); /* Alarm user about overflow */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment