1/*
2 * Copyright (C) 2015 Igalia S.L.
3 * Copyright (c) 2012, Samsung Electronics
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "WebKitColorChooserRequest.h"
29
30#include "WebKitColorChooserRequestPrivate.h"
31#include <glib/gi18n-lib.h>
32#include <wtf/glib/WTFGType.h>
33
34using namespace WebKit;
35using namespace WebCore;
36
37/**
38 * SECTION: WebKitColorChooserRequest
39 * @Short_description: A request to open a color chooser
40 * @Title: WebKitColorChooserRequest
41 * @See_also: #WebKitWebView
42 *
43 * Whenever the user interacts with an &lt;input type='color' /&gt;
44 * HTML element, WebKit will need to show a dialog to choose a color. For that
45 * to happen in a general way, instead of just opening a #GtkColorChooser
46 * (which might be not desirable in some cases, which could prefer to use their
47 * own color chooser dialog), WebKit will fire the
48 * #WebKitWebView::run-color-chooser signal with a #WebKitColorChooserRequest
49 * object, which will allow the client application to specify the color to be
50 * selected, to inspect the details of the request (e.g. to get initial color)
51 * and to cancel the request, in case nothing was selected.
52 *
53 * In case the client application does not wish to handle this signal,
54 * WebKit will provide a default handler which will asynchronously run
55 * a regular #GtkColorChooserDialog for the user to interact with.
56 */
57
58enum {
59 PROP_0,
60
61 PROP_RGBA
62};
63
64enum {
65 FINISHED,
66
67 LAST_SIGNAL
68};
69
70struct _WebKitColorChooserRequestPrivate {
71 WebKitColorChooser* colorChooser;
72 GdkRGBA rgba;
73 bool handled;
74};
75
76static guint signals[LAST_SIGNAL] = { 0, };
77
78WEBKIT_DEFINE_TYPE(WebKitColorChooserRequest, webkit_color_chooser_request, G_TYPE_OBJECT)
79
80static void webkitColorChooserRequestDispose(GObject* object)
81{
82 WebKitColorChooserRequest* request = WEBKIT_COLOR_CHOOSER_REQUEST(object);
83 if (!request->priv->handled)
84 webkit_color_chooser_request_finish(request);
85
86 G_OBJECT_CLASS(webkit_color_chooser_request_parent_class)->dispose(object);
87}
88
89static void webkitColorChooserRequestGetProperty(GObject* object, guint propertyID, GValue* value, GParamSpec* paramSpec)
90{
91 WebKitColorChooserRequest* request = WEBKIT_COLOR_CHOOSER_REQUEST(object);
92
93 switch (propertyID) {
94 case PROP_RGBA:
95 g_value_set_boxed(value, &request->priv->rgba);
96 break;
97 default:
98 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, paramSpec);
99 }
100}
101
102static void webkitColorChooserRequestSetProperty(GObject* object, guint propertyID, const GValue* value, GParamSpec* paramSpec)
103{
104 WebKitColorChooserRequest* request = WEBKIT_COLOR_CHOOSER_REQUEST(object);
105
106 switch (propertyID) {
107 case PROP_RGBA:
108 webkit_color_chooser_request_set_rgba(request, static_cast<GdkRGBA*>(g_value_get_boxed(value)));
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, paramSpec);
112 }
113}
114
115static void webkit_color_chooser_request_class_init(WebKitColorChooserRequestClass* requestClass)
116{
117 GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
118 objectClass->dispose = webkitColorChooserRequestDispose;
119 objectClass->get_property = webkitColorChooserRequestGetProperty;
120 objectClass->set_property = webkitColorChooserRequestSetProperty;
121
122 /**
123 * WebKitWebView:rgba:
124 *
125 * The #GdkRGBA color of the request
126 *
127 * Since: 2.8
128 */
129 g_object_class_install_property(objectClass,
130 PROP_RGBA,
131 g_param_spec_boxed("rgba",
132 _("Current RGBA color"),
133 _("The current RGBA color for the request"),
134 GDK_TYPE_RGBA,
135 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT)));
136
137 /**
138 * WebKitColorChooserRequest::finished:
139 * @request: the #WebKitColorChooserRequest on which the signal is emitted
140 *
141 * Emitted when the @request finishes. This signal can be emitted because the
142 * user completed the @request calling webkit_color_chooser_request_finish(),
143 * or cancelled it with webkit_color_chooser_request_cancel() or because the
144 * color input element is removed from the DOM.
145 *
146 * Since: 2.8
147 */
148 signals[FINISHED] =
149 g_signal_new(
150 "finished",
151 G_TYPE_FROM_CLASS(requestClass),
152 G_SIGNAL_RUN_LAST,
153 0, 0,
154 nullptr,
155 g_cclosure_marshal_VOID__VOID,
156 G_TYPE_NONE, 0);
157}
158
159/**
160 * webkit_color_chooser_request_set_rgba:
161 * @request: a #WebKitFileChooserRequest
162 * @rgba: a pointer #GdkRGBA
163 *
164 * Sets the current #GdkRGBA color of @request
165 *
166 * Since: 2.8
167 */
168void webkit_color_chooser_request_set_rgba(WebKitColorChooserRequest* request, const GdkRGBA* rgba)
169{
170 g_return_if_fail(WEBKIT_IS_COLOR_CHOOSER_REQUEST(request));
171 g_return_if_fail(rgba);
172
173 if (gdk_rgba_equal(&request->priv->rgba, rgba))
174 return;
175
176 request->priv->rgba = *rgba;
177 g_object_notify(G_OBJECT(request), "rgba");
178}
179
180/**
181 * webkit_color_chooser_request_get_rgba:
182 * @request: a #WebKitColorChooserRequest
183 * @rgba: (out): a #GdkRGBA to fill in with the current color.
184 *
185 * Gets the current #GdkRGBA color of @request
186 *
187 * Since: 2.8
188 */
189void webkit_color_chooser_request_get_rgba(WebKitColorChooserRequest* request, GdkRGBA* rgba)
190{
191 g_return_if_fail(WEBKIT_IS_COLOR_CHOOSER_REQUEST(request));
192 g_return_if_fail(rgba);
193
194 *rgba = request->priv->rgba;
195}
196
197/**
198 * webkit_color_chooser_request_get_element_rectangle:
199 * @request: a #WebKitColorChooserRequest
200 * @rect: (out): a #GdkRectangle to fill in with the element area
201 *
202 * Gets the bounding box of the color input element.
203 *
204 * Since: 2.8
205 */
206void webkit_color_chooser_request_get_element_rectangle(WebKitColorChooserRequest* request, GdkRectangle* rect)
207{
208 g_return_if_fail(WEBKIT_IS_COLOR_CHOOSER_REQUEST(request));
209 g_return_if_fail(rect);
210
211 *rect = request->priv->colorChooser->elementRect();
212}
213
214/**
215 * webkit_color_chooser_request_finish:
216 * @request: a #WebKitColorChooserRequest
217 *
218 * Finishes @request and the input element keeps the current value of
219 * #WebKitColorChooserRequest:rgba.
220 * The signal #WebKitColorChooserRequest::finished
221 * is emitted to notify that the request has finished.
222 *
223 * Since: 2.8
224 */
225void webkit_color_chooser_request_finish(WebKitColorChooserRequest* request)
226{
227 g_return_if_fail(WEBKIT_IS_COLOR_CHOOSER_REQUEST(request));
228
229 if (request->priv->handled)
230 return;
231
232 request->priv->handled = true;
233 g_signal_emit(request, signals[FINISHED], 0);
234}
235
236/**
237 * webkit_color_chooser_request_cancel:
238 * @request: a #WebKitColorChooserRequest
239 *
240 * Cancels @request and the input element changes to use the initial color
241 * it has before the request started.
242 * The signal #WebKitColorChooserRequest::finished
243 * is emitted to notify that the request has finished.
244 *
245 * Since: 2.8
246 */
247void webkit_color_chooser_request_cancel(WebKitColorChooserRequest* request)
248{
249 g_return_if_fail(WEBKIT_IS_COLOR_CHOOSER_REQUEST(request));
250
251 if (request->priv->handled)
252 return;
253
254 request->priv->handled = true;
255 request->priv->colorChooser->cancel();
256 g_signal_emit(request, signals[FINISHED], 0);
257}
258
259WebKitColorChooserRequest* webkitColorChooserRequestCreate(WebKitColorChooser* colorChooser)
260{
261 WebKitColorChooserRequest* request = WEBKIT_COLOR_CHOOSER_REQUEST(
262 g_object_new(WEBKIT_TYPE_COLOR_CHOOSER_REQUEST, "rgba", colorChooser->initialColor(), nullptr));
263 request->priv->colorChooser = colorChooser;
264 return request;
265}
266