gimptransformtool.c 47.4 KB
Newer Older
Nate Summers's avatar
Nate Summers committed
1
/* The GIMP -- an image manipulation program
Nate Summers's avatar
Nate Summers committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * 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 <stdlib.h>

#include <gtk/gtk.h>

#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "apptypes.h"

#include "cursorutil.h"
#include "drawable.h"
#include "floating_sel.h"
#include "gdisplay.h"
#include "gimage_mask.h"
#include "gimpchannel.h"
#include "gimpimage.h"
#include "gimplayer.h"
Nate Summers's avatar
Nate Summers committed
38
#include "gimpmarshal.h"
Nate Summers's avatar
Nate Summers committed
39 40 41 42 43 44 45 46 47 48
#include "gimprc.h"
#include "gimpui.h"
#include "info_dialog.h"
#include "path_transform.h"
#include "paint_funcs.h"
#include "pixel_region.h"
#include "undo.h"
#include "tile_manager.h"
#include "tile.h"

Nate Summers's avatar
Nate Summers committed
49 50
#include "tools/gimptool.h"
#include "tools/tool_manager.h"
Nate Summers's avatar
Nate Summers committed
51 52 53 54 55 56 57 58 59 60
#include "tools/gimpdrawtool.h"
#include "tools/gimptransformtool.h"
/*#include "transform_tool.h"*/

#include "libgimp/gimpintl.h"

enum
{
  TRANSFORM,
  LAST_SIGNAL
Nate Summers's avatar
Nate Summers committed
61
};
Nate Summers's avatar
Nate Summers committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

/* This should be migrated to pixel_region or similar... */
/* PixelSurround describes a (read-only)
 *  region around a pixel in a tile manager
 */

typedef struct _PixelSurround
{
  Tile        *tile;
  TileManager *mgr;
  guchar      *buff;
  gint         buff_size;
  gint         bpp;
  gint         w;
  gint         h;
  guchar       bg[MAX_CHANNELS];
  gint         row_stride;
} PixelSurround;

#define BILINEAR(jk,j1k,jk1,j1k1,dx,dy) \
                ((1-dy) * (jk + dx * (j1k - jk)) + \
		    dy  * (jk1 + dx * (j1k1 - jk1)))

/* access interleaved pixels */
#define CUBIC_ROW(dx, row, step) \
Nate Summers's avatar
Nate Summers committed
87
  gimp_transform_tool_cubic(dx, (row)[0], (row)[step], (row)[step+step], (row)[step+step+step])
Nate Summers's avatar
Nate Summers committed
88
#define CUBIC_SCALED_ROW(dx, row, step, i) \
Nate Summers's avatar
Nate Summers committed
89
  gimp_transform_tool_cubic(dx, (row)[0] * (row)[i], \
Nate Summers's avatar
Nate Summers committed
90 91 92 93 94 95 96 97 98
            (row)[step] * (row)[step + i], \
            (row)[step+step]* (row)[step+step + i], \
            (row)[step+step+step] * (row)[step+step+step + i])

#define REF_TILE(i,x,y) \
     tile[i] = tile_manager_get_tile (float_tiles, x, y, TRUE, FALSE); \
     src[i] = tile_data_pointer (tile[i], (x) % TILE_WIDTH, (y) % TILE_HEIGHT);

/*  forward function declarations  */
Nate Summers's avatar
Nate Summers committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static void      gimp_transform_tool_bounds    (GimpTransformTool      *tool,
                                                GDisplay               *gdisp);
static void      gimp_transform_tool_recalc    (GimpTransformTool      *tool,
					        GDisplay               *gdisp);
static void      gimp_transform_tool_doit      (GimpTransformTool      *tool,
					        GDisplay               *gdisp);
static gdouble   gimp_transform_tool_cubic     (gdouble                 dx,
					        gint                    jm1,
					        gint                    j,
					        gint                    jp1,
					        gint                    jp2);
static void    gimp_transform_tool_setup_grid  (GimpTransformTool      *tool);
static void    gimp_transform_tool_grid_recalc (GimpTransformTool      *gimp_transform_tool);
static void    gimp_transform_tool_init        (GimpTransformTool      *tool);
static void    gimp_transform_tool_class_init  (GimpTransformToolClass *tool);
Nate Summers's avatar
Nate Summers committed
114 115 116


/*  variables  */
Nate Summers's avatar
Nate Summers committed
117 118 119 120
static TranInfo           old_trans_info;
InfoDialog               *transform_info        = NULL;
static gboolean           transform_info_inited = FALSE;
static GimpDrawToolClass *parent_class          = NULL;
Nate Summers's avatar
Nate Summers committed
121

Nate Summers's avatar
Nate Summers committed
122
static guint gimp_transform_tool_signals[LAST_SIGNAL] = { 0 };
Nate Summers's avatar
Nate Summers 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

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

  if (! tool_type)
    {
      GtkTypeInfo tool_info =
      {
        "GimpTransformTool",
        sizeof (GimpTransformTool),
        sizeof (GimpTransformToolClass),
        (GtkClassInitFunc) gimp_transform_tool_class_init,
        (GtkObjectInitFunc) gimp_transform_tool_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        NULL /* (GtkClassInitFunc) gimp_tool_class_init, */
      };

      tool_type = gtk_type_unique (GIMP_TYPE_DRAW_TOOL, &tool_info);
    }

  return tool_type;
}


static void
gimp_transform_tool_class_init (GimpTransformToolClass *klass)
{
Nate Summers's avatar
Nate Summers committed
153 154 155
  GtkObjectClass    *object_class;
  GimpToolClass     *tool_class;
  GimpDrawToolClass *draw_class;
Nate Summers's avatar
Nate Summers committed
156 157 158

  object_class = (GtkObjectClass *) klass;
  tool_class   = (GimpToolClass *) klass;
Nate Summers's avatar
Nate Summers committed
159
  draw_class   = (GimpDrawToolClass *) klass;
Nate Summers's avatar
Nate Summers committed
160 161 162 163 164 165 166 167 168

  parent_class = gtk_type_class (GIMP_TYPE_DRAW_TOOL);

  gimp_transform_tool_signals[TRANSFORM] =
    gtk_signal_new ("transform",
    		    GTK_RUN_FIRST,
    		    object_class->type,
    		    GTK_SIGNAL_OFFSET (GimpTransformToolClass,
    		    		       transform),
Nate Summers's avatar
Nate Summers committed
169
    		    gimp_marshal_POINTER__POINTER_INT,
Nate Summers's avatar
Nate Summers committed
170 171 172 173 174 175 176
    		    GTK_TYPE_POINTER, 2,
    		    GTK_TYPE_POINTER,
    		    GTK_TYPE_INT);

  gtk_object_class_add_signals (object_class, gimp_transform_tool_signals,
				LAST_SIGNAL);

Nate Summers's avatar
Nate Summers committed
177
  object_class->destroy      = gimp_transform_tool_destroy;
Nate Summers's avatar
Nate Summers committed
178 179 180 181 182 183

  tool_class->button_press   = gimp_transform_tool_button_press;
  tool_class->button_release = gimp_transform_tool_button_release;
  tool_class->motion         = gimp_transform_tool_motion;
  tool_class->cursor_update  = gimp_transform_tool_cursor_update;
  tool_class->control        = gimp_transform_tool_control;
Nate Summers's avatar
Nate Summers committed
184 185 186 187 188 189 190

  /* FIXME if (interactive) */
  draw_class->draw           = gimp_transform_tool_draw;
  /* else
    private->core = gimp_draw_tool_new (gimp_transform_tool_no_draw); */


Nate Summers's avatar
Nate Summers committed
191 192 193
}

