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
51 changes: 51 additions & 0 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,57 @@ list_create_widget (GtkWidget *dlg)
if (options.list_data.tree_expanded)
gtk_tree_view_expand_all (GTK_TREE_VIEW (list_view));

/* pre-select rows if specified */
if (options.list_data.select_row && !options.list_data.no_selection)
{
GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));
GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
gchar **rows = g_strsplit (options.list_data.select_row, ",", -1);
gint i;
GtkTreePath *first_path = NULL;

/* block select_action handler during initial selection */
if (select_hndl)
g_signal_handler_block (G_OBJECT (sel), select_hndl);

for (i = 0; rows[i]; i++)
{
gint row_num = atoi (rows[i]);
if (row_num > 0)
{
GtkTreePath *path = gtk_tree_path_new_from_indices (row_num - 1, -1);
GtkTreeIter iter;

if (gtk_tree_model_get_iter (model, &iter, path))
{
gtk_tree_selection_select_path (sel, path);
if (!first_path)
first_path = gtk_tree_path_copy (path);

/* for single selection, only select first valid row */
if (gtk_tree_selection_get_mode (sel) != GTK_SELECTION_MULTIPLE)
{
gtk_tree_path_free (path);
break;
}
}
gtk_tree_path_free (path);
}
}

/* scroll to first selected row */
if (first_path)
{
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (list_view), first_path, NULL, TRUE, 0.5, 0.0);
gtk_tree_path_free (first_path);
}

if (select_hndl)
g_signal_handler_unblock (G_OBJECT (sel), select_hndl);

g_strfreev (rows);
}

return w;
}

Expand Down
3 changes: 3 additions & 0 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ static GOptionEntry list_options[] = {
N_("Set columns alignment"), N_("STRING") },
{ "header-align", 0, 0, G_OPTION_ARG_STRING, &options.list_data.hdr_align,
N_("Set headers alignment"), N_("STRING") },
{ "select-row", 0, 0, G_OPTION_ARG_STRING, &options.list_data.select_row,
N_("Pre-select row by number (1-based, comma-separated for multiple)"), N_("ROW[,ROW...]") },
{ NULL }
};

Expand Down Expand Up @@ -1885,6 +1887,7 @@ yad_options_init (void)
options.list_data.header_tips = FALSE;
options.list_data.col_align = NULL;
options.list_data.hdr_align = NULL;
options.list_data.select_row = NULL;

/* Initialize notebook data */
options.notebook_data.tabs = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/yad.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ typedef struct {
gboolean header_tips;
gchar *col_align;
gchar *hdr_align;
gchar *select_row;
} YadListData;

typedef struct {
Expand Down