2005-09-15 09:58:31 -04:00
|
|
|
/* Input field widget implementation. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "bfu/button.h"
|
|
|
|
#include "bfu/dialog.h"
|
|
|
|
#include "bfu/inpfield.h"
|
|
|
|
#include "bfu/inphist.h"
|
|
|
|
#include "bfu/msgbox.h"
|
|
|
|
#include "bfu/text.h"
|
|
|
|
#include "config/kbdbind.h"
|
2006-01-14 16:44:00 -05:00
|
|
|
#include "intl/charsets.h"
|
2021-08-08 15:25:08 -04:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "osdep/osdep.h"
|
|
|
|
#include "session/session.h"
|
|
|
|
#include "terminal/draw.h"
|
|
|
|
#include "terminal/kbd.h"
|
|
|
|
#include "terminal/mouse.h"
|
|
|
|
#include "terminal/terminal.h"
|
|
|
|
#include "terminal/window.h"
|
|
|
|
#include "util/color.h"
|
|
|
|
#include "util/memlist.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
|
|
|
|
#define INPUTFIELD_HEIGHT 1
|
|
|
|
|
|
|
|
#define INPUTFIELD_FLOATLABEL_PADDING 1
|
|
|
|
|
|
|
|
#define INPUTFIELD_FLOAT_SEPARATOR ":"
|
|
|
|
#define INPUTFIELD_FLOAT_SEPARATOR_LEN 1
|
|
|
|
|
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
add_dlg_field_do(struct dialog *dlg, enum widget_type type, char *label,
|
2005-09-15 09:58:31 -04:00
|
|
|
int min, int max, widget_handler_T *handler,
|
|
|
|
int datalen, void *data,
|
|
|
|
struct input_history *history, enum inpfield_flags flags)
|
|
|
|
{
|
|
|
|
struct widget *widget = &dlg->widgets[dlg->number_of_widgets++];
|
|
|
|
|
|
|
|
widget->type = type;
|
|
|
|
widget->text = label;
|
|
|
|
widget->handler = handler;
|
|
|
|
widget->datalen = datalen;
|
|
|
|
widget->data = data;
|
|
|
|
|
|
|
|
widget->info.field.history = history;
|
|
|
|
widget->info.field.flags = flags;
|
|
|
|
widget->info.field.min = min;
|
|
|
|
widget->info.field.max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget_handler_status_T
|
|
|
|
check_number(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
struct widget *widget = widget_data->widget;
|
|
|
|
char *end;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
l = strtol(widget_data->cdata, &end, 10);
|
|
|
|
|
|
|
|
if (errno || !*widget_data->cdata || *end) {
|
|
|
|
info_box(dlg_data->win->term, 0,
|
|
|
|
N_("Bad number"), ALIGN_CENTER,
|
|
|
|
N_("Number expected in field"));
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l < widget->info.field.min || l > widget->info.field.max) {
|
|
|
|
info_box(dlg_data->win->term, MSGBOX_FREE_TEXT,
|
|
|
|
N_("Bad number"), ALIGN_CENTER,
|
|
|
|
msg_text(dlg_data->win->term,
|
|
|
|
N_("Number should be in the range from %d to %d."),
|
|
|
|
widget->info.field.min, widget->info.field.max));
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget_handler_status_T
|
|
|
|
check_nonempty(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *p;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
for (p = widget_data->cdata; *p; p++)
|
|
|
|
if (*p > ' ')
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
|
|
|
|
info_box(dlg_data->win->term, 0,
|
|
|
|
N_("Bad string"), ALIGN_CENTER,
|
|
|
|
N_("Empty string not allowed"));
|
|
|
|
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-07 12:27:06 -04:00
|
|
|
dlg_format_field(struct dialog_data *dlg_data,
|
2005-09-15 09:58:31 -04:00
|
|
|
struct widget_data *widget_data,
|
2006-03-06 00:01:12 -05:00
|
|
|
int x, int *y, int w, int *rw, enum format_align align, int format_only)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2008-09-07 12:27:06 -04:00
|
|
|
struct terminal *term = dlg_data->win->term;
|
2005-09-15 09:58:31 -04:00
|
|
|
static int max_label_width;
|
|
|
|
static int *prev_y; /* Assert the uniqueness of y */ /* TODO: get rid of this !! --Zas */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *label = widget_data->widget->text;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct color_pair *text_color = NULL;
|
|
|
|
int label_width = 0;
|
|
|
|
int float_label = widget_data->widget->info.field.flags & (INPFIELD_FLOAT|INPFIELD_FLOAT2);
|
|
|
|
|
|
|
|
if (label && *label && float_label) {
|
|
|
|
label_width = strlen(label);
|
|
|
|
if (prev_y == y) {
|
|
|
|
int_lower_bound(&max_label_width, label_width);
|
|
|
|
} else {
|
|
|
|
max_label_width = label_width;
|
|
|
|
prev_y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Right align the floating label up against the
|
|
|
|
* input field */
|
|
|
|
x += max_label_width - label_width;
|
|
|
|
w -= max_label_width - label_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (label && *label) {
|
2006-03-06 00:01:12 -05:00
|
|
|
if (!format_only) text_color = get_bfu_color(term, "dialog.text");
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-09-07 12:12:42 -04:00
|
|
|
dlg_format_text_do(dlg_data, label, x, y, w, rw, text_color, ALIGN_LEFT, format_only);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: We want the field and label on the same line if the terminal
|
|
|
|
* width allows it. */
|
|
|
|
if (label && *label && float_label) {
|
|
|
|
if (widget_data->widget->info.field.flags & INPFIELD_FLOAT) {
|
|
|
|
(*y) -= INPUTFIELD_HEIGHT;
|
2008-09-07 12:12:42 -04:00
|
|
|
dlg_format_text_do(dlg_data, INPUTFIELD_FLOAT_SEPARATOR,
|
2005-09-15 09:58:31 -04:00
|
|
|
x + label_width, y, w, rw,
|
2006-03-06 00:01:12 -05:00
|
|
|
text_color, ALIGN_LEFT, format_only);
|
2005-09-15 09:58:31 -04:00
|
|
|
w -= INPUTFIELD_FLOAT_SEPARATOR_LEN + INPUTFIELD_FLOATLABEL_PADDING;
|
|
|
|
x += INPUTFIELD_FLOAT_SEPARATOR_LEN + INPUTFIELD_FLOATLABEL_PADDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Is 5 chars for input field enough? --jonas */
|
|
|
|
if (label_width < w - 5) {
|
|
|
|
(*y) -= INPUTFIELD_HEIGHT;
|
|
|
|
w -= label_width;
|
|
|
|
x += label_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rw) int_lower_bound(rw, int_min(w, DIALOG_MIN_WIDTH));
|
|
|
|
|
|
|
|
set_box(&widget_data->box, x, *y, w, INPUTFIELD_HEIGHT);
|
|
|
|
|
|
|
|
(*y) += INPUTFIELD_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
input_field_cancel(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
void (*fn)(void *) = widget_data->widget->data;
|
|
|
|
void *data = dlg_data->dlg->udata2;
|
|
|
|
|
|
|
|
if (fn) fn(data);
|
|
|
|
|
|
|
|
return cancel_dialog(dlg_data, widget_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
input_field_ok(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
void (*fn)(void *, char *) = widget_data->widget->data;
|
2005-09-15 09:58:31 -04:00
|
|
|
void *data = dlg_data->dlg->udata2;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *text = dlg_data->widgets_data->cdata;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (check_dialog(dlg_data)) return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
if (widget_has_history(dlg_data->widgets_data))
|
|
|
|
add_to_input_history(dlg_data->dlg->widgets->info.field.history,
|
|
|
|
text, 1);
|
|
|
|
|
|
|
|
if (fn) fn(data, text);
|
|
|
|
|
|
|
|
return cancel_dialog(dlg_data, widget_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_field(struct terminal *term, struct memory_list *ml, int intl,
|
2021-01-02 10:20:27 -05:00
|
|
|
char *title,
|
|
|
|
char *text,
|
|
|
|
char *okbutton,
|
|
|
|
char *cancelbutton,
|
2005-09-15 09:58:31 -04:00
|
|
|
void *data, struct input_history *history, int l,
|
2021-01-02 10:20:27 -05:00
|
|
|
char *def, int min, int max,
|
2005-09-15 09:58:31 -04:00
|
|
|
widget_handler_T *check,
|
2021-01-02 10:20:27 -05:00
|
|
|
void (*fn)(void *, char *),
|
2005-09-15 09:58:31 -04:00
|
|
|
void (*cancelfn)(void *))
|
|
|
|
{
|
|
|
|
struct dialog *dlg;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *field;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (intl) {
|
|
|
|
title = _(title, term);
|
|
|
|
text = _(text, term);
|
|
|
|
okbutton = _(okbutton, term);
|
|
|
|
cancelbutton = _(cancelbutton, term);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INPUT_WIDGETS_COUNT 3
|
|
|
|
dlg = calloc_dialog(INPUT_WIDGETS_COUNT, l);
|
|
|
|
if (!dlg) return;
|
|
|
|
|
|
|
|
/* @field is automatically cleared by calloc() */
|
|
|
|
field = get_dialog_offset(dlg, INPUT_WIDGETS_COUNT);
|
|
|
|
|
|
|
|
if (def) {
|
|
|
|
int defsize = strlen(def) + 1;
|
|
|
|
|
|
|
|
memcpy(field, def, (defsize > l) ? l - 1 : defsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
dlg->title = title;
|
|
|
|
dlg->layouter = generic_dialog_layouter;
|
|
|
|
dlg->layout.fit_datalen = 1;
|
|
|
|
dlg->udata2 = data;
|
|
|
|
|
|
|
|
add_dlg_field(dlg, text, min, max, check, l, field, history);
|
|
|
|
|
|
|
|
add_dlg_button(dlg, okbutton, B_ENTER, input_field_ok, fn);
|
|
|
|
add_dlg_button(dlg, cancelbutton, B_ESC, input_field_cancel, cancelfn);
|
|
|
|
|
|
|
|
add_dlg_end(dlg, INPUT_WIDGETS_COUNT);
|
|
|
|
|
2007-03-11 06:44:13 -04:00
|
|
|
add_to_ml(&ml, (void *) dlg, (void *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
do_dialog(term, dlg, ml);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_dialog(struct terminal *term, struct memory_list *ml,
|
2021-01-02 10:20:27 -05:00
|
|
|
char *title,
|
|
|
|
char *text,
|
2005-09-15 09:58:31 -04:00
|
|
|
void *data, struct input_history *history, int l,
|
2021-01-02 10:20:27 -05:00
|
|
|
char *def, int min, int max,
|
2005-09-15 09:58:31 -04:00
|
|
|
widget_handler_T *check,
|
2021-01-02 10:20:27 -05:00
|
|
|
void (*fn)(void *, char *),
|
2005-09-15 09:58:31 -04:00
|
|
|
void (*cancelfn)(void *))
|
2006-05-31 13:34:49 -04:00
|
|
|
{
|
Here is a framework that detects cases where a PO file assigns
the same accelerator key to multiple buttons in a dialog box or
to multiple items in a menu. ELinks already has some support for
this but it requires the translator to run ELinks and manually
scan through all menus and dialogs. The attached changes make it
possible to quickly detect and list any conflicts, including ones
that can only occur on operating systems or configurations that
the translator is not currently using.
The changes have no immediate effect on the elinks executable or
the MO files. PO files become larger, however.
The scheme works like this:
- Like before, accelerator keys in translatable strings are
tagged with the tilde (~) character.
- Whenever a C source file defines an accelerator key, it must
assign one or more named "contexts" to it. The translations in
the PO files inherit these contexts. If multiple strings use
the same accelerator (case insensitive) in the same context,
that's a conflict and can be detected automatically.
- The contexts are defined with "gettext_accelerator_context"
comments in source files. These comments delimit regions where
all translatable strings containing tildes are given the same
contexts. There must be one special comment at the top of the
region; it lists the contexts assigned to that region. The
region automatically ends at the end of the function (found
with regexp /^\}/), but it can also be closed explicitly with
another special comment. The comments are formatted like this:
/* [gettext_accelerator_context(foo, bar, baz)]
begins a region that uses the contexts "foo", "bar", and "baz".
The comma is the delimiter; whitespace is optional.
[gettext_accelerator_context()]
ends the region. */
The scripts don't currently check whether this syntax occurs
inside or outside comments.
- The names of contexts consist of C identifiers delimited with
periods. I typically used the name of a function that sets
up a dialog, or the name of an array where the items of a
menu are listed. There is a special feature for static
functions: if the name begins with a period, then the period
will be replaced with the name of the source file and a colon.
- If a menu is programmatically generated from multiple parts,
of which some are never used together, so that it is safe to
use the same accelerators in them, then it is necessary to
define multiple contexts for the same menu. link_menu() in
src/viewer/text/link.c is the most complex example of this.
- During make update-po:
- A Perl script (po/gather-accelerator-contexts.pl) reads
po/elinks.pot, scans the source files listed in it for
"gettext_accelerator_context" comments, and rewrites
po/elinks.pot with "accelerator_context" comments that
indicate the contexts of each msgid: the union of all
contexts of all of its uses in the source files. It also
removes any "gettext_accelerator_context" comments that
xgettext --add-comments has copied to elinks.pot.
- If po/gather-accelerator-contexts.pl does not find any
contexts for some use of an msgid that seems to contain an
accelerator (because it contains a tilde), it warns. If the
tilde refers to e.g. "~/.elinks" and does not actually mark
an accelerator, the warning can be silenced by specifying the
special context "IGNORE", which the script otherwise ignores.
- msgmerge copies the "accelerator_context" comments from
po/elinks.pot to po/*.po. Translators do not edit those
comments.
- During make check-po:
- Another Perl script (po/check-accelerator-contexts.pl) reads
po/*.po and keeps track of which accelerators have been bound
in each context. It warns about any conflicts it finds.
This script does not access the C source files; thus it does
not matter if the line numbers in "#:" lines are out of date.
This implementation is not perfect and I am not proposing to
add it to the main source tree at this time. Specifically:
- It introduces compile-time dependencies on Perl and Locale::PO.
There should be a configure-time or compile-time check so that
the new features are skipped if the prerequisites are missing.
- When the scripts include msgstr strings in warnings, they
should transcode them from the charset of the PO file to the
one specified by the user's locale.
- It is not adequately documented (well, except perhaps here).
- po/check-accelerator-contexts.pl reports the same conflict
multiple times if it occurs in multiple contexts.
- The warning messages should include line numbers, so that users
of Emacs could conveniently edit the conflicting part of the PO
file. This is not feasible with the current version of
Locale::PO.
- Locale::PO does not understand #~ lines and spews warnings
about them. There is an ugly hack to hide these warnings.
- Jonas Fonseca suggested the script could propose accelerators
that are still available. This has not been implemented.
There are three files attached:
- po/gather-accelerator-contexts.pl: Augments elinks.pot with
context information.
- po/check-accelerator-contexts.pl: Checks conflicts.
- accelerator-contexts.diff: Makes po/Makefile run the scripts,
and adds special comments to source files.
2005-12-04 18:38:29 -05:00
|
|
|
/* [gettext_accelerator_context(input_dialog)] */
|
2005-09-15 09:58:31 -04:00
|
|
|
input_field(term, ml, 1, title, text, N_("~OK"), N_("~Cancel"),
|
|
|
|
data, history, l,
|
|
|
|
def, min, max,
|
|
|
|
check, fn, cancelfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
display_field_do(struct dialog_data *dlg_data, struct widget_data *widget_data,
|
|
|
|
int hide)
|
|
|
|
{
|
|
|
|
struct terminal *term = dlg_data->win->term;
|
|
|
|
struct color_pair *color;
|
|
|
|
int sel = is_selected_widget(dlg_data, widget_data);
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2006-01-14 16:44:00 -05:00
|
|
|
int len = 0, left = 0;
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2006-01-14 16:44:00 -05:00
|
|
|
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *t = widget_data->cdata;
|
2006-01-14 16:44:00 -05:00
|
|
|
int p = widget_data->info.field.cpos;
|
|
|
|
|
2006-04-07 18:16:10 -04:00
|
|
|
len = utf8_ptr2cells(t, &t[p]);
|
2006-01-14 16:44:00 -05:00
|
|
|
int_bounds(&left, len - widget_data->box.width + 1, len);
|
|
|
|
int_lower_bound(&left, 0);
|
2006-04-07 18:16:10 -04:00
|
|
|
widget_data->info.field.vpos = utf8_cells2bytes(t, left, NULL);
|
2006-07-27 03:51:10 -04:00
|
|
|
} else
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2006-02-02 18:27:01 -05:00
|
|
|
{
|
2006-01-14 16:44:00 -05:00
|
|
|
int_bounds(&widget_data->info.field.vpos,
|
2005-09-15 09:58:31 -04:00
|
|
|
widget_data->info.field.cpos - widget_data->box.width + 1,
|
|
|
|
widget_data->info.field.cpos);
|
2006-01-14 16:44:00 -05:00
|
|
|
int_lower_bound(&widget_data->info.field.vpos, 0);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
color = get_bfu_color(term, "dialog.field");
|
|
|
|
if (color)
|
|
|
|
draw_box(term, &widget_data->box, ' ', 0, color);
|
|
|
|
|
|
|
|
color = get_bfu_color(term, "dialog.field-text");
|
|
|
|
if (color) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *text = widget_data->cdata + widget_data->info.field.vpos;
|
2006-02-02 18:27:01 -05:00
|
|
|
int len, w;
|
|
|
|
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp && !hide)
|
2006-04-07 18:16:10 -04:00
|
|
|
len = utf8_ptr2cells(text, NULL);
|
2007-05-20 08:31:02 -04:00
|
|
|
else if (term->utf8_cp)
|
2006-04-07 18:16:10 -04:00
|
|
|
len = utf8_ptr2chars(text, NULL);
|
2006-02-02 18:27:01 -05:00
|
|
|
else
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2006-02-02 18:27:01 -05:00
|
|
|
len = strlen(text);
|
2006-01-30 19:26:27 -05:00
|
|
|
w = int_min(len, widget_data->box.width);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!hide) {
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp)
|
2006-04-07 18:16:10 -04:00
|
|
|
w = utf8_cells2bytes(text, w, NULL);
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2008-10-11 16:45:27 -04:00
|
|
|
draw_dlg_text(dlg_data, widget_data->box.x, widget_data->box.y,
|
2006-01-30 19:26:27 -05:00
|
|
|
text, w, 0, color);
|
2005-09-15 09:58:31 -04:00
|
|
|
} else {
|
2018-09-09 13:14:56 -04:00
|
|
|
struct el_box box;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
copy_box(&box, &widget_data->box);
|
|
|
|
box.width = w;
|
|
|
|
|
|
|
|
draw_box(term, &box, '*', 0, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sel) {
|
2006-01-14 16:44:00 -05:00
|
|
|
int x;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp)
|
2006-01-14 16:44:00 -05:00
|
|
|
x = widget_data->box.x + len - left;
|
2006-07-27 03:51:10 -04:00
|
|
|
else
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2006-01-14 16:44:00 -05:00
|
|
|
x = widget_data->box.x + widget_data->info.field.cpos - widget_data->info.field.vpos;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
set_cursor(term, x, widget_data->box.y, 0);
|
|
|
|
set_window_ptr(dlg_data->win, widget_data->box.x, widget_data->box.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
display_field(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
return display_field_do(dlg_data, widget_data, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
display_field_pass(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
return display_field_do(dlg_data, widget_data, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
init_field(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
if (widget_has_history(widget_data)) {
|
|
|
|
struct input_history_entry *entry;
|
|
|
|
|
|
|
|
foreach (entry, widget_data->widget->info.field.history->entries) {
|
|
|
|
int datalen = strlen(entry->data);
|
|
|
|
struct input_history_entry *new_entry;
|
|
|
|
|
|
|
|
/* One byte is reserved in struct input_history_entry. */
|
|
|
|
new_entry = mem_alloc(sizeof(*new_entry) + datalen);
|
|
|
|
if (!new_entry) continue;
|
|
|
|
|
|
|
|
memcpy(new_entry->data, entry->data, datalen + 1);
|
|
|
|
add_to_list(widget_data->info.field.history, new_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
widget_data->info.field.cpos = strlen(widget_data->cdata);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
field_prev_history(struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
if (widget_has_history(widget_data)
|
|
|
|
&& (void *) widget_data->info.field.cur_hist->prev != &widget_data->info.field.history) {
|
|
|
|
widget_data->info.field.cur_hist = widget_data->info.field.cur_hist->prev;
|
|
|
|
dlg_set_history(widget_data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
field_next_history(struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
if (widget_has_history(widget_data)
|
|
|
|
&& (void *) widget_data->info.field.cur_hist != &widget_data->info.field.history) {
|
|
|
|
widget_data->info.field.cur_hist = widget_data->info.field.cur_hist->next;
|
|
|
|
dlg_set_history(widget_data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
mouse_field(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
struct term_event *ev = dlg_data->term_event;
|
|
|
|
|
|
|
|
if (!check_mouse_position(ev, &widget_data->box))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
/* Handle navigation through history (if any) using up/down mouse wheel */
|
|
|
|
switch (get_mouse_button(ev)) {
|
|
|
|
case B_WHEEL_UP:
|
|
|
|
if (check_mouse_action(ev, B_DOWN)) {
|
|
|
|
if (field_prev_history(widget_data)) {
|
|
|
|
select_widget(dlg_data, widget_data);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
case B_WHEEL_DOWN:
|
|
|
|
if (check_mouse_action(ev, B_DOWN)) {
|
|
|
|
if (field_next_history(widget_data)) {
|
|
|
|
select_widget(dlg_data, widget_data);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Place text cursor at mouse position and focus the widget. */
|
|
|
|
widget_data->info.field.cpos = widget_data->info.field.vpos
|
|
|
|
+ ev->info.mouse.x - widget_data->box.x;
|
|
|
|
int_upper_bound(&widget_data->info.field.cpos, strlen(widget_data->cdata));
|
|
|
|
|
|
|
|
select_widget(dlg_data, widget_data);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
kbd_field(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
struct window *win = dlg_data->win;
|
|
|
|
struct terminal *term = win->term;
|
|
|
|
struct term_event *ev = dlg_data->term_event;
|
|
|
|
action_id_T action_id;
|
|
|
|
|
|
|
|
action_id = kbd_action(KEYMAP_EDIT, ev, NULL);
|
|
|
|
if (action_id != -1
|
|
|
|
&& !action_is_anonymous_safe(KEYMAP_EDIT, action_id)
|
|
|
|
&& get_cmd_opt_bool("anonymous"))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
switch (action_id) {
|
|
|
|
case ACT_EDIT_UP:
|
|
|
|
if (!widget_has_history(widget_data))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
if (field_prev_history(widget_data)) {
|
|
|
|
goto display_field;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_EDIT_DOWN:
|
|
|
|
if (!widget_has_history(widget_data))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
if (field_next_history(widget_data)) {
|
|
|
|
goto display_field;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_EDIT_RIGHT:
|
2006-01-14 16:44:00 -05:00
|
|
|
if (widget_data->info.field.cpos < strlen(widget_data->cdata)) {
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *next = widget_data->cdata + widget_data->info.field.cpos;
|
|
|
|
char *end = strchr((const char *)next, '\0');
|
2006-07-27 03:51:10 -04:00
|
|
|
|
2006-09-17 09:06:22 -04:00
|
|
|
utf8_to_unicode(&next, end);
|
2006-01-14 16:44:00 -05:00
|
|
|
widget_data->info.field.cpos = (int)(next - widget_data->cdata);
|
|
|
|
} else
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2006-02-02 18:27:01 -05:00
|
|
|
{
|
2006-01-14 16:44:00 -05:00
|
|
|
widget_data->info.field.cpos++;
|
2006-02-02 18:27:01 -05:00
|
|
|
}
|
2006-01-14 16:44:00 -05:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_LEFT:
|
|
|
|
if (widget_data->info.field.cpos > 0)
|
|
|
|
widget_data->info.field.cpos--;
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (widget_data->info.field.cpos && term->utf8_cp) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *t = widget_data->cdata;
|
|
|
|
char *t2 = t;
|
2006-01-14 16:44:00 -05:00
|
|
|
int p = widget_data->info.field.cpos;
|
|
|
|
unsigned char tmp = t[p];
|
|
|
|
|
|
|
|
t[p] = '\0';
|
|
|
|
strlen_utf8(&t2);
|
|
|
|
t[p] = tmp;
|
|
|
|
widget_data->info.field.cpos = (int)(t2 - t);
|
2006-07-27 03:51:10 -04:00
|
|
|
|
2006-01-14 16:44:00 -05:00
|
|
|
}
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2005-09-15 09:58:31 -04:00
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_HOME:
|
|
|
|
widget_data->info.field.cpos = 0;
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_END:
|
|
|
|
widget_data->info.field.cpos = strlen(widget_data->cdata);
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_BACKSPACE:
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (widget_data->info.field.cpos && term->utf8_cp) {
|
2006-04-07 18:16:10 -04:00
|
|
|
/* XXX: stolen from src/viewer/text/form.c */
|
|
|
|
/* FIXME: This isn't nice. We remove last byte
|
|
|
|
* from UTF-8 character to detect
|
|
|
|
* character before it. */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *text = widget_data->cdata;
|
|
|
|
char *end = widget_data->cdata + widget_data->info.field.cpos - 1;
|
2006-04-07 18:16:10 -04:00
|
|
|
unicode_val_T data;
|
|
|
|
int old = widget_data->info.field.cpos;
|
|
|
|
|
|
|
|
while(1) {
|
2006-09-17 09:06:22 -04:00
|
|
|
data = utf8_to_unicode(&text, end);
|
2006-04-07 18:16:10 -04:00
|
|
|
if (data == UCS_NO_CHAR)
|
|
|
|
break;
|
|
|
|
}
|
2006-01-14 16:44:00 -05:00
|
|
|
|
2006-04-07 18:16:10 -04:00
|
|
|
widget_data->info.field.cpos = (int)(text - widget_data->cdata);
|
|
|
|
if (old != widget_data->info.field.cpos) {
|
|
|
|
int length;
|
|
|
|
|
|
|
|
text = widget_data->cdata;
|
|
|
|
length = strlen(text + old) + 1;
|
|
|
|
memmove(text + widget_data->info.field.cpos, text + old, length);
|
|
|
|
}
|
2006-01-14 16:44:00 -05:00
|
|
|
goto display_field;
|
|
|
|
}
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2005-09-15 09:58:31 -04:00
|
|
|
if (widget_data->info.field.cpos) {
|
|
|
|
memmove(widget_data->cdata + widget_data->info.field.cpos - 1,
|
|
|
|
widget_data->cdata + widget_data->info.field.cpos,
|
|
|
|
strlen(widget_data->cdata) - widget_data->info.field.cpos + 1);
|
|
|
|
widget_data->info.field.cpos--;
|
|
|
|
}
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_DELETE:
|
|
|
|
{
|
|
|
|
int cdata_len = strlen(widget_data->cdata);
|
|
|
|
|
|
|
|
if (widget_data->info.field.cpos >= cdata_len) goto display_field;
|
|
|
|
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2007-05-20 08:31:02 -04:00
|
|
|
if (term->utf8_cp) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *end = widget_data->cdata + cdata_len;
|
|
|
|
char *text = widget_data->cdata + widget_data->info.field.cpos;
|
|
|
|
char *old = text;
|
2006-04-07 18:16:10 -04:00
|
|
|
|
2006-09-17 09:06:22 -04:00
|
|
|
utf8_to_unicode(&text, end);
|
2006-04-07 18:16:10 -04:00
|
|
|
if (old != text) {
|
|
|
|
memmove(old, text,
|
|
|
|
(int)(end - text) + 1);
|
|
|
|
}
|
2006-01-14 16:44:00 -05:00
|
|
|
goto display_field;
|
|
|
|
}
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* CONFIG_UTF8 */
|
2005-09-15 09:58:31 -04:00
|
|
|
memmove(widget_data->cdata + widget_data->info.field.cpos,
|
|
|
|
widget_data->cdata + widget_data->info.field.cpos + 1,
|
|
|
|
cdata_len - widget_data->info.field.cpos + 1);
|
|
|
|
goto display_field;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ACT_EDIT_KILL_TO_BOL:
|
|
|
|
memmove(widget_data->cdata,
|
|
|
|
widget_data->cdata + widget_data->info.field.cpos,
|
|
|
|
strlen(widget_data->cdata + widget_data->info.field.cpos) + 1);
|
|
|
|
widget_data->info.field.cpos = 0;
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_KILL_TO_EOL:
|
|
|
|
widget_data->cdata[widget_data->info.field.cpos] = 0;
|
|
|
|
goto display_field;
|
|
|
|
|
2006-06-23 00:07:52 -04:00
|
|
|
case ACT_EDIT_KILL_WORD_BACK:
|
|
|
|
{
|
|
|
|
int cdata_len = strlen(widget_data->cdata);
|
|
|
|
int start = widget_data->info.field.cpos;
|
|
|
|
|
|
|
|
while (start > 0 && isspace(widget_data->cdata[start - 1]))
|
|
|
|
--start;
|
|
|
|
while (start > 0 && !isspace(widget_data->cdata[start - 1]))
|
|
|
|
--start;
|
|
|
|
|
|
|
|
memmove(widget_data->cdata + start,
|
|
|
|
widget_data->cdata + widget_data->info.field.cpos,
|
|
|
|
cdata_len - widget_data->info.field.cpos + 1);
|
|
|
|
|
|
|
|
widget_data->info.field.cpos = start;
|
|
|
|
|
|
|
|
goto display_field;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ACT_EDIT_MOVE_BACKWARD_WORD:
|
|
|
|
while (widget_data->info.field.cpos > 0 && isspace(widget_data->cdata[widget_data->info.field.cpos - 1]))
|
|
|
|
--widget_data->info.field.cpos;
|
|
|
|
while (widget_data->info.field.cpos > 0 && !isspace(widget_data->cdata[widget_data->info.field.cpos - 1]))
|
|
|
|
--widget_data->info.field.cpos;
|
|
|
|
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_MOVE_FORWARD_WORD:
|
|
|
|
while (isspace(widget_data->cdata[widget_data->info.field.cpos]))
|
|
|
|
++widget_data->info.field.cpos;
|
|
|
|
while (widget_data->cdata[widget_data->info.field.cpos] && !isspace(widget_data->cdata[widget_data->info.field.cpos]))
|
|
|
|
++widget_data->info.field.cpos;
|
|
|
|
while (isspace(widget_data->cdata[widget_data->info.field.cpos]))
|
|
|
|
++widget_data->info.field.cpos;
|
|
|
|
|
|
|
|
goto display_field;
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
case ACT_EDIT_COPY_CLIPBOARD:
|
|
|
|
/* Copy to clipboard */
|
|
|
|
set_clipboard_text(widget_data->cdata);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
|
|
|
|
case ACT_EDIT_CUT_CLIPBOARD:
|
|
|
|
/* Cut to clipboard */
|
|
|
|
set_clipboard_text(widget_data->cdata);
|
|
|
|
widget_data->cdata[0] = 0;
|
|
|
|
widget_data->info.field.cpos = 0;
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_PASTE_CLIPBOARD:
|
|
|
|
{
|
|
|
|
/* Paste from clipboard */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *clipboard = get_clipboard_text();
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!clipboard) goto display_field;
|
|
|
|
|
|
|
|
safe_strncpy(widget_data->cdata, clipboard, widget_data->widget->datalen);
|
|
|
|
widget_data->info.field.cpos = strlen(widget_data->cdata);
|
|
|
|
mem_free(clipboard);
|
|
|
|
goto display_field;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ACT_EDIT_AUTO_COMPLETE:
|
|
|
|
if (!widget_has_history(widget_data))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
do_tab_compl(dlg_data, &widget_data->info.field.history);
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_AUTO_COMPLETE_FILE:
|
|
|
|
if (!widget_has_history(widget_data))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
do_tab_compl_file(dlg_data, &widget_data->info.field.history);
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_AUTO_COMPLETE_UNAMBIGUOUS:
|
|
|
|
if (!widget_has_history(widget_data))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
do_tab_compl_unambiguous(dlg_data, &widget_data->info.field.history);
|
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
case ACT_EDIT_REDRAW:
|
|
|
|
redraw_terminal_cls(term);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (check_kbd_textinput_key(ev)) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *text = widget_data->cdata;
|
2005-09-15 09:58:31 -04:00
|
|
|
int textlen = strlen(text);
|
2006-10-22 10:18:26 -04:00
|
|
|
#ifndef CONFIG_UTF8
|
|
|
|
/* Both get_kbd_key(ev) and @text
|
|
|
|
* are in the terminal's charset. */
|
2006-08-05 12:38:15 -04:00
|
|
|
const int inslen = 1;
|
2006-10-22 10:18:26 -04:00
|
|
|
#else /* CONFIG_UTF8 */
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *ins;
|
2006-10-22 10:18:26 -04:00
|
|
|
int inslen;
|
|
|
|
|
2007-05-20 08:31:02 -04:00
|
|
|
/* get_kbd_key(ev) is UCS-4, and @text
|
|
|
|
* is in the terminal's charset. */
|
|
|
|
ins = u2cp_no_nbsp(get_kbd_key(ev),
|
2008-12-28 10:42:29 -05:00
|
|
|
get_terminal_codepage(term));
|
2006-10-22 10:18:26 -04:00
|
|
|
inslen = strlen(ins);
|
|
|
|
#endif /* CONFIG_UTF8 */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-08-05 12:38:15 -04:00
|
|
|
if (textlen >= widget_data->widget->datalen - inslen)
|
2005-09-15 09:58:31 -04:00
|
|
|
goto display_field;
|
|
|
|
|
|
|
|
/* Shift to position of the cursor */
|
|
|
|
textlen -= widget_data->info.field.cpos;
|
2006-08-05 12:38:15 -04:00
|
|
|
text += widget_data->info.field.cpos;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-08-05 12:38:15 -04:00
|
|
|
memmove(text + inslen, text, textlen + 1);
|
2006-09-17 09:12:47 -04:00
|
|
|
#ifdef CONFIG_UTF8
|
2006-08-05 12:38:15 -04:00
|
|
|
memcpy(text, ins, inslen);
|
2006-09-17 09:12:47 -04:00
|
|
|
#else /* !CONFIG_UTF8 */
|
2006-08-05 12:38:15 -04:00
|
|
|
*text = get_kbd_key(ev);
|
2006-09-17 09:12:47 -04:00
|
|
|
#endif /* !CONFIG_UTF8 */
|
2006-08-05 12:38:15 -04:00
|
|
|
widget_data->info.field.cpos += inslen;
|
2005-09-15 09:58:31 -04:00
|
|
|
goto display_field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
display_field:
|
|
|
|
display_widget(dlg_data, widget_data);
|
2008-10-12 07:01:05 -04:00
|
|
|
redraw_windows(REDRAW_IN_FRONT_OF_WINDOW, dlg_data->win);
|
2005-09-15 09:58:31 -04:00
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
clear_field(struct dialog_data *dlg_data, struct widget_data *widget_data)
|
|
|
|
{
|
|
|
|
widget_data->info.field.cpos = 0;
|
|
|
|
|
|
|
|
if (widget_data->widget->datalen)
|
|
|
|
memset(widget_data->cdata, 0, widget_data->widget->datalen);
|
|
|
|
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
2007-01-28 07:41:40 -05:00
|
|
|
const struct widget_ops field_ops = {
|
2005-09-15 09:58:31 -04:00
|
|
|
display_field,
|
|
|
|
init_field,
|
|
|
|
mouse_field,
|
|
|
|
kbd_field,
|
|
|
|
NULL,
|
|
|
|
clear_field,
|
|
|
|
};
|
|
|
|
|
2007-01-28 07:41:40 -05:00
|
|
|
const struct widget_ops field_pass_ops = {
|
2005-09-15 09:58:31 -04:00
|
|
|
display_field_pass,
|
|
|
|
init_field,
|
|
|
|
mouse_field,
|
|
|
|
kbd_field,
|
|
|
|
NULL,
|
|
|
|
clear_field,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Input lines */
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_line_layouter(struct dialog_data *dlg_data)
|
|
|
|
{
|
|
|
|
struct input_line *input_line = dlg_data->dlg->udata;
|
|
|
|
struct session *ses = input_line->ses;
|
|
|
|
struct window *win = dlg_data->win;
|
|
|
|
int y = win->term->height - 1
|
|
|
|
- ses->status.show_status_bar
|
|
|
|
- ses->status.show_tabs_bar;
|
|
|
|
|
2008-09-07 12:27:06 -04:00
|
|
|
dlg_format_field(dlg_data, dlg_data->widgets_data, 0,
|
2006-03-06 00:01:12 -05:00
|
|
|
&y, win->term->width, NULL, ALIGN_LEFT, 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static widget_handler_status_T
|
|
|
|
input_line_event_handler(struct dialog_data *dlg_data)
|
|
|
|
{
|
|
|
|
struct input_line *input_line = dlg_data->dlg->udata;
|
|
|
|
input_line_handler_T handler = input_line->handler;
|
|
|
|
enum edit_action action_id;
|
|
|
|
struct widget_data *widget_data = dlg_data->widgets_data;
|
|
|
|
struct term_event *ev = dlg_data->term_event;
|
|
|
|
|
|
|
|
/* Noodle time */
|
|
|
|
switch (ev->ev) {
|
|
|
|
case EVENT_KBD:
|
|
|
|
action_id = kbd_action(KEYMAP_EDIT, ev, NULL);
|
|
|
|
|
|
|
|
/* Handle some basic actions such as quiting for empty buffers */
|
|
|
|
switch (action_id) {
|
|
|
|
case ACT_EDIT_ENTER:
|
|
|
|
case ACT_EDIT_NEXT_ITEM:
|
|
|
|
case ACT_EDIT_PREVIOUS_ITEM:
|
|
|
|
if (widget_has_history(widget_data))
|
|
|
|
add_to_input_history(widget_data->widget->info.field.history,
|
|
|
|
input_line->buffer, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_EDIT_BACKSPACE:
|
|
|
|
if (!*input_line->buffer)
|
|
|
|
goto cancel_input_line;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACT_EDIT_CANCEL:
|
|
|
|
goto cancel_input_line;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First let the input field do its business */
|
|
|
|
kbd_field(dlg_data, widget_data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_MOUSE:
|
|
|
|
#ifdef CONFIG_MOUSE
|
|
|
|
if (ev->info.mouse.y != dlg_data->win->y) {
|
|
|
|
delete_window_ev(dlg_data->win, ev);
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MOUSE */
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
case EVENT_REDRAW:
|
|
|
|
/* Try to catch the redraw event initiated by the history
|
|
|
|
* completion and only respond if something was actually
|
|
|
|
* updated. Meaning we have new data in the line buffer that
|
|
|
|
* should be propagated to the line handler. */
|
|
|
|
if (!widget_has_history(widget_data)
|
|
|
|
|| widget_data->info.field.cpos <= 0
|
|
|
|
|| widget_data->info.field.cpos <= strlen(input_line->buffer))
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
/* Fall thru */
|
|
|
|
|
|
|
|
case EVENT_RESIZE:
|
|
|
|
action_id = ACT_EDIT_REDRAW;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return EVENT_NOT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_dialog_data(dlg_data);
|
|
|
|
|
|
|
|
send_action_to_handler:
|
|
|
|
/* Then pass it on to the specialized handler */
|
|
|
|
switch (handler(input_line, action_id)) {
|
|
|
|
case INPUT_LINE_CANCEL:
|
|
|
|
cancel_input_line:
|
|
|
|
cancel_dialog(dlg_data, widget_data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INPUT_LINE_REWIND:
|
|
|
|
/* This is stolen kbd_field() handling for ACT_EDIT_BACKSPACE */
|
|
|
|
memmove(widget_data->cdata + widget_data->info.field.cpos - 1,
|
|
|
|
widget_data->cdata + widget_data->info.field.cpos,
|
|
|
|
strlen(widget_data->cdata) - widget_data->info.field.cpos + 1);
|
|
|
|
widget_data->info.field.cpos--;
|
|
|
|
|
|
|
|
update_dialog_data(dlg_data);
|
|
|
|
|
2008-12-27 04:46:21 -05:00
|
|
|
/* Set action_id to -2 to signal to the handler that it should
|
|
|
|
* not report errors or take any action except to search. */
|
|
|
|
action_id = -2;
|
2005-09-15 09:58:31 -04:00
|
|
|
goto send_action_to_handler;
|
|
|
|
|
|
|
|
case INPUT_LINE_PROCEED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hack: We want our caller to perform its redrawing routine,
|
|
|
|
* even if we did process the event here. */
|
|
|
|
if (action_id == ACT_EDIT_REDRAW) return EVENT_NOT_PROCESSED;
|
|
|
|
|
|
|
|
/* Completely bypass any further dialog event handling */
|
|
|
|
return EVENT_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
input_field_line(struct session *ses, char *prompt, void *data,
|
2005-09-15 09:58:31 -04:00
|
|
|
struct input_history *history, input_line_handler_T handler)
|
|
|
|
{
|
|
|
|
struct dialog *dlg;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *buffer;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct input_line *input_line;
|
|
|
|
|
|
|
|
assert(ses);
|
|
|
|
|
|
|
|
dlg = calloc_dialog(INPUT_LINE_WIDGETS, sizeof(*input_line));
|
|
|
|
if (!dlg) return;
|
|
|
|
|
|
|
|
input_line = (void *) get_dialog_offset(dlg, INPUT_LINE_WIDGETS);
|
|
|
|
input_line->ses = ses;
|
|
|
|
input_line->data = data;
|
|
|
|
input_line->handler = handler;
|
|
|
|
buffer = input_line->buffer;
|
|
|
|
|
|
|
|
dlg->handle_event = input_line_event_handler;
|
|
|
|
dlg->layouter = input_line_layouter;
|
|
|
|
dlg->layout.only_widgets = 1;
|
|
|
|
dlg->udata = input_line;
|
|
|
|
|
|
|
|
add_dlg_field_float2(dlg, prompt, 0, 0, NULL, INPUT_LINE_BUFFER_SIZE,
|
|
|
|
buffer, history);
|
|
|
|
|
2007-03-11 06:41:17 -04:00
|
|
|
do_dialog(ses->tab->term, dlg, getml(dlg, (void *) NULL));
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|