dh-notebook.c 9.75 KB
Newer Older
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
5 6 7
 */

#include "dh-notebook.h"
8
#include "dh-tab-label.h"
9

10 11 12 13 14 15
/**
 * SECTION:dh-notebook
 * @Title: DhNotebook
 * @Short_description: Subclass of #GtkNotebook
 *
 * #DhNotebook is a subclass of #GtkNotebook. The content of the tabs are
16
 * #DhTab's, and the tab labels are #DhTabLabel's.
17 18 19
 *
 * #DhNotebook handles the #DhWebView::open-new-tab signal by appending a new
 * #DhTab.
20 21
 */

22
struct _DhNotebookPrivate {
23
        DhProfile *profile;
24
};
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

enum {
        PROP_0,
        PROP_PROFILE,
        N_PROPERTIES
};

static GParamSpec *properties[N_PROPERTIES];

G_DEFINE_TYPE_WITH_PRIVATE (DhNotebook, dh_notebook, GTK_TYPE_NOTEBOOK)

static void
set_profile (DhNotebook *notebook,
             DhProfile  *profile)
{
        if (profile == NULL)
                return;

        g_return_if_fail (DH_IS_PROFILE (profile));

45 46
        g_assert (notebook->priv->profile == NULL);
        notebook->priv->profile = g_object_ref (profile);
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 94
}

static void
dh_notebook_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
        DhNotebook *notebook = DH_NOTEBOOK (object);

        switch (prop_id) {
                case PROP_PROFILE:
                        g_value_set_object (value, dh_notebook_get_profile (notebook));
                        break;

                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
        }
}

static void
dh_notebook_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
        DhNotebook *notebook = DH_NOTEBOOK (object);

        switch (prop_id) {
                case PROP_PROFILE:
                        set_profile (notebook, g_value_get_object (value));
                        break;

                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
        }
}

static void
dh_notebook_constructed (GObject *object)
{
        DhNotebook *notebook = DH_NOTEBOOK (object);

        if (G_OBJECT_CLASS (dh_notebook_parent_class)->constructed != NULL)
                G_OBJECT_CLASS (dh_notebook_parent_class)->constructed (object);

95
        if (notebook->priv->profile == NULL)
96 97 98 99 100 101 102 103
                set_profile (notebook, dh_profile_get_default ());
}

static void
dh_notebook_dispose (GObject *object)
{
        DhNotebook *notebook = DH_NOTEBOOK (object);

104
        g_clear_object (&notebook->priv->profile);
105 106 107

        G_OBJECT_CLASS (dh_notebook_parent_class)->dispose (object);
}
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
static void
show_or_hide_tabs (GtkNotebook *notebook)
{
        gint n_pages;

        n_pages = gtk_notebook_get_n_pages (notebook);
        gtk_notebook_set_show_tabs (notebook, n_pages > 1);
}

static void
dh_notebook_page_added (GtkNotebook *notebook,
                        GtkWidget   *child,
                        guint        page_num)
{
        if (GTK_NOTEBOOK_CLASS (dh_notebook_parent_class)->page_added != NULL)
                GTK_NOTEBOOK_CLASS (dh_notebook_parent_class)->page_added (notebook, child, page_num);

        show_or_hide_tabs (notebook);
}

static void
dh_notebook_page_removed (GtkNotebook *notebook,
                          GtkWidget   *child,
                          guint        page_num)
{
        if (GTK_NOTEBOOK_CLASS (dh_notebook_parent_class)->page_removed != NULL)
                GTK_NOTEBOOK_CLASS (dh_notebook_parent_class)->page_removed (notebook, child, page_num);

        show_or_hide_tabs (notebook);
}

140 141 142
static void
dh_notebook_class_init (DhNotebookClass *klass)
{
143
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
144 145
        GtkNotebookClass *gtk_notebook_class = GTK_NOTEBOOK_CLASS (klass);

146 147 148 149 150
        object_class->get_property = dh_notebook_get_property;
        object_class->set_property = dh_notebook_set_property;
        object_class->constructed = dh_notebook_constructed;
        object_class->dispose = dh_notebook_dispose;

151 152
        gtk_notebook_class->page_added = dh_notebook_page_added;
        gtk_notebook_class->page_removed = dh_notebook_page_removed;
153

154 155 156 157 158 159 160 161
        /**
         * DhNotebook:profile:
         *
         * The #DhProfile. If set to %NULL, the default profile as returned by
         * dh_profile_get_default() is used.
         *
         * Since: 3.30
         */
162 163 164 165 166 167 168 169 170 171
        properties[PROP_PROFILE] =
                g_param_spec_object ("profile",
                                     "profile",
                                     "",
                                     DH_TYPE_PROFILE,
                                     G_PARAM_READWRITE |
                                     G_PARAM_CONSTRUCT_ONLY |
                                     G_PARAM_STATIC_STRINGS);

        g_object_class_install_properties (object_class, N_PROPERTIES, properties);
172 173 174 175 176
}

static void
dh_notebook_init (DhNotebook *notebook)
{
177 178
        notebook->priv = dh_notebook_get_instance_private (notebook);

179 180 181
        gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
}

