gimpink.c 43.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

21
#include <stdlib.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
22
#include <string.h>
23

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

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

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

31 32
#include "paint-funcs/paint-funcs.h"

33
#include "cursorutil.h"
34 35
#include "drawable.h"
#include "gimage_mask.h"
36 37
#include "gimpcontext.h"
#include "gimpimage.h"
38
#include "gimprc.h"
39
#include "undo.h"
40
#include "gdisplay.h"
41 42 43 44
#include "pixel_region.h"
#include "temp_buf.h"
#include "tile.h"
#include "tile_manager.h"
45

46 47 48
#include "gimpinktool.h"
#include "gimpinktool-blob.h"
#include "gimptool.h"
49
#include "paint_options.h"
50
#include "tool_manager.h"
51

52 53
#include "libgimp/gimpintl.h"

54
#include "pixmaps2.h"
55

56

57
#define SUBSAMPLE 8
58

59 60
/*  the Ink structures  */

61 62 63 64 65 66 67
typedef Blob * (* BlobFunc) (gdouble,
			     gdouble,
			     gdouble,
			     gdouble,
			     gdouble,
			     gdouble);

68

69
typedef struct _BrushWidget BrushWidget;
Michael Natterer's avatar
Michael Natterer committed
70

71 72
struct _BrushWidget
{
73 74
  GtkWidget *widget;
  gboolean   state;
75 76
};

77
typedef struct _InkOptions InkOptions;
Michael Natterer's avatar
Michael Natterer committed
78

79 80
struct _InkOptions
{
81
  PaintOptions  paint_options;
82

Michael Natterer's avatar
Michael Natterer committed
83 84
  gdouble       size;
  gdouble       size_d;
85
  GtkObject    *size_w;
86

Michael Natterer's avatar
Michael Natterer committed
87 88
  gdouble       sensitivity;
  gdouble       sensitivity_d;
89
  GtkObject    *sensitivity_w;
90

Michael Natterer's avatar
Michael Natterer committed
91 92
  gdouble       vel_sensitivity;
  gdouble       vel_sensitivity_d;
93
  GtkObject    *vel_sensitivity_w;
94

Michael Natterer's avatar
Michael Natterer committed
95 96
  gdouble       tilt_sensitivity;
  gdouble       tilt_sensitivity_d;
97
  GtkObject    *tilt_sensitivity_w;
98

Michael Natterer's avatar
Michael Natterer committed
99 100
  gdouble       tilt_angle;
  gdouble       tilt_angle_d;
101
  GtkObject    *tilt_angle_w;
102

103 104 105
  BlobFunc      function;
  BlobFunc      function_d;
  GtkWidget    *function_w[3];  /* 3 radio buttons */
106

Michael Natterer's avatar
Michael Natterer committed
107 108 109 110
  gdouble       aspect;
  gdouble       aspect_d;
  gdouble       angle;
  gdouble       angle_d;
111
  BrushWidget  *brush_w;
112 113 114
};


115
/*  local function prototypes  */
116

117 118 119 120 121 122
static void        gimp_ink_tool_class_init      (GimpInkToolClass *klass);
static void        gimp_ink_tool_init            (GimpInkTool      *tool);

static void        gimp_ink_tool_destroy         (GtkObject        *object);

static InkOptions * ink_options_new     (void);
123
static void         ink_options_reset   (ToolOptions    *tool_options);
124 125

static void        ink_button_press     (GimpTool       *tool,
126 127
					 GdkEventButton *mevent,
					 GDisplay       *gdisp);
128
static void        ink_button_release   (GimpTool       *tool,
129 130
					 GdkEventButton *bevent,
					 GDisplay       *gdisp);
131
static void        ink_motion           (GimpTool       *tool,
132 133
					 GdkEventMotion *mevent,
					 GDisplay       *gdisp);
134
static void        ink_cursor_update    (GimpTool       *tool,
135 136
					 GdkEventMotion *mevent,
					 GDisplay       *gdisp);
137
static void        ink_control          (GimpTool       *tool,
138 139 140
					 ToolAction      tool_action,
					 GDisplay       *gdisp);

141
static void        time_smoother_add    (GimpInkTool    *ink_tool,
142
					 guint32         value);
143 144
static gdouble     time_smoother_result (GimpInkTool    *ink_tool);
static void        time_smoother_init   (GimpInkTool    *ink_tool,
145
					 guint32         initval);
146
static void        dist_smoother_add    (GimpInkTool    *ink_tool,
147
					 gdouble         value);
