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 ...@@ -591,6 +591,10 @@ Use specific type for extended completion. \fITYPE\fP can be \fIany\fP for match
.B \-\-scroll .B \-\-scroll
Make form scrollable. Make form scrollable.
.TP .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 .B \-\-quoted-output
Output values will be in shell-style quotes. Output values will be in shell-style quotes.
.TP .TP
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
static GSList *fields = NULL; static GSList *fields = NULL;
static guint n_fields; static guint n_fields;
static gboolean disable_changed = TRUE;
/* expand %N in command to fields values */ /* expand %N in command to fields values */
static GString * static GString *
expand_action (gchar * cmd) expand_action (gchar * cmd)
...@@ -372,22 +374,12 @@ set_field_value (guint num, gchar *value) ...@@ -372,22 +374,12 @@ set_field_value (guint num, gchar *value)
} }
static void static void
button_clicked_cb (GtkButton * b, gpointer d) parse_cmd_output (gchar *data)
{ {
gchar *action = (gchar *) g_object_get_data (G_OBJECT (b), "cmd");
if (action && action[0])
{
if (action[0] == '@')
{
gchar *data;
gint exit = 1;
GString *cmd = expand_action (action + 1);
exit = run_command_sync (cmd->str, &data);
if (exit == 0)
{
guint i = 0; guint i = 0;
gchar **lines = g_strsplit (data, "\n", 0); gchar **lines = g_strsplit (data, "\n", 0);
disable_changed = TRUE;
while (lines[i] && lines[i][0]) while (lines[i] && lines[i][0])
{ {
gint fn; gint fn;
...@@ -405,16 +397,33 @@ button_clicked_cb (GtkButton * b, gpointer d) ...@@ -405,16 +397,33 @@ button_clicked_cb (GtkButton * b, gpointer d)
} }
i++; 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 = NULL;
gint exit = 1;
cmd = expand_action (action + 1);
exit = run_command_sync (cmd->str, &data);
if (exit == 0)
parse_cmd_output (data);
g_free (data); g_free (data);
g_string_free (cmd, TRUE);
} }
else else
{ {
GString *cmd = expand_action (action); cmd = expand_action (action);
run_command_async (cmd->str); run_command_async (cmd->str);
g_string_free (cmd, TRUE);
} }
g_string_free (cmd, TRUE);
} }
/* set focus to specified field */ /* set focus to specified field */
...@@ -423,6 +432,33 @@ button_clicked_cb (GtkButton * b, gpointer d) ...@@ -423,6 +432,33 @@ button_clicked_cb (GtkButton * b, gpointer d)
} }
static void 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) form_activate_cb (GtkEntry * entry, gpointer data)
{ {
if (options.plug == -1) if (options.plug == -1)
...@@ -702,10 +738,7 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data) ...@@ -702,10 +738,7 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data)
if (options.form_data.cycle_read) if (options.form_data.cycle_read)
cnt = 0; cnt = 0;
else else
{ goto shutdown;
g_io_channel_shutdown (ch, TRUE, NULL);
return FALSE;
}
} }
do do
...@@ -726,8 +759,7 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data) ...@@ -726,8 +759,7 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data)
err = NULL; err = NULL;
} }
/* stop handling */ /* stop handling */
g_io_channel_shutdown (ch, TRUE, NULL); goto shutdown;
return FALSE;
} }
strip_new_line (string->str); strip_new_line (string->str);
...@@ -751,12 +783,14 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data) ...@@ -751,12 +783,14 @@ handle_stdin (GIOChannel * ch, GIOCondition cond, gpointer data)
} }
if ((cond != G_IO_IN) && (cond != G_IO_IN + G_IO_HUP)) if ((cond != G_IO_IN) && (cond != G_IO_IN + G_IO_HUP))
{ goto shutdown;
g_io_channel_shutdown (ch, TRUE, NULL);
return FALSE;
}
return TRUE; return TRUE;
shutdown:
g_io_channel_shutdown (ch, TRUE, NULL);
disable_changed = FALSE;
return FALSE;
} }
GtkWidget * GtkWidget *
...@@ -921,6 +955,7 @@ form_create_widget (GtkWidget * dlg) ...@@ -921,6 +955,7 @@ form_create_widget (GtkWidget * dlg)
gtk_widget_set_hexpand (e, TRUE); gtk_widget_set_hexpand (e, TRUE);
fields = g_slist_append (fields, e); fields = g_slist_append (fields, e);
g_free (buf); g_free (buf);
g_signal_connect_after (G_OBJECT (e), "toggled", G_CALLBACK (field_changed_cb), GINT_TO_POINTER (i));
} }
break; break;
...@@ -938,6 +973,7 @@ form_create_widget (GtkWidget * dlg) ...@@ -938,6 +973,7 @@ form_create_widget (GtkWidget * dlg)
gtk_widget_set_hexpand (e, TRUE); gtk_widget_set_hexpand (e, TRUE);
gtk_label_set_mnemonic_widget (GTK_LABEL (l), e); gtk_label_set_mnemonic_widget (GTK_LABEL (l), e);
fields = g_slist_append (fields, 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; break;
case YAD_FIELD_COMBO_ENTRY: case YAD_FIELD_COMBO_ENTRY:
...@@ -1295,6 +1331,8 @@ form_create_widget (GtkWidget * dlg) ...@@ -1295,6 +1331,8 @@ form_create_widget (GtkWidget * dlg)
if (options.form_data.focus_field > 0 && options.form_data.focus_field <= n_fields) 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))); gtk_widget_grab_focus (GTK_WIDGET (g_slist_nth_data (fields, options.form_data.focus_field - 1)));
disable_changed = FALSE;
return w; return w;
} }
......
...@@ -385,6 +385,8 @@ static GOptionEntry form_options[] = { ...@@ -385,6 +385,8 @@ static GOptionEntry form_options[] = {
N_("Cycled reading of stdin data"), NULL }, N_("Cycled reading of stdin data"), NULL },
{ "align-buttons", 0, 0, G_OPTION_ARG_NONE, &options.form_data.align_buttons, { "align-buttons", 0, 0, G_OPTION_ARG_NONE, &options.form_data.align_buttons,
N_("Align labels on button fields"), NULL }, 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 } { NULL }
}; };
...@@ -1734,6 +1736,7 @@ yad_options_init (void) ...@@ -1734,6 +1736,7 @@ yad_options_init (void)
options.form_data.focus_field = 1; options.form_data.focus_field = 1;
options.form_data.cycle_read = FALSE; options.form_data.cycle_read = FALSE;
options.form_data.align_buttons = FALSE; options.form_data.align_buttons = FALSE;
options.form_data.changed_action = NULL;
#ifdef HAVE_HTML #ifdef HAVE_HTML
/* Initialize html data */ /* Initialize html data */
......
...@@ -340,6 +340,7 @@ typedef struct { ...@@ -340,6 +340,7 @@ typedef struct {
guint focus_field; guint focus_field;
gboolean cycle_read; gboolean cycle_read;
gboolean align_buttons; gboolean align_buttons;
gchar *changed_action;
} YadFormData; } YadFormData;
#ifdef HAVE_HTML #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