gimpbrushselectbutton.c 30.8 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpbrushselectbutton.c
 * Copyright (C) 1998 Andy Thomas
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24
#include <gegl.h>
25 26 27 28 29 30 31 32 33 34 35 36 37
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"

#include "gimpuitypes.h"
#include "gimpbrushselectbutton.h"
#include "gimpuimarshal.h"

#include "libgimp-intl.h"


38 39 40 41 42 43 44 45 46
/**
 * SECTION: gimpbrushselectbutton
 * @title: gimpbrushselectbutton
 * @short_description: A button that pops up a brush selection dialog.
 *
 * A button that pops up a brush selection dialog.
 **/


47 48 49 50 51 52 53 54 55
#define CELL_SIZE 20


#define GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIMP_TYPE_BRUSH_SELECT_BUTTON, GimpBrushSelectButtonPrivate))

typedef struct _GimpBrushSelectButtonPrivate GimpBrushSelectButtonPrivate;

struct _GimpBrushSelectButtonPrivate
{
56 57 58 59 60 61 62 63 64 65 66 67 68
  gchar         *title;

  gchar         *brush_name;      /* Local copy */
  gdouble        opacity;
  gint           spacing;
  GimpLayerMode  paint_mode;
  gint           width;
  gint           height;
  guchar        *mask_data;       /* local copy */

  GtkWidget     *inside;
  GtkWidget     *preview;
  GtkWidget     *popup;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
};

enum
{
  BRUSH_SET,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_TITLE,
  PROP_BRUSH_NAME,
  PROP_BRUSH_OPACITY,
  PROP_BRUSH_SPACING,
  PROP_BRUSH_PAINT_MODE
};


/*  local function prototypes  */

static void   gimp_brush_select_button_finalize     (GObject      *object);

static void   gimp_brush_select_button_set_property (GObject      *object,
                                                     guint         property_id,
                                                     const GValue *value,
                                                     GParamSpec   *pspec);
static void   gimp_brush_select_button_get_property (GObject      *object,
                                                     guint         property_id,
                                                     GValue       *value,
                                                     GParamSpec   *pspec);

101
static void   gimp_brush_select_button_clicked  (GimpBrushSelectButton *button);
102 103 104 105

static void   gimp_brush_select_button_callback (const gchar          *brush_name,
                                                 gdouble               opacity,
                                                 gint                  spacing,
106
                                                 GimpLayerMode         paint_mode,
107 108 109 110 111 112
                                                 gint                  width,
                                                 gint                  height,
                                                 const guchar         *mask_data,
                                                 gboolean              dialog_closing,
                                                 gpointer              user_data);

113
static void     gimp_brush_select_preview_resize  (GimpBrushSelectButton *button);
114 115
static gboolean gimp_brush_select_preview_events  (GtkWidget             *widget,
                                                   GdkEvent              *event,
116
                                                   GimpBrushSelectButton *button);
117 118 119 120 121 122 123 124 125 126 127 128
static void     gimp_brush_select_preview_draw    (GimpPreviewArea       *area,
                                                   gint                   x,
                                                   gint                   y,
                                                   gint                   width,
                                                   gint                   height,
                                                   const guchar          *mask_data,
                                                   gint                   rowstride);
static void     gimp_brush_select_preview_update  (GtkWidget             *preview,
                                                   gint                   brush_width,
                                                   gint                   brush_height,
                                                   const guchar          *mask_data);

129
static void     gimp_brush_select_button_open_popup  (GimpBrushSelectButton *button,
130 131
                                                      gint                   x,
                                                      gint                   y);
132
static void     gimp_brush_select_button_close_popup (GimpBrushSelectButton *button);
133

134
static void   gimp_brush_select_drag_data_received (GimpBrushSelectButton *button,
135 136 137 138 139 140 141
                                                    GdkDragContext        *context,
                                                    gint                   x,
                                                    gint                   y,
                                                    GtkSelectionData      *selection,
                                                    guint                  info,
                                                    guint                  time);

