gimpdrawtool.c 24.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
 *
 * 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.
 */

#include "config.h"

#include <gtk/gtk.h>

Michael Natterer's avatar
Michael Natterer committed
23 24 25
#include "libgimpmath/gimpmath.h"

#include "tools-types.h"
26

27
#include "display/gimpdisplay.h"
Michael Natterer's avatar
Michael Natterer committed
28
#include "display/gimpdisplayshell.h"
Michael Natterer's avatar
Michael Natterer committed
29

30
#include "gimpdrawtool.h"
31 32


33 34 35 36 37 38
static void          gimp_draw_tool_class_init (GimpDrawToolClass *klass);
static void          gimp_draw_tool_init       (GimpDrawTool      *draw_tool);

static void          gimp_draw_tool_finalize   (GObject           *object);

static void          gimp_draw_tool_control    (GimpTool          *tool,
39
                                                GimpToolAction     action,
40 41
                                                GimpDisplay       *gdisp);

42 43
static void          gimp_draw_tool_draw       (GimpDrawTool      *draw_tool);

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static inline void   gimp_draw_tool_shift_to_north_west
                                               (gdouble            x,
                                                gdouble            y,
                                                gint               handle_width,
                                                gint               handle_height,
                                                GtkAnchorType      anchor,
                                                gdouble           *shifted_x,
                                                gdouble           *shifted_y);
static inline void   gimp_draw_tool_shift_to_center
                                               (gdouble            x,
                                                gdouble            y,
                                                gint               handle_width,
                                                gint               handle_height,
                                                GtkAnchorType      anchor,
                                                gdouble           *shifted_x,
                                                gdouble           *shifted_y);
60 61


62 63
static GimpToolClass *parent_class = NULL;

64

65
GType
66 67
gimp_draw_tool_get_type (void)
{
68
  static GType tool_type = 0;
69 70 71

  if (! tool_type)
    {
72
      static const GTypeInfo tool_info =
73 74
      {
        sizeof (GimpDrawToolClass),
75 76 77 78 79 80 81 82
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_draw_tool_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data     */
	sizeof (GimpDrawTool),
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_draw_tool_init,
83 84
      };

85 86 87
      tool_type = g_type_register_static (GIMP_TYPE_TOOL,
					  "GimpDrawTool", 
                                          &tool_info, 0);
88 89 90 91 92 93 94 95
    }

  return tool_type;
}

static void
gimp_draw_tool_class_init (GimpDrawToolClass *klass)
{
96 97
  GObjectClass  *object_class;
  GimpToolClass *tool_class;
98

99 100
  object_class = G_OBJECT_CLASS (klass);
  tool_class   = GIMP_TOOL_CLASS (klass);
101

102
  parent_class = g_type_class_peek_parent (klass);
103

104
  object_class->finalize = gimp_draw_tool_finalize;
105

106
  tool_class->control    = gimp_draw_tool_control;
107 108

  klass->draw            = NULL;
109 110
}

111
static void
112
gimp_draw_tool_init (GimpDrawTool *draw_tool)
113
{
114
  draw_tool->gdisp        = NULL;
115 116
  draw_tool->win          = NULL;
  draw_tool->gc           = NULL;
117 118

  draw_tool->draw_state   = GIMP_DRAW_TOOL_STATE_INVISIBLE;
119
  draw_tool->paused_count = 0;
120

121 122 123 124
  draw_tool->line_width   = 0;
  draw_tool->line_style   = GDK_LINE_SOLID;
  draw_tool->cap_style    = GDK_CAP_NOT_LAST;
  draw_tool->join_style   = GDK_JOIN_MITER;
125
}
126

127
static void
128
gimp_draw_tool_finalize (GObject *object)
129 130 131 132 133 134
{
  GimpDrawTool *draw_tool;

  draw_tool = GIMP_DRAW_TOOL (object);

  if (draw_tool->gc)
135
    {
136
      g_object_unref (G_OBJECT (draw_tool->gc));
137 138
      draw_tool->gc = NULL;
    }
139

140
  G_OBJECT_CLASS (parent_class)->finalize (object);
141 142 143
}

