File: WebKitPrintCustomWidget.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (285 lines) | stat: -rw-r--r-- 9,062 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2017 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitPrintCustomWidget.h"

#include "WebKitPrintCustomWidgetPrivate.h"
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>

/**
 * WebKitPrintCustomWidget:
 * @See_also: #WebKitPrintOperation
 *
 * Allows to embed a custom widget in print dialog.
 *
 * A WebKitPrintCustomWidget allows to embed a custom widget in the print
 * dialog by connecting to the #WebKitPrintOperation::create-custom-widget
 * signal, creating a new WebKitPrintCustomWidget with
 * webkit_print_custom_widget_new() and returning it from there. You can later
 * use webkit_print_operation_run_dialog() to display the dialog.
 *
 * Unfortunately, use of custom widgets is incompatible with modern
 * containerized application frameworks like Flatpak. A print dialog
 * constructed in the application process will not have access to host
 * printers, so instead it must be constructed by a desktop portal service
 * running on the host system. Because this print dialog runs in a separate
 * process, it's not possible to attach a custom widget.
 *
 * Since: 2.16
 *
 * Deprecated: 2.40
 */

ALLOW_DEPRECATED_DECLARATIONS_BEGIN

enum {
    APPLY,
    UPDATE,

    LAST_SIGNAL
};

enum {
    PROP_0,

    PROP_WIDGET,
    PROP_TITLE
};

struct _WebKitPrintCustomWidgetPrivate {
    CString title;
    GRefPtr<GtkWidget> widget;
};

static guint signals[LAST_SIGNAL] = { 0, };

WEBKIT_DEFINE_TYPE(WebKitPrintCustomWidget, webkit_print_custom_widget, G_TYPE_OBJECT)

static void webkitPrintCustomWidgetGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
    WebKitPrintCustomWidget* printCustomWidget = WEBKIT_PRINT_CUSTOM_WIDGET(object);

    switch (propId) {
    case PROP_WIDGET:
        g_value_set_object(value, webkit_print_custom_widget_get_widget(printCustomWidget));
        break;
    case PROP_TITLE:
        g_value_set_string(value, webkit_print_custom_widget_get_title(printCustomWidget));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static void webkitPrintCustomWidgetSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
{
    WebKitPrintCustomWidget* printCustomWidget = WEBKIT_PRINT_CUSTOM_WIDGET(object);

    switch (propId) {
    case PROP_WIDGET:
        printCustomWidget->priv->widget = GTK_WIDGET(g_value_get_object(value));
        break;
    case PROP_TITLE:
        printCustomWidget->priv->title = g_value_get_string(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static void webkit_print_custom_widget_class_init(WebKitPrintCustomWidgetClass* printCustomWidgetClass)
{
    GObjectClass* objectClass = G_OBJECT_CLASS(printCustomWidgetClass);
    objectClass->get_property = webkitPrintCustomWidgetGetProperty;
    objectClass->set_property = webkitPrintCustomWidgetSetProperty;

    /**
     * WebKitPrintCustomWidget:widget:
     *
     * The custom #GtkWidget that will be embedded in the dialog.
     *
     * Since: 2.16
     *
     * Deprecated: 2.40
     */
    g_object_class_install_property(
        objectClass,
        PROP_WIDGET,
        g_param_spec_object(
            "widget",
            nullptr, nullptr,
            GTK_TYPE_WIDGET,
            static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));

    /**
     * WebKitPrintCustomWidget:title:
     *
     * The title of the custom widget.
     *
     * Since: 2.16
     *
     * Deprecated: 2.40
     */
    g_object_class_install_property(
        objectClass,
        PROP_TITLE,
        g_param_spec_string(
            "title",
            nullptr, nullptr,
            nullptr,
            static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));

    /**
     * WebKitPrintCustomWidget::update:
     * @print_custom_widget: the #WebKitPrintCustomWidget on which the signal was emitted
     * @page_setup: actual page setup
     * @print_settings: actual print settings
     *
     * Emitted after change of selected printer in the dialog. The actual page setup
     * and print settings are available and the custom widget can actualize itself
     * according to their values.
     *
     * Since: 2.16
     *
     * Deprecated: 2.40
     */
    signals[UPDATE] =
        g_signal_new(
            "update",
            G_TYPE_FROM_CLASS(printCustomWidgetClass),
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET(WebKitPrintCustomWidgetClass, update),
            0, 0,
            g_cclosure_marshal_generic,
            G_TYPE_NONE, 2,
            GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS);

    /**
     * WebKitPrintCustomWidget::apply:
     * @print_custom_widget: the #WebKitPrintCustomWidget on which the signal was emitted
     *
     * Emitted right before the printing will start. You should read the information
     * from the widget and update the content based on it if necessary. The widget
     * is not guaranteed to be valid at a later time.
     *
     * Since: 2.16
     *
     * Deprecated: 2.40
     */
    signals[APPLY] =
        g_signal_new(
            "apply",
            G_TYPE_FROM_CLASS(printCustomWidgetClass),
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET(WebKitPrintCustomWidgetClass, apply),
            0, 0,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE, 0);
}

/**
 * webkit_print_custom_widget_new:
 * @widget: a #GtkWidget
 * @title: a @widget's title
 *
 * Create a new #WebKitPrintCustomWidget with given @widget and @title.
 *
 * The @widget
 * ownership is taken and it is destroyed together with the dialog even if this
 * object could still be alive at that point. You typically want to pass a container
 * widget with multiple widgets in it.
 *
 * Returns: (transfer full): a new #WebKitPrintOperation.
 *
 * Since: 2.16
 *
 * Deprecated: 2.40
 */
WebKitPrintCustomWidget* webkit_print_custom_widget_new(GtkWidget* widget, const char* title)
{
    g_return_val_if_fail(GTK_IS_WIDGET(widget), nullptr);
    g_return_val_if_fail(title, nullptr);

    return WEBKIT_PRINT_CUSTOM_WIDGET(g_object_new(WEBKIT_TYPE_PRINT_CUSTOM_WIDGET, "widget", widget, "title", title, nullptr));
}

/**
 * webkit_print_custom_widget_get_widget:
 * @print_custom_widget: a #WebKitPrintCustomWidget
 *
 * Return the value of #WebKitPrintCustomWidget:widget property.
 *
 * Return the value of #WebKitPrintCustomWidget:widget property for the given
 * @print_custom_widget object. The returned value will always be valid if called
 * from #WebKitPrintCustomWidget::apply or #WebKitPrintCustomWidget::update
 * callbacks, but it will be %NULL if called after the
 * #WebKitPrintCustomWidget::apply signal is emitted.
 *
 * Returns: (transfer none): a #GtkWidget.
 *
 * Since: 2.16
 *
 * Deprecated: 2.40
 */
GtkWidget* webkit_print_custom_widget_get_widget(WebKitPrintCustomWidget* printCustomWidget)
{
    g_return_val_if_fail(WEBKIT_IS_PRINT_CUSTOM_WIDGET(printCustomWidget), nullptr);

    return printCustomWidget->priv->widget.get();
}

/**
 * webkit_print_custom_widget_get_title:
 * @print_custom_widget: a #WebKitPrintCustomWidget
 *
 * Return the value of #WebKitPrintCustomWidget:title property.
 *
 * Return the value of #WebKitPrintCustomWidget:title property for the given
 * @print_custom_widget object.
 *
 * Returns: Title of the @print_custom_widget.
 *
 * Since: 2.16
 *
 * Deprecated: 2.40
 */
const gchar* webkit_print_custom_widget_get_title(WebKitPrintCustomWidget* printCustomWidget)
{
    g_return_val_if_fail(WEBKIT_IS_PRINT_CUSTOM_WIDGET(printCustomWidget), nullptr);

    return printCustomWidget->priv->title.data();
}

void webkitPrintCustomWidgetEmitCustomWidgetApplySignal(WebKitPrintCustomWidget* printCustomWidget)
{
    g_signal_emit(printCustomWidget, signals[APPLY], 0);
    printCustomWidget->priv->widget = nullptr;
}

void webkitPrintCustomWidgetEmitUpdateCustomWidgetSignal(WebKitPrintCustomWidget *printCustomWidget, GtkPageSetup *pageSetup, GtkPrintSettings *printSettings)
{
    g_signal_emit(printCustomWidget, signals[UPDATE], 0, pageSetup, printSettings);
}

ALLOW_DEPRECATED_DECLARATIONS_END