2000-05-08 01:23:30 +00:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
2000-05-31 06:15:06 +00:00
|
|
|
*
|
|
|
|
* gimpgradientmenu.c
|
1998-10-31 16:22:37 +00:00
|
|
|
* Copyright (C) 1998 Andy Thomas
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
1999-11-17 21:13:50 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-10-31 16:22:37 +00:00
|
|
|
* License as published by the Free Software Foundation; either
|
2000-05-08 01:23:30 +00:00
|
|
|
* 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
|
1998-10-31 16:22:37 +00:00
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
1999-11-17 21:13:50 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
1998-10-31 16:22:37 +00:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
2000-05-08 01:23:30 +00:00
|
|
|
*/
|
|
|
|
|
2002-05-13 23:30:23 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-01-30 14:54:27 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#warning GTK_DISABLE_DEPRECATED
|
|
|
|
#endif
|
|
|
|
#undef GTK_DISABLE_DEPRECATED
|
|
|
|
|
1998-10-31 16:22:37 +00:00
|
|
|
#include "gimp.h"
|
|
|
|
#include "gimpui.h"
|
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
#include "libgimp-intl.h"
|
|
|
|
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
/* Idea is to have a function to call that returns a widget that
|
|
|
|
* completely controls the selection of a gradient.
|
|
|
|
* you get a widget returned that you can use in a table say.
|
|
|
|
* In:- Initial gradient name. Null means use current selection.
|
2003-06-17 09:43:14 +00:00
|
|
|
* pointer to func to call when gradient changes
|
|
|
|
* (GimpRunGradientCallback).
|
1998-10-31 16:22:37 +00:00
|
|
|
* Returned:- Pointer to a widget that you can use in UI.
|
|
|
|
*
|
|
|
|
* Widget simply made up of a preview widget (20x40) containing the gradient
|
|
|
|
* which can be clicked on to changed the gradient selection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
#define GSEL_DATA_KEY "__gsel_data"
|
|
|
|
#define CELL_SIZE_HEIGHT 18
|
|
|
|
#define CELL_SIZE_WIDTH 84
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
struct __gradients_sel
|
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
gchar *title;
|
|
|
|
GimpRunGradientCallback callback;
|
2000-05-31 06:15:06 +00:00
|
|
|
GtkWidget *gradient_preview;
|
|
|
|
GtkWidget *button;
|
|
|
|
gchar *gradient_name; /* Local copy */
|
|
|
|
gint sample_size;
|
2003-06-17 09:43:14 +00:00
|
|
|
gchar *gradient_popup_pnt;
|
2000-05-31 06:15:06 +00:00
|
|
|
gpointer data;
|
|
|
|
};
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
typedef struct __gradients_sel GSelect;
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
|
1998-10-31 16:22:37 +00:00
|
|
|
static void
|
2003-06-17 09:43:14 +00:00
|
|
|
gradient_preview_update (GtkWidget *gradient_preview,
|
|
|
|
gint width,
|
|
|
|
const gdouble *grad_data)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
const gdouble *src;
|
|
|
|
gdouble r, g, b, a;
|
|
|
|
gdouble c0, c1;
|
|
|
|
guchar *p0;
|
|
|
|
guchar *p1;
|
|
|
|
guchar *even;
|
|
|
|
guchar *odd;
|
|
|
|
gint x, y;
|
|
|
|
|
|
|
|
width /= 4;
|
|
|
|
width = MIN (width, CELL_SIZE_WIDTH);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
/* Draw the gradient */
|
|
|
|
src = grad_data;
|
2000-05-08 01:23:30 +00:00
|
|
|
p0 = even = g_malloc (width * 3);
|
|
|
|
p1 = odd = g_malloc (width * 3);
|
|
|
|
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
r = src[x * 4 + 0];
|
|
|
|
g = src[x * 4 + 1];
|
|
|
|
b = src[x * 4 + 2];
|
|
|
|
a = src[x * 4 + 3];
|
2000-05-08 01:23:30 +00:00
|
|
|
|
|
|
|
if ((x / GIMP_CHECK_SIZE_SM) & 1)
|
|
|
|
{
|
|
|
|
c0 = GIMP_CHECK_LIGHT;
|
|
|
|
c1 = GIMP_CHECK_DARK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c0 = GIMP_CHECK_DARK;
|
|
|
|
c1 = GIMP_CHECK_LIGHT;
|
|
|
|
}
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
*p0++ = (c0 + (r - c0) * a) * 255.0;
|
|
|
|
*p0++ = (c0 + (g - c0) * a) * 255.0;
|
|
|
|
*p0++ = (c0 + (b - c0) * a) * 255.0;
|
|
|
|
|
|
|
|
*p1++ = (c1 + (r - c1) * a) * 255.0;
|
|
|
|
*p1++ = (c1 + (g - c1) * a) * 255.0;
|
|
|
|
*p1++ = (c1 + (b - c1) * a) * 255.0;
|
|
|
|
}
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
for (y = 0; y < CELL_SIZE_HEIGHT; y++)
|
|
|
|
{
|
2000-05-08 01:23:30 +00:00
|
|
|
if ((y / GIMP_CHECK_SIZE_SM) & 1)
|
|
|
|
gtk_preview_draw_row (GTK_PREVIEW (gradient_preview),
|
2003-06-17 09:43:14 +00:00
|
|
|
odd, 0, y, width);
|
1998-10-31 16:22:37 +00:00
|
|
|
else
|
2003-06-17 09:43:14 +00:00
|
|
|
gtk_preview_draw_row (GTK_PREVIEW (gradient_preview),
|
|
|
|
even, 0, y, width);
|
1998-10-31 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
g_free (even);
|
|
|
|
g_free (odd);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2001-12-29 13:26:29 +00:00
|
|
|
gtk_widget_queue_draw (gradient_preview);
|
1998-10-31 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-06-17 09:43:14 +00:00
|
|
|
gradient_select_invoker (const gchar *name,
|
|
|
|
gint width,
|
|
|
|
const gdouble *grad_data,
|
|
|
|
gint closing,
|
|
|
|
gpointer data)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
GSelect *gsel = (GSelect*) data;
|
2000-05-08 01:23:30 +00:00
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
g_free (gsel->gradient_name);
|
2000-05-08 01:23:30 +00:00
|
|
|
|
|
|
|
gsel->gradient_name = g_strdup (name);
|
2003-06-17 09:43:14 +00:00
|
|
|
|
|
|
|
gradient_preview_update (gsel->gradient_preview, width, grad_data);
|
|
|
|
|
|
|
|
if (gsel->callback)
|
|
|
|
gsel->callback (name, width, grad_data, closing, gsel->data);
|
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
if (closing)
|
2003-06-17 09:43:14 +00:00
|
|
|
gsel->gradient_popup_pnt = NULL;
|
1998-10-31 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2003-06-17 09:43:14 +00:00
|
|
|
gradient_select_callback (GtkWidget *widget,
|
|
|
|
gpointer data)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
GSelect *gsel = (GSelect*) data;
|
2000-05-08 01:23:30 +00:00
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
if (gsel->gradient_popup_pnt)
|
|
|
|
{
|
|
|
|
/* calling gimp_gradients_set_popup() raises the dialog */
|
|
|
|
gimp_gradients_set_popup (gsel->gradient_popup_pnt, gsel->gradient_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gsel->gradient_popup_pnt =
|
|
|
|
gimp_interactive_selection_gradient (gsel->title ?
|
|
|
|
gsel->title : _("Gradient Selection"),
|
|
|
|
gsel->gradient_name,
|
|
|
|
gsel->sample_size,
|
|
|
|
gradient_select_invoker,
|
|
|
|
gsel);
|
|
|
|
}
|
1998-10-31 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2002-12-17 19:12:58 +00:00
|
|
|
/**
|
|
|
|
* gimp_gradient_select_widget:
|
2003-06-17 09:43:14 +00:00
|
|
|
* @title: Title of the dialog to use or %NULL to use the default title.
|
|
|
|
* @gradient_name: Initial gradient name or %NULL to use current selection.
|
|
|
|
* @callback: a function to call when the selected gradient changes.
|
|
|
|
* @data: a pointer to arbitary data to be used in the call to @callback.
|
2002-12-17 19:12:58 +00:00
|
|
|
*
|
|
|
|
* Creates a new #GtkWidget that completely controls the selection of a
|
|
|
|
* gradient. This widget is suitable for placement in a table in a
|
|
|
|
* plug-in dialog.
|
|
|
|
*
|
|
|
|
* Returns:A #GtkWidget that you can use in your UI.
|
|
|
|
*/
|
1998-10-31 16:22:37 +00:00
|
|
|
GtkWidget *
|
2003-06-17 09:43:14 +00:00
|
|
|
gimp_gradient_select_widget (const gchar *title,
|
|
|
|
const gchar *gradient_name,
|
|
|
|
GimpRunGradientCallback callback,
|
2000-05-31 06:15:06 +00:00
|
|
|
gpointer data)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2000-05-08 01:23:30 +00:00
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *gradient;
|
|
|
|
gint width;
|
|
|
|
gdouble *grad_data;
|
2003-06-17 09:43:14 +00:00
|
|
|
gchar *name;
|
2000-05-08 01:23:30 +00:00
|
|
|
GSelect *gsel;
|
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
gsel = g_new0 (GSelect, 1);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
hbox = gtk_hbox_new (FALSE, 3);
|
|
|
|
gtk_widget_show(hbox);
|
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
button = gtk_button_new ();
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
gradient = gtk_preview_new (GTK_PREVIEW_COLOR);
|
2003-06-17 09:43:14 +00:00
|
|
|
gtk_preview_size (GTK_PREVIEW (gradient),
|
|
|
|
CELL_SIZE_WIDTH, CELL_SIZE_HEIGHT);
|
2000-05-08 01:23:30 +00:00
|
|
|
gtk_widget_show (gradient);
|
1998-10-31 16:22:37 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (button), gradient);
|
|
|
|
|
2003-01-05 22:07:10 +00:00
|
|
|
g_signal_connect (button, "clicked",
|
2003-06-17 09:43:14 +00:00
|
|
|
G_CALLBACK (gradient_select_callback),
|
2001-07-31 12:10:14 +00:00
|
|
|
gsel);
|
2000-05-08 01:23:30 +00:00
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
gtk_widget_show (button);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
gsel->button = button;
|
|
|
|
gsel->callback = callback;
|
|
|
|
gsel->data = data;
|
|
|
|
gsel->gradient_preview = gradient;
|
|
|
|
gsel->sample_size = CELL_SIZE_WIDTH;
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
/* Do initial gradient setup */
|
2003-06-17 09:43:14 +00:00
|
|
|
name = gimp_gradients_get_gradient_data ((gchar *) gradient_name,
|
|
|
|
gsel->sample_size, &width,
|
|
|
|
&grad_data);
|
|
|
|
|
|
|
|
if (name)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
gsel->gradient_name = name;
|
|
|
|
|
|
|
|
gradient_preview_update (gsel->gradient_preview, width, grad_data);
|
|
|
|
|
|
|
|
g_free (grad_data);
|
1998-10-31 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
|
|
|
|
|
2001-07-31 12:10:14 +00:00
|
|
|
g_object_set_data (G_OBJECT (hbox), GSEL_DATA_KEY, gsel);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
|
|
|
return hbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-17 19:12:58 +00:00
|
|
|
/**
|
|
|
|
* gimp_gradient_select_widget_close_popup:
|
|
|
|
* @widget: A gradient select widget.
|
|
|
|
*
|
|
|
|
* Closes the popup window associated with @widget.
|
|
|
|
*/
|
2000-05-31 21:16:11 +00:00
|
|
|
void
|
2000-05-08 01:23:30 +00:00
|
|
|
gimp_gradient_select_widget_close_popup (GtkWidget *widget)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2000-05-08 01:23:30 +00:00
|
|
|
GSelect *gsel;
|
|
|
|
|
2001-07-31 12:10:14 +00:00
|
|
|
gsel = (GSelect*) g_object_get_data (G_OBJECT (widget), GSEL_DATA_KEY);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
if (gsel && gsel->gradient_popup_pnt)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2000-05-31 21:16:11 +00:00
|
|
|
gimp_gradients_close_popup (gsel->gradient_popup_pnt);
|
1998-10-31 16:22:37 +00:00
|
|
|
gsel->gradient_popup_pnt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-17 19:12:58 +00:00
|
|
|
/**
|
|
|
|
* gimp_gradient_select_widget_set_popup:
|
|
|
|
* @widget: A gradient select widget.
|
2003-06-17 09:43:14 +00:00
|
|
|
* @gradient_name: gradient name to set.
|
2002-12-17 19:12:58 +00:00
|
|
|
*
|
|
|
|
* Sets the current gradient for the gradient
|
|
|
|
* select widget. Calls the callback function if one was
|
|
|
|
* supplied in the call to gimp_gradient_select_widget().
|
|
|
|
*/
|
2000-05-31 21:16:11 +00:00
|
|
|
void
|
2003-06-17 09:43:14 +00:00
|
|
|
gimp_gradient_select_widget_set_popup (GtkWidget *widget,
|
|
|
|
const gchar *gradient_name)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2000-05-08 01:23:30 +00:00
|
|
|
gint width;
|
|
|
|
gdouble *grad_data;
|
2003-06-17 09:43:14 +00:00
|
|
|
gchar *name;
|
2000-05-08 01:23:30 +00:00
|
|
|
GSelect *gsel;
|
|
|
|
|
2001-07-31 12:10:14 +00:00
|
|
|
gsel = (GSelect*) g_object_get_data (G_OBJECT (widget), GSEL_DATA_KEY);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2000-05-08 01:23:30 +00:00
|
|
|
if (gsel)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
name = gimp_gradients_get_gradient_data ((gchar *) gradient_name,
|
|
|
|
gsel->sample_size, &width,
|
|
|
|
&grad_data);
|
1998-10-31 16:22:37 +00:00
|
|
|
|
2003-06-17 09:43:14 +00:00
|
|
|
if (name)
|
1998-10-31 16:22:37 +00:00
|
|
|
{
|
2003-06-17 09:43:14 +00:00
|
|
|
gradient_select_invoker (name, width, grad_data, FALSE, gsel);
|
2000-05-08 01:23:30 +00:00
|
|
|
|
2000-05-31 21:16:11 +00:00
|
|
|
if (gsel->gradient_popup_pnt)
|
2003-06-17 09:43:14 +00:00
|
|
|
gimp_gradients_set_popup (gsel->gradient_popup_pnt, name);
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
g_free (grad_data);
|
1998-10-31 16:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|