brush-select.c 28.8 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
19
20
21
22
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "appenv.h"
#include "actionarea.h"
23
#include "gimpbrushlist.h"
24
#include "gimplist.h"
25
26
27
#include "gimpbrushgenerated.h"
#include "brush_edit.h"
#include "brush_select.h"
Elliot Lee's avatar
Elliot Lee committed
28
29
30
31
32
33
#include "brush_select.h"
#include "buildmenu.h"
#include "colormaps.h"
#include "disp_callbacks.h"
#include "errors.h"
#include "paint_funcs.h"
34
#include "session.h"
Elliot Lee's avatar
Elliot Lee committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


#define STD_CELL_WIDTH    24
#define STD_CELL_HEIGHT   24

#define STD_BRUSH_COLUMNS 5
#define STD_BRUSH_ROWS    5

#define MAX_WIN_WIDTH     (STD_CELL_WIDTH * NUM_BRUSH_COLUMNS)
#define MAX_WIN_HEIGHT    (STD_CELL_HEIGHT * NUM_BRUSH_ROWS)
#define MARGIN_WIDTH      3
#define MARGIN_HEIGHT     3

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

/*  local function prototypes  */
55
static void brush_popup_open             (BrushSelectP, int, int, GimpBrushP);
Elliot Lee's avatar
Elliot Lee committed
56
static void brush_popup_close            (BrushSelectP);
57
static void display_brush                (BrushSelectP, GimpBrushP, int, int);
Elliot Lee's avatar
Elliot Lee committed
58
59
60
61
62
static void display_brushes              (BrushSelectP);
static void display_setup                (BrushSelectP);
static void preview_calc_scrollbar       (BrushSelectP);
static void brush_select_show_selected   (BrushSelectP, int, int);
static void update_active_brush_field    (BrushSelectP);
63
64
65
66
static gint edit_brush_callback		(GtkWidget *w, GdkEvent *e,
					 gpointer data);
static gint new_brush_callback		(GtkWidget *w, GdkEvent *e,
					 gpointer data);
67
68
69
70
71
72
73
74
75
76
77
78
static gint brush_select_brush_dirty_callback(GimpBrushP brush,
					      BrushSelectP bsp);
static void connect_signals_to_brush    (GimpBrushP brush, 
					 BrushSelectP bsp);
static void disconnect_signals_from_brush(GimpBrushP brush,
					  BrushSelectP bsp);
static void brush_added_callback     (GimpBrushList *list,
				      GimpBrushP brush,
				      BrushSelectP bsp);
static void brush_removed_callback   (GimpBrushList *list,
				      GimpBrushP brush,
				      BrushSelectP bsp);
Elliot Lee's avatar
Elliot Lee committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
static void brush_select_close_callback  (GtkWidget *, gpointer);
static void brush_select_refresh_callback(GtkWidget *, gpointer);
static void paint_mode_menu_callback     (GtkWidget *, gpointer);
static gint brush_select_events          (GtkWidget *, GdkEvent *, BrushSelectP);
static gint brush_select_resize		 (GtkWidget *, GdkEvent *, BrushSelectP);

static gint brush_select_delete_callback        (GtkWidget *, GdkEvent *, gpointer);
static void preview_scroll_update        (GtkAdjustment *, gpointer);
static void opacity_scale_update         (GtkAdjustment *, gpointer);
static void spacing_scale_update         (GtkAdjustment *, gpointer);

/*  the option menu items -- the paint modes  */
static MenuItem option_items[] =
{
  { "Normal", 0, 0, paint_mode_menu_callback, (gpointer) NORMAL_MODE, NULL, NULL },
  { "Dissolve", 0, 0, paint_mode_menu_callback, (gpointer) DISSOLVE_MODE, NULL, NULL },
  { "Behind", 0, 0, paint_mode_menu_callback, (gpointer) BEHIND_MODE, NULL, NULL },
Manish Singh's avatar
Manish Singh committed
96
97
  { "Multiply (Burn)", 0, 0, paint_mode_menu_callback, (gpointer) MULTIPLY_MODE, NULL, NULL },
  { "Divide (Dodge)", 0, 0, paint_mode_menu_callback, (gpointer) DIVIDE_MODE, NULL, NULL },
Elliot Lee's avatar
Elliot Lee committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  { "Screen", 0, 0, paint_mode_menu_callback, (gpointer) SCREEN_MODE, NULL, NULL },
  { "Overlay", 0, 0, paint_mode_menu_callback, (gpointer) OVERLAY_MODE, NULL, NULL },
  { "Difference", 0, 0, paint_mode_menu_callback, (gpointer) DIFFERENCE_MODE, NULL, NULL },
  { "Addition", 0, 0, paint_mode_menu_callback, (gpointer) ADDITION_MODE, NULL, NULL },
  { "Subtract", 0, 0, paint_mode_menu_callback, (gpointer) SUBTRACT_MODE, NULL, NULL },
  { "Darken Only", 0, 0, paint_mode_menu_callback, (gpointer) DARKEN_ONLY_MODE, NULL, NULL },
  { "Lighten Only", 0, 0, paint_mode_menu_callback, (gpointer) LIGHTEN_ONLY_MODE, NULL, NULL },
  { "Hue", 0, 0, paint_mode_menu_callback, (gpointer) HUE_MODE, NULL, NULL },
  { "Saturation", 0, 0, paint_mode_menu_callback, (gpointer) SATURATION_MODE, NULL, NULL },
  { "Color", 0, 0, paint_mode_menu_callback, (gpointer) COLOR_MODE, NULL, NULL },
  { "Value", 0, 0, paint_mode_menu_callback, (gpointer) VALUE_MODE, NULL, NULL },
  { NULL, 0, 0, NULL, NULL, NULL, NULL }
};