148 149
static gdouble     dist_smoother_result (GimpInkTool    *ink_tool);
static void        dist_smoother_init   (GimpInkTool    *ink_tool,
150 151
					 gdouble         initval);

152
static void        ink_init             (GimpInkTool    *ink_tool, 
153 154 155
					 GimpDrawable   *drawable, 
					 gdouble         x, 
					 gdouble         y);
156 157
static void        ink_finish           (GimpInkTool    *ink_tool, 
					 GimpDrawable   *drawable);
158 159 160 161 162 163 164 165 166 167
static void        ink_cleanup          (void);

static void        ink_type_update      (GtkWidget      *radio_button,
					 BlobFunc        function);
static GdkPixmap * blob_pixmap          (GdkColormap    *colormap,
					 GdkVisual      *visual,
					 BlobFunc        function);
static void        paint_blob           (GdkDrawable    *drawable, 
					 GdkGC          *gc,
					 Blob           *blob);
168

Michael Natterer's avatar
Michael Natterer committed
169
/*  Rendering functions  */
170
static void        ink_set_paint_area   (GimpInkTool    *ink_tool, 
171 172
					 GimpDrawable   *drawable, 
					 Blob           *blob);
173
static void        ink_paste            (GimpInkTool    *ink_tool, 
174 175 176
					 GimpDrawable   *drawable,
					 Blob           *blob);

177
static void        ink_to_canvas_tiles  (GimpInkTool    *ink_tool,
178 179 180 181 182 183 184 185 186 187 188 189
					 Blob           *blob,
					 guchar         *color);

static void        ink_set_undo_tiles   (GimpDrawable   *drawable,
					 gint            x, 
					 gint            y,
					 gint            w, 
					 gint            h);
static void        ink_set_canvas_tiles (gint            x, 
					 gint            y,
					 gint            w, 
					 gint            h);
190

Michael Natterer's avatar
Michael Natterer committed
191
/*  Brush pseudo-widget callbacks  */
192 193
static void   brush_widget_active_rect    (BrushWidget    *brush_widget,
					   GtkWidget      *widget,
194
					   GdkRectangle   *rect);
195 196
static void   brush_widget_realize        (GtkWidget      *widget);
static void   brush_widget_expose         (GtkWidget      *widget,
197 198
					   GdkEventExpose *event,
					   BrushWidget    *brush_widget);
199
static void   brush_widget_button_press   (GtkWidget      *widget,
200 201
					   GdkEventButton *event,
					   BrushWidget    *brush_widget);
202
static void   brush_widget_button_release (GtkWidget      *widget,
203 204
					   GdkEventButton *event,
					   BrushWidget    *brush_widget);
205
static void   brush_widget_motion_notify  (GtkWidget      *widget,
206 207 208 209
					   GdkEventMotion *event,
					   BrushWidget    *brush_widget);


210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/* local variables */

/* the ink tool options  */
static InkOptions *ink_options = NULL;

/*  undo blocks variables  */
static TileManager *undo_tiles = NULL;

/* Tiles used to render the stroke at 1 byte/pp */
static TileManager *canvas_tiles = NULL;

/* Flat buffer that is used to used to render the dirty region
 * for composition onto the destination drawable
 */
static TempBuf *canvas_buf = NULL;

226 227
static GimpToolClass *parent_class      = NULL;

228

229 230
/*  functions  */

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
void
gimp_ink_tool_register (void)
{
  tool_manager_register_tool (GIMP_TYPE_INK_TOOL,
			      TRUE,
			      "gimp:ink_tool",
			      _("Ink Tool"),
			      _("Draw in ink"),
			      N_("/Tools/Paint Tools/Ink"), "K",
			      NULL, "tools/ink.html",
			      (const gchar **) ink_bits);
}

GtkType
gimp_ink_tool_get_type (void)
{
  static GtkType tool_type = 0;

  if (! tool_type)
    {
      GtkTypeInfo tool_info =
      {
	"GimpInkTool",
	sizeof (GimpInkTool),
	sizeof (GimpInkToolClass),
	(GtkClassInitFunc) gimp_ink_tool_class_init,
	(GtkObjectInitFunc) gimp_ink_tool_init,
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
	(GtkClassInitFunc) NULL,
      };

      tool_type = gtk_type_unique (GIMP_TYPE_TOOL, &tool_info);
    }

  return tool_type;
}

