brush_edit.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * brush_edit module Copyright 1998 Jay Cox <jaycox@earthlink.net>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
20 21 22

#include "config.h"

23 24
#include <string.h>

Sven Neumann's avatar
Sven Neumann committed
25 26
#include <gtk/gtk.h>

27
#include "libgimpmath/gimpmath.h"
28
#include "libgimpwidgets/gimpwidgets.h"
29

Sven Neumann's avatar
Sven Neumann committed
30 31
#include "apptypes.h"

32 33
#include "gimpbrushgenerated.h"
#include "brush_edit.h"
34
#include "temp_buf.h"
35

36
#include "libgimp/gimpintl.h"
37

38 39 40 41 42

static void brush_edit_close_callback (GtkWidget                *widget,
				       gpointer                  data);
static gint brush_edit_preview_resize (GtkWidget                *widget,
				       GdkEvent                 *event, 
43
				       BrushEditGeneratedWindow *begw);
44

45

46
static void
47
update_brush_callback (GtkAdjustment            *adjustment,
48 49 50 51
		       BrushEditGeneratedWindow *begw)
{
  if (begw->brush &&
      ((begw->radius_data->value  
52
	!= gimp_brush_generated_get_radius (begw->brush))
53
       || (begw->hardness_data->value
54
	   != gimp_brush_generated_get_hardness (begw->brush))
55
       || (begw->aspect_ratio_data->value
56
	   != gimp_brush_generated_get_aspect_ratio (begw->brush))
57
       || (begw->angle_data->value
58 59 60 61 62 63 64 65 66 67 68 69 70
	   != gimp_brush_generated_get_angle (begw->brush))))
    {
      gimp_brush_generated_freeze (begw->brush);
      gimp_brush_generated_set_radius       (begw->brush,
					     begw->radius_data->value);
      gimp_brush_generated_set_hardness     (begw->brush,
					     begw->hardness_data->value);
      gimp_brush_generated_set_aspect_ratio (begw->brush,
					     begw->aspect_ratio_data->value);
      gimp_brush_generated_set_angle        (begw->brush,
					     begw->angle_data->value);
      gimp_brush_generated_thaw (begw->brush);
    }
71 72
}

73 74 75
static void
brush_edit_clear_preview (BrushEditGeneratedWindow *begw)
{
76 77
  guchar *buf;
  gint    i;
78

79
  buf = g_new (guchar, begw->preview->requisition.width);
80 81 82 83 84 85 86 87 88 89 90 91 92

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

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

  g_free (buf);
}

static gint 
93 94
brush_edit_brush_dirty_callback (GimpBrush                *brush,
				 BrushEditGeneratedWindow *begw)
95
{
96 97
  gint x, y, width, yend, ystart, xo;
  gint scale;
98 99 100
  gchar *src, *buf;

  brush_edit_clear_preview (begw);
101
  if (brush == NULL || brush->mask == NULL)
102
    return TRUE;
103 104 105 106
  scale = MAX (ceil (brush->mask->width/
		     (float) begw->preview->requisition.width),
	       ceil (brush->mask->height/
		     (float) begw->preview->requisition.height));
107 108 109 110 111

  ystart = 0;
  xo = begw->preview->requisition.width/2 - brush->mask->width/(2*scale);
  ystart = begw->preview->requisition.height/2 - brush->mask->height/(2*scale);
  yend = ystart + brush->mask->height/(scale);
112
  width = CLAMP (brush->mask->width/scale, 0, begw->preview->requisition.width);
113 114

  buf = g_new (gchar, width);
115
  src = (gchar *) temp_buf_data (brush->mask);
116 117

  for (y = ystart; y < yend; y++)
118 119 120 121 122 123 124 125 126 127
    {
      /*  Invert the mask for display.
       */
      for (x = 0; x < width; x++)
	buf[x] = 255 - src[x*scale];
      gtk_preview_draw_row (GTK_PREVIEW (begw->preview), (guchar *)buf, xo, y,
			    width);
      src += brush->mask->width*scale;
    }
  g_free (buf);
128
  if (begw->scale != scale)
129 130 131 132 133 134 135 136
    {
      gchar str[255];
      begw->scale = scale;
      g_snprintf (str, sizeof (str), "%d:1", scale);
      gtk_label_set_text (GTK_LABEL (begw->scale_label), str);
      gtk_widget_draw (begw->scale_label, NULL);
    }
  gtk_widget_draw (begw->preview, NULL);
137 138 139
  return TRUE;
}

140
static void
141 142
brush_name_changed_callback (GtkWidget                *widget,
			     BrushEditGeneratedWindow *begw)