static void
Nate Summers's avatar
Nate Summers committed
194
gimp_transform_tool_init (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
195
{
Nate Summers's avatar
Nate Summers committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  GimpTool      *tool = GIMP_TOOL(tr_tool);
  gint           i;

  tr_tool->function = TRANSFORM_CREATING;
  tr_tool->original = NULL;

  tr_tool->bpressed = FALSE;

  for (i = 0; i < TRAN_INFO_SIZE; i++)
    tr_tool->trans_info[i] = 0;

  tr_tool->grid_coords = tr_tool->tgrid_coords = NULL;

  tool->scroll_lock = TRUE;   /*  Disallow scrolling  */
  tool->preserve    = FALSE;  /*  Don't preserve on drawable change  */
Nate Summers's avatar
Nate Summers committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}

static void
pixel_surround_init (PixelSurround *ps,
		     TileManager   *tm,
		     gint           w,
		     gint           h,
		     guchar         bg[MAX_CHANNELS])
{
  gint i;

  for (i = 0; i < MAX_CHANNELS; ++i)
    {
      ps->bg[i] = bg[i];
    }

  ps->tile       = NULL;
  ps->mgr        = tm;
  ps->bpp        = tile_manager_bpp (tm);
  ps->w          = w;
  ps->h          = h;
  /* make sure buffer is big enough */
  ps->buff_size  = w * h * ps->bpp;
  ps->buff       = g_malloc (ps->buff_size);
  ps->row_stride = 0;
}

/* return a pointer to a buffer which contains all the surrounding pixels */
/* strategy: if we are in the middle of a tile, use the tile storage */
/* otherwise just copy into our own malloced buffer and return that */

static guchar *
pixel_surround_lock (PixelSurround *ps,
		     gint           x,
		     gint           y)
{
  gint    i, j;
  guchar *k;
  guchar *ptr;

  ps->tile = tile_manager_get_tile (ps->mgr, x, y, TRUE, FALSE);

  i = x % TILE_WIDTH;
  j = y % TILE_HEIGHT;

  /* do we have the whole region? */
  if (ps->tile &&
      (i < (tile_ewidth(ps->tile) - ps->w)) &&
      (j < (tile_eheight(ps->tile) - ps->h)))
    {
      ps->row_stride = tile_ewidth (ps->tile) * ps->bpp;
      /* is this really the correct way? */
      return tile_data_pointer (ps->tile, i, j);
    }

  /* nope, do this the hard way (for now) */
  if (ps->tile)
    {
      tile_release (ps->tile, FALSE);
      ps->tile = 0;
    }

  /* copy pixels, one by one */
  /* no, this is not the best way, but it's much better than before */
  ptr = ps->buff;
  for (j = y; j < y+ps->h; ++j)
    {
      for (i = x; i < x+ps->w; ++i)
	{
	  Tile *tile = tile_manager_get_tile (ps->mgr, i, j, TRUE, FALSE);

	  if (tile)
	    {
	      guchar *buff = tile_data_pointer (tile,
						i % TILE_WIDTH,
						j % TILE_HEIGHT);

	      for (k = buff; k < buff+ps->bpp; ++k, ++ptr)
		{
		  *ptr = *k;
		}
	      tile_release (tile, FALSE);
	    }
	  else
	    {
	      for (k = ps->bg; k < ps->bg+ps->bpp; ++k, ++ptr)
		{
		  *ptr = *k;
		}
	    }
	}
    }
  ps->row_stride = ps->w * ps->bpp;

  return ps->buff;
}

static gint
pixel_surround_rowstride (PixelSurround *ps)
{
  return ps->row_stride;
}

static void
pixel_surround_release (PixelSurround *ps)
{
  /* always get new tile (for now), so release the old one */
  if (ps->tile)
    {
      tile_release (ps->tile, FALSE);
      ps->tile = 0;
    }
}

static void
pixel_surround_clear (PixelSurround *ps)
{
  if (ps->buff)
    {
      g_free (ps->buff);
      ps->buff = 0;
      ps->buff_size = 0;
    }
}

static void
transform_ok_callback (GtkWidget *widget,
		       gpointer   data)
{
Nate Summers's avatar
Nate Summers committed
340
  GimpTool *tool;
Nate Summers's avatar
Nate Summers committed
341

Nate Summers's avatar
Nate Summers committed
342 343
  tool = GIMP_TOOL(data);
  gimp_transform_tool_doit (GIMP_TRANSFORM_TOOL(tool), tool->gdisp);
Nate Summers's avatar
Nate Summers committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357
}

static void
transform_reset_callback (GtkWidget *widget,
			  gpointer   data)
{
  GimpTransformTool   *tool;
  GimpDrawTool        *dr_tool;
  gint                 i;

  tool    = GIMP_TRANSFORM_TOOL(data);
  dr_tool = GIMP_DRAW_TOOL(data);

  /*  stop the current tool drawing process  */
Nate Summers's avatar
Nate Summers committed
358
  gimp_draw_tool_pause (dr_tool);
Nate Summers's avatar
Nate Summers committed
359 360 361 362 363 364

  /*  Restore the previous transformation info  */
  for (i = 0; i < TRAN_INFO_SIZE; i++)
    tool->trans_info [i] = old_trans_info [i];

  /*  recalculate the tool's transformation matrix  */
Nate Summers's avatar
Nate Summers committed
365
  gimp_transform_tool_recalc (tool, GIMP_TOOL(tool)->gdisp);
Nate Summers's avatar
Nate Summers committed
366 367

  /*  resume drawing the current tool  */
Nate Summers's avatar
Nate Summers committed
368
  gimp_draw_tool_resume (dr_tool);
Nate Summers's avatar
Nate Summers committed
369 370 371 372 373 374 375 376 377 378 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
}

static const gchar *action_labels[] =
{
  N_("Rotate"),
  N_("Scale"),
  N_("Shear"),
  N_("Transform")
};

void
gimp_transform_tool_button_press (GimpTransformTool  *gt_tool,
                                  GdkEventButton     *bevent,
			          GDisplay           *gdisp)
{
  GimpTool      *tool;
  GimpDrawable  *drawable;
  gint           dist;
  gint           closest_dist;
  gint           x, y;
  gint           i;
  gint           off_x, off_y;

  tool = GIMP_TOOL(gt_tool);

  gt_tool->bpressed = TRUE; /* ALT */

  drawable = gimp_image_active_drawable (gdisp->gimage);

  if (gt_tool->function == TRANSFORM_CREATING && tool->state == ACTIVE)
    {
      /*  Save the current transformation info  */
      for (i = 0; i < TRAN_INFO_SIZE; i++)
	old_trans_info [i] = gt_tool->trans_info [i];
    }

  /*  if we have already displayed the bounding box and handles,
   *  check to make sure that the display which currently owns the
   *  tool is the one which just received the button pressed event
   */
  if ((gdisp == tool->gdisp) && gt_tool->interactive)
    {
      /*  start drawing the bounding box and handles...  */
      gimp_draw_tool_start (GIMP_DRAW_TOOL(gt_tool), gdisp->canvas->window);
	
      x = bevent->x;
      y = bevent->y;

      closest_dist = SQR (x - gt_tool->sx1) + SQR (y - gt_tool->sy1);
      gt_tool->function = TRANSFORM_HANDLE_1;

      dist = SQR (x - gt_tool->sx2) + SQR (y - gt_tool->sy2);
      if (dist < closest_dist)
	{
	  closest_dist = dist;
	  gt_tool->function = TRANSFORM_HANDLE_2;
	}

      dist = SQR (x - gt_tool->sx3) + SQR (y - gt_tool->sy3);
      if (dist < closest_dist)
	{
	  closest_dist = dist;
	  gt_tool->function = TRANSFORM_HANDLE_3;
	}

      dist = SQR (x - gt_tool->sx4) + SQR (y - gt_tool->sy4);
      if (dist < closest_dist)
	{
	  closest_dist = dist;
	  gt_tool->function = TRANSFORM_HANDLE_4;
	}

Nate Summers's avatar
Nate Summers committed
441
#ifdef FIXME_I_DO_NOT_BELONG_HERE
Nate Summers's avatar
Nate Summers committed
442 443 444 445 446 447
      if (tool->type == ROTATE
	  && (SQR (x - gt_tool->scx) +
	      SQR (y - gt_tool->scy)) <= 100)
	{
	  gt_tool->function = TRANSFORM_HANDLE_CENTER;
	}
Nate Summers's avatar
Nate Summers committed
448
#endif
Nate Summers's avatar
Nate Summers committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 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 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

      /*  Save the current pointer position  */
      gdisplay_untransform_coords (gdisp, bevent->x, bevent->y,
				   &gt_tool->startx,
				   &gt_tool->starty, TRUE, 0);
      gt_tool->lastx = gt_tool->startx;
      gt_tool->lasty = gt_tool->starty;

      gdk_pointer_grab (gdisp->canvas->window, FALSE,
			GDK_POINTER_MOTION_HINT_MASK |
			GDK_BUTTON1_MOTION_MASK |
			GDK_BUTTON_RELEASE_MASK,
			NULL, NULL, bevent->time);

      tool->state = ACTIVE;
      return;
    }


  /*  Initialisation stuff: if the cursor is clicked inside the current
   *  selection, show the bounding box and handles...
   */
  gdisplay_untransform_coords (gdisp, bevent->x, bevent->y, &x, &y,
			       FALSE, FALSE);

  gimp_drawable_offsets (drawable, &off_x, &off_y);
  if (x >= off_x && y >= off_y &&
      x < (off_x + gimp_drawable_width (drawable)) &&
      y < (off_y + gimp_drawable_height (drawable)))
    if (gimage_mask_is_empty (gdisp->gimage) ||
	gimage_mask_value (gdisp->gimage, x, y))
      {
	if (GIMP_IS_LAYER (drawable) &&
	    gimp_layer_get_mask (GIMP_LAYER (drawable)))
	  {
	    g_message (_("Transformations do not work on\n"
			 "layers that contain layer masks."));
	    tool->state = INACTIVE;
	    return;
	  }

	/*  If the tool is already active, clear the current state
	 *  and reset
	 */
	if (tool->state == ACTIVE)
	  gimp_transform_tool_reset (gt_tool, gdisp);

	/*  Set the pointer to the active display  */
	tool->gdisp    = gdisp;
	tool->drawable = drawable;
	tool->state    = ACTIVE;

	/*  Grab the pointer if we're in non-interactive mode  */
	if (!gt_tool->interactive)
	  gdk_pointer_grab (gdisp->canvas->window, FALSE,
			    (GDK_POINTER_MOTION_HINT_MASK |
			     GDK_BUTTON1_MOTION_MASK |
			     GDK_BUTTON_RELEASE_MASK),
			    NULL, NULL, bevent->time);

	/*  Find the transform bounds for some tools (like scale,
	 *  perspective) that actually need the bounds for
	 *  initializing
	 */
	gimp_transform_tool_bounds (gt_tool, gdisp);

	/*  Calculate the grid line endpoints  */
	if (gimp_transform_tool_show_grid ())
	  gimp_transform_tool_setup_grid (tool);

	/*  Initialize the transform tool */
	/*FIXME*/ (* gt_tool->trans_func) (tool, gdisp, TRANSFORM_INIT);

	if (transform_info && !transform_info_inited)
	  {
	    gimp_dialog_create_action_area
	      (GTK_DIALOG (transform_info->shell),

	       gettext (action_labels[tool->type - ROTATE]),
	       transform_ok_callback,
	       tool, NULL, NULL, TRUE, FALSE,
	       _("Reset"), transform_reset_callback,
	       tool, NULL, NULL, FALSE, FALSE,

	       NULL);

	    transform_info_inited = TRUE;
	  }

	/*  Recalculate the transform tool  */
	gimp_transform_tool_recalc (gt_tool, gdisp);

	/*  recall this function to find which handle we're dragging  */
	if (gimp_transform_tool->interactive)
	  gimp_transform_tool_button_press (gt_tool, bevent, gdisp);
      }
}

void
gimp_transform_tool_button_release (GimpTransformTool  *gt_tool,
			            GdkEventButton     *bevent,
			            GDisplay           *gdisp)
{
  GimpTool *tool;
  gint      i;

  tool = GIMP_TOOL(tool);

  gt_tool->bpressed = FALSE; /* ALT */

  /*  if we are creating, there is nothing to be done...exit  */
  if (gt_tool->function == TRANSFORM_CREATING &&
      gt_tool->interactive)
    return;

  /*  release of the pointer grab  */
  gdk_pointer_ungrab (bevent->time);
  gdk_flush ();

  /*  if the 3rd button isn't pressed, transform the selected mask  */
  if (! (bevent->state & GDK_BUTTON3_MASK))
    {
      /* Shift-clicking is another way to approve the transform  */
      if ((bevent->state & GDK_SHIFT_MASK) || (tool->type == FLIP))
	{
	  gimp_transform_tool_doit (tool, gdisp);
	}
      else
	{
	  /*  Only update the paths preview */
	  path_transform_current_path (gdisp->gimage,
				       gt_tool->transform, TRUE);
	}
    }
  else
    {
      /*  stop the current tool drawing process  */
      gimp_draw_tool_pause (GIMP_DRAW_TOOL(gt_tool));

      /*  Restore the previous transformation info  */
      for (i = 0; i < TRAN_INFO_SIZE; i++)
	gt_tool->trans_info [i] = old_trans_info [i];

      /*  recalculate the tool's transformation matrix  */
      gimp_transform_tool_recalc (gt_tool, gdisp);

      /*  resume drawing the current tool  */
      gimp_draw_tool_resume (GIMP_DRAW_TOOL(gt_tool);

      /* Update the paths preview */
      path_transform_current_path (gdisp->gimage,
				   gt_tool->transform, TRUE);
    }

  /*  if this tool is non-interactive, make it inactive after use  */
  if (!gt_tool->interactive)
    tool->state = INACTIVE;
}

void
gimp_transform_tool_doit (GimpTransformTool  *gt_tool,
		          GDisplay           *gdisp)
{
  GimpTool      *tool;
  TileManager   *new_tiles;
  TransformUndo *tu;
  PathUndo      *pundo;
  gboolean       new_layer;
  gint           i, x, y;

  gimp_add_busy_cursors ();

  gimp_transform_tool = GIMP_TOOL(tool);

  /* undraw the tool before we muck around with the transform matrix */
  gimp_draw_tool_pause (GIMP_DRAW_TOOL(gt_tool));

  /*  We're going to dirty this image, but we want to keep the tool
   *  around
   */
  tool->preserve = TRUE;

  /*  Start a transform undo group  */
  undo_push_group_start (gdisp->gimage, TRANSFORM_CORE_UNDO);

  /*  With the old UI, if original is NULL, then this is the
      first transformation. In the new UI, it is always so, right?  */
  g_assert (gt_tool->original == NULL);

  /* If we're in interactive mode, we need to copy the current
   *  selection to the transform tool's private selection pointer, so
   *  that the original source can be repeatedly modified.
   */
  tool->drawable = gimp_image_active_drawable (gdisp->gimage);

  gt_tool->original = gimp_transform_tool_cut (gdisp->gimage,
					       tool->drawable,
					       &new_layer);

  pundo = path_transform_start_undo (gdisp->gimage);

  /*  Send the request for the transformation to the tool...
   */
  /* FIXME */ new_tiles = (* gimp_transform_tool->trans_func) (gt_tool, gdisp,
					      TRANSFORM_FINISH);

  /* FIXME TOO */(* gimp_transform_tool->trans_func) (gt_tool, gdisp, TRANSFORM_INIT);

  gimp_transform_tool_recalc (gt_tool, gdisp);

  if (new_tiles)
    {
      /*  paste the new transformed image to the gimage...also implement
       *  undo...
       */
      /*  FIXME: we should check if the drawable is still valid  */
      gimp_transform_tool_paste (gdisp->gimage, tool->drawable,
			    new_tiles, new_layer);

      /*  create and initialize the transform_undo structure  */
      tu = g_new (TransformUndo, 1);
      tu->tool_ID = tool->ID;
      tu->tool_type = tool->type;
      for (i = 0; i < TRAN_INFO_SIZE; i++)
	tu->trans_info[i] = old_trans_info[i];
      tu->original = NULL;
      tu->path_undo = pundo;

      /*  Make a note of the new current drawable (since we may have
       *  a floating selection, etc now.
       */
      tool->drawable = gimp_image_active_drawable (gdisp->gimage);

      undo_push_transform (gdisp->gimage, (void *) tu);
    }

  /*  push the undo group end  */
  undo_push_group_end (gdisp->gimage);

  /*  We're done dirtying the image, and would like to be restarted
   *  if the image gets dirty while the tool exists
   */
  tool->preserve = FALSE;

  /*  Flush the gdisplays  */
  if (gdisp->disp_xoffset || gdisp->disp_yoffset)
    {
      gdk_window_get_size (gdisp->canvas->window, &x, &y);
      if (gdisp->disp_yoffset)
	{
	  gdisplay_expose_area (gdisp, 0, 0, gdisp->disp_width,
				gdisp->disp_yoffset);
	  gdisplay_expose_area (gdisp, 0, gdisp->disp_yoffset + y,
				gdisp->disp_width, gdisp->disp_height);
	}
      if (gdisp->disp_xoffset)
	{
	  gdisplay_expose_area (gdisp, 0, 0, gdisp->disp_xoffset,
				gdisp->disp_height);
	  gdisplay_expose_area (gdisp, gdisp->disp_xoffset + x, 0,
				gdisp->disp_width, gdisp->disp_height);
	}
    }

  gimp_remove_busy_cursors (NULL);

  gdisplays_flush ();

  gimp_transform_tool_reset (tool, gdisp);

  /*  if this tool is non-interactive, make it inactive after use  */
  if (!gimp_transform_tool->interactive)
    tool->state = INACTIVE;
}


void
Nate Summers's avatar
Nate Summers committed
726 727 728
gimp_transform_tool_motion (GimpTransformTool  *tr_tool,
		            GdkEventMotion     *mevent,
		            GDisplay           *gdisp)
Nate Summers's avatar
Nate Summers committed
729
{
Nate Summers's avatar
Nate Summers committed
730
  GimpTool *tool;
Nate Summers's avatar
Nate Summers committed
731

Nate Summers's avatar
Nate Summers committed
732
  tool = GIMP_TOOL(tr_tool);
Nate Summers's avatar
Nate Summers committed
733

Nate Summers's avatar
Nate Summers committed
734
  if (tr_tool->bpressed == FALSE)
Nate Summers's avatar
Nate Summers committed
735 736 737 738 739 740 741 742 743 744
  {
    /*  hey we have not got the button press yet
     *  so go away.
     */
    return;
  }

  /*  if we are creating or this tool is non-interactive, there is
   *  nothing to be done so exit.
   */
Nate Summers's avatar
Nate Summers committed
745 746
  if (tr_tool->function == TRANSFORM_CREATING ||
      !tr_tool->interactive)
Nate Summers's avatar
Nate Summers committed
747 748 749
    return;

  /*  stop the current tool drawing process  */
Nate Summers's avatar
Nate Summers committed
750
  gimp_draw_tool_pause (GIMP_DRAW_TOOL(tool));
Nate Summers's avatar
Nate Summers committed
751 752

  gdisplay_untransform_coords (gdisp, mevent->x, mevent->y,
Nate Summers's avatar
Nate Summers committed
753 754 755
			       &tr_tool->curx,
			       &tr_tool->cury, TRUE, 0);
  tr_tool->state = mevent->state;
Nate Summers's avatar
Nate Summers committed
756 757

  /*  recalculate the tool's transformation matrix  */
Nate Summers's avatar
Nate Summers committed
758
  /* FIXME */ (* gimp_transform_tool->trans_func) (tool, gdisp, TRANSFORM_MOTION);
Nate Summers's avatar
Nate Summers committed
759

Nate Summers's avatar
Nate Summers committed
760 761
  tr_tool->lastx = tr_tool->curx;
  tr_tool->lasty = tr_tool->cury;
Nate Summers's avatar
Nate Summers committed
762 763

  /*  resume drawing the current tool  */
Nate Summers's avatar
Nate Summers committed
764
  gimp_draw_tool_resume (GIMP_DRAW_TOOL(tool));
Nate Summers's avatar
Nate Summers committed
765 766 767
}

void
Nate Summers's avatar
Nate Summers committed
768 769 770
gimp_transform_tool_cursor_update (GimpTransformTool  *tr_tool,
			           GdkEventMotion     *mevent,
			           GDisplay           *gdisp)
Nate Summers's avatar
Nate Summers committed
771
{
Nate Summers's avatar
Nate Summers committed
772
  GimpTool      *tool;
Nate Summers's avatar
Nate Summers committed
773 774 775 776
  GimpDrawable  *drawable;
  GdkCursorType  ctype = GDK_TOP_LEFT_ARROW;
  gint           x, y;

Nate Summers's avatar
Nate Summers committed
777
  tool = GIMP_TOOL(tool));
Nate Summers's avatar
Nate Summers committed
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

  gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y,
			       FALSE, FALSE);

  if ((drawable = gimp_image_active_drawable (gdisp->gimage)))
    {
      if (GIMP_IS_LAYER (drawable) &&
	  gimp_layer_get_mask (GIMP_LAYER (drawable)))
	{
	  ctype = GIMP_BAD_CURSOR;
	}
      else if (x >= drawable->offset_x &&
	       y >= drawable->offset_y &&
	       x < (drawable->offset_x + drawable->width) &&
	       y < (drawable->offset_y + drawable->height))
	{
	  if (gimage_mask_is_empty (gdisp->gimage) ||
	      gimage_mask_value (gdisp->gimage, x, y))
	    {
	      ctype = GIMP_MOUSE_CURSOR;
	    }
	}
    }

  gdisplay_install_tool_cursor (gdisp,
				ctype,
				tool->tool_cursor,
				GIMP_CURSOR_MODIFIER_NONE);
}

