brush_select.c 50.4 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
18
 */
#include <string.h>
19

Elliot Lee's avatar
Elliot Lee committed
20
#include "appenv.h"
21
#include "brush_scale.h"
22
23
24
#include "brush_edit.h"
#include "brush_select.h"
#include "dialog_handler.h"
25
#include "gimpbrushgenerated.h"
26
#include "gimpbrushlist.h"
27
28
#include "gimpbrushpipe.h"
#include "gimpbrushpipeP.h"
29
#include "gimpcontext.h"
Michael Natterer's avatar
Michael Natterer committed
30
#include "gimpdnd.h"
31
#include "gimplist.h"
32
#include "gimprc.h"
33
#include "gimpui.h"
34
#include "paint_options.h"
35
#include "session.h"
Elliot Lee's avatar
Elliot Lee committed
36

Sven Neumann's avatar
Sven Neumann committed
37
#include "config.h"
38
#include "libgimp/gimpintl.h"
Elliot Lee's avatar
Elliot Lee committed
39

40
41
#define MIN_CELL_SIZE     25
#define MAX_CELL_SIZE     25  /*  disable variable brush preview size  */ 
Elliot Lee's avatar
Elliot Lee committed
42
43
44
45

#define STD_BRUSH_COLUMNS 5
#define STD_BRUSH_ROWS    5

46
/* how long to wait after mouse-down before showing brush popup */
47
48
#define POPUP_DELAY_MS      150

49
50
#define MAX_WIN_WIDTH(bsp)     (MIN_CELL_SIZE * ((bsp)->NUM_BRUSH_COLUMNS))
#define MAX_WIN_HEIGHT(bsp)    (MIN_CELL_SIZE * ((bsp)->NUM_BRUSH_ROWS))
51
52
#define MARGIN_WIDTH      1
#define MARGIN_HEIGHT     1
Elliot Lee's avatar
Elliot Lee committed
53
54
55
56
57
58
59

#define BRUSH_EVENT_MASK  GDK_EXPOSURE_MASK | \
                          GDK_BUTTON_PRESS_MASK | \
			  GDK_BUTTON_RELEASE_MASK | \
                          GDK_BUTTON1_MOTION_MASK | \
			  GDK_ENTER_NOTIFY_MASK

60
/*  the pixmaps for the [scale|pipe]_indicators  */
61
#define indicator_width  7
62
#define indicator_height 7
63

64
65
66
#define WHT {255, 255, 255}
#define BLK {  0,   0,   0}
#define RED {255, 127, 127}
67

Elliot Lee's avatar
Elliot Lee committed
68
/*  local function prototypes  */
Michael Natterer's avatar
Michael Natterer committed
69
70
71
static void     brush_change_callbacks             (BrushSelect      *bsp,
						    gboolean          closing);

72
73
static GimpBrush * brush_select_drag_brush         (GtkWidget        *widget,
						    gpointer          data);
Michael Natterer's avatar
Michael Natterer committed
74
75
76
static void     brush_select_drop_brush            (GtkWidget        *widget,
						    GimpBrush        *brush,
						    gpointer          data);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
static void     brush_select_brush_changed         (GimpContext      *context,
						    GimpBrush        *brush,
						    BrushSelect      *bsp);
static void     brush_select_opacity_changed       (GimpContext      *context,
						    gdouble           opacity,
						    BrushSelect      *bsp);
static void     brush_select_paint_mode_changed    (GimpContext      *context,
						    LayerModeEffects  paint_mode,
						    BrushSelect      *bsp);
static void     brush_select_select                (BrushSelect      *bsp,
						    GimpBrush        *brush);

static void     brush_select_brush_dirty_callback  (GimpBrush        *brush,
						    BrushSelect      *bsp);
static void     connect_signals_to_brush           (GimpBrush        *brush, 
						    BrushSelect      *bsp);
static void     disconnect_signals_from_brush      (GimpBrush        *brush,
						    BrushSelect      *bsp);
static void     brush_added_callback               (GimpBrushList    *list,
						    GimpBrush        *brush,
						    BrushSelect      *bsp);
static void     brush_removed_callback             (GimpBrushList    *list,
						    GimpBrush        *brush,
						    BrushSelect      *bsp);

static void     draw_brush_popup                   (GtkPreview       *preview,
						    GimpBrush        *brush,
						    gint              width,
						    gint              height);
static gint     brush_popup_anim_timeout           (gpointer          data);
static gboolean brush_popup_timeout                (gpointer          data);
static void     brush_popup_open                   (BrushSelect *,
						    gint, gint, GimpBrush *);
static void     brush_popup_close                  (BrushSelect *);

static void     display_setup                      (BrushSelect *);
static void     display_brush                      (BrushSelect *,
						    GimpBrush *, gint, gint);
static void     do_display_brush                   (GimpBrush   *brush,
						    BrushSelect *bsp);
static void     display_brushes                    (BrushSelect *);

static void     brush_select_show_selected         (BrushSelect *, gint, gint);
static void     update_active_brush_field          (BrushSelect *);
static void     preview_calc_scrollbar             (BrushSelect *);

static gint     brush_select_resize		   (GtkWidget *, GdkEvent *,
						    BrushSelect *);
static gint     brush_select_events                (GtkWidget *, GdkEvent *,
						    BrushSelect *);

/* static void  brush_select_map_callback          (GtkWidget *,
                                                    BrushSelect *); */
static void     brush_select_scroll_update         (GtkAdjustment *, gpointer);
static void     opacity_scale_update               (GtkAdjustment *, gpointer);
static void     paint_mode_menu_callback           (GtkWidget *, gpointer);
static void     spacing_scale_update               (GtkAdjustment *, gpointer);

static void     brush_select_close_callback        (GtkWidget *, gpointer);
static void     brush_select_refresh_callback      (GtkWidget *, gpointer);
static void     brush_select_new_brush_callback    (GtkWidget *, gpointer);
static void     brush_select_edit_brush_callback   (GtkWidget *, gpointer);
static void     brush_select_delete_brush_callback (GtkWidget *, gpointer);