static void
144 145 146
gimp_draw_tool_control (GimpTool       *tool,
			GimpToolAction  action,
			GimpDisplay    *gdisp)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
{
  GimpDrawTool *draw_tool;

  draw_tool = GIMP_DRAW_TOOL (tool);

  switch (action)
    {
    case PAUSE:
      gimp_draw_tool_pause (draw_tool);
      break;

    case RESUME:
      gimp_draw_tool_resume (draw_tool);
      break;

    case HALT:
      gimp_draw_tool_stop (draw_tool);
      break;

    default:
      break;
    }
169

170
  GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
171 172
}

173 174 175 176 177 178 179 180 181
static void
gimp_draw_tool_draw (GimpDrawTool *draw_tool)
{
  if (draw_tool->gdisp)
    {
      GIMP_DRAW_TOOL_GET_CLASS (draw_tool)->draw (draw_tool);
    }
}

182
void
183
gimp_draw_tool_start (GimpDrawTool *draw_tool,
184
		      GimpDisplay  *gdisp)
185
{
186 187
  GimpDisplayShell *shell;
  GdkColor          fg, bg;
188

189
  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
190 191 192
  g_return_if_fail (GIMP_IS_DISPLAY (gdisp));

  shell = GIMP_DISPLAY_SHELL (gdisp->shell);
193

194
  gimp_draw_tool_stop (draw_tool);
195

196
  draw_tool->gdisp        = gdisp;
197
  draw_tool->win          = shell->canvas->window;
198
  draw_tool->gc           = gdk_gc_new (draw_tool->win);
199

200
  gdk_gc_set_function (draw_tool->gc, GDK_INVERT);
201 202
  fg.pixel = 0xFFFFFFFF;
  bg.pixel = 0x00000000;
203 204 205 206 207 208 209
  gdk_gc_set_foreground (draw_tool->gc, &fg);
  gdk_gc_set_background (draw_tool->gc, &bg);
  gdk_gc_set_line_attributes (draw_tool->gc,
                              draw_tool->line_width,
                              draw_tool->line_style,
			      draw_tool->cap_style,
                              draw_tool->join_style);
210

211
  gimp_draw_tool_draw (draw_tool);
212

213
  draw_tool->draw_state = GIMP_DRAW_TOOL_STATE_VISIBLE;
214 215 216
}

void
217
gimp_draw_tool_stop (GimpDrawTool *draw_tool)
218
{
219 220
  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

221 222
  if (draw_tool->draw_state == GIMP_DRAW_TOOL_STATE_VISIBLE)
    {
223
      gimp_draw_tool_draw (draw_tool);
224
    }
225

226 227 228 229 230
  draw_tool->draw_state   = GIMP_DRAW_TOOL_STATE_INVISIBLE;
  draw_tool->paused_count = 0;

  draw_tool->gdisp = NULL;
  draw_tool->win   = NULL;
231

232 233 234 235 236
  if (draw_tool->gc)
    {
      g_object_unref (G_OBJECT (draw_tool->gc));
      draw_tool->gc = NULL;
    }
237 238
}

239 240 241
void
gimp_draw_tool_pause (GimpDrawTool *draw_tool)
{
242 243
  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

244 245
  if (draw_tool->paused_count == 0)
    {
246
      draw_tool->draw_state = GIMP_DRAW_TOOL_STATE_INVISIBLE;
247

248
      gimp_draw_tool_draw (draw_tool);
249 250 251 252
    }

  draw_tool->paused_count++;
}
253 254

void
255
gimp_draw_tool_resume (GimpDrawTool *draw_tool)
256
{
257 258 259
  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

  if (draw_tool->paused_count > 0)
260
    {
261
      draw_tool->paused_count--;
262

263 264 265 266
      if (draw_tool->paused_count == 0)
        {
          draw_tool->draw_state = GIMP_DRAW_TOOL_STATE_VISIBLE;

267
          gimp_draw_tool_draw (draw_tool);
268 269 270 271
        }
    }
  else
    {
272 273
      g_warning ("gimp_draw_tool_resume(): "
                 "called with draw_tool->paused_count == 0");
274 275 276
    }
}

