Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions data/yad.1
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,11 @@ Set the \fICMD\fP as a action when selection is changed. \fICMD\fP will be launc
\fICMD\fP may contain a special character `%s' for setting a position for arguments. By default arguments will be concatenated to the end of \fICMD\fP.
This option doesn't work with \fI--multiple\fP.
.TP
.B \-\-toggle-action=\fICMD\fP
Set the \fICMD\fP as a action when either a checkbox or radiobox is toggled. \fICMD\fP will be launched with values of all columns as an arguments.
\fICMD\fP may contain a special character `%s' for setting a position for arguments. By default arguments will be concatenated to the end of \fICMD\fP.
This option doesn't work with \fI--multiple\fP.
.TP
.B \-\-row-action=\fICMD\fP
Set the \fICMD\fP as a action when the row is added, modified or removed. First argument for the command is the name of action (\fIadd\fP, \fIedit\fP or \fIdel\fP).
The rest of command line is data from selected row. Output of this command sets the new row values.
Expand Down
208 changes: 111 additions & 97 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,83 @@ static guint n_cols = 0;

static gulong select_hndl = 0;

static gchar *
cell_get_data (GtkTreeIter *it, guint num)
{
gchar *data = NULL;
GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num);

switch (col->type)
{
case YAD_COLUMN_CHECK:
case YAD_COLUMN_RADIO:
{
gboolean bval;
gtk_tree_model_get (model, it, num, &bval, -1);
data = g_strdup (print_bool_val (bval));
break;
}
case YAD_COLUMN_NUM:
case YAD_COLUMN_SIZE:
case YAD_COLUMN_BAR:
{
gint64 nval;
gtk_tree_model_get (model, it, num, &nval, -1);
data = g_strdup_printf ("%ld", (long) nval);
break;
}
case YAD_COLUMN_FLOAT:
{
gdouble nval;
gtk_tree_model_get (model, it, num, &nval, -1);
data = g_strdup_printf ("%lf", nval);
break;
}
case YAD_COLUMN_IMAGE:
{
data = g_strdup ("''");
break;
}
default:
{
gchar *cval;
gtk_tree_model_get (model, it, num, &cval, -1);
if (cval)
data = g_shell_quote (cval);
break;
}
}

return data;
}

static gchar *
get_data_as_string (GtkTreeIter *iter)
{
GString *str;
gchar *res;
guint i;

str = g_string_new (NULL);

for (i = 0; i < n_cols; i++)
{
gchar *val = cell_get_data (iter, i);
if (val)
{
g_string_append_printf (str, "%s ", val);
g_free (val);
}
}

str->str[str->len-1] = '\0';
res = str->str;
g_string_free (str, FALSE);

return res;
}

static inline void
yad_list_add_row (GtkTreeStore *m, GtkTreeIter *it, gchar *row_id, gchar *par_id)
{
Expand Down Expand Up @@ -114,9 +191,36 @@ tooltip_cb (GtkWidget *w, gint x, gint y, gboolean mode, GtkTooltip *tip, gpoint
return FALSE;
}

static void
row_data_cb (gchar *action, GtkTreeIter *iter)
{
gchar *cmd, *args;

args = get_data_as_string (iter);
if (!args)
args = g_strdup ("");

if (g_strstr_len (action, -1, "%s"))
{
static GRegex *regex = NULL;

if (!regex)
regex = g_regex_new ("\%s", G_REGEX_OPTIMIZE, 0, NULL);
cmd = g_regex_replace_literal (regex, action, -1, 0, args, 0, NULL);
}
else
cmd = g_strdup_printf ("%s %s", action, args);
g_free (args);

run_command_async (cmd);

g_free (cmd);
}

static void
toggled_cb (GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
{
g_debug("HERE");
gint column;
gboolean fixed;
GtkTreeIter iter;
Expand All @@ -132,6 +236,9 @@ toggled_cb (GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
gtk_tree_store_set (GTK_TREE_STORE (model), &iter, column, fixed, -1);

gtk_tree_path_free (path);

if (options.list_data.toggle_action)
row_data_cb(options.list_data.toggle_action, &iter);
}

static gboolean
Expand All @@ -158,6 +265,9 @@ rtoggled_cb (GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
gtk_tree_store_set (GTK_TREE_STORE (model), &iter, column, TRUE, -1);

gtk_tree_path_free (path);

if (options.list_data.toggle_action)
row_data_cb(options.list_data.toggle_action, &iter);
}

static void
Expand Down Expand Up @@ -513,57 +623,6 @@ cell_set_data (GtkTreeIter *it, guint num, gchar *data)
}
}

static gchar *
cell_get_data (GtkTreeIter *it, guint num)
{
gchar *data = NULL;
GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num);

switch (col->type)
{
case YAD_COLUMN_CHECK:
case YAD_COLUMN_RADIO:
{
gboolean bval;
gtk_tree_model_get (model, it, num, &bval, -1);
data = g_strdup (print_bool_val (bval));
break;
}
case YAD_COLUMN_NUM:
case YAD_COLUMN_SIZE:
case YAD_COLUMN_BAR:
{
gint64 nval;
gtk_tree_model_get (model, it, num, &nval, -1);
data = g_strdup_printf ("%ld", (long) nval);
break;
}
case YAD_COLUMN_FLOAT:
{
gdouble nval;
gtk_tree_model_get (model, it, num, &nval, -1);
data = g_strdup_printf ("%lf", nval);
break;
}
case YAD_COLUMN_IMAGE:
{
data = g_strdup ("''");
break;
}
default:
{
gchar *cval;
gtk_tree_model_get (model, it, num, &cval, -1);
if (cval)
data = g_shell_quote (cval);
break;
}
}

return data;
}

