gimpbrushmenu.c 17.2 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4
 *
 * gimpbrushmenu.c
5
 * Copyright (C) 1998 Andy Thomas
6 7
 *
 * 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
  GtkWidget            *popup;
54

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
/*  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,
88
                                                   const guchar *mask_data);
89 90 91 92
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
/**
 * gimp_brush_select_widget_new:
 * @title:      Title of the dialog to use or %NULL to use the default title.
99
 * @brush_name: Initial brush name or %NULL to use current selection.
100 101 102 103 104 105 106 107 108 109 110 111
 * @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.
 */
112
GtkWidget *
113
gimp_brush_select_widget_new (const gchar          *title,
114
                              const gchar          *brush_name,
115 116 117 118 119
                              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
  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);
153 154
  gtk_preview_size (GTK_PREVIEW (brush_sel->preview), CELL_SIZE, CELL_SIZE);
  gtk_container_add (GTK_CONTAINER (frame), brush_sel->preview);
155 156 157 158 159 160
  gtk_widget_show (brush_sel->preview);

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

161
  brush_sel->button = gtk_button_new_with_mnemonic (_("_Browse..."));
162 163 164 165 166 167 168 169 170
  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
/**
 * gimp_brush_select_widget_set;
 * @widget:     A brush select widget.
224
 * @brush_name: Brush name to set; %NULL means no change.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
 * @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;
241

242
  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
  if (brush_sel->temp_brush_callback)
    {
248
      gimp_brushes_set_popup (brush_sel->temp_brush_callback,
249
                              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
      if (name)
        {
          if (opacity    == -1.0) opacity    = init_opacity;
          if (spacing    == -1)   spacing    = init_spacing;
          if (paint_mode == -1)   paint_mode = init_paint_mode;
276

277 278 279
          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
  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  */
332
      gimp_brushes_set_popup (brush_sel->temp_brush_callback,
333 334 335 336 337 338
			      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
    {
      switch (event->type)
	{
	case GDK_BUTTON_PRESS:
	  bevent = (GdkEventButton *) event;
381

382 383
	  if (bevent->button == 1)
	    {
384
	      gtk_grab_add (widget);
385
	      gimp_brush_select_popup_open (brush_sel, bevent->x, bevent->y);
386 387
	    }
	  break;
388

389 390
	case GDK_BUTTON_RELEASE:
	  bevent = (GdkEventButton *) event;
391

392 393
	  if (bevent->button == 1)
	    {
394
	      gtk_grab_remove (widget);
395
	      gimp_brush_select_popup_close (brush_sel);
396 397
	    }
	  break;
398

399 400 401 402 403 404 405 406 407
	default:
	  break;
	}
    }

  return FALSE;
}

static void
408 409 410 411
gimp_brush_select_preview_update (GtkWidget    *preview,
                                  gint          brush_width,
                                  gint          brush_height,
                                  const guchar *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
  GtkWidget    *frame;
  GtkWidget    *preview;
  const guchar *src;
  const guchar *s;
  guchar       *buf;
  guchar       *b;
  gint          x_org;
  gint          y_org;
475
  GdkScreen    *screen;
476 477
  gint          scr_w;
  gint          scr_h;
478

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

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

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

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

492 493 494 495 496
  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);
497

498 499
  /* decide where to put the popup */
  gdk_window_get_origin (brush_sel->preview->window, &x_org, &y_org);
500 501 502 503

  screen = gtk_widget_get_screen (brush_sel->popup);
  scr_w = gdk_screen_get_width (screen);
  scr_h = gdk_screen_get_height (screen);
504

505 506 507 508 509 510
  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;
511

512
  gtk_window_move (GTK_WINDOW (brush_sel->popup), x, y);
513

514 515 516
  /*  Draw the brush  */
  buf = g_new (guchar, brush_sel->width);
  memset (buf, 255, brush_sel->width);
517

518
  src = brush_sel->mask_data;
519

520
  for (y = 0; y < brush_sel->height; y++)
521
    {
522
      int j;
523

524 525
      s = src;
      b = buf;
526

527 528
      for (j = 0; j < brush_sel->width ; j++)
	*b++ = 255 - *s++;
529

530 531 532 533
      gtk_preview_draw_row (GTK_PREVIEW (preview), buf,
			    0, y, brush_sel->width);
      src += brush_sel->width;
    }
534

535
  g_free (buf);
536

537
  gtk_widget_show (brush_sel->popup);
538 539
}

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