Bug 1198256 - Replace deprecated GtkColorSelectionDialog with GtkColorChooserDialog in Gtk3. r=karlt

This commit is contained in:
Petr Jašíček 2015-09-01 04:09:00 -04:00
parent 68a1353ed5
commit ff26d1d901
3 changed files with 123 additions and 32 deletions

View File

@ -562,6 +562,12 @@ STUB(gtk_app_chooser_get_type)
STUB(gtk_app_chooser_get_app_info)
STUB(gtk_app_chooser_dialog_get_type)
STUB(gtk_app_chooser_dialog_set_heading)
STUB(gtk_color_chooser_dialog_new)
STUB(gtk_color_chooser_dialog_get_type)
STUB(gtk_color_chooser_get_type)
STUB(gtk_color_chooser_set_rgba)
STUB(gtk_color_chooser_get_rgba)
STUB(gtk_color_chooser_set_use_alpha)
#endif
#ifdef GTK2_SYMBOLS

View File

@ -13,6 +13,25 @@
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
#if GTK_CHECK_VERSION(3,4,0)
int nsColorPicker::convertGdkRgbaComponent(gdouble color_component) {
// GdkRGBA value is in range [0.0..1.0]. We need something in range [0..255]
return color_component * 255 + 0.5;
}
gdouble nsColorPicker::convertToGdkRgbaComponent(int color_component) {
return color_component / 255.0;
}
GdkRGBA nsColorPicker::convertToRgbaColor(nscolor color) {
GdkRGBA result = { convertToGdkRgbaComponent(NS_GET_R(color)),
convertToGdkRgbaComponent(NS_GET_G(color)),
convertToGdkRgbaComponent(NS_GET_B(color)),
convertToGdkRgbaComponent(NS_GET_A(color)) };
return result;
}
#else
int nsColorPicker::convertGdkColorComponent(guint16 color_component) {
// GdkColor value is in range [0..65535]. We need something in range [0..255]
return (color_component * 255 + 127) / 65535;
@ -36,6 +55,7 @@ GtkColorSelection* nsColorPicker::WidgetGetColorSelection(GtkWidget* widget)
return GTK_COLOR_SELECTION(gtk_color_selection_dialog_get_color_selection(
GTK_COLOR_SELECTION_DIALOG(widget)));
}
#endif
NS_IMETHODIMP nsColorPicker::Init(nsIDOMWindow *parent,
const nsAString& title,
@ -63,8 +83,6 @@ NS_IMETHODIMP nsColorPicker::Open(nsIColorPickerShownCallback *aColorPickerShown
return NS_ERROR_FAILURE;
}
GdkColor color_gdk = convertToGdkColor(color);
if (mCallback) {
// It means Open has already been called: this is not allowed
NS_WARNING("mCallback is already set. Open called twice?");
@ -74,21 +92,41 @@ NS_IMETHODIMP nsColorPicker::Open(nsIColorPickerShownCallback *aColorPickerShown
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 GTK_CHECK_VERSION(3,4,0)
GtkWidget* color_chooser = gtk_color_chooser_dialog_new(title, parent_window);
if (parent_window) {
gtk_window_set_destroy_with_parent(GTK_WINDOW(color_chooser), TRUE);
}
gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(color_chooser), FALSE);
GdkRGBA color_rgba = convertToRgbaColor(color);
gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(color_chooser),
&color_rgba);
g_signal_connect(GTK_COLOR_CHOOSER(color_chooser), "color-activated",
G_CALLBACK(OnColorChanged), this);
#else
GtkWidget *color_chooser = gtk_color_selection_dialog_new(title);
if (parent_window) {
GtkWindow *window = GTK_WINDOW(color_chooser);
gtk_window_set_transient_for(window, parent_window);
gtk_window_set_destroy_with_parent(window, TRUE);
}
GdkColor color_gdk = convertToGdkColor(color);
gtk_color_selection_set_current_color(WidgetGetColorSelection(color_chooser),
&color_gdk);
NS_ADDREF_THIS();
g_signal_connect(WidgetGetColorSelection(color_chooser), "color-changed",
G_CALLBACK(OnColorChanged), this);
#endif
NS_ADDREF_THIS();
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);
@ -96,6 +134,31 @@ NS_IMETHODIMP nsColorPicker::Open(nsIColorPickerShownCallback *aColorPickerShown
return NS_OK;
}
#if GTK_CHECK_VERSION(3,4,0)
/* static */ void
nsColorPicker::OnColorChanged(GtkColorChooser* color_chooser, GdkRGBA* color,
gpointer user_data)
{
static_cast<nsColorPicker*>(user_data)->Update(color);
}
void
nsColorPicker::Update(GdkRGBA* color)
{
SetColor(color);
if (mCallback) {
mCallback->Update(mColor);
}
}
void nsColorPicker::SetColor(const GdkRGBA* color)
{
mColor.Assign('#');
mColor += ToHexString(convertGdkRgbaComponent(color->red));
mColor += ToHexString(convertGdkRgbaComponent(color->green));
mColor += ToHexString(convertGdkRgbaComponent(color->blue));
}
#else
/* static */ void
nsColorPicker::OnColorChanged(GtkColorSelection* colorselection,
gpointer user_data)
@ -103,6 +166,27 @@ nsColorPicker::OnColorChanged(GtkColorSelection* colorselection,
static_cast<nsColorPicker*>(user_data)->Update(colorselection);
}
void
nsColorPicker::Update(GtkColorSelection* colorselection)
{
ReadValueFromColorSelection(colorselection);
if (mCallback) {
mCallback->Update(mColor);
}
}
void nsColorPicker::ReadValueFromColorSelection(GtkColorSelection* colorselection)
{
GdkColor rgba;
gtk_color_selection_get_current_color(colorselection, &rgba);
mColor.Assign('#');
mColor += ToHexString(convertGdkColorComponent(rgba.red));
mColor += ToHexString(convertGdkColorComponent(rgba.green));
mColor += ToHexString(convertGdkColorComponent(rgba.blue));
}
#endif
/* static */ void
nsColorPicker::OnResponse(GtkWidget* color_chooser, gint response_id,
gpointer user_data)
@ -118,22 +202,19 @@ nsColorPicker::OnDestroy(GtkWidget* color_chooser, gpointer user_data)
Done(color_chooser, GTK_RESPONSE_CANCEL);
}
void
nsColorPicker::Update(GtkColorSelection* colorselection)
{
ReadValueFromColorSelection(colorselection);
if (mCallback) {
mCallback->Update(mColor);
}
}
void
nsColorPicker::Done(GtkWidget* color_chooser, gint response)
{
switch (response) {
case GTK_RESPONSE_OK:
case GTK_RESPONSE_ACCEPT:
#if GTK_CHECK_VERSION(3,4,0)
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(color_chooser), &color);
SetColor(&color);
#else
ReadValueFromColorSelection(WidgetGetColorSelection(color_chooser));
#endif
break;
case GTK_RESPONSE_CANCEL:
case GTK_RESPONSE_CLOSE:
@ -167,14 +248,3 @@ nsString nsColorPicker::ToHexString(int n)
result.AppendInt(n, 16);
return result;
}
void nsColorPicker::ReadValueFromColorSelection(GtkColorSelection* colorselection)
{
GdkColor rgba;
gtk_color_selection_get_current_color(colorselection, &rgba);
mColor.Assign('#');
mColor += ToHexString(convertGdkColorComponent(rgba.red));
mColor += ToHexString(convertGdkColorComponent(rgba.green));
mColor += ToHexString(convertGdkColorComponent(rgba.blue));
}

