1/*
2 * Copyright (C) 2015 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebColorPickerGtk.h"
28
29#if ENABLE(INPUT_TYPE_COLOR)
30
31#include "WebPageProxy.h"
32#include <WebCore/GtkUtilities.h>
33#include <glib/gi18n-lib.h>
34#include <gtk/gtk.h>
35
36namespace WebKit {
37using namespace WebCore;
38
39Ref<WebColorPickerGtk> WebColorPickerGtk::create(WebPageProxy& page, const Color& initialColor, const IntRect& rect)
40{
41 return adoptRef(*new WebColorPickerGtk(page, initialColor, rect));
42}
43
44WebColorPickerGtk::WebColorPickerGtk(WebPageProxy& page, const Color& initialColor, const IntRect&)
45 : WebColorPicker(&page)
46 , m_initialColor(initialColor)
47 , m_webView(page.viewWidget())
48 , m_colorChooser(nullptr)
49{
50}
51
52WebColorPickerGtk::~WebColorPickerGtk()
53{
54 endPicker();
55}
56
57void WebColorPickerGtk::cancel()
58{
59 setSelectedColor(m_initialColor);
60}
61
62void WebColorPickerGtk::endPicker()
63{
64 if (!m_colorChooser)
65 return;
66
67 gtk_widget_destroy(m_colorChooser);
68 m_colorChooser = nullptr;
69}
70
71void WebColorPickerGtk::didChooseColor(const Color& color)
72{
73 if (!m_client)
74 return;
75
76 m_client->didChooseColor(color);
77}
78
79void WebColorPickerGtk::colorChooserDialogRGBAChangedCallback(GtkColorChooser* colorChooser, GParamSpec*, WebColorPickerGtk* colorPicker)
80{
81 GdkRGBA rgba;
82 gtk_color_chooser_get_rgba(colorChooser, &rgba);
83 colorPicker->didChooseColor(rgba);
84}
85
86void WebColorPickerGtk::colorChooserDialogResponseCallback(GtkColorChooser*, int responseID, WebColorPickerGtk* colorPicker)
87{
88 if (responseID != GTK_RESPONSE_OK)
89 colorPicker->cancel();
90 colorPicker->endPicker();
91}
92
93void WebColorPickerGtk::showColorPicker(const Color& color)
94{
95 if (!m_client)
96 return;
97
98 m_initialColor = color;
99
100 if (!m_colorChooser) {
101 GtkWidget* toplevel = gtk_widget_get_toplevel(m_webView);
102 m_colorChooser = gtk_color_chooser_dialog_new(_("Select Color"), WebCore::widgetIsOnscreenToplevelWindow(toplevel) ? GTK_WINDOW(toplevel) : nullptr);
103 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(m_colorChooser), &m_initialColor);
104 g_signal_connect(m_colorChooser, "notify::rgba", G_CALLBACK(WebColorPickerGtk::colorChooserDialogRGBAChangedCallback), this);
105 g_signal_connect(m_colorChooser, "response", G_CALLBACK(WebColorPickerGtk::colorChooserDialogResponseCallback), this);
106 } else
107 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(m_colorChooser), &m_initialColor);
108
109 gtk_widget_show(m_colorChooser);
110}
111
112} // namespace WebKit
113
114#endif // ENABLE(INPUT_TYPE_COLOR)
115