gimpgradientmenu.c 8.55 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4
 *
 * gimpgradientmenu.c
5 6 7
 * Copyright (C) 1998 Andy Thomas                
 *
 * This library is free software; you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 11 12 13 14
 * 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
15 16
 * Library General Public License for more details.
 *
Marc Lehmann's avatar
Marc Lehmann committed
17
 * You should have received a copy of the GNU Lesser General Public
18 19 20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
21 22
 */

Sven Neumann's avatar
Sven Neumann committed
23 24
#include "config.h"

25 26
#include <string.h>

27 28 29 30 31
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

32 33 34 35 36 37 38 39
#include "gimp.h"
#include "gimpui.h"


/* 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.
40
 *      pointer to func to call when gradient changes (GimpRunGradientCallback).
41 42 43 44 45 46 47
 * 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.
 */


48 49 50
#define GSEL_DATA_KEY     "__gsel_data"
#define CELL_SIZE_HEIGHT  18
#define CELL_SIZE_WIDTH   84
51 52


53 54
struct __gradients_sel 
{
55 56 57 58
  gchar                   *dname;
  GimpRunGradientCallback  cback;
  GtkWidget               *gradient_preview;
  GtkWidget               *button;
59
  gchar                   *gradient_popup_pnt;
60 61 62 63 64 65
  gint                     width;
  gchar                   *gradient_name;      /* Local copy */
  gdouble                 *grad_data;          /* local copy */
  gint                     sample_size;
  gpointer                 data;
}; 
66

67
typedef struct __gradients_sel GSelect;
68 69

static void
70 71 72
gradient_pre_update (GtkWidget *gradient_preview,
		     gint       width_data,
		     gdouble   *grad_data)
73
{
74 75
  gint     x;
  gint     y;
76
  gdouble *src;
77 78 79 80 81 82 83 84 85
  gdouble  r, g, b, a;
  gdouble  c0, c1;
  guchar  *p0;
  guchar  *p1;
  guchar  *even;
  guchar  *odd;
  gint     width;

  width = width_data / 4;
86 87 88

  /*  Draw the gradient  */
  src = grad_data;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  p0  = even = g_malloc (width * 3);
  p1  = odd  = g_malloc (width * 3);

  for (x = 0; x < width; x++) 
    {
      r =  src[x*4+0];
      g = src[x*4+1];
      b = src[x*4+2];
      a = src[x*4+3];
      
      if ((x / GIMP_CHECK_SIZE_SM) & 1) 
	{
	  c0 = GIMP_CHECK_LIGHT;
	  c1 = GIMP_CHECK_DARK;
	} 
      else 
	{
	  c0 = GIMP_CHECK_DARK;
	  c1 = GIMP_CHECK_LIGHT;
	}
109
    
110 111 112 113 114 115 116 117
      *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;
    }
118 119 120
  
  for (y = 0; y < CELL_SIZE_HEIGHT; y++)
    {
121 122 123 124
      if ((y / GIMP_CHECK_SIZE_SM) & 1)
	gtk_preview_draw_row (GTK_PREVIEW (gradient_preview), 
			      (guchar *)odd, 0, y, 
			      (width < CELL_SIZE_WIDTH) ? width : CELL_SIZE_WIDTH);
125
      else
126 127 128
	  gtk_preview_draw_row (GTK_PREVIEW (gradient_preview), 
				(guchar *)even, 0, y, 
				(width < CELL_SIZE_WIDTH) ? width : CELL_SIZE_WIDTH);
129 130
    }

131 132
  g_free (even);
  g_free (odd);
133

134
  gtk_widget_queue_draw (gradient_preview);
135 136 137
}

static void
138 139 140 141 142
gradient_select_invoker (gchar    *name,
			 gint      width,
			 gdouble  *grad_data,
			 gint      closing,
			 gpointer  data)
143
{
144 145 146 147 148
  GSelect *gsel = (GSelect*)data;

  if (gsel->grad_data != NULL)
    g_free (gsel->grad_data);

149
  gsel->width = width;
150 151 152 153
  if (gsel->gradient_name)
    g_free (gsel->gradient_name);

  gsel->gradient_name = g_strdup (name);
154
  /* one row only each row has four doubles r,g,b,a */
155
  gsel->grad_data = g_new (gdouble, width);
156
  /*  printf("name = %s width = %d\n",name,width);*/
157 158
  g_memmove (gsel->grad_data, grad_data, width * sizeof (gdouble)); 
  gradient_pre_update (gsel->gradient_preview, gsel->width, gsel->grad_data);
159

160 161 162 163 164
  if (gsel->cback != NULL)
    (gsel->cback)(name, width, grad_data, closing, gsel->data);
  
  if (closing)
    gtk_widget_set_sensitive (gsel->button, TRUE);
165 166 167 168
}


static void
169 170
gradient_preview_callback (GtkWidget *widget,
			   gpointer   data)
