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

196 lines
4.2 KiB
C
Raw Permalink Normal View History

/* Prefabricated message box implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdarg.h>
#include "elinks.h"
#include "bfu/dialog.h"
#include "bfu/button.h"
#include "bfu/msgbox.h"
#include "bfu/text.h"
#include "intl/libintl.h"
#include "terminal/terminal.h"
#include "util/color.h"
#include "util/memlist.h"
#include "util/memory.h"
#include "util/snprintf.h"
#include "util/string.h"
struct dialog_data *
2022-01-28 15:41:23 +00:00
msg_box(struct terminal *term, struct memory_list *ml, msgbox_flags_T flags,
char *title, format_align_T align,
char *text, void *udata, int buttons, ...)
{
struct dialog *dlg;
va_list ap;
/* Check if the info string is valid. */
if (!text || buttons < 0) return NULL;
/* Use the @flags to determine whether @text should be free()d. */
if (flags & MSGBOX_FREE_TEXT)
add_one_to_ml(&ml, text);
/* Use the @flags to determine whether strings should be l18n'd. */
if (!(flags & MSGBOX_NO_INTL)) {
title = _(title, term);
if (!(flags & MSGBOX_FREE_TEXT)
&& !(flags & MSGBOX_NO_TEXT_INTL))
text = _(text, term);
/* Button labels will be gettextized as will they be extracted
* from @ap. */
}
dlg = calloc_dialog(buttons + 1, 0);
if (!dlg) {
freeml(ml);
return NULL;
}
add_one_to_ml(&ml, dlg);
dlg->title = title;
dlg->layouter = generic_dialog_layouter;
dlg->layout.padding_top = 1;
dlg->udata2 = udata;
if (flags & MSGBOX_SCROLLABLE)
dlg->widgets->info.text.is_scrollable = 1;
add_dlg_text(dlg, text, align, 0);
va_start(ap, buttons);
while (dlg->number_of_widgets < buttons + 1) {
char *label;
done_handler_T *done;
int bflags;
label = va_arg(ap, char *);
done = va_arg(ap, done_handler_T *);
bflags = va_arg(ap, int);
if (!label) {
/* Skip this button. */
buttons--;
continue;
}
if (!(flags & MSGBOX_NO_INTL))
label = _(label, term);
add_dlg_ok_button(dlg, label, bflags, done, udata);
}
va_end(ap);
add_dlg_end(dlg, buttons + 1);
return do_dialog(term, dlg, ml);
}
static inline char *
msg_text_do(char *format, va_list ap)
{
char *info;
int infolen, len;
va_list ap2;
va_copy(ap2, ap);
infolen = vsnprintf(NULL, 0, format, ap2);
2022-01-16 18:09:27 +00:00
info = (char *)mem_alloc(infolen + 1);
if (!info) return NULL;
len = vsnprintf((char *) info, infolen + 1, format, ap);
if (len != infolen) {
mem_free(info);
return NULL;
}
/* Wear safety boots */
info[infolen] = '\0';
return info;
}
char *
2021-03-03 13:38:11 +00:00
msg_text(struct terminal *term, const char *format, ...)
{
char *info;
va_list ap;
va_start(ap, format);
info = msg_text_do(_(format, term), ap);
va_end(ap);
return info;
}
static void
abort_refreshed_msg_box_handler(struct dialog_data *dlg_data)
{
void *data = dlg_data->dlg->widgets->text;
if (dlg_data->dlg->udata != data)
mem_free(data);
}
static enum dlg_refresh_code
refresh_msg_box(struct dialog_data *dlg_data, void *data)
{
2022-01-24 20:53:18 +00:00
char *(*get_info)(struct terminal *, void *) = (char *(*)(struct terminal *, void *))data;
void *msg_data = dlg_data->dlg->udata2;
char *info = get_info(dlg_data->win->term, msg_data);
if (!info) return REFRESH_CANCEL;
abort_refreshed_msg_box_handler(dlg_data);
dlg_data->dlg->widgets->text = info;
return REFRESH_DIALOG;
}
void
2022-01-28 15:41:23 +00:00
refreshed_msg_box(struct terminal *term, msgbox_flags_T flags,
char *title, format_align_T align,
char *(get_info)(struct terminal *, void *),
void *data)
{
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(refreshed_msg_box)] */
struct dialog_data *dlg_data;
char *info = get_info(term, data);
if (!info) return;
dlg_data = msg_box(term, NULL, flags | MSGBOX_FREE_TEXT,
title, align,
info,
data, 1,
MSG_BOX_BUTTON(N_("~OK"), NULL, B_ENTER | B_ESC));
if (!dlg_data) return;
/* Save the original text to check up on it when the dialog
* is freed. */
dlg_data->dlg->udata = info;
dlg_data->dlg->abort = abort_refreshed_msg_box_handler;
2022-02-21 14:54:02 +00:00
refresh_dialog(dlg_data, refresh_msg_box, (void *)get_info);
}
struct dialog_data *
2022-01-28 15:41:23 +00:00
info_box(struct terminal *term, msgbox_flags_T flags,
char *title, format_align_T align,
char *text)
{
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(info_box)] */
return msg_box(term, NULL, flags,
title, align,
text,
NULL, 1,
MSG_BOX_BUTTON(N_("~OK"), NULL, B_ENTER | B_ESC));
}