2012-01-29 15:42:34 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-29 15:42:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkdialog.h"
|
2013-12-08 14:13:25 +00:00
|
|
|
#include "gtkdialogprivate.h"
|
2012-03-08 03:06:22 +00:00
|
|
|
#include "gtkbutton.h"
|
2012-01-29 15:42:34 +00:00
|
|
|
#include "gtkbox.h"
|
2012-02-01 06:53:08 +00:00
|
|
|
#include "gtkprivate.h"
|
2013-12-08 14:13:25 +00:00
|
|
|
#include "gtksettings.h"
|
2012-01-29 15:42:34 +00:00
|
|
|
|
|
|
|
#include "gtkcolorchooserprivate.h"
|
|
|
|
#include "gtkcolorchooserdialog.h"
|
|
|
|
#include "gtkcolorchooserwidget.h"
|
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
2021-02-26 16:25:46 +00:00
|
|
|
* GtkColorChooserDialog:
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2021-02-26 16:25:46 +00:00
|
|
|
* A dialog for choosing a color.
|
|
|
|
*
|
|
|
|
* ![An example GtkColorChooserDialog](colorchooser.png)
|
|
|
|
*
|
2021-02-26 21:08:35 +00:00
|
|
|
* `GtkColorChooserDialog` implements the [iface@Gtk.ColorChooser] interface
|
2021-02-26 16:25:46 +00:00
|
|
|
* and does not provide much API of its own.
|
|
|
|
*
|
|
|
|
* To create a `GtkColorChooserDialog`, use [ctor@Gtk.ColorChooserDialog.new].
|
|
|
|
*
|
|
|
|
* To change the initially selected color, use
|
|
|
|
* [method@Gtk.ColorChooser.set_rgba]. To get the selected color use
|
|
|
|
* [method@Gtk.ColorChooser.get_rgba].
|
2012-02-04 00:40:36 +00:00
|
|
|
*/
|
2012-01-29 15:42:34 +00:00
|
|
|
|
2019-05-28 00:04:15 +00:00
|
|
|
typedef struct _GtkColorChooserDialogClass GtkColorChooserDialogClass;
|
|
|
|
|
|
|
|
struct _GtkColorChooserDialog
|
|
|
|
{
|
|
|
|
GtkDialog parent_instance;
|
2020-03-28 21:12:40 +00:00
|
|
|
|
|
|
|
GtkWidget *chooser;
|
2019-05-28 00:04:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkColorChooserDialogClass
|
|
|
|
{
|
|
|
|
GtkDialogClass parent_class;
|
|
|
|
};
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_ZERO,
|
2012-02-04 00:40:36 +00:00
|
|
|
PROP_RGBA,
|
|
|
|
PROP_USE_ALPHA,
|
2012-02-01 06:53:08 +00:00
|
|
|
PROP_SHOW_EDITOR
|
2012-01-29 15:42:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkColorChooserDialog, gtk_color_chooser_dialog, GTK_TYPE_DIALOG,
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,
|
|
|
|
gtk_color_chooser_dialog_iface_init))
|
|
|
|
|
|
|
|
static void
|
|
|
|
propagate_notify (GObject *o,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkColorChooserDialog *cc)
|
|
|
|
{
|
2012-02-09 02:54:55 +00:00
|
|
|
g_object_notify (G_OBJECT (cc), pspec->name);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 03:06:22 +00:00
|
|
|
static void
|
|
|
|
save_color (GtkColorChooserDialog *dialog)
|
|
|
|
{
|
|
|
|
GdkRGBA color;
|
|
|
|
|
|
|
|
/* This causes the color chooser widget to save the
|
|
|
|
* selected and custom colors to GSettings.
|
|
|
|
*/
|
|
|
|
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
|
|
|
|
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (dialog), &color);
|
|
|
|
}
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
static void
|
|
|
|
color_activated_cb (GtkColorChooser *chooser,
|
|
|
|
GdkRGBA *color,
|
|
|
|
GtkDialog *dialog)
|
|
|
|
{
|
2012-03-08 03:06:22 +00:00
|
|
|
save_color (GTK_COLOR_CHOOSER_DIALOG (dialog));
|
|
|
|
gtk_dialog_response (dialog, GTK_RESPONSE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-08 14:13:25 +00:00
|
|
|
gtk_color_chooser_dialog_response (GtkDialog *dialog,
|
2020-07-24 13:54:49 +00:00
|
|
|
int response_id,
|
2013-12-08 14:13:25 +00:00
|
|
|
gpointer user_data)
|
2012-03-08 03:06:22 +00:00
|
|
|
{
|
2013-12-08 14:13:25 +00:00
|
|
|
if (response_id == GTK_RESPONSE_OK)
|
|
|
|
save_color (GTK_COLOR_CHOOSER_DIALOG (dialog));
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_init (GtkColorChooserDialog *cc)
|
|
|
|
{
|
2013-03-23 15:41:07 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (cc));
|
2013-12-08 14:13:25 +00:00
|
|
|
gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (cc));
|
|
|
|
|
|
|
|
g_signal_connect (cc, "response",
|
|
|
|
G_CALLBACK (gtk_color_chooser_dialog_response), NULL);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 02:42:28 +00:00
|
|
|
static void
|
2015-08-12 11:22:05 +00:00
|
|
|
gtk_color_chooser_dialog_unmap (GtkWidget *widget)
|
2012-03-08 02:42:28 +00:00
|
|
|
{
|
2015-08-12 11:22:05 +00:00
|
|
|
GTK_WIDGET_CLASS (gtk_color_chooser_dialog_parent_class)->unmap (widget);
|
|
|
|
|
2012-03-08 02:42:28 +00:00
|
|
|
/* We never want the dialog to come up with the editor,
|
|
|
|
* even if it was showing the editor the last time it was used.
|
|
|
|
*/
|
2015-08-12 11:22:05 +00:00
|
|
|
g_object_set (widget, "show-editor", FALSE, NULL);
|
2012-01-31 05:31:41 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2020-03-28 21:12:40 +00:00
|
|
|
GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (object);
|
2012-01-29 15:42:34 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2012-02-04 00:40:36 +00:00
|
|
|
case PROP_RGBA:
|
2012-01-29 15:42:34 +00:00
|
|
|
{
|
|
|
|
GdkRGBA color;
|
|
|
|
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (cc), &color);
|
2012-01-29 15:42:34 +00:00
|
|
|
g_value_set_boxed (value, &color);
|
|
|
|
}
|
2012-02-01 06:53:08 +00:00
|
|
|
break;
|
2012-02-04 00:40:36 +00:00
|
|
|
case PROP_USE_ALPHA:
|
2020-03-28 21:12:40 +00:00
|
|
|
g_value_set_boolean (value, gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (cc->chooser)));
|
2012-01-31 07:05:37 +00:00
|
|
|
break;
|
2012-02-01 06:53:08 +00:00
|
|
|
case PROP_SHOW_EDITOR:
|
|
|
|
{
|
|
|
|
gboolean show_editor;
|
2020-03-28 21:12:40 +00:00
|
|
|
g_object_get (cc->chooser, "show-editor", &show_editor, NULL);
|
2012-02-01 06:53:08 +00:00
|
|
|
g_value_set_boolean (value, show_editor);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-29 15:42:34 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2020-03-28 21:12:40 +00:00
|
|
|
GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (object);
|
2012-01-29 15:42:34 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2012-02-04 00:40:36 +00:00
|
|
|
case PROP_RGBA:
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc), g_value_get_boxed (value));
|
2012-02-01 06:53:08 +00:00
|
|
|
break;
|
2012-02-04 00:40:36 +00:00
|
|
|
case PROP_USE_ALPHA:
|
2020-03-28 21:12:40 +00:00
|
|
|
if (gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (cc->chooser)) != g_value_get_boolean (value))
|
2014-06-08 15:31:37 +00:00
|
|
|
{
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (cc->chooser), g_value_get_boolean (value));
|
2014-06-08 15:31:37 +00:00
|
|
|
g_object_notify_by_pspec (object, pspec);
|
|
|
|
}
|
2012-02-01 06:53:08 +00:00
|
|
|
break;
|
|
|
|
case PROP_SHOW_EDITOR:
|
2020-03-28 21:12:40 +00:00
|
|
|
g_object_set (cc->chooser,
|
2012-02-01 06:53:08 +00:00
|
|
|
"show-editor", g_value_get_boolean (value),
|
|
|
|
NULL);
|
|
|
|
break;
|
2012-01-29 15:42:34 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 18:14:55 +00:00
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (object);
|
|
|
|
|
|
|
|
g_clear_pointer (&cc->chooser, gtk_widget_unparent);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_color_chooser_dialog_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_class_init (GtkColorChooserDialogClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
2012-03-08 02:42:28 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
|
2012-01-29 15:42:34 +00:00
|
|
|
|
2020-05-09 18:14:55 +00:00
|
|
|
object_class->dispose = gtk_color_chooser_dialog_dispose;
|
2012-01-29 15:42:34 +00:00
|
|
|
object_class->get_property = gtk_color_chooser_dialog_get_property;
|
|
|
|
object_class->set_property = gtk_color_chooser_dialog_set_property;
|
|
|
|
|
2015-08-12 11:22:05 +00:00
|
|
|
widget_class->unmap = gtk_color_chooser_dialog_unmap;
|
2012-03-08 02:42:28 +00:00
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
g_object_class_override_property (object_class, PROP_RGBA, "rgba");
|
|
|
|
g_object_class_override_property (object_class, PROP_USE_ALPHA, "use-alpha");
|
2012-02-01 06:53:08 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_SHOW_EDITOR,
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_boolean ("show-editor", NULL, NULL,
|
2012-02-01 06:53:08 +00:00
|
|
|
FALSE, GTK_PARAM_READWRITE));
|
|
|
|
|
2013-03-23 15:41:07 +00:00
|
|
|
/* Bind class to template
|
|
|
|
*/
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class,
|
2014-01-23 23:59:20 +00:00
|
|
|
"/org/gtk/libgtk/ui/gtkcolorchooserdialog.ui");
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkColorChooserDialog, chooser);
|
2013-07-26 17:49:49 +00:00
|
|
|
gtk_widget_class_bind_template_callback (widget_class, propagate_notify);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, color_activated_cb);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-02-04 00:40:36 +00:00
|
|
|
gtk_color_chooser_dialog_get_rgba (GtkColorChooser *chooser,
|
|
|
|
GdkRGBA *color)
|
2012-01-29 15:42:34 +00:00
|
|
|
{
|
|
|
|
GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);
|
|
|
|
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (cc->chooser), color);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-02-04 00:40:36 +00:00
|
|
|
gtk_color_chooser_dialog_set_rgba (GtkColorChooser *chooser,
|
|
|
|
const GdkRGBA *color)
|
2012-01-29 15:42:34 +00:00
|
|
|
{
|
|
|
|
GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);
|
|
|
|
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->chooser), color);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 06:45:55 +00:00
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_add_palette (GtkColorChooser *chooser,
|
2012-03-07 02:07:02 +00:00
|
|
|
GtkOrientation orientation,
|
2020-07-24 13:54:49 +00:00
|
|
|
int colors_per_line,
|
|
|
|
int n_colors,
|
2012-02-04 06:45:55 +00:00
|
|
|
GdkRGBA *colors)
|
|
|
|
{
|
|
|
|
GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);
|
|
|
|
|
2020-03-28 21:12:40 +00:00
|
|
|
gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (cc->chooser),
|
2012-03-07 02:07:02 +00:00
|
|
|
orientation, colors_per_line, n_colors, colors);
|
2012-02-04 06:45:55 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
static void
|
|
|
|
gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface)
|
|
|
|
{
|
2012-02-04 00:40:36 +00:00
|
|
|
iface->get_rgba = gtk_color_chooser_dialog_get_rgba;
|
|
|
|
iface->set_rgba = gtk_color_chooser_dialog_set_rgba;
|
2012-02-04 06:45:55 +00:00
|
|
|
iface->add_palette = gtk_color_chooser_dialog_add_palette;
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
|
|
|
* gtk_color_chooser_dialog_new:
|
2021-05-21 00:45:06 +00:00
|
|
|
* @title: (nullable): Title of the dialog
|
|
|
|
* @parent: (nullable): Transient parent of the dialog
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2021-02-26 16:25:46 +00:00
|
|
|
* Creates a new `GtkColorChooserDialog`.
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2021-02-26 16:25:46 +00:00
|
|
|
* Returns: a new `GtkColorChooserDialog`
|
2012-02-04 00:40:36 +00:00
|
|
|
*/
|
2012-01-29 15:42:34 +00:00
|
|
|
GtkWidget *
|
2020-07-24 18:40:36 +00:00
|
|
|
gtk_color_chooser_dialog_new (const char *title,
|
2012-01-29 15:42:34 +00:00
|
|
|
GtkWindow *parent)
|
|
|
|
{
|
2020-03-28 21:12:40 +00:00
|
|
|
return g_object_new (GTK_TYPE_COLOR_CHOOSER_DIALOG,
|
|
|
|
"title", title,
|
|
|
|
"transient-for", parent,
|
|
|
|
NULL);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|