static void
gimp_ink_tool_class_init (GimpInkToolClass *klass)
{
  GtkObjectClass *object_class;
  GimpToolClass  *tool_class;

  object_class = (GtkObjectClass *) klass;
  tool_class   = (GimpToolClass *) klass;

  parent_class = gtk_type_class (GIMP_TYPE_TOOL);

  object_class->destroy = gimp_ink_tool_destroy;

  tool_class->control        = ink_control;
  tool_class->button_press   = ink_button_press;
  tool_class->button_release = ink_button_release;
  tool_class->motion         = ink_motion;
  tool_class->cursor_update  = ink_cursor_update;
}

static void
gimp_ink_tool_init (GimpInkTool *ink_tool)
{
  GimpTool *tool;

  tool = GIMP_TOOL (ink_tool);
 
  /*  The tool options  */
  if (! ink_options)
    {
      ink_options = ink_options_new ();

      tool_manager_register_tool_options (GIMP_TYPE_INK_TOOL,
					  (ToolOptions *) ink_options);

304
      ink_options_reset ((ToolOptions *) ink_options);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    }
}

static void
gimp_ink_tool_destroy (GtkObject *object)
{
  GimpInkTool *ink_tool;

  ink_tool = GIMP_INK_TOOL (object);

  if (ink_tool->last_blob)
    g_free (ink_tool->last_blob);

  ink_cleanup ();

  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

324 325 326 327 328 329 330 331 332 333 334 335
static void 
ink_type_update (GtkWidget      *radio_button,
		 BlobFunc        function)
{
  InkOptions *options = ink_options;

  if (GTK_TOGGLE_BUTTON (radio_button)->active)
    options->function = function;

  gtk_widget_queue_draw (options->brush_w->widget);
}

336
static void
337
ink_options_reset (ToolOptions *tool_options)
338
{
339
  InkOptions *options;
340

341 342 343
  options = (InkOptions *) tool_options;

  paint_options_reset (tool_options);
344

345 346 347 348 349 350
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->size_w),
			    options->size_d);
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->sensitivity_w),
			    options->sensitivity_d);
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_sensitivity_w),
			    options->tilt_sensitivity_d);
351 352
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->vel_sensitivity_w),
			    options->vel_sensitivity_d);
353 354
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_angle_w),
			    options->tilt_angle_d);
355 356 357 358 359 360
  gtk_toggle_button_set_active (((options->function_d == blob_ellipse) ?
				 GTK_TOGGLE_BUTTON (options->function_w[0]) :
				 ((options->function_d == blob_square) ?
				  GTK_TOGGLE_BUTTON (options->function_w[1]) :
				  GTK_TOGGLE_BUTTON (options->function_w[2]))),
				TRUE);
361 362
  options->aspect = options->aspect_d;
  options->angle  = options->angle_d;
363
  gtk_widget_queue_draw (options->brush_w->widget);
364 365
}

366
static InkOptions *
367
ink_options_new (void)
368
{
369
  InkOptions *options;
370 371 372 373 374 375 376 377 378 379
  GtkWidget  *table;
  GtkWidget  *vbox;
  GtkWidget  *hbox;
  GtkWidget  *hbox2;
  GtkWidget  *radio_button;
  GtkWidget  *pixmap_widget;
  GtkWidget  *slider;
  GtkWidget  *frame;
  GtkWidget  *darea;
  GdkPixmap  *pixmap;
380

381
  options = g_new0 (InkOptions, 1);
382
  paint_options_init ((PaintOptions *) options,
383
		      GIMP_TYPE_INK_TOOL,
384
		      ink_options_reset);
385

386
  options->size             = options->size_d             = 4.4;
387
  options->sensitivity      = options->sensitivity_d      = 1.0;
388 389
  options->vel_sensitivity  = options->vel_sensitivity_d  = 0.8;
  options->tilt_sensitivity = options->tilt_sensitivity_d = 0.4;
390 391 392 393 394
  options->tilt_angle       = options->tilt_angle_d       = 0.0;
  options->function         = options->function_d         = blob_ellipse;
  options->aspect           = options->aspect_d           = 1.0;
  options->angle            = options->angle_d            = 0.0;

jtl's avatar
jtl committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  /*  the main vbox  */
  vbox = gtk_vbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (((ToolOptions *) options)->main_vbox), vbox,
		      TRUE, TRUE, 0);
  gtk_widget_show (vbox);

  /* adjust sliders */
  frame = gtk_frame_new (_("Adjustment"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
  gtk_widget_show (frame);

  table = gtk_table_new (2, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_container_set_border_width (GTK_CONTAINER (table), 2);
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_widget_show (table);
412 413 414

  /*  size slider  */
  options->size_w =
415
    gtk_adjustment_new (options->size_d, 0.0, 20.0, 1.0, 2.0, 0.0);
416
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->size_w));
417
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
jtl's avatar
jtl committed
418 419 420
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
			     _("Size:"), 1.0, 1.0,
			     slider, 1, FALSE);