/*  the action area structure  */
static ActionAreaItem action_items[] =
{
  { "Close", brush_select_close_callback, NULL, NULL },
116
  { "Refresh", brush_select_refresh_callback, NULL, NULL }
Elliot Lee's avatar
Elliot Lee committed
117
118
119
120
121
};

static double old_opacity;
static int old_spacing;
static int old_paint_mode;
122
static BrushEditGeneratedWindow *brush_edit_generated_dialog;
Elliot Lee's avatar
Elliot Lee committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

int NUM_BRUSH_COLUMNS=5;
int NUM_BRUSH_ROWS=5;


GtkWidget *
create_paint_mode_menu (MenuItemCallback callback)
{
  GtkWidget *menu;
  int i;

  for (i = 0; i <= VALUE_MODE; i++)
    option_items[i].callback = callback;

  menu = build_menu (option_items, NULL);

  return menu;
}


BrushSelectP
brush_select_new ()
{
  BrushSelectP bsp;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *sbar;
  GtkWidget *label;
  GtkWidget *menu;
  GtkWidget *util_box;
  GtkWidget *option_menu;
  GtkWidget *slider;
155
156
  GimpBrushP active;
  GtkWidget *button1, *button2;
Elliot Lee's avatar
Elliot Lee committed
157
158
159

  bsp = g_malloc (sizeof (_BrushSelect));
  bsp->redraw = TRUE;
160
  bsp->scroll_offset = 0;
Elliot Lee's avatar
Elliot Lee committed
161
162
163
164
  bsp->brush_popup = NULL;

  /*  The shell and main vbox  */
  bsp->shell = gtk_dialog_new ();
165
  gtk_window_set_wmclass (GTK_WINDOW (bsp->shell), "brushselection", "Gimp");
Elliot Lee's avatar
Elliot Lee committed
166
  gtk_window_set_title (GTK_WINDOW (bsp->shell), "Brush Selection");
167
  session_set_window_geometry (bsp->shell, &brush_select_session_info, TRUE);
168
  gtk_window_set_policy(GTK_WINDOW(bsp->shell), FALSE, TRUE, FALSE);
Elliot Lee's avatar
Elliot Lee committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  vbox = gtk_vbox_new (FALSE, 1);
  gtk_container_border_width (GTK_CONTAINER (vbox), 2);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (bsp->shell)->vbox), vbox, TRUE, TRUE, 0);

  /* handle the wm close signal */
  gtk_signal_connect (GTK_OBJECT (bsp->shell), "delete_event",
		      GTK_SIGNAL_FUNC (brush_select_delete_callback),
		      bsp);

  /*  The horizontal box containing preview & scrollbar & options box */
  hbox = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  bsp->frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (bsp->frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (hbox), bsp->frame, TRUE, TRUE, 0);
  bsp->sbar_data = GTK_ADJUSTMENT (gtk_adjustment_new (0, 0, MAX_WIN_HEIGHT, 1, 1, MAX_WIN_HEIGHT));
  sbar = gtk_vscrollbar_new (bsp->sbar_data);
  gtk_signal_connect (GTK_OBJECT (bsp->sbar_data), "value_changed",
		      (GtkSignalFunc) preview_scroll_update, bsp);
  gtk_box_pack_start (GTK_BOX (hbox), sbar, FALSE, FALSE, 0);


  /*  Create the brush preview window and the underlying image  */
  /*  Get the maximum brush extents  */

  bsp->cell_width = STD_CELL_WIDTH;
  bsp->cell_height = STD_CELL_HEIGHT;

  bsp->width = MAX_WIN_WIDTH;
  bsp->height = MAX_WIN_HEIGHT;

  bsp->preview = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
  gtk_preview_size (GTK_PREVIEW (bsp->preview), bsp->width, bsp->height);
  gtk_widget_set_events (bsp->preview, BRUSH_EVENT_MASK);
  gtk_signal_connect (GTK_OBJECT (bsp->preview), "event",
		      (GtkSignalFunc) brush_select_events,
		      bsp);
  gtk_signal_connect_after (GTK_OBJECT(bsp->frame), "size_allocate",
		       (GtkSignalFunc) brush_select_resize,
		       bsp);

  gtk_container_add (GTK_CONTAINER (bsp->frame), bsp->preview);
  gtk_widget_show (bsp->preview);

  gtk_widget_show (sbar);
  gtk_widget_show (bsp->frame);

  /*  options box  */
  bsp->options_box = gtk_vbox_new (TRUE, 4);
  gtk_box_pack_start (GTK_BOX (hbox), bsp->options_box, TRUE, TRUE, 0);

  /*  Create the active brush label  */
  util_box = gtk_hbox_new (FALSE, 5);
  gtk_box_pack_start (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 0);

  bsp->brush_name = gtk_label_new ("Active");
  gtk_box_pack_start (GTK_BOX (util_box), bsp->brush_name, FALSE, FALSE, 2);
  bsp->brush_size = gtk_label_new ("(0x0)");
  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);

  /*  Create the paint mode option menu  */
