1
/* LIBGIMP - The GIMP Library
2
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
4
* gimpgradientselectbutton.c
5
* Copyright (C) 1998 Andy Thomas
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
27
#include "libgimpwidgets/gimpwidgets.h"
31
#include "gimpuitypes.h"
32
#include "gimpgradientselectbutton.h"
33
#include "gimpuimarshal.h"
35
#include "libgimp-intl.h"
38
#define CELL_HEIGHT 18
42
#define GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIMP_TYPE_GRADIENT_SELECT_BUTTON, GimpGradientSelectButtonPrivate))
44
typedef struct _GimpGradientSelectButtonPrivate GimpGradientSelectButtonPrivate;
46
struct _GimpGradientSelectButtonPrivate
50
gchar *gradient_name; /* Local copy */
54
gdouble *gradient_data; /* Local copy */
74
/* local function prototypes */
76
static void gimp_gradient_select_button_finalize (GObject *object);
78
static void gimp_gradient_select_button_set_property (GObject *object,
82
static void gimp_gradient_select_button_get_property (GObject *object,
87
static void gimp_gradient_select_button_clicked (GimpGradientSelectButton *button);
89
static void gimp_gradient_select_button_callback (const gchar *gradient_name,
91
const gdouble *gradient_data,
92
gboolean dialog_closing,
95
static void gimp_gradient_select_preview_size_allocate
97
GtkAllocation *allocation,
98
GimpGradientSelectButton *button);
99
static void gimp_gradient_select_preview_expose (GtkWidget *preview,
100
GdkEventExpose *event,
101
GimpGradientSelectButton *button);
103
static void gimp_gradient_select_drag_data_received (GimpGradientSelectButton *button,
104
GdkDragContext *context,
107
GtkSelectionData *selection,
111
static GtkWidget * gimp_gradient_select_button_create_inside (GimpGradientSelectButton *button);
114
static const GtkTargetEntry target = { "application/x-gimp-gradient-name", 0 };
116
static guint gradient_button_signals[LAST_SIGNAL] = { 0 };
119
G_DEFINE_TYPE (GimpGradientSelectButton, gimp_gradient_select_button,
120
GIMP_TYPE_SELECT_BUTTON)
124
gimp_gradient_select_button_class_init (GimpGradientSelectButtonClass *klass)
126
GObjectClass *object_class = G_OBJECT_CLASS (klass);
127
GimpSelectButtonClass *select_button_class = GIMP_SELECT_BUTTON_CLASS (klass);
129
object_class->finalize = gimp_gradient_select_button_finalize;
130
object_class->set_property = gimp_gradient_select_button_set_property;
131
object_class->get_property = gimp_gradient_select_button_get_property;
133
select_button_class->select_destroy = gimp_gradient_select_destroy;
135
klass->gradient_set = NULL;
138
* GimpGradientSelectButton:title:
140
* The title to be used for the gradient selection popup dialog.
144
g_object_class_install_property (object_class, PROP_TITLE,
145
g_param_spec_string ("title",
147
"The title to be used for the gradient selection popup dialog",
148
_("Gradient Selection"),
149
GIMP_PARAM_READWRITE |
150
G_PARAM_CONSTRUCT_ONLY));
153
* GimpGradientSelectButton:gradient-name:
155
* The name of the currently selected gradient.
159
g_object_class_install_property (object_class, PROP_GRADIENT_NAME,
160
g_param_spec_string ("gradient-name",
162
"The name of the currently selected gradient",
164
GIMP_PARAM_READWRITE));
167
* GimpGradientSelectButton::gradient-set:
168
* @widget: the object which received the signal.
169
* @gradient_name: the name of the currently selected gradient.
170
* @width: width of the gradient
171
* @grad_data: gradient data
172
* @dialog_closing: whether the dialog was closed or not.
174
* The ::gradient-set signal is emitted when the user selects a gradient.
178
gradient_button_signals[GRADIENT_SET] =
179
g_signal_new ("gradient-set",
180
G_TYPE_FROM_CLASS (klass),
182
G_STRUCT_OFFSET (GimpGradientSelectButtonClass, gradient_set),
184
_gimpui_marshal_VOID__STRING_INT_POINTER_BOOLEAN,
191
g_type_class_add_private (object_class,
192
sizeof (GimpGradientSelectButtonPrivate));
196
gimp_gradient_select_button_init (GimpGradientSelectButton *button)
198
GimpGradientSelectButtonPrivate *priv;
200
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
202
priv->gradient_name = gimp_context_get_gradient ();
203
priv->sample_size = CELL_WIDTH;
204
priv->reverse = FALSE;
206
priv->inside = gimp_gradient_select_button_create_inside (button);
207
gtk_container_add (GTK_CONTAINER (button), priv->inside);
211
* gimp_gradient_select_button_new:
212
* @title: Title of the dialog to use or %NULL to use the default title.
213
* @gradient_name: Initial gradient name.
215
* Creates a new #GtkWidget that completely controls the selection of
216
* a gradient. This widget is suitable for placement in a table in a
219
* Returns: A #GtkWidget that you can use in your UI.
224
gimp_gradient_select_button_new (const gchar *title,
225
const gchar *gradient_name)
230
button = g_object_new (GIMP_TYPE_GRADIENT_SELECT_BUTTON,
232
"gradient-name", gradient_name,
235
button = g_object_new (GIMP_TYPE_GRADIENT_SELECT_BUTTON,
236
"gradient-name", gradient_name,
243
* gimp_gradient_select_button_get_gradient:
244
* @button: A #GimpGradientSelectButton
246
* Retrieves the name of currently selected gradient.
248
* Returns: an internal copy of the gradient name which must not be freed.
253
gimp_gradient_select_button_get_gradient (GimpGradientSelectButton *button)
255
GimpGradientSelectButtonPrivate *priv;
257
g_return_val_if_fail (GIMP_IS_GRADIENT_SELECT_BUTTON (button), NULL);
259
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
260
return priv->gradient_name;
264
* gimp_gradient_select_button_set_gradient:
265
* @button: A #GimpGradientSelectButton
266
* @gradient_name: Gradient name to set.
268
* Sets the current gradient for the gradient select button.
273
gimp_gradient_select_button_set_gradient (GimpGradientSelectButton *button,
274
const gchar *gradient_name)
276
GimpGradientSelectButtonPrivate *priv;
277
GimpSelectButton *select_button;
279
g_return_if_fail (GIMP_IS_GRADIENT_SELECT_BUTTON (button));
281
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
282
select_button = GIMP_SELECT_BUTTON (button);
284
if (select_button->temp_callback)
286
gimp_gradients_set_popup (select_button->temp_callback, gradient_name);
294
if (gradient_name && *gradient_name)
295
name = g_strdup (gradient_name);
297
name = gimp_context_get_gradient ();
299
if (gimp_gradient_get_uniform_samples (name,
305
gimp_gradient_select_button_callback (name,
317
/* private functions */
320
gimp_gradient_select_button_finalize (GObject *object)
322
GimpGradientSelectButtonPrivate *priv;
324
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (object);
326
g_free (priv->gradient_name);
327
priv->gradient_name = NULL;
329
g_free (priv->gradient_data);
330
priv->gradient_data = NULL;
332
g_free (priv->title);
335
G_OBJECT_CLASS (gimp_gradient_select_button_parent_class)->finalize (object);
339
gimp_gradient_select_button_set_property (GObject *object,
344
GimpGradientSelectButton *button;
345
GimpGradientSelectButtonPrivate *priv;
347
button = GIMP_GRADIENT_SELECT_BUTTON (object);
348
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
353
priv->title = g_value_dup_string (value);
355
case PROP_GRADIENT_NAME:
356
gimp_gradient_select_button_set_gradient (button,
357
g_value_get_string (value));
360
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
366
gimp_gradient_select_button_get_property (GObject *object,
371
GimpGradientSelectButton *button;
372
GimpGradientSelectButtonPrivate *priv;
374
button = GIMP_GRADIENT_SELECT_BUTTON (object);
375
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
380
g_value_set_string (value, priv->title);
382
case PROP_GRADIENT_NAME:
383
g_value_set_string (value, priv->gradient_name);
386
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
392
gimp_gradient_select_button_callback (const gchar *gradient_name,
394
const gdouble *gradient_data,
395
gboolean dialog_closing,
398
GimpGradientSelectButton *button;
399
GimpGradientSelectButtonPrivate *priv;
400
GimpSelectButton *select_button;
402
button = GIMP_GRADIENT_SELECT_BUTTON (user_data);
404
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
405
select_button = GIMP_SELECT_BUTTON (button);
407
g_free (priv->gradient_name);
408
g_free (priv->gradient_data);
410
priv->gradient_name = g_strdup (gradient_name);
411
priv->n_samples = n_samples;
412
priv->gradient_data = g_memdup (gradient_data, n_samples * sizeof (gdouble));
414
gtk_widget_queue_draw (priv->preview);
417
select_button->temp_callback = NULL;
419
g_signal_emit (button, gradient_button_signals[GRADIENT_SET], 0,
420
gradient_name, n_samples, gradient_data, dialog_closing);
421
g_object_notify (G_OBJECT (button), "gradient-name");
425
gimp_gradient_select_button_clicked (GimpGradientSelectButton *button)
427
GimpGradientSelectButtonPrivate *priv;
428
GimpSelectButton *select_button;
430
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
431
select_button = GIMP_SELECT_BUTTON (button);
433
if (select_button->temp_callback)
435
/* calling gimp_gradients_set_popup() raises the dialog */
436
gimp_gradients_set_popup (select_button->temp_callback,
437
priv->gradient_name);
441
select_button->temp_callback =
442
gimp_gradient_select_new (priv->title, priv->gradient_name,
444
gimp_gradient_select_button_callback,
450
gimp_gradient_select_preview_size_allocate (GtkWidget *widget,
451
GtkAllocation *allocation,
452
GimpGradientSelectButton *button)
456
GimpGradientSelectButtonPrivate *priv;
458
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
460
if (gimp_gradient_get_uniform_samples (priv->gradient_name,
466
g_free (priv->gradient_data);
468
priv->sample_size = allocation->width;
469
priv->n_samples = n_samples;
470
priv->gradient_data = samples;
475
gimp_gradient_select_preview_expose (GtkWidget *widget,
476
GdkEventExpose *event,
477
GimpGradientSelectButton *button)
486
GimpGradientSelectButtonPrivate *priv;
488
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (button);
490
src = priv->gradient_data;
494
width = priv->n_samples / 4;
496
p0 = even = g_malloc (width * 3);
497
p1 = odd = g_malloc (width * 3);
499
for (x = 0; x < width; x++)
509
if ((x / GIMP_CHECK_SIZE_SM) & 1)
511
c0 = GIMP_CHECK_LIGHT;
512
c1 = GIMP_CHECK_DARK;
516
c0 = GIMP_CHECK_DARK;
517
c1 = GIMP_CHECK_LIGHT;
520
*p0++ = (c0 + (r - c0) * a) * 255.0;
521
*p0++ = (c0 + (g - c0) * a) * 255.0;
522
*p0++ = (c0 + (b - c0) * a) * 255.0;
524
*p1++ = (c1 + (r - c1) * a) * 255.0;
525
*p1++ = (c1 + (g - c1) * a) * 255.0;
526
*p1++ = (c1 + (b - c1) * a) * 255.0;
529
for (y = event->area.y; y < event->area.y + event->area.height; y++)
531
guchar *buf = ((y / GIMP_CHECK_SIZE_SM) & 1) ? odd : even;
533
gdk_draw_rgb_image_dithalign (widget->window,
534
widget->style->fg_gc[widget->state],
536
event->area.width, 1,
538
buf + event->area.x * 3,
540
- event->area.x, - y);
548
gimp_gradient_select_drag_data_received (GimpGradientSelectButton *button,
549
GdkDragContext *context,
552
GtkSelectionData *selection,
558
if ((selection->format != 8) || (selection->length < 1))
560
g_warning ("Received invalid gradient data!");
564
str = g_strndup ((const gchar *) selection->data, selection->length);
566
if (g_utf8_validate (str, -1, NULL))
570
gint name_offset = 0;
572
if (sscanf (str, "%i:%p:%n", &pid, &unused, &name_offset) >= 2 &&
573
pid == gimp_getpid () && name_offset > 0)
575
gchar *name = str + name_offset;
577
gimp_gradient_select_button_set_gradient (button, name);
585
gimp_gradient_select_button_create_inside (GimpGradientSelectButton *gradient_button)
588
GimpGradientSelectButtonPrivate *priv;
590
priv = GIMP_GRADIENT_SELECT_BUTTON_GET_PRIVATE (gradient_button);
592
gtk_widget_push_composite_child ();
594
button = gtk_button_new ();
596
priv->preview = gtk_drawing_area_new ();
597
gtk_widget_set_size_request (priv->preview, CELL_WIDTH, CELL_HEIGHT);
598
gtk_container_add (GTK_CONTAINER (button), priv->preview);
600
g_signal_connect (priv->preview, "size-allocate",
601
G_CALLBACK (gimp_gradient_select_preview_size_allocate),
604
g_signal_connect (priv->preview, "expose-event",
605
G_CALLBACK (gimp_gradient_select_preview_expose),
608
gtk_widget_show_all (button);
610
g_signal_connect_swapped (button, "clicked",
611
G_CALLBACK (gimp_gradient_select_button_clicked),
614
gtk_drag_dest_set (GTK_WIDGET (button),
615
GTK_DEST_DEFAULT_HIGHLIGHT |
616
GTK_DEST_DEFAULT_MOTION |
617
GTK_DEST_DEFAULT_DROP,
621
g_signal_connect_swapped (button, "drag-data-received",
622
G_CALLBACK (gimp_gradient_select_drag_data_received),
625
gtk_widget_pop_composite_child ();