void
Nate Summers's avatar
Nate Summers committed
809 810 811
gimp_transform_tool_control (GimpTransformTool        *tr_tool,
			     GimpTransformToolAction   action,
			     GDisplay                 *gdisp)
Nate Summers's avatar
Nate Summers committed
812
{
Nate Summers's avatar
Nate Summers committed
813
  GimpDrawTool *tool;
Nate Summers's avatar
Nate Summers committed
814

Nate Summers's avatar
Nate Summers committed
815
  tool = GIMP_DRAW_TOOL(tool);
Nate Summers's avatar
Nate Summers committed
816 817 818 819

  switch (action)
    {
    case PAUSE:
Nate Summers's avatar
Nate Summers committed
820
      gimp_draw_tool_pause (tool);
Nate Summers's avatar
Nate Summers committed
821 822 823
      break;

    case RESUME:
Nate Summers's avatar
Nate Summers committed
824 825
      gimp_transform_tool_recalc (tr_tool, gdisp);
      gimp_draw_tool_resume (tool);
Nate Summers's avatar
Nate Summers committed
826 827 828
      break;

    case HALT:
Nate Summers's avatar
Nate Summers committed
829
      gimp_transform_tool_reset (tr_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843
      break;

    default:
      break;
    }
}

void
gimp_transform_tool_no_draw (GimpTransformTool *tool)
{
  return;
}

void
Nate Summers's avatar
Nate Summers committed
844
gimp_transform_tool_draw (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
845 846
{
  GDisplay      *gdisp;
Nate Summers's avatar
Nate Summers committed
847 848
  GimpTool      *tool;
  GimpDrawTool  *dr_tool;
Nate Summers's avatar
Nate Summers committed
849 850 851 852 853
  gint           x1, y1, x2, y2, x3, y3, x4, y4;
  gint           srw, srh;
  gint           i, k, gci;
  gint           xa, ya, xb, yb;

Nate Summers's avatar
Nate Summers committed
854
  tool           = GIMP_TOOL(tool);
Nate Summers's avatar
Nate Summers committed
855
  gdisp          = tool->gdisp;
Nate Summers's avatar
Nate Summers committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869

  gdisplay_transform_coords (gdisp, tr_tool->tx1, tr_tool->ty1,
			     &tr_tool->sx1, &tr_tool->sy1, FALSE);
  gdisplay_transform_coords (gdisp, tr_tool->tx2, tr_tool->ty2,
			     &tr_tool->sx2, &tr_tool->sy2, FALSE);
  gdisplay_transform_coords (gdisp, tr_tool->tx3, tr_tool->ty3,
			     &tr_tool->sx3, &tr_tool->sy3, FALSE);
  gdisplay_transform_coords (gdisp, tr_tool->tx4, tr_tool->ty4,
			     &tr_tool->sx4, &tr_tool->sy4, FALSE);

  x1 = tr_tool->sx1;  y1 = tr_tool->sy1;
  x2 = tr_tool->sx2;  y2 = tr_tool->sy2;
  x3 = tr_tool->sx3;  y3 = tr_tool->sy3;
  x4 = tr_tool->sx4;  y4 = tr_tool->sy4;
Nate Summers's avatar
Nate Summers committed
870 871 872 873 874 875

  /*  find the handles' width and height  */
  srw = 10;
  srh = 10;

  /*  draw the bounding box  */
Nate Summers's avatar
Nate Summers committed
876
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
877
		 x1, y1, x2, y2);
Nate Summers's avatar
Nate Summers committed
878
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
879
		 x2, y2, x4, y4);
Nate Summers's avatar
Nate Summers committed
880
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
881
		 x3, y3, x4, y4);