234
  old_paint_mode = gimp_brush_get_paint_mode ();
Elliot Lee's avatar
Elliot Lee committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

  util_box = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 0);
  label = gtk_label_new ("Mode:");
  gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
  menu = create_paint_mode_menu (paint_mode_menu_callback);
  option_menu = gtk_option_menu_new ();
  gtk_box_pack_start (GTK_BOX (util_box), option_menu, FALSE, FALSE, 2);

  gtk_widget_show (label);
  gtk_widget_show (option_menu);
  gtk_widget_show (util_box);
  gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);

  /*  Create the opacity scale widget  */
250
  old_opacity = gimp_brush_get_opacity ();
Elliot Lee's avatar
Elliot Lee committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

  util_box = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 0);
  label = gtk_label_new ("Opacity:");
  gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
  bsp->opacity_data = GTK_ADJUSTMENT (gtk_adjustment_new (100.0, 0.0, 100.0, 1.0, 1.0, 0.0));
  slider = gtk_hscale_new (bsp->opacity_data);
  gtk_box_pack_start (GTK_BOX (util_box), slider, TRUE, TRUE, 0);
  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",
		      (GtkSignalFunc) opacity_scale_update, bsp);

  gtk_widget_show (label);
  gtk_widget_show (slider);
  gtk_widget_show (util_box);

  /*  Create the brush spacing scale widget  */
269
  old_spacing = gimp_brush_get_spacing ();
Elliot Lee's avatar
Elliot Lee committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

  util_box = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 0);
  label = gtk_label_new ("Spacing:");
  gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
  bsp->spacing_data = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 1000.0, 1.0, 1.0, 0.0));
  slider = gtk_hscale_new (bsp->spacing_data);
  gtk_box_pack_start (GTK_BOX (util_box), slider, TRUE, TRUE, 0);
  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->spacing_data), "value_changed",
		      (GtkSignalFunc) spacing_scale_update, bsp);

  gtk_widget_show (label);
  gtk_widget_show (slider);
  gtk_widget_show (util_box);

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  /*  Create the edit/new buttons  */
  util_box = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (bsp->options_box), util_box, FALSE, FALSE, 0);
  button1 =  gtk_button_new_with_label ("Edit Brush");
  gtk_signal_connect (GTK_OBJECT (button1), "clicked",
		      (GtkSignalFunc) edit_brush_callback,
		      NULL);
  gtk_box_pack_start (GTK_BOX (util_box), button1, TRUE, TRUE, 5);

  button2 =  gtk_button_new_with_label ("New Brush");
  gtk_signal_connect (GTK_OBJECT (button2), "clicked",
		      (GtkSignalFunc) new_brush_callback,
		      NULL);
  gtk_box_pack_start (GTK_BOX (util_box), button2, TRUE, TRUE, 5);

  gtk_widget_show (button1);
  gtk_widget_show (button2);
  gtk_widget_show (util_box);

Elliot Lee's avatar
Elliot Lee committed
306
307
308
309
310
311
312
313
314
315
316
  /*  The action area  */
  action_items[0].user_data = bsp;
  action_items[1].user_data = bsp;
  build_action_area (GTK_DIALOG (bsp->shell), action_items, 2, 0);

  gtk_widget_show (bsp->options_box);
  gtk_widget_show (hbox);
  gtk_widget_show (vbox);
  gtk_widget_show (bsp->shell);

  /* calculate the scrollbar */
317
318
319
  if(no_data)
    brushes_init(FALSE);
  /* This is done by size_allocate anyway, which is much better */