277 278 279 280 281 282 283 284
gdouble
gimp_draw_tool_calc_distance (GimpDrawTool *draw_tool,
                              GimpDisplay  *gdisp,
                              gdouble       x1,
                              gdouble       y1,
                              gdouble       x2,
                              gdouble       y2)
{
285 286
  gdouble tx1, ty1;
  gdouble tx2, ty2;
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

  g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), 0.0);
  g_return_val_if_fail (GIMP_IS_DISPLAY (gdisp), 0.0);

  gdisplay_transform_coords_f (gdisp, x1, y1, &tx1, &ty1, FALSE);
  gdisplay_transform_coords_f (gdisp, x2, y2, &tx2, &ty2, FALSE);

  return sqrt (SQR (tx2 - tx1) + SQR (ty2 - ty1));
}

gboolean
gimp_draw_tool_in_radius (GimpDrawTool *draw_tool,
                          GimpDisplay  *gdisp,
                          gdouble       x1,
                          gdouble       y1,
                          gdouble       x2,
                          gdouble       y2,
                          gint          radius)
{
306 307
  gdouble tx1, ty1;
  gdouble tx2, ty2;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

  g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
  g_return_val_if_fail (GIMP_IS_DISPLAY (gdisp), FALSE);

  gdisplay_transform_coords_f (gdisp, x1, y1, &tx1, &ty1, FALSE);
  gdisplay_transform_coords_f (gdisp, x2, y2, &tx2, &ty2, FALSE);

  return (SQR (tx2 - tx1) + SQR (ty2 - ty1)) < SQR (radius);
}

void
gimp_draw_tool_draw_line (GimpDrawTool *draw_tool,
                          gdouble       x1,
                          gdouble       y1,
                          gdouble       x2,
                          gdouble       y2,
                          gboolean      use_offsets)
{
  GimpDisplayShell *shell;
  gdouble           tx1, ty1;
  gdouble           tx2, ty2;

  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

332
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
333

334 335 336 337 338 339 340 341
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               x1, y1,
                               &tx1, &ty1,
                               use_offsets);
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               x2, y2,
                               &tx2, &ty2,
                               use_offsets);
342 343 344 345 346 347

  gdk_draw_line (draw_tool->win,
                 draw_tool->gc,
                 RINT (tx1), RINT (ty1),
                 RINT (tx2), RINT (ty2));
}
348 349

void
350 351 352 353 354 355 356
gimp_draw_tool_draw_rectangle (GimpDrawTool *draw_tool,
                               gboolean      filled,
                               gdouble       x,
                               gdouble       y,
                               gdouble       width,
                               gdouble       height,
                               gboolean      use_offsets)
357
{
358 359 360
  GimpDisplayShell *shell;
  gdouble           tx1, ty1;
  gdouble           tx2, ty2;
361

362 363
  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

364
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
365 366 367 368 369 370

  tx1 = MIN (x, x + width);
  ty1 = MIN (y, y + height);
  tx2 = MAX (x, x + width);
  ty2 = MAX (y, y + height);

371 372 373 374 375 376 377 378
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               tx1, ty1,
                               &tx1, &ty1,
                               use_offsets);
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               tx2, ty2,
                               &tx2, &ty2,
                               use_offsets);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

  gdk_draw_rectangle (draw_tool->win,
                      draw_tool->gc,
                      filled,
                      RINT (tx1), RINT (ty1),
                      RINT (tx2 - tx1), RINT (ty2 - ty1));
}

void
gimp_draw_tool_draw_arc (GimpDrawTool *draw_tool,
                         gboolean      filled,
                         gdouble       x,
                         gdouble       y,
                         gdouble       width,
                         gdouble       height,
                         gint          angle1,
                         gint          angle2,
                         gboolean      use_offsets)
{
  GimpDisplayShell *shell;
  gdouble           tx1, ty1;
  gdouble           tx2, ty2;

  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

404
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
405 406 407 408 409 410

  tx1 = MIN (x, x + width);
  ty1 = MIN (y, y + height);
  tx2 = MAX (x, x + width);
  ty2 = MAX (y, y + height);

411 412 413 414 415 416 417 418
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               tx1, ty1,
                               &tx1, &ty1,
                               use_offsets);
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               tx2, ty2,
                               &tx2, &ty2,
                               use_offsets);
419 420 421 422 423 424 425 426 427 428

  gdk_draw_arc (draw_tool->win,
                draw_tool->gc,
                filled,
                RINT (tx1), RINT (ty1),
                RINT (tx2 - tx1), RINT (ty2 - ty1),
                angle1, angle2);
}

