gimpbrushselectbutton.c 30.4 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171


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.
   *
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 274 275 276
   */
  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
277
gimp_brush_select_button_init (GimpBrushSelectButton *button)
278 279 280 281 282 283 284
{
  GimpBrushSelectButtonPrivate *priv;
  gint                          mask_bpp;
  gint                          mask_data_size;
  gint                          color_bpp;
  gint                          color_data_size;
  guint8                       *color_data;
285

286 287 288 289 290 291
  button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
                                              GIMP_TYPE_BRUSH_SELECT_BUTTON,
                                              GimpBrushSelectButtonPrivate);


  priv = GET_PRIVATE (button);
292 293 294 295 296 297 298 299 300 301 302 303

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

304 305 306
  if (color_data)
    g_free (color_data);

307 308
  priv->inside = gimp_brush_select_button_create_inside (button);
  gtk_container_add (GTK_CONTAINER (button), priv->inside);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

  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.
 *
328
 * Since: 2.4
329 330
 */
GtkWidget *
331 332 333 334 335
gimp_brush_select_button_new (const gchar   *title,
                              const gchar   *brush_name,
                              gdouble        opacity,
                              gint           spacing,
                              GimpLayerMode  paint_mode)
336
{
337
  GtkWidget *button;
338 339

  if (title)
340 341 342 343 344 345 346
    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);
347
  else
348 349 350 351 352 353 354 355
    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;
356 357 358 359 360 361 362 363 364 365 366 367 368
}

/**
 * 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.
 *
369
 * Since: 2.4
370
 */
371
const gchar *
372
gimp_brush_select_button_get_brush (GimpBrushSelectButton *button,
373 374
                                    gdouble               *opacity,
                                    gint                  *spacing,
375
                                    GimpLayerMode         *paint_mode)
376 377 378
{
  GimpBrushSelectButtonPrivate *priv;

379 380
  g_return_val_if_fail (GIMP_IS_BRUSH_SELECT_BUTTON (button), NULL);

381
  priv = GET_PRIVATE (button);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

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

417
  g_return_if_fail (GIMP_IS_BRUSH_SELECT_BUTTON (button));
418

419
  select_button = GIMP_SELECT_BUTTON (button);
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 464 465 466 467

  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,
468
                                             FALSE, button);
469 470 471 472 473 474 475 476 477 478 479 480 481 482

          g_free (mask_data);
        }

      g_free (name);
    }
}


/*  private functions  */

static void
gimp_brush_select_button_finalize (GObject *object)
{
483
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (object);
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

  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)
{
503
  GimpBrushSelectButton        *button = GIMP_BRUSH_SELECT_BUTTON (object);
504
  GimpBrushSelectButtonPrivate *priv   = GET_PRIVATE (button);
505 506 507
  gdouble opacity;
  gint32 spacing;
  gint32 paint_mode;
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
  GimpBrushSelectButtonPrivate *priv   = GET_PRIVATE (button);
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

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

  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;

604
  g_signal_emit (button, brush_button_signals[BRUSH_SET], 0,
605 606
                 name, opacity, spacing, paint_mode, width, height, mask_data,
                 dialog_closing);
607
  g_object_notify (G_OBJECT (button), "brush-name");
608 609 610
}

static void
611
gimp_brush_select_button_clicked (GimpBrushSelectButton *button)
612
{
613 614
  GimpBrushSelectButtonPrivate *priv          = GET_PRIVATE (button);
  GimpSelectButton             *select_button = GIMP_SELECT_BUTTON (button);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

  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,
631
                               button);
632 633 634 635
    }
}

static void
636
gimp_brush_select_preview_resize (GimpBrushSelectButton *button)
637
{
638
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
639 640 641 642 643 644 645 646 647 648 649

  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,
650
                                  GimpBrushSelectButton *button)
651
{
652
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
653
  GdkEventButton               *bevent;
654 655 656 657 658 659 660 661 662 663 664

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

          if (bevent->button == 1)
            {
              gtk_grab_add (widget);
665
              gimp_brush_select_button_open_popup (button,
666 667 668 669 670 671 672 673 674 675
                                                   bevent->x, bevent->y);
            }
          break;

        case GDK_BUTTON_RELEASE:
          bevent = (GdkEventButton *) event;

          if (bevent->button == 1)
            {
              gtk_grab_remove (widget);
676
              gimp_brush_select_button_close_popup (button);
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 728 729 730 731 732
            }
          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);
