[gnome-control-center] region: Improved selection handling on the input chooser



commit e38f9acf39f6e7a5fa079efb081586ec215a5bcc
Author: Rui Matos <tiagomatos gmail com>
Date:   Wed Apr 18 17:11:08 2012 +0200

    region: Improved selection handling on the input chooser
    
    This makes the input chooser list always have a selected row and be
    centered on it when the filter is applied.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=662489

 panels/region/gnome-region-panel-input.c |   45 +++++++++++++++++++++--------
 1 files changed, 32 insertions(+), 13 deletions(-)
---
diff --git a/panels/region/gnome-region-panel-input.c b/panels/region/gnome-region-panel-input.c
index ab630fe..1a7d71f 100644
--- a/panels/region/gnome-region-panel-input.c
+++ b/panels/region/gnome-region-panel-input.c
@@ -360,12 +360,13 @@ static void
 filter_changed (GtkBuilder *builder)
 {
   GtkTreeModelFilter *filtered_model;
+  GtkTreeView *tree_view;
+  GtkTreeSelection *selection;
+  GtkTreeIter selected_iter;
   GtkWidget *filter_entry;
   const gchar *pattern;
   gchar *upattern;
 
-  filtered_model = GTK_TREE_MODEL_FILTER (gtk_builder_get_object (builder,
-                                                   "filtered_input_source_model"));
   filter_entry = WID ("input_source_filter");
   pattern = gtk_entry_get_text (GTK_ENTRY (filter_entry));
   upattern = g_utf8_strup (pattern, -1);
@@ -388,7 +389,24 @@ filter_changed (GtkBuilder *builder)
   search_pattern_list = g_strsplit (upattern, " ", -1);
   g_free (upattern);
 
+  filtered_model = GTK_TREE_MODEL_FILTER (gtk_builder_get_object (builder, "filtered_input_source_model"));
   gtk_tree_model_filter_refilter (filtered_model);
+
+  tree_view = GTK_TREE_VIEW (WID ("filtered_input_source_list"));
+  selection = gtk_tree_view_get_selection (tree_view);
+  if (gtk_tree_selection_get_selected (selection, NULL, &selected_iter))
+    {
+      GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (filtered_model),
+                                                   &selected_iter);
+      gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5, 0.5);
+      gtk_tree_path_free (path);
+    }
+  else
+    {
+      GtkTreeIter iter;
+      if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (filtered_model), &iter))
+        gtk_tree_selection_select_iter (selection, &iter);
+    }
 }
 
 static void
@@ -464,6 +482,7 @@ input_chooser_new (GtkWindow *main_window)
   GtkTreeSelection *selection;
   GtkListStore *model;
   GtkTreeModelFilter *filtered_model;
+  GtkTreeIter iter;
 
   builder = gtk_builder_new ();
   gtk_builder_add_from_file (builder,
@@ -493,17 +512,6 @@ input_chooser_new (GtkWindow *main_window)
   g_signal_connect (G_OBJECT (filter_entry), "icon-release",
                     G_CALLBACK (filter_clear), NULL);
 
-  selection =
-    gtk_tree_view_get_selection (GTK_TREE_VIEW (filtered_list));
-
-  g_signal_connect (G_OBJECT (selection), "changed",
-                    G_CALLBACK (selection_changed), builder);
-
-  selection_changed (selection, builder);
-
-  g_signal_connect (G_OBJECT (filtered_list), "row-activated",
-                    G_CALLBACK (row_activated), builder);
-
   filtered_model = GTK_TREE_MODEL_FILTER (gtk_builder_get_object (builder, "filtered_input_source_model"));
   model = GTK_LIST_STORE (gtk_builder_get_object (builder, "input_source_model"));
 
@@ -514,6 +522,17 @@ input_chooser_new (GtkWindow *main_window)
                                           filter_func,
                                           NULL, NULL);
 
+  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (filtered_list));
+
+  g_signal_connect (G_OBJECT (selection), "changed",
+                    G_CALLBACK (selection_changed), builder);
+
+  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (filtered_model), &iter))
+    gtk_tree_selection_select_iter (selection, &iter);
+
+  g_signal_connect (G_OBJECT (filtered_list), "row-activated",
+                    G_CALLBACK (row_activated), builder);
+
   gtk_widget_grab_focus (filter_entry);
 
   gtk_widget_show (chooser);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]