gimpbrushmenu.c 17.1 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4
 *
 * gimpbrushmenu.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
#include "gimp.h"
#include "gimpui.h"

35 36
#include "libgimp-intl.h"

37

38 39
#define BRUSH_SELECT_DATA_KEY "gimp-brush-select-data"
#define CELL_SIZE             20
40 41


42
typedef struct _BrushSelect BrushSelect;
43

44
struct _BrushSelect
45
{
46 47
  gchar                *title;
  GimpRunBrushCallback  callback;
48 49 50
  gpointer              data;

  GtkWidget            *preview;
51
  GtkWidget            *button;
52 53 54

  GtkWidget            *popup; 

55 56 57
  gchar                *brush_name;       /* Local copy */
  gdouble               opacity;
  gint                  spacing;
58
  GimpLayerModeEffects  paint_mode;
59 60
  gint                  width;
  gint                  height;
61
  guchar               *mask_data;        /* local copy */
62 63

  const gchar          *temp_brush_callback;
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
/*  local function prototypes  */

static void     gimp_brush_select_widget_callback (const gchar  *name,
                                                   gdouble       opacity,
                                                   gint          spacing,
                                                   GimpLayerModeEffects  paint_mode,
                                                   gint          width,
                                                   gint          height,
                                                   const guchar *mask_data,
                                                   gboolean      closing,
                                                   gpointer      data);
static void     gimp_brush_select_widget_clicked  (GtkWidget    *widget,
                                                   BrushSelect  *brush_sel);
static void     gimp_brush_select_widget_destroy  (GtkWidget    *widget,
                                                   BrushSelect  *brush_sel);
static gboolean gimp_brush_select_preview_events  (GtkWidget    *widget,
                                                   GdkEvent     *event,
                                                   BrushSelect  *brush_sel);
static void     gimp_brush_select_preview_update  (GtkWidget    *preview,
                                                   gint          brush_width,
                                                   gint          brush_height,
                                                   const gchar  *mask_data);
static void     gimp_brush_select_popup_open      (BrushSelect  *brush_sel,
                                                   gint          x,
                                                   gint          y);
static void     gimp_brush_select_popup_close     (BrushSelect  *brush_sel);
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
/**
 * gimp_brush_select_widget_new:
 * @title:      Title of the dialog to use or %NULL to use the default title.
 * @brush_name: Initial brush name or %NULL to use current selection. 
 * @opacity:    Initial opacity. -1 means to use current opacity.
 * @spacing:    Initial spacing. -1 means to use current spacing.
 * @paint_mode: Initial paint mode.  -1 means to use current paint mode.
 * @callback:   A function to call when the selected brush changes.
 * @data:       A pointer to arbitary data to be used in the call to @callback.
 *
 * Creates a new #GtkWidget that completely controls the selection of
 * a #GimpBrush. This widget is suitable for placement in a table in
 * a plug-in dialog.
 *
 * Returns: A #GtkWidget that you can use in your UI.
 */
GtkWidget * 
gimp_brush_select_widget_new (const gchar          *title,
                              const gchar          *brush_name, 
                              gdouble               opacity,
                              gint                  spacing,
                              GimpLayerModeEffects  paint_mode,
                              GimpRunBrushCallback  callback,
                              gpointer              data)
120
{
121 122 123 124 125 126 127
  BrushSelect          *brush_sel;
  GtkWidget            *frame;
  GtkWidget            *hbox;
  gint                  init_spacing;
  GimpLayerModeEffects  init_paint_mode;
  gdouble               init_opacity;
  gint                  mask_data_size;
128

129
  g_return_val_if_fail (callback != NULL, NULL);
130

131 132 133 134
  if (! title)
    title = _("Brush Selection");

  brush_sel = g_new0 (BrushSelect, 1);
135

136 137 138 139 140
  brush_sel->title    = g_strdup (title);
  brush_sel->callback = callback;
  brush_sel->data     = data;

  hbox = gtk_hbox_new (FALSE, 4);
141

142
  frame = gtk_frame_new (NULL);
143 144
  gtk_frame_set_shadow_type (GTK_FRAME(frame), GTK_SHADOW_OUT);
  gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
145
  gtk_widget_show (frame);
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
  brush_sel->preview = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
  gtk_widget_set_events (brush_sel->preview,
                         GDK_EXPOSURE_MASK       |
                         GDK_BUTTON_PRESS_MASK   |
                         GDK_BUTTON_RELEASE_MASK |
                         GDK_BUTTON1_MOTION_MASK);
  gtk_preview_size (GTK_PREVIEW (brush_sel->preview), CELL_SIZE, CELL_SIZE); 
  gtk_container_add (GTK_CONTAINER (frame), brush_sel->preview); 
  gtk_widget_show (brush_sel->preview);

  g_signal_connect (brush_sel->preview, "event",
                    G_CALLBACK (gimp_brush_select_preview_events),
                    brush_sel);

  brush_sel->button = gtk_button_new_with_label (" ... ");
  gtk_box_pack_end (GTK_BOX (hbox), brush_sel->button, FALSE, FALSE, 0);
  gtk_widget_show (brush_sel->button);

  g_signal_connect (brush_sel->button, "clicked",
                    G_CALLBACK (gimp_brush_select_widget_clicked),
                    brush_sel);
  g_signal_connect (brush_sel->button, "destroy",
                    G_CALLBACK (gimp_brush_select_widget_destroy),
                    brush_sel);
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  /* Do initial brush setup */
  brush_sel->brush_name = gimp_brushes_get_brush_data (brush_name,
                                                       &init_opacity,
                                                       &init_spacing,
                                                       &init_paint_mode,
                                                       &brush_sel->width,
                                                       &brush_sel->height,
                                                       &mask_data_size,
                                                       &brush_sel->mask_data);

  if (brush_sel->brush_name)
    {
      brush_sel->opacity    = (opacity == -1.0)  ? init_opacity    : opacity;
      brush_sel->spacing    = (spacing == -1)    ? init_spacing    : spacing;
      brush_sel->paint_mode = (paint_mode == -1) ? init_paint_mode : paint_mode;

      gimp_brush_select_preview_update (brush_sel->preview,
                                        brush_sel->width,
                                        brush_sel->height,
                                        brush_sel->mask_data);
    }
193

194
  g_object_set_data (G_OBJECT (hbox), BRUSH_SELECT_DATA_KEY, brush_sel);
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  return hbox;
}

/**
 * gimp_brush_select_widget_close:
 * @widget: A brush select widget.
 *
 * Closes the popup window associated with @widget.
 */
void
gimp_brush_select_widget_close (GtkWidget *widget)
{
  BrushSelect *brush_sel;

  brush_sel = g_object_get_data (G_OBJECT (widget), BRUSH_SELECT_DATA_KEY);
211

212 213 214
  g_return_if_fail (brush_sel != NULL);

  if (brush_sel->temp_brush_callback)
215
    {
216 217 218 219
      gimp_brush_select_destroy (brush_sel->temp_brush_callback);
      brush_sel->temp_brush_callback = NULL;
    }
}
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
/**
 * gimp_brush_select_widget_set;
 * @widget:     A brush select widget.
 * @brush_name: Brush name to set; %NULL means no change. 
 * @opacity:    Opacity to set. -1 means no change.
 * @spacing:    Spacing to set. -1 means no change.
 * @paint_mode: Paint mode to set.  -1 means no change.
 *
 * Sets the current brush and other values for the brush select
 * widget.  Calls the callback function if one was supplied in the
 * call to gimp_brush_select_widget_new().
 */
void
gimp_brush_select_widget_set (GtkWidget            *widget,
                              const gchar          *brush_name,
                              gdouble               opacity,
                              gint                  spacing,
                              GimpLayerModeEffects  paint_mode)
{
  BrushSelect *brush_sel;
  
  brush_sel = g_object_get_data (G_OBJECT (widget), BRUSH_SELECT_DATA_KEY);
243

244
  g_return_if_fail (brush_sel != NULL);
245

246 247 248 249
  if (brush_sel->temp_brush_callback)
    {
      gimp_brushes_set_popup (brush_sel->temp_brush_callback, 
                              brush_name, opacity, spacing, paint_mode);
250
    }
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  else
    {
      gint                  width;
      gint                  height;
      gint                  init_spacing;
      GimpLayerModeEffects  init_paint_mode;
      gdouble               init_opacity;
      gint                  mask_data_size;
      guint8               *mask_data;
      gchar                *name;

      name = gimp_brushes_get_brush_data (brush_name,
					  &init_opacity,
					  &init_spacing,
					  &init_paint_mode,
					  &width,
					  &height,
					  &mask_data_size,
					  &mask_data);
270

271 272 273 274 275 276 277 278 279
      if (name)
        {
          if (opacity    == -1.0) opacity    = init_opacity;
          if (spacing    == -1)   spacing    = init_spacing;
          if (paint_mode == -1)   paint_mode = init_paint_mode;
  
          gimp_brush_select_widget_callback (brush_name, opacity, spacing,
                                             paint_mode, width, height,
                                             mask_data, FALSE, brush_sel);
280

281 282 283 284
          g_free (name);
          g_free (mask_data);
        }
    }
285 286
}

287 288 289

/*  private functions  */

290
static void
291 292 293 294 295 296 297 298 299
gimp_brush_select_widget_callback (const gchar          *name,
                                   gdouble               opacity,
                                   gint                  spacing,
                                   GimpLayerModeEffects  paint_mode,
                                   gint                  width,
                                   gint                  height,
                                   const guchar         *mask_data,
                                   gboolean              closing,
                                   gpointer              data)
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
  BrushSelect *brush_sel = (BrushSelect *) data;

  g_free (brush_sel->brush_name);
  g_free (brush_sel->mask_data);

  brush_sel->brush_name = g_strdup (name);
  brush_sel->width      = width;
  brush_sel->height     = height;
  brush_sel->mask_data  = g_memdup (mask_data, width * height);
  brush_sel->opacity    = opacity;
  brush_sel->spacing    = spacing;
  brush_sel->paint_mode = paint_mode;

  gimp_brush_select_preview_update (brush_sel->preview,
                                    width, height, mask_data);

  if (brush_sel->callback)
    brush_sel->callback (name, opacity, spacing, paint_mode,
                         width, height, mask_data, closing, brush_sel->data);

  if (closing)
    brush_sel->temp_brush_callback = NULL;
}