Michael Natterer's avatar
Michael Natterer committed
141
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
172
173
174
175
176
177
178
179
180
181
static unsigned char scale_indicator_bits[7][7][3] = 
{
  { WHT, WHT, WHT, WHT, WHT, WHT, WHT },
  { WHT, WHT, WHT, BLK, WHT, WHT, WHT },
  { WHT, WHT, WHT, BLK, WHT, WHT, WHT },
  { WHT, BLK, BLK, BLK, BLK, BLK, WHT },
  { WHT, WHT, WHT, BLK, WHT, WHT, WHT },
  { WHT, WHT, WHT, BLK, WHT, WHT, WHT },
  { WHT, WHT, WHT, WHT, WHT, WHT, WHT }
};

static unsigned char pipe_indicator_bits[7][7][3] = 
{
  { WHT, WHT, WHT, WHT, WHT, WHT, WHT },
  { WHT, WHT, WHT, WHT, WHT, WHT, RED },
  { WHT, WHT, WHT, WHT, WHT, RED, RED },
  { WHT, WHT, WHT, WHT, RED, RED, RED },
  { WHT, WHT, WHT, RED, RED, RED, RED },
  { WHT, WHT, RED, RED, RED, RED, RED },
  { WHT, RED, RED, RED, RED, RED, RED }
};

static unsigned char scale_pipe_indicator_bits[7][7][3] = 
{
  { WHT, WHT, WHT, WHT, WHT, WHT, WHT },
  { WHT, WHT, WHT, BLK, WHT, WHT, RED },
  { WHT, WHT, WHT, BLK, WHT, RED, RED },
  { WHT, BLK, BLK, BLK, BLK, BLK, RED },
  { WHT, WHT, WHT, BLK, RED, RED, RED },
  { WHT, WHT, RED, BLK, RED, RED, RED },
  { WHT, RED, RED, RED, RED, RED, RED }
};

/*  dnd stuff  */
static GtkTargetEntry preview_target_table[] =
{
  GIMP_TARGET_BRUSH
};
static guint preview_n_targets = (sizeof (preview_target_table) /
                                  sizeof (preview_target_table[0]));

182
183
/*  The main brush selection dialog  */
BrushSelect *brush_select_dialog = NULL;
Elliot Lee's avatar
Elliot Lee committed
184

185
/*  local variables  */
Elliot Lee's avatar
Elliot Lee committed
186

187
/*  List of active dialogs  */
188
GSList *brush_active_dialogs = NULL;
Elliot Lee's avatar
Elliot Lee committed
189

190
/*  Brush editor dialog  */
191
static BrushEditGeneratedWindow *brush_edit_generated_dialog;
192

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
void
brush_dialog_create (void)
{
  if (! brush_select_dialog)
    {
      brush_select_dialog = brush_select_new (NULL, NULL, 0.0, 0, 0);
    }
  else
    {
      if (!GTK_WIDGET_VISIBLE (brush_select_dialog->shell))
	gtk_widget_show (brush_select_dialog->shell);
      else
	gdk_window_raise (brush_select_dialog->shell->window);
    }
}

void
brush_dialog_free ()
{
  if (brush_select_dialog)
    {
Michael Natterer's avatar
Michael Natterer committed
214
215
216
217
218
219
220
221
222
      session_get_window_info (brush_select_dialog->shell,
			       &brush_select_session_info);

      /*  save the size of the preview  */
      brush_select_session_info.width =
	brush_select_dialog->preview->allocation.width;
      brush_select_session_info.height =
	brush_select_dialog->preview->allocation.height;

223
224
225
226
227
      brush_select_free (brush_select_dialog);
      brush_select_dialog = NULL;
    }
}

228
/*  If title == NULL then it is the main brush dialog  */
229
BrushSelect *
230
231
232
233
234
235
236
237
brush_select_new (gchar   *title,
		  /*  These are the required initial vals
		   *  If init_name == NULL then use current brush
		   */
		  gchar   *init_name,
		  gdouble  init_opacity,
		  gint     init_spacing,
		  gint     init_mode)
Elliot Lee's avatar
Elliot Lee committed
238
{
239
  BrushSelect *bsp;
Elliot Lee's avatar
Elliot Lee committed
240
241
  GtkWidget *vbox;
  GtkWidget *hbox;
242
  GtkWidget *frame;
Elliot Lee's avatar
Elliot Lee committed
243
  GtkWidget *sbar;
244
245
  GtkWidget *sep;
  GtkWidget *table;
Elliot Lee's avatar
Elliot Lee committed
246
247
  GtkWidget *util_box;
  GtkWidget *option_menu;
248
  GtkWidget *menu;
Elliot Lee's avatar
Elliot Lee committed
249
  GtkWidget *slider;
250
  GtkWidget *button;
251

252
  GimpBrush *active = NULL;
Elliot Lee's avatar
Elliot Lee committed
253

Michael Natterer's avatar
Michael Natterer committed
254
  static gboolean first_call = TRUE;
Elliot Lee's avatar
Elliot Lee committed
255

256
257
  bsp = g_new (BrushSelect, 1);
  bsp->callback_name          = NULL;
258
  bsp->dnd_brush              = NULL;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  bsp->brush_popup            = NULL;
  bsp->popup_timeout_tag      = 0;
  bsp->popup_anim_timeout_tag = 0;
  bsp->scroll_offset          = 0;
  bsp->old_row                = 0;
  bsp->old_col                = 0;
  bsp->NUM_BRUSH_COLUMNS      = STD_BRUSH_COLUMNS;
  bsp->NUM_BRUSH_ROWS         = STD_BRUSH_ROWS;
  bsp->redraw                 = TRUE;
  bsp->freeze                 = FALSE;

  /*  The shell  */
  bsp->shell = gimp_dialog_new (title ? title : _("Brush Selection"),
				"brush_selection",
				gimp_standard_help_func,
				"dialogs/brush_selection.html",
275
				title ? GTK_WIN_POS_MOUSE : GTK_WIN_POS_NONE,
276
277
278
				FALSE, TRUE, FALSE,

				_("Refresh"), brush_select_refresh_callback,
279
				bsp, NULL, NULL, FALSE, FALSE,
280
				_("Close"), brush_select_close_callback,
281
				bsp, NULL, NULL, TRUE, TRUE,
282
283

				NULL);
284

285
  if (title)
286
    {
287
288
289
290
      bsp->context = gimp_context_new (title, NULL);
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
291
292
      bsp->context = gimp_context_get_user ();

293
294
      session_set_window_geometry (bsp->shell, &brush_select_session_info,
				   FALSE);
Michael Natterer's avatar
Michael Natterer committed
295
      dialog_register (bsp->shell);
296
    }
297

Michael Natterer's avatar
Michael Natterer committed
298
  if (no_data && first_call)
299
    brushes_init (FALSE);
300

Michael Natterer's avatar
Michael Natterer committed
301
302
  first_call = FALSE;

303
304
305
  if (title && init_name && strlen (init_name))
    {
      active = gimp_brush_list_get_brush (brush_list, init_name);
306
    }
307
308
  else
    {
309
310
      active = gimp_context_get_brush (gimp_context_get_user ());
    }
Elliot Lee's avatar
Elliot Lee committed
311

312
313
314
315
  if (!active)
    {
      active = gimp_context_get_brush (gimp_context_get_standard ());
    }
316

317
318
319
320
321
322
  if (title)
    {
      gimp_context_set_brush (bsp->context, active);
      gimp_context_set_paint_mode (bsp->context, init_mode);
      gimp_context_set_opacity (bsp->context, init_opacity);
      bsp->spacing_value = init_spacing;
323
    }
Elliot Lee's avatar
Elliot Lee committed
324

325
326
327
328
329
  /*  The main vbox  */
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (bsp->shell)->vbox), vbox);

