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

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

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

#define STD_BRUSH_COLUMNS 5
#define STD_BRUSH_ROWS    5

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

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

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

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

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

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

71
72
static GimpBrush * brush_select_drag_brush         (GtkWidget        *widget,
						    gpointer          data);
Michael Natterer's avatar
Michael Natterer committed
73
74
75
static void     brush_select_drop_brush            (GtkWidget        *widget,
						    GimpBrush        *brush,
						    gpointer          data);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
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
140
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
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]));

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

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

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

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

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
213
214
215
216
217
218
219
220
221
      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;

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

227
/*  If title == NULL then it is the main brush dialog  */
228
BrushSelect *
229
230
231
232
233
234
235
236
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
237
{
238
  BrushSelect *bsp;
Elliot Lee's avatar
Elliot Lee committed
239
240
  GtkWidget *vbox;
  GtkWidget *hbox;
241
  GtkWidget *frame;
Elliot Lee's avatar
Elliot Lee committed
242
  GtkWidget *sbar;
243
244
  GtkWidget *sep;
  GtkWidget *table;
Elliot Lee's avatar
Elliot Lee committed
245
246
  GtkWidget *util_box;
  GtkWidget *option_menu;
247
  GtkWidget *menu;
Elliot Lee's avatar
Elliot Lee committed
248
  GtkWidget *slider;
249
  GtkWidget *button;
250

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

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

255
256
  bsp = g_new (BrushSelect, 1);
  bsp->callback_name          = NULL;
257
  bsp->dnd_brush              = NULL;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  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",
				GTK_WIN_POS_NONE,
				FALSE, TRUE, FALSE,

				_("Refresh"), brush_select_refresh_callback,
				bsp, NULL, FALSE, FALSE,
				_("Close"), brush_select_close_callback,
				bsp, NULL, TRUE, TRUE,

				NULL);
283

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

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

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

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

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

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

316
317
318
319
320
321
  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;
322
    }
Elliot Lee's avatar
Elliot Lee committed
323

324
325
326
327
328
  /*  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);

329
  /*  The horizontal box containing the brush list & options box */
Elliot Lee's avatar
Elliot Lee committed
330
  hbox = gtk_hbox_new (FALSE, 2);
331
332
333
334
335
336
  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);

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

341
342
343
  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,
344
		      TRUE, TRUE, 0);
345
346
347
348

  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
349
350
  sbar = gtk_vscrollbar_new (bsp->sbar_data);
  gtk_signal_connect (GTK_OBJECT (bsp->sbar_data), "value_changed",
351
352
		      GTK_SIGNAL_FUNC (brush_select_scroll_update),
		      bsp);
353
  gtk_box_pack_start (GTK_BOX (bsp->brush_selection_box), sbar, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
354
355
356

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

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

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

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

Michael Natterer's avatar
Michael Natterer committed
376
  /*  dnd stuff  */
377
378
379
380
381
382
  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
383
384
385
386
387
388
  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);

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

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

397
  /*  Options box  */
398
399
  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
400
401

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

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

414
415
416
  /*  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
417

418
419
420
421
422
  /*  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);

423
  /*  A separator before the paint options  */
424
425
426
427
428
429
430
431
432
  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
433
434

  /*  Create the opacity scale widget  */
435
  bsp->opacity_data = 
436
437
438
    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
439
440
441
442
  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",
443
444
445
446
447
		      GTK_SIGNAL_FUNC (opacity_scale_update),
		      bsp);
  gimp_table_attach_aligned (GTK_TABLE (table), 0,
			     _("Opacity:"), 1.0, 1.0,
			     slider, FALSE);
Elliot Lee's avatar
Elliot Lee committed
448

449
450
451
452
  /*  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);
453
454
455
456
457
  gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu),
			       gimp_context_get_paint_mode (bsp->context));
  gimp_table_attach_aligned (GTK_TABLE (table), 1,
			     _("Mode:"), 1.0, 0.5,
			     option_menu, TRUE);
458
459
460
461

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

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

467
468
469
470
471
  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);
472

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

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

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

490
491
492
493
494
495
496
497
498
499
  /*  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);
500
501
502
503
504
505
  if (title && init_spacing >= 0)
    {
      /*  Use passed spacing instead of brushes default  */
      gtk_adjustment_set_value (GTK_ADJUSTMENT (bsp->spacing_data),
				init_spacing);
    }
