2004-07-27 02:52:35 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpcellrenderercolor.c
|
2007-12-13 00:14:49 +08:00
|
|
|
* Copyright (C) 2004,2007 Sven Neuman <sven1@gimp.org>
|
2004-07-27 02:52:35 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2004-07-27 02:52:35 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2004-07-27 02:52:35 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
2004-07-27 02:52:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-05-03 09:36:22 +08:00
|
|
|
#include <gegl.h>
|
2004-07-27 02:52:35 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2007-12-12 17:10:07 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2004-07-27 02:52:35 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
|
|
|
|
#include "gimpwidgetstypes.h"
|
|
|
|
|
2007-12-12 22:41:25 +08:00
|
|
|
#include "gimpcairo-utils.h"
|
2004-07-27 02:52:35 +08:00
|
|
|
#include "gimpcellrenderercolor.h"
|
|
|
|
|
|
|
|
|
2010-07-06 00:01:28 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpcellrenderercolor
|
|
|
|
* @title: GimpCellRendererColor
|
2024-03-18 00:19:25 +08:00
|
|
|
* @short_description: A #GtkCellRenderer to display a #GeglColor color.
|
2010-07-06 00:01:28 +08:00
|
|
|
*
|
2024-03-18 00:19:25 +08:00
|
|
|
* A #GtkCellRenderer to display a #GeglColor color.
|
2010-07-06 00:01:28 +08:00
|
|
|
**/
|
|
|
|
|
|
|
|
|
2004-07-27 05:09:16 +08:00
|
|
|
#define DEFAULT_ICON_SIZE GTK_ICON_SIZE_MENU
|
|
|
|
|
|
|
|
|
2004-07-27 02:52:35 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_COLOR,
|
|
|
|
PROP_OPAQUE,
|
|
|
|
PROP_SIZE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
struct _GimpCellRendererColor
|
2011-01-01 00:27:50 +08:00
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
GtkCellRenderer parent_instance;
|
2011-01-01 00:27:50 +08:00
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
GeglColor *color;
|
|
|
|
gboolean opaque;
|
|
|
|
GtkIconSize size;
|
|
|
|
gint border;
|
|
|
|
};
|
2011-01-01 00:27:50 +08:00
|
|
|
|
|
|
|
|
2024-03-14 12:04:53 +08:00
|
|
|
static void gimp_cell_renderer_color_finalize (GObject *object);
|
2010-10-15 16:25:40 +08:00
|
|
|
static void gimp_cell_renderer_color_get_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gimp_cell_renderer_color_set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gimp_cell_renderer_color_get_size (GtkCellRenderer *cell,
|
|
|
|
GtkWidget *widget,
|
|
|
|
const GdkRectangle *rectangle,
|
|
|
|
gint *x_offset,
|
|
|
|
gint *y_offset,
|
|
|
|
gint *width,
|
|
|
|
gint *height);
|
|
|
|
static void gimp_cell_renderer_color_render (GtkCellRenderer *cell,
|
|
|
|
cairo_t *cr,
|
|
|
|
GtkWidget *widget,
|
|
|
|
const GdkRectangle *background_area,
|
|
|
|
const GdkRectangle *cell_area,
|
2004-07-27 02:52:35 +08:00
|
|
|
GtkCellRendererState flags);
|
|
|
|
|
2007-12-12 17:10:07 +08:00
|
|
|
|
2004-07-27 02:52:35 +08:00
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
G_DEFINE_TYPE (GimpCellRendererColor, gimp_cell_renderer_color, GTK_TYPE_CELL_RENDERER)
|
2004-07-27 02:52:35 +08:00
|
|
|
|
2005-12-21 04:35:23 +08:00
|
|
|
#define parent_class gimp_cell_renderer_color_parent_class
|
2004-07-27 02:52:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_cell_renderer_color_class_init (GimpCellRendererColorClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
|
|
|
|
|
2024-03-14 12:04:53 +08:00
|
|
|
object_class->finalize = gimp_cell_renderer_color_finalize;
|
2004-07-27 02:52:35 +08:00
|
|
|
object_class->get_property = gimp_cell_renderer_color_get_property;
|
|
|
|
object_class->set_property = gimp_cell_renderer_color_set_property;
|
|
|
|
|
|
|
|
cell_class->get_size = gimp_cell_renderer_color_get_size;
|
|
|
|
cell_class->render = gimp_cell_renderer_color_render;
|
|
|
|
|
2017-06-07 03:19:17 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_COLOR,
|
2024-04-20 05:02:29 +08:00
|
|
|
gimp_param_spec_color ("color",
|
2024-03-14 12:04:53 +08:00
|
|
|
"Color",
|
|
|
|
"The displayed color",
|
2024-04-20 05:02:29 +08:00
|
|
|
TRUE, NULL,
|
2024-03-14 12:04:53 +08:00
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2017-06-07 03:19:17 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_OPAQUE,
|
|
|
|
g_param_spec_boolean ("opaque",
|
|
|
|
"Opaque",
|
|
|
|
"Whether to show transparency",
|
2004-07-27 02:52:35 +08:00
|
|
|
TRUE,
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READWRITE |
|
2004-07-27 02:52:35 +08:00
|
|
|
G_PARAM_CONSTRUCT));
|
2017-06-07 03:19:17 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_SIZE,
|
|
|
|
g_param_spec_int ("icon-size",
|
|
|
|
"Icon Size",
|
|
|
|
"The cell's size",
|
2004-07-27 02:52:35 +08:00
|
|
|
0, G_MAXINT,
|
2004-07-27 05:09:16 +08:00
|
|
|
DEFAULT_ICON_SIZE,
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READWRITE |
|
2004-07-27 02:52:35 +08:00
|
|
|
G_PARAM_CONSTRUCT));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_cell_renderer_color_init (GimpCellRendererColor *cell)
|
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
cell->color = gegl_color_new ("black");
|
2024-03-14 12:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_cell_renderer_color_finalize (GObject *object)
|
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
GimpCellRendererColor *renderer = GIMP_CELL_RENDERER_COLOR (object);
|
2024-03-14 12:04:53 +08:00
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
g_clear_object (&renderer->color);
|
2024-03-14 12:04:53 +08:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2004-07-27 02:52:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_cell_renderer_color_get_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
GimpCellRendererColor *renderer = GIMP_CELL_RENDERER_COLOR (object);
|
2004-07-27 02:52:35 +08:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_COLOR:
|
2024-07-29 09:51:38 +08:00
|
|
|
g_clear_object (&renderer->color);
|
|
|
|
renderer->color = gegl_color_duplicate (g_value_get_object (value));
|
2004-07-27 02:52:35 +08:00
|
|
|
break;
|
|
|
|
case PROP_OPAQUE:
|
2024-07-29 09:51:38 +08:00
|
|
|
g_value_set_boolean (value, renderer->opaque);
|
2004-07-27 02:52:35 +08:00
|
|
|
break;
|
|
|
|
case PROP_SIZE:
|
2024-07-29 09:51:38 +08:00
|
|
|
g_value_set_int (value, renderer->size);
|
2004-07-27 02:52:35 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_cell_renderer_color_set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
GimpCellRendererColor *renderer = GIMP_CELL_RENDERER_COLOR (object);
|
2004-07-27 02:52:35 +08:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_COLOR:
|
2024-07-29 09:51:38 +08:00
|
|
|
g_set_object (&renderer->color, g_value_get_object (value));
|
2004-07-27 02:52:35 +08:00
|
|
|
break;
|
|
|
|
case PROP_OPAQUE:
|
2024-07-29 09:51:38 +08:00
|
|
|
renderer->opaque = g_value_get_boolean (value);
|
2004-07-27 02:52:35 +08:00
|
|
|
break;
|
|
|
|
case PROP_SIZE:
|
2024-07-29 09:51:38 +08:00
|
|
|
renderer->size = g_value_get_int (value);
|
2004-07-27 02:52:35 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-15 16:25:40 +08:00
|
|
|
gimp_cell_renderer_color_get_size (GtkCellRenderer *cell,
|
|
|
|
GtkWidget *widget,
|
|
|
|
const GdkRectangle *cell_area,
|
|
|
|
gint *x_offset,
|
|
|
|
gint *y_offset,
|
|
|
|
gint *width,
|
|
|
|
gint *height)
|
2004-07-27 02:52:35 +08:00
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
GimpCellRendererColor *renderer = GIMP_CELL_RENDERER_COLOR (cell);
|
|
|
|
gint calc_width;
|
|
|
|
gint calc_height;
|
|
|
|
gfloat xalign;
|
|
|
|
gfloat yalign;
|
|
|
|
gint xpad;
|
|
|
|
gint ypad;
|
|
|
|
|
|
|
|
gtk_icon_size_lookup (renderer->size, &calc_width, &calc_height);
|
2009-10-18 00:51:48 +08:00
|
|
|
gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
|
|
|
|
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
|
2004-07-27 02:52:35 +08:00
|
|
|
|
2004-07-27 04:26:07 +08:00
|
|
|
if (cell_area && calc_width > 0 && calc_height > 0)
|
2004-07-27 02:52:35 +08:00
|
|
|
{
|
|
|
|
if (x_offset)
|
2006-04-12 18:53:28 +08:00
|
|
|
{
|
|
|
|
*x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
|
2009-10-18 00:51:48 +08:00
|
|
|
1.0 - xalign : xalign) *
|
2004-07-27 04:26:07 +08:00
|
|
|
(cell_area->width - calc_width));
|
2009-10-18 00:51:48 +08:00
|
|
|
*x_offset = MAX (*x_offset, 0) + xpad;
|
2006-04-12 18:53:28 +08:00
|
|
|
}
|
2004-07-27 02:52:35 +08:00
|
|
|
if (y_offset)
|
2006-04-12 18:53:28 +08:00
|
|
|
{
|
2009-10-18 00:51:48 +08:00
|
|
|
*y_offset = (yalign * (cell_area->height - calc_height));
|
|
|
|
*y_offset = MAX (*y_offset, 0) + ypad;
|
2006-04-12 18:53:28 +08:00
|
|
|
}
|
2004-07-27 02:52:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (x_offset)
|
|
|
|
*x_offset = 0;
|
|
|
|
if (y_offset)
|
|
|
|
*y_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width)
|
2009-10-18 00:51:48 +08:00
|
|
|
*width = calc_width + 2 * xpad;
|
2004-07-27 02:52:35 +08:00
|
|
|
if (height)
|
2009-10-18 00:51:48 +08:00
|
|
|
*height = calc_height + 2 * ypad;
|
2004-07-27 02:52:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_cell_renderer_color_render (GtkCellRenderer *cell,
|
2010-10-15 16:25:40 +08:00
|
|
|
cairo_t *cr,
|
2004-07-27 02:52:35 +08:00
|
|
|
GtkWidget *widget,
|
2010-10-15 16:25:40 +08:00
|
|
|
const GdkRectangle *background_area,
|
|
|
|
const GdkRectangle *cell_area,
|
2004-07-27 02:52:35 +08:00
|
|
|
GtkCellRendererState flags)
|
|
|
|
{
|
2024-07-29 09:51:38 +08:00
|
|
|
GimpCellRendererColor *renderer = GIMP_CELL_RENDERER_COLOR (cell);
|
|
|
|
GdkRectangle rect;
|
|
|
|
gint xpad;
|
|
|
|
gint ypad;
|
2004-07-27 02:52:35 +08:00
|
|
|
|
|
|
|
gimp_cell_renderer_color_get_size (cell, widget, cell_area,
|
|
|
|
&rect.x,
|
|
|
|
&rect.y,
|
|
|
|
&rect.width,
|
|
|
|
&rect.height);
|
|
|
|
|
2009-10-18 00:51:48 +08:00
|
|
|
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
|
|
|
|
|
|
|
|
rect.x += cell_area->x + xpad;
|
|
|
|
rect.y += cell_area->y + ypad;
|
|
|
|
rect.width -= 2 * xpad;
|
|
|
|
rect.height -= 2 * ypad;
|
2004-07-27 02:52:35 +08:00
|
|
|
|
|
|
|
if (rect.width > 2 && rect.height > 2)
|
|
|
|
{
|
2010-12-10 06:10:01 +08:00
|
|
|
GtkStyleContext *context = gtk_widget_get_style_context (widget);
|
2011-02-07 00:44:49 +08:00
|
|
|
GtkStateFlags state;
|
2010-12-10 06:10:01 +08:00
|
|
|
GdkRGBA color;
|
2024-03-14 12:04:53 +08:00
|
|
|
gdouble rgba[4];
|
2007-12-12 17:10:07 +08:00
|
|
|
|
|
|
|
cairo_rectangle (cr,
|
|
|
|
rect.x + 1, rect.y + 1,
|
|
|
|
rect.width - 2, rect.height - 2);
|
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
gimp_cairo_set_source_color (cr, renderer->color, NULL, FALSE,
|
2024-03-14 12:04:53 +08:00
|
|
|
widget);
|
2007-12-12 17:10:07 +08:00
|
|
|
cairo_fill (cr);
|
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
gegl_color_get_pixel (renderer->color, babl_format ("R'G'B'A double"), rgba);
|
|
|
|
if (! renderer->opaque && rgba[3] < 1.0)
|
2007-12-12 17:10:07 +08:00
|
|
|
{
|
2007-12-12 22:41:25 +08:00
|
|
|
cairo_pattern_t *pattern;
|
|
|
|
|
2007-12-12 17:10:07 +08:00
|
|
|
cairo_move_to (cr, rect.x + 1, rect.y + rect.height - 1);
|
|
|
|
cairo_line_to (cr, rect.x + rect.width - 1, rect.y + rect.height - 1);
|
|
|
|
cairo_line_to (cr, rect.x + rect.width - 1, rect.y + 1);
|
|
|
|
cairo_close_path (cr);
|
|
|
|
|
2007-12-29 02:44:32 +08:00
|
|
|
pattern = gimp_cairo_checkerboard_create (cr,
|
|
|
|
GIMP_CHECK_SIZE_SM,
|
|
|
|
NULL, NULL);
|
2007-12-12 22:41:25 +08:00
|
|
|
cairo_set_source (cr, pattern);
|
|
|
|
cairo_pattern_destroy (pattern);
|
|
|
|
|
2007-12-12 17:10:07 +08:00
|
|
|
cairo_fill_preserve (cr);
|
|
|
|
|
2024-07-29 09:51:38 +08:00
|
|
|
gimp_cairo_set_source_color (cr, renderer->color, NULL, FALSE,
|
2024-03-14 12:04:53 +08:00
|
|
|
widget);
|
2007-12-12 17:10:07 +08:00
|
|
|
cairo_fill (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw border */
|
|
|
|
cairo_rectangle (cr,
|
|
|
|
rect.x + 0.5, rect.y + 0.5,
|
|
|
|
rect.width - 1, rect.height - 1);
|
|
|
|
|
2011-02-07 00:44:49 +08:00
|
|
|
state = gtk_cell_renderer_get_state (cell, widget, flags);
|
2007-12-12 17:10:07 +08:00
|
|
|
|
|
|
|
cairo_set_line_width (cr, 1);
|
2010-12-10 06:10:01 +08:00
|
|
|
gtk_style_context_get_color (context, state, &color);
|
|
|
|
gdk_cairo_set_source_rgba (cr, &color);
|
|
|
|
cairo_stroke (cr);
|
2004-07-27 02:52:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-27 02:55:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_cell_renderer_color_new:
|
|
|
|
*
|
|
|
|
* Creates a #GtkCellRenderer that displays a color.
|
|
|
|
*
|
2019-08-03 06:10:14 +08:00
|
|
|
* Returns: a new #GimpCellRendererColor
|
2004-07-27 02:55:50 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.2
|
2004-07-27 02:55:50 +08:00
|
|
|
**/
|
2004-07-27 02:52:35 +08:00
|
|
|
GtkCellRenderer *
|
|
|
|
gimp_cell_renderer_color_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GIMP_TYPE_CELL_RENDERER_COLOR, NULL);
|
|
|
|
}
|