gimpbrushselectbutton.c 30.2 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
 * License along with this library.  If not, see
19
 * <https://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
#define CELL_SIZE 20


50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
enum
{
  BRUSH_SET,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_TITLE,
  PROP_BRUSH_NAME,
  PROP_BRUSH_OPACITY,
  PROP_BRUSH_SPACING,
  PROP_BRUSH_PAINT_MODE
};
65 66 67 68


struct _GimpBrushSelectButtonPrivate
{
69 70 71 72 73 74 75 76 77 78 79 80 81
  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;
82 83
};

84
#define GET_PRIVATE(obj) (((GimpBrushSelectButton *) (obj))->priv)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99


/*  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);

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

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

112
static void     gimp_brush_select_preview_resize  (GimpBrushSelectButton *button);
113 114
static gboolean gimp_brush_select_preview_events  (GtkWidget             *widget,
                                                   GdkEvent              *event,
115
                                                   GimpBrushSelectButton *button);
116 117 118 119 120 121 122 123 124 125 126 127
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);

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

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

141
static GtkWidget * gimp_brush_select_button_create_inside (GimpBrushSelectButton *button);
142 143 144 145 146 147 148


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

static guint brush_button_signals[LAST_SIGNAL] = { 0 };


149 150
G_DEFINE_TYPE_WITH_PRIVATE (GimpBrushSelectButton, gimp_brush_select_button,
                            GIMP_TYPE_SELECT_BUTTON)
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171


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.
   *
172
   * Since: 2.4
173 174 175 176 177 178 179 180 181 182 183 184 185 186
   */
  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.
   *
187
   * Since: 2.4
188 189 190 191 192 193 194 195 196 197 198 199 200
   */
  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.
   *
201
   * Since: 2.4
202 203 204 205 206 207 208 209 210 211 212 213 214
   */
  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.
   *
215
   * Since: 2.4
216 217 218 219 220 221 222 223 224 225 226
   */
  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:
   *
227
   * The paint mode.
228
   *
229
   * Since: 2.4
230 231 232 233 234
   */
  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",
235
                                                     -1, GIMP_LAYER_MODE_LUMINANCE,
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                                                     -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.
   *
253
   * Since: 2.4
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
   */
  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);
}

static void
274
gimp_brush_select_button_init (GimpBrushSelectButton *button)
275 276 277 278 279 280 281
{
  GimpBrushSelectButtonPrivate *priv;
  gint                          mask_bpp;
  gint                          mask_data_size;
  gint                          color_bpp;
  gint                          color_data_size;
  guint8                       *color_data;
282

283
  button->priv = gimp_brush_select_button_get_instance_private (button);
284 285 286


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

  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);

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

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

  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.
 *
323
 * Since: 2.4
324 325
 */
GtkWidget *
326 327 328 329 330
gimp_brush_select_button_new (const gchar   *title,
                              const gchar   *brush_name,
                              gdouble        opacity,
                              gint           spacing,
                              GimpLayerMode  paint_mode)
331
{
332
  GtkWidget *button;
333 334

  if (title)
335 336 337 338 339 340 341
    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);
342
  else
343 344 345 346 347 348 349 350
    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;
351 352 353 354 355 356 357 358 359 360 361 362 363
}

/**
 * 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.
 *
364
 * Since: 2.4
365
 */
366
const gchar *
367
gimp_brush_select_button_get_brush (GimpBrushSelectButton *button,
368 369
                                    gdouble               *opacity,
                                    gint                  *spacing,
370
                                    GimpLayerMode         *paint_mode)
371 372 373
{
  GimpBrushSelectButtonPrivate *priv;

374 375
  g_return_val_if_fail (GIMP_IS_BRUSH_SELECT_BUTTON (button), NULL);

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

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

412
  g_return_if_fail (GIMP_IS_BRUSH_SELECT_BUTTON (button));
413

414
  select_button = GIMP_SELECT_BUTTON (button);
415 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

  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,
463
                                             FALSE, button);
464 465 466 467 468 469 470 471 472 473 474 475 476 477

          g_free (mask_data);
        }

      g_free (name);
    }
}


/*  private functions  */

static void
gimp_brush_select_button_finalize (GObject *object)
{
478
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (object);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

  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)
{
498
  GimpBrushSelectButton        *button = GIMP_BRUSH_SELECT_BUTTON (object);
499
  GimpBrushSelectButtonPrivate *priv   = GET_PRIVATE (button);
500 501 502
  gdouble opacity;
  gint32 spacing;
  gint32 paint_mode;
503 504 505 506 507 508 509

  switch (property_id)
    {
    case PROP_TITLE:
      priv->title = g_value_dup_string (value);
      break;
    case PROP_BRUSH_NAME:
510
      gimp_brush_select_button_set_brush (button,
511 512 513 514
                                          g_value_get_string (value),
                                          -1.0, -1, -1);
      break;
    case PROP_BRUSH_OPACITY:
515 516 517
      opacity = g_value_get_double (value);
      if (opacity >= 0.0)
        priv->opacity = opacity;
518 519
      break;
    case PROP_BRUSH_SPACING:
520 521 522
      spacing = g_value_get_int (value);
      if (spacing != -1)
        priv->spacing = spacing;
523 524
      break;
    case PROP_BRUSH_PAINT_MODE:
525 526 527
      paint_mode = g_value_get_int (value);
      if (paint_mode != -1)
        priv->paint_mode = paint_mode;
528 529 530 531 532 533 534 535 536 537 538 539 540
      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)
{
541
  GimpBrushSelectButton        *button = GIMP_BRUSH_SELECT_BUTTON (object);
542
  GimpBrushSelectButtonPrivate *priv   = GET_PRIVATE (button);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

  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
568 569 570 571 572 573 574 575 576
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)
577
{
578 579 580
  GimpBrushSelectButton        *button        = GIMP_BRUSH_SELECT_BUTTON (data);
  GimpBrushSelectButtonPrivate *priv          = GET_PRIVATE (button);
  GimpSelectButton             *select_button = GIMP_SELECT_BUTTON (button);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

  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;

599
  g_signal_emit (button, brush_button_signals[BRUSH_SET], 0,
600 601
                 name, opacity, spacing, paint_mode, width, height, mask_data,
                 dialog_closing);
602
  g_object_notify (G_OBJECT (button), "brush-name");
603 604 605
}

static void
606
gimp_brush_select_button_clicked (GimpBrushSelectButton *button)
607
{
608 609
  GimpBrushSelectButtonPrivate *priv          = GET_PRIVATE (button);
  GimpSelectButton             *select_button = GIMP_SELECT_BUTTON (button);
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

  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,
626
                               button);
627 628 629 630
    }
}