506
  gtk_signal_connect (GTK_OBJECT (bsp->spacing_data), "value_changed",
507
508
509
510
511
		      GTK_SIGNAL_FUNC (spacing_scale_update),
		      bsp);
  gimp_table_attach_aligned (GTK_TABLE (table), 0,
			     _("Spacing:"), 1.0, 1.0,
			     slider, FALSE);
512
513
514

  gtk_widget_show (table);

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

519
520
521
522
523
524
525
526
527
528
  /*  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
529

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

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

543
  gtk_widget_show (bsp->shell);
544

545
  preview_calc_scrollbar (bsp);
546

547
548
549
550
551
552
553
554
555
  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);
556

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

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

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

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

572
573
574
575
576
577
578
579
580
  /* 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)
581
    {
582
583
      g_free (bsp->callback_name);
      gtk_object_unref (GTK_OBJECT (bsp->context));
584
    }
Elliot Lee's avatar
Elliot Lee committed
585

586
587
588
589
590
591
  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
592
593
594
}

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

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

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

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

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

618
      bsp->freeze = FALSE;
619

620
      preview_calc_scrollbar (bsp);
621
622
623
    }
}

624
void
625
626
brush_select_show_paint_options (BrushSelect *bsp,
				 gboolean     show)
627
628
629
630
631
632
633
634
{
  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);
635

636
637
638
639
640
641
      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),
642
643
				 bsp->left_box,
				 TRUE, TRUE, 0, GTK_PACK_START);
644
645
646
647
648
649
      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);
650

651
652
      if (bsp->brush_selection_box->parent != bsp->right_box)
	gtk_widget_reparent (bsp->brush_selection_box, bsp->right_box);
653
654
655
      gtk_box_set_child_packing (GTK_BOX (bsp->left_box->parent),
				 bsp->left_box,
				 FALSE, FALSE, 0, GTK_PACK_START);
656
657
658
659
660
661
662
      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);
    }
}

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

Michael Natterer's avatar
Michael Natterer committed
665
static void
666
brush_change_callbacks (BrushSelect *bsp,
Michael Natterer's avatar
Michael Natterer committed
667
			gboolean     closing)
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
{
  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
693
694
695
696
697
698
699
				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,
700
				PDB_INT8ARRAY, temp_buf_data (brush->mask),
Michael Natterer's avatar
Michael Natterer committed
701
				PDB_INT32,     (gint) closing,
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
				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
727
      list = g_slist_next (list);
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747

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

748
749
750
751
752
753
754
755
756
757
758
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
759
760
761
762
763
764
765
766
767
768
769
770
static void
brush_select_drop_brush (GtkWidget *widget,
			 GimpBrush *brush,
			 gpointer   data)
{
  BrushSelect *bsp;

  bsp = (BrushSelect *) data;

  gimp_context_set_brush (bsp->context, brush);
}

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

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

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

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

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

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

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

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

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

  index = gimp_brush_list_get_brush_index (brush_list, brush); 

Michael Natterer's avatar
Michael Natterer committed
822
  if (index < 0)
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
    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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  /*  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);
858
859
860
861
862
}

static void
brush_select_brush_dirty_callback (GimpBrush   *brush,
				   BrushSelect *bsp)
863
{
864
865
866
867
868
869
870
871
872
873
  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));
874
875
876
}

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

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

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

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

906
  preview_calc_scrollbar (bsp);
907
908
909
}

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

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

919
  preview_calc_scrollbar (bsp);
920
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
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);
}

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

997
998
999
1000
static gint
brush_popup_anim_timeout (gpointer data)
{
  popup_timeout_args_t *args = data;
1001
  BrushSelect *bsp = args->bsp;
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  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);
}

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

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

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

      bsp->brush_popup = gtk_window_new (GTK_WINDOW_POPUP);
1042
1043
      gtk_window_set_policy (GTK_WINDOW (bsp->brush_popup),
			     FALSE, FALSE, TRUE);
Elliot Lee's avatar
Elliot Lee committed
1044
1045
1046
1047
      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);
1048
      bsp->brush_preview = gtk_preview_new (GTK_PREVIEW_COLOR);
Elliot Lee's avatar
Elliot Lee committed
1049
1050
1051
1052
      gtk_container_add (GTK_CONTAINER (frame), bsp->brush_preview);
      gtk_widget_show (bsp->brush_preview);
    }
  else
1053
1054
1055
    {
      gtk_widget_hide (bsp->brush_popup);
    }
Elliot Lee's avatar
Elliot Lee committed
1056

1057
  /* decide where to put the popup */