Nate Summers's avatar
Nate Summers committed
882
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
883 884 885 886
		 x3, y3, x1, y1);

  /*  Draw the grid */

Nate Summers's avatar
Nate Summers committed
887 888
  if ((tr_tool->grid_coords != NULL) &&
      (tr_tool->tgrid_coords != NULL) &&
Nate Summers's avatar
Nate Summers committed
889
      ((tool->type != PERSPECTIVE) ||
Nate Summers's avatar
Nate Summers committed
890 891
       ((tr_tool->transform[0][0] >=0.0) &&
	(tr_tool->transform[1][1] >=0.0))))
Nate Summers's avatar
Nate Summers committed
892 893 894
    {

      gci = 0;
Nate Summers's avatar
Nate Summers committed
895
      k = tr_tool->ngx + tr_tool->ngy;
Nate Summers's avatar
Nate Summers committed
896 897
      for (i = 0; i < k; i++)
	{
Nate Summers's avatar
Nate Summers committed
898 899
	  gdisplay_transform_coords (gdisp, tr_tool->tgrid_coords[gci],
				     tr_tool->tgrid_coords[gci+1],
Nate Summers's avatar
Nate Summers committed
900
				     &xa, &ya, FALSE);
Nate Summers's avatar
Nate Summers committed
901 902
	  gdisplay_transform_coords (gdisp, tr_tool->tgrid_coords[gci+2],
				     tr_tool->tgrid_coords[gci+3],
Nate Summers's avatar
Nate Summers committed
903 904
				     &xb, &yb, FALSE);

Nate Summers's avatar
Nate Summers committed
905
	  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
906 907 908 909 910 911
			 xa, ya, xb, yb);
	  gci += 4;
	}
    }

  /*  draw the tool handles  */
