[devhelp] BookTree: add get_selected_link()



commit 6edcc37b0dc61dab6947cc991420032a72332b84
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri May 18 14:46:25 2018 +0200

    BookTree: add get_selected_link()
    
    It will replace dh_book_tree_get_selected_book().

 devhelp/dh-book-tree.c              |   57 ++++++++++++++++++++++++----------
 devhelp/dh-book-tree.h              |    2 +
 docs/reference/devhelp-sections.txt |    1 +
 3 files changed, 43 insertions(+), 17 deletions(-)
---
diff --git a/devhelp/dh-book-tree.c b/devhelp/dh-book-tree.c
index a23d55a..aa161b3 100644
--- a/devhelp/dh-book-tree.c
+++ b/devhelp/dh-book-tree.c
@@ -100,26 +100,19 @@ book_tree_selection_changed_cb (GtkTreeSelection *selection,
                                 DhBookTree       *tree)
 {
         DhBookTreePrivate *priv = dh_book_tree_get_instance_private (tree);
-        GtkTreeIter iter;
-
-        if (gtk_tree_selection_get_selected (selection, NULL, &iter)) {
-                DhLink *link;
-
-                gtk_tree_model_get (GTK_TREE_MODEL (priv->store),
-                                    &iter,
-                                    COL_LINK, &link,
-                                    -1);
+        DhLink *link;
 
-                if (link != NULL &&
-                    link != priv->selected_link) {
-                        g_clear_pointer (&priv->selected_link, (GDestroyNotify)dh_link_unref);
-                        priv->selected_link = dh_link_ref (link);
-                        g_signal_emit (tree, signals[LINK_SELECTED], 0, link);
-                }
+        link = dh_book_tree_get_selected_link (tree);
 
-                if (link != NULL)
-                        dh_link_unref (link);
+        if (link != NULL &&
+            link != priv->selected_link) {
+                g_clear_pointer (&priv->selected_link, (GDestroyNotify)dh_link_unref);
+                priv->selected_link = dh_link_ref (link);
+                g_signal_emit (tree, signals[LINK_SELECTED], 0, link);
         }
+
+        if (link != NULL)
+                dh_link_unref (link);
 }
 
 static void
@@ -924,6 +917,36 @@ dh_book_tree_get_profile (DhBookTree *tree)
         return priv->profile;
 }
 
+/**
+ * dh_book_tree_get_selected_link:
+ * @tree: a #DhBookTree.
+ *
+ * Returns: (transfer full) (nullable): the currently selected #DhLink in @tree,
+ * or %NULL if the selection is empty or if a language group row is selected.
+ * Unref with dh_link_unref() when no longer needed.
+ * Since: 3.30
+ */
+DhLink *
+dh_book_tree_get_selected_link (DhBookTree *tree)
+{
+        GtkTreeSelection *selection;
+        GtkTreeModel *model;
+        GtkTreeIter iter;
+        DhLink *link;
+
+        g_return_val_if_fail (DH_IS_BOOK_TREE (tree), NULL);
+
+        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
+        if (!gtk_tree_selection_get_selected (selection, &model, &iter))
+                return NULL;
+
+        gtk_tree_model_get (model, &iter,
+                            COL_LINK, &link,
+                            -1);
+
+        return link;
+}
+
 static gboolean
 book_tree_find_uri_foreach_func (GtkTreeModel *model,
                                  GtkTreePath  *path,
diff --git a/devhelp/dh-book-tree.h b/devhelp/dh-book-tree.h
index fc2ca24..2f494c2 100644
--- a/devhelp/dh-book-tree.h
+++ b/devhelp/dh-book-tree.h
@@ -54,6 +54,8 @@ DhBookTree *    dh_book_tree_new                (DhProfile *profile);
 
 DhProfile *     dh_book_tree_get_profile        (DhBookTree *tree);
 
+DhLink *        dh_book_tree_get_selected_link  (DhBookTree *tree);
+
 void            dh_book_tree_select_uri         (DhBookTree  *tree,
                                                  const gchar *uri);
 
diff --git a/docs/reference/devhelp-sections.txt b/docs/reference/devhelp-sections.txt
index 6f175c2..97c47d1 100644
--- a/docs/reference/devhelp-sections.txt
+++ b/docs/reference/devhelp-sections.txt
@@ -126,6 +126,7 @@ dh_book_manager_get_type
 DhBookTree
 dh_book_tree_new
 dh_book_tree_get_profile
+dh_book_tree_get_selected_link
 dh_book_tree_select_uri
 dh_book_tree_get_selected_book
 <SUBSECTION Standard>


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