330
  /*  The horizontal box containing the brush list & options box */
Elliot Lee's avatar
Elliot Lee committed
331
  hbox = gtk_hbox_new (FALSE, 2);
332
333
334
335
336
337
  gtk_container_add (GTK_CONTAINER (vbox), hbox);

  /*  A place holder for paint mode switching  */
  bsp->left_box = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), bsp->left_box, TRUE, TRUE, 0);

338
339
  /*  The horizontal box containing preview & scrollbar  */
  bsp->brush_selection_box = gtk_hbox_new (FALSE, 2);
340
341
  gtk_container_add (GTK_CONTAINER (bsp->left_box), bsp->brush_selection_box);

342
343
344
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (bsp->brush_selection_box), frame,
345
		      TRUE, TRUE, 0);
346
347
348
349

  bsp->sbar_data =
    GTK_ADJUSTMENT (gtk_adjustment_new (0, 0, MAX_WIN_HEIGHT (bsp),
					1, 1, MAX_WIN_HEIGHT (bsp)));
Elliot Lee's avatar
Elliot Lee committed
350
351
  sbar = gtk_vscrollbar_new (bsp->sbar_data);
  gtk_signal_connect (GTK_OBJECT (bsp->sbar_data), "value_changed",
352
353
		      GTK_SIGNAL_FUNC (brush_select_scroll_update),
		      bsp);
354
  gtk_box_pack_start (GTK_BOX (bsp->brush_selection_box), sbar, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
355
356
357

  /*  Create the brush preview window and the underlying image  */

358
  /*  Get the maximum brush extents  */
359
  bsp->cell_width  = MIN_CELL_SIZE;
360
  bsp->cell_height = MIN_CELL_SIZE;
Elliot Lee's avatar
Elliot Lee committed
361

362
  bsp->preview = gtk_preview_new (GTK_PREVIEW_COLOR);
363
364
  gtk_preview_size (GTK_PREVIEW (bsp->preview),
		    MAX_WIN_WIDTH (bsp), MAX_WIN_HEIGHT (bsp));
365
366
  gtk_widget_set_usize (bsp->preview,
			MAX_WIN_WIDTH (bsp), MAX_WIN_HEIGHT (bsp));
367
  gtk_preview_set_expand (GTK_PREVIEW (bsp->preview), TRUE);
Elliot Lee's avatar
Elliot Lee committed
368
  gtk_widget_set_events (bsp->preview, BRUSH_EVENT_MASK);
369

Elliot Lee's avatar
Elliot Lee committed
370
  gtk_signal_connect (GTK_OBJECT (bsp->preview), "event",
371
		      GTK_SIGNAL_FUNC (brush_select_events),
Elliot Lee's avatar
Elliot Lee committed
372
		      bsp);
373
  gtk_signal_connect (GTK_OBJECT(bsp->preview), "size_allocate",
374
		      GTK_SIGNAL_FUNC (brush_select_resize),
375
		      bsp);
Elliot Lee's avatar
Elliot Lee committed
376

Michael Natterer's avatar
Michael Natterer committed
377
  /*  dnd stuff  */
378
379
380
381
382
383
  gtk_drag_source_set (bsp->preview,
		       GDK_BUTTON2_MASK,
		       preview_target_table, preview_n_targets,
		       GDK_ACTION_COPY);
  gimp_dnd_brush_source_set (bsp->preview, brush_select_drag_brush, bsp);

Michael Natterer's avatar
Michael Natterer committed
384
385
386
387
388
389
  gtk_drag_dest_set (bsp->preview,
                     GTK_DEST_DEFAULT_ALL,
                     preview_target_table, preview_n_targets,
                     GDK_ACTION_COPY);
  gimp_dnd_brush_dest_set (bsp->preview, brush_select_drop_brush, bsp);

390
  gtk_container_add (GTK_CONTAINER (frame), bsp->preview);
Elliot Lee's avatar
Elliot Lee committed
391
392
393
  gtk_widget_show (bsp->preview);

  gtk_widget_show (sbar);
394
  gtk_widget_show (frame);
395
396
  gtk_widget_show (bsp->brush_selection_box);
  gtk_widget_show (bsp->left_box);
Elliot Lee's avatar
Elliot Lee committed
397

398
  /*  Options box  */
399
400
  bsp->options_box = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), bsp->options_box, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
401
402

  /*  Create the active brush label  */
403
  util_box = gtk_hbox_new (FALSE, 0);
404
  gtk_box_pack_start (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 2);
Elliot Lee's avatar
Elliot Lee committed
405

406
  bsp->brush_name = gtk_label_new (_("No Brushes available"));
407
  gtk_box_pack_start (GTK_BOX (util_box), bsp->brush_name, FALSE, FALSE, 4);
