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 "gtkcolorchooser.h"
|
|
|
|
#include "gtkcolorchooserprivate.h"
|
|
|
|
#include "gtkintl.h"
|
|
|
|
#include "gtktypebuiltins.h"
|
|
|
|
#include "gtkprivate.h"
|
2019-08-11 06:34:43 +00:00
|
|
|
#include "gtksnapshot.h"
|
|
|
|
#include "gdk/gdkrgbaprivate.h"
|
2012-01-29 15:42:34 +00:00
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* GtkColorChooser:
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2021-02-26 16:26:32 +00:00
|
|
|
* `GtkColorChooser` is an interface that is implemented by widgets
|
|
|
|
* for choosing colors.
|
|
|
|
*
|
|
|
|
* Depending on the situation, colors may be allowed to have alpha (translucency).
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2020-09-12 16:01:04 +00:00
|
|
|
* In GTK, the main widgets that implement this interface are
|
2021-02-26 16:26:32 +00:00
|
|
|
* [class@Gtk.ColorChooserWidget], [class@Gtk.ColorChooserDialog] and
|
|
|
|
* [class@Gtk.ColorButton].
|
2012-02-04 00:40:36 +00:00
|
|
|
*/
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
COLOR_ACTIVATED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL];
|
|
|
|
|
|
|
|
G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
|
|
|
|
{
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* GtkColorChooser:rgba: (attributes org.gtk.Property.get=gtk_color_chooser_get_rgba org.gtk.Property.set=gtk_color_chooser_set_rgba)
|
|
|
|
*
|
|
|
|
* The currently selected color, as a `GdkRGBA` struct.
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2021-02-26 16:26:32 +00:00
|
|
|
* The property can be set to change the current selection
|
|
|
|
* programmatically.
|
2012-02-04 00:40:36 +00:00
|
|
|
*/
|
2012-01-29 15:42:34 +00:00
|
|
|
g_object_interface_install_property (iface,
|
2012-02-03 23:34:33 +00:00
|
|
|
g_param_spec_boxed ("rgba",
|
2012-01-29 15:42:34 +00:00
|
|
|
P_("Color"),
|
|
|
|
P_("Current color, as a GdkRGBA"),
|
|
|
|
GDK_TYPE_RGBA,
|
|
|
|
GTK_PARAM_READWRITE));
|
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* GtkColorChooser:use-alpha: (attributes org.gtk.Property.get=gtk_color_chooser_get_use_alpha org.gtk.Property.set=gtk_color_chooser_set_use_alpha)
|
|
|
|
*
|
|
|
|
* Whether colors may have alpha (translucency).
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
2021-02-26 16:26:32 +00:00
|
|
|
* When ::use-alpha is %FALSE, the `GdkRGBA` struct obtained
|
|
|
|
* via the [property@Gtk.ColorChooser:rgba] property will be
|
|
|
|
* forced to have alpha == 1.
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
|
|
|
* Implementations are expected to show alpha by rendering the color
|
|
|
|
* over a non-uniform background (like a checkerboard pattern).
|
|
|
|
*/
|
2012-01-31 07:05:37 +00:00
|
|
|
g_object_interface_install_property (iface,
|
2012-02-03 23:34:33 +00:00
|
|
|
g_param_spec_boolean ("use-alpha",
|
|
|
|
P_("Use alpha"),
|
2012-01-31 07:05:37 +00:00
|
|
|
P_("Whether alpha should be shown"),
|
|
|
|
TRUE,
|
2014-06-08 15:30:58 +00:00
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
|
2012-02-01 06:53:08 +00:00
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
/**
|
|
|
|
* GtkColorChooser::color-activated:
|
2012-02-04 00:40:36 +00:00
|
|
|
* @chooser: the object which received the signal
|
2012-01-29 15:42:34 +00:00
|
|
|
* @color: the color
|
|
|
|
*
|
|
|
|
* Emitted when a color is activated from the color chooser.
|
2021-02-26 16:26:32 +00:00
|
|
|
*
|
2012-01-29 15:42:34 +00:00
|
|
|
* This usually happens when the user clicks a color swatch,
|
|
|
|
* or a color is selected and the user presses one of the keys
|
|
|
|
* Space, Shift+Space, Return or Enter.
|
2012-02-04 00:40:36 +00:00
|
|
|
*/
|
2012-01-29 15:42:34 +00:00
|
|
|
signals[COLOR_ACTIVATED] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("color-activated"),
|
2012-01-29 15:42:34 +00:00
|
|
|
GTK_TYPE_COLOR_CHOOSER,
|
|
|
|
G_SIGNAL_RUN_FIRST,
|
|
|
|
G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
|
|
|
|
NULL, NULL,
|
|
|
|
NULL,
|
|
|
|
G_TYPE_NONE,
|
2012-03-02 17:29:33 +00:00
|
|
|
1, GDK_TYPE_RGBA);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 05:02:49 +00:00
|
|
|
void
|
|
|
|
_gtk_color_chooser_color_activated (GtkColorChooser *chooser,
|
|
|
|
const GdkRGBA *color)
|
|
|
|
{
|
|
|
|
g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
|
|
|
|
}
|
|
|
|
|
2012-01-29 15:42:34 +00:00
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* gtk_color_chooser_get_rgba: (attributes org.gtk.Method.get_property=rgba)
|
|
|
|
* @chooser: a `GtkColorChooser`
|
|
|
|
* @color: (out): a `GdkRGBA` to fill in with the current color
|
2012-01-29 15:42:34 +00:00
|
|
|
*
|
|
|
|
* Gets the currently-selected color.
|
|
|
|
*/
|
|
|
|
void
|
2012-02-03 23:34:33 +00:00
|
|
|
gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
|
|
|
|
GdkRGBA *color)
|
2012-01-29 15:42:34 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
|
|
|
|
|
2012-02-03 23:34:33 +00:00
|
|
|
GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* gtk_color_chooser_set_rgba: (attributes org.gtk.Method.set_property=rgba)
|
|
|
|
* @chooser: a `GtkColorChooser`
|
2012-01-29 15:42:34 +00:00
|
|
|
* @color: the new color
|
|
|
|
*
|
2012-02-04 00:40:36 +00:00
|
|
|
* Sets the color.
|
2012-01-29 15:42:34 +00:00
|
|
|
*/
|
|
|
|
void
|
2012-02-03 23:34:33 +00:00
|
|
|
gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
|
|
|
|
const GdkRGBA *color)
|
2012-01-29 15:42:34 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
|
|
|
|
g_return_if_fail (color != NULL);
|
|
|
|
|
2012-02-03 23:34:33 +00:00
|
|
|
GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
|
2012-01-29 15:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* gtk_color_chooser_get_use_alpha: (attributes org.gtk.Method.get_property=use-alpha)
|
|
|
|
* @chooser: a `GtkColorChooser`
|
2012-02-04 00:40:36 +00:00
|
|
|
*
|
|
|
|
* Returns whether the color chooser shows the alpha channel.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the color chooser uses the alpha channel,
|
2021-05-18 21:05:26 +00:00
|
|
|
* %FALSE if not
|
2012-02-04 00:40:36 +00:00
|
|
|
*/
|
2012-01-31 07:05:37 +00:00
|
|
|
gboolean
|
2012-02-03 23:34:33 +00:00
|
|
|
gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser)
|
2012-01-31 07:05:37 +00:00
|
|
|
{
|
2012-02-03 23:34:33 +00:00
|
|
|
gboolean use_alpha;
|
2012-01-31 07:05:37 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);
|
|
|
|
|
2012-02-03 23:34:33 +00:00
|
|
|
g_object_get (chooser, "use-alpha", &use_alpha, NULL);
|
2012-01-31 07:05:37 +00:00
|
|
|
|
2012-02-03 23:34:33 +00:00
|
|
|
return use_alpha;
|
2012-01-31 07:05:37 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 00:40:36 +00:00
|
|
|
/**
|
2021-02-26 16:26:32 +00:00
|
|
|
* gtk_color_chooser_set_use_alpha: (attributes org.gtk.Method.set_property=use-alpha)
|
|
|
|
* @chooser: a `GtkColorChooser`
|
2012-02-04 00:40:36 +00:00
|
|
|
* @use_alpha: %TRUE if color chooser should use alpha channel, %FALSE if not
|
|
|
|
*
|
|
|
|
* Sets whether or not the color chooser should use the alpha channel.
|
|
|
|
*/
|
2012-01-31 07:05:37 +00:00
|
|
|
void
|
2012-02-03 23:34:33 +00:00
|
|
|
gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser,
|
|
|
|
gboolean use_alpha)
|
2012-01-31 07:05:37 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
|
|
|
|
|
2012-02-03 23:34:33 +00:00
|
|
|
g_object_set (chooser, "use-alpha", use_alpha, NULL);
|
2012-01-31 07:05:37 +00:00
|
|
|
}
|
2012-02-04 06:45:55 +00:00
|
|
|
|
2012-02-09 02:25:02 +00:00
|
|
|
/**
|
|
|
|
* gtk_color_chooser_add_palette:
|
2021-02-26 16:26:32 +00:00
|
|
|
* @chooser: a `GtkColorChooser`
|
2012-03-07 02:07:02 +00:00
|
|
|
* @orientation: %GTK_ORIENTATION_HORIZONTAL if the palette should
|
2021-05-18 21:05:26 +00:00
|
|
|
* be displayed in rows, %GTK_ORIENTATION_VERTICAL for columns
|
2012-02-09 02:25:02 +00:00
|
|
|
* @colors_per_line: the number of colors to show in each row/column
|
|
|
|
* @n_colors: the total number of elements in @colors
|
2021-05-19 11:24:34 +00:00
|
|
|
* @colors: (nullable) (array length=n_colors): the colors of the palette, or %NULL
|
2012-02-09 02:25:02 +00:00
|
|
|
*
|
2021-02-26 16:26:32 +00:00
|
|
|
* Adds a palette to the color chooser.
|
|
|
|
*
|
|
|
|
* If @orientation is horizontal, the colors are grouped in rows,
|
|
|
|
* with @colors_per_line colors in each row. If @horizontal is %FALSE,
|
|
|
|
* the colors are grouped in columns instead.
|
2012-02-09 02:25:02 +00:00
|
|
|
*
|
2021-02-26 16:26:32 +00:00
|
|
|
* The default color palette of [class@Gtk.ColorChooserWidget] has
|
|
|
|
* 45 colors, organized in columns of 5 colors (this includes some
|
|
|
|
* grays).
|
2012-02-09 02:25:02 +00:00
|
|
|
*
|
|
|
|
* The layout of the color chooser widget works best when the
|
|
|
|
* palettes have 9-10 columns.
|
|
|
|
*
|
2021-02-26 16:26:32 +00:00
|
|
|
* Calling this function for the first time has the side effect
|
|
|
|
* of removing the default color palette from the color chooser.
|
2012-03-04 05:28:08 +00:00
|
|
|
*
|
|
|
|
* If @colors is %NULL, removes all previously added palettes.
|
2012-02-09 02:25:02 +00:00
|
|
|
*/
|
2012-02-04 06:45:55 +00:00
|
|
|
void
|
|
|
|
gtk_color_chooser_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)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
|
|
|
|
|
2012-02-09 02:25:02 +00:00
|
|
|
if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette)
|
2012-03-07 02:07:02 +00:00
|
|
|
GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, orientation, colors_per_line, n_colors, colors);
|
2012-02-04 06:45:55 +00:00
|
|
|
}
|
2012-02-14 17:16:56 +00:00
|
|
|
|
2019-08-11 06:34:43 +00:00
|
|
|
void
|
|
|
|
_gtk_color_chooser_snapshot_checkered_pattern (GtkSnapshot *snapshot,
|
|
|
|
int width,
|
|
|
|
int height)
|
2012-02-14 17:16:56 +00:00
|
|
|
{
|
2019-08-11 06:34:43 +00:00
|
|
|
const GdkRGBA color1 = GDK_RGBA("A8A8A8");
|
2020-02-21 12:58:49 +00:00
|
|
|
const GdkRGBA color2 = GDK_RGBA("545454");
|
2019-08-11 06:34:43 +00:00
|
|
|
|
|
|
|
gtk_snapshot_push_repeat (snapshot, &GRAPHENE_RECT_INIT (0, 0, width, height), NULL);
|
|
|
|
gtk_snapshot_append_color (snapshot, &color1, &GRAPHENE_RECT_INIT (0, 0, 10, 10));
|
|
|
|
gtk_snapshot_append_color (snapshot, &color2, &GRAPHENE_RECT_INIT (10, 0, 10, 10));
|
|
|
|
gtk_snapshot_append_color (snapshot, &color2, &GRAPHENE_RECT_INIT (0, 10, 10, 10));
|
|
|
|
gtk_snapshot_append_color (snapshot, &color1, &GRAPHENE_RECT_INIT (10, 10, 10, 10));
|
|
|
|
gtk_snapshot_pop (snapshot);
|
2012-02-14 17:16:56 +00:00
|
|
|
}
|