From e5d8462bde55da406e171e1a375d24b552555a28 Mon Sep 17 00:00:00 2001 From: mg <102437792+mg-dev25@users.noreply.github.com> Date: Thu, 4 Dec 2025 23:19:22 +0100 Subject: [PATCH] feat(list): Add --select-row option for pre-selecting rows Adds --select-row=ROW[,ROW...] option to list dialog that allows pre-selecting one or more rows by their 1-based index. Features: - Single row selection: --select-row=3 selects the third row - Multiple row selection: --select-row=1,3,5 with --multiple flag - Automatically scrolls to first selected row - Works with both single and multi-selection modes - Blocks select-action callback during initial selection Resolves: #47, #131 (upstream) --- src/list.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/option.c | 3 +++ src/yad.h | 1 + 3 files changed, 55 insertions(+) diff --git a/src/list.c b/src/list.c index 1975bde..9e016f5 100644 --- a/src/list.c +++ b/src/list.c @@ -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; } diff --git a/src/option.c b/src/option.c index 11e48e5..0620daf 100644 --- a/src/option.c +++ b/src/option.c @@ -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 } }; @@ -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; diff --git a/src/yad.h b/src/yad.h index 649ba4f..2f6314b 100644 --- a/src/yad.h +++ b/src/yad.h @@ -417,6 +417,7 @@ typedef struct { gboolean header_tips; gchar *col_align; gchar *hdr_align; + gchar *select_row; } YadListData; typedef struct {