1058
1059
  width = brush->mask->width;
  height = brush->mask->height;
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
  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
1073
1074
1075
  gdk_window_get_origin (bsp->preview->window, &x_org, &y_org);
  scr_w = gdk_screen_width ();
  scr_h = gdk_screen_height ();
1076
1077
  x = x_org + args->x - width * 0.5;
  y = y_org + args->y - height * 0.5;
Elliot Lee's avatar
Elliot Lee committed
1078
1079
  x = (x < 0) ? 0 : x;
  y = (y < 0) ? 0 : y;
1080
1081
1082
  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
1083
1084

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

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

1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
  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;
1100
1101
      bsp->popup_anim_timeout_tag =
	gtk_timeout_add (300, brush_popup_anim_timeout, &timeout_args);
1102
    }
1103

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

1107
static void
1108
1109
1110
1111
brush_popup_open (BrushSelect *bsp,
		  gint         x,
		  gint         y,
		  GimpBrush   *brush)
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
{
  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
1126
1127

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

1134
1135
1136
1137
  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
1138
1139
1140
  if (bsp->brush_popup != NULL)
    gtk_widget_hide (bsp->brush_popup);
}
1141

Elliot Lee's avatar
Elliot Lee committed
1142
static void
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
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
1166
{
1167
1168
1169
  TempBuf *mask_buf, *pixmap_buf = NULL;
  guchar *mask, *buf, *b;
  guchar bg;
1170
  gboolean scale = FALSE;
1171
  gint cell_width, cell_height;
1172
1173
1174
1175
1176
  gint width, height;
  gint offset_x, offset_y;
  gint yend;
  gint ystart;
  gint i, j;
Elliot Lee's avatar
Elliot Lee committed
1177

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

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

1185
  if (mask_buf->width > cell_width || mask_buf->height > cell_height)
1186
    {
1187
1188
      gdouble ratio_x = (gdouble)mask_buf->width / cell_width;
      gdouble ratio_y = (gdouble)mask_buf->height / cell_height;
1189
   
1190
      mask_buf = brush_scale_mask (mask_buf, 
1191
1192
				   (gdouble)(mask_buf->width) / MAX (ratio_x, ratio_y) + 0.5, 
				   (gdouble)(mask_buf->height) / MAX (ratio_x, ratio_y) + 0.5);
1193
1194
1195
1196
      if (GIMP_IS_BRUSH_PIXMAP (brush))
	{
	  /*  TODO: the scale function should scale the pixmap 
	            and the mask in one run                     */
1197
1198
 	  pixmap_buf = brush_scale_pixmap (pixmap_buf,
					   mask_buf->width, mask_buf->height);
1199
	}
1200
1201
1202
      scale = TRUE;
    }

Elliot Lee's avatar
Elliot Lee committed
1203
  /*  calculate the offset into the image  */
1204
1205
  width = (mask_buf->width > cell_width) ? cell_width : mask_buf->width;
  height = (mask_buf->height > cell_height) ? cell_height : mask_buf->height;
Elliot Lee's avatar
Elliot Lee committed
1206

1207
1208
  offset_x = col * bsp->cell_width + ((cell_width - width) >> 1) + MARGIN_WIDTH;
  offset_y = row * bsp->cell_height + ((cell_height - height) >> 1) - bsp->scroll_offset + MARGIN_HEIGHT;
Elliot Lee's avatar
Elliot Lee committed
1209

1210
  ystart = BOUNDS (offset_y, 0, bsp->preview->allocation.height);
Michael Natterer's avatar