[devhelp] Move bind_sidebar_and_notebook() to the libdevhelp



commit 4912d0a338dbead0215790cd81e53e75718a2bcb
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jun 8 12:54:19 2018 +0200

    Move bind_sidebar_and_notebook() to the libdevhelp
    
    See the comments in the code for the API design and implementation
    rationale.

 devhelp/devhelp.h                   |   1 +
 devhelp/dh-util-lib.c               | 130 ++++++++++++++++++++++++++++++++++++
 devhelp/dh-util-lib.h               |   6 ++
 devhelp/dh-window.c                 |  75 +++++++++++++++++++++
 devhelp/dh-window.h                 |  35 ++++++++++
 devhelp/meson.build                 |   6 +-
 docs/reference/devhelp-docs.xml     |   5 ++
 docs/reference/devhelp-sections.txt |   5 ++
 po/POTFILES.in                      |   1 +
 src/dh-util-app.c                   | 130 ------------------------------------
 src/dh-util-app.h                   |   4 --
 src/dh-window.c                     |   2 +-
 src/dh-window.h                     |   6 +-
 13 files changed, 266 insertions(+), 140 deletions(-)
---
diff --git a/devhelp/devhelp.h b/devhelp/devhelp.h
index 4ce30ef0..bc6c4a6f 100644
--- a/devhelp/devhelp.h
+++ b/devhelp/devhelp.h
@@ -46,5 +46,6 @@
 #include <devhelp/dh-tab.h>
 #include <devhelp/dh-tab-label.h>
 #include <devhelp/dh-web-view.h>
+#include <devhelp/dh-window.h>
 
 #endif /* DEVHELP_H */
diff --git a/devhelp/dh-util-lib.c b/devhelp/dh-util-lib.c
index 39758677..4a929d5a 100644
--- a/devhelp/dh-util-lib.c
+++ b/devhelp/dh-util-lib.c
@@ -216,3 +216,133 @@ _dh_util_get_possible_index_files (GFile *book_directory)
         g_free (directory_name);
         return list;
 }
+
+static void
+sidebar_link_selected_cb (DhSidebar  *sidebar,
+                          DhLink     *link,
+                          DhNotebook *notebook)
+{
+        gchar *uri;
+        DhWebView *web_view;
+
+        uri = dh_link_get_uri (link);
+        if (uri == NULL)
+                return;
+
+        web_view = dh_notebook_get_active_web_view (notebook);
+        if (web_view != NULL)
+                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), uri);
+
+        g_free (uri);
+}
+
+static void
+sync_active_web_view_uri_to_sidebar (DhNotebook *notebook,
+                                     DhSidebar  *sidebar)
+{
+        DhWebView *web_view;
+        const gchar *uri = NULL;
+
+        g_signal_handlers_block_by_func (sidebar,
+                                         sidebar_link_selected_cb,
+                                         notebook);
+
+        web_view = dh_notebook_get_active_web_view (notebook);
+        if (web_view != NULL)
+                uri = webkit_web_view_get_uri (WEBKIT_WEB_VIEW (web_view));
+        if (uri != NULL)
+                dh_sidebar_select_uri (sidebar, uri);
+
+        g_signal_handlers_unblock_by_func (sidebar,
+                                           sidebar_link_selected_cb,
+                                           notebook);
+}
+
+static DhNotebook *
+get_notebook_containing_web_view (DhWebView *web_view)
+{
+        GtkWidget *widget;
+
+        widget = GTK_WIDGET (web_view);
+
+        while (widget != NULL) {
+                widget = gtk_widget_get_parent (widget);
+
+                if (DH_IS_NOTEBOOK (widget))
+                        return DH_NOTEBOOK (widget);
+        }
+
+        g_return_val_if_reached (NULL);
+}
+
+static void
+web_view_load_changed_cb (DhWebView       *web_view,
+                          WebKitLoadEvent  load_event,
+                          DhSidebar       *sidebar)
+{
+        DhNotebook *notebook;
+
+        notebook = get_notebook_containing_web_view (web_view);
+
+        if (load_event == WEBKIT_LOAD_COMMITTED &&
+            web_view == dh_notebook_get_active_web_view (notebook)) {
+                sync_active_web_view_uri_to_sidebar (notebook, sidebar);
+        }
+}
+
+static void
+notebook_page_added_after_cb (GtkNotebook *notebook,
+                              GtkWidget   *child,
+                              guint        page_num,
+                              DhSidebar   *sidebar)
+{
+        DhTab *tab;
+        DhWebView *web_view;
+
+        g_return_if_fail (DH_IS_TAB (child));
+
+        tab = DH_TAB (child);
+        web_view = dh_tab_get_web_view (tab);
+
+        g_signal_connect_object (web_view,
+                                 "load-changed",
+                                 G_CALLBACK (web_view_load_changed_cb),
+                                 sidebar,
+                                 0);
+}
+
+static void
+notebook_switch_page_after_cb (DhNotebook *notebook,
+                               GtkWidget  *new_page,
+                               guint       new_page_num,
+                               DhSidebar  *sidebar)
+{
+        sync_active_web_view_uri_to_sidebar (notebook, sidebar);
+}
+
+void
+_dh_util_bind_sidebar_and_notebook (DhSidebar  *sidebar,
+                                    DhNotebook *notebook)
+{
+        g_return_if_fail (DH_IS_SIDEBAR (sidebar));
+        g_return_if_fail (DH_IS_NOTEBOOK (notebook));
+        g_return_if_fail (dh_notebook_get_active_tab (notebook) == NULL);
+
+        g_signal_connect_object (sidebar,
+                                 "link-selected",
+                                 G_CALLBACK (sidebar_link_selected_cb),
+                                 notebook,
+                                 0);
+
+        g_signal_connect_object (notebook,
+                                 "page-added",
+                                 G_CALLBACK (notebook_page_added_after_cb),
+                                 sidebar,
+                                 G_CONNECT_AFTER);
+
+        g_signal_connect_object (notebook,
+                                 "switch-page",
+                                 G_CALLBACK (notebook_switch_page_after_cb),
+                                 sidebar,
+                                 G_CONNECT_AFTER);
+}
diff --git a/devhelp/dh-util-lib.h b/devhelp/dh-util-lib.h
index a4306826..6f59ed8d 100644
--- a/devhelp/dh-util-lib.h
+++ b/devhelp/dh-util-lib.h
@@ -24,6 +24,8 @@
 #define DH_UTIL_LIB_H
 
 #include <gio/gio.h>
+#include "dh-notebook.h"
+#include "dh-sidebar.h"
 
 G_BEGIN_DECLS
 
@@ -48,6 +50,10 @@ void            _dh_util_free_book_tree                 (GNode *book_tree);
 G_GNUC_INTERNAL
 GSList *        _dh_util_get_possible_index_files       (GFile *book_directory);
 
+G_GNUC_INTERNAL
+void            _dh_util_bind_sidebar_and_notebook      (DhSidebar  *sidebar,
+                                                         DhNotebook *notebook);
+
 G_END_DECLS
 
 #endif /* DH_UTIL_LIB_H */
diff --git a/devhelp/dh-window.c b/devhelp/dh-window.c
new file mode 100644
index 00000000..e65e80ea
--- /dev/null
+++ b/devhelp/dh-window.c
@@ -0,0 +1,75 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/*
+ * This file is part of Devhelp.
+ *
+ * Copyright (C) 2018 Sébastien Wilmet <swilmet gnome org>
+ *
+ * Devhelp is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
+ *
+ * Devhelp is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Devhelp.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dh-window.h"
+#include "dh-util-lib.h"
+
+/**
+ * SECTION:dh-window
+ * @Title: DhWindow
+ * @Short_description: For the main application window
+ *
+ * Functions for the main application window.
+ */
+
+/**
+ * dh_window_bind_sidebar_and_notebook:
+ * @sidebar: a #DhSidebar.
+ * @notebook: an empty #DhNotebook.
+ *
+ * Binds @sidebar and @notebook:
+ * - When the #DhSidebar::link-selected signal is emitted, open the URI in the
+ *   active #DhWebView.
+ * - On #GtkNotebook::switch-page or when the user clicks on a link, calls
+ *   dh_sidebar_select_uri() with the new active URI.
+ *
+ * You need to call this function when the #DhNotebook is empty, i.e. before
+ * adding the first #DhTab.
+ *
+ * Note that this function doesn't take a “self” window parameter, to be more
+ * flexible: it is possible to have several pairs of #DhSidebar/#DhNotebook per
+ * window, to show different #DhProfile's.
+ *
+ * Since: 3.30
+ */
+void
+dh_window_bind_sidebar_and_notebook (DhSidebar  *sidebar,
+                                     DhNotebook *notebook)
+{
+        g_return_if_fail (DH_IS_SIDEBAR (sidebar));
+        g_return_if_fail (DH_IS_NOTEBOOK (notebook));
+        g_return_if_fail (dh_notebook_get_active_tab (notebook) == NULL);
+
+        /* Have the implementation separate from dh-window, because it is
+         * planned to have a real DhWindow class in the libdevhelp that will
+         * have similar signal handlers, it would be confusing to have two times
+         * the same signal handlers.
+         *
+         * API design:
+         * But the public function belongs to the window, since the window is
+         * the container containing the two widgets. Another container
+         * containing the two widgets, in the Devhelp app, is the horizontal
+         * GtkPaned, but it would have been less flexible to create a GtkPaned
+         * subclass with the bind() function, because it would have forced an
+         * IDE to use GtkPaned to be able to bind the two widgets (an IDE may
+         * want to use something else than a GtkPaned).
+         */
+        _dh_util_bind_sidebar_and_notebook (sidebar, notebook);
+}
diff --git a/devhelp/dh-window.h b/devhelp/dh-window.h
new file mode 100644
index 00000000..4b3b988e
--- /dev/null
+++ b/devhelp/dh-window.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/*
+ * This file is part of Devhelp.
+ *
+ * Copyright (C) 2018 Sébastien Wilmet <swilmet gnome org>
+ *
+ * Devhelp is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
+ *
+ * Devhelp is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Devhelp.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DH_WINDOW_LIB_H
+#define DH_WINDOW_LIB_H
+
+#include <glib.h>
+#include <devhelp/dh-notebook.h>
+#include <devhelp/dh-sidebar.h>
+
+G_BEGIN_DECLS
+
+void    dh_window_bind_sidebar_and_notebook     (DhSidebar  *sidebar,
+                                                 DhNotebook *notebook);
+
+G_END_DECLS
+
+#endif /* DH_WINDOW_LIB_H */
diff --git a/devhelp/meson.build b/devhelp/meson.build
index 89c82b14..755362b8 100644
--- a/devhelp/meson.build
+++ b/devhelp/meson.build
@@ -20,7 +20,8 @@ libdevhelp_public_headers = [
         'dh-sidebar.h',
         'dh-tab.h',
         'dh-tab-label.h',
-        'dh-web-view.h'
+        'dh-web-view.h',
+        'dh-window.h'
 ]
 
 libdevhelp_public_c_files = [
@@ -44,7 +45,8 @@ libdevhelp_public_c_files = [
         'dh-sidebar.c',
         'dh-tab.c',
         'dh-tab-label.c',
-        'dh-web-view.c'
+        'dh-web-view.c',
+        'dh-window.c'
 ]
 
 libdevhelp_private_c_files = [
diff --git a/docs/reference/devhelp-docs.xml b/docs/reference/devhelp-docs.xml
index 2c8e7913..50f20f87 100644
--- a/docs/reference/devhelp-docs.xml
+++ b/docs/reference/devhelp-docs.xml
@@ -51,6 +51,11 @@
       <xi:include href="xml/dh-web-view.xml"/>
     </chapter>
 
+    <chapter id="putting-it-all-together">
+      <title>Putting It All Together</title>
+      <xi:include href="xml/dh-window.xml"/>
+    </chapter>
+
     <chapter id="assistant">
       <title>Assistant</title>
       <xi:include href="xml/dh-assistant-view.xml"/>
diff --git a/docs/reference/devhelp-sections.txt b/docs/reference/devhelp-sections.txt
index 755db178..aa98c878 100644
--- a/docs/reference/devhelp-sections.txt
+++ b/docs/reference/devhelp-sections.txt
@@ -409,3 +409,8 @@ DhWebViewClass
 DhWebViewPrivate
 dh_web_view_get_type
 </SECTION>
+
+<SECTION>
+<FILE>dh-window</FILE>
+dh_window_bind_sidebar_and_notebook
+</SECTION>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index cec438e0..e1aa3fba 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -30,6 +30,7 @@ devhelp/dh-tab.c
 devhelp/dh-tab-label.c
 devhelp/dh-util-lib.c
 devhelp/dh-web-view.c
+devhelp/dh-window.c
 plugins/gedit-plugin/devhelp.plugin.desktop.in
 plugins/gedit-plugin/devhelp.py
 src/dh-app.c
diff --git a/src/dh-util-app.c b/src/dh-util-app.c
index 500a9265..d129cfb4 100644
--- a/src/dh-util-app.c
+++ b/src/dh-util-app.c
@@ -125,133 +125,3 @@ dh_util_window_settings_restore (GtkWindow *gtk_window,
         if (has_maximized_key && g_settings_get_boolean (settings, "maximized"))
                 gtk_window_maximize (gtk_window);
 }
-
-static void
-sidebar_link_selected_cb (DhSidebar  *sidebar,
-                          DhLink     *link,
-                          DhNotebook *notebook)
-{
-        gchar *uri;
-        DhWebView *web_view;
-
-        uri = dh_link_get_uri (link);
-        if (uri == NULL)
-                return;
-
-        web_view = dh_notebook_get_active_web_view (notebook);
-        if (web_view != NULL)
-                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), uri);
-
-        g_free (uri);
-}
-
-static void
-sync_active_web_view_uri_to_sidebar (DhNotebook *notebook,
-                                     DhSidebar  *sidebar)
-{
-        DhWebView *web_view;
-        const gchar *uri = NULL;
-
-        g_signal_handlers_block_by_func (sidebar,
-                                         sidebar_link_selected_cb,
-                                         notebook);
-
-        web_view = dh_notebook_get_active_web_view (notebook);
-        if (web_view != NULL)
-                uri = webkit_web_view_get_uri (WEBKIT_WEB_VIEW (web_view));
-        if (uri != NULL)
-                dh_sidebar_select_uri (sidebar, uri);
-
-        g_signal_handlers_unblock_by_func (sidebar,
-                                           sidebar_link_selected_cb,
-                                           notebook);
-}
-
-static DhNotebook *
-get_notebook_containing_web_view (DhWebView *web_view)
-{
-        GtkWidget *widget;
-
-        widget = GTK_WIDGET (web_view);
-
-        while (widget != NULL) {
-                widget = gtk_widget_get_parent (widget);
-
-                if (DH_IS_NOTEBOOK (widget))
-                        return DH_NOTEBOOK (widget);
-        }
-
-        g_return_val_if_reached (NULL);
-}
-
-static void
-web_view_load_changed_cb (DhWebView       *web_view,
-                          WebKitLoadEvent  load_event,
-                          DhSidebar       *sidebar)
-{
-        DhNotebook *notebook;
-
-        notebook = get_notebook_containing_web_view (web_view);
-
-        if (load_event == WEBKIT_LOAD_COMMITTED &&
-            web_view == dh_notebook_get_active_web_view (notebook)) {
-                sync_active_web_view_uri_to_sidebar (notebook, sidebar);
-        }
-}
-
-static void
-notebook_page_added_after_cb (GtkNotebook *notebook,
-                              GtkWidget   *child,
-                              guint        page_num,
-                              DhSidebar   *sidebar)
-{
-        DhTab *tab;
-        DhWebView *web_view;
-
-        g_return_if_fail (DH_IS_TAB (child));
-
-        tab = DH_TAB (child);
-        web_view = dh_tab_get_web_view (tab);
-
-        g_signal_connect_object (web_view,
-                                 "load-changed",
-                                 G_CALLBACK (web_view_load_changed_cb),
-                                 sidebar,
-                                 0);
-}
-
-static void
-notebook_switch_page_after_cb (DhNotebook *notebook,
-                               GtkWidget  *new_page,
-                               guint       new_page_num,
-                               DhSidebar  *sidebar)
-{
-        sync_active_web_view_uri_to_sidebar (notebook, sidebar);
-}
-
-void
-dh_util_bind_sidebar_and_notebook (DhSidebar  *sidebar,
-                                   DhNotebook *notebook)
-{
-        g_return_if_fail (DH_IS_SIDEBAR (sidebar));
-        g_return_if_fail (DH_IS_NOTEBOOK (notebook));
-        g_return_if_fail (dh_notebook_get_active_tab (notebook) == NULL);
-
-        g_signal_connect_object (sidebar,
-                                 "link-selected",
-                                 G_CALLBACK (sidebar_link_selected_cb),
-                                 notebook,
-                                 0);
-
-        g_signal_connect_object (notebook,
-                                 "page-added",
-                                 G_CALLBACK (notebook_page_added_after_cb),
-                                 sidebar,
-                                 G_CONNECT_AFTER);
-
-        g_signal_connect_object (notebook,
-                                 "switch-page",
-                                 G_CALLBACK (notebook_switch_page_after_cb),
-                                 sidebar,
-                                 G_CONNECT_AFTER);
-}
diff --git a/src/dh-util-app.h b/src/dh-util-app.h
index 85b748e8..a76173fe 100644
--- a/src/dh-util-app.h
+++ b/src/dh-util-app.h
@@ -24,7 +24,6 @@
 #define DH_UTIL_APP_H
 
 #include <gtk/gtk.h>
-#include <devhelp/devhelp.h>
 
 G_BEGIN_DECLS
 
@@ -34,9 +33,6 @@ void    dh_util_window_settings_save            (GtkWindow *window,
 void    dh_util_window_settings_restore         (GtkWindow *gtk_window,
                                                  GSettings *settings);
 
-void    dh_util_bind_sidebar_and_notebook       (DhSidebar  *sidebar,
-                                                 DhNotebook *notebook);
-
 G_END_DECLS
 
 #endif /* DH_UTIL_APP_H */
diff --git a/src/dh-window.c b/src/dh-window.c
index 3434d8ee..bb543ab9 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -674,7 +674,7 @@ dh_window_init (DhWindow *window)
         priv->notebook = dh_notebook_new (NULL);
         gtk_widget_show (GTK_WIDGET (priv->notebook));
 
-        dh_util_bind_sidebar_and_notebook (priv->sidebar, priv->notebook);
+        dh_window_bind_sidebar_and_notebook (priv->sidebar, priv->notebook);
 
         g_signal_connect_after (priv->notebook,
                                 "page-added",
diff --git a/src/dh-window.h b/src/dh-window.h
index d568fbbb..4b93474d 100644
--- a/src/dh-window.h
+++ b/src/dh-window.h
@@ -21,8 +21,8 @@
  * along with Devhelp.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef DH_WINDOW_H
-#define DH_WINDOW_H
+#ifndef DH_WINDOW_APP_H
+#define DH_WINDOW_APP_H
 
 #include <gtk/gtk.h>
 
@@ -58,4 +58,4 @@ void            _dh_window_display_uri          (DhWindow    *window,
 
 G_END_DECLS
 
-#endif /* DH_WINDOW_H */
+#endif /* DH_WINDOW_APP_H */


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