Elliot Lee's avatar
Elliot Lee committed
320
321
  preview_calc_scrollbar (bsp);

322

Elliot Lee's avatar
Elliot Lee committed
323
324
325
  /*  render the brushes into the newly created image structure  */
  display_brushes (bsp);

326
  /*  add callbacks to keep the display area current  */
327
328
  gimp_list_foreach(GIMP_LIST(brush_list), (GFunc)connect_signals_to_brush,
		    bsp);
329
330
331
332
333
334
335
  gtk_signal_connect (GTK_OBJECT (brush_list), "add",
		      (GtkSignalFunc) brush_added_callback,
		      bsp);
  gtk_signal_connect (GTK_OBJECT (brush_list), "remove",
		      (GtkSignalFunc) brush_removed_callback,
		      bsp);
  
Elliot Lee's avatar
Elliot Lee committed
336
337
338
339
340
341
  /*  update the active selection  */
  active = get_active_brush ();
  if (active)
    {
      int old_value = bsp->redraw;
      bsp->redraw = FALSE;
342
343
      brush_select_select (bsp, gimp_brush_list_get_brush_index(brush_list, 
								active));
Elliot Lee's avatar
Elliot Lee committed
344
345
346
347
348
349
350
351
352
353
354
355
      bsp->redraw = old_value;
    }

  return bsp;
}


void
brush_select_select (BrushSelectP bsp,
		     int          index)
{
  int row, col;
356
357
  if (index < 0)
    return;
Elliot Lee's avatar
Elliot Lee committed
358
359
360
361
362
363
364
365
366
367
368
369
370
  update_active_brush_field (bsp);
  row = index / NUM_BRUSH_COLUMNS;
  col = index - row * NUM_BRUSH_COLUMNS;

  brush_select_show_selected (bsp, row, col);
}


void
brush_select_free (BrushSelectP bsp)
{
  if (bsp)
    {
371
      session_get_window_info (bsp->shell, &brush_select_session_info);
Elliot Lee's avatar
Elliot Lee committed
372
373
374
375
376
377
      if (bsp->brush_popup != NULL)
	gtk_widget_destroy (bsp->brush_popup);
      g_free (bsp);
    }
}

378
static void
379
380
381
382
383
384
385
386
387
388
389
brush_select_brush_changed(BrushSelectP bsp, GimpBrushP brush)
{
/* TODO: be smarter here and only update the part of the preview
 *       that has changed */
  if (bsp)
  {
    display_brushes(bsp);
    gtk_widget_draw (bsp->preview, NULL);
  }
}

390
static gint
391
392
393
brush_select_brush_dirty_callback(GimpBrushP brush, BrushSelectP bsp)
{
  brush_select_brush_changed(bsp, brush);
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  return TRUE;
}

static void
connect_signals_to_brush(GimpBrushP brush, BrushSelectP bsp)
{
  gtk_signal_connect(GTK_OBJECT (brush), "dirty",
		     GTK_SIGNAL_FUNC(brush_select_brush_dirty_callback),
		     bsp);
}

static void
disconnect_signals_from_brush(GimpBrushP brush, BrushSelectP bsp)
{
408
409
  if (!GTK_OBJECT_DESTROYED(brush))
    gtk_signal_disconnect_by_data(GTK_OBJECT(brush), bsp);
410
411
412
413
414
415
416
}

static void
brush_added_callback(GimpBrushList *list, GimpBrushP brush, 
		     BrushSelectP bsp)
{
  connect_signals_to_brush(brush, bsp);
417
  preview_calc_scrollbar(bsp);
418
419
420
421
422
423
424
425
  brush_select_brush_changed(bsp, brush);
}

static void
brush_removed_callback(GimpBrushList *list, GimpBrushP brush,
		       BrushSelectP bsp)
{
  disconnect_signals_from_brush(brush, bsp);
426
  preview_calc_scrollbar(bsp);
427
428
}

Elliot Lee's avatar
Elliot Lee committed
429
430
431
432
433
434
435
436

/*
 *  Local functions
 */
static void
brush_popup_open (BrushSelectP bsp,
		  int          x,
		  int          y,
437
		  GimpBrushP   brush)
Elliot Lee's avatar
Elliot Lee committed
438
439
440
441
442
{
  gint x_org, y_org;
  gint scr_w, scr_h;
  gchar *src, *buf;

443
  /* make sure the popup exists and is not visible */
Elliot Lee's avatar
Elliot Lee committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  if (bsp->brush_popup == NULL)
    {
      GtkWidget *frame;

      bsp->brush_popup = gtk_window_new (GTK_WINDOW_POPUP);
      gtk_window_set_policy (GTK_WINDOW (bsp->brush_popup), FALSE, FALSE, TRUE);

      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);
      bsp->brush_preview = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
      gtk_container_add (GTK_CONTAINER (frame), bsp->brush_preview);
      gtk_widget_show (bsp->brush_preview);
    }
  else