static void
gimp_brush_select_widget_clicked (GtkWidget   *widget,
                                  BrushSelect *brush_sel)
{
  if (brush_sel->temp_brush_callback)
    {
      /*  calling gimp_brushes_set_popup() raises the dialog  */
      gimp_brushes_set_popup (brush_sel->temp_brush_callback, 
			      brush_sel->brush_name,
			      brush_sel->opacity,
			      brush_sel->spacing,
			      brush_sel->paint_mode);
    }
  else
339
    {
340 341 342 343 344 345 346 347
      brush_sel->temp_brush_callback =
        gimp_brush_select_new (brush_sel->title,
                               brush_sel->brush_name,
                               brush_sel->opacity,
                               brush_sel->spacing,
                               brush_sel->paint_mode,
                               gimp_brush_select_widget_callback,
                               brush_sel);
348
    }
349 350
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
static void
gimp_brush_select_widget_destroy (GtkWidget   *widget,
                                  BrushSelect *brush_sel)
{
  if (brush_sel->temp_brush_callback)
    {
      gimp_brush_select_destroy (brush_sel->temp_brush_callback);
      brush_sel->temp_brush_callback = NULL;
    }

  g_free (brush_sel->title);
  g_free (brush_sel->brush_name);
  g_free (brush_sel->mask_data);
  g_free (brush_sel);
}


static gboolean
gimp_brush_select_preview_events (GtkWidget   *widget,
                                  GdkEvent    *event,
                                  BrushSelect *brush_sel)
372 373 374
{
  GdkEventButton *bevent;

375
  if (brush_sel->mask_data)
376 377 378 379 380 381 382 383
    {
      switch (event->type)
	{
	case GDK_BUTTON_PRESS:
	  bevent = (GdkEventButton *) event;
	  
	  if (bevent->button == 1)
	    {
384
	      gtk_grab_add (widget);
385
	      gimp_brush_select_popup_open (brush_sel, bevent->x, bevent->y);
386 387 388 389 390 391 392 393
	    }
	  break;
	  
	case GDK_BUTTON_RELEASE:
	  bevent = (GdkEventButton *) event;
	  
	  if (bevent->button == 1)
	    {
394
	      gtk_grab_remove (widget);
395
	      gimp_brush_select_popup_close (brush_sel);
396 397 398 399 400 401 402 403 404 405 406 407
	    }
	  break;
	  
	default:
	  break;
	}
    }

  return FALSE;
}

static void
408 409 410 411
gimp_brush_select_preview_update (GtkWidget   *preview,
                                  gint         brush_width,
                                  gint         brush_height,
                                  const gchar *mask_data)
412
{
413 414 415 416 417 418 419 420
  guchar       *buf;
  guchar       *b;
  const guchar *src;
  const guchar *s;
  gint          i, y;
  gint          offset_x, offset_y;
  gint          ystart, yend;
  gint          width, height;
421

422
  /*  Draw the brush  */
423
  buf = g_new (guchar, CELL_SIZE); 
424 425

  /* Set buffer to white */  
426
  memset (buf, 255, CELL_SIZE);
427
  for (i = 0; i < CELL_SIZE; i++)
428
    gtk_preview_draw_row (GTK_PREVIEW (preview),
429 430 431 432 433 434 435 436
			  buf, 0, i, CELL_SIZE);

 /* Limit to cell size */
  width  = (brush_width  > CELL_SIZE) ? CELL_SIZE: brush_width;
  height = (brush_height > CELL_SIZE) ? CELL_SIZE: brush_height;

  offset_x = ((CELL_SIZE - width)  / 2);
  offset_y = ((CELL_SIZE - height) / 2);
437 438

  ystart = CLAMP (offset_y, 0, CELL_SIZE);
439
  yend   = CLAMP (offset_y + height, 0, CELL_SIZE);
440 441 442 443 444

  src = mask_data;

  for (y = ystart; y < yend; y++)
    {
445 446
      gint j;

447 448 449 450 451
      s = src;
      b = buf;
      for (j = 0; j < width ; j++)
	*b++ = 255 - *s++;

452
      gtk_preview_draw_row (GTK_PREVIEW (preview),
453
			    buf, offset_x, y, width);
454 455
      src += brush_width;
    }
456

457
  g_free (buf);
458

459
  gtk_widget_queue_draw (preview);
460 461 462
}

static void
463 464 465
gimp_brush_select_popup_open (BrushSelect  *brush_sel,
                              gint          x,
                              gint          y)
466
{
467 468 469 470 471 472 473 474 475 476
  GtkWidget    *frame;
  GtkWidget    *preview;
  const guchar *src;
  const guchar *s;
  guchar       *buf;
  guchar       *b;
  gint          x_org;
  gint          y_org;
  gint          scr_w;
  gint          scr_h;
477

478 479
  if (brush_sel->popup)
    gimp_brush_select_popup_close (brush_sel);
480

481 482
  if (brush_sel->width <= CELL_SIZE && brush_sel->height <= CELL_SIZE)
    return;
483

484
  brush_sel->popup = gtk_window_new (GTK_WINDOW_POPUP);
485 486

  frame = gtk_frame_new (NULL);
487 488
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
  gtk_container_add (GTK_CONTAINER (brush_sel->popup), frame);
489
  gtk_widget_show (frame);
490

491 492 493 494 495
  preview = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
  gtk_preview_size (GTK_PREVIEW (preview),
                    brush_sel->width, brush_sel->height);
  gtk_container_add (GTK_CONTAINER (frame), preview);
  gtk_widget_show (preview);
496

497 498 499 500
  /* decide where to put the popup */
  gdk_window_get_origin (brush_sel->preview->window, &x_org, &y_org);
  scr_w = gdk_screen_width ();
  scr_h = gdk_screen_height ();
501

502 503 504 505 506 507
  x = x_org + x - (brush_sel->width  / 2);
  y = y_org + y - (brush_sel->height / 2);
  x = (x < 0) ? 0 : x;
  y = (y < 0) ? 0 : y;
  x = (x + brush_sel->width  > scr_w) ? scr_w - brush_sel->width  : x;
  y = (y + brush_sel->height > scr_h) ? scr_h - brush_sel->height : y;
508

509 510 511 512 513 514 515
  gtk_window_move (GTK_WINDOW (brush_sel->popup), x, y);
  
  /*  Draw the brush  */
  buf = g_new (guchar, brush_sel->width);
  memset (buf, 255, brush_sel->width);
  
  src = brush_sel->mask_data;
516

517
  for (y = 0; y < brush_sel->height; y++)
518
    {
519
      int j;
520

521 522
      s = src;
      b = buf;
523

524 525
      for (j = 0; j < brush_sel->width ; j++)
	*b++ = 255 - *s++;
526

527 528 529 530
      gtk_preview_draw_row (GTK_PREVIEW (preview), buf,
			    0, y, brush_sel->width);
      src += brush_sel->width;
    }
531

532
  g_free (buf);
533

534
  gtk_widget_show (brush_sel->popup);
535 536
}

537 538
static void
gimp_brush_select_popup_close (BrushSelect *brush_sel)
539
{
540
  if (brush_sel->popup)
541
    {
542 543
      gtk_widget_destroy (brush_sel->popup);
      brush_sel->popup = NULL;
544 545
    }
}