142
static GtkWidget * gimp_brush_select_button_create_inside (GimpBrushSelectButton *button);
143 144 145 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 171 172


static const GtkTargetEntry target = { "application/x-gimp-brush-name", 0 };

static guint brush_button_signals[LAST_SIGNAL] = { 0 };


G_DEFINE_TYPE (GimpBrushSelectButton, gimp_brush_select_button,
               GIMP_TYPE_SELECT_BUTTON)


static void
gimp_brush_select_button_class_init (GimpBrushSelectButtonClass *klass)
{
  GObjectClass          *object_class        = G_OBJECT_CLASS (klass);
  GimpSelectButtonClass *select_button_class = GIMP_SELECT_BUTTON_CLASS (klass);

  object_class->finalize     = gimp_brush_select_button_finalize;
  object_class->set_property = gimp_brush_select_button_set_property;
  object_class->get_property = gimp_brush_select_button_get_property;

  select_button_class->select_destroy = gimp_brush_select_destroy;

  klass->brush_set = NULL;

  /**
   * GimpBrushSelectButton:title:
   *
   * The title to be used for the brush selection popup dialog.
   *
173
   * Since: 2.4
174 175 176 177 178 179 180 181 182 183 184 185 186 187
   */
  g_object_class_install_property (object_class, PROP_TITLE,
                                   g_param_spec_string ("title",
                                                        "Title",
                                                        "The title to be used for the brush selection popup dialog",
                                                        _("Brush Selection"),
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  /**
   * GimpBrushSelectButton:brush-name:
   *
   * The name of the currently selected brush.
   *
188
   * Since: 2.4
189 190 191 192 193 194 195 196 197 198 199 200 201
   */
  g_object_class_install_property (object_class, PROP_BRUSH_NAME,
                                   g_param_spec_string ("brush-name",
                                                        "Brush name",
                                                        "The name of the currently selected brush",
                                                        NULL,
                                                        GIMP_PARAM_READWRITE));

  /**
   * GimpBrushSelectButton:opacity:
   *
   * The opacity of the currently selected brush.
   *
202
   * Since: 2.4
203 204 205 206 207 208 209 210 211 212 213 214 215
   */
  g_object_class_install_property (object_class, PROP_BRUSH_OPACITY,
                                   g_param_spec_double ("brush-opacity",
                                                        "Brush opacity",
                                                        "The opacity of the currently selected brush",
                                                        -1.0, 100.0, -1.0,
                                                        GIMP_PARAM_READWRITE));

  /**
   * GimpBrushSelectButton:spacing:
   *
   * The spacing of the currently selected brush.
   *
216
   * Since: 2.4
217 218 219 220 221 222 223 224 225 226 227
   */
  g_object_class_install_property (object_class, PROP_BRUSH_SPACING,
                                   g_param_spec_int ("brush-spacing",
                                                     "Brush spacing",
                                                     "The spacing of the currently selected brush",
                                                     -G_MAXINT, 1000, -1,
                                                     GIMP_PARAM_READWRITE));

  /**
   * GimpBrushSelectButton:paint-mode:
   *
228
   * The paint mode.
229
   *
230
   * Since: 2.4
231 232 233 234 235
   */
  g_object_class_install_property (object_class, PROP_BRUSH_PAINT_MODE,
                                   g_param_spec_int ("brush-paint-mode",
                                                     "Brush paint mode",
                                                     "The paint mode of the currently selected brush",
236
                                                     -1, GIMP_LAYER_MODE_LUMINANCE,
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
                                                     -1,
                                                     GIMP_PARAM_READWRITE));

  /**
   * GimpBrushSelectButton::brush-set:
   * @widget: the object which received the signal.
   * @brush_name: the name of the currently selected brush.
   * @opacity: opacity of the brush
   * @spacing: spacing of the brush
   * @paint_mode: paint mode of the brush
   * @width: width of the brush
   * @height: height of the brush
   * @mask_data: brush mask data
   * @dialog_closing: whether the dialog was closed or not.
   *
   * The ::brush-set signal is emitted when the user selects a brush.
   *
254
   * Since: 2.4
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
   */
  brush_button_signals[BRUSH_SET] =
    g_signal_new ("brush-set",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpBrushSelectButtonClass, brush_set),
                  NULL, NULL,
                  _gimpui_marshal_VOID__STRING_DOUBLE_INT_INT_INT_INT_POINTER_BOOLEAN,
                  G_TYPE_NONE, 8,
                  G_TYPE_STRING,
                  G_TYPE_DOUBLE,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_POINTER,
                  G_TYPE_BOOLEAN);

  g_type_class_add_private (object_class,
                            sizeof (GimpBrushSelectButtonPrivate));
}

static void
278
gimp_brush_select_button_init (GimpBrushSelectButton *button)
279 280 281 282 283 284 285
{
  GimpBrushSelectButtonPrivate *priv;
  gint                          mask_bpp;
  gint                          mask_data_size;
  gint                          color_bpp;
  gint                          color_data_size;
  guint8                       *color_data;
286 287

  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
288 289 290 291 292 293 294 295 296 297 298 299

  priv->brush_name = gimp_context_get_brush ();
  gimp_brush_get_pixels (priv->brush_name,
                         &priv->width,
                         &priv->height,
                         &mask_bpp,
                         &mask_data_size,
                         &priv->mask_data,
                         &color_bpp,
                         &color_data_size,
                         &color_data);

300 301 302
  if (color_data)
    g_free (color_data);

303 304
  priv->inside = gimp_brush_select_button_create_inside (button);
  gtk_container_add (GTK_CONTAINER (button), priv->inside);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

  priv->popup = NULL;
}

/**
 * gimp_brush_select_button_new:
 * @title:      Title of the dialog to use or %NULL means 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.
 *
 * 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.
 *
324
 * Since: 2.4
325 326
 */
GtkWidget *
327 328 329 330 331
gimp_brush_select_button_new (const gchar   *title,
                              const gchar   *brush_name,
                              gdouble        opacity,
                              gint           spacing,
                              GimpLayerMode  paint_mode)
332
{
333
  GtkWidget *button;
334 335

  if (title)
336 337 338 339 340 341 342
    button = g_object_new (GIMP_TYPE_BRUSH_SELECT_BUTTON,
                           "title",            title,
                           "brush-name",       brush_name,
                           "brush-opacity",    opacity,
                           "brush-spacing",    spacing,
                           "brush-paint-mode", paint_mode,
                           NULL);
343
  else
344 345 346 347 348 349 350 351
    button = g_object_new (GIMP_TYPE_BRUSH_SELECT_BUTTON,
                           "brush-name",       brush_name,
                           "brush-opacity",    opacity,
                           "brush-spacing",    spacing,
                           "brush-paint-mode", paint_mode,
                           NULL);

  return button;
352 353 354 355 356 357 358 359 360 361 362 363 364
}

/**
 * gimp_brush_select_button_get_brush:
 * @button: A #GimpBrushSelectButton
 * @opacity: Opacity of the selected brush.
 * @spacing: Spacing of the selected brush.
 * @paint_mode: Paint mode of the selected brush.
 *
 * Retrieves the properties of currently selected brush.
 *
 * Returns: an internal copy of the brush name which must not be freed.
 *
365
 * Since: 2.4
366
 */
367
const gchar *
368
gimp_brush_select_button_get_brush (GimpBrushSelectButton *button,
369 370
                                    gdouble               *opacity,
                                    gint                  *spacing,
371
                                    GimpLayerMode         *paint_mode)
372 373 374
{
  GimpBrushSelectButtonPrivate *priv;

375 376 377
  g_return_val_if_fail (GIMP_IS_BRUSH_SELECT_BUTTON (button), NULL);

  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

  if (opacity)
    *opacity = priv->opacity;

  if (spacing)
    *spacing = priv->spacing;

  if (paint_mode)
    *paint_mode = priv->paint_mode;

  return priv->brush_name;
}

/**
 * gimp_brush_select_button_set_brush:
393
 * @button: A #GimpBrushSelectButton
394 395 396 397 398 399 400 401
 * @brush_name: Brush name to set; %NULL means no change.
 * @opacity:    Opacity to set. -1.0 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
 * button.
 *
402
 * Since: 2.4
403 404
 */
void
405
gimp_brush_select_button_set_brush (GimpBrushSelectButton *button,
406 407
                                    const gchar           *brush_name,
                                    gdouble                opacity,
408
                                    gint                   spacing,
409
                                    GimpLayerMode          paint_mode)
410 411 412
{
  GimpSelectButton *select_button;

413
  g_return_if_fail (GIMP_IS_BRUSH_SELECT_BUTTON (button));
414

415
  select_button = GIMP_SELECT_BUTTON (button);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

  if (select_button->temp_callback)
    {
      gimp_brushes_set_popup (select_button->temp_callback, brush_name,
                              opacity, spacing, paint_mode);
    }
  else
    {
      gchar  *name;
      gint    width;
      gint    height;
      gint    bytes;
      gint    mask_data_size;
      guint8 *mask_data;
      gint    color_bpp;
      gint    color_data_size;
      guint8 *color_data;

      if (brush_name && *brush_name)
        name = g_strdup (brush_name);
      else
        name = gimp_context_get_brush ();

      if (gimp_brush_get_pixels (name,
                                 &width,
                                 &height,
                                 &bytes,
                                 &mask_data_size,
                                 &mask_data,
                                 &color_bpp,
                                 &color_data_size,
                                 &color_data))
        {
          if (color_data)
            g_free (color_data);

          if (opacity < 0.0)
            opacity = gimp_context_get_opacity ();

          if (spacing == -1)
            gimp_brush_get_spacing (name, &spacing);

          if (paint_mode == -1)
            paint_mode = gimp_context_get_paint_mode ();

          gimp_brush_select_button_callback (name,
                                             opacity, spacing, paint_mode,
                                             width, height, mask_data,
464
                                             FALSE, button);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

          g_free (mask_data);
        }

      g_free (name);
    }
}


/*  private functions  */

static void
gimp_brush_select_button_finalize (GObject *object)
{
  GimpBrushSelectButtonPrivate *priv;

  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (object);

  g_free (priv->brush_name);
  priv->brush_name = NULL;

  g_free (priv->mask_data);
  priv->mask_data = NULL;

  g_free (priv->title);
  priv->title = NULL;

  G_OBJECT_CLASS (gimp_brush_select_button_parent_class)->finalize (object);
}

static void
gimp_brush_select_button_set_property (GObject      *object,
                                       guint         property_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
501
  GimpBrushSelectButton        *button = GIMP_BRUSH_SELECT_BUTTON (object);
502
  GimpBrushSelectButtonPrivate *priv;
503 504 505
  gdouble opacity;
  gint32 spacing;
  gint32 paint_mode;
506

507
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
508 509 510 511 512 513 514

  switch (property_id)
    {
    case PROP_TITLE:
      priv->title = g_value_dup_string (value);
      break;
    case PROP_BRUSH_NAME:
515
      gimp_brush_select_button_set_brush (button,
516 517 518 519
                                          g_value_get_string (value),
                                          -1.0, -1, -1);
      break;
    case PROP_BRUSH_OPACITY:
520 521 522
      opacity = g_value_get_double (value);
      if (opacity >= 0.0)
        priv->opacity = opacity;
523 524
      break;
    case PROP_BRUSH_SPACING:
525 526 527
      spacing = g_value_get_int (value);
      if (spacing != -1)
        priv->spacing = spacing;
528 529
      break;
    case PROP_BRUSH_PAINT_MODE:
530 531 532
      paint_mode = g_value_get_int (value);
      if (paint_mode != -1)
        priv->paint_mode = paint_mode;
533 534 535 536 537 538 539 540 541 542 543 544 545
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_brush_select_button_get_property (GObject    *object,
                                       guint       property_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
546
  GimpBrushSelectButton        *button = GIMP_BRUSH_SELECT_BUTTON (object);
547 548
  GimpBrushSelectButtonPrivate *priv;

549
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

  switch (property_id)
    {
    case PROP_TITLE:
      g_value_set_string (value, priv->title);
      break;
    case PROP_BRUSH_NAME:
      g_value_set_string (value, priv->brush_name);
      break;
    case PROP_BRUSH_OPACITY:
      g_value_set_double (value, priv->opacity);
      break;
    case PROP_BRUSH_SPACING:
      g_value_set_int (value, priv->spacing);
      break;
    case PROP_BRUSH_PAINT_MODE:
      g_value_set_int (value, priv->paint_mode);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
575 576 577 578 579 580 581 582 583
gimp_brush_select_button_callback (const gchar   *name,
                                   gdouble        opacity,
                                   gint           spacing,
                                   GimpLayerMode  paint_mode,
                                   gint           width,
                                   gint           height,
                                   const guchar  *mask_data,
                                   gboolean       dialog_closing,
                                   gpointer       data)
584
{
585
  GimpBrushSelectButton        *button;
586 587 588
  GimpBrushSelectButtonPrivate *priv;
  GimpSelectButton             *select_button;

589
  button = GIMP_BRUSH_SELECT_BUTTON (data);
590

591 592
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
  select_button = GIMP_SELECT_BUTTON (button);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

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

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

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

  if (dialog_closing)
    select_button->temp_callback = NULL;

611
  g_signal_emit (button, brush_button_signals[BRUSH_SET], 0,
612 613
                 name, opacity, spacing, paint_mode, width, height, mask_data,
                 dialog_closing);
614
  g_object_notify (G_OBJECT (button), "brush-name");
615 616 617
}

static void
618
gimp_brush_select_button_clicked (GimpBrushSelectButton *button)
619 620
{
  GimpBrushSelectButtonPrivate *priv;
621 622 623 624
  GimpSelectButton             *select_button;

  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
  select_button = GIMP_SELECT_BUTTON (button);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

  if (select_button->temp_callback)
    {
      /*  calling gimp_brushes_set_popup() raises the dialog  */
      gimp_brushes_set_popup (select_button->temp_callback,
                              priv->brush_name,
                              priv->opacity,
                              priv->spacing,
                              priv->paint_mode);
    }
  else
    {
      select_button->temp_callback =
        gimp_brush_select_new (priv->title, priv->brush_name,
                               priv->opacity, priv->spacing, priv->paint_mode,
                               gimp_brush_select_button_callback,
641
                               button);
642 643 644 645
    }
}

static void
646
gimp_brush_select_preview_resize (GimpBrushSelectButton *button)
647 648 649
{
  GimpBrushSelectButtonPrivate *priv;

650
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
651 652 653 654 655 656 657 658 659 660 661

  if (priv->width > 0 && priv->height > 0)
    gimp_brush_select_preview_update (priv->preview,
                                      priv->width,
                                      priv->height,
                                      priv->mask_data);
}

static gboolean
gimp_brush_select_preview_events (GtkWidget             *widget,
                                  GdkEvent              *event,
662
                                  GimpBrushSelectButton *button)
663 664
{
  GimpBrushSelectButtonPrivate *priv;
665
  GdkEventButton               *bevent;
666

667
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
668 669 670 671 672 673 674 675 676 677 678

  if (priv->mask_data)
    {
      switch (event->type)
        {
        case GDK_BUTTON_PRESS:
          bevent = (GdkEventButton *) event;

          if (bevent->button == 1)
            {
              gtk_grab_add (widget);
679
              gimp_brush_select_button_open_popup (button,
680 681 682 683 684 685 686 687 688 689
                                                   bevent->x, bevent->y);
            }
          break;

        case GDK_BUTTON_RELEASE:
          bevent = (GdkEventButton *) event;

          if (bevent->button == 1)
            {
              gtk_grab_remove (widget);
690
              gimp_brush_select_button_close_popup (button);
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
            }
          break;

        default:
          break;
        }
    }

  return FALSE;
}

static void
gimp_brush_select_preview_draw (GimpPreviewArea *area,
                                gint             x,
                                gint             y,
                                gint             width,
                                gint             height,
                                const guchar    *mask_data,
                                gint             rowstride)
{
  const guchar *src;
  guchar       *dest;
  guchar       *buf;
  gint          i, j;

  buf = g_new (guchar, width * height);

  src  = mask_data;
  dest = buf;

  for (j = 0; j < height; j++)
    {
      const guchar *s = src;

      for (i = 0; i < width; i++, s++, dest++)
        *dest = 255 - *s;

      src += rowstride;
    }

  gimp_preview_area_draw (area,
                          x, y, width, height,
                          GIMP_GRAY_IMAGE,
                          buf,
                          width);

  g_free (buf);
}

static void
gimp_brush_select_preview_update (GtkWidget    *preview,
                                  gint          brush_width,
                                  gint          brush_height,
                                  const guchar *mask_data)
{
  GimpPreviewArea *area = GIMP_PREVIEW_AREA (preview);
747
  GtkAllocation    allocation;
748 749 750
  gint             x, y;
  gint             width, height;

751
  gtk_widget_get_allocation (preview, &allocation);
752

753 754 755 756 757
  width  = MIN (brush_width,  allocation.width);
  height = MIN (brush_height, allocation.height);

  x = ((allocation.width  - width)  / 2);
  y = ((allocation.height - height) / 2);
758 759 760 761

  if (x || y)
    gimp_preview_area_fill (area,
                            0, 0,
762 763
                            allocation.width,
                            allocation.height,
764 765 766 767 768 769 770 771
                            0xFF, 0xFF, 0xFF);

  gimp_brush_select_preview_draw (area,
                                  x, y, width, height,
                                  mask_data, brush_width);
}

static void
772
gimp_brush_select_button_open_popup (GimpBrushSelectButton *button,
773 774 775 776 777 778 779 780 781 782 783 784
                                     gint                   x,
                                     gint                   y)
{
  GimpBrushSelectButtonPrivate *priv;
  GtkWidget                    *frame;
  GtkWidget                    *preview;
  GdkScreen                    *screen;
  gint                          x_org;
  gint                          y_org;
  gint                          scr_w;
  gint                          scr_h;

785
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
786 787

  if (priv->popup)
788
    gimp_brush_select_button_close_popup (button);
789 790 791 792

  if (priv->width <= CELL_SIZE && priv->height <= CELL_SIZE)
    return;

793 794
  screen = gtk_widget_get_screen (GTK_WIDGET (button));

795
  priv->popup = gtk_window_new (GTK_WINDOW_POPUP);
796 797
  gtk_window_set_type_hint (GTK_WINDOW (priv->popup), GDK_WINDOW_TYPE_HINT_DND);
  gtk_window_set_screen (GTK_WINDOW (priv->popup), screen);
798 799 800 801 802 803 804 805 806 807 808 809

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
  gtk_container_add (GTK_CONTAINER (priv->popup), frame);
  gtk_widget_show (frame);

  preview = gimp_preview_area_new ();
  gtk_widget_set_size_request (preview, priv->width, priv->height);
  gtk_container_add (GTK_CONTAINER (frame), preview);
  gtk_widget_show (preview);

  /* decide where to put the popup */
810 811
  gdk_window_get_origin (gtk_widget_get_window (priv->preview),
                         &x_org, &y_org);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

  scr_w = gdk_screen_get_width (screen);
  scr_h = gdk_screen_get_height (screen);

  x = x_org + x - (priv->width  / 2);
  y = y_org + y - (priv->height / 2);
  x = (x < 0) ? 0 : x;
  y = (y < 0) ? 0 : y;
  x = (x + priv->width  > scr_w) ? scr_w - priv->width  : x;
  y = (y + priv->height > scr_h) ? scr_h - priv->height : y;

  gtk_window_move (GTK_WINDOW (priv->popup), x, y);

  gtk_widget_show (priv->popup);

  /*  Draw the brush  */
  gimp_brush_select_preview_draw (GIMP_PREVIEW_AREA (preview),
                                  0, 0, priv->width, priv->height,
                                  priv->mask_data, priv->width);
}

static void
834
gimp_brush_select_button_close_popup (GimpBrushSelectButton *button)
835 836 837
{
  GimpBrushSelectButtonPrivate *priv;

838
  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (button);
839 840 841 842 843 844 845 846 847

  if (priv->popup)
    {
      gtk_widget_destroy (priv->popup);
      priv->popup = NULL;
    }
}

static void
848 849 850 851 852 853 854
gimp_brush_select_drag_data_received (GimpBrushSelectButton *button,
                                      GdkDragContext        *context,
                                      gint                   x,
                                      gint                   y,
                                      GtkSelectionData      *selection,
                                      guint                  info,
                                      guint                  time)
855
{
856
  gint   length = gtk_selection_data_get_length (selection);
857 858
  gchar *str;

859
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
860
    {
861
      g_warning ("%s: received invalid brush data", G_STRFUNC);
862 863 864
      return;
    }

865 866
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
867 868 869 870 871 872 873 874 875 876 877 878

  if (g_utf8_validate (str, -1, NULL))
    {
      gint     pid;
      gpointer unused;
      gint     name_offset = 0;

      if (sscanf (str, "%i:%p:%n", &pid, &unused, &name_offset) >= 2 &&
          pid == gimp_getpid () && name_offset > 0)
        {
          gchar *name = str + name_offset;

879
          gimp_brush_select_button_set_brush (button, name, -1.0, -1, -1);
880 881 882 883 884 885 886 887 888
        }
    }

  g_free (str);
}

static GtkWidget *
gimp_brush_select_button_create_inside (GimpBrushSelectButton *brush_button)
{
889 890 891
  GtkWidget                    *hbox;
  GtkWidget                    *frame;
  GtkWidget                    *button;
892 893 894 895 896 897
  GimpBrushSelectButtonPrivate *priv;

  priv = GIMP_BRUSH_SELECT_BUTTON_GET_PRIVATE (brush_button);

  gtk_widget_push_composite_child ();

898
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);

  priv->preview = gimp_preview_area_new ();
  gtk_widget_add_events (priv->preview,
                         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
  gtk_widget_set_size_request (priv->preview, CELL_SIZE, CELL_SIZE);
  gtk_container_add (GTK_CONTAINER (frame), priv->preview);

  g_signal_connect_swapped (priv->preview, "size-allocate",
                            G_CALLBACK (gimp_brush_select_preview_resize),
                            brush_button);
  g_signal_connect (priv->preview, "event",
                    G_CALLBACK (gimp_brush_select_preview_events),
                    brush_button);

  gtk_drag_dest_set (GTK_WIDGET (priv->preview),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     &target, 1,
                     GDK_ACTION_COPY);

924 925 926
  g_signal_connect_swapped (priv->preview, "drag-data-received",
                            G_CALLBACK (gimp_brush_select_drag_data_received),
                            brush_button);
927 928 929 930 931 932 933 934 935 936 937 938 939 940

  button = gtk_button_new_with_mnemonic (_("_Browse..."));
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  g_signal_connect_swapped (button, "clicked",
                            G_CALLBACK (gimp_brush_select_button_clicked),
                            brush_button);

  gtk_widget_show_all (hbox);

  gtk_widget_pop_composite_child ();

  return hbox;
}