421
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
422
  gtk_signal_connect (GTK_OBJECT (options->size_w), "value_changed",
Michael Natterer's avatar
Michael Natterer committed
423
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
424
		      &options->size);
jtl's avatar
jtl committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

  /* angle adjust slider */
  options->tilt_angle_w =
    gtk_adjustment_new (options->tilt_angle_d, -90.0, 90.0, 1, 10.0, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_angle_w));
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
			     _("Angle:"), 1.0, 1.0,
			     slider, 1, FALSE);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_signal_connect (GTK_OBJECT (options->tilt_angle_w), "value_changed",
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
		      &options->tilt_angle);

  /* sens sliders */
  frame = gtk_frame_new (_("Sensitivity"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
  gtk_widget_show (frame);

  table = gtk_table_new (3, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_container_set_border_width (GTK_CONTAINER (table), 2);
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_widget_show (table);

  /* size sens slider */
452 453 454
  options->sensitivity_w =
    gtk_adjustment_new (options->sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->sensitivity_w));
455
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
jtl's avatar
jtl committed
456 457 458
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
			     _("Size:"), 1.0, 1.0,
			     slider, 1, FALSE);
459
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
460
  gtk_signal_connect (GTK_OBJECT (options->sensitivity_w), "value_changed",
Michael Natterer's avatar
Michael Natterer committed
461
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
462
		      &options->sensitivity);
463
  
464 465 466 467
  /* tilt sens slider */
  options->tilt_sensitivity_w =
    gtk_adjustment_new (options->tilt_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_sensitivity_w));
468
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
jtl's avatar
jtl committed
469 470 471
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
			     _("Tilt:"), 1.0, 1.0,
			     slider, 1, FALSE);
472
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
473
  gtk_signal_connect (GTK_OBJECT (options->tilt_sensitivity_w), "value_changed",
Michael Natterer's avatar
Michael Natterer committed
474
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
475
		      &options->tilt_sensitivity);
476 477 478 479 480 481

  /* velocity sens slider */
  options->vel_sensitivity_w =
    gtk_adjustment_new (options->vel_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->vel_sensitivity_w));
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
jtl's avatar
jtl committed
482 483 484
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
			     _("Speed:"), 1.0, 1.0,
			     slider, 1, FALSE);
485 486
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_signal_connect (GTK_OBJECT (options->vel_sensitivity_w), "value_changed",
Michael Natterer's avatar
Michael Natterer committed
487
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
488 489
		      &options->vel_sensitivity);

jtl's avatar
jtl committed
490 491 492 493
  /*  bottom hbox */
  hbox = gtk_hbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  gtk_widget_show (hbox);
Raph Levien's avatar
Raph Levien committed
494

495
  /* Brush type radiobuttons */
496
  frame = gtk_frame_new (_("Type"));
jtl's avatar
jtl committed
497
  gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
498 499
  gtk_widget_show (frame);

jtl's avatar
jtl committed
500 501 502
  hbox2 = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (frame), hbox2);
  
503 504
  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
jtl's avatar
jtl committed
505
  gtk_box_pack_start (GTK_BOX (hbox2), vbox, FALSE, FALSE, 0);
506 507 508 509
  gtk_widget_show (vbox);

  pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
			gtk_widget_get_visual (vbox),
510 511 512 513
			blob_ellipse);

  pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  gdk_pixmap_unref (pixmap);
jtl's avatar
jtl committed
514
  gtk_misc_set_padding (GTK_MISC (pixmap_widget), 6, 0);
515 516 517 518 519 520 521

  radio_button = gtk_radio_button_new (NULL);
  gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
		      GTK_SIGNAL_FUNC (ink_type_update), 
		      (gpointer)blob_ellipse);

  gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
522
  gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
523

524
  options->function_w[0] = radio_button;
525 526 527

  pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
			gtk_widget_get_visual (vbox),
528 529 530 531
			blob_square);

  pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  gdk_pixmap_unref (pixmap);
jtl's avatar
jtl committed
532
  gtk_misc_set_padding (GTK_MISC (pixmap_widget), 6, 0);
