2013-06-22 17:39:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "nsColor.h"
|
|
|
|
#include "nsColorPicker.h"
|
|
|
|
#include "nsGtkUtils.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "WidgetUtils.h"
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
|
2013-06-22 17:39:43 +04:00
|
|
|
|
|
|
|
int nsColorPicker::convertGdkColorComponent(guint16 color_component) {
|
|
|
|
// GdkColor value is in range [0..65535]. We need something in range [0..255]
|
2014-02-18 23:44:01 +04:00
|
|
|
return (color_component * 255 + 127) / 65535;
|
2013-06-22 17:39:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
guint16 nsColorPicker::convertToGdkColorComponent(int color_component) {
|
2014-02-18 23:44:01 +04:00
|
|
|
return color_component * 65535 / 255;
|
2013-06-22 17:39:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkColor nsColorPicker::convertToGdkColor(nscolor color) {
|
|
|
|
GdkColor result = { 0 /* obsolete, unused 'pixel' value */,
|
|
|
|
convertToGdkColorComponent(NS_GET_R(color)),
|
|
|
|
convertToGdkColorComponent(NS_GET_G(color)),
|
|
|
|
convertToGdkColorComponent(NS_GET_B(color)) };
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-18 23:44:01 +04:00
|
|
|
GtkColorSelection* nsColorPicker::WidgetGetColorSelection(GtkWidget* widget)
|
|
|
|
{
|
|
|
|
return GTK_COLOR_SELECTION(gtk_color_selection_dialog_get_color_selection(
|
|
|
|
GTK_COLOR_SELECTION_DIALOG(widget)));
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:39:43 +04:00
|
|
|
/* void init (in nsIDOMWindow parent, in AString title, in short mode); */
|
|
|
|
NS_IMETHODIMP nsColorPicker::Init(nsIDOMWindow *parent,
|
|
|
|
const nsAString& title,
|
|
|
|
const nsAString& initialColor)
|
|
|
|
{
|
2014-02-18 23:46:00 +04:00
|
|
|
mParentWidget = mozilla::widget::WidgetUtils::DOMWindowToWidget(parent);
|
|
|
|
mTitle = title;
|
|
|
|
mInitialColor = initialColor;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void open (in nsIColorPickerShownCallback aColorPickerShownCallback); */
|
|
|
|
NS_IMETHODIMP nsColorPicker::Open(nsIColorPickerShownCallback *aColorPickerShownCallback)
|
|
|
|
{
|
|
|
|
|
2013-06-22 17:39:43 +04:00
|
|
|
// Input color string should be 7 length (i.e. a string representing a valid
|
|
|
|
// simple color)
|
2014-02-18 23:46:00 +04:00
|
|
|
if (mInitialColor.Length() != 7) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-06-22 17:39:43 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 23:46:00 +04:00
|
|
|
const nsAString& withoutHash = StringTail(mInitialColor, 6);
|
2013-06-22 17:39:43 +04:00
|
|
|
nscolor color;
|
|
|
|
if (!NS_HexToRGB(withoutHash, &color)) {
|
2014-02-18 23:46:00 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-06-22 17:39:43 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 23:46:00 +04:00
|
|
|
GdkColor color_gdk = convertToGdkColor(color);
|
2013-06-22 17:39:43 +04:00
|
|
|
|
|
|
|
if (mCallback) {
|
|
|
|
// It means Open has already been called: this is not allowed
|
|
|
|
NS_WARNING("mCallback is already set. Open called twice?");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mCallback = aColorPickerShownCallback;
|
|
|
|
|
|
|
|
nsXPIDLCString title;
|
|
|
|
title.Adopt(ToNewUTF8String(mTitle));
|
|
|
|
GtkWidget *color_chooser = gtk_color_selection_dialog_new(title);
|
|
|
|
|
|
|
|
GtkWindow *parent_window = GTK_WINDOW(mParentWidget->GetNativeData(NS_NATIVE_SHELLWIDGET));
|
|
|
|
if (parent_window) {
|
2014-01-21 01:09:51 +04:00
|
|
|
GtkWindow *window = GTK_WINDOW(color_chooser);
|
2013-06-22 17:39:43 +04:00
|
|
|
gtk_window_set_transient_for(window, parent_window);
|
|
|
|
gtk_window_set_destroy_with_parent(window, TRUE);
|
|
|
|
}
|
|
|
|
|
2014-02-18 23:44:01 +04:00
|
|
|
gtk_color_selection_set_current_color(WidgetGetColorSelection(color_chooser),
|
2014-02-18 23:46:00 +04:00
|
|
|
&color_gdk);
|
2013-06-22 17:39:43 +04:00
|
|
|
|
|
|
|
NS_ADDREF_THIS();
|
2014-02-18 23:44:01 +04:00
|
|
|
g_signal_connect(WidgetGetColorSelection(color_chooser), "color-changed",
|
|
|
|
G_CALLBACK(OnColorChanged), this);
|
2013-06-22 17:39:43 +04:00
|
|
|
g_signal_connect(color_chooser, "response", G_CALLBACK(OnResponse), this);
|
|
|
|
g_signal_connect(color_chooser, "destroy", G_CALLBACK(OnDestroy), this);
|
|
|
|
gtk_widget_show(color_chooser);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 23:44:01 +04:00
|
|
|
/* static */ void
|
|
|
|
nsColorPicker::OnColorChanged(GtkColorSelection* colorselection,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
static_cast<nsColorPicker*>(user_data)->Update(colorselection);
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:39:43 +04:00
|
|
|
/* static */ void
|
|
|
|
nsColorPicker::OnResponse(GtkWidget* color_chooser, gint response_id,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
static_cast<nsColorPicker*>(user_data)->
|
|
|
|
Done(color_chooser, response_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsColorPicker::OnDestroy(GtkWidget* color_chooser, gpointer user_data)
|
|
|
|
{
|
|
|
|
static_cast<nsColorPicker*>(user_data)->
|
|
|
|
Done(color_chooser, GTK_RESPONSE_CANCEL);
|
|
|
|
}
|
|
|
|
|
2014-02-18 23:44:01 +04:00
|
|
|
void
|
|
|
|
nsColorPicker::Update(GtkColorSelection* colorselection)
|
|
|
|
{
|
|
|
|
ReadValueFromColorSelection(colorselection);
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Update(mColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:39:43 +04:00
|
|
|
void
|
|
|
|
nsColorPicker::Done(GtkWidget* color_chooser, gint response)
|
|
|
|
{
|
|
|
|
switch (response) {
|
|
|
|
case GTK_RESPONSE_OK:
|
|
|
|
case GTK_RESPONSE_ACCEPT:
|
2014-02-18 23:44:01 +04:00
|
|
|
ReadValueFromColorSelection(WidgetGetColorSelection(color_chooser));
|
2013-06-22 17:39:43 +04:00
|
|
|
break;
|
|
|
|
case GTK_RESPONSE_CANCEL:
|
|
|
|
case GTK_RESPONSE_CLOSE:
|
|
|
|
case GTK_RESPONSE_DELETE_EVENT:
|
2014-02-18 23:46:00 +04:00
|
|
|
mColor = mInitialColor;
|
2013-06-22 17:39:43 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected response");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A "response" signal won't be sent again but "destroy" will be.
|
|
|
|
g_signal_handlers_disconnect_by_func(color_chooser,
|
|
|
|
FuncToGpointer(OnDestroy), this);
|
|
|
|
|
|
|
|
gtk_widget_destroy(color_chooser);
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Done(mColor);
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString nsColorPicker::ToHexString(int n)
|
|
|
|
{
|
|
|
|
nsString result;
|
|
|
|
if (n <= 0x0F) {
|
|
|
|
result.Append('0');
|
|
|
|
}
|
|
|
|
result.AppendInt(n, 16);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-18 23:44:01 +04:00
|
|
|
void nsColorPicker::ReadValueFromColorSelection(GtkColorSelection* colorselection)
|
2013-06-22 17:39:43 +04:00
|
|
|
{
|
|
|
|
GdkColor rgba;
|
2014-02-18 23:44:01 +04:00
|
|
|
gtk_color_selection_get_current_color(colorselection, &rgba);
|
2013-06-22 17:39:43 +04:00
|
|
|
|
2014-05-26 22:55:08 +04:00
|
|
|
mColor.Assign('#');
|
2013-06-22 17:39:43 +04:00
|
|
|
mColor += ToHexString(convertGdkColorComponent(rgba.red));
|
|
|
|
mColor += ToHexString(convertGdkColorComponent(rgba.green));
|
|
|
|
mColor += ToHexString(convertGdkColorComponent(rgba.blue));
|
|
|
|
}
|