1 | /* |
2 | * Copyright (C) 2017 Red Hat Inc. |
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 "WebKitPrintCustomWidget.h" |
22 | |
23 | #include "WebKitPrintCustomWidgetPrivate.h" |
24 | #include <glib/gi18n-lib.h> |
25 | #include <gtk/gtk.h> |
26 | #include <wtf/glib/GRefPtr.h> |
27 | #include <wtf/glib/WTFGType.h> |
28 | #include <wtf/text/CString.h> |
29 | |
30 | /** |
31 | * SECTION: WebKitPrintCustomWidget |
32 | * @Short_description: Allows to embed a custom widget in print dialog |
33 | * @Title: WebKitPrintCustomWidget |
34 | * @See_also: #WebKitPrintOperation |
35 | * |
36 | * A WebKitPrintCustomWidget allows to embed a custom widget in the print |
37 | * dialog by connecting to the #WebKitPrintOperation::create-custom-widget |
38 | * signal, creating a new WebKitPrintCustomWidget with |
39 | * webkit_print_custom_widget_new() and returning it from there. You can later |
40 | * use webkit_print_operation_run_dialog() to display the dialog. |
41 | * |
42 | * Since: 2.16 |
43 | */ |
44 | |
45 | enum { |
46 | APPLY, |
47 | UPDATE, |
48 | |
49 | LAST_SIGNAL |
50 | }; |
51 | |
52 | enum { |
53 | PROP_0, |
54 | |
55 | PROP_WIDGET, |
56 | PROP_TITLE |
57 | }; |
58 | |
59 | struct _WebKitPrintCustomWidgetPrivate { |
60 | CString title; |
61 | GRefPtr<GtkWidget> widget; |
62 | }; |
63 | |
64 | static guint signals[LAST_SIGNAL] = { 0, }; |
65 | |
66 | WEBKIT_DEFINE_TYPE(WebKitPrintCustomWidget, webkit_print_custom_widget, G_TYPE_OBJECT) |
67 | |
68 | static void webkitPrintCustomWidgetGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec) |
69 | { |
70 | WebKitPrintCustomWidget* printCustomWidget = WEBKIT_PRINT_CUSTOM_WIDGET(object); |
71 | |
72 | switch (propId) { |
73 | case PROP_WIDGET: |
74 | g_value_set_object(value, webkit_print_custom_widget_get_widget(printCustomWidget)); |
75 | break; |
76 | case PROP_TITLE: |
77 | g_value_set_string(value, webkit_print_custom_widget_get_title(printCustomWidget)); |
78 | break; |
79 | default: |
80 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); |
81 | } |
82 | } |
83 | |
84 | static void webkitPrintCustomWidgetSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec) |
85 | { |
86 | WebKitPrintCustomWidget* printCustomWidget = WEBKIT_PRINT_CUSTOM_WIDGET(object); |
87 | |
88 | switch (propId) { |
89 | case PROP_WIDGET: |
90 | printCustomWidget->priv->widget = GTK_WIDGET(g_value_get_object(value)); |
91 | break; |
92 | case PROP_TITLE: |
93 | printCustomWidget->priv->title = g_value_get_string(value); |
94 | break; |
95 | default: |
96 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); |
97 | } |
98 | } |
99 | |
100 | static void webkit_print_custom_widget_class_init(WebKitPrintCustomWidgetClass* printCustomWidgetClass) |
101 | { |
102 | GObjectClass* objectClass = G_OBJECT_CLASS(printCustomWidgetClass); |
103 | objectClass->get_property = webkitPrintCustomWidgetGetProperty; |
104 | objectClass->set_property = webkitPrintCustomWidgetSetProperty; |
105 | |
106 | /** |
107 | * WebKitPrintCustomWidget:widget: |
108 | * |
109 | * The custom #GtkWidget that will be embedded in the dialog. |
110 | * |
111 | * Since: 2.16 |
112 | */ |
113 | g_object_class_install_property( |
114 | objectClass, |
115 | PROP_WIDGET, |
116 | g_param_spec_object( |
117 | "widget" , |
118 | _("Widget" ), |
119 | _("Widget that will be added to the print dialog." ), |
120 | GTK_TYPE_WIDGET, |
121 | static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY))); |
122 | |
123 | /** |
124 | * WebKitPrintCustomWidget:title: |
125 | * |
126 | * The title of the custom widget. |
127 | * |
128 | * Since: 2.16 |
129 | */ |
130 | g_object_class_install_property( |
131 | objectClass, |
132 | PROP_TITLE, |
133 | g_param_spec_string( |
134 | "title" , |
135 | _("Title" ), |
136 | _("Title of the widget that will be added to the print dialog." ), |
137 | nullptr, |
138 | static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY))); |
139 | |
140 | /** |
141 | * WebKitPrintCustomWidget::update: |
142 | * @print_custom_widget: the #WebKitPrintCustomWidget on which the signal was emitted |
143 | * @page_setup: actual page setup |
144 | * @print_settings: actual print settings |
145 | * |
146 | * Emitted after change of selected printer in the dialog. The actual page setup |
147 | * and print settings are available and the custom widget can actualize itself |
148 | * according to their values. |
149 | * |
150 | * Since: 2.16 |
151 | */ |
152 | signals[UPDATE] = |
153 | g_signal_new( |
154 | "update" , |
155 | G_TYPE_FROM_CLASS(printCustomWidgetClass), |
156 | G_SIGNAL_RUN_LAST, |
157 | G_STRUCT_OFFSET(WebKitPrintCustomWidgetClass, update), |
158 | 0, 0, |
159 | g_cclosure_marshal_generic, |
160 | G_TYPE_NONE, 2, |
161 | GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS); |
162 | |
163 | /** |
164 | * WebKitPrintCustomWidget::apply: |
165 | * @print_custom_widget: the #WebKitPrintCustomWidget on which the signal was emitted |
166 | * |
167 | * Emitted right before the printing will start. You should read the information |
168 | * from the widget and update the content based on it if necessary. The widget |
169 | * is not guaranteed to be valid at a later time. |
170 | * |
171 | * Since: 2.16 |
172 | */ |
173 | signals[APPLY] = |
174 | g_signal_new( |
175 | "apply" , |
176 | G_TYPE_FROM_CLASS(printCustomWidgetClass), |
177 | G_SIGNAL_RUN_LAST, |
178 | G_STRUCT_OFFSET(WebKitPrintCustomWidgetClass, apply), |
179 | 0, 0, |
180 | g_cclosure_marshal_VOID__VOID, |
181 | G_TYPE_NONE, 0); |
182 | } |
183 | |
184 | /** |
185 | * webkit_print_custom_widget_new: |
186 | * @widget: a #GtkWidget |
187 | * @title: a @widget's title |
188 | * |
189 | * Create a new #WebKitPrintCustomWidget with given @widget and @title. The @widget |
190 | * ownership is taken and it is destroyed together with the dialog even if this |
191 | * object could still be alive at that point. You typically want to pass a container |
192 | * widget with multiple widgets in it. |
193 | * |
194 | * Returns: (transfer full): a new #WebKitPrintOperation. |
195 | * |
196 | * Since: 2.16 |
197 | */ |
198 | WebKitPrintCustomWidget* webkit_print_custom_widget_new(GtkWidget* widget, const char* title) |
199 | { |
200 | g_return_val_if_fail(GTK_IS_WIDGET(widget), nullptr); |
201 | g_return_val_if_fail(title, nullptr); |
202 | |
203 | return WEBKIT_PRINT_CUSTOM_WIDGET(g_object_new(WEBKIT_TYPE_PRINT_CUSTOM_WIDGET, "widget" , widget, "title" , title, nullptr)); |
204 | } |
205 | |
206 | /** |
207 | * webkit_print_custom_widget_get_widget: |
208 | * @print_custom_widget: a #WebKitPrintCustomWidget |
209 | * |
210 | * Return the value of #WebKitPrintCustomWidget:widget property for the given |
211 | * @print_custom_widget object. The returned value will always be valid if called |
212 | * from #WebKitPrintCustomWidget::apply or #WebKitPrintCustomWidget::update |
213 | * callbacks, but it will be %NULL if called after the |
214 | * #WebKitPrintCustomWidget::apply signal is emitted. |
215 | * |
216 | * Returns: (transfer none): a #GtkWidget. |
217 | * |
218 | * Since: 2.16 |
219 | */ |
220 | GtkWidget* webkit_print_custom_widget_get_widget(WebKitPrintCustomWidget* printCustomWidget) |
221 | { |
222 | g_return_val_if_fail(WEBKIT_IS_PRINT_CUSTOM_WIDGET(printCustomWidget), nullptr); |
223 | |
224 | return printCustomWidget->priv->widget.get(); |
225 | } |
226 | |
227 | /** |
228 | * webkit_print_custom_widget_get_title: |
229 | * @print_custom_widget: a #WebKitPrintCustomWidget |
230 | * |
231 | * Return the value of #WebKitPrintCustomWidget:title property for the given |
232 | * @print_custom_widget object. |
233 | * |
234 | * Returns: Title of the @print_custom_widget. |
235 | * |
236 | * Since: 2.16 |
237 | */ |
238 | const gchar* webkit_print_custom_widget_get_title(WebKitPrintCustomWidget* printCustomWidget) |
239 | { |
240 | g_return_val_if_fail(WEBKIT_IS_PRINT_CUSTOM_WIDGET(printCustomWidget), nullptr); |
241 | |
242 | return printCustomWidget->priv->title.data(); |
243 | } |
244 | |
245 | void webkitPrintCustomWidgetEmitCustomWidgetApplySignal(WebKitPrintCustomWidget* printCustomWidget) |
246 | { |
247 | g_signal_emit(printCustomWidget, signals[APPLY], 0); |
248 | printCustomWidget->priv->widget = nullptr; |
249 | } |
250 | |
251 | void webkitPrintCustomWidgetEmitUpdateCustomWidgetSignal(WebKitPrintCustomWidget *printCustomWidget, GtkPageSetup *pageSetup, GtkPrintSettings *printSettings) |
252 | { |
253 | g_signal_emit(printCustomWidget, signals[UPDATE], 0, pageSetup, printSettings); |
254 | } |
255 | |