143
{
144
  gtk_entry_set_text (GTK_ENTRY (begw->name),
145
		      gimp_object_get_name (GIMP_OBJECT (begw->brush)));
146 147
}

148
void
149 150
brush_edit_generated_set_brush (BrushEditGeneratedWindow *begw,
				GimpBrush                *gbrush)
151
{
152 153 154 155 156
  GimpBrushGenerated *brush = NULL;

  g_return_if_fail (begw != NULL);

  if (begw->brush == (GimpBrushGenerated *) gbrush)
157
    return;
158 159

  if (begw->brush)
160 161 162 163 164
    {
      gtk_signal_disconnect_by_data (GTK_OBJECT (begw->brush), begw);
      gtk_object_unref (GTK_OBJECT (begw->brush));
      begw->brush = NULL;
    }
165

166 167 168 169 170 171 172
  if (!gbrush || !GIMP_IS_BRUSH_GENERATED (gbrush))
    {
      begw->brush = NULL;
      if (GTK_WIDGET_VISIBLE (begw->shell))
	gtk_widget_hide (begw->shell);
      return;
    }
173

174
  brush = GIMP_BRUSH_GENERATED (gbrush);
175 176 177 178

  gtk_signal_connect (GTK_OBJECT (brush), "dirty",
		      GTK_SIGNAL_FUNC (brush_edit_brush_dirty_callback),
		      begw);
179 180
  gtk_signal_connect (GTK_OBJECT (brush), "name_changed",
		      GTK_SIGNAL_FUNC (brush_name_changed_callback),
181 182 183 184 185 186 187 188 189 190 191
		      begw);

  begw->brush = NULL;
  gtk_adjustment_set_value (GTK_ADJUSTMENT (begw->radius_data),
			    gimp_brush_generated_get_radius (brush));
  gtk_adjustment_set_value (GTK_ADJUSTMENT (begw->hardness_data),
			    gimp_brush_generated_get_hardness (brush));
  gtk_adjustment_set_value (GTK_ADJUSTMENT (begw->angle_data),
			    gimp_brush_generated_get_angle (brush));
  gtk_adjustment_set_value (GTK_ADJUSTMENT (begw->aspect_ratio_data),
			    gimp_brush_generated_get_aspect_ratio (brush));
192 193
  gtk_entry_set_text (GTK_ENTRY (begw->name),
		      gimp_object_get_name (GIMP_OBJECT (gbrush)));
194 195 196 197
  begw->brush = brush;

  gtk_object_ref (GTK_OBJECT (begw->brush));
  brush_edit_brush_dirty_callback (GIMP_BRUSH (brush), begw);
198 199
}

200 201 202
void
name_changed_func (GtkWidget                *widget,
		   BrushEditGeneratedWindow *begw)
203 204
{
  gchar *entry_text;
205

206
  entry_text = gtk_entry_get_text (GTK_ENTRY (widget));
207
  gimp_object_set_name (GIMP_OBJECT (begw->brush), entry_text);
208 209
}

210 211 212 213
void
focus_out_func (GtkWidget                *wid1,
		GtkWidget                *wid2,
		BrushEditGeneratedWindow *begw)
214
{
215
  name_changed_func (wid1, begw);
216 217
}

218
BrushEditGeneratedWindow *
219
brush_edit_generated_new (void)
220
{
221
  BrushEditGeneratedWindow *begw;
222 223 224 225
  GtkWidget *vbox;
  GtkWidget *slider;
  GtkWidget *table;

226
  begw = g_new0 (BrushEditGeneratedWindow, 1);
227

228 229
  begw->shell = gimp_dialog_new (_("Brush Editor"), "generated_brush_editor",
				 gimp_standard_help_func,
230
				 "dialogs/brush_editor.html",
231 232
				 GTK_WIN_POS_NONE,
				 FALSE, TRUE, FALSE,
233

234
				 _("Close"), brush_edit_close_callback,
235
				 begw, NULL, NULL, TRUE, TRUE,
236

237
				 NULL);
238 239

  vbox = gtk_vbox_new (FALSE, 1);
240
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
241
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (begw->shell)->vbox), vbox);
242

243
  /* Brush's name */
244 245 246
  begw->name = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), begw->name, FALSE, FALSE, 0);

247
  gtk_signal_connect (GTK_OBJECT (begw->name), "activate",
248
		      GTK_SIGNAL_FUNC (name_changed_func),
249 250
		      begw);
  gtk_signal_connect (GTK_OBJECT (begw->name), "focus_out_event",
251
		      GTK_SIGNAL_FUNC (focus_out_func),
252
		      begw);