460
461
462
    {
      gtk_widget_hide (bsp->brush_popup);
    }
Elliot Lee's avatar
Elliot Lee committed
463

464
  /* decide where to put the popup */
Elliot Lee's avatar
Elliot Lee committed
465
466
467
468
469
470
471
472
473
474
475
476
  gdk_window_get_origin (bsp->preview->window, &x_org, &y_org);
  scr_w = gdk_screen_width ();
  scr_h = gdk_screen_height ();
  x = x_org + x - brush->mask->width * 0.5;
  y = y_org + y - brush->mask->height * 0.5;
  x = (x < 0) ? 0 : x;
  y = (y < 0) ? 0 : y;
  x = (x + brush->mask->width > scr_w) ? scr_w - brush->mask->width : x;
  y = (y + brush->mask->height > scr_h) ? scr_h - brush->mask->height : y;
  gtk_preview_size (GTK_PREVIEW (bsp->brush_preview), brush->mask->width, brush->mask->height);

  gtk_widget_popup (bsp->brush_popup, x, y);
477
  
Elliot Lee's avatar
Elliot Lee committed
478
479
  /*  Draw the brush  */
  buf = g_new (gchar, brush->mask->width);
480
  src = (gchar *)temp_buf_data (brush->mask);
Elliot Lee's avatar
Elliot Lee committed
481
482
483
484
485
486
487
488
489
  for (y = 0; y < brush->mask->height; y++)
    {
      /*  Invert the mask for display.  We're doing this because
       *  a value of 255 in the  mask means it is full intensity.
       *  However, it makes more sense for full intensity to show
       *  up as black in this brush preview window...
       */
      for (x = 0; x < brush->mask->width; x++)
	buf[x] = 255 - src[x];
490
      gtk_preview_draw_row (GTK_PREVIEW (bsp->brush_preview), (guchar *)buf, 0, y, brush->mask->width);
Elliot Lee's avatar
Elliot Lee committed
491
492
      src += brush->mask->width;
    }
493
494
  g_free(buf);
  
Elliot Lee's avatar
Elliot Lee committed
495
496
497
498
499
500
501
502
503
504
505
  /*  Draw the brush preview  */
  gtk_widget_draw (bsp->brush_preview, NULL);
}


static void
brush_popup_close (BrushSelectP bsp)
{
  if (bsp->brush_popup != NULL)
    gtk_widget_hide (bsp->brush_popup);
}
506

Elliot Lee's avatar
Elliot Lee committed
507
508
static void
display_brush (BrushSelectP bsp,
509
	       GimpBrushP    brush,
Elliot Lee's avatar
Elliot Lee committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
	       int          col,
	       int          row)
{
  TempBuf * brush_buf;
  unsigned char * src, *s;
  unsigned char * buf, *b;
  int width, height;
  int offset_x, offset_y;
  int yend;
  int ystart;
  int i, j;

  buf = (unsigned char *) g_malloc (sizeof (char) * bsp->cell_width);

  brush_buf = brush->mask;

  /*  calculate the offset into the image  */
  width = (brush_buf->width > bsp->cell_width) ? bsp->cell_width :
    brush_buf->width;
  height = (brush_buf->height > bsp->cell_height) ? bsp->cell_height :
    brush_buf->height;

  offset_x = col * bsp->cell_width + ((bsp->cell_width - width) >> 1);
  offset_y = row * bsp->cell_height + ((bsp->cell_height - height) >> 1)
    - bsp->scroll_offset;

  ystart = BOUNDS (offset_y, 0, bsp->preview->requisition.height);
  yend = BOUNDS (offset_y + height, 0, bsp->preview->requisition.height);

  /*  Get the pointer into the brush mask data  */
  src = mask_buf_data (brush_buf) + (ystart - offset_y) * brush_buf->width;

  for (i = ystart; i < yend; i++)
    {
      /*  Invert the mask for display.  We're doing this because
       *  a value of 255 in the  mask means it is full intensity.
       *  However, it makes more sense for full intensity to show
       *  up as black in this brush preview window...
       */
      s = src;
      b = buf;
      for (j = 0; j < width; j++)
	*b++ = 255 - *s++;

      gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x, i, width);

      src += brush_buf->width;
    }

  g_free (buf);
}


static void
display_setup (BrushSelectP bsp)
{
  unsigned char * buf;
  int i;

  buf = (unsigned char *) g_malloc (sizeof (char) * bsp->preview->requisition.width);

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

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

  g_free (buf);
}


582
583
584
585
586
587
588
589
static int brush_counter = 0;
static void do_display_brush (GimpBrush *brush, BrushSelectP bsp)
{
  display_brush (bsp, brush, brush_counter % NUM_BRUSH_COLUMNS,
		 brush_counter / NUM_BRUSH_COLUMNS);
  brush_counter++;
}