void
429 430 431 432 433 434 435 436
gimp_draw_tool_draw_rectangle_by_anchor (GimpDrawTool   *draw_tool,
                                         gboolean        filled,
                                         gdouble         x,
                                         gdouble         y,
                                         gint            width,
                                         gint            height,
                                         GtkAnchorType   anchor,
                                         gboolean        use_offsets)
437 438 439 440 441 442
{
  GimpDisplayShell *shell;
  gdouble           tx, ty;

  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

443
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
444

445 446 447 448
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               x, y,
                               &tx, &ty,
                               use_offsets);
449

450 451 452 453 454 455 456 457 458 459 460
  gimp_draw_tool_shift_to_north_west (tx, ty,
                                      width, height,
                                      anchor,
                                      &tx, &ty);

  if (filled)
    {
      width++;
      height++;
    }

461 462 463
  gdk_draw_rectangle (draw_tool->win,
                      draw_tool->gc,
                      filled,
464
                      RINT (tx), RINT (ty),
465
                      width, height);
466
}
467

468
void
469 470 471 472 473 474 475 476 477 478
gimp_draw_tool_draw_arc_by_anchor (GimpDrawTool  *draw_tool,
                                   gboolean       filled,
                                   gdouble        x,
                                   gdouble        y,
                                   gint           radius_x,
                                   gint           radius_y,
                                   gint           angle1,
                                   gint           angle2,
                                   GtkAnchorType  anchor,
                                   gboolean       use_offsets)
479 480 481 482 483 484
{
  GimpDisplayShell *shell;
  gdouble           tx, ty;

  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));

485
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
486

487 488 489 490
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               x, y,
                               &tx, &ty,
                               use_offsets);
491

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  /* well... */
  radius_x *= 2;
  radius_y *= 2;

  gimp_draw_tool_shift_to_north_west (tx, ty,
                                      radius_x, radius_y,
                                      anchor,
                                      &tx, &ty);

  if (filled)
    {
      radius_x += 1;
      radius_y += 1;
    }

507 508 509
  gdk_draw_arc (draw_tool->win,
                draw_tool->gc,
                filled,
510 511
                RINT (tx), RINT (ty),
                radius_x, radius_y,
512 513
                angle1, angle2);
}
514 515

void
516 517 518 519 520 521 522
gimp_draw_tool_draw_cross_by_anchor (GimpDrawTool  *draw_tool,
                                     gdouble        x,
                                     gdouble        y,
                                     gint           width,
                                     gint           height,
                                     GtkAnchorType  anchor,
                                     gboolean       use_offsets)
523
{
Michael Natterer's avatar
Michael Natterer committed
524
  GimpDisplayShell *shell;
525 526 527
  gdouble           tx, ty;

  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
Michael Natterer's avatar
Michael Natterer committed
528

529
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
530

531 532 533 534
  gdisplay_transform_coords_f (draw_tool->gdisp,
                               x, y,
                               &tx, &ty,
                               use_offsets);
535

536 537 538 539 540
  gimp_draw_tool_shift_to_center (tx, ty,
                                  width, height,
                                  anchor,
                                  &tx, &ty);

541 542
  gdk_draw_line (draw_tool->win,
                 draw_tool->gc,
543 544
		 RINT (tx), RINT (ty) - (height >> 1),
		 RINT (tx), RINT (ty) + (height >> 1));
545 546
  gdk_draw_line (draw_tool->win,
                 draw_tool->gc,
547 548
		 RINT (tx) - (width >> 1), RINT (ty),
		 RINT (tx) + (width >> 1), RINT (ty));
549
}
550