253 254

  gtk_widget_show (begw->name);
255

256
  /* brush's preview widget w/frame  */
257 258 259 260 261
  begw->frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (begw->frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (vbox), begw->frame, TRUE, TRUE, 0);

  begw->preview = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
262
  gtk_preview_size (GTK_PREVIEW (begw->preview), 125, 100);
263 264 265
  gtk_signal_connect_after (GTK_OBJECT (begw->frame), "size_allocate",
			    GTK_SIGNAL_FUNC (brush_edit_preview_resize),
			    begw);
266 267
  gtk_container_add (GTK_CONTAINER (begw->frame), begw->preview);

268 269
  gtk_widget_show (begw->preview);
  gtk_widget_show (begw->frame);
270

271 272 273
  /* table for sliders/labels */
  begw->scale_label = gtk_label_new ("-1:1");
  gtk_box_pack_start (GTK_BOX (vbox), begw->scale_label, FALSE, FALSE, 0);
274
  gtk_widget_show (begw->scale_label);
275 276 277

  begw->scale = -1;

278
  /* table for sliders/labels */
279
  table = gtk_table_new (4, 2, FALSE);
280 281
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

282 283 284
  /*  brush radius scale  */
  begw->radius_data =
    GTK_ADJUSTMENT (gtk_adjustment_new (10.0, 0.0, 100.0, 0.1, 1.0, 0.0));
285 286 287 288
  slider = gtk_hscale_new (begw->radius_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 (begw->radius_data), "value_changed",
289 290
		      GTK_SIGNAL_FUNC (update_brush_callback),
		      begw);
291
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
292
			     _("Radius:"), 1.0, 1.0,
293
			     slider, 1, FALSE);
294

295 296 297
  /*  brush hardness scale  */
  begw->hardness_data =
    GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 1.0, 0.01, 0.01, 0.0));
298 299 300 301
  slider = gtk_hscale_new (begw->hardness_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 (begw->hardness_data), "value_changed",
302 303
		      GTK_SIGNAL_FUNC (update_brush_callback),
		      begw);
304
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
305
			     _("Hardness:"), 1.0, 1.0,
306
			     slider, 1, FALSE);
307

308 309 310
  /*  brush aspect ratio scale  */
  begw->aspect_ratio_data =
    GTK_ADJUSTMENT (gtk_adjustment_new (1.0, 1.0, 20.0, 0.1, 1.0, 0.0));
311 312 313 314
  slider = gtk_hscale_new (begw->aspect_ratio_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 (begw->aspect_ratio_data), "value_changed",
315 316
		      GTK_SIGNAL_FUNC (update_brush_callback),
		      begw);
317
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
318
			     _("Aspect Ratio:"), 1.0, 1.0,
319
			     slider, 1, FALSE);
320

321 322 323
  /*  brush angle scale  */
  begw->angle_data =
    GTK_ADJUSTMENT (gtk_adjustment_new (00.0, 0.0, 180.0, 0.1, 1.0, 0.0));
324 325 326 327
  slider = gtk_hscale_new (begw->angle_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 (begw->angle_data), "value_changed",
328 329
		      GTK_SIGNAL_FUNC (update_brush_callback),
		      begw);
330
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
331
			     _("Angle:"), 1.0, 1.0,
332
			     slider, 1, FALSE);
333

334 335
  gtk_table_set_row_spacings (GTK_TABLE (table), 3);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
336 337 338 339 340 341 342
  gtk_widget_show (table);

  gtk_widget_show (vbox);
  gtk_widget_show (begw->shell);

  return begw;
}
343

344
static gint 
345 346
brush_edit_preview_resize (GtkWidget                *widget,
			   GdkEvent                 *event,
347 348
			   BrushEditGeneratedWindow *begw)
{
349 350 351 352 353 354 355 356 357
  gtk_preview_size (GTK_PREVIEW (begw->preview),
		    widget->allocation.width - 4,
		    widget->allocation.height - 4);

  /*  update the display  */
  if (begw->brush)
    brush_edit_brush_dirty_callback (GIMP_BRUSH (begw->brush), begw);

  return FALSE;
358 359
}
 
360
static void
361 362
brush_edit_close_callback (GtkWidget *widget,
			   gpointer   data)
363
{
364 365
  BrushEditGeneratedWindow *begw = (BrushEditGeneratedWindow *) data;

366 367 368
  if (GTK_WIDGET_VISIBLE (begw->shell))
    gtk_widget_hide (begw->shell);
}