533

534 535
  radio_button =
    gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (radio_button));
536 537 538 539 540
  gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
		      GTK_SIGNAL_FUNC (ink_type_update), 
		      (gpointer)blob_square);
  
  gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
541
  gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
542

543 544
  options->function_w[1] = radio_button;

545 546
  pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
			gtk_widget_get_visual (vbox),
547 548 549 550
			blob_diamond);

  pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  gdk_pixmap_unref (pixmap);
jtl's avatar
jtl committed
551
  gtk_misc_set_padding (GTK_MISC (pixmap_widget), 6, 0);
552

553 554
  radio_button =
    gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (radio_button));
555 556 557 558 559
  gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
		      GTK_SIGNAL_FUNC (ink_type_update), 
		      (gpointer)blob_diamond);

  gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
560
  gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
561

562 563
  options->function_w[2] = radio_button;

564
  /* Brush shape widget */
565
  frame = gtk_frame_new (_("Shape"));
jtl's avatar
jtl committed
566
  gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
567 568 569 570 571 572
  gtk_widget_show (frame);

  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
573

jtl's avatar
jtl committed
574
  frame = gtk_aspect_frame_new (NULL, 0.0, 0.5, 1.0, FALSE);
575 576
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
577

jtl's avatar
jtl committed
578 579 580
  options->brush_w = g_new (BrushWidget, 1);
  options->brush_w->state = FALSE;

581
  darea = gtk_drawing_area_new();
582
  options->brush_w->widget = darea;
583

584 585
  gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 60, 60);
  gtk_container_add (GTK_CONTAINER (frame), darea);
586

587 588 589 590 591
  gtk_widget_set_events (darea, 
			 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
			 | GDK_POINTER_MOTION_MASK | GDK_EXPOSURE_MASK);
  gtk_signal_connect (GTK_OBJECT (darea), "button_press_event",
		      GTK_SIGNAL_FUNC (brush_widget_button_press),
592
		      options->brush_w);
593 594
  gtk_signal_connect (GTK_OBJECT (darea), "button_release_event",
		      GTK_SIGNAL_FUNC (brush_widget_button_release),
595
		      options->brush_w);
596 597
  gtk_signal_connect (GTK_OBJECT (darea), "motion_notify_event",
		      GTK_SIGNAL_FUNC (brush_widget_motion_notify),
598
		      options->brush_w);
599 600
  gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
		      GTK_SIGNAL_FUNC (brush_widget_expose), 
601
		      options->brush_w);
602 603
  gtk_signal_connect (GTK_OBJECT (darea), "realize",
		      GTK_SIGNAL_FUNC (brush_widget_realize),
604
		      options->brush_w);
605

jtl's avatar
jtl committed
606
  gtk_widget_show_all (hbox);
607 608 609 610

  return options;
}

611 612 613

/*  the brush widget functions  */

614
static void
615 616
brush_widget_active_rect (BrushWidget  *brush_widget,
			  GtkWidget    *widget,
617 618 619 620 621
			  GdkRectangle *rect)
{
  int x,y;
  int r;

622
  r = MIN (widget->allocation.width, widget->allocation.height) / 2;
623

624
  x = widget->allocation.width / 2 + 0.85 * r * ink_options->aspect / 10.0 *
625
    cos (ink_options->angle);
626
  y = widget->allocation.height / 2 + 0.85 * r * ink_options->aspect / 10.0 *
627 628
    sin (ink_options->angle);

629 630
  rect->x = x - 5;
  rect->y = y - 5;
631 632 633 634 635
  rect->width = 10;
  rect->height = 10;
}

static void
636
brush_widget_realize (GtkWidget *widget)
637
{
638
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
639 640 641
}

static void
642 643 644 645 646
brush_widget_draw_brush (BrushWidget *brush_widget,
			 GtkWidget   *widget,
			 gdouble      xc,
			 gdouble      yc,
			 gdouble      radius)
647
{
648 649 650 651 652 653 654 655 656 657 658 659
  Blob *blob;

  blob = ink_options->function (xc, yc,
				radius * cos (ink_options->angle),
				radius * sin (ink_options->angle),
				(- (radius / ink_options->aspect) *
				 sin (ink_options->angle)),
				((radius / ink_options->aspect) *
				 cos (ink_options->angle)));

  paint_blob (widget->window, widget->style->fg_gc[widget->state], blob);
  g_free (blob);
660 661 662
}