Nate Summers's avatar
Nate Summers committed
912
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
913
		      x1 - (srw >> 1), y1 - (srh >> 1), srw, srh);
Nate Summers's avatar
Nate Summers committed
914
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
915
		      x2 - (srw >> 1), y2 - (srh >> 1), srw, srh);
Nate Summers's avatar
Nate Summers committed
916
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
917
		      x3 - (srw >> 1), y3 - (srh >> 1), srw, srh);
Nate Summers's avatar
Nate Summers committed
918
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
919 920
		      x4 - (srw >> 1), y4 - (srh >> 1), srw, srh);

Nate Summers's avatar
Nate Summers committed
921
#ifdef FIXME_I_DO_NOT_BELONG_HERE
Nate Summers's avatar
Nate Summers committed
922 923 924 925 926 927 928 929 930 931 932
  /*  draw the center  */
  if (tool->type == ROTATE)
    {
      gdisplay_transform_coords (gdisp, gimp_transform_tool->tcx, gimp_transform_tool->tcy,
				 &gimp_transform_tool->scx, &gimp_transform_tool->scy, FALSE);

      gdk_draw_arc (gimp_transform_tool->core->win, gimp_transform_tool->core->gc, 1,
		    gimp_transform_tool->scx - (srw >> 1),
		    gimp_transform_tool->scy - (srh >> 1),
		    srw, srh, 0, 23040);
    }