551 552 553 554 555
void
gimp_draw_tool_draw_handle (GimpDrawTool   *draw_tool, 
                            GimpHandleType  type,
                            gdouble         x,
                            gdouble         y,
556 557 558
                            gint            width,
                            gint            height,
                            GtkAnchorType   anchor,
559 560 561
                            gboolean        use_offsets)
{
  g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
562

563
  switch (type)
Michael Natterer's avatar
Michael Natterer committed
564
    {
565
    case GIMP_HANDLE_SQUARE:
566 567
      gimp_draw_tool_draw_rectangle_by_anchor (draw_tool,
                                               FALSE,
568
                                               x, y,
569 570 571 572 573 574 575 576 577 578 579 580 581
                                               width,
                                               height,
                                               anchor,
                                               use_offsets);
      break;

    case GIMP_HANDLE_FILLED_SQUARE:
      gimp_draw_tool_draw_rectangle_by_anchor (draw_tool,
                                               TRUE,
                                               x, y,
                                               width,
                                               height,
                                               anchor,
582 583 584 585
                                               use_offsets);
      break;

    case GIMP_HANDLE_CIRCLE:
586 587 588 589 590 591 592 593 594 595 596 597 598
      gimp_draw_tool_draw_arc_by_anchor (draw_tool,
                                         FALSE,
                                         x, y,
                                         width >> 1,
                                         height >> 1,
                                         0, 360 * 64,
                                         anchor,
                                         use_offsets);
      break;

    case GIMP_HANDLE_FILLED_CIRCLE:
      gimp_draw_tool_draw_arc_by_anchor (draw_tool,
                                         TRUE,
599
                                         x, y,
600 601
                                         width >> 1,
                                         height >> 1,
602
                                         0, 360 * 64,
603
                                         anchor,
604 605 606
                                         use_offsets);
      break;

607 608 609 610 611 612 613 614 615
    case GIMP_HANDLE_CROSS:
      gimp_draw_tool_draw_cross_by_anchor (draw_tool,
                                           x, y,
                                           width,
                                           height,
                                           anchor,
                                           use_offsets);
      break;

616
    default:
617
      g_warning ("%s: invalid handle type %d", G_GNUC_PRETTY_FUNCTION, type);
618
      break;
Michael Natterer's avatar
Michael Natterer committed
619
    }
620 621
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
gboolean
gimp_draw_tool_on_handle (GimpDrawTool   *draw_tool,
                          GimpDisplay    *gdisp,
                          gdouble         x,
                          gdouble         y,
                          GimpHandleType  type,
                          gdouble         handle_x,
                          gdouble         handle_y,
                          gint            width,
                          gint            height,
                          GtkAnchorType   anchor,
                          gboolean        use_offsets)
{
  gdouble tx, ty;
  gdouble handle_tx, handle_ty;

638 639
  g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
  g_return_val_if_fail (GIMP_IS_DISPLAY (gdisp), FALSE);
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

  gdisplay_transform_coords_f (gdisp, x, y, &tx, &ty, use_offsets);
  gdisplay_transform_coords_f (gdisp,
                               handle_x, handle_y,
                               &handle_tx, &handle_ty,
                               use_offsets);

  switch (type)
    {
    case GIMP_HANDLE_SQUARE:
    case GIMP_HANDLE_FILLED_SQUARE:
    case GIMP_HANDLE_CROSS:
      gimp_draw_tool_shift_to_north_west (handle_tx, handle_ty,
                                          width, height,
                                          anchor,
                                          &handle_tx, &handle_ty);

      return (tx == CLAMP (tx, handle_tx, handle_tx + width) &&
              ty == CLAMP (ty, handle_ty, handle_ty + height));

    case GIMP_HANDLE_CIRCLE:
    case GIMP_HANDLE_FILLED_CIRCLE:
      gimp_draw_tool_shift_to_center (handle_tx, handle_ty,
                                      width, height,
                                      anchor,
                                      &handle_tx, &handle_ty);

      /* FIXME */
      if (width != height)
669 670 671
        width = (width + height) / 2;

      width /= 2;
672 673 674 675 676 677 678 679 680 681

      return ((SQR (handle_tx - tx) + SQR (handle_ty - ty)) < SQR (width));

    default:
      g_warning ("%s: invalid handle type %d", G_GNUC_PRETTY_FUNCTION, type);
      break;
    }

  return FALSE;
}
682 683 684

void
gimp_draw_tool_draw_lines (GimpDrawTool *draw_tool, 
Michael Natterer's avatar
Michael Natterer committed
685 686 687
			   gdouble      *points,
			   gint          npoints,
			   gint          filled)
688
{
Michael Natterer's avatar
Michael Natterer committed
689 690 691 692
  GimpDisplayShell *shell;
  GdkPoint         *coords;
  gint              i;
  gdouble           sx, sy;
693

694
  shell = GIMP_DISPLAY_SHELL (draw_tool->gdisp->shell);
695

Michael Natterer's avatar
Michael Natterer committed
696 697 698 699
  coords = g_new (GdkPoint, npoints);

  for (i = 0; i < npoints ; i++)
    {
700 701 702 703
      gdisplay_transform_coords_f (draw_tool->gdisp,
                                   points[i*2], points[i*2+1],
                                   &sx, &sy,
                                   TRUE);
Michael Natterer's avatar
Michael Natterer committed
704 705 706
      coords[i].x = ROUND (sx);
      coords[i].y = ROUND (sy);
    }
707 708

  if (filled)
Michael Natterer's avatar
Michael Natterer committed
709
    {
710
      gdk_draw_polygon (draw_tool->win,
Michael Natterer's avatar
Michael Natterer committed
711 712 713
                        draw_tool->gc, TRUE,
                        coords, npoints);
    }
714
  else
Michael Natterer's avatar
Michael Natterer committed
715
    {
716
      gdk_draw_lines (draw_tool->win,
Michael Natterer's avatar
Michael Natterer committed
717 718 719
                      draw_tool->gc,
                      coords, npoints);
    }
720 721 722

  g_free (coords);
}
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849


/*  private functions  */

static inline void
gimp_draw_tool_shift_to_north_west (gdouble        x,
                                    gdouble        y,
                                    gint           handle_width,
                                    gint           handle_height,
                                    GtkAnchorType  anchor,
                                    gdouble       *shifted_x,
                                    gdouble       *shifted_y)
{
  switch (anchor)
    {
    case GTK_ANCHOR_CENTER:
      x -= (handle_width >> 1);
      y -= (handle_height >> 1);
      break;

    case GTK_ANCHOR_NORTH:
      x -= (handle_width >> 1);
      break;

    case GTK_ANCHOR_NORTH_WEST:
      /*  nothing, this is the default  */
      break;

    case GTK_ANCHOR_NORTH_EAST:
      x -= handle_width;
      break;

    case GTK_ANCHOR_SOUTH:
      x -= (handle_width >> 1);
      y -= handle_height;
      break;

    case GTK_ANCHOR_SOUTH_WEST:
      y -= handle_height;
      break;

    case GTK_ANCHOR_SOUTH_EAST:
      x -= handle_width;
      y -= handle_height;
      break;

    case GTK_ANCHOR_WEST:
      y -= (handle_height >> 1);
      break;

    case GTK_ANCHOR_EAST:
      x -= handle_width;
      y -= (handle_height >> 1);
      break;

    default:
      break;
    }

  if (shifted_x)
    *shifted_x = x;

  if (shifted_y)
    *shifted_y = y;
}
                                    
static inline void
gimp_draw_tool_shift_to_center (gdouble        x,
                                gdouble        y,
                                gint           handle_width,
                                gint           handle_height,
                                GtkAnchorType  anchor,
                                gdouble       *shifted_x,
                                gdouble       *shifted_y)
{
  switch (anchor)
    {
    case GTK_ANCHOR_CENTER:
      /*  nothing, this is the default  */
      break;

    case GTK_ANCHOR_NORTH:
      y += (handle_height >> 1);
      break;

    case GTK_ANCHOR_NORTH_WEST:
      x += (handle_width >> 1);
      y += (handle_height >> 1);
      break;

    case GTK_ANCHOR_NORTH_EAST:
      x -= (handle_width >> 1);
      y += (handle_height >> 1);
      break;

    case GTK_ANCHOR_SOUTH:
      y -= (handle_height >> 1);
      break;

    case GTK_ANCHOR_SOUTH_WEST:
      x += (handle_width >> 1);
      y -= (handle_height >> 1);
      break;

    case GTK_ANCHOR_SOUTH_EAST:
      x -= (handle_width >> 1);
      y -= (handle_height >> 1);
      break;

    case GTK_ANCHOR_WEST:
      x += (handle_width >> 1);
      break;

    case GTK_ANCHOR_EAST:
      x -= (handle_width >> 1);
      break;

    default:
      break;
    }

  if (shifted_x)
    *shifted_x = x;

  if (shifted_y)
    *shifted_y = y;
}