733
  GtkAllocation    allocation;
734 735 736
  gint             x, y;
  gint             width, height;

737
  gtk_widget_get_allocation (preview, &allocation);
738

739 740 741 742 743
  width  = MIN (brush_width,  allocation.width);
  height = MIN (brush_height, allocation.height);

  x = ((allocation.width  - width)  / 2);
  y = ((allocation.height - height) / 2);
744 745 746 747

  if (x || y)
    gimp_preview_area_fill (area,
                            0, 0,
748 749
                            allocation.width,
                            allocation.height,
750 751 752 753 754 755 756 757
                            0xFF, 0xFF, 0xFF);

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

static void
758
gimp_brush_select_button_open_popup (GimpBrushSelectButton *button,
759 760 761
                                     gint                   x,
                                     gint                   y)
{
762
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
763 764
  GtkWidget                    *frame;
  GtkWidget                    *preview;
765 766
  GdkMonitor                   *monitor;
  GdkRectangle                  workarea;
767 768 769 770
  gint                          x_org;
  gint                          y_org;

  if (priv->popup)
771
    gimp_brush_select_button_close_popup (button);
772 773 774 775 776

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

  priv->popup = gtk_window_new (GTK_WINDOW_POPUP);
777
  gtk_window_set_type_hint (GTK_WINDOW (priv->popup), GDK_WINDOW_TYPE_HINT_DND);
778 779
  gtk_window_set_screen (GTK_WINDOW (priv->popup),
                         gtk_widget_get_screen (GTK_WIDGET (button)));
780 781 782 783 784 785 786 787 788 789 790 791

  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 */
792 793
  gdk_window_get_origin (gtk_widget_get_window (priv->preview),
                         &x_org, &y_org);
794

795 796
  monitor = gimp_widget_get_monitor (GTK_WIDGET (button));
  gdk_monitor_get_workarea (monitor, &workarea);
797 798 799

  x = x_org + x - (priv->width  / 2);
  y = y_org + y - (priv->height / 2);
800 801 802

  x = CLAMP (x, workarea.x, workarea.x + workarea.width  - priv->width);
  y = CLAMP (y, workarea.y, workarea.y + workarea.height - priv->height);
803 804 805 806 807 808 809 810 811 812 813 814

  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
815
gimp_brush_select_button_close_popup (GimpBrushSelectButton *button)
816
{
817
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (button);
818 819 820 821 822 823 824 825 826

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

static void
827 828 829 830 831 832 833
gimp_brush_select_drag_data_received (GimpBrushSelectButton *button,
                                      GdkDragContext        *context,
                                      gint                   x,
                                      gint                   y,
                                      GtkSelectionData      *selection,
                                      guint                  info,
                                      guint                  time)
834
{
835
  gint   length = gtk_selection_data_get_length (selection);
836 837
  gchar *str;

838
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
839
    {
840
      g_warning ("%s: received invalid brush data", G_STRFUNC);
841 842 843
      return;
    }

844 845
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
846 847 848 849 850 851 852 853 854 855 856 857

  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;

858
          gimp_brush_select_button_set_brush (button, name, -1.0, -1, -1);
859 860 861 862 863 864 865 866 867
        }
    }

  g_free (str);
}

static GtkWidget *
gimp_brush_select_button_create_inside (GimpBrushSelectButton *brush_button)
{
868
  GimpBrushSelectButtonPrivate *priv = GET_PRIVATE (brush_button);
869 870 871
  GtkWidget                    *hbox;
  GtkWidget                    *frame;
  GtkWidget                    *button;
872

873
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

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

899 900 901
  g_signal_connect_swapped (priv->preview, "drag-data-received",
                            G_CALLBACK (gimp_brush_select_drag_data_received),
                            brush_button);
902 903 904 905 906 907 908 909 910 911 912 913

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