408
  bsp->brush_size = gtk_label_new ("(0 x 0)");
Elliot Lee's avatar
Elliot Lee committed
409
410
411
412
413
414
  gtk_box_pack_start (GTK_BOX (util_box), bsp->brush_size, FALSE, FALSE, 2);

  gtk_widget_show (bsp->brush_name);
  gtk_widget_show (bsp->brush_size);
  gtk_widget_show (util_box);

415
416
417
  /*  A place holder for paint mode switching  */
  bsp->right_box = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (bsp->options_box), bsp->right_box, TRUE, TRUE, 0);
Elliot Lee's avatar
Elliot Lee committed
418

419
420
421
422
423
  /*  The vbox for the paint options  */
  bsp->paint_options_box = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (bsp->right_box), bsp->paint_options_box,
		      FALSE, FALSE, 0);

424
  /*  A separator before the paint options  */
425
426
427
428
429
430
431
432
433
  sep = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (bsp->paint_options_box), sep, FALSE, FALSE, 0);
  gtk_widget_show (sep);

  /*  Create the frame and the table for the options  */
  table = gtk_table_new (2, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  gtk_box_pack_start (GTK_BOX (bsp->paint_options_box), table, FALSE, FALSE, 2);
Elliot Lee's avatar
Elliot Lee committed
434
435

  /*  Create the opacity scale widget  */
436
  bsp->opacity_data = 
437
438
439
    GTK_ADJUSTMENT (gtk_adjustment_new
		    (gimp_context_get_opacity (bsp->context) * 100.0,
		     0.0, 100.0, 1.0, 1.0, 0.0));
Elliot Lee's avatar
Elliot Lee committed
440
441
442
443
  slider = gtk_hscale_new (bsp->opacity_data);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_signal_connect (GTK_OBJECT (bsp->opacity_data), "value_changed",
444
445
		      GTK_SIGNAL_FUNC (opacity_scale_update),
		      bsp);
446
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
447
			     _("Opacity:"), 1.0, 1.0,
448
			     slider, 1, FALSE);
Elliot Lee's avatar
Elliot Lee committed
449

450
451
452
453
  /*  Create the paint mode option menu  */
  menu = paint_mode_menu_new (paint_mode_menu_callback, (gpointer) bsp);
  bsp->option_menu = option_menu = gtk_option_menu_new ();
  gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
454
455
  gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu),
			       gimp_context_get_paint_mode (bsp->context));
456
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
457
			     _("Mode:"), 1.0, 0.5,
458
			     option_menu, 1, TRUE);
459
460
461
462

  gtk_widget_show (table);
  gtk_widget_show (bsp->paint_options_box);
  gtk_widget_show (bsp->right_box);
Elliot Lee's avatar
Elliot Lee committed
463

464
  /*  Create the edit/new buttons  */
465
466
  util_box = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_end (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 4);
467

468
469
470
471
472
  button =  gtk_button_new_with_label (_("New"));
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      GTK_SIGNAL_FUNC (brush_select_new_brush_callback),
		      bsp);
  gtk_box_pack_start (GTK_BOX (util_box), button, TRUE, TRUE, 6);
473

474
  bsp->edit_button =  gtk_button_new_with_label (_("Edit"));
475
  gtk_signal_connect (GTK_OBJECT (bsp->edit_button), "clicked",
476
477
		      GTK_SIGNAL_FUNC (brush_select_edit_brush_callback),
		      bsp);
478
479
  gtk_box_pack_start (GTK_BOX (util_box), bsp->edit_button, TRUE, TRUE, 5);

480
  bsp->delete_button =  gtk_button_new_with_label (_("Delete"));
481
  gtk_signal_connect (GTK_OBJECT (bsp->delete_button), "clicked",
482
		      GTK_SIGNAL_FUNC (brush_select_delete_brush_callback),
483
484
485
		      bsp);
  gtk_box_pack_start (GTK_BOX (util_box), bsp->delete_button, TRUE, TRUE, 5);

486
  gtk_widget_show (bsp->edit_button);    
487
  gtk_widget_show (button);
488
  gtk_widget_show (bsp->delete_button);    
489
490
  gtk_widget_show (util_box);

491
492
493
494
495
496
497
498
499
500
  /*  Create the spacing scale widget  */
  table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  gtk_box_pack_end (GTK_BOX (bsp->options_box), table, FALSE, FALSE, 2);

  bsp->spacing_data =
    GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 1.0, 1000.0, 1.0, 1.0, 0.0));
  slider = gtk_hscale_new (bsp->spacing_data);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
501
502
503
504
505
506
  if (title && init_spacing >= 0)
    {
      /*  Use passed spacing instead of brushes default  */
      gtk_adjustment_set_value (GTK_ADJUSTMENT (bsp->spacing_data),
				init_spacing);
    }
507
  gtk_signal_connect (GTK_OBJECT (bsp->spacing_data), "value_changed",
508
509
		      GTK_SIGNAL_FUNC (spacing_scale_update),
		      bsp);
510
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
511
			     _("Spacing:"), 1.0, 1.0,
512
			     slider, 1, FALSE);
513
514
515

  gtk_widget_show (table);

Elliot Lee's avatar
Elliot Lee committed
516
517
518
519
  gtk_widget_show (bsp->options_box);
  gtk_widget_show (hbox);
  gtk_widget_show (vbox);

520
521
522
523
524
525
526
527
528
529
  /*  add callbacks to keep the display area current  */
  gimp_list_foreach (GIMP_LIST (brush_list),
		     (GFunc) connect_signals_to_brush,
		     bsp);
  gtk_signal_connect (GTK_OBJECT (brush_list), "add",
		      GTK_SIGNAL_FUNC (brush_added_callback),
		      bsp);
  gtk_signal_connect (GTK_OBJECT (brush_list), "remove",
		      GTK_SIGNAL_FUNC (brush_removed_callback),
		      bsp);
Elliot Lee's avatar
Elliot Lee committed
530

531
  /*  Only for main dialog  */
532
  if (!title)
