New QuickDialog engine.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Andrew Borodin 2012-09-06 10:23:19 +04:00
parent 706c966af8
commit e97ac7507a
4 changed files with 959 additions and 0 deletions

View File

@ -31,6 +31,7 @@ typedef struct Dlg_head Dlg_head;
#include "lib/widget/input.h"
#include "lib/widget/listbox-window.h"
#include "lib/widget/quick.h"
#include "lib/widget/quick2.h"
#include "lib/widget/wtools.h"
#include "lib/widget/dialog-switch.h"

View File

@ -18,6 +18,7 @@ libmcwidget_la_SOURCES = \
label.c label.h \
menu.c menu.h \
quick.c quick.h \
quick2.c quick2.h \
radio.c radio.h \
widget-common.c widget-common.h \
wtools.c wtools.h

622
lib/widget/quick2.c Normal file
View File

@ -0,0 +1,622 @@
/*
Widget based utility functions.
Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
The Free Software Foundation, Inc.
Authors:
Miguel de Icaza, 1994, 1995, 1996
Radek Doulik, 1994, 1995
Jakub Jelinek, 1995
Andrej Borsenkow, 1995
Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file quick2.c
* \brief Source: quick dialog engine
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h> /* fprintf() */
#include "lib/global.h"
#include "lib/strutil.h" /* str_term_width1() */
#include "lib/util.h" /* tilde_expand() */
#include "lib/widget.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
#ifdef ENABLE_NLS
#define I18N(x) (x = x != NULL && *x != '\0' ? _(x) : x)
#else
#define I18N(x) (x = x)
#endif
/*** file scope type declarations ****************************************************************/
typedef struct
{
Widget *widget;
quick_widget_t *quick_widget;
} quick_widget_item_t;
/*** file scope variables ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static WInput *
quick_create_input (int y, int x, const quick_widget_t * qw)
{
WInput *in;
in = input_new (y, x, input_get_default_colors (), 8, qw->u.input.text, qw->u.input.histname,
INPUT_COMPLETE_DEFAULT);
in->is_password = (qw->u.input.flags == 1);
if ((qw->u.input.flags & 2) != 0)
in->completion_flags |= INPUT_COMPLETE_CD;
if ((qw->u.input.flags & 4) != 0)
in->strip_password = TRUE;
return in;
}
/* --------------------------------------------------------------------------------------------- */
static void
quick_create_labeled_input (GArray * widgets, int *y, int x, quick_widget_t * quick_widget,
int *width)
{
quick_widget_item_t in, label;
label.quick_widget = g_new0 (quick_widget_t, 1);
label.quick_widget->widget_type = quick2_label;
label.quick_widget->options = quick_widget->options;
switch (quick_widget->u.input.label_location)
{
case input_label_above:
label.widget = WIDGET (label_new (*y, x, I18N (quick_widget->u.input.label_text)));
*y += label.widget->lines - 1;
g_array_append_val (widgets, label);
in.widget = WIDGET (quick_create_input (++(*y), x, quick_widget));
in.quick_widget = quick_widget;
g_array_append_val (widgets, in);
*width = max (label.widget->cols, in.widget->cols);
break;
case input_label_left:
label.widget = WIDGET (label_new (*y, x, I18N (quick_widget->u.input.label_text)));
g_array_append_val (widgets, label);
in.widget = WIDGET (quick_create_input (*y, x + label.widget->cols + 1, quick_widget));
in.quick_widget = quick_widget;
g_array_append_val (widgets, in);
*width = label.widget->cols + in.widget->cols + 1;
break;
case input_label_right:
in.widget = WIDGET (quick_create_input (*y, x, quick_widget));
in.quick_widget = quick_widget;
g_array_append_val (widgets, in);
label.widget =
WIDGET (label_new
(*y, x + in.widget->cols + 1, I18N (quick_widget->u.input.label_text)));
g_array_append_val (widgets, label);
*width = label.widget->cols + in.widget->cols + 1;
break;
case input_label_below:
in.widget = WIDGET (quick_create_input (*y, x, quick_widget));
in.quick_widget = quick_widget;
g_array_append_val (widgets, in);
label.widget = WIDGET (label_new (++(*y), x, I18N (quick_widget->u.input.label_text)));
*y += label.widget->lines - 1;
g_array_append_val (widgets, label);
*width = max (label.widget->cols, in.widget->cols);
break;
default:
return;
}
((WInput *) in.widget)->label = (WLabel *) label.widget;
/* cross references */
label.quick_widget->u.label.input = in.quick_widget;
in.quick_widget->u.input.label = label.quick_widget;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
int
quick2_dialog_skip (quick_dialog_t * quick_dlg, int nskip)
{
int len;
int blen = 0;
int x, y; /* current positions */
int y1 = 0; /* bottom of 1st column in case of two columns */
int y2 = -1; /* start of two columns */
int width1 = 0; /* width of single column */
int width2 = 0; /* width of each of two columns*/
gboolean have_groupbox = FALSE;
gboolean two_columns = FALSE;
gboolean put_buttons = FALSE;
/* x position of 1st column is 3 */
const int x1 = 3;
/* x position of 2nd column is 4 and it will be fixed later, after creation of all widgets */
int x2 = 4;
GArray *widgets;
size_t i;
quick_widget_t *quick_widget;
WGroupbox *g = NULL;
Dlg_head *dd;
int return_val;
len = str_term_width1 (I18N (quick_dlg->title)) + 6;
quick_dlg->cols = max (quick_dlg->cols, len);
y = 1;
x = x1;
/* create widgets */
widgets = g_array_sized_new (FALSE, FALSE, sizeof (quick_widget_item_t), 8);
for (quick_widget = quick_dlg->widgets; quick_widget->widget_type != quick2_end; quick_widget++)
{
quick_widget_item_t item = { NULL, quick_widget };
int width = 0;
switch (quick_widget->widget_type)
{
case quick2_checkbox:
item.widget =
WIDGET (check_new
(++y, x, *quick_widget->u.checkbox.state,
I18N (quick_widget->u.checkbox.text)));
g_array_append_val (widgets, item);
width = item.widget->cols;
if (g != NULL)
width += 2;
if (two_columns)
width2 = max (width2, width);
else
width1 = max (width1, width);
break;
case quick2_button:
/* single button */
item.widget = WIDGET (button_new (++y, x, quick_widget->u.button.action,
quick_widget->u.button.action == B_ENTER ?
DEFPUSH_BUTTON : NORMAL_BUTTON,
I18N (quick_widget->u.button.text),
quick_widget->u.button.callback));
g_array_append_val (widgets, item);
width = item.widget->cols;
if (g != NULL)
width += 2;
if (two_columns)
width2 = max (width2, width);
else
width1 = max (width1, width);
break;
case quick2_input:
*quick_widget->u.input.result = NULL;
y++;
if (quick_widget->u.input.label_location != input_label_none)
quick_create_labeled_input (widgets, &y, x, quick_widget, &width);
else
{
item.widget = WIDGET (quick_create_input (y, x, quick_widget));
g_array_append_val (widgets, item);
width = item.widget->cols;
}
if (g != NULL)
width += 2;
if (two_columns)
width2 = max (width2, width);
else
width1 = max (width1, width);
break;
case quick2_label:
item.widget = WIDGET (label_new (++y, x, I18N (quick_widget->u.label.text)));
g_array_append_val (widgets, item);
y += item.widget->lines - 1;
width = item.widget->cols;
if (g != NULL)
width += 2;
if (two_columns)
width2 = max (width2, width);
else
width1 = max (width1, width);
break;
case quick2_radio:
{
WRadio *r;
char **items = NULL;
/* create the copy of radio_items to avoid mwmory leak */
items = g_new (char *, quick_widget->u.radio.count + 1);
for (i = 0; i < (size_t) quick_widget->u.radio.count; i++)
items[i] = g_strdup (_(quick_widget->u.radio.items[i]));
items[i] = NULL;
r = radio_new (++y, x, quick_widget->u.radio.count, (const char **) items);
r->pos = r->sel = *quick_widget->u.radio.value;
g_strfreev (items);
item.widget = WIDGET (r);
g_array_append_val (widgets, item);
y += item.widget->lines - 1;
width = item.widget->cols;
if (g != NULL)
width += 2;
if (two_columns)
width2 = max (width2, width);
else
width1 = max (width1, width);
}
break;
case quick2_start_groupbox:
I18N (quick_widget->u.groupbox.title);
len = str_term_width1 (quick_widget->u.groupbox.title);
g = groupbox_new (++y, x, 1, len + 4, quick_widget->u.groupbox.title);
item.widget = WIDGET (g);
g_array_append_val (widgets, item);
have_groupbox = TRUE;
break;
case quick2_stop_groupbox:
if (g != NULL)
{
Widget *w = WIDGET (g);
y++;
w->lines = y + 1 - w->y;
g = NULL;
g_array_append_val (widgets, item);
}
break;
case quick2_separator:
y++;
if (quick_widget->u.separator.line)
{
item.widget = WIDGET (hline_new (y, x, 1));
g_array_append_val (widgets, item);
}
break;
case quick2_start_columns:
y2 = y;
g_array_append_val (widgets, item);
two_columns = TRUE;
break;
case quick2_next_column:
x = x2;
y1 = y;
y = y2;
break;
case quick2_stop_columns:
x = x1;
y = max (y1, y);
g_array_append_val (widgets, item);
two_columns = FALSE;
break;
case quick2_buttons:
/* start put several buttons in bottom line */
if (quick_widget->u.separator.space)
{
y++;
if (quick_widget->u.separator.line)
item.widget = WIDGET (hline_new (y, 1, -1));
}
g_array_append_val (widgets, item);
/* several buttons in bottom line */
y++;
quick_widget++;
for (; quick_widget->widget_type == quick2_button; quick_widget++)
{
item.widget = WIDGET (button_new (y, x++, quick_widget->u.button.action,
quick_widget->u.button.action == B_ENTER ?
DEFPUSH_BUTTON : NORMAL_BUTTON,
I18N (quick_widget->u.button.text),
quick_widget->u.button.callback));
item.quick_widget = quick_widget;
g_array_append_val (widgets, item);
blen += item.widget->cols + 1;
}
/* stop dialog build here */
blen--;
quick_widget->widget_type = quick2_end;
quick_widget--;
break;
default:
break;
}
}
/* adjust dialog width */
quick_dlg->cols = max (quick_dlg->cols, blen + 6);
if (have_groupbox)
{
if (width1 != 0)
width1 += 2;
if (width2 != 0)
width2 += 2;
}
if (width2 == 0)
len = width1 + 6;
else
{
len = width2 * 2 + 7;
if (width1 != 0)
len = max (len, width1 + 6);
}
quick_dlg->cols = max (quick_dlg->cols, len);
width1 = quick_dlg->cols - 6;
width2 = (quick_dlg->cols - 7) / 2;
if (quick_dlg->x == -1 || quick_dlg->y == -1)
dd = create_dlg (TRUE, 0, 0, y + 3, quick_dlg->cols,
dialog_colors, quick_dlg->callback, quick_dlg->mouse, quick_dlg->help,
quick_dlg->title, DLG_CENTER | DLG_TRYUP);
else
dd = create_dlg (TRUE, quick_dlg->y, quick_dlg->x, y + 3, quick_dlg->cols,
dialog_colors, quick_dlg->callback, quick_dlg->mouse, quick_dlg->help,
quick_dlg->title, DLG_NONE);
/* add widgets into the dialog */
x2 = x1 + width2 + 1;
g = NULL;
two_columns = FALSE;
x = (WIDGET (dd)->cols - blen) / 2;
for (i = 0; i < widgets->len; i++)
{
quick_widget_item_t *item;
int column_width;
item = &g_array_index (widgets, quick_widget_item_t, i);
column_width = two_columns ? width2 : width1;
/* adjust widget width and x position */
switch (item->quick_widget->widget_type)
{
case quick2_label:
{
quick_widget_t *input = item->quick_widget->u.label.input;
if (input != NULL && input->u.input.label_location == input_label_right)
{
/* location of this label will be adjusted later */
break;
}
}
/* fall through */
case quick2_checkbox:
case quick2_radio:
if (item->widget->x != x1)
item->widget->x = x2;
if (g != NULL)
item->widget->x += 2;
break;
case quick2_button:
if (!put_buttons)
{
if (item->widget->x != x1)
item->widget->x = x2;
if (g != NULL)
item->widget->x += 2;
}
else
{
item->widget->x = x;
x += item->widget->cols + 1;
}
break;
case quick2_input:
{
Widget *label = WIDGET (((WInput *) item->widget)->label);
int width = column_width;
if (g != NULL)
width -= 4;
switch (item->quick_widget->u.input.label_location)
{
case input_label_left:
/* label was adjusted before; adjust input line */
item->widget->x = label->x + label->cols + 1 - WIDGET (label->owner)->x;
item->widget->cols = width - label->cols - 1;
break;
case input_label_right:
label->x =
item->widget->x + item->widget->cols + 1 - WIDGET (item->widget->owner)->x;
item->widget->cols = width - label->cols - 1;
break;
default:
if (item->widget->x != x1)
item->widget->x = x2;
if (g != NULL)
item->widget->x += 2;
item->widget->cols = width;
break;
}
/* forced update internal variables of inpuit line */
input_set_origin ((WInput *) (item->widget), item->widget->x, item->widget->cols);
}
break;
case quick2_start_groupbox:
g = (WGroupbox *) item->widget;
if (item->widget->x != x1)
item->widget->x = x2;
item->widget->cols = column_width;
break;
case quick2_stop_groupbox:
g = NULL;
break;
case quick2_separator:
if (item->widget != NULL)
{
if (g != NULL)
{
Widget *wg = WIDGET (g);
((WHLine *) item->widget)->auto_adjust_cols = FALSE;
item->widget->x = wg->x + 1 - WIDGET (wg->owner)->x;
item->widget->cols = wg->cols;
}
else if (two_columns)
{
((WHLine *) item->widget)->auto_adjust_cols = FALSE;
if (item->widget->x != x1)
item->widget->x = x2;
item->widget->x--;
item->widget->cols = column_width + 2;
}
else
((WHLine *) item->widget)->auto_adjust_cols = TRUE;
}
break;
case quick2_start_columns:
two_columns = TRUE;
break;
case quick2_stop_columns:
two_columns = FALSE;
break;
case quick2_buttons:
/* several buttons in bottom line */
put_buttons = TRUE;
break;
default:
break;
}
if (item->widget != NULL)
{
unsigned long id;
/* add widget into dialog */
item->widget->options |= item->quick_widget->options; /* FIXME: cannot reset flags, setup only */
id = add_widget (dd, item->widget);
if (item->quick_widget->id != NULL)
*item->quick_widget->id = id;
}
}
while (nskip-- != 0)
{
dd->current = g_list_next (dd->current);
if (dd->current == NULL)
dd->current = dd->widgets;
}
return_val = run_dlg (dd);
/* Get the data if we found something interesting */
if (return_val != B_CANCEL)
for (i = 0; i < widgets->len; i++)
{
quick_widget_item_t *item;
item = &g_array_index (widgets, quick_widget_item_t, i);
switch (item->quick_widget->widget_type)
{
case quick_checkbox:
*item->quick_widget->u.checkbox.state = ((WCheck *) item->widget)->state & C_BOOL;
break;
case quick_input:
if ((quick_widget->u.input.flags & 2) != 0)
*item->quick_widget->u.input.result =
tilde_expand (((WInput *) item->widget)->buffer);
else
*item->quick_widget->u.input.result =
g_strdup (((WInput *) item->widget)->buffer);
break;
case quick_radio:
*item->quick_widget->u.radio.value = ((WRadio *) item->widget)->sel;
break;
default:
break;
}
}
destroy_dlg (dd);
/* destroy input labels created before */
for (i = 0; i < widgets->len; i++)
{
quick_widget_item_t *item;
item = &g_array_index (widgets, quick_widget_item_t, i);
if (item->quick_widget->widget_type == quick2_input)
g_free (item->quick_widget->u.input.label);
}
g_array_free (widgets, TRUE);
return return_val;
}
/* --------------------------------------------------------------------------------------------- */

335
lib/widget/quick2.h Normal file
View File

@ -0,0 +1,335 @@
/** \file quick2.h
* \brief Header: quick dialog engine
*/
#ifndef MC__QUICK2_H
#define MC__QUICK2_H
#include "lib/tty/mouse.h"
/*** typedefs(not structures) and defined constants **********************************************/
#define QUICK2_CHECKBOX(txt, st, id_) \
{ \
.widget_type = quick2_checkbox, \
.options = 0, \
.id = id_, \
.u = { \
.checkbox = { \
.text = txt, \
.state = st \
} \
} \
}
#define QUICK2_BUTTON(txt, act, cb, id_) \
{ \
.widget_type = quick2_button, \
.options = 0, \
.id = id_, \
.u = { \
.button = { \
.text = txt, \
.action = act, \
.callback = cb \
} \
} \
}
#define QUICK2_INPUT(txt, flags_, hname, res, id_) \
{ \
.widget_type = quick2_input, \
.options = 0, \
.id = id_, \
.u = { \
.input = { \
.label_text = NULL, \
.label_location = input_label_none, \
.label = NULL, \
.text = txt, \
.flags = flags_, \
.histname = hname, \
.result = res \
} \
} \
}
#define QUICK2_LABELED_INPUT(label_, label_loc, txt, flags_, hname, res, id_) \
{ \
.widget_type = quick2_input, \
.options = 0, \
.id = id_, \
.u = { \
.input = { \
.label_text = label_, \
.label_location = label_loc, \
.label = NULL, \
.text = txt, \
.flags = flags_, \
.histname = hname, \
.result = res \
} \
} \
}
#define QUICK2_LABEL(txt, id_) \
{ \
.widget_type = quick2_label, \
.options = 0, \
.id = id_, \
.u = { \
.label = { \
.text = txt, \
.input = NULL \
} \
} \
}
#define QUICK2_RADIO(cnt, items_, val, id_) \
{ \
.widget_type = quick2_radio, \
.options = 0, \
.id = id_, \
.u = { \
.radio = { \
.count = cnt, \
.items = items_, \
.value = val \
} \
} \
}
#define QUICK2_START_GROUPBOX(t) \
{ \
.widget_type = quick2_start_groupbox, \
.options = 0, \
.id = NULL, \
.u = { \
.groupbox = { \
.title = t \
} \
} \
}
#define QUICK2_STOP_GROUPBOX \
{ \
.widget_type = quick2_stop_groupbox, \
.options = 0, \
.id = NULL, \
.u = { \
.input = { \
.text = NULL, \
.flags = 0, \
.histname = NULL, \
.result = NULL \
} \
} \
}
#define QUICK2_SEPARATOR(line_) \
{ \
.widget_type = quick2_separator, \
.options = 0, \
.id = NULL, \
.u = { \
.separator = { \
.space = TRUE, \
.line = line_ \
} \
} \
}
#define QUICK2_START_COLUMNS \
{ \
.widget_type = quick2_start_columns, \
.options = 0, \
.id = NULL, \
.u = { \
.input = { \
.text = NULL, \
.flags = 0, \
.histname = NULL, \
.result = NULL \
} \
} \
}
#define QUICK2_NEXT_COLUMN \
{ \
.widget_type = quick2_next_column, \
.options = 0, \
.id = NULL, \
.u = { \
.input = { \
.text = NULL, \
.flags = 0, \
.histname = NULL, \
.result = NULL \
} \
} \
}
#define QUICK2_STOP_COLUMNS \
{ \
.widget_type = quick2_stop_columns, \
.options = 0, \
.id = NULL, \
.u = { \
.input = { \
.text = NULL, \
.flags = 0, \
.histname = NULL, \
.result = NULL \
} \
} \
}
#define QUICK2_START_BUTTONS(space_, line_) \
{ \
.widget_type = quick2_buttons, \
.options = 0, \
.id = NULL, \
.u = { \
.separator = { \
.space = space_, \
.line = line_ \
} \
} \
}
#define QUICK2_END \
{ \
.widget_type = quick2_end, \
.options = 0, \
.id = NULL, \
.u = { \
.input = { \
.text = NULL, \
.flags = 0, \
.histname = NULL, \
.result = NULL \
} \
} \
}
/*** enums ***************************************************************************************/
/* Quick Widgets */
typedef enum
{
quick2_end = 0,
quick2_checkbox = 1,
quick2_button = 2,
quick2_input = 3,
quick2_label = 4,
quick2_radio = 5,
quick2_start_groupbox = 6,
quick2_stop_groupbox = 7,
quick2_separator = 8,
quick2_start_columns = 9,
quick2_next_column = 10,
quick2_stop_columns = 11,
quick2_buttons = 12
} quick2_t;
typedef enum
{
input_label_none = 0,
input_label_above = 1,
input_label_left = 2,
input_label_right = 3,
input_label_below = 4
} quick_input_label_location_t;
/*** structures declarations (and typedefs of structures)*****************************************/
/* The widget is placed on relative_?/divisions_? of the parent widget */
typedef struct quick_widget_t quick_widget_t;
struct quick_widget_t
{
quick2_t widget_type;
widget_options_t options;
unsigned long *id;
/* widget parameters */
union
{
struct
{
const char *text;
int *state; /* in/out */
} checkbox;
struct
{
const char *text;
int action;
bcback_fn callback;
} button;
struct
{
const char *label_text;
quick_input_label_location_t label_location;
quick_widget_t *label;
const char *text;
int flags; /* 1 -- is_password, 2 -- INPUT_COMPLETE_CD */
const char *histname;
char **result;
gboolean strip_password;
} input;
struct
{
const char *text;
quick_widget_t *input;
} label;
struct
{
int count;
const char **items;
int *value; /* in/out */
} radio;
struct
{
const char *title;
} groupbox;
struct
{
gboolean space;
gboolean line;
} separator;
} u;
};
typedef struct
{
int y, x; /* if -1, then center the dialog */
int cols; /* heigth is calculated automatically */
const char *title;
const char *help;
quick_widget_t *widgets;
dlg_cb_fn callback;
mouse_h mouse;
} quick_dialog_t;
/*** global variables defined in .c file *********************************************************/
/*** declarations of public functions ************************************************************/
int quick2_dialog_skip (quick_dialog_t * quick_dlg, int nskip);
/*** inline functions ****************************************************************************/
static inline int
quick2_dialog (quick_dialog_t * quick_dlg)
{
return quick2_dialog_skip (quick_dlg, 1);
}
#endif /* MC__QUICK2_H */