Nate Summers's avatar
Nate Summers committed
933
#endif
Nate Summers's avatar
Nate Summers committed
934 935 936 937 938 939 940

  if (transform_tool_showpath ())
    {
      GimpMatrix3 tmp_matrix;

      if (transform_tool_direction () == TRANSFORM_CORRECTIVE)
	{
Nate Summers's avatar
Nate Summers committed
941
	  gimp_matrix3_invert (tr_tool->transform, tmp_matrix);
Nate Summers's avatar
Nate Summers committed
942 943 944
	}
      else
	{
Nate Summers's avatar
Nate Summers committed
945
	  gimp_matrix3_duplicate (tr_tool->transform, tmp_matrix);
Nate Summers's avatar
Nate Summers committed
946 947
	}

Nate Summers's avatar
Nate Summers committed
948
      path_transform_draw_current (gdisp, dr_tool, tmp_matrix);
Nate Summers's avatar
Nate Summers committed
949 950 951 952
    }
}

void
Nate Summers's avatar
Nate Summers committed
953
gimp_transform_tool_destroy (GtkObject object)
Nate Summers's avatar
Nate Summers committed
954
{
Nate Summers's avatar
Nate Summers committed
955 956 957
  GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (object);
  GimpDrawTool      *dr_tool = GIMP_DRAW_TOOL      (tr_tool);
  GimpTool          *tool    = GIMP_TOOL           (tr_tool);
Nate Summers's avatar
Nate Summers committed
958 959 960

  /*  Make sure the selection core is not visible  */
  if (tool->state == ACTIVE)
Nate Summers's avatar
Nate Summers committed
961
    gimp_draw_tool_stop (dr_tool);
Nate Summers's avatar
Nate Summers committed
962 963

  /*  Free the selection core  */
Nate Summers's avatar
Nate Summers committed
964
  gimp_draw_tool_free (dr_tool);
Nate Summers's avatar
Nate Summers committed
965 966

  /*  Free up the original selection if it exists  */
Nate Summers's avatar
Nate Summers committed
967 968
  if (tr_tool->original)
    tile_manager_destroy (tr_tool->original);
Nate Summers's avatar
Nate Summers committed
969 970 971 972 973 974 975 976 977

  /*  If there is an information dialog, free it up  */
  if (transform_info)
    info_dialog_free (transform_info);

  transform_info        = NULL;
  transform_info_inited = FALSE;

  /*  Free the grid line endpoint arrays if they exist */
Nate Summers's avatar
Nate Summers committed
978 979
  if (tr_tool->grid_coords != NULL)
    g_free (tr_tool->grid_coords);
Nate Summers's avatar
Nate Summers committed
980

Nate Summers's avatar
Nate Summers committed
981 982
  if (tr_tool->tgrid_coords != NULL)
    g_free (tr_tool->tgrid_coords);
Nate Summers's avatar
Nate Summers committed
983 984 985 986

}

void
Nate Summers's avatar
Nate Summers committed
987
gimp_transform_tool_transform_bounding_box (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
988
{
Nate Summers's avatar
Nate Summers committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
  GimpTool  *tool;
  gint       i, k;
  gint       gci;

  tool = GIMP_TOOL(tr_tool);

  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x1, tr_tool->y1,
				&tr_tool->tx1, &tr_tool->ty1);
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x2, tr_tool->y1,
				&tr_tool->tx2, &tr_tool->ty2);
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x1, tr_tool->y2,
				&tr_tool->tx3, &tr_tool->ty3);
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x2, tr_tool->y2,
				&tr_tool->tx4, &tr_tool->ty4);