533
    {
534
535
536
537
538
      /*  set the preview's size in the callback
      gtk_signal_connect (GTK_OBJECT (bsp->shell), "map",
			  GTK_SIGNAL_FUNC (brush_select_map_callback),
			  bsp);
      */
539
540

      /*  if we are in per-tool paint options mode, hide the paint options  */
541
      brush_select_show_paint_options (bsp, global_paint_options);
542
    }
543

544
  gtk_widget_show (bsp->shell);
545

546
  preview_calc_scrollbar (bsp);
547

548
549
550
551
552
553
554
555
556
  gtk_signal_connect (GTK_OBJECT (bsp->context), "brush_changed",
		      GTK_SIGNAL_FUNC (brush_select_brush_changed),
		      bsp);
  gtk_signal_connect (GTK_OBJECT (bsp->context), "opacity_changed",
		      GTK_SIGNAL_FUNC (brush_select_opacity_changed),
		      bsp);
  gtk_signal_connect (GTK_OBJECT (bsp->context), "paint_mode_changed",
		      GTK_SIGNAL_FUNC (brush_select_paint_mode_changed),
		      bsp);
557

558
559
  if (active)
    brush_select_select (bsp, active);
560

561
562
  /*  Add to active brush dialogs list  */
  brush_active_dialogs = g_slist_append (brush_active_dialogs, bsp);
563

Elliot Lee's avatar
Elliot Lee committed
564
565
566
567
  return bsp;
}

void
568
brush_select_free (BrushSelect *bsp)
Elliot Lee's avatar
Elliot Lee committed
569
{
570
  if (!bsp)
571
    return;
572

573
574
575
576
577
578
579
580
581
  /* remove from active list */
  brush_active_dialogs = g_slist_remove (brush_active_dialogs, bsp);

  gtk_signal_disconnect_by_data (GTK_OBJECT (bsp->context), bsp);

  if (bsp->brush_popup != NULL)
    gtk_widget_destroy (bsp->brush_popup);

  if (bsp->callback_name)
582
    {
583
584
      g_free (bsp->callback_name);
      gtk_object_unref (GTK_OBJECT (bsp->context));
585
    }
Elliot Lee's avatar
Elliot Lee committed
586

587
588
589
590
591
592
  gimp_list_foreach (GIMP_LIST (brush_list),
		     (GFunc) disconnect_signals_from_brush,
		     bsp);
  gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), bsp);

  g_free (bsp);
Elliot Lee's avatar
Elliot Lee committed
593
594
595
}

void
596
brush_select_freeze_all (void)
Elliot Lee's avatar
Elliot Lee committed
597
{
598
599
  BrushSelect *bsp;
  GSList *list;
600

601
602
603
  for (list = brush_active_dialogs; list; list = g_slist_next (list))
    {
      bsp = (BrushSelect *) list->data;
604

605
      bsp->freeze = TRUE;
Elliot Lee's avatar
Elliot Lee committed
606
607
    }
}
Michael Natterer's avatar
Michael Natterer committed
608

609
void
610
brush_select_thaw_all (void)
611
{
612
613
  BrushSelect *bsp;
  GSList *list;
614

615
616
617
  for (list = brush_active_dialogs; list; list = g_slist_next (list))
    {
      bsp = (BrushSelect *) list->data;
618

619
      bsp->freeze = FALSE;
620

621
      preview_calc_scrollbar (bsp);
622
623
624
    }
}

625
void
626
627
brush_select_show_paint_options (BrushSelect *bsp,
				 gboolean     show)
628
629
630
631
632
633
634
635
{
  if ((bsp == NULL) && ((bsp = brush_select_dialog) == NULL))
    return;

  if (show)
    {
      if (! GTK_WIDGET_VISIBLE (bsp->paint_options_box))
	gtk_widget_show (bsp->paint_options_box);
636

637
638
639
640
641
642
      if (bsp->brush_selection_box->parent != bsp->left_box)
	gtk_widget_reparent (bsp->brush_selection_box, bsp->left_box);
      gtk_box_set_child_packing (GTK_BOX (bsp->options_box->parent),
				 bsp->options_box,
				 FALSE, FALSE, 0, GTK_PACK_START);
      gtk_box_set_child_packing (GTK_BOX (bsp->left_box->parent),
643
644
				 bsp->left_box,
				 TRUE, TRUE, 0, GTK_PACK_START);
645
646
647
648
649
650
      gtk_box_set_spacing (GTK_BOX (bsp->left_box->parent), 2);
    }
  else
    {
      if (GTK_WIDGET_VISIBLE (bsp->paint_options_box))
	gtk_widget_hide (bsp->paint_options_box);
651

652
653
      if (bsp->brush_selection_box->parent != bsp->right_box)
	gtk_widget_reparent (bsp->brush_selection_box, bsp->right_box);
654
655
656
      gtk_box_set_child_packing (GTK_BOX (bsp->left_box->parent),
				 bsp->left_box,
				 FALSE, FALSE, 0, GTK_PACK_START);
657
658
659
660
661
662
663
      gtk_box_set_child_packing (GTK_BOX (bsp->options_box->parent),
				 bsp->options_box,
				 TRUE, TRUE, 0, GTK_PACK_START);
      gtk_box_set_spacing (GTK_BOX (bsp->left_box->parent), 0);
    }
}

664
665
/*  call this dialog's PDB callback  */

Michael Natterer's avatar
Michael Natterer committed
666
static void
667
brush_change_callbacks (BrushSelect *bsp,
Michael Natterer's avatar
Michael Natterer committed
668
			gboolean     closing)
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
{
  gchar *name;
  ProcRecord *prec = NULL;
  GimpBrush *brush;
  gint nreturn_vals;
  static gboolean busy = FALSE;

  /* Any procs registered to callback? */
  Argument *return_vals; 

  if (!bsp || !bsp->callback_name || busy)
    return;

  busy = TRUE;
  name = bsp->callback_name;
  brush = gimp_context_get_brush (bsp->context);

  /* If its still registered run it */
  prec = procedural_db_lookup (name);

  if (prec && brush)
    {
      return_vals =
	procedural_db_run_proc (name,
				&nreturn_vals,
Michael Natterer's avatar
Michael Natterer committed
694
695
696
697
698
699
700
				PDB_STRING,    brush->name,
				PDB_FLOAT,     gimp_context_get_opacity (bsp->context),
				PDB_INT32,     bsp->spacing_value,
				PDB_INT32,     (gint) gimp_context_get_paint_mode (bsp->context),
				PDB_INT32,     brush->mask->width,
				PDB_INT32,     brush->mask->height,
				PDB_INT32,     brush->mask->width * brush->mask->height,
701
				PDB_INT8ARRAY, temp_buf_data (brush->mask),
Michael Natterer's avatar
Michael Natterer committed
702
				PDB_INT32,     (gint) closing,
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
				PDB_END);
 
      if (!return_vals || return_vals[0].value.pdb_int != PDB_SUCCESS)
	g_message ("failed to run brush callback function");
      
      procedural_db_destroy_args (return_vals, nreturn_vals);
    }
  busy = FALSE;
}

