[devhelp] Tab: add :web-view construct-only property



commit eec8e231d5f193c8d84f1ad6799fa632909f750e
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jun 5 21:14:01 2018 +0200

    Tab: add :web-view construct-only property
    
    That way DhTab won't require a DhProfile property.

 src/dh-notebook.c |   8 +++--
 src/dh-tab.c      | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 src/dh-tab.h      |   2 +-
 3 files changed, 105 insertions(+), 11 deletions(-)
---
diff --git a/src/dh-notebook.c b/src/dh-notebook.c
index c5853009..a536bbb2 100644
--- a/src/dh-notebook.c
+++ b/src/dh-notebook.c
@@ -92,17 +92,19 @@ dh_notebook_open_new_tab (DhNotebook  *notebook,
                           const gchar *uri,
                           gboolean     switch_focus)
 {
-        DhTab *tab;
         DhWebView *web_view;
+        DhTab *tab;
         GtkWidget *label;
         gint page_num;
 
         g_return_if_fail (DH_IS_NOTEBOOK (notebook));
 
-        tab = dh_tab_new ();
+        web_view = dh_web_view_new ();
+        gtk_widget_show (GTK_WIDGET (web_view));
+
+        tab = dh_tab_new (web_view);
         gtk_widget_show (GTK_WIDGET (tab));
 
-        web_view = dh_tab_get_web_view (tab);
         g_signal_connect (web_view,
                           "open-new-tab",
                           G_CALLBACK (web_view_open_new_tab_cb),
diff --git a/src/dh-tab.c b/src/dh-tab.c
index 4516b961..63b88775 100644
--- a/src/dh-tab.c
+++ b/src/dh-tab.c
@@ -31,14 +31,92 @@ struct _DhTabPrivate {
         DhWebView *web_view;
 };
 
+enum {
+        PROP_0,
+        PROP_WEB_VIEW,
+        N_PROPERTIES
+};
+
+static GParamSpec *properties[N_PROPERTIES];
+
 G_DEFINE_TYPE_WITH_PRIVATE (DhTab, dh_tab, GTK_TYPE_GRID)
 
+static void
+set_web_view (DhTab     *tab,
+              DhWebView *web_view)
+{
+        if (web_view == NULL)
+                return;
+
+        g_return_if_fail (DH_IS_WEB_VIEW (web_view));
+
+        g_assert (tab->priv->web_view == NULL);
+        tab->priv->web_view = g_object_ref_sink (web_view);
+
+        gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (tab->priv->web_view));
+}
+
+static void
+dh_tab_get_property (GObject    *object,
+                     guint       prop_id,
+                     GValue     *value,
+                     GParamSpec *pspec)
+{
+        DhTab *tab = DH_TAB (object);
+
+        switch (prop_id) {
+                case PROP_WEB_VIEW:
+                        g_value_set_object (value, dh_tab_get_web_view (tab));
+                        break;
+
+                default:
+                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                        break;
+        }
+}
+
+static void
+dh_tab_set_property (GObject      *object,
+                     guint         prop_id,
+                     const GValue *value,
+                     GParamSpec   *pspec)
+{
+        DhTab *tab = DH_TAB (object);
+
+        switch (prop_id) {
+                case PROP_WEB_VIEW:
+                        set_web_view (tab, g_value_get_object (value));
+                        break;
+
+                default:
+                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                        break;
+        }
+}
+
+static void
+dh_tab_constructed (GObject *object)
+{
+        DhTab *tab = DH_TAB (object);
+
+        if (G_OBJECT_CLASS (dh_tab_parent_class)->constructed != NULL)
+                G_OBJECT_CLASS (dh_tab_parent_class)->constructed (object);
+
+        if (tab->priv->web_view == NULL) {
+                DhWebView *web_view;
+
+                web_view = dh_web_view_new ();
+                gtk_widget_show (GTK_WIDGET (web_view));
+                set_web_view (tab, web_view);
+        }
+}
+
 static void
 dh_tab_dispose (GObject *object)
 {
         DhTab *tab = DH_TAB (object);
 
-        tab->priv->web_view = NULL;
+        g_clear_object (&tab->priv->web_view);
 
         G_OBJECT_CLASS (dh_tab_parent_class)->dispose (object);
 }
@@ -48,7 +126,21 @@ dh_tab_class_init (DhTabClass *klass)
 {
         GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+        object_class->get_property = dh_tab_get_property;
+        object_class->set_property = dh_tab_set_property;
+        object_class->constructed = dh_tab_constructed;
         object_class->dispose = dh_tab_dispose;
+
+        properties[PROP_WEB_VIEW] =
+                g_param_spec_object ("web-view",
+                                     "web-view",
+                                     "",
+                                     DH_TYPE_WEB_VIEW,
+                                     G_PARAM_READWRITE |
+                                     G_PARAM_CONSTRUCT_ONLY |
+                                     G_PARAM_STATIC_STRINGS);
+
+        g_object_class_install_properties (object_class, N_PROPERTIES, properties);
 }
 
 static void
@@ -57,16 +149,16 @@ dh_tab_init (DhTab *tab)
         tab->priv = dh_tab_get_instance_private (tab);
 
         gtk_orientable_set_orientation (GTK_ORIENTABLE (tab), GTK_ORIENTATION_VERTICAL);
-
-        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->web_view));
 }
 
 DhTab *
-dh_tab_new (void)
+dh_tab_new (DhWebView *web_view)
 {
-        return g_object_new (DH_TYPE_TAB, NULL);
+        g_return_val_if_fail (web_view == NULL || DH_IS_WEB_VIEW (web_view), NULL);
+
+        return g_object_new (DH_TYPE_TAB,
+                             "web-view", web_view,
+                             NULL);
 }
 
 DhWebView *
diff --git a/src/dh-tab.h b/src/dh-tab.h
index c2729f2e..5b9ba0ac 100644
--- a/src/dh-tab.h
+++ b/src/dh-tab.h
@@ -52,7 +52,7 @@ struct _DhTabClass {
 
 GType           dh_tab_get_type         (void);
 
-DhTab *         dh_tab_new              (void);
+DhTab *         dh_tab_new              (DhWebView *web_view);
 
 DhWebView *     dh_tab_get_web_view     (DhTab *tab);
 


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