Nate Summers's avatar
Nate Summers committed
1007 1008

  if (tool->type == ROTATE)
Nate Summers's avatar
Nate Summers committed
1009 1010 1011
    gimp_matrix3_transform_point (tr_tool->transform,
				  tr_tool->cx, tr_tool->cy,
				  &tr_tool->tcx, &tr_tool->tcy);
Nate Summers's avatar
Nate Summers committed
1012

Nate Summers's avatar
Nate Summers committed
1013 1014
  if (tr_tool->grid_coords != NULL &&
      tr_tool->tgrid_coords != NULL)
Nate Summers's avatar
Nate Summers committed
1015 1016
    {
      gci = 0;
Nate Summers's avatar
Nate Summers committed
1017
      k  = (tr_tool->ngx + tr_tool->ngy) * 2;
Nate Summers's avatar
Nate Summers committed
1018 1019
      for (i = 0; i < k; i++)
	{
Nate Summers's avatar
Nate Summers committed
1020 1021 1022 1023 1024
	  gimp_matrix3_transform_point (tr_tool->transform,
					tr_tool->grid_coords[gci],
					tr_tool->grid_coords[gci+1],
					&(tr_tool->tgrid_coords[gci]),
					&(tr_tool->tgrid_coords[gci+1]));
Nate Summers's avatar
Nate Summers committed
1025 1026 1027 1028 1029 1030
	  gci += 2;
	}
    }
}

void
Nate Summers's avatar
Nate Summers committed
1031 1032
gimp_transform_tool_reset (GimpTransformTool   *tr_tool,
		           GDisplay            *gdisp)
Nate Summers's avatar
Nate Summers committed
1033
{
Nate Summers's avatar
Nate Summers committed
1034
  GimpTool *tool;
Nate Summers's avatar
Nate Summers committed
1035

Nate Summers's avatar
Nate Summers committed
1036
  tool = GIMP_TOOL(tool);
Nate Summers's avatar
Nate Summers committed
1037

Nate Summers's avatar
Nate Summers committed
1038 1039 1040
  if (tr_tool->original)
    tile_manager_destroy (tr_tool->original);
  tr_tool->original = NULL;
Nate Summers's avatar
Nate Summers committed
1041 1042

  /*  inactivate the tool  */
Nate Summers's avatar
Nate Summers committed
1043 1044
  tr_tool->function = TRANSFORM_CREATING;
  gimp_draw_tool_stop (GIMP_DRAW_TOOL(tr_tool));
Nate Summers's avatar
Nate Summers committed
1045 1046 1047 1048 1049 1050 1051 1052
  info_dialog_popdown (transform_info);

  tool->state    = INACTIVE;
  tool->gdisp    = NULL;
  tool->drawable = NULL;
}

static void
Nate Summers's avatar
Nate Summers committed
1053 1054
gimp_transform_tool_bounds (GimpTransformTool   *tr_tool,
		            GDisplay            *gdisp)
Nate Summers's avatar
Nate Summers committed
1055
{
Nate Summers's avatar
Nate Summers committed
1056
  GimpTool      *tool;
Nate Summers's avatar
Nate Summers committed
1057 1058 1059 1060
  TileManager   *tiles;
  GimpDrawable  *drawable;
  gint           offset_x, offset_y;

Nate Summers's avatar
Nate Summers committed
1061 1062
  tiles      = tr_tool->original;
  drawable   = gimp_image_active_drawable (gdisp->gimage);
Nate Summers's avatar
Nate Summers committed
1063 1064 1065 1066 1067

  /*  find the boundaries  */
  if (tiles)
    {
      tile_manager_get_offsets (tiles,
Nate Summers's avatar
Nate Summers committed
1068 1069 1070 1071
				&tr_tool->x1, &tr_tool->y1);
				
      tr_tool->x2 = tr_tool->x1 + tile_manager_width (tiles);
      tr_tool->y2 = tr_tool->y1 + tile_manager_height (tiles);
Nate Summers's avatar
Nate Summers committed
1072 1073 1074 1075 1076
    }
  else
    {
      gimp_drawable_offsets (drawable, &offset_x, &offset_y);
      gimp_drawable_mask_bounds (drawable,
Nate Summers's avatar
Nate Summers committed
1077 1078 1079 1080 1081 1082
				 &tr_tool->x1, &tr_tool->y1,
				 &tr_tool->x2, &tr_tool->y2);
      tr_tool->x1 += offset_x;
      tr_tool->y1 += offset_y;
      tr_tool->x2 += offset_x;
      tr_tool->y2 += offset_y;
Nate Summers's avatar
Nate Summers committed
1083
    }
Nate Summers's avatar
Nate Summers committed
1084 1085 1086

  tr_tool->cx = (tr_tool->x1 + tr_tool->x2) / 2;
  tr_tool->cy = (tr_tool->y1 + tr_tool->y2) / 2;
Nate Summers's avatar
Nate Summers committed
1087 1088

  /*  changing the bounds invalidates any grid we may have  */
Nate Summers's avatar
Nate Summers committed
1089
  gimp_transform_tool_grid_recalc (tr_tool);
Nate Summers's avatar
Nate Summers committed
1090 1091 1092 1093 1094
}

void
gimp_transform_tool_grid_density_changed (void)
{
Nate Summers's avatar
Nate Summers committed
1095 1096
  GimpTransformTool *tr_tool;
  GimpDrawTool      *dr_tool;
Nate Summers's avatar
Nate Summers committed
1097 1098

  /* EEEEEEEK!!! */
Nate Summers's avatar
Nate Summers committed
1099 1100
  tr_tool = GIMP_TRANSFORM_TOOL(active_tool);
  dr_tool = GIMP_DRAW_TOOL(tr_tool);
Nate Summers's avatar
Nate Summers committed
1101

Nate Summers's avatar
Nate Summers committed
1102
  if (tr_tool->function == TRANSFORM_CREATING)
Nate Summers's avatar
Nate Summers committed
1103 1104
    return;

Nate Summers's avatar
Nate Summers committed
1105 1106 1107 1108
  gimp_draw_tool_pause (dr_tool);
  gimp_transform_tool_grid_recalc (tr_tool);
  gimp_transform_tool_transform_bounding_box (tr_tool);
  gimp_draw_tool_resume (dr_tool);
Nate Summers's avatar
Nate Summers committed
1109 1110 1111
}