static void
663 664 665
brush_widget_expose (GtkWidget      *widget,
		     GdkEventExpose *event,
		     BrushWidget    *brush_widget)
666 667 668
{
  GdkRectangle rect;
  int r0;
669 670

  r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
671 672 673 674

  if (r0 < 2)
    return;

675 676 677 678 679 680 681
  gdk_window_clear_area (widget->window, 0, 0,
			 widget->allocation.width,
			 widget->allocation.height);
  brush_widget_draw_brush (brush_widget, widget,
			   widget->allocation.width / 2,
			   widget->allocation.height / 2,
			   0.9 * r0);
682

683 684
  brush_widget_active_rect (brush_widget, widget, &rect);
  gdk_draw_rectangle (widget->window, widget->style->bg_gc[GTK_STATE_NORMAL],
685 686 687
		      TRUE,	/* filled */
		      rect.x, rect.y, 
		      rect.width, rect.height);
688
  gtk_draw_shadow (widget->style, widget->window, widget->state, GTK_SHADOW_OUT,
689 690 691 692 693
		   rect.x, rect.y,
		   rect.width, rect.height);
}

static void
694 695 696
brush_widget_button_press (GtkWidget      *widget,
			   GdkEventButton *event,
			   BrushWidget    *brush_widget)
697 698 699
{
  GdkRectangle rect;

700
  brush_widget_active_rect (brush_widget, widget, &rect);
701 702 703 704 705
  
  if ((event->x >= rect.x) && (event->x-rect.x < rect.width) &&
      (event->y >= rect.y) && (event->y-rect.y < rect.height))
    {
      brush_widget->state = TRUE;
706

707
      gtk_grab_add (brush_widget->widget);
708 709 710 711
    }
}

static void
712 713 714
brush_widget_button_release (GtkWidget      *widget,
			     GdkEventButton *event,
			     BrushWidget    *brush_widget)
715 716
{
  brush_widget->state = FALSE;
717

718
  gtk_grab_remove (brush_widget->widget);
719 720 721
}

static void
722 723 724
brush_widget_motion_notify (GtkWidget      *widget,
			    GdkEventMotion *event,
			    BrushWidget    *brush_widget)
725 726 727 728 729 730 731 732
{
  int x;
  int y;
  int r0;
  int rsquare;

  if (brush_widget->state)
    {
733 734
      x = event->x - widget->allocation.width / 2;
      y = event->y - widget->allocation.height / 2;
735 736 737 738
      rsquare = x*x + y*y;

      if (rsquare != 0)
	{
739
	  ink_options->angle = atan2 (y, x);
740

741
	  r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
742
	  ink_options->aspect =
743
	    10.0 * sqrt ((double) rsquare / (r0 * r0)) / 0.85;
744 745 746 747 748 749

	  if (ink_options->aspect < 1.0)
	    ink_options->aspect = 1.0;
	  if (ink_options->aspect > 10.0)
	    ink_options->aspect = 10.0;

750
	  gtk_widget_draw (widget, NULL);
751 752 753 754
	}
    }
}

755 756 757 758 759 760
/*
 * Return a black-on white pixmap in the given colormap and
 * visual that represents the BlobFunc 'function'
 */
static GdkPixmap *
blob_pixmap (GdkColormap *colormap,
761 762
	     GdkVisual   *visual,
	     BlobFunc     function)
763 764
{
  GdkPixmap *pixmap;
765 766 767
  GdkGC     *black_gc, *white_gc;
  GdkColor   tmp_color;
  Blob      *blob;
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

  pixmap = gdk_pixmap_new (NULL, 22, 21, visual->depth);

  black_gc = gdk_gc_new (pixmap);
  gdk_color_black (colormap, &tmp_color);
  gdk_gc_set_foreground (black_gc, &tmp_color);

  white_gc = gdk_gc_new (pixmap);
  gdk_color_white (colormap, &tmp_color);
  gdk_gc_set_foreground (white_gc, &tmp_color);

  gdk_draw_rectangle (pixmap, white_gc, TRUE, 0, 0, 21, 20);
  gdk_draw_rectangle (pixmap, black_gc, FALSE, 0, 0, 21, 20);
  blob = (*function) (10, 10, 8, 0, 0, 8);
  paint_blob (pixmap, black_gc, blob);
Sven Neumann's avatar
Sven Neumann committed
783
  g_free (blob);
784 785 786 787 788 789 790 791 792 793 794

  gdk_gc_unref (white_gc);
  gdk_gc_unref (black_gc);

  return pixmap;
}