Elliot Lee's avatar
Elliot Lee committed
590
591
592
593
static void
display_brushes (BrushSelectP bsp)
{
  /*  If there are no brushes, insensitize widgets  */
594
595
596
597
598
  if (brush_list == NULL || gimp_brush_list_length(brush_list) == 0)
  {
    gtk_widget_set_sensitive (bsp->options_box, FALSE);
    return;
  }
Elliot Lee's avatar
Elliot Lee committed
599
600
601
602
603
604
  /*  Else, sensitize widgets  */
  else
    gtk_widget_set_sensitive (bsp->options_box, TRUE);

  /*  setup the display area  */
  display_setup (bsp);
605
606
  brush_counter = 0;
  gimp_list_foreach(GIMP_LIST(brush_list), (GFunc)do_display_brush, bsp);
Elliot Lee's avatar
Elliot Lee committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
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
693
694
695
696
697
698
699
700
701
}


static void
brush_select_show_selected (BrushSelectP bsp,
			    int          row,
			    int          col)
{
  GdkRectangle area;
  static int old_row = 0;
  static int old_col = 0;
  unsigned char * buf;
  int yend;
  int ystart;
  int offset_x, offset_y;
  int i;

  buf = (unsigned char *) g_malloc (sizeof (char) * bsp->cell_width);

  if (old_col != col || old_row != row)
    {
      /*  remove the old selection  */
      offset_x = old_col * bsp->cell_width;
      offset_y = old_row * bsp->cell_height - bsp->scroll_offset;

      ystart = BOUNDS (offset_y , 0, bsp->preview->requisition.height);
      yend = BOUNDS (offset_y + bsp->cell_height, 0, bsp->preview->requisition.height);

      /*  set the buf to white  */
      memset (buf, 255, bsp->cell_width);

      for (i = ystart; i < yend; i++)
	{
	  if (i == offset_y || i == (offset_y + bsp->cell_height - 1))
	    gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x, i, bsp->cell_width);
	  else
	    {
	      gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x, i, 1);
	      gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x + bsp->cell_width - 1, i, 1);
	    }
	}

      if (bsp->redraw)
	{
	  area.x = offset_x;
	  area.y = ystart;
	  area.width = bsp->cell_width;
	  area.height = (yend - ystart);
	  gtk_widget_draw (bsp->preview, &area);
	}
    }

  /*  make the new selection  */
  offset_x = col * bsp->cell_width;
  offset_y = row * bsp->cell_height - bsp->scroll_offset;

  ystart = BOUNDS (offset_y , 0, bsp->preview->requisition.height);
  yend = BOUNDS (offset_y + bsp->cell_height, 0, bsp->preview->requisition.height);

  /*  set the buf to black  */
  memset (buf, 0, bsp->cell_width);

  for (i = ystart; i < yend; i++)
    {
      if (i == offset_y || i == (offset_y + bsp->cell_height - 1))
	gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x, i, bsp->cell_width);
      else
	{
	  gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x, i, 1);
	  gtk_preview_draw_row (GTK_PREVIEW (bsp->preview), buf, offset_x + bsp->cell_width - 1, i, 1);
	}
    }

  if (bsp->redraw)
    {
      area.x = offset_x;
      area.y = ystart;
      area.width = bsp->cell_width;
      area.height = (yend - ystart);
      gtk_widget_draw (bsp->preview, &area);
    }

  old_row = row;
  old_col = col;

  g_free (buf);
}


static void
preview_calc_scrollbar (BrushSelectP bsp)
{
  int num_rows;
  int page_size;
  int max;
702
  int offs;
Elliot Lee's avatar
Elliot Lee committed
703

704
  offs = bsp->scroll_offset;
705
  num_rows = (gimp_brush_list_length(brush_list) + NUM_BRUSH_COLUMNS - 1)
706
    / NUM_BRUSH_COLUMNS;
Elliot Lee's avatar
Elliot Lee committed
707
708
709
  max = num_rows * bsp->cell_width;
  if (!num_rows) num_rows = 1;
  page_size = bsp->preview->allocation.height;
710
711
712
  page_size = ((page_size < max) ? page_size : max);

  bsp->scroll_offset = offs;
Elliot Lee's avatar
Elliot Lee committed
713
714
  bsp->sbar_data->value = bsp->scroll_offset;
  bsp->sbar_data->upper = max;
715
  bsp->sbar_data->page_size = ((page_size < max) ? page_size : max);
Elliot Lee's avatar
Elliot Lee committed
716
717
718
719
720
721
722
723
724
725
726
727
  bsp->sbar_data->page_increment = (page_size >> 1);
  bsp->sbar_data->step_increment = bsp->cell_width;

  gtk_signal_emit_by_name (GTK_OBJECT (bsp->sbar_data), "changed");
}