static void
631
gimp_brush_select_preview_resize (GimpBrushSelectButton *button)
632
{
633
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
634 635 636 637 638 639 640 641 642 643 644

  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,
645
                                  GimpBrushSelectButton *button)
646
{
647
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
648
  GdkEventButton               *bevent;
649 650 651 652 653 654 655 656 657 658 659

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

          if (bevent->button == 1)
            {
              gtk_grab_add (widget);
660
              gimp_brush_select_button_open_popup (button,
661 662 663 664 665 666 667 668 669 670
                                                   bevent->x, bevent->y);
            }
          break;

        case GDK_BUTTON_RELEASE:
          bevent = (GdkEventButton *) event;

          if (bevent->button == 1)
            {
              gtk_grab_remove (widget);
671
              gimp_brush_select_button_close_popup (button);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 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
            }
          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);
728
  GtkAllocation    allocation;
729 730 731
  gint             x, y;
  gint             width, height;

732
  gtk_widget_get_allocation (preview, &allocation);
733

734 735 736 737 738
  width  = MIN (brush_width,  allocation.width);
  height = MIN (brush_height, allocation.height);

  x = ((allocation.width  - width)  / 2);
  y = ((allocation.height - height) / 2);
739 740 741 742

  if (x || y)
    gimp_preview_area_fill (area,
                            0, 0,
743 744
                            allocation.width,
                            allocation.height,
745 746 747 748 749 750 751 752
                            0xFF, 0xFF, 0xFF);

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

static void
753
gimp_brush_select_button_open_popup (GimpBrushSelectButton *button,
754 755 756
                                     gint                   x,
                                     gint                   y)
{
757
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
758 759
  GtkWidget                    *frame;
  GtkWidget                    *preview;
760 761
  GdkMonitor                   *monitor;
  GdkRectangle                  workarea;
762 763 764 765
  gint                          x_org;
  gint                          y_org;

  if (priv->popup)
766
    gimp_brush_select_button_close_popup (button);
767 768 769 770 771

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

  priv->popup = gtk_window_new (GTK_WINDOW_POPUP);
772
  gtk_window_set_type_hint (GTK_WINDOW (priv->popup), GDK_WINDOW_TYPE_HINT_DND);
773 774
  gtk_window_set_screen (GTK_WINDOW (priv->popup),
                         gtk_widget_get_screen (GTK_WIDGET (button)));
775 776 777 778 779 780 781 782 783 784 785 786

  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 */
787 788
  gdk_window_get_origin (gtk_widget_get_window (priv->preview),
                         &x_org, &y_org);
789

790 791
  monitor = gimp_widget_get_monitor (GTK_WIDGET (button));
  gdk_monitor_get_workarea (monitor, &workarea);
792 793 794

  x = x_org + x - (priv->width  / 2);
  y = y_org + y - (priv->height / 2);
795 796 797

  x = CLAMP (x, workarea.x, workarea.x + workarea.width  - priv->width);
  y = CLAMP (y, workarea.y, workarea.y + workarea.height - priv->height);
798 799 800 801 802 803 804 805 806 807 808 809

  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
810
gimp_brush_select_button_close_popup (GimpBrushSelectButton *button)
811
{
812
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
813 814 815 816 817 818 819 820 821

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

static void
822 823 824 825 826 827 828
gimp_brush_select_drag_data_received (GimpBrushSelectButton *button,
                                      GdkDragContext        *context,
                                      gint                   x,
                                      gint                   y,
                                      GtkSelectionData      *selection,
                                      guint                  info,
                                      guint                  time)
829
{
830
  gint   length = gtk_selection_data_get_length (selection);
831 832
  gchar *str;

833
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
834
    {
835
      g_warning ("%s: received invalid brush data", G_STRFUNC);
836 837 838
      return;
    }

839 840
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
841 842 843 844 845 846 847 848 849 850 851 852

  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;

853
          gimp_brush_select_button_set_brush (button, name, -1.0, -1, -1);
854 855 856 857 858 859 860 861 862
        }
    }

  g_free (str);
}

static GtkWidget *
gimp_brush_select_button_create_inside (GimpBrushSelectButton *brush_button)
{
863
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (brush_button);
864 865 866
  GtkWidget                    *hbox;
  GtkWidget                    *frame;
  GtkWidget                    *button;
867

868
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

  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);

894 895 896
  g_signal_connect_swapped (priv->preview, "drag-data-received",
                            G_CALLBACK (gimp_brush_select_drag_data_received),
                            brush_button);
897 898 899 900 901 902 903 904 905 906 907 908

  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);

  return hbox;
}