1/*
2 * Copyright (C) 2018 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitWebViewDialog.h"
22
23#include <wtf/glib/GRefPtr.h>
24#include <wtf/glib/WTFGType.h>
25
26struct _WebKitWebViewDialogPrivate {
27#if !GTK_CHECK_VERSION(3, 20, 0)
28 GRefPtr<GtkStyleContext> styleContext;
29#endif
30};
31
32WEBKIT_DEFINE_ABSTRACT_TYPE(WebKitWebViewDialog, webkit_web_view_dialog, GTK_TYPE_EVENT_BOX)
33
34static gboolean webkitWebViewDialogDraw(GtkWidget* widget, cairo_t* cr)
35{
36 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
37 cairo_set_source_rgba(cr, 0, 0, 0, 0.5);
38 cairo_paint(cr);
39
40 if (GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget))) {
41 GtkAllocation allocation;
42 gtk_widget_get_allocation(child, &allocation);
43
44#if GTK_CHECK_VERSION(3, 20, 0)
45 GtkStyleContext* styleContext = gtk_widget_get_style_context(widget);
46#else
47 GtkStyleContext* styleContext = WEBKIT_WEB_VIEW_DIALOG(widget)->priv->styleContext.get();
48#endif
49 gtk_render_background(styleContext, cr, allocation.x, allocation.y, allocation.width, allocation.height);
50 }
51
52 GTK_WIDGET_CLASS(webkit_web_view_dialog_parent_class)->draw(widget, cr);
53
54 return FALSE;
55}
56
57static void webkitWebViewDialogSizeAllocate(GtkWidget* widget, GtkAllocation* allocation)
58{
59 GTK_WIDGET_CLASS(webkit_web_view_dialog_parent_class)->size_allocate(widget, allocation);
60
61 GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget));
62 if (!child)
63 return;
64
65 GtkRequisition naturalSize;
66 gtk_widget_get_preferred_size(child, 0, &naturalSize);
67
68 GtkAllocation childAllocation;
69 gtk_widget_get_allocation(child, &childAllocation);
70
71 childAllocation.x += (allocation->width - naturalSize.width) / 2;
72 childAllocation.y += (allocation->height - naturalSize.height) / 2;
73 childAllocation.width = naturalSize.width;
74 childAllocation.height = naturalSize.height;
75 gtk_widget_size_allocate(child, &childAllocation);
76}
77
78static void webkitWebViewDialogConstructed(GObject* object)
79{
80 G_OBJECT_CLASS(webkit_web_view_dialog_parent_class)->constructed(object);
81
82 gtk_widget_set_app_paintable(GTK_WIDGET(object), TRUE);
83
84#if GTK_CHECK_VERSION(3, 20, 0)
85 gtk_style_context_add_class(gtk_widget_get_style_context(GTK_WIDGET(object)), GTK_STYLE_CLASS_CSD);
86 gtk_style_context_add_class(gtk_widget_get_style_context(GTK_WIDGET(object)), GTK_STYLE_CLASS_BACKGROUND);
87#else
88 WebKitWebViewDialogPrivate* priv = WEBKIT_WEB_VIEW_DIALOG(object)->priv;
89 priv->styleContext = adoptGRef(gtk_style_context_new());
90 GtkWidgetPath* path = gtk_widget_path_new();
91 gtk_widget_path_append_type(path, GTK_TYPE_WINDOW);
92 gtk_style_context_add_class(priv->styleContext.get(), GTK_STYLE_CLASS_BACKGROUND);
93 gtk_style_context_add_class(priv->styleContext.get(), GTK_STYLE_CLASS_TITLEBAR);
94 gtk_style_context_add_class(priv->styleContext.get(), GTK_STYLE_CLASS_CSD);
95 gtk_style_context_set_path(priv->styleContext.get(), path);
96 gtk_widget_path_free(path);
97#endif
98}
99
100static void webkit_web_view_dialog_class_init(WebKitWebViewDialogClass* klass)
101{
102 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
103 objectClass->constructed = webkitWebViewDialogConstructed;
104
105 GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(klass);
106 widgetClass->draw = webkitWebViewDialogDraw;
107 widgetClass->size_allocate = webkitWebViewDialogSizeAllocate;
108
109#if GTK_CHECK_VERSION(3, 20, 0)
110 gtk_widget_class_set_css_name(widgetClass, "messagedialog");
111#endif
112}
113