/* Close active dialogs that no longer have PDB registered for them */

void
brushes_check_dialogs (void)
{
  BrushSelect *bsp;
  GSList *list;
  gchar *name;
  ProcRecord *prec = NULL;

  list = brush_active_dialogs;

  while (list)
    {
      bsp = (BrushSelect *) list->data;
Michael Natterer's avatar
Michael Natterer committed
728
      list = g_slist_next (list);
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748

      name = bsp->callback_name;

      if (name)
	{
	  prec = procedural_db_lookup (name);

	  if (!prec)
	    {
	      /*  Can alter brush_active_dialogs list  */
	      brush_select_close_callback (NULL, bsp);
	    }
	}
    }
}

/*
 *  Local functions
 */

749
750
751
752
753
754
755
756
757
758
759
static GimpBrush *
brush_select_drag_brush (GtkWidget *widget,
			 gpointer   data)
{
  BrushSelect *bsp;

  bsp = (BrushSelect *) data;

  return bsp->dnd_brush;
}

Michael Natterer's avatar
Michael Natterer committed
760
761
762
763
764
765
766
767
768
769
770
771
static void
brush_select_drop_brush (GtkWidget *widget,
			 GimpBrush *brush,
			 gpointer   data)
{
  BrushSelect *bsp;

  bsp = (BrushSelect *) data;

  gimp_context_set_brush (bsp->context, brush);
}

772
static void
773
774
775
brush_select_brush_changed (GimpContext *context,
			    GimpBrush   *brush,
			    BrushSelect *bsp)
776
{
777
  if (brush)
778
779
780
781
782
783
    {
      brush_select_select (bsp, brush);

      if (bsp->callback_name)
	brush_change_callbacks (bsp, FALSE);
    }
784
}
785

786
787
788
789
790
791
static void
brush_select_opacity_changed (GimpContext *context,
			      gdouble      opacity,
			      BrushSelect *bsp)
{
  gtk_signal_handler_block_by_data (GTK_OBJECT (bsp->opacity_data), bsp);
792

793
794
  gtk_adjustment_set_value (GTK_ADJUSTMENT (bsp->opacity_data),
			    opacity * 100.0);
795

796
  gtk_signal_handler_unblock_by_data (GTK_OBJECT (bsp->opacity_data), bsp);
797
798
799

  if (bsp->callback_name)
    brush_change_callbacks (bsp, FALSE);
800
801
}

802
static void
803
804
805
brush_select_paint_mode_changed (GimpContext      *context,
				 LayerModeEffects  paint_mode,
				 BrushSelect      *bsp)
806
{
807
  gtk_option_menu_set_history (GTK_OPTION_MENU (bsp->option_menu), paint_mode);
808
809
810

  if (bsp->callback_name)
    brush_change_callbacks (bsp, FALSE);
811
812
}

813
814
815
816
817
818
static void
brush_select_select (BrushSelect *bsp,
		     GimpBrush   *brush)
{
  gint index;
  gint row, col;
Michael Natterer's avatar
Michael Natterer committed
819
  gint scroll_offset = 0;
820
821
822

  index = gimp_brush_list_get_brush_index (brush_list, brush); 

Michael Natterer's avatar
Michael Natterer committed
823
  if (index < 0)
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
    return;

  if (GIMP_IS_BRUSH_GENERATED (brush))
    {
      gtk_widget_set_sensitive (bsp->edit_button, TRUE);
      gtk_widget_set_sensitive (bsp->delete_button, TRUE);
    }
  else
    {
      gtk_widget_set_sensitive (bsp->edit_button, FALSE);
      gtk_widget_set_sensitive (bsp->delete_button, FALSE);
    }

  update_active_brush_field (bsp);

  row = index / bsp->NUM_BRUSH_COLUMNS;
  col = index - row * (bsp->NUM_BRUSH_COLUMNS);

Michael Natterer's avatar
Michael Natterer committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
  /*  check if the new active brush is already in the preview  */
  if (((row + 1) * bsp->cell_height) >
      (bsp->preview->allocation.height + bsp->scroll_offset))
    {
      scroll_offset = (((row + 1) * bsp->cell_height) -
		       (bsp->scroll_offset + bsp->preview->allocation.height));
    }
  else if ((row * bsp->cell_height) < bsp->scroll_offset)
    {
      scroll_offset = (row * bsp->cell_height) - bsp->scroll_offset;
    }
  else
    {
      brush_select_show_selected (bsp, row, col);
    }

  gtk_adjustment_set_value (bsp->sbar_data, bsp->scroll_offset + scroll_offset);
859
860
861
862
863
}

static void
brush_select_brush_dirty_callback (GimpBrush   *brush,
				   BrushSelect *bsp)
864
{
865
866
867
868
869
870
871
872
873
874
  gint index;

  if (!bsp && bsp->freeze)
    return;

  index = gimp_brush_list_get_brush_index (brush_list, brush);

  display_brush (bsp, brush,
		 index % (bsp->NUM_BRUSH_COLUMNS),
		 index / (bsp->NUM_BRUSH_COLUMNS));
875
876
877
}

static void
878
879
connect_signals_to_brush (GimpBrush   *brush,
			  BrushSelect *bsp)
880
{
881
882
883
  gtk_signal_connect (GTK_OBJECT (brush), "dirty",
		      GTK_SIGNAL_FUNC (brush_select_brush_dirty_callback),
		      bsp);
884
885
886
  gtk_signal_connect (GTK_OBJECT (brush), "rename",
		      GTK_SIGNAL_FUNC (brush_select_brush_dirty_callback),
		      bsp);
887
888
889
}