void
Nate Summers's avatar
Nate Summers committed
1112
gimp_transform_tool_showpath_changed (gint type /* a truly undescriptive name */)
Nate Summers's avatar
Nate Summers committed
1113
{
Nate Summers's avatar
Nate Summers committed
1114
  GimpTransformTool *tr_tool;
Nate Summers's avatar
Nate Summers committed
1115

Nate Summers's avatar
Nate Summers committed
1116 1117
  /* EEEEEEEK!!! */
  tr_tool = (GimpTransformTool *) active_tool->private;
Nate Summers's avatar
Nate Summers committed
1118

Nate Summers's avatar
Nate Summers committed
1119
  if (tr_tool->function == TRANSFORM_CREATING)
Nate Summers's avatar
Nate Summers committed
1120 1121 1122
    return;

  if (type)
Nate Summers's avatar
Nate Summers committed
1123
    gimp_draw_tool_pause (GIMP_DRAW_TOOL(tr_tool));
Nate Summers's avatar
Nate Summers committed
1124
  else
Nate Summers's avatar
Nate Summers committed
1125
    gimp_draw_tool_resume (GIMP_DRAW_TOOL(tr_tool));
Nate Summers's avatar
Nate Summers committed
1126 1127 1128
}

static void
Nate Summers's avatar
Nate Summers committed
1129
gimp_transform_tool_grid_recalc (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
1130
{
Nate Summers's avatar
Nate Summers committed
1131
  if (tr_tool->grid_coords != NULL)
Nate Summers's avatar
Nate Summers committed
1132
    {
Nate Summers's avatar
Nate Summers committed
1133 1134
      g_free (tr_tool->grid_coords);
      tr_tool->grid_coords = NULL;
Nate Summers's avatar
Nate Summers committed
1135
    }
Nate Summers's avatar
Nate Summers committed
1136
  if (tr_tool->tgrid_coords != NULL)
Nate Summers's avatar
Nate Summers committed
1137
    {
Nate Summers's avatar
Nate Summers committed
1138 1139
      g_free (tr_tool->tgrid_coords);
      tr_tool->tgrid_coords = NULL;
Nate Summers's avatar
Nate Summers committed
1140 1141
    }
  if (transform_tool_show_grid ())
Nate Summers's avatar
Nate Summers committed
1142
    gimp_transform_tool_setup_grid (/* EEEEEEK!!! */ GIMP_TRANSFORM_TOOL(active_tool));
Nate Summers's avatar
Nate Summers committed
1143 1144 1145
}

static void
Nate Summers's avatar
Nate Summers committed
1146
gimp_transform_tool_setup_grid (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
1147
{
Nate Summers's avatar
Nate Summers committed
1148 1149 1150
  GimpTool   *tool;
  gint        i, gci;
  gdouble    *coords;
Nate Summers's avatar
Nate Summers committed
1151

Nate Summers's avatar
Nate Summers committed
1152
  tool = GIMP_TOOL(tr_tool);
Nate Summers's avatar
Nate Summers committed
1153 1154 1155 1156 1157

  /*  We use the transform_tool_grid_size function only here, even
   *  if the user changes the grid size in the middle of an
   *  operation, nothing happens.
   */
Nate Summers's avatar
Nate Summers committed
1158 1159 1160 1161
  tr_tool->ngx =
    (tr_tool->x2 - tr_tool->x1) / transform_tool_grid_size ();
  if (tr_tool->ngx > 0)
    tr_tool->ngx--;
Nate Summers's avatar
Nate Summers committed
1162

Nate Summers's avatar
Nate Summers committed
1163 1164 1165 1166
 tr_tool->ngy =
    (tr_tool->y2 - tr_tool->y1) / transform_tool_grid_size ();
  if (tr_tool->ngy > 0)
    tr_tool->ngy--;
Nate Summers's avatar
Nate Summers committed
1167

Nate Summers's avatar
Nate Summers committed
1168 1169
  tr_tool->grid_coords = coords =
    g_new (double, (tr_tool->ngx + tr_tool->ngy) * 4);
Nate Summers's avatar
Nate Summers committed
1170

Nate Summers's avatar
Nate Summers committed
1171 1172
  tr_tool->tgrid_coords =
    g_new (double, (tr_tool->ngx + tr_tool->ngy) * 4);
Nate Summers's avatar
Nate Summers committed
1173 1174

  gci = 0;
Nate Summers's avatar
Nate Summers committed
1175
  for (i = 1; i <= tr_tool->ngx; i++)
Nate Summers's avatar
Nate Summers committed
1176
    {
Nate Summers's avatar
Nate Summers committed
1177 1178 1179 1180
      coords[gci] = tr_tool->x1 +
	((double) i)/(tr_tool->ngx + 1) *
	(tr_tool->x2 - tr_tool->x1);
      coords[gci+1] = tr_tool->y1;
Nate Summers's avatar
Nate Summers committed
1181
      coords[gci+2] = coords[gci];
Nate Summers's avatar
Nate Summers committed
1182
      coords[gci+3] = tr_tool->y2;
Nate Summers's avatar
Nate Summers committed
1183 1184
      gci += 4;
    }
Nate Summers's avatar
Nate Summers committed
1185
  for (i = 1; i <= tr_tool->ngy; i++)
Nate Summers's avatar
Nate Summers committed
1186
    {
Nate Summers's avatar
Nate Summers committed
1187 1188 1189 1190 1191
      coords[gci] = tr_tool->x1;
      coords[gci+1] = tr_tool->y1 +
	((double) i)/(tr_tool->ngy + 1) *
	(tr_tool->y2 - tr_tool->y1);
      coords[gci+2] = tr_tool->x2;
Nate Summers's avatar
Nate Summers committed
1192 1193 1194 1195 1196 1197
      coords[gci+3] = coords[gci+1];
      gci += 4;
    }
}

static void
Nate Summers's avatar
Nate Summers committed
1198 1199
gimp_transform_tool_recalc (GimpTransformTool   *tr_tool,
		            GDisplay            *gdisp)
Nate Summers's avatar
Nate Summers committed
1200
{
Nate Summers's avatar
Nate Summers committed
1201
  gimp_transform_tool_bounds (tr_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
1202

Nate Summers's avatar
Nate Summers committed
1203
  /* FIXME */ (* gimp_transform_tool->trans_func) (tool, gdisp, TRANSFORM_RECALC);
Nate Summers's avatar
Nate Summers committed
1204 1205 1206 1207 1208
}

/*  Actually carry out a transformation  */
TileManager *
gimp_transform_tool_do (GImage           *gimage,
Nate Summers's avatar
Nate Summers committed
1209 1210 1211 1212 1213 1214
                        GimpDrawable     *drawable,
                        TileManager      *float_tiles,
                        gboolean          interpolation,
                        GimpMatrix3       matrix,
                        GimpProgressFunc  progress_callback,
                        gpointer          progress_data)
Nate Summers's avatar
Nate Summers committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
{
  PixelRegion  destPR;
  TileManager *tiles;
  GimpMatrix3  m;
  GimpMatrix3  im