Commit 3d18c91d authored by Victor Ananjevsky's avatar Victor Ananjevsky

add --changed-action option to form dialog

parent 92b4a24e
......@@ -591,6 +591,10 @@ Use specific type for extended completion. \fITYPE\fP can be \fIany\fP for match
.B \-\-scroll
Make form scrollable.
.TP
.B \-\-changed-action=\fICMD\fP
Run \fICMD\fP when \fICHK\fP or \fICB\fP field values are changed. Command runs with two arguments - number of changed field and its current value.
Output of a command parsing in a same manner as in \fIBTN\fP fields with \fI@\fP prefix. \fBAttention\fP - this option may slow down your dialog.
.TP
.B \-\-quoted-output
Output values will be in shell-style quotes.
.TP
......
......@@ -27,6 +27,8 @@
static GSList *fields = NULL;
static guint n_fields;
static gboolean disable_changed = TRUE;
/* expand %N in command to fields values */
static GString *
expand_action (gchar * cmd)
......@@ -372,49 +374,56 @@ set_field_value (guint num, gchar *value)
}
static void
parse_cmd_output (gchar *data)
{
guint i = 0;
gchar **lines = g_strsplit (data, "\n", 0);
disable_changed = TRUE;
while (lines[i] && lines[i][0])
{
gint fn;
gchar *ptr = lines[i];
while (isblank (*ptr)) ptr++;
if (isdigit (*ptr))
{
gchar **ln = g_strsplit (ptr, ":", 2);
fn = g_ascii_strtoll (ln[0], NULL, 10);
if (fn && ln[1])
set_field_value (fn - 1, ln[1]);
g_strfreev (ln);
}
i++;
}
disable_changed = FALSE;
}
static void
button_clicked_cb (GtkButton * b, gpointer d)
{
gchar *action = (gchar *) g_object_get_data (G_OBJECT (b), "cmd");
if (action && action[0])
{
GString *cmd;
if (action[0] == '@')
{
gchar *data;
gchar *data = NULL;
gint exit = 1;
GString *cmd = expand_action (action + 1);
cmd = expand_action (action + 1);
exit = run_command_sync (cmd->str, &data);
if (exit == 0)
{
guint i = 0;
gchar **lines = g_strsplit (data, "\n", 0);
while (lines[i] && lines[i][0])
{
gint fn;
gchar *ptr = lines[i];
while (isblank (*ptr)) ptr++;
if (isdigit (*ptr))
{
gchar **ln = g_strsplit (ptr, ":", 2);
fn = g_ascii_strtoll (ln[0], NULL, 10);
if (fn && ln[1])
set_field_value (fn - 1, ln[1]);
g_strfreev (ln);
}
i++;
}
}
parse_cmd_output (data);
g_free (data);
g_string_free (cmd, TRUE);
}
else
{
GString *cmd = expand_action (action);
cmd = expand_action (action);
run_command_async (cmd->str);
g_string_free (cmd, TRUE);
}
g_string_free (cmd, TRUE);
}
/* set focus to specified field */
......@@ -423,6 +432,33 @@ button_clicked_cb (GtkButton * b, gpointer d)
}
static void
field_changed_cb (GtkWidget *w, guint fn)
{
gchar *data = NULL;
gint exit = 1;
if (disable_changed)
return;
if (options.form_data.changed_action)
{
gchar *str;
GString *cmd;
str = g_strdup_printf ("%s %d %%%d", options.form_data.changed_action, fn + 1, fn + 1);
cmd = expand_action (str);
g_free (str);
exit = run_command_sync (cmd->str, &data);
if (exit == 0)
parse_cmd_output (data);
g_free (data);
g_string_free (cmd, TRUE);
}
}
static void
form_activate_cb (GtkEntry * entry, gpointer data)
{
if (options.plug == -1)
......@@ -702,10 +738,7 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data)
if (options.form_data.cycle_read)
cnt = 0;
else
{
g_io_channel_shutdown (ch, TRUE, NULL);
return FALSE;
}
goto shutdown;
}
do
......@@ -726,8 +759,7 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data)
err = NULL;
}
/* stop handling */
g_io_channel_shutdown (ch, TRUE, NULL);
return FALSE;
goto shutdown;
}
strip_new_line (string->str);
......@@ -751,12 +783,14 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data)
}
if ((cond != G_IO_IN) && (cond != G_IO_IN + G_IO_HUP))
{
g_io_channel_shutdown (ch, TRUE, NULL);
return FALSE;
}
goto shutdown;
return TRUE;
shutdown:
g_io_channel_shutdown (ch, TRUE, NULL);
disable_changed = FALSE;
return FALSE;
}
GtkWidget *
......@@ -921,6 +955,7 @@ form_create_widget (GtkWidget * dlg)
gtk_widget_set_hexpand (e, TRUE);
fields = g_slist_append (fields, e);
g_free (buf);
g_signal_connect_after (G_OBJECT (e), "toggled", G_CALLBACK (field_changed_cb), GINT_TO_POINTER (i));
}
break;
......@@ -938,6 +973,7 @@ form_create_widget (GtkWidget * dlg)
gtk_widget_set_hexpand (e, TRUE);
gtk_label_set_mnemonic_widget (GTK_LABEL (l), e);
fields = g_slist_append (fields, e);
g_signal_connect_after (G_OBJECT (e), "changed", G_CALLBACK (field_changed_cb), GINT_TO_POINTER (i));
break;
case YAD_FIELD_COMBO_ENTRY:
......@@ -1295,6 +1331,8 @@ form_create_widget (GtkWidget * dlg)
if (options.form_data.focus_field > 0 && options.form_data.focus_field <= n_fields)
gtk_widget_grab_focus (GTK_WIDGET (g_slist_nth_data (fields, options.form_data.focus_field - 1)));
disable_changed = FALSE;
return w;
}
......
......@@ -385,6 +385,8 @@ static GOptionEntry form_options[] = {
N_("Cycled reading of stdin data"), NULL },
{ "align-buttons", 0, 0, G_OPTION_ARG_NONE, &options.form_data.align_buttons,
N_("Align labels on button fields"), NULL },
{ "changed-action", 0, 0, G_OPTION_ARG_STRING, &options.form_data.changed_action,
N_("Set changed action"), "CMD" },
{ NULL }
};
......@@ -1734,6 +1736,7 @@ yad_options_init (void)
options.form_data.focus_field = 1;
options.form_data.cycle_read = FALSE;
options.form_data.align_buttons = FALSE;
options.form_data.changed_action = NULL;
#ifdef HAVE_HTML
/* Initialize html data */
......
......@@ -340,6 +340,7 @@ typedef struct {
guint focus_field;
gboolean cycle_read;
gboolean align_buttons;
gchar *changed_action;
} YadFormData;
#ifdef HAVE_HTML
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment