[devhelp] Create DhTab class



commit 61d4283fe842700f5e86404fd95bc275b70e096b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Feb 10 13:12:53 2018 +0100

    Create DhTab class
    
    To have less code in DhWindow, and to avoid using g_object_set_data()
    (it was a bit ugly in this case).

 docs/reference/Makefile.am |    1 +
 po/POTFILES.in             |    1 +
 src/Makefile.am            |    2 +
 src/dh-tab.c               |   93 ++++++++++++++++++++++++++++++
 src/dh-tab.h               |   61 ++++++++++++++++++++
 src/dh-window.c            |  134 ++++++++++++++++++--------------------------
 6 files changed, 212 insertions(+), 80 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 1a07ad1..89b27a2 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -54,6 +54,7 @@ IGNORE_HFILES = \
        dh-preferences.h \
        dh-search-context.h \
        dh-settings.h \
+       dh-tab.h \
        dh-util.h \
        dh-web-view.h \
        dh-window.h
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 73c12ea..8e890f0 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -19,6 +19,7 @@ src/dh-parser.c
 src/dh-preferences.ui
 src/dh-search-context.c
 src/dh-sidebar.c
+src/dh-tab.c
 src/dh-util.c
 src/dh-web-view.c
 src/dh-window.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 38cb976..6dabb3c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -69,6 +69,7 @@ libdevhelp_built_sources =                    \
 app_headers =                  \
        dh-app.h                \
        dh-assistant.h          \
+       dh-tab.h                \
        dh-web-view.h           \
        dh-window.h             \
        $(NULL)
@@ -77,6 +78,7 @@ app_c_files =                 \
        dh-app.c                \
        dh-assistant.c          \
        dh-main.c               \
+       dh-tab.c                \
        dh-web-view.c           \
        dh-window.c             \
        $(NULL)
diff --git a/src/dh-tab.c b/src/dh-tab.c
new file mode 100644
index 0000000..6ec7f41
--- /dev/null
+++ b/src/dh-tab.c
@@ -0,0 +1,93 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2018 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program 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 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dh-tab.h"
+
+struct _DhTabPrivate {
+        GtkInfoBar *info_bar;
+        DhWebView *web_view;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (DhTab, dh_tab, GTK_TYPE_GRID)
+
+static void
+dh_tab_dispose (GObject *object)
+{
+        DhTab *tab = DH_TAB (object);
+
+        tab->priv->info_bar = NULL;
+        tab->priv->web_view = NULL;
+
+        G_OBJECT_CLASS (dh_tab_parent_class)->dispose (object);
+}
+
+static void
+dh_tab_class_init (DhTabClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+        object_class->dispose = dh_tab_dispose;
+}
+
+static void
+dh_tab_init (DhTab *tab)
+{
+        tab->priv = dh_tab_get_instance_private (tab);
+
+        gtk_orientable_set_orientation (GTK_ORIENTABLE (tab), GTK_ORIENTATION_VERTICAL);
+
+        /* GtkInfoBar */
+        tab->priv->info_bar = GTK_INFO_BAR (gtk_info_bar_new ());
+        gtk_widget_set_no_show_all (GTK_WIDGET (tab->priv->info_bar), TRUE);
+        gtk_info_bar_set_show_close_button (tab->priv->info_bar, TRUE);
+        gtk_info_bar_set_message_type (tab->priv->info_bar, GTK_MESSAGE_ERROR);
+
+        g_signal_connect (tab->priv->info_bar,
+                          "response",
+                          G_CALLBACK (gtk_widget_hide),
+                          NULL);
+
+        /* DhWebView */
+        tab->priv->web_view = dh_web_view_new ();
+        gtk_widget_show (GTK_WIDGET (tab->priv->web_view));
+
+        gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (tab->priv->info_bar));
+        gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (tab->priv->web_view));
+}
+
+DhTab *
+dh_tab_new (void)
+{
+        return g_object_new (DH_TYPE_TAB, NULL);
+}
+
+DhWebView *
+dh_tab_get_web_view (DhTab *tab)
+{
+        g_return_val_if_fail (DH_IS_TAB (tab), NULL);
+
+        return tab->priv->web_view;
+}
+
+GtkInfoBar *
+dh_tab_get_info_bar (DhTab *tab)
+{
+        g_return_val_if_fail (DH_IS_TAB (tab), NULL);
+
+        return tab->priv->info_bar;
+}
diff --git a/src/dh-tab.h b/src/dh-tab.h
new file mode 100644
index 0000000..f7d472e
--- /dev/null
+++ b/src/dh-tab.h
@@ -0,0 +1,61 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2018 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program 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 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DH_TAB_H
+#define DH_TAB_H
+
+#include <gtk/gtk.h>
+#include "dh-web-view.h"
+
+G_BEGIN_DECLS
+
+#define DH_TYPE_TAB             (dh_tab_get_type ())
+#define DH_TAB(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), DH_TYPE_TAB, DhTab))
+#define DH_TAB_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), DH_TYPE_TAB, DhTabClass))
+#define DH_IS_TAB(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DH_TYPE_TAB))
+#define DH_IS_TAB_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), DH_TYPE_TAB))
+#define DH_TAB_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), DH_TYPE_TAB, DhTabClass))
+
+typedef struct _DhTab         DhTab;
+typedef struct _DhTabClass    DhTabClass;
+typedef struct _DhTabPrivate  DhTabPrivate;
+
+struct _DhTab {
+        GtkGrid parent;
+
+        DhTabPrivate *priv;
+};
+
+struct _DhTabClass {
+        GtkGridClass parent_class;
+
+       /* Padding for future expansion */
+        gpointer padding[12];
+};
+
+GType           dh_tab_get_type         (void);
+
+DhTab *         dh_tab_new              (void);
+
+DhWebView *     dh_tab_get_web_view     (DhTab *tab);
+
+GtkInfoBar *    dh_tab_get_info_bar     (DhTab *tab);
+
+G_END_DECLS
+
+#endif /* DH_TAB_H */
diff --git a/src/dh-window.c b/src/dh-window.c
index ab3eb13..874b94a 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -25,6 +25,7 @@
 #include "dh-book-manager.h"
 #include "dh-settings.h"
 #include "dh-sidebar.h"
+#include "dh-tab.h"
 #include "dh-util.h"
 #include "dh-web-view.h"
 
@@ -59,9 +60,6 @@ static void           window_tab_set_title           (DhWindow        *window,
                                                       WebKitWebView   *web_view,
                                                       const gchar     *title);
 
-#define TAB_WEB_VIEW_KEY "web_view"
-#define TAB_INFO_BAR_KEY "info_bar"
-
 G_DEFINE_TYPE_WITH_PRIVATE (DhWindow, dh_window, GTK_TYPE_APPLICATION_WINDOW);
 
 static void
@@ -114,36 +112,35 @@ dh_window_class_init (DhWindowClass *klass)
         gtk_widget_class_bind_template_child_private (widget_class, DhWindow, notebook);
 }
 
-static DhWebView *
-get_active_web_view (DhWindow *window)
+static DhTab *
+get_active_tab (DhWindow *window)
 {
         DhWindowPrivate *priv = dh_window_get_instance_private (window);
         gint page_num;
-        GtkWidget *page;
 
         page_num = gtk_notebook_get_current_page (priv->notebook);
         if (page_num == -1)
                 return NULL;
 
-        page = gtk_notebook_get_nth_page (priv->notebook, page_num);
+        return DH_TAB (gtk_notebook_get_nth_page (priv->notebook, page_num));
+}
 
-        return g_object_get_data (G_OBJECT (page), TAB_WEB_VIEW_KEY);
+static DhWebView *
+get_active_web_view (DhWindow *window)
+{
+        DhTab *tab;
+
+        tab = get_active_tab (window);
+        return tab != NULL ? dh_tab_get_web_view (tab) : NULL;
 }
 
 static GtkWidget *
 window_get_active_info_bar (DhWindow *window)
 {
-        DhWindowPrivate *priv = dh_window_get_instance_private (window);
-        gint page_num;
-        GtkWidget *page;
+        DhTab *tab;
 
-        page_num = gtk_notebook_get_current_page (priv->notebook);
-        if (page_num == -1)
-                return NULL;
-
-        page = gtk_notebook_get_nth_page (priv->notebook, page_num);
-
-        return g_object_get_data (G_OBJECT (page), TAB_INFO_BAR_KEY);
+        tab = get_active_tab (window);
+        return tab != NULL ? GTK_WIDGET (dh_tab_get_info_bar (tab)) : NULL;
 }
 
 static void
@@ -476,12 +473,12 @@ settings_fonts_changed_cb (DhSettings  *settings,
         n_pages = gtk_notebook_get_n_pages (priv->notebook);
 
         for (page_num = 0; page_num < n_pages; page_num++) {
-                GtkWidget *page;
-                WebKitWebView *view;
+                DhTab *tab;
+                WebKitWebView *web_view;
 
-                page = gtk_notebook_get_nth_page (priv->notebook, page_num);
-                view = g_object_get_data (G_OBJECT (page), TAB_WEB_VIEW_KEY);
-                dh_util_view_set_font (view, font_name_fixed, font_name_variable);
+                tab = DH_TAB (gtk_notebook_get_nth_page (priv->notebook, page_num));
+                web_view = WEBKIT_WEB_VIEW (dh_tab_get_web_view (tab));
+                dh_util_view_set_font (web_view, font_name_fixed, font_name_variable);
         }
 }
 
@@ -536,13 +533,10 @@ update_search_in_all_web_views (DhWindow *window)
         n_pages = gtk_notebook_get_n_pages (priv->notebook);
 
         for (page_num = 0; page_num < n_pages; page_num++) {
-                GtkWidget *page;
-                DhWebView *view;
+                DhTab *tab;
 
-                page = gtk_notebook_get_nth_page (priv->notebook, page_num);
-                view = g_object_get_data (G_OBJECT (page), TAB_WEB_VIEW_KEY);
-
-                update_search_in_web_view (window, view);
+                tab = DH_TAB (gtk_notebook_get_nth_page (priv->notebook, page_num));
+                update_search_in_web_view (window, dh_tab_get_web_view (tab));
         }
 }
 