static gboolean
handle_stdin (GIOChannel *channel, GIOCondition condition, gpointer data)
{
Expand Down Expand Up @@ -733,32 +792,6 @@ fill_data ()
}
}

static gchar *
get_data_as_string (GtkTreeIter *iter)
{
GString *str;
gchar *res;
guint i;

str = g_string_new (NULL);

for (i = 0; i < n_cols; i++)
{
gchar *val = cell_get_data (iter, i);
if (val)
{
g_string_append_printf (str, "%s ", val);
g_free (val);
}
}

str->str[str->len-1] = '\0';
res = str->str;
g_string_free (str, FALSE);

return res;
}

static void edit_row_cb (GtkMenuItem *item, gpointer data);

static void
Expand Down Expand Up @@ -850,30 +883,11 @@ select_cb (GtkTreeSelection *sel, gpointer data)
{
GtkTreeModel *model;
GtkTreeIter iter;
gchar *cmd, *args;

if (!gtk_tree_selection_get_selected (sel, &model, &iter))
return;

args = get_data_as_string (&iter);
if (!args)
args = g_strdup ("");

if (g_strstr_len (options.list_data.select_action, -1, "%s"))
{
static GRegex *regex = NULL;

if (!regex)
regex = g_regex_new ("\%s", G_REGEX_OPTIMIZE, 0, NULL);
cmd = g_regex_replace_literal (regex, options.list_data.select_action, -1, 0, args, 0, NULL);
}
else
cmd = g_strdup_printf ("%s %s", options.list_data.select_action, args);
g_free (args);

run_command_async (cmd);

g_free (cmd);
row_data_cb(options.list_data.select_action, &iter);
}

static void
Expand Down
3 changes: 3 additions & 0 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ static GOptionEntry list_options[] = {
N_("Set double-click action"), N_("CMD") },
{ "select-action", 0, 0, G_OPTION_ARG_STRING, &options.list_data.select_action,
N_("Set select action"), N_("CMD") },
{ "toggle-action", 0, 0, G_OPTION_ARG_STRING, &options.list_data.toggle_action,
N_("Set toggle action"), N_("CMD") },
{ "row-action", 0, 0, G_OPTION_ARG_STRING, &options.list_data.row_action,
N_("Set row action"), N_("CMD") },
{ "tree-expanded", 0, 0, G_OPTION_ARG_NONE, &options.list_data.tree_expanded,
Expand Down Expand Up @@ -1790,6 +1792,7 @@ yad_options_init (void)
options.list_data.ellipsize_cols = NULL;
options.list_data.dclick_action = NULL;
options.list_data.select_action = NULL;
options.list_data.toggle_action = NULL;
options.list_data.row_action = NULL;
options.list_data.tree_expanded = FALSE;
options.list_data.regex_search = FALSE;
Expand Down
1 change: 1 addition & 0 deletions src/yad.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ typedef struct {
gchar *ellipsize_cols;
gchar *dclick_action;
gchar *select_action;
gchar *toggle_action;
gchar *row_action;
gboolean tree_expanded;
gboolean regex_search;
Expand Down