[devhelp/wip/swilmet/various-code-improvements: 1/3] book-tree: be able to jump back to the currently selected item



commit 2b7923baeb79e2add287a4aea9bb95caea59971f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Nov 11 18:04:15 2015 +0100

    book-tree: be able to jump back to the currently selected item
    
    It's already done for the search hitlist in DhSidebar. So do it also for
    the book tree, for consistency and because it's something useful.

 src/dh-book-tree.c |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/src/dh-book-tree.c b/src/dh-book-tree.c
index fbc3ef1..a2c1720 100644
--- a/src/dh-book-tree.c
+++ b/src/dh-book-tree.c
@@ -698,16 +698,62 @@ dh_book_tree_constructed (GObject *object)
         G_OBJECT_CLASS (dh_book_tree_parent_class)->constructed (object);
 }
 
+/* Make it possible to jump back to the currently selected item, useful when the
+ * html view has been scrolled away.
+ */
+static gboolean
+dh_book_tree_button_press_event (GtkWidget      *widget,
+                                 GdkEventButton *event)
+{
+        DhBookTree *tree = DH_BOOK_TREE (widget);
+        DhBookTreePrivate *priv = dh_book_tree_get_instance_private (tree);
+        GtkTreePath *path;
+
+        gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (tree),
+                                       event->x, event->y,
+                                       &path, NULL, NULL, NULL);
+
+        if (path != NULL) {
+                GtkTreeIter iter;
+                DhLink *link;
+
+                gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
+
+                gtk_tree_model_get (GTK_TREE_MODEL (priv->store),
+                                    &iter,
+                                    COL_LINK, &link,
+                                    -1);
+
+                if (link != NULL) {
+                        priv->selected_link = link;
+                        g_signal_emit (tree, signals[LINK_SELECTED], 0, link);
+                }
+
+                gtk_tree_path_free (path);
+        }
+
+        /* Always propagate the event so the tree view can update
+         * the selection etc.
+         */
+        if (GTK_WIDGET_CLASS (dh_book_tree_parent_class)->button_press_event == NULL)
+                return GDK_EVENT_PROPAGATE;
+
+        return GTK_WIDGET_CLASS (dh_book_tree_parent_class)->button_press_event (widget, event);
+}
+
 static void
 dh_book_tree_class_init (DhBookTreeClass *klass)
 {
         GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
         object_class->dispose = dh_book_tree_dispose;
         object_class->get_property = dh_book_tree_get_property;
         object_class->set_property = dh_book_tree_set_property;
         object_class->constructed = dh_book_tree_constructed;
 
+        widget_class->button_press_event = dh_book_tree_button_press_event;
+
         g_object_class_install_property (object_class,
                                          PROP_BOOK_MANAGER,
                                          g_param_spec_object ("book-manager",


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