View File

@ -25,23 +25,38 @@ public:
private:
~nsColorPicker() {};
static void OnColorChanged(GtkColorSelection* colorselection,
gpointer user_data);
static nsString ToHexString(int n);
static void OnResponse(GtkWidget* dialog, gint response_id,
gpointer user_data);
static void OnDestroy(GtkWidget* dialog, gpointer user_data);
#if GTK_CHECK_VERSION(3,4,0)
static void OnColorChanged(GtkColorChooser* color_chooser, GdkRGBA* color,
gpointer user_data);
static int convertGdkRgbaComponent(gdouble color_component);
static gdouble convertToGdkRgbaComponent(int color_component);
static GdkRGBA convertToRgbaColor(nscolor color);
void Update(GdkRGBA* color);
void SetColor(const GdkRGBA* color);
#else
static void OnColorChanged(GtkColorSelection* colorselection,
gpointer user_data);
// Conversion functions for color
static int convertGdkColorComponent(guint16 color_component);
static guint16 convertToGdkColorComponent(int color_component);
static GdkColor convertToGdkColor(nscolor color);
static nsString ToHexString(int n);
static GdkColor convertToGdkColor(nscolor color);
static GtkColorSelection* WidgetGetColorSelection(GtkWidget* widget);
void Done(GtkWidget* dialog, gint response_id);
void Update(GtkColorSelection* colorselection);
void ReadValueFromColorSelection(GtkColorSelection* colorselection);
#endif
void Done(GtkWidget* dialog, gint response_id);
nsCOMPtr<nsIWidget> mParentWidget;
nsCOMPtr<nsIColorPickerShownCallback> mCallback;