1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-25 01:05:37 +00:00
elinks/src/bfu/hierbox.c

1001 lines
24 KiB
C
Raw Normal View History

/* Hiearchic listboxes browser dialog commons */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdarg.h>
#include "elinks.h"
#include "bfu/button.h"
#include "bfu/dialog.h"
#include "bfu/hierbox.h"
#include "bfu/inpfield.h"
#include "bfu/listbox.h"
#include "bfu/msgbox.h"
#include "bfu/text.h"
#include "config/kbdbind.h"
#include "dialogs/menu.h"
#include "intl/libintl.h"
#include "protocol/uri.h"
#include "session/task.h"
#include "terminal/screen.h"
#include "terminal/tab.h"
#include "terminal/terminal.h"
void
update_hierbox_browser(struct hierbox_browser *browser)
{
struct hierbox_dialog_list_item *item;
foreach (item, browser->dialogs) {
redraw_windows(REDRAW_WINDOW_AND_FRONT, item->dlg_data->win);
}
}
/* Common backend for listbox adding */
struct listbox_item *
add_listbox_item(struct hierbox_browser *browser, struct listbox_item *root,
enum listbox_item_type type, void *data, int add_position)
{
struct listbox_item *item;
if (!root) {
assertm(browser != NULL, "Nowhere to add new list box item");
root = &browser->root;
}
2022-01-16 20:08:50 +00:00
item = (struct listbox_item *)mem_calloc(1, sizeof(*item));
if (!item) return NULL;
init_list(item->child);
item->visible = 1;
item->udata = data;
item->type = type;
item->depth = root->depth + 1;
/* TODO: Possibility to sort by making add_position into a flag */
if (add_position < 0)
add_to_list_end(root->child, item);
else
add_to_list(root->child, item);
if (browser) update_hierbox_browser(browser);
return item;
}
/* Find a listbox item to replace @item. This is done by trying first to
* traverse down then up, and if both traversals end up returning the @item
* (that is, it is the last item in the box), return NULL. */
static inline struct listbox_item *
replace_listbox_item(struct listbox_item *item, struct listbox_data *data)
{
struct listbox_item *new_item;
new_item = traverse_listbox_items_list(item, data, 1, 1, NULL, NULL);
if (item != new_item) return new_item;
new_item = traverse_listbox_items_list(item, data, -1, 1, NULL, NULL);
return (item == new_item) ? NULL : new_item;
}
void
done_listbox_item(struct hierbox_browser *browser, struct listbox_item *item)
{
struct listbox_data *box_data;
assert(item && list_empty(item->child));
if_assert_failed return;
/* The option dialog needs this test */
if (item->next) {
/* If we are removing the top or the selected box
* we have to figure out a replacement. */
foreach (box_data, browser->boxes) {
if (box_data->sel == item)
box_data->sel = replace_listbox_item(item,
box_data);
if (box_data->top == item)
box_data->top = replace_listbox_item(item,
box_data);
}
del_from_list(item);
if (item->visible) update_hierbox_browser(browser);
}
mem_free(item);
}
static void
recursively_set_expanded(struct listbox_item *item, int expanded)
{
struct listbox_item *child;
if (item->type != BI_FOLDER)
return;
item->expanded = expanded;
foreach (child, item->child)
recursively_set_expanded(child, expanded);
}
static widget_handler_status_T
hierbox_ev_kbd(struct dialog_data *dlg_data)
{
2022-01-24 20:53:18 +00:00
struct hierbox_browser *browser = (struct hierbox_browser *)dlg_data->dlg->udata2;
struct widget_data *widget_data = dlg_data->widgets_data;
struct widget *widget = widget_data->widget;
struct listbox_data *box;
struct listbox_item *selected;
2022-01-28 13:05:46 +00:00
action_id_T action_id;
struct term_event *ev = dlg_data->term_event;
/* Check if listbox has something to say to this */
if (widget->ops->kbd
&& widget->ops->kbd(dlg_data, widget_data)
== EVENT_PROCESSED)
return EVENT_PROCESSED;
box = get_dlg_listbox_data(dlg_data);
selected = box->sel;
action_id = kbd_action(KEYMAP_MENU, ev, NULL);
2006-01-05 10:41:26 +00:00
switch (action_id) {
case ACT_MENU_SELECT:
if (!selected) return EVENT_PROCESSED;
if (selected->type != BI_FOLDER)
return EVENT_NOT_PROCESSED;
selected->expanded = !selected->expanded;
2006-01-05 10:41:26 +00:00
break;
2006-01-05 10:41:26 +00:00
case ACT_MENU_UNEXPAND:
/* Recursively unexpand all folders */
if (!selected) return EVENT_PROCESSED;
/* Special trick: if the folder is already
* folded, jump to the parent folder, so the
* next time when user presses the key, the
* whole parent folder will be closed. */
if (list_empty(selected->child)
|| !selected->expanded) {
2006-01-05 10:41:26 +00:00
struct listbox_item *root;
2006-01-05 10:41:26 +00:00
root = box->ops->get_root(selected);
if (root) {
listbox_sel(widget_data, root);
}
} else if (selected->type == BI_FOLDER) {
recursively_set_expanded(selected, 0);
}
2006-01-05 10:41:26 +00:00
break;
2006-01-05 10:41:26 +00:00
case ACT_MENU_EXPAND:
/* Recursively expand all folders */
if (!selected || selected->type != BI_FOLDER)
return EVENT_PROCESSED;
recursively_set_expanded(selected, 1);
2006-01-05 10:41:26 +00:00
break;
2006-01-05 10:41:26 +00:00
case ACT_MENU_SEARCH:
if (!box->ops->match)
return EVENT_NOT_PROCESSED;
push_hierbox_search_button(dlg_data, NULL);
return EVENT_PROCESSED;
2006-01-05 10:41:26 +00:00
default:
return EVENT_NOT_PROCESSED;
}
if (browser->expansion_callback)
browser->expansion_callback();
display_widget(dlg_data, widget_data);
return EVENT_PROCESSED;
}
static widget_handler_status_T
hierbox_ev_init(struct dialog_data *dlg_data)
{
2022-01-24 20:53:18 +00:00
struct hierbox_browser *browser = (struct hierbox_browser *)dlg_data->dlg->udata2;
struct hierbox_dialog_list_item *item;
struct listbox_item *litem;
/* If we fail here it only means automatic updating
* will not be possible so no need to panic. */
2022-01-16 18:09:27 +00:00
item = (struct hierbox_dialog_list_item *)mem_alloc(sizeof(*item));
if (item) {
item->dlg_data = dlg_data;
add_to_list(browser->dialogs, item);
}
foreach (litem, browser->root.child) {
litem->visible = 1;
}
/* Return this so that the generic dialog code will run and initialise
* the widgets and stuff. */
return EVENT_NOT_PROCESSED;
}
static widget_handler_status_T
hierbox_ev_abort(struct dialog_data *dlg_data)
{
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
2022-01-24 20:53:18 +00:00
struct hierbox_browser *browser = (struct hierbox_browser *)dlg_data->dlg->udata2;
struct hierbox_dialog_list_item *item;
/* Save state and delete the box structure */
if (!browser->do_not_save_state)
copy_struct(&browser->box_data, box);
del_from_list(box);
/* Delete the dialog list entry */
foreach (item, browser->dialogs) {
if (item->dlg_data == dlg_data) {
del_from_list(item);
mem_free(item);
break;
}
}
/* Return this so that the generic dialog code will run and initialise
* the widgets and stuff. */
return EVENT_NOT_PROCESSED;
}
/* We install own dialog event handler, so that we can give the listbox widget
* an early chance to catch the event. Basically, the listbox widget is itself
* unselectable, instead one of the buttons below is always active. So, we
* always first let the listbox catch the keypress and handle it, and if it
* doesn't care, we pass it on to the button. */
static widget_handler_status_T
hierbox_dialog_event_handler(struct dialog_data *dlg_data)
{
struct term_event *ev = dlg_data->term_event;
switch (ev->ev) {
case EVENT_KBD:
return hierbox_ev_kbd(dlg_data);
case EVENT_INIT:
return hierbox_ev_init(dlg_data);
case EVENT_RESIZE:
case EVENT_REDRAW:
case EVENT_MOUSE:
return EVENT_NOT_PROCESSED;
case EVENT_ABORT:
return hierbox_ev_abort(dlg_data);
}
return EVENT_NOT_PROCESSED;
}
struct dialog_data *
hierbox_browser(struct hierbox_browser *browser, struct session *ses)
{
struct terminal *term = ses->tab->term;
struct listbox_data *listbox_data;
struct dialog *dlg;
int button = browser->buttons_size + 2;
int anonymous = get_cmd_opt_bool("anonymous");
assert(ses);
dlg = calloc_dialog(button, sizeof(*listbox_data));
if (!dlg) return NULL;
listbox_data = (struct listbox_data *) get_dialog_offset(dlg, button);
dlg->title = _(browser->title, term);
dlg->layouter = generic_dialog_layouter;
dlg->layout.maximize_width = 1;
dlg->layout.padding_top = 1;
dlg->handle_event = hierbox_dialog_event_handler;
dlg->udata = ses;
dlg->udata2 = browser;
add_dlg_listbox(dlg, listbox_data);
for (button = 0; button < browser->buttons_size; button++) {
const struct hierbox_browser_button *but = &browser->buttons[button];
/* Skip buttons that should not be displayed in anonymous mode */
if (anonymous && !but->anonymous) {
anonymous++;
continue;
}
add_dlg_button(dlg, _(but->label, term), B_ENTER, but->handler, NULL);
}
add_dlg_button(dlg, _("Close", term), B_ESC, cancel_dialog, NULL);
/* @anonymous was initially 1 if we are running in anonymous mode so we
* have to subtract one. */
add_dlg_end(dlg, button + 2 - (anonymous ? anonymous - 1 : 0));
return do_dialog(term, dlg, getml(dlg, (void *) NULL));
}
/* Action info management */
static int
scan_for_marks(struct listbox_item *item, void *info_, int *offset)
{
if (item->marked) {
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)info_;
context->item = NULL;
*offset = 0;
}
return 0;
}
static int
scan_for_used(struct listbox_item *item, void *info_, int *offset)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)info_;
if (context->box->ops->is_used(item)) {
context->item = item;
*offset = 0;
}
return 0;
}
static struct listbox_context *
init_listbox_context(struct listbox_data *box, struct terminal *term,
struct listbox_item *item,
int (*scanner)(struct listbox_item *, void *, int *))
{
struct listbox_context *context;
2022-01-16 20:08:50 +00:00
context = (struct listbox_context *)mem_calloc(1, sizeof(*context));
if (!context) return NULL;
context->item = item;
context->term = term;
context->box = box;
if (!scanner) return context;
/* Look if it wouldn't be more interesting to blast off the marked
* item. */
assert(!list_empty(*box->items));
2022-01-24 20:53:18 +00:00
traverse_listbox_items_list((struct listbox_item *)box->items->next, box, 0, 0,
scanner, context);
return context;
}
static void
done_listbox_context(void *context_)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)context_;
if (context->item)
context->box->ops->unlock(context->item);
}
/* Info action */
widget_handler_status_T
push_hierbox_info_button(struct dialog_data *dlg_data, struct widget_data *button)
{
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 23:38:29 +00:00
/* [gettext_accelerator_context(push_hierbox_info_button)] */
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
struct listbox_item *item = box->sel;
struct terminal *term = dlg_data->win->term;
struct listbox_context *context;
char *msg;
if (!item) return EVENT_PROCESSED;
assert(box->ops);
context = init_listbox_context(box, term, item, NULL);
if (!context) return EVENT_PROCESSED;
msg = box->ops->get_info(item, term);
if (!msg) {
mem_free(context);
if (item->type == BI_FOLDER) {
info_box(term, 0, N_("Info"), ALIGN_CENTER,
N_("Press space to expand this folder."));
}
return EVENT_PROCESSED;
}
box->ops->lock(item);
msg_box(term, getml(context, (void *) NULL), MSGBOX_FREE_TEXT /* | MSGBOX_SCROLLABLE */,
N_("Info"), ALIGN_LEFT,
msg,
context, 1,
MSG_BOX_BUTTON(N_("~OK"), done_listbox_context, B_ESC | B_ENTER));
return EVENT_PROCESSED;
}
/* Goto action */
static void recursively_goto_each_listbox(struct session *ses,
struct listbox_item *root,
struct listbox_data *box);
static void
recursively_goto_listbox(struct session *ses, struct listbox_item *item,
struct listbox_data *box)
{
2006-01-01 03:02:44 +00:00
if (item->type == BI_FOLDER) {
recursively_goto_each_listbox(ses, item, box);
return;
2006-01-01 03:02:44 +00:00
} else if (item->type == BI_LEAF) {
struct uri *uri = box->ops->get_uri(item);
2006-01-01 03:02:44 +00:00
if (!uri) return;
2006-01-01 03:02:44 +00:00
open_uri_in_new_tab(ses, uri, 1, 0);
done_uri(uri);
}
}
static void
recursively_goto_each_listbox(struct session *ses, struct listbox_item *root,
struct listbox_data *box)
{
struct listbox_item *item;
foreach (item, root->child) {
recursively_goto_listbox(ses, item, box);
}
}
static int
goto_marked(struct listbox_item *item, void *data_, int *offset)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)data_;
if (item->marked) {
2022-01-24 20:53:18 +00:00
struct session *ses = (struct session *)context->dlg_data->dlg->udata;
struct listbox_data *box = context->box;
recursively_goto_listbox(ses, item, box);
}
return 0;
}
widget_handler_status_T
push_hierbox_goto_button(struct dialog_data *dlg_data,
struct widget_data *button)
{
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
struct listbox_item *item = box->sel;
2022-01-24 20:53:18 +00:00
struct session *ses = (struct session *)dlg_data->dlg->udata;
struct terminal *term = dlg_data->win->term;
struct listbox_context *context;
if (!item) return EVENT_PROCESSED;
context = init_listbox_context(box, term, item, scan_for_marks);
if (!context) return EVENT_PROCESSED;
if (!context->item) {
context->dlg_data = dlg_data;
2022-01-24 20:53:18 +00:00
traverse_listbox_items_list((struct listbox_item *)context->box->items->next,
context->box, 0, 0,
goto_marked, context);
} else if (item->type == BI_FOLDER) {
recursively_goto_each_listbox(ses, item, box);
} else if (item->type == BI_LEAF) {
struct uri *uri = box->ops->get_uri(item);
if (uri) {
int how = get_opt_int("document.browse.links.hierbox_goto", ses);
switch (how) {
default:
case 0:
goto_uri(ses, uri);
break;
case 1:
open_uri_in_new_tab(ses, uri, 0, 0);
break;
case 2:
open_uri_in_new_tab(ses, uri, 1, 0);
break;
case 3:
open_uri_in_new_window(ses, uri, NULL, ENV_ANY, CACHE_MODE_NORMAL, TASK_NONE);
break;
}
done_uri(uri);
}
} else {
mem_free(context);
return EVENT_PROCESSED;
}
mem_free(context);
/* Close the dialog */
delete_window(dlg_data->win);
return EVENT_PROCESSED;
}
/* Delete action */
enum delete_error {
DELETE_IMPOSSIBLE = 0,
DELETE_LOCKED,
DELETE_ERRORS,
};
static const struct listbox_ops_messages default_listbox_ops_messages = {
/* cant_delete_item */
N_("Sorry, but the item \"%s\" cannot be deleted."),
/* cant_delete_used_item */
N_("Sorry, but the item \"%s\" is being used by something else."),
/* cant_delete_folder */
N_("Sorry, but the folder \"%s\" cannot be deleted."),
/* cant_delete_used_folder */
N_("Sorry, but the folder \"%s\" is being used by something else."),
/* delete_marked_items_title */
2006-01-01 05:01:16 +00:00
N_("Delete marked items"),
/* delete_marked_items */
2006-01-01 05:01:16 +00:00
N_("Delete marked items?"),
/* delete_folder_title */
2006-01-01 05:01:16 +00:00
N_("Delete folder"),
/* delete_folder */
2006-01-01 05:01:16 +00:00
N_("Delete the folder \"%s\" and its content?"),
/* delete_item_title */
2006-01-01 05:01:16 +00:00
N_("Delete item"),
/* delete_item */
2006-01-01 05:01:16 +00:00
N_("Delete \"%s\"?\n\n%s"),
/* clear_all_items_title */
2006-01-01 05:01:16 +00:00
N_("Clear all items"),
/* clear_all_items */
2006-01-01 05:01:16 +00:00
N_("Do you really want to remove all items?"),
};
#define listbox_message(msg) \
ops->messages && ops->messages->msg \
? ops->messages->msg \
: default_listbox_ops_messages.msg
static void
print_delete_error(struct listbox_item *item, struct terminal *term,
const struct listbox_ops *ops, enum delete_error err)
{
struct string msg;
char *errmsg;
char *text;
switch (err) {
case DELETE_IMPOSSIBLE:
if (item->type == BI_FOLDER) {
errmsg = listbox_message(cant_delete_folder);
} else {
errmsg = listbox_message(cant_delete_item);
}
break;
case DELETE_LOCKED:
if (item->type == BI_FOLDER) {
errmsg = listbox_message(cant_delete_used_folder);
} else {
errmsg = listbox_message(cant_delete_used_item);
}
break;
default:
INTERNAL("Bad delete error code (%d)!", err);
return;
}
text = ops->get_text(item, term);
if (!text || !init_string(&msg)) {
mem_free_if(text);
return;
}
add_format_to_string(&msg, _(errmsg, term), text);
mem_free(text);
if (item->type == BI_LEAF) {
char *info = ops->get_info(item, term);
if (info) {
add_format_to_string(&msg, "\n\n%s", info);
mem_free(info);
}
}
info_box(term, MSGBOX_FREE_TEXT, N_("Delete error"), ALIGN_LEFT,
msg.source);
}
static void
do_delete_item(struct listbox_item *item, struct listbox_context *info,
int last)
{
const struct listbox_ops *ops = info->box->ops;
assert(item);
if (!ops->can_delete(item)) {
print_delete_error(item, info->term, ops, DELETE_IMPOSSIBLE);
return;
}
if (ops->is_used(item)) {
print_delete_error(item, info->term, ops, DELETE_LOCKED);
return;
}
ops->delete_(item, last);
}
static int
delete_marked(struct listbox_item *item, void *data_, int *offset)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)data_;
if (item->marked && !context->box->ops->is_used(item)) {
/* Save the first marked so it can be deleted last */
if (!context->item) {
context->item = item;
} else {
do_delete_item(item, context, 0);
}
return 1;
}
return 0;
}
static void
push_ok_delete_button(void *context_)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)context_;
struct listbox_item *root;
int last = 0;
if (context->item) {
context->box->ops->unlock(context->item);
} else {
2022-01-24 20:53:18 +00:00
traverse_listbox_items_list((struct listbox_item *)context->box->items->next,
context->box, 0, 0,
delete_marked, context);
if (!context->item) return;
}
root = context->box->ops->get_root(context->item);
if (root) {
last = context->item == root->child.prev;
}
/* Delete the last one (traversal should save one to delete) */
do_delete_item(context->item, context, 1);
/* If removing the last item in a folder move focus to previous item in
* the folder or the root. */
if (last)
listbox_sel_move(context->widget_data, -1);
}
static widget_handler_status_T
query_delete_selected_item(void *context_)
{
/* [gettext_accelerator_context(query_delete_selected_item)] */
2022-01-24 20:53:18 +00:00
struct listbox_context *context, *oldcontext = (struct listbox_context *)context_;
struct terminal *term = oldcontext->term;
struct listbox_data *box = oldcontext->box;
const struct listbox_ops *ops = box->ops;
struct listbox_item *item = box->sel;
char *text;
enum delete_error delete_;
assert(item);
delete_ = ops->can_delete(item) ? DELETE_LOCKED : DELETE_IMPOSSIBLE;
if (delete_ == DELETE_IMPOSSIBLE || ops->is_used(item)) {
print_delete_error(item, term, ops, delete_);
return EVENT_PROCESSED;
}
context = init_listbox_context(box, term, item, NULL);
if (!context) return EVENT_PROCESSED;
context->widget_data = oldcontext->widget_data;
text = ops->get_text(item, term);
if (!text) {
mem_free(context);
return EVENT_PROCESSED;
}
if (item->type == BI_FOLDER) {
ops->lock(item);
msg_box(term, getml(context, (void *) NULL), MSGBOX_FREE_TEXT,
listbox_message(delete_folder_title), ALIGN_CENTER,
msg_text(term, listbox_message(delete_folder), text),
context, 2,
MSG_BOX_BUTTON(N_("~Yes"), push_ok_delete_button, B_ENTER),
MSG_BOX_BUTTON(N_("~No"), done_listbox_context, B_ESC));
} else {
char *msg = ops->get_info(item, term);
ops->lock(item);
msg_box(term, getml(context, (void *) NULL), MSGBOX_FREE_TEXT,
listbox_message(delete_item_title), ALIGN_LEFT,
msg_text(term, listbox_message(delete_item),
text, empty_string_or_(msg)),
context, 2,
MSG_BOX_BUTTON(N_("~Yes"), push_ok_delete_button, B_ENTER),
MSG_BOX_BUTTON(N_("~No"), done_listbox_context, B_ESC));
mem_free_if(msg);
}
mem_free(text);
return EVENT_PROCESSED;
}
static void
dont_delete_marked_items(void *const context_)
{
query_delete_selected_item(context_);
}
widget_handler_status_T
push_hierbox_delete_button(struct dialog_data *dlg_data,
struct widget_data *button)
{
/* [gettext_accelerator_context(push_hierbox_delete_button)] */
struct terminal *term = dlg_data->win->term;
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
const struct listbox_ops *ops = box->ops;
struct listbox_item *item = box->sel;
struct listbox_context *context;
if (!item) return EVENT_PROCESSED;
assert(ops && ops->can_delete && ops->delete_);
context = init_listbox_context(box, term, item, scan_for_marks);
if (!context) return EVENT_PROCESSED;
context->widget_data = dlg_data->widgets_data;
if (context->item) {
widget_handler_status_T status;
status = query_delete_selected_item(context);
mem_free(context);
return status;
}
msg_box(term, getml(context, (void *) NULL), 0,
listbox_message(delete_marked_items_title), ALIGN_CENTER,
listbox_message(delete_marked_items),
context, 2,
MSG_BOX_BUTTON(N_("~Yes"), push_ok_delete_button, B_ENTER),
MSG_BOX_BUTTON(N_("~No"), dont_delete_marked_items, B_ESC));
return EVENT_PROCESSED;
}
/* Clear action */
static int
delete_unused(struct listbox_item *item, void *data_, int *offset)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)data_;
if (context->box->ops->is_used(item)) return 0;
do_delete_item(item, context, 0);
return 1;
}
static void
do_clear_browser(void *context_)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)context_;
2022-01-24 20:53:18 +00:00
traverse_listbox_items_list((struct listbox_item *)context->box->items->next,
context->box, 0, 0,
delete_unused, context);
}
widget_handler_status_T
push_hierbox_clear_button(struct dialog_data *dlg_data,
struct widget_data *button)
{
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 23:38:29 +00:00
/* [gettext_accelerator_context(push_hierbox_clear_button)] */
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
const struct listbox_ops *ops = box->ops;
struct terminal *term = dlg_data->win->term;
struct listbox_context *context;
if (!box->sel) return EVENT_PROCESSED;
assert(ops);
context = init_listbox_context(box, term, NULL, scan_for_used);
if (!context) return EVENT_PROCESSED;
if (context->item) {
/* FIXME: If the clear button should be used for browsers where
* not all items can be deleted scan_for_used() should also can
* for undeletable and we should be able to pass either delete
* error types. */
print_delete_error(context->item, term, ops, DELETE_LOCKED);
mem_free(context);
return EVENT_PROCESSED;
}
msg_box(term, getml(context, (void *) NULL), 0,
listbox_message(clear_all_items_title), ALIGN_CENTER,
listbox_message(clear_all_items),
context, 2,
MSG_BOX_BUTTON(N_("~Yes"), do_clear_browser, B_ENTER),
MSG_BOX_BUTTON(N_("~No"), NULL, B_ESC));
return EVENT_PROCESSED;
}
2006-01-01 05:01:16 +00:00
#undef listbox_message
/* Search action */
static int
scan_for_matches(struct listbox_item *item, void *info_, int *offset)
{
2022-01-24 20:53:18 +00:00
struct listbox_context *context = (struct listbox_context *)info_;
char *text = (char *)context->widget_data;
if (!*text) {
item->visible = 1;
return 0;
}
switch (context->box->ops->match(item, context->term, text)) {
case LISTBOX_MATCH_OK:
/* Mark that we have a match by setting the item to non-NULL */
context->item = item;
item->visible = 1;
break;
case LISTBOX_MATCH_NO:
item->visible = 0;
break;
case LISTBOX_MATCH_IMPOSSIBLE:
break;
}
return 0;
}
static int
mark_visible(struct listbox_item *item, void *xxx, int *offset)
{
item->visible = 1;
return 0;
}
static void
search_hierbox_browser(void *data, char *text)
{
2022-01-24 20:53:18 +00:00
struct dialog_data *dlg_data = (struct dialog_data *)data;
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
struct terminal *term = dlg_data->win->term;
struct listbox_context *context;
context = init_listbox_context(box, term, NULL, NULL);
if (!context) return;
/* Eeew :/ */
2022-01-24 20:53:18 +00:00
context->widget_data = (struct widget_data *) text;
2022-01-24 20:53:18 +00:00
traverse_listbox_items_list((struct listbox_item *)box->items->next, box, 0, 0,
scan_for_matches, context);
if (!context->item && *text) {
switch (get_opt_int("document.browse.search.show_not_found",
NULL)) {
case 2:
info_box(term, MSGBOX_FREE_TEXT,
N_("Search"), ALIGN_CENTER,
msg_text(term,
N_("Search string '%s' not found"),
text));
break;
case 1:
beep_terminal(term);
default:
break;
}
2022-01-24 20:53:18 +00:00
traverse_listbox_items_list((struct listbox_item *)box->items->next, box, 0, 0,
mark_visible, NULL);
}
mem_free(context);
}
widget_handler_status_T
push_hierbox_search_button(struct dialog_data *dlg_data,
struct widget_data *button)
{
struct terminal *term = dlg_data->win->term;
struct listbox_data *box = get_dlg_listbox_data(dlg_data);
if (!box->sel) return EVENT_PROCESSED;
assert(box->ops->match);
input_dialog(term, NULL, N_("Search"), N_("Name"),
dlg_data, NULL,
MAX_STR_LEN, "", 0, 0, NULL,
search_hierbox_browser, NULL);
return EVENT_PROCESSED;
}