182 183 184 185 186 187 188
/**
 * dh_notebook_new:
 * @profile: (nullable): a #DhProfile, or %NULL for the default profile.
 *
 * Returns: (transfer floating): a new #DhNotebook.
 * Since: 3.30
 */
189
DhNotebook *
190
dh_notebook_new (DhProfile *profile)
191
{
192 193 194 195 196 197 198
        g_return_val_if_fail (profile == NULL || DH_IS_PROFILE (profile), NULL);

        return g_object_new (DH_TYPE_NOTEBOOK,
                             "profile", profile,
                             NULL);
}

199 200 201 202 203 204 205
/**
 * dh_notebook_get_profile:
 * @notebook: a #DhNotebook.
 *
 * Returns: (transfer none): the #DhNotebook:profile.
 * Since: 3.30
 */
206 207 208 209 210
DhProfile *
dh_notebook_get_profile (DhNotebook *notebook)
{
        g_return_val_if_fail (DH_IS_NOTEBOOK (notebook), NULL);

211
        return notebook->priv->profile;
212 213
}

214 215 216 217 218 219 220 221
static void
web_view_open_new_tab_cb (DhWebView   *web_view,
                          const gchar *uri,
                          DhNotebook  *notebook)
{
        dh_notebook_open_new_tab (notebook, uri, FALSE);
}

222 223 224 225 226 227 228 229 230 231 232 233 234
/**
 * dh_notebook_open_new_tab:
 * @notebook: a #DhNotebook.
 * @uri: (nullable): the URI to open, or %NULL for a blank page.
 * @switch_focus: whether to call gtk_notebook_set_current_page() on the new
 *   tab.
 *
 * Creates a new #DhTab and #DhTabLabel and appends them to the #GtkNotebook.
 *
 * The #DhWebView will have the same #DhProfile as @notebook.
 *
 * Since: 3.30
 */
235
void
236 237 238
dh_notebook_open_new_tab (DhNotebook  *notebook,
                          const gchar *uri,
                          gboolean     switch_focus)
239
{
240
        DhWebView *web_view;
241
        DhTab *tab;
242 243 244 245
        GtkWidget *label;
        gint page_num;

        g_return_if_fail (DH_IS_NOTEBOOK (notebook));
246

247
        web_view = dh_web_view_new (notebook->priv->profile);
248 249 250
        gtk_widget_show (GTK_WIDGET (web_view));

        tab = dh_tab_new (web_view);
251
        gtk_widget_show (GTK_WIDGET (tab));
252

253 254 255 256 257
        g_signal_connect (web_view,
                          "open-new-tab",
                          G_CALLBACK (web_view_open_new_tab_cb),
                          notebook);

258 259 260 261 262 263 264 265 266 267 268 269 270
        label = dh_tab_label_new (tab);
        gtk_widget_show (label);

        page_num = gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
                                             GTK_WIDGET (tab),
                                             label);

        gtk_container_child_set (GTK_CONTAINER (notebook),
                                 GTK_WIDGET (tab),
                                 "tab-expand", TRUE,
                                 "reorderable", TRUE,
                                 NULL);

271
        if (switch_focus)
272
                gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page_num);
273 274 275 276 277

        if (uri != NULL)
                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), uri);
        else
                webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "about:blank");
278 279
}

280 281 282 283 284 285 286
/**
 * dh_notebook_get_active_tab:
 * @notebook: a #DhNotebook.
 *
 * Returns: (transfer none) (nullable): the #DhTab currently shown in @notebook.
 * Since: 3.30
 */
287 288 289 290 291 292 293 294 295 296 297 298 299 300
DhTab *
dh_notebook_get_active_tab (DhNotebook *notebook)
{
        gint page_num;

        g_return_val_if_fail (DH_IS_NOTEBOOK (notebook), NULL);

        page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook));
        if (page_num == -1)
                return NULL;

        return DH_TAB (gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), page_num));
}

301 302 303 304 305 306 307
/**
 * dh_notebook_get_active_web_view:
 * @notebook: a #DhNotebook.
 *
 * Returns: (transfer none) (nullable): the #DhWebView of the active tab.
 * Since: 3.30
 */
308 309 310 311 312 313 314 315 316 317
DhWebView *
dh_notebook_get_active_web_view (DhNotebook *notebook)
{
        DhTab *tab;

        g_return_val_if_fail (DH_IS_NOTEBOOK (notebook), NULL);

        tab = dh_notebook_get_active_tab (notebook);
        return tab != NULL ? dh_tab_get_web_view (tab) : NULL;
}
318

319 320 321 322 323 324 325 326
/**
 * dh_notebook_get_all_web_views:
 * @notebook: a #DhNotebook.
 *
 * Returns: (transfer container) (element-type DhWebView): a #GList of all
 *   #DhWebView's contained in @notebook.
 * Since: 3.30
 */
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
GList *
dh_notebook_get_all_web_views (DhNotebook *notebook)
{
        gint n_pages;
        gint page_num;
        GList *list = NULL;

        g_return_val_if_fail (DH_IS_NOTEBOOK (notebook), NULL);

        n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));

        for (page_num = 0; page_num < n_pages; page_num++) {
                DhTab *tab;

                tab = DH_TAB (gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), page_num));
                list = g_list_prepend (list, dh_tab_get_web_view (tab));
        }

        return g_list_reverse (list);
}