static gint 
brush_select_resize (GtkWidget *widget, 
		     GdkEvent *event, 
		     BrushSelectP bsp)
{
   NUM_BRUSH_COLUMNS = (gint)((widget->allocation.width - 4) / STD_CELL_WIDTH);
728
   NUM_BRUSH_ROWS = (gimp_brush_list_length(brush_list) + NUM_BRUSH_COLUMNS - 1) / NUM_BRUSH_COLUMNS;
Elliot Lee's avatar
Elliot Lee committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
   
   bsp->width = widget->allocation.width - 4;
   bsp->height = widget->allocation.height - 4;
 
   gtk_preview_size (GTK_PREVIEW (bsp->preview), bsp->width, bsp->height);
   
   /*  recalculate scrollbar extents  */
   preview_calc_scrollbar (bsp);
 
   /*  render the patterns into the newly created image structure  */
   display_brushes (bsp);
 
   /*  update the display  */   
   if (bsp->redraw)
	gtk_widget_draw (bsp->preview, NULL);
   
   return FALSE;
}
 
static void
update_active_brush_field (BrushSelectP bsp)
{
751
  GimpBrushP brush;
Elliot Lee's avatar
Elliot Lee committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
  char buf[32];

  brush = get_active_brush ();

  if (!brush)
    return;

  /*  Set brush name  */
  gtk_label_set (GTK_LABEL (bsp->brush_name), brush->name);

  /*  Set brush size  */
  sprintf (buf, "(%d X %d)", brush->mask->width, brush->mask->height);
  gtk_label_set (GTK_LABEL (bsp->brush_size), buf);

  /*  Set brush spacing  */
767
  bsp->spacing_data->value = gimp_brush_get_spacing ();
Elliot Lee's avatar
Elliot Lee committed
768
769
770
771
772
773
774
775
776
777
  gtk_signal_emit_by_name (GTK_OBJECT (bsp->spacing_data), "value_changed");
}


static gint
brush_select_events (GtkWidget    *widget,
		     GdkEvent     *event,
		     BrushSelectP  bsp)
{
  GdkEventButton *bevent;
778
  GimpBrushP brush;
Elliot Lee's avatar
Elliot Lee committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
  int row, col, index;

  switch (event->type)
    {
    case GDK_EXPOSE:
      break;

    case GDK_BUTTON_PRESS:
      bevent = (GdkEventButton *) event;

      if (bevent->button == 1)
	{
	  col = bevent->x / bsp->cell_width;
	  row = (bevent->y + bsp->scroll_offset) / bsp->cell_height;
	  index = row * NUM_BRUSH_COLUMNS + col;

	  /*  Get the brush and display the popup brush preview  */
796
	  if ((brush = gimp_brush_list_get_brush_by_index (brush_list, index)))
Elliot Lee's avatar
Elliot Lee committed
797
798
799
800
801
802
803
804
805
	    {
	      gdk_pointer_grab (bsp->preview->window, FALSE,
				(GDK_POINTER_MOTION_HINT_MASK |
				 GDK_BUTTON1_MOTION_MASK |
				 GDK_BUTTON_RELEASE_MASK),
				NULL, NULL, bevent->time);

	      /*  Make this brush the active brush  */
	      select_brush (brush);
806
807
808
809
	      if (brush_edit_generated_dialog)
		brush_edit_generated_set_brush(brush_edit_generated_dialog,
					       get_active_brush());
	      
Elliot Lee's avatar
Elliot Lee committed
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
	      /*  Show the brush popup window if the brush is too large  */
	      if (brush->mask->width > bsp->cell_width ||
		  brush->mask->height > bsp->cell_height)
		brush_popup_open (bsp, bevent->x, bevent->y, brush);
	    }
	}
      break;

    case GDK_BUTTON_RELEASE:
      bevent = (GdkEventButton *) event;

      if (bevent->button == 1)
	{
	  /*  Ungrab the pointer  */
	  gdk_pointer_ungrab (bevent->time);

	  /*  Close the brush popup window  */
	  brush_popup_close (bsp);
	}
      break;
    case GDK_DELETE:
Manish Singh's avatar
Manish Singh committed
831
      /* g_warning ("test"); */
Elliot Lee's avatar
Elliot Lee committed
832
833
834
835
836
837
838
839
840
      break;

    default:
      break;
    }

  return FALSE;
}