/*
 * Draw a blob onto a drawable with the specified graphics context
 */
static void
795 796 797
paint_blob (GdkDrawable *drawable,
	    GdkGC       *gc,
	    Blob        *blob)
798 799 800 801 802 803 804 805 806 807 808 809
{
  int i;

  for (i=0;i<blob->height;i++)
    {
      if (blob->data[i].left <= blob->data[i].right)
	gdk_draw_line (drawable, gc,
		       blob->data[i].left,i+blob->y,
		       blob->data[i].right+1,i+blob->y);
    }
}

810

811
static Blob *
812 813 814 815 816
ink_pen_ellipse (gdouble x_center,
		 gdouble y_center,
		 gdouble pressure,
		 gdouble xtilt,
		 gdouble ytilt,
817
		 gdouble velocity)
818 819 820 821 822
{
  double size;
  double tsin, tcos;
  double aspect, radmin;
  double x,y;
823
  double tscale;
Raph Levien's avatar
Raph Levien committed
824 825
  double tscale_c;
  double tscale_s;
826
  
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
  /* Adjust the size depending on pressure. */

  size = ink_options->size * (1.0 + ink_options->sensitivity *
			      (2.0 * pressure - 1.0) );

  /* Adjust the size further depending on pointer velocity
     and velocity-sensitivity.  These 'magic constants' are
     'feels natural' tigert-approved. --ADM */

  if (velocity < 3.0)
    velocity = 3.0;

#ifdef VERBOSE
  g_print("%f (%f) -> ", (float)size, (float)velocity);
#endif  

  size = ink_options->vel_sensitivity *
    ((4.5 * size) / (1.0 + ink_options->vel_sensitivity * (2.0*(velocity))))
    + (1.0 - ink_options->vel_sensitivity) * size;

#ifdef VERBOSE
  g_print("%f\n", (float)size);
#endif

  /* Clamp resulting size to sane limits */

  if (size > ink_options->size * (1.0 + ink_options->sensitivity))
    size = ink_options->size * (1.0 + ink_options->sensitivity);
855

856 857 858
  if (size*SUBSAMPLE < 1.0) size = 1.0/SUBSAMPLE;

  /* Add brush angle/aspect to tilt vectorially */
859

Raph Levien's avatar
Raph Levien committed
860 861 862 863
  /* I'm not happy with the way the brush widget info is combined with
     tilt info from the brush. My personal feeling is that representing
     both as affine transforms would make the most sense. -RLL */

864
  tscale = ink_options->tilt_sensitivity * 10.0;
865 866
  tscale_c = tscale * cos (gimp_deg_to_rad (ink_options->tilt_angle));
  tscale_s = tscale * sin (gimp_deg_to_rad (ink_options->tilt_angle));
Raph Levien's avatar
Raph Levien committed
867 868 869 870 871 872 873 874
  x = ink_options->aspect*cos(ink_options->angle) +
    xtilt * tscale_c - ytilt * tscale_s;
  y = ink_options->aspect*sin(ink_options->angle) +
    ytilt * tscale_c + xtilt * tscale_s;
#ifdef VERBOSE
  g_print ("angle %g aspect %g; %g %g; %g %g\n",
	   ink_options->angle, ink_options->aspect, tscale_c, tscale_s, x, y);
#endif
875 876 877 878 879 880 881 882 883 884 885 886
  aspect = sqrt(x*x+y*y);

  if (aspect != 0)
    {
      tcos = x/aspect;
      tsin = y/aspect;
    }
  else
    {
      tsin = sin(ink_options->angle);
      tcos = cos(ink_options->angle);
    }
887

888 889 890 891 892 893 894 895
  if (aspect < 1.0) 
    aspect = 1.0;
  else if (aspect > 10.0) 
    aspect = 10.0;

  radmin = SUBSAMPLE * size/aspect;
  if (radmin < 1.0) radmin = 1.0;
  
896 897 898
  return ink_options->function (x_center * SUBSAMPLE, y_center * SUBSAMPLE,
				radmin*aspect*tcos, radmin*aspect*tsin,  
				-radmin*tsin, radmin*tcos);
899 900 901
}

static void
902
ink_button_press (GimpTool       *tool,
903
		  GdkEventButton *bevent,
904
		  GDisplay       *gdisp)
