Merge branch '2115_input_dialog_buttons'

* 2115_input_dialog_buttons:
  Applied MC indentation policy.
  Ticket #2115: fixed button location in common input dialog.
This commit is contained in:
Andrew Borodin 2010-06-28 16:23:00 +04:00
commit 2e973fbbbd
2 changed files with 465 additions and 429 deletions

View File

@ -49,8 +49,7 @@ Listbox *
create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
const char *title, const char *help)
{
const int listbox_colors[DLG_COLOR_NUM] =
{
const int listbox_colors[DLG_COLOR_NUM] = {
MENU_ENTRY_COLOR,
MENU_SELECTED_COLOR,
MENU_HOT_COLOR,
@ -65,7 +64,8 @@ create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
/* Adjust sizes */
lines = min (lines, LINES - 6);
if (title != NULL) {
if (title != NULL)
{
len = str_term_width1 (title) + 4;
cols = max (cols, len);
}
@ -73,16 +73,19 @@ create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
cols = min (cols, COLS - 6);
/* adjust position */
if ((center_y < 0) || (center_x < 0)) {
ypos = LINES/2;
xpos = COLS/2;
} else {
if ((center_y < 0) || (center_x < 0))
{
ypos = LINES / 2;
xpos = COLS / 2;
}
else
{
ypos = center_y;
xpos = center_x;
}
ypos -= lines/2;
xpos -= cols/2;
ypos -= lines / 2;
xpos -= cols / 2;
if (ypos + lines >= LINES)
ypos = LINES - lines - space;
@ -98,8 +101,7 @@ create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
listbox->dlg =
create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
listbox_colors, NULL,
help, title, DLG_REVERSE | DLG_TRYUP);
listbox_colors, NULL, help, title, DLG_REVERSE | DLG_TRYUP);
listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
add_widget (listbox->dlg, listbox->list);
@ -115,7 +117,7 @@ create_listbox_window (int lines, int cols, const char *title, const char *help)
/* Returns the number of the item selected */
int
run_listbox (Listbox *l)
run_listbox (Listbox * l)
{
int val = -1;
@ -128,10 +130,10 @@ run_listbox (Listbox *l)
/* default query callback, used to reposition query */
static cb_ret_t
default_query_callback (Dlg_head *h, Widget *sender,
dlg_msg_t msg, int parm, void *data)
default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
{
switch (msg) {
switch (msg)
{
case DLG_RESIZE:
{
int xpos = COLS / 2 - h->cols / 2;
@ -164,15 +166,16 @@ query_dialog (const char *header, const char *text, int flags, int count, ...)
int result = -1;
int cols, lines;
char *cur_name;
const int *query_colors = (flags & D_ERROR) ?
alarm_colors : dialog_colors;
const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
if (header == MSG_ERROR)
header = _("Error");
if (count > 0) {
if (count > 0)
{
va_start (ap, count);
for (i = 0; i < count; i++) {
for (i = 0; i < count; i++)
{
char *cp = va_arg (ap, char *);
win_len += str_term_width1 (cp) + 6;
if (strchr (cp, '&') != NULL)
@ -191,10 +194,12 @@ query_dialog (const char *header, const char *text, int flags, int count, ...)
create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
"[QueryBox]", header, DLG_NONE);
if (count > 0) {
if (count > 0)
{
cols = (cols - win_len - 2) / 2 + 2;
va_start (ap, count);
for (i = 0; i < count; i++) {
for (i = 0; i < count; i++)
{
int xpos;
cur_name = va_arg (ap, char *);
@ -202,9 +207,7 @@ query_dialog (const char *header, const char *text, int flags, int count, ...)
if (strchr (cur_name, '&') != NULL)
xpos--;
button =
button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON,
cur_name, 0);
button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
add_widget (query_dlg, button);
cols += xpos;
if (i == sel_pos)
@ -221,7 +224,8 @@ query_dialog (const char *header, const char *text, int flags, int count, ...)
dlg_select_widget (defbutton);
/* run dialog and make result */
switch (run_dlg (query_dlg)) {
switch (run_dlg (query_dlg))
{
case B_CANCEL:
break;
default:
@ -230,17 +234,19 @@ query_dialog (const char *header, const char *text, int flags, int count, ...)
/* free used memory */
destroy_dlg (query_dlg);
} else {
}
else
{
add_widget (query_dlg, label_new (2, 3, text));
add_widget (query_dlg,
button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
last_query_dlg = query_dlg;
}
sel_pos = 0;
return result;
}
void query_set_sel (int new_sel)
void
query_set_sel (int new_sel)
{
sel_pos = new_sel;
}
@ -325,7 +331,8 @@ message (int flags, const char *title, const char *text, ...)
{
char *p;
va_list ap;
union {
union
{
void *p;
void (*f) (int, int *, char *, const char *);
} func;
@ -338,11 +345,12 @@ message (int flags, const char *title, const char *text, ...)
title = _("Error");
#ifdef WITH_BACKGROUND
if (we_are_background) {
if (we_are_background)
{
func.f = bg_message;
parent_call (func.p, NULL, 3, sizeof (flags), &flags,
strlen (title), title, strlen (p), p);
} else
parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title, strlen (p), p);
}
else
#endif /* WITH_BACKGROUND */
fg_message (flags, title, p);
@ -354,7 +362,7 @@ message (int flags, const char *title, const char *text, ...)
int
quick_dialog_skip (QuickDialog *qd, int nskip)
quick_dialog_skip (QuickDialog * qd, int nskip)
{
#ifdef ENABLE_NLS
#define I18N(x) (x = !qd->i18n && x && *x ? _(x): x)
@ -367,8 +375,7 @@ quick_dialog_skip (QuickDialog *qd, int nskip)
WRadio *r;
int return_val;
const int input_colors[3] =
{
const int input_colors[3] = {
INPUT_COLOR,
INPUT_UNCHANGED_COLOR,
INPUT_MARK_COLOR
@ -382,24 +389,28 @@ quick_dialog_skip (QuickDialog *qd, int nskip)
DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
else
dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
dialog_colors, NULL, qd->help, qd->title,
DLG_REVERSE);
dialog_colors, NULL, qd->help, qd->title, DLG_REVERSE);
for (qw = qd->widgets; qw->widget_type != quick_end; qw++) {
for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
{
int xpos;
int ypos;
xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
switch (qw->widget_type) {
switch (qw->widget_type)
{
case quick_checkbox:
qw->widget = (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state, I18N (qw->u.checkbox.text));
qw->widget =
(Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
I18N (qw->u.checkbox.text));
break;
case quick_button:
qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
(qw->u.button.action == B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
(qw->u.button.action ==
B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
I18N (qw->u.button.text), qw->u.button.callback);
break;
@ -462,9 +473,12 @@ quick_dialog_skip (QuickDialog *qd, int nskip)
return_val = run_dlg (dd);
/* Get the data if we found something interesting */
if (return_val != B_CANCEL) {
for (qw = qd->widgets; qw->widget_type != quick_end; qw++) {
switch (qw->widget_type) {
if (return_val != B_CANCEL)
{
for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
{
switch (qw->widget_type)
{
case quick_checkbox:
*qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
break;
@ -493,7 +507,7 @@ quick_dialog_skip (QuickDialog *qd, int nskip)
}
int
quick_dialog (QuickDialog *qd)
quick_dialog (QuickDialog * qd)
{
return quick_dialog_skip (qd, 0);
}
@ -520,60 +534,69 @@ fg_input_dialog_help (const char *header, const char *text, const char *help,
char *my_str;
QuickWidget quick_widgets[] = {
/* 0 */ QUICK_BUTTON (6, 10, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
/* 1 */ QUICK_BUTTON (3, 10, 1, 0, N_("&OK"), B_ENTER, NULL),
/* 2 */ QUICK_INPUT (3, 80, 0, 0, def_text, 58, 0, NULL, &my_str),
/* 3 */ QUICK_LABEL (3, 80, 2, 0, ""),
/* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
/* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
/* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
/* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
QUICK_END
};
char histname [64] = "inp|";
int b0_len, b1_len, b_len, gap;
char histname[64] = "inp|";
int lines, cols;
int len;
int i;
char *p_text;
int ret;
if (history_name != NULL && *history_name != '\0') {
/* buttons */
#ifdef ENABLE_NLS
quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
#endif /* ENABLE_NLS */
b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
b_len = b0_len + b1_len + 2; /* including gap */
/* input line */
if (history_name != NULL && *history_name != '\0')
{
g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
quick_widgets[2].u.input.histname = histname;
}
msglen (text, &lines, &cols);
len = max (max (str_term_width1 (header), cols) + 4, 64);
/* The special value of def_text is used to identify password boxes
and hide characters with "*". Don't save passwords in history! */
if (def_text == INPUT_PASSWORD) {
if (def_text == INPUT_PASSWORD)
{
quick_widgets[2].u.input.flags = 1;
histname[3] = '\0';
quick_widgets[2].u.input.text = "";
}
#ifdef ENABLE_NLS
/*
* An attempt to place buttons symmetrically, based on actual i18n
* length of the string. It looks nicer with i18n (IMO) - alex
*/
quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
quick_widgets[0].relative_x = len / 2 + 4;
quick_widgets[1].relative_x =
len / 2 - (str_term_width1 (quick_widgets[1].u.button.text) + 9);
quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
#endif /* ENABLE_NLS */
/* text */
p_text = g_strstrip (g_strdup (text));
msglen (p_text, &lines, &cols);
quick_widgets[3].u.label.text = p_text;
/* dialog width */
len = max (max (str_term_width1 (header), cols) + 4, 64);
len = min (max (len, b_len + 6), COLS);
/* button locations */
gap = (len - 8 - b_len) / 3;
quick_widgets[1].relative_x = 3 + gap;
quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
{
QuickDialog Quick_input =
{
QuickDialog Quick_input = {
len, lines + 6, -1, -1, header,
help, quick_widgets, TRUE
};
for (i = 0; i < 4; i++) {
for (i = 0; i < 4; i++)
{
quick_widgets[i].x_divisions = Quick_input.xlen;
quick_widgets[i].y_divisions = Quick_input.ylen;
}
@ -602,10 +625,10 @@ char *
input_dialog_help (const char *header, const char *text, const char *help,
const char *history_name, const char *def_text)
{
union {
union
{
void *p;
char * (*f) (const char *, const char *, const char *,
const char *, const char *);
char *(*f) (const char *, const char *, const char *, const char *, const char *);
} func;
#ifdef WITH_BACKGROUND
if (we_are_background)
@ -623,8 +646,8 @@ input_dialog_help (const char *header, const char *text, const char *help,
}
/* Show input dialog with default help, background safe */
char *input_dialog (const char *header, const char *text,
const char *history_name, const char *def_text)
char *
input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
{
return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
}
@ -637,7 +660,8 @@ input_expand_dialog (const char *header, const char *text,
char *expanded;
result = input_dialog (header, text, history_name, def_text);
if (result) {
if (result)
{
expanded = tilde_expand (result);
g_free (result);
return expanded;
@ -652,4 +676,4 @@ input_expand_dialog (const char *header, const char *text,
Cause emacs to enter folding mode for this file:
Local variables:
end:
*/
*/

View File

@ -10,7 +10,8 @@
#include "dialog.h"
#include "widget.h"
typedef struct {
typedef struct
{
struct Dlg_head *dlg;
struct WListbox *list;
} Listbox;
@ -22,10 +23,11 @@ Listbox *create_listbox_window (int lines, int cols, const char *title, const ch
#define LISTBOX_APPEND_TEXT(l,h,t,d) \
listbox_add_item (l->list, LISTBOX_APPEND_AT_END, h, t, d)
int run_listbox (Listbox *l);
int run_listbox (Listbox * l);
/* Quick Widgets */
typedef enum {
typedef enum
{
quick_end = 0,
quick_checkbox = 1,
quick_button = 2,
@ -36,7 +38,8 @@ typedef enum {
} quick_t;
/* The widget is placed on relative_?/divisions_? of the parent widget */
typedef struct {
typedef struct
{
quick_t widget_type;
int relative_x;
@ -47,19 +50,23 @@ typedef struct {
Widget *widget;
/* widget parameters */
union {
struct {
union
{
struct
{
const char *text;
int *state; /* in/out */
} checkbox;
struct {
struct
{
const char *text;
int action;
bcback callback;
} button;
struct {
struct
{
const char *text;
int len;
int flags; /* 1 -- is_password, 2 -- INPUT_COMPLETE_CD */
@ -67,17 +74,20 @@ typedef struct {
char **result;
} input;
struct {
struct
{
const char *text;
} label;
struct {
struct
{
int count;
const char **items;
int *value; /* in/out */
} radio;
struct {
struct
{
int width;
int height;
const char *title;
@ -186,6 +196,7 @@ typedef struct {
} \
}
#define QUICK_END \
{ \
.widget_type = quick_end, \
@ -205,7 +216,8 @@ typedef struct {
} \
}
typedef struct {
typedef struct
{
int xlen, ylen;
int xpos, ypos; /* if -1, then center the dialog */
const char *title;
@ -214,8 +226,8 @@ typedef struct {
gboolean i18n; /* If true, internationalization has happened */
} QuickDialog;
int quick_dialog (QuickDialog *qd);
int quick_dialog_skip (QuickDialog *qd, int nskip);
int quick_dialog (QuickDialog * qd);
int quick_dialog_skip (QuickDialog * qd, int nskip);
/* The input dialogs */
@ -233,8 +245,7 @@ void query_set_sel (int new_sel);
/* Create message box but don't dismiss it yet, not background safe */
struct Dlg_head *create_message (int flags, const char *title,
const char *text, ...)
__attribute__ ((format (__printf__, 3, 4)));
const char *text, ...) __attribute__ ((format (__printf__, 3, 4)));
/* Show message box, background safe */
void message (int flags, const char *title, const char *text, ...)
@ -247,9 +258,10 @@ void message (int flags, const char *title, const char *text, ...)
int query_dialog (const char *header, const char *text, int flags, int count, ...);
/* flags for message() and query_dialog() */
enum {
enum
{
D_NORMAL = 0,
D_ERROR = 1
} /* dialog options */;
} /* dialog options */ ;
#endif