841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
static gint
edit_brush_callback (GtkWidget *w, GdkEvent *e, gpointer data)
{
  if (GIMP_IS_BRUSH_GENERATED(get_active_brush()))
  {
    if (!brush_edit_generated_dialog)
    {
      /*  Create the dialog...  */
      brush_edit_generated_dialog = brush_edit_generated_new();
      brush_edit_generated_set_brush(brush_edit_generated_dialog,
				     get_active_brush());
    }
    else
    {
      /*  Popup the dialog  */
      if (!GTK_WIDGET_VISIBLE (brush_edit_generated_dialog->shell))
	gtk_widget_show (brush_edit_generated_dialog->shell);
      else
	gdk_window_raise(brush_edit_generated_dialog->shell->window);
    }
  }
  else
863
864
    g_message("We are all fresh out of brush editors today,\n"
	      "please write your own or try back tomorrow\n");
865
866
867
868
869
870
  return TRUE;
}

static gint
new_brush_callback (GtkWidget *w, GdkEvent *e, gpointer data)
{
871
872
873
874
  GimpBrushGenerated *brush;
  brush = gimp_brush_generated_new(10, .5, 0.0, 1.0);
  gimp_brush_list_add(brush_list, GIMP_BRUSH(brush));
  select_brush(GIMP_BRUSH(brush));
875
876
877
  return TRUE;
}

Elliot Lee's avatar
Elliot Lee committed
878
879
880
881
882
static gint
brush_select_delete_callback (GtkWidget *w, GdkEvent *e, gpointer data)
{
  brush_select_close_callback (w, data);

883
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
884
885
886
887
888
889
890
891
892
893
}

static void
brush_select_close_callback (GtkWidget *w,
			     gpointer   client_data)
{
  BrushSelectP bsp;

  bsp = (BrushSelectP) client_data;

894
895
896
  old_paint_mode = gimp_brush_get_paint_mode ();
  old_opacity = gimp_brush_get_opacity ();
  old_spacing = gimp_brush_get_spacing ();
Elliot Lee's avatar
Elliot Lee committed
897
898
899
900
901
902
903
904
905
906
907

  if (GTK_WIDGET_VISIBLE (bsp->shell))
    gtk_widget_hide (bsp->shell);
}


static void
brush_select_refresh_callback (GtkWidget *w,
			       gpointer   client_data)
{
  BrushSelectP bsp;
908
  GimpBrushP active;
Elliot Lee's avatar
Elliot Lee committed
909
910
911
912

  bsp = (BrushSelectP) client_data;

  /*  re-init the brush list  */
913
914
915
916
917
  brushes_init(FALSE);

  /*  update the active selection  */
  active = get_active_brush ();
  if (active)
918
919
    brush_select_select (bsp, gimp_brush_list_get_brush_index(brush_list, 
							      active));
Elliot Lee's avatar
Elliot Lee committed
920
921
922
923
924
925
926

  /*  recalculate scrollbar extents  */
  preview_calc_scrollbar (bsp);

  /*  render the brushes into the newly created image structure  */
  display_brushes (bsp);

927
 
Elliot Lee's avatar
Elliot Lee committed
928
929
930
931
932
933
934
935
936
937
938
939

  /*  update the display  */
  if (bsp->redraw)
    gtk_widget_draw (bsp->preview, NULL);
}


static void
preview_scroll_update (GtkAdjustment *adjustment,
		       gpointer       data)
{
  BrushSelectP bsp;
940
  GimpBrushP active;
941
  int row, col, index;
Elliot Lee's avatar
Elliot Lee committed
942
943
944
945
946
947
948
949
950
951
952

  bsp = data;

  if (bsp)
    {
      bsp->scroll_offset = adjustment->value;
      display_brushes (bsp);

      active = get_active_brush ();
      if (active)
	{
953
	  index = gimp_brush_list_get_brush_index(brush_list, active);
954
955
	  if (index < 0)
	    return;
956
957
	  row = index / NUM_BRUSH_COLUMNS;
	  col = index - row * NUM_BRUSH_COLUMNS;
Elliot Lee's avatar
Elliot Lee committed
958
959
960
961
962
963
964
965
966
967
968
969
	  brush_select_show_selected (bsp, row, col);
	}

      if (bsp->redraw)
	gtk_widget_draw (bsp->preview, NULL);
    }
}

static void
paint_mode_menu_callback (GtkWidget *w,
			  gpointer   client_data)
{
970
  gimp_brush_set_paint_mode ((long) client_data);
Elliot Lee's avatar
Elliot Lee committed
971
972
973
974
975
976
977
}


static void
opacity_scale_update (GtkAdjustment *adjustment,
		      gpointer       data)
{
978
  gimp_brush_set_opacity (adjustment->value / 100.0);
Elliot Lee's avatar
Elliot Lee committed
979
980
981
982
983
984
985
}


static void
spacing_scale_update (GtkAdjustment *adjustment,
		      gpointer       data)
{
986
  gimp_brush_set_spacing ((int) adjustment->value);
Elliot Lee's avatar
Elliot Lee committed
987
}