@@ -629,13 +623,13 @@ notebook_switch_page_after_cb (GtkNotebook *notebook,
         update_search_in_active_web_view (window);
 
         if (new_page != NULL) {
-                WebKitWebView *new_web_view;
+                DhWebView *web_view;
                 const gchar *uri;
 
-                new_web_view = g_object_get_data (G_OBJECT (new_page), TAB_WEB_VIEW_KEY);
+                web_view = dh_tab_get_web_view (DH_TAB (new_page));
 
                 /* Sync the book tree */
-                uri = webkit_web_view_get_uri (new_web_view);
+                uri = webkit_web_view_get_uri (WEBKIT_WEB_VIEW (web_view));
                 if (uri != NULL)
                         dh_sidebar_select_uri (priv->sidebar, uri);
         }
@@ -947,93 +941,73 @@ window_open_new_tab (DhWindow    *window,
                      gboolean     switch_focus)
 {
         DhWindowPrivate *priv = dh_window_get_instance_private (window);
-        DhWebView *view;
+        DhTab *tab;
+        DhWebView *web_view;
         DhSettings *settings;
         gchar *font_fixed = NULL;
         gchar *font_variable = NULL;
-        GtkInfoBar *info_bar;
-        GtkWidget *vgrid;
         GtkWidget *label;
         gint page_num;
         WebKitBackForwardList *back_forward_list;
 
-        /* Prepare the web view */
-        view = dh_web_view_new ();
-        gtk_widget_show (GTK_WIDGET (view));
+        tab = dh_tab_new ();
+        gtk_widget_show (GTK_WIDGET (tab));
+
+        web_view = dh_tab_get_web_view (tab);
 
         /* Set font */
         settings = dh_settings_get_singleton ();
         dh_settings_get_selected_fonts (settings, &font_fixed, &font_variable);
-        dh_util_view_set_font (WEBKIT_WEB_VIEW (view), font_fixed, font_variable);
+        dh_util_view_set_font (WEBKIT_WEB_VIEW (web_view), font_fixed, font_variable);
         g_free (font_fixed);
         g_free (font_variable);
 
-        /* Prepare the info bar */
-        info_bar = GTK_INFO_BAR (gtk_info_bar_new ());
-        gtk_widget_set_no_show_all (GTK_WIDGET (info_bar), TRUE);
-        gtk_info_bar_set_show_close_button (info_bar, TRUE);
-        gtk_info_bar_set_message_type (info_bar, GTK_MESSAGE_ERROR);
-
-        g_signal_connect (info_bar,
-                          "response",
-                          G_CALLBACK (gtk_widget_hide),
-                          NULL);
-
-        /* Tab container */
-        vgrid = gtk_grid_new ();
-        gtk_orientable_set_orientation (GTK_ORIENTABLE (vgrid), GTK_ORIENTATION_VERTICAL);
-        gtk_widget_show (vgrid);
-
-        /* TODO: create DhTab class, subclass of GtkGrid. */
-        g_object_set_data (G_OBJECT (vgrid), TAB_WEB_VIEW_KEY, view);
-        g_object_set_data (G_OBJECT (vgrid), TAB_INFO_BAR_KEY, info_bar);
-
-        gtk_container_add (GTK_CONTAINER (vgrid), GTK_WIDGET (info_bar));
-        gtk_container_add (GTK_CONTAINER (vgrid), GTK_WIDGET (view));
-
-        label = window_new_tab_label (window, _("Empty Page"), vgrid);
+        label = window_new_tab_label (window, _("Empty Page"), GTK_WIDGET (tab));
         gtk_widget_show_all (label);
 
-        g_signal_connect (view,
+        g_signal_connect (web_view,
                           "notify::title",
                           G_CALLBACK (window_web_view_title_changed_cb),
                           window);
 
-        g_signal_connect (view,
+        g_signal_connect (web_view,
                           "notify::zoom-level",
                           G_CALLBACK (web_view_zoom_level_notify_cb),
                           window);
 
-        g_signal_connect (view,
+        g_signal_connect (web_view,
                           "button-press-event",
                           G_CALLBACK (window_web_view_button_press_event_cb),
                           window);
 
-        g_signal_connect (view,
+        g_signal_connect (web_view,
                           "decide-policy",
                           G_CALLBACK (window_web_view_decide_policy_cb),
                           window);
 
-        g_signal_connect (view,
+        g_signal_connect (web_view,
                           "load-changed",
                           G_CALLBACK (window_web_view_load_changed_cb),
                           window);
 
-        g_signal_connect (view,
+        g_signal_connect (web_view,
                           "load-failed",
                           G_CALLBACK (window_web_view_load_failed_cb),
                           window);
 
-        back_forward_list = webkit_web_view_get_back_forward_list (WEBKIT_WEB_VIEW (view));
+        back_forward_list = webkit_web_view_get_back_forward_list (WEBKIT_WEB_VIEW (web_view));
         g_signal_connect_object (back_forward_list,
                                  "changed",
                                  G_CALLBACK (window_update_back_forward_actions_sensitivity),
                                  window,
                                  G_CONNECT_AFTER | G_CONNECT_SWAPPED);
 
-        page_num = gtk_notebook_append_page (priv->notebook, vgrid, label);
+        page_num = gtk_notebook_append_page (priv->notebook,
+                                             GTK_WIDGET (tab),
+                                             label);
+
         gtk_container_child_set (GTK_CONTAINER (priv->notebook),
-                                 vgrid,
+                                 GTK_WIDGET (tab),
                                  "tab-expand", TRUE,
                                  "reorderable", TRUE,
                                  NULL);
@@ -1044,9 +1018,9 @@ window_open_new_tab (DhWindow    *window,
                 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
 
         if (location != NULL)
-                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (view), location);
+                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), location);
         else
-                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (view), "about:blank");
+                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "about:blank");
 
         if (switch_focus)
                 gtk_notebook_set_current_page (priv->notebook, page_num);
@@ -1128,17 +1102,17 @@ window_tab_set_title (DhWindow      *window,
 
         num_pages = gtk_notebook_get_n_pages (priv->notebook);
         for (i = 0; i < num_pages; i++) {
-                GtkWidget *page;
-                GtkWidget *page_web_view;
+                DhTab *cur_tab;
+                DhWebView *cur_web_view;
 
-                page = gtk_notebook_get_nth_page (priv->notebook, i);
-                page_web_view = g_object_get_data (G_OBJECT (page), TAB_WEB_VIEW_KEY);
+                cur_tab = DH_TAB (gtk_notebook_get_nth_page (priv->notebook, i));
+                cur_web_view = dh_tab_get_web_view (cur_tab);
 
                 /* The web_view widget is inside a frame. */
-                if (page_web_view == GTK_WIDGET (web_view)) {
+                if (WEBKIT_WEB_VIEW (cur_web_view) == web_view) {
                         GtkWidget *hbox;
 
-                        hbox = gtk_notebook_get_tab_label (priv->notebook, page);
+                        hbox = gtk_notebook_get_tab_label (priv->notebook, GTK_WIDGET (cur_tab));
 
                         if (hbox != NULL) {
                                 GtkLabel *label = g_object_get_data (G_OBJECT (hbox), "label");


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