static void
890
891
disconnect_signals_from_brush (GimpBrush   *brush,
			       BrushSelect *bsp)
892
{
893
894
  if (!GTK_OBJECT_DESTROYED (brush))
    gtk_signal_disconnect_by_data (GTK_OBJECT (brush), bsp);
895
896
897
}

static void
898
brush_added_callback (GimpBrushList *list,
899
900
		      GimpBrush     *brush, 
		      BrushSelect   *bsp)
901
{
902
  connect_signals_to_brush (brush, bsp);
903

904
905
  if (bsp->freeze)
    return;
906

907
  preview_calc_scrollbar (bsp);
908
909
910
}

static void
911
brush_removed_callback (GimpBrushList *list,
912
913
			GimpBrush     *brush,
			BrushSelect   *bsp)
914
{
915
  disconnect_signals_from_brush (brush, bsp);
916

917
918
  if (bsp->freeze)
    return;
919

920
  preview_calc_scrollbar (bsp);
921
922
}

923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
static void
draw_brush_popup (GtkPreview *preview,
		  GimpBrush  *brush,
		  gint        width,
		  gint        height)
{
  gint x, y;
  gint brush_width, brush_height;
  gint offset_x, offset_y;
  guchar *mask, *buf, *b;
  guchar bg;
  
  brush_width = brush->mask->width;
  brush_height = brush->mask->height;
  offset_x = (width - brush_width) / 2;
  offset_y = (height - brush_height) / 2;

  mask = temp_buf_data (brush->mask);
  buf = g_new (guchar, 3 * width);
  memset (buf, 255, 3 * width);

  if (GIMP_IS_BRUSH_PIXMAP (brush)) 
    {
      guchar *pixmap = temp_buf_data (GIMP_BRUSH_PIXMAP (brush)->pixmap_mask);

      for (y = 0; y < offset_y; y++)
	gtk_preview_draw_row (preview, buf, 0, y, width); 
      for (y = offset_y; y < brush_height + offset_y; y++)
	{
	  b = buf + 3 * offset_x;
	  for (x = 0; x < brush_width ; x++)
	    {
	      bg = (255 - *mask);
	      *b++ = bg + (*mask * *pixmap++) / 255;
	      *b++ = bg + (*mask * *pixmap++) / 255; 
	      *b++ = bg + (*mask * *pixmap++) / 255;
	      mask++;
	    }
	  gtk_preview_draw_row (preview, buf, 0, y, width); 
	}
      memset (buf, 255, 3 * width);
      for (y = brush_height + offset_y; y < height; y++)
	gtk_preview_draw_row (preview, buf, 0, y, width); 
    }
  else
    {
      for (y = 0; y < offset_y; y++)
	gtk_preview_draw_row (preview, buf, 0, y, width); 
      for (y = offset_y; y < brush_height + offset_y; y++)
	{
	  b = buf + 3 * offset_x;
	  for (x = 0; x < brush_width ; x++)
	    {
	      bg = 255 - *mask++;
	      memset (b, bg, 3);
	      b += 3;
	    }   
	  gtk_preview_draw_row (preview, buf, 0, y, width); 
	}
      memset (buf, 255, 3 * width);
      for (y = brush_height + offset_y; y < height; y++)
	gtk_preview_draw_row (preview, buf, 0, y, width);    
    }

  g_free (buf);
}

990
991
992
993
994
995
typedef struct
{
  BrushSelect *bsp;
  gint         x;
  gint         y;
  GimpBrush   *brush;
996
997
} popup_timeout_args_t;

998
999
1000
1001
static gint
brush_popup_anim_timeout (gpointer data)
{
  popup_timeout_args_t *args = data;
1002
  BrushSelect *bsp = args->bsp;
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
  GimpBrushPipe *pipe;
  GimpBrush *brush;

  if (bsp->brush_popup != NULL && !GTK_WIDGET_VISIBLE (bsp->brush_popup))
    {
      bsp->popup_anim_timeout_tag = 0;
      return (FALSE);
    }

  pipe = GIMP_BRUSH_PIPE (args->brush);
  if (++bsp->popup_pipe_index >= pipe->nbrushes)
    bsp->popup_pipe_index = 0;
  brush = GIMP_BRUSH (pipe->brushes[bsp->popup_pipe_index]);

  draw_brush_popup (GTK_PREVIEW (bsp->brush_preview), brush, args->x, args->y);
  gtk_widget_queue_draw (bsp->brush_preview);

  return (TRUE);
}