171
{
172 173 174 175
  GSelect *gsel = (GSelect*)data;

  gtk_widget_set_sensitive (gsel->button, FALSE);

176
  gsel->gradient_popup_pnt = 
177 178 179 180 181 182
    gimp_interactive_selection_gradient ((gsel->dname) ? gsel->dname 
					               : "Gradient Plugin Selection",
					 gsel->gradient_name,
					 gsel->sample_size,
					 gradient_select_invoker,
					 gsel);
183 184
}

185 186
/**
 * gimp_gradient_select_widget:
187
 * @gname: Title of the dialog to use.  NULL means to use the default title.
188 189 190 191 192 193 194 195 196 197
 * @igradient: Initial gradient name. NULL means to use current selection. 
 * @cback: a function to call when the selected gradient changes.
 * @data: a pointer to arbitary data to be used in the call to @cback.
 *
 * 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.
 */
198
GtkWidget * 
199
gimp_gradient_select_widget (gchar                   *gname,
200 201 202
			     gchar                   *igradient, 
			     GimpRunGradientCallback  cback,
			     gpointer                 data)
203
{
204 205 206 207 208 209 210 211 212
  GtkWidget *button;
  GtkWidget *hbox;
  GtkWidget *gradient;
  gint       width;
  gdouble   *grad_data;
  gchar     *gradient_name;
  GSelect   *gsel;
  
  gsel = g_new (GSelect, 1);
213 214 215 216

  hbox = gtk_hbox_new (FALSE, 3);
  gtk_widget_show(hbox);

217
  button = gtk_button_new ();
218 219

  gradient = gtk_preview_new (GTK_PREVIEW_COLOR);
220 221
  gtk_preview_size (GTK_PREVIEW (gradient), 
                    CELL_SIZE_WIDTH, CELL_SIZE_HEIGHT); 
222
  gtk_widget_show (gradient);
223 224
  gtk_container_add (GTK_CONTAINER (button), gradient); 

225
  g_signal_connect (button, "clicked",
226 227
                    G_CALLBACK (gradient_preview_callback),
                    gsel);
228
  
229 230
  gtk_widget_show(button);

231 232 233 234 235
  gsel->button             = button;
  gsel->cback              = cback;
  gsel->data               = data;
  gsel->grad_data          = NULL;
  gsel->gradient_preview   = gradient;
236
  gsel->dname              = gname;
237
  gsel->gradient_popup_pnt = NULL;
238
  gsel->sample_size        = CELL_SIZE_WIDTH;
239 240

  /* Do initial gradient setup */
241
  gradient_name = 
242
    gimp_gradients_get_gradient_data (igradient, 
243
                                      CELL_SIZE_WIDTH, &width,
244
                                      &grad_data);
245 246

  if (gradient_name)
247
    {
248 249
      gradient_pre_update (gsel->gradient_preview, width, grad_data);
      gsel->grad_data     = grad_data;
250
      gsel->gradient_name = gradient_name;
251
      gsel->width         = width;
252 253 254
    }
  else
    {
255
      gsel->gradient_name = g_strdup (igradient);
256 257 258 259
    }

  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

260
  g_object_set_data (G_OBJECT (hbox), GSEL_DATA_KEY, gsel);
261 262 263 264 265

  return hbox;
}


266 267 268 269 270 271
/**
 * gimp_gradient_select_widget_close_popup:
 * @widget: A gradient select widget.
 *
 * Closes the popup window associated with @widget.
 */
272
void
273
gimp_gradient_select_widget_close_popup (GtkWidget *widget)
274
{
275 276
  GSelect  *gsel;
  
277
  gsel = (GSelect*) g_object_get_data (G_OBJECT (widget), GSEL_DATA_KEY);
278

279
  if (gsel && gsel->gradient_popup_pnt)
280
    {
281
      gimp_gradients_close_popup (gsel->gradient_popup_pnt);
282 283 284 285
      gsel->gradient_popup_pnt = NULL;
    }
}

286 287 288 289 290 291 292 293 294
/**
 * gimp_gradient_select_widget_set_popup:
 * @widget: A gradient select widget.
 * @gname: gradient name to set.
 *
 * 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().
 */
295
void
296 297
gimp_gradient_select_widget_set_popup (GtkWidget *widget,
				       gchar     *gname)
298
{
299 300 301 302 303
  gint      width;
  gdouble  *grad_data;
  gchar    *gradient_name;
  GSelect  *gsel;
  
304
  gsel = (GSelect*) g_object_get_data (G_OBJECT (widget), GSEL_DATA_KEY);
305

306
  if (gsel)
307
    {
308
      gradient_name = 
309
	gimp_gradients_get_gradient_data (gname, 
310
                                          gsel->sample_size, &width,
311
                                          &grad_data);
312
  
313
      if (gradient_name)
314
	{
315 316
	  gradient_select_invoker (gname, width, grad_data, 0, gsel);

317 318
	  if (gsel->gradient_popup_pnt)
	    gimp_gradients_set_popup (gsel->gradient_popup_pnt, gname);
319 320 321
	}
    }
}