1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimplabelcolor.c
* Copyright (C) 2022 Jehan
*
* 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 3 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpbase/gimpbase.h"
#include "gimpwidgets.h"
#include "gimpwidgets-private.h"
/**
* SECTION: gimplabelcolor
* @title: GimpLabelColor
* @short_description: Widget containing a color widget and a label.
*
* This widget is a subclass of #GimpLabeled with a #GtkColor.
**/
enum
{
VALUE_CHANGED,
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_VALUE,
PROP_EDITABLE,
N_PROPS
};
static GParamSpec *object_props[N_PROPS] = { NULL, };
struct _GimpLabelColor
{
GimpLabeled parent_instance;
GtkWidget *area;
gboolean editable;
};
static void gimp_label_color_constructed (GObject *object);
static void gimp_label_color_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_label_color_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static GtkWidget * gimp_label_color_populate (GimpLabeled *color,
gint *x,
gint *y,
gint *width,
gint *height);
G_DEFINE_TYPE (GimpLabelColor, gimp_label_color, GIMP_TYPE_LABELED)
#define parent_class gimp_label_color_parent_class
static guint gimp_label_color_signals[LAST_SIGNAL] = { 0 };
static void
gimp_label_color_class_init (GimpLabelColorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpLabeledClass *labeled_class = GIMP_LABELED_CLASS (klass);
gimp_label_color_signals[VALUE_CHANGED] =
g_signal_new ("value-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL, NULL,
G_TYPE_NONE, 0);
object_class->constructed = gimp_label_color_constructed;
object_class->set_property = gimp_label_color_set_property;
object_class->get_property = gimp_label_color_get_property;
labeled_class->populate = gimp_label_color_populate;
babl_init ();
/**
* GimpLabelColor:value:
*
* The currently set value.
*
* Since: 3.0
**/
object_props[PROP_VALUE] = gimp_param_spec_color_from_string ("value",
"Color",
"The displayed color",
TRUE, "black",
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT);
/**
* GimpLabelColor:editable:
*
* Whether the color can be edited.
*
* Since: 3.0
**/
object_props[PROP_EDITABLE] = g_param_spec_boolean ("editable",
"Whether the color can be edited",
"Whether the color can be edited",
FALSE,
GIMP_PARAM_READWRITE);
g_object_class_install_properties (object_class, N_PROPS, object_props);
}
static void
gimp_label_color_init (GimpLabelColor *color)
{
GeglColor *black = gegl_color_new ("black");
color->editable = FALSE;
color->area = gimp_color_area_new (black, GIMP_COLOR_AREA_SMALL_CHECKS,
GDK_BUTTON1_MASK | GDK_BUTTON2_MASK);
/* Typically for a labelled color area, a small square next to your
* label is probably what you want to display.
*/
gtk_widget_set_size_request (color->area, 20, 20);
g_object_unref (black);
}
static void
gimp_label_color_constructed (GObject *object)
{
GimpLabelColor *color = GIMP_LABEL_COLOR (object);
G_OBJECT_CLASS (parent_class)->constructed (object);
/* This is important to make this object into a property widget. It
* will allow config object to bind the "value" property of this
* widget, and therefore be updated automatically.
*/
g_object_bind_property (G_OBJECT (color->area), "color",
G_OBJECT (color), "value",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}
static void
gimp_label_color_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpLabelColor *lcolor = GIMP_LABEL_COLOR (object);
switch (property_id)
{
case PROP_VALUE:
{
GeglColor *new_color;
GeglColor *color;
new_color = g_value_get_object (value);
g_object_get (lcolor->area,
"color", &color,
NULL);
/* Avoid looping forever since we have bound this widget's
* "value" property with the color button "value" property.
*/
if (! gimp_color_is_perceptually_identical (color, new_color))
{
g_object_set (lcolor->area, "color", new_color, NULL);
g_signal_emit (object, gimp_label_color_signals[VALUE_CHANGED], 0);
}
g_object_unref (color);
}
break;
case PROP_EDITABLE:
if (lcolor->editable != g_value_get_boolean (value))
{
const gchar *dialog_title;
GimpLabeled *labeled;
GeglColor *color;
GimpColorAreaType type;
gboolean attached;
labeled = GIMP_LABELED (lcolor);
/* Reuse the label contents (without mnemonics) as dialog
* title for the color selection. This is why the "editable"
* property must not be a G_PARAM_CONSTRUCT.
*/
dialog_title = gtk_label_get_text (GTK_LABEL (gimp_labeled_get_label (labeled)));
attached = (gtk_widget_get_parent (lcolor->area) != NULL);
g_object_get (lcolor->area,
"type", &type,
"color", &color,
NULL);
gtk_widget_destroy (lcolor->area);
lcolor->editable = g_value_get_boolean (value);
if (lcolor->editable)
lcolor->area = gimp_color_button_new (dialog_title,
20, 20, color, type);
else
lcolor->area = gimp_color_area_new (color, type,
GDK_BUTTON1_MASK | GDK_BUTTON2_MASK);
g_object_unref (color);
gtk_widget_set_size_request (lcolor->area, 20, 20);
g_object_bind_property (G_OBJECT (lcolor->area), "color",
G_OBJECT (lcolor), "value",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
if (attached)
{
gtk_grid_attach (GTK_GRID (lcolor), lcolor->area, 1, 0, 1, 1);
gtk_widget_show (lcolor->area);
g_signal_emit_by_name (object, "mnemonic-widget-changed", lcolor->area);
}
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_label_color_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpLabelColor *lcolor = GIMP_LABEL_COLOR (object);
switch (property_id)
{
case PROP_VALUE:
{
GeglColor *color;
g_object_get (lcolor->area,
"color", &color,
NULL);
g_value_take_object (value, color);
}
break;
case PROP_EDITABLE:
g_value_set_boolean (value, lcolor->editable);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static GtkWidget *
gimp_label_color_populate (GimpLabeled *labeled,
gint *x,
gint *y,
gint *width,
gint *height)
{
GimpLabelColor *color = GIMP_LABEL_COLOR (labeled);
gtk_grid_attach (GTK_GRID (color), color->area, 1, 0, 1, 1);
/* Make sure the label and color won't be glued next to each other's. */
gtk_grid_set_column_spacing (GTK_GRID (color),
4 * gtk_widget_get_scale_factor (GTK_WIDGET (color)));
gtk_widget_show (color->area);
return color->area;
}
/* Public Functions */
/**
* gimp_label_color_new:
* @label: The text for the #GtkLabel.
* @color: The color displayed.
*
* Creates a #GimpLabelColor which contains a widget and displays a
* color area. By default, the color area is of type
* %GIMP_COLOR_AREA_SMALL_CHECKS, which means transparency of @color
* will be shown.
*
* Moreover in the non-editable case, the color is draggable to other
* widgets accepting color drops with buttons 1 and 2.
* In the editable case, the @label is reused as the color chooser's
* dialog title.
*
* If you wish to customize any of these default behaviors, get the
* #GimpColorArea or #GimpColorButton with gimp_label_color_get_color_widget().
*
* Returns: (transfer full): The new #GimpLabelColor widget.
**/
GtkWidget *
gimp_label_color_new (const gchar *label,
GeglColor *color,
gboolean editable)
{
GtkWidget *labeled;
labeled = g_object_new (GIMP_TYPE_LABEL_COLOR,
"label", label,
"value", color,
"editable", editable,
NULL);
return labeled;
}
/**
* gimp_label_color_set_value:
* @color: The #GtkLabelColor.
* @value: A new value.
*
* This function sets the value in the #GtkColor inside @color.
**/
void
gimp_label_color_set_value (GimpLabelColor *color,
GeglColor *value)
{
g_return_if_fail (GIMP_IS_LABEL_COLOR (color));
g_return_if_fail (GEGL_IS_COLOR (value));
g_object_set (color,
"value", value,
NULL);
}
/**
* gimp_label_color_get_value:
* @color: The #GtkLabelColor.
*
* This function returns the value shown by @color.
*
* Returns: (transfer full): a copy of the [class@Gegl.Color] used by the widget.
**/
GeglColor *
gimp_label_color_get_value (GimpLabelColor *color)
{
GeglColor *value = NULL;
GeglColor *retval;
g_return_val_if_fail (GIMP_IS_LABEL_COLOR (color), NULL);
g_object_get (color->area,
"color", &value,
NULL);
retval = gegl_color_duplicate (value);
g_clear_object (&value);
return retval;
}
/**
* gimp_label_color_set_editable:
* @color: The #GtkLabelColor.
* @editable: Whether the color should be editable.
*
* Changes the editability of the color.
**/
void
gimp_label_color_set_editable (GimpLabelColor *color,
gboolean editable)
{
g_return_if_fail (GIMP_IS_LABEL_COLOR (color));
g_object_set (color, "editable", editable, NULL);
}
/**
* gimp_label_color_is_editable:
* @color: The #GtkLabelColor.
*
* This function tells whether the color widget allows to edit the
* color.
* Returns: %TRUE if the color is editable.
**/
gboolean
gimp_label_color_is_editable (GimpLabelColor *color)
{
g_return_val_if_fail (GIMP_IS_LABEL_COLOR (color), FALSE);
return GIMP_IS_COLOR_SELECT (color->area);
}
/**
* gimp_label_color_get_color_widget:
* @color: The #GimpLabelColor
*
* This function returns the color widget packed in @color, which can be
* either a #GimpColorButton (if the @color is editable) or a
* #GimpColorArea otherwise.
*
* Returns: (transfer none): The color widget packed in @color.
**/
GtkWidget *
gimp_label_color_get_color_widget (GimpLabelColor *color)
{
g_return_val_if_fail (GIMP_IS_LABEL_COLOR (color), NULL);
return color->area;
}
|