1023
1024
static gboolean
brush_popup_timeout (gpointer data)
Elliot Lee's avatar
Elliot Lee committed
1025
{
1026
  popup_timeout_args_t *args = data;
1027
1028
  BrushSelect *bsp = args->bsp;
  GimpBrush *brush = args->brush;
1029
  gint x, y;
Elliot Lee's avatar
Elliot Lee committed
1030
1031
  gint x_org, y_org;
  gint scr_w, scr_h;
1032
  gint width, height;
Elliot Lee's avatar
Elliot Lee committed
1033

1034
1035
1036
  /* timeout has gone off so our tag is now invalid  */
  bsp->popup_timeout_tag = 0;

1037
  /* make sure the popup exists and is not visible */
Elliot Lee's avatar
Elliot Lee committed
1038
1039
1040
1041
1042
  if (bsp->brush_popup == NULL)
    {
      GtkWidget *frame;

      bsp->brush_popup = gtk_window_new (GTK_WINDOW_POPUP);
1043
1044
      gtk_window_set_policy (GTK_WINDOW (bsp->brush_popup),
			     FALSE, FALSE, TRUE);
Elliot Lee's avatar
Elliot Lee committed
1045
1046
1047
1048
      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
      gtk_container_add (GTK_CONTAINER (bsp->brush_popup), frame);
      gtk_widget_show (frame);
1049
      bsp->brush_preview = gtk_preview_new (GTK_PREVIEW_COLOR);
Elliot Lee's avatar
Elliot Lee committed
1050
1051
1052
1053
      gtk_container_add (GTK_CONTAINER (frame), bsp->brush_preview);
      gtk_widget_show (bsp->brush_preview);
    }
  else
1054
1055
1056
    {
      gtk_widget_hide (bsp->brush_popup);
    }
Elliot Lee's avatar
Elliot Lee committed
1057

1058
  /* decide where to put the popup */
1059
1060
  width = brush->mask->width;
  height = brush->mask->height;
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
  if (GIMP_IS_REALLY_A_BRUSH_PIPE (brush))
    {      
      GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
      GimpBrush *tmp_brush;
      gint i;
      
      for (i = 1; i < pipe->nbrushes; i++)
	{
	  tmp_brush = GIMP_BRUSH (pipe->brushes[i]);
	  width = MAX (width, tmp_brush->mask->width);
	  height = MAX (height, tmp_brush->mask->height);
	}
    }
Elliot Lee's avatar
Elliot Lee committed
1074
1075
1076
  gdk_window_get_origin (bsp->preview->window, &x_org, &y_org);
  scr_w = gdk_screen_width ();
  scr_h = gdk_screen_height ();
1077
1078
  x = x_org + args->x - width * 0.5;
  y = y_org + args->y - height * 0.5;
Elliot Lee's avatar
Elliot Lee committed
1079
1080
  x = (x < 0) ? 0 : x;
  y = (y < 0) ? 0 : y;
1081
1082
1083
  x = (x + width > scr_w) ? scr_w - width : x;
  y = (y + height > scr_h) ? scr_h - height : y;
  gtk_preview_size (GTK_PREVIEW (bsp->brush_preview), width, height);
Elliot Lee's avatar
Elliot Lee committed
1084
1085

  gtk_widget_popup (bsp->brush_popup, x, y);
1086

Elliot Lee's avatar
Elliot Lee committed
1087
  /*  Draw the brush preview  */
1088
  draw_brush_popup (GTK_PREVIEW (bsp->brush_preview), brush, width, height);
1089
  gtk_widget_queue_draw (bsp->brush_preview);
1090

1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
  if (GIMP_IS_REALLY_A_BRUSH_PIPE (brush) && bsp->popup_anim_timeout_tag == 0)
    {
      static popup_timeout_args_t timeout_args;
      
      timeout_args.bsp = bsp;
      timeout_args.x = width;
      timeout_args.y = height;
      timeout_args.brush = brush;

      bsp->popup_pipe_index = 0;
1101
1102
      bsp->popup_anim_timeout_tag =
	gtk_timeout_add (300, brush_popup_anim_timeout, &timeout_args);
1103
    }
1104

1105
  return FALSE;  /* don't repeat */
Elliot Lee's avatar
Elliot Lee committed
1106
1107
}

1108
static void
1109
1110
1111
1112
brush_popup_open (BrushSelect *bsp,
		  gint         x,
		  gint         y,
		  GimpBrush   *brush)
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
{
  static popup_timeout_args_t popup_timeout_args;

  /* if we've already got a timeout scheduled, then we complain */
  g_return_if_fail (bsp->popup_timeout_tag == 0);

  popup_timeout_args.bsp = bsp;
  popup_timeout_args.x = x;
  popup_timeout_args.y = y;
  popup_timeout_args.brush = brush;
  bsp->popup_timeout_tag = gtk_timeout_add (POPUP_DELAY_MS,
					    brush_popup_timeout,
					    &popup_timeout_args);
}
Elliot Lee's avatar
Elliot Lee committed
1127
1128

static void
1129
brush_popup_close (BrushSelect *bsp)
Elliot Lee's avatar
Elliot Lee committed
1130
{
1131
1132
1133
1134
  if (bsp->popup_timeout_tag != 0)
    gtk_timeout_remove (bsp->popup_timeout_tag);
  bsp->popup_timeout_tag = 0;

1135
1136
1137
1138
  if (bsp->popup_anim_timeout_tag != 0)
    gtk_timeout_remove (bsp->popup_anim_timeout_tag);
  bsp->popup_anim_timeout_tag = 0;

Elliot Lee's avatar
Elliot Lee committed
1139
1140
1141
  if (bsp->brush_popup != NULL)
    gtk_widget_hide (bsp->brush_popup);
}
1142

Elliot Lee's avatar
Elliot Lee committed
1143
static void
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
display_setup (BrushSelect *bsp)
{
  guchar * buf;
  gint i;

  buf = g_new (guchar, 3 * bsp->preview->allocation.width);

  /*  Set the buffer to white  */
  memset (buf, 255, bsp->preview->allocation.width * 3);

  /*  Set the image buffer to white  */
  for (i = 0; i < bsp->preview->allocation.height; i++)
    gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, 0, i,
			  bsp->preview->allocation.width);

  g_free (buf);
}

static void
display_brush (BrushSelect *bsp,
	       GimpBrush   *brush,
	       gint         col,
	       gint         row)
Elliot Lee's avatar
Elliot Lee committed
1167
{
1168
1169
1170
  TempBuf *mask_buf, *pixmap_buf = NULL;
  guchar *mask, *buf, *b;
  guchar bg;
1171
  gboolean scale = FALSE;
1172
  gint cell_width, cell_height;
1173
1174
1175
1176
1177
  gint width, height;
  gint offset_x, offset_y;
  gint yend;
  gint ystart;
  gint i, j;
Elliot Lee's avatar
Elliot Lee committed
1178

1179
1180
  cell_width  = bsp->cell_width  - 2 * MARGIN_WIDTH;
  cell_height = bsp->cell_height - 2 * MARGIN_HEIGHT;
1181

1182
  mask_buf = brush->mask;
1183
  if (GIMP_IS_BRUSH_PIXMAP (brush))
1184
    pixmap_buf = GIMP_BRUSH_PIXMAP (brush)->pixmap_mask;
Elliot Lee's avatar
Elliot Lee committed
1185

1186
  if (mask_buf->width > cell_width || mask_buf->height > cell_height)
1187
    {
1188
1189
      gdouble ratio_x = (gdouble)mask_buf->width / cell_width;
      gdouble ratio_y = (gdouble)mask_buf->height / cell_height;
1190
   
1191
      mask_buf = brush_scale_mask (mask_buf,