dh-tab.c 4.77 KB
Newer Older
Sébastien Wilmet's avatar
Sébastien Wilmet committed
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
3 4
 * SPDX-FileCopyrightText: 2018 Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: GPL-3.0-or-later
Sébastien Wilmet's avatar
Sébastien Wilmet committed
5 6 7
 */

#include "dh-tab.h"
8

9 10 11 12
/**
 * SECTION:dh-tab
 * @Title: DhTab
 * @Short_description: Subclass of #GtkGrid containing a #DhWebView
13
 *
14
 * #DhTab is a subclass of #GtkGrid, it is meant to be the content of one tab in
15
 * a #DhNotebook. It contains by default only one element: a #DhWebView. But
16 17
 * applications can add more widgets to the #GtkGrid.
 */
18

19
struct _DhTabPrivate {
Sébastien Wilmet's avatar
Sébastien Wilmet committed
20 21 22
        DhWebView *web_view;
};

23 24 25 26 27 28 29 30
enum {
        PROP_0,
        PROP_WEB_VIEW,
        N_PROPERTIES
};

static GParamSpec *properties[N_PROPERTIES];

31
G_DEFINE_TYPE_WITH_PRIVATE (DhTab, dh_tab, GTK_TYPE_GRID)
Sébastien Wilmet's avatar
Sébastien Wilmet committed
32

33 34 35 36 37 38 39 40 41
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));

42 43
        g_assert (tab->priv->web_view == NULL);
        tab->priv->web_view = g_object_ref_sink (web_view);
44

45
        gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (tab->priv->web_view));
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

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);

94
        if (tab->priv->web_view == NULL) {
95 96
                DhWebView *web_view;

97
                web_view = dh_web_view_new (NULL);
98 99 100 101 102
                gtk_widget_show (GTK_WIDGET (web_view));
                set_web_view (tab, web_view);
        }
}

Sébastien Wilmet's avatar
Sébastien Wilmet committed
103 104 105 106 107
static void
dh_tab_dispose (GObject *object)
{
        DhTab *tab = DH_TAB (object);

108
        g_clear_object (&tab->priv->web_view);
Sébastien Wilmet's avatar
Sébastien Wilmet committed
109 110 111 112 113 114 115 116 117

        G_OBJECT_CLASS (dh_tab_parent_class)->dispose (object);
}

static void
dh_tab_class_init (DhTabClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

118 119 120
        object_class->get_property = dh_tab_get_property;
        object_class->set_property = dh_tab_set_property;
        object_class->constructed = dh_tab_constructed;
Sébastien Wilmet's avatar
Sébastien Wilmet committed
121
        object_class->dispose = dh_tab_dispose;
122

123 124 125 126 127 128 129 130
        /**
         * DhTab:web-view:
         *
         * The #DhWebView of the tab. If set to %NULL a #DhWebView is created
         * with the default #DhProfile.
         *
         * Since: 3.30
         */
131 132 133 134 135 136 137 138 139 140
        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);
Sébastien Wilmet's avatar
Sébastien Wilmet committed
141 142 143 144 145
}

static void
dh_tab_init (DhTab *tab)
{
146 147
        tab->priv = dh_tab_get_instance_private (tab);

Sébastien Wilmet's avatar
Sébastien Wilmet committed
148 149 150
        gtk_orientable_set_orientation (GTK_ORIENTABLE (tab), GTK_ORIENTATION_VERTICAL);
}

151 152 153 154 155 156 157 158
/**
 * dh_tab_new:
 * @web_view: (nullable): a #DhWebView, or %NULL to create a #DhWebView with the
 *   default #DhProfile.
 *
 * Returns: (transfer floating): a new #DhTab.
 * Since: 3.30
 */
Sébastien Wilmet's avatar
Sébastien Wilmet committed
159
DhTab *
160
dh_tab_new (DhWebView *web_view)
Sébastien Wilmet's avatar
Sébastien Wilmet committed
161
{
162 163 164 165 166
        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);
Sébastien Wilmet's avatar
Sébastien Wilmet committed
167 168
}

169 170 171 172 173 174 175
/**
 * dh_tab_get_web_view:
 * @tab: a #DhTab.
 *
 * Returns: (transfer none): the #DhTab:web-view.
 * Since: 3.30
 */
Sébastien Wilmet's avatar
Sébastien Wilmet committed
176 177 178 179 180
DhWebView *
dh_tab_get_web_view (DhTab *tab)
{
        g_return_val_if_fail (DH_IS_TAB (tab), NULL);

181
        return tab->priv->web_view;
Sébastien Wilmet's avatar
Sébastien Wilmet committed
182
}