905
{
906
  GimpInkTool  *ink_tool;
907 908
  GimpDrawable *drawable;
  Blob         *b;
909
  gdouble       x, y;
910

911
  ink_tool = GIMP_INK_TOOL (tool);
912 913 914 915

  /*  Keep the coordinates of the target  */
  gdisplay_untransform_coords_f (gdisp, bevent->x, bevent->y,
				 &x, &y, TRUE);
916
  drawable = gimp_image_active_drawable (gdisp->gimage);
917 918 919

  ink_init (ink_tool, drawable, x, y);

920 921
  tool->state        = ACTIVE;
  tool->gdisp        = gdisp;
922 923 924
  tool->paused_count = 0;

  /*  pause the current selection and grab the pointer  */
925
  gdisplays_selection_visibility (gdisp->gimage, SelectionPause);
926

927 928
  /* add motion memory if you press mod1 first ^ perfectmouse */
  if (((bevent->state & GDK_MOD1_MASK) != 0) != (perfectmouse != 0))
929 930 931 932 933
    gdk_pointer_grab (gdisp->canvas->window, FALSE,
		      GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
		      NULL, NULL, bevent->time);
  else
    gdk_pointer_grab (gdisp->canvas->window, FALSE,
934 935
		      GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK |
		      GDK_BUTTON_RELEASE_MASK,
936 937
		      NULL, NULL, bevent->time);
  
938
  tool->gdisp = gdisp;
939 940 941
  tool->state = ACTIVE;

  b = ink_pen_ellipse (x, y, 
942
		       bevent->pressure, bevent->xtilt, bevent->ytilt, 10.0);
943 944 945 946

  ink_paste (ink_tool, drawable, b);
  ink_tool->last_blob = b;

947 948 949 950 951 952 953
  time_smoother_init (ink_tool, bevent->time);
  ink_tool->last_time = bevent->time;
  dist_smoother_init (ink_tool, 0.0);
  ink_tool->init_velocity = TRUE;
  ink_tool->lastx = x;
  ink_tool->lasty = y;

954
  gdisplay_flush_now (gdisp);
955 956 957
}

static void
958
ink_button_release (GimpTool       *tool,
959
		    GdkEventButton *bevent,
960
		    GDisplay       *gdisp)
961
{
962 963
  GimpInkTool *ink_tool;
  GimpImage   *gimage;
964

965 966 967
  ink_tool = GIMP_INK_TOOL (tool);

  gimage = gdisp->gimage;
968 969

  /*  resume the current selection and ungrab the pointer  */
970
  gdisplays_selection_visibility (gdisp->gimage, SelectionResume);
971 972 973 974 975 976 977

  gdk_pointer_ungrab (bevent->time);
  gdk_flush ();

  /*  Set tool state to inactive -- no longer painting */
  tool->state = INACTIVE;

978 979 980 981
  /*  free the last blob  */
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = NULL;

982
  ink_finish (ink_tool, gimp_image_active_drawable (gdisp->gimage));
983 984 985
  gdisplays_flush ();
}

986 987

static void
988 989
dist_smoother_init (GimpInkTool *ink_tool,
		    gdouble      initval)
990 991 992 993 994 995 996 997 998 999 1000 1001
{
  gint i;

  ink_tool->dt_index = 0;

  for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
    {
      ink_tool->dt_buffer[i] = initval;
    }
}

static gdouble
1002
dist_smoother_result (GimpInkTool *ink_tool)
1003
{
1004
  gint    i;
1005 1006
  gdouble result = 0.0;

1007
  for (i = 0; i < DIST_SMOOTHER_BUFFER; i++)
1008 1009 1010 1011
    {
      result += ink_tool->dt_buffer[i];
    }

1012
  return (result / (gdouble) DIST_SMOOTHER_BUFFER);
1013 1014 1015
}

static void
1016 1017
dist_smoother_add (GimpInkTool *ink_tool,
		   gdouble      value)
1018 1019 1020 1021 1022 1023 1024 1025 1026
{
  ink_tool->dt_buffer[ink_tool->dt_index] = value;

  if ((++ink_tool->dt_index) == DIST_SMOOTHER_BUFFER)
    ink_tool->dt_index = 0;
}


static void
1027 1028
time_smoother_init (GimpInkTool *ink_tool,
		    guint32      initval)
1029 1030 1031 1032 1033
{
  gint i;

  ink_tool->ts_index = 0;

1034
  for (i = 0; i < TIME_SMOOTHER_BUFFER; i++)
1035 1036 1037 1038 1039 1040
    {
      ink_tool->ts_buffer[i] = initval;
    }
}

static gdouble
1041
time_smoother_result (GimpInkTool *ink_tool)