gimpmeasuretool.c 28.8 KB
Newer Older
Sven Neumann's avatar
Sven Neumann committed
1
2
3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * Measure tool
5
 * Copyright (C) 1999-2003 Sven Neumann <sven@gimp.org>
6
 *
Sven Neumann's avatar
Sven Neumann committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * 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
21

22
#include "config.h"
23

24
25
#include <stdlib.h>

26
#include <gtk/gtk.h>
27
#include <gdk/gdkkeysyms.h>
Sven Neumann's avatar
Sven Neumann committed
28

29
#include "libgimpmath/gimpmath.h"
30
#include "libgimpwidgets/gimpwidgets.h"
31

32
33
#include "tools-types.h"

34
#include "core/gimpimage.h"
35
#include "core/gimpimage-guides.h"
36
#include "core/gimpimage-undo.h"
37
#include "core/gimpimage-undo-push.h"
38
#include "core/gimptoolinfo.h"
39
#include "core/gimpunit.h"
40

41
#include "widgets/gimphelp-ids.h"
42
#include "widgets/gimptooldialog.h"
43
44
#include "widgets/gimpviewabledialog.h"

45
#include "display/gimpdisplay.h"
Michael Natterer's avatar
Michael Natterer committed
46
#include "display/gimpdisplayshell.h"
47

48
#include "gimpmeasureoptions.h"
49
#include "gimpmeasuretool.h"
50
#include "gimptoolcontrol.h"
51
#include "tools-utils.h"
Seth Burgess's avatar
Seth Burgess committed
52

53
#include "gimp-intl.h"
54

55

56
57
58
/*  definitions  */
#define  TARGET         8
#define  ARC_RADIUS     30
59

60

61
/*  local function prototypes  */
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
87
88
89
90
91
92
93
94
95
96
static void     gimp_measure_tool_class_init      (GimpMeasureToolClass *klass);
static void     gimp_measure_tool_init            (GimpMeasureTool      *tool);

static void     gimp_measure_tool_control         (GimpTool        *tool,
                                                   GimpToolAction   action,
                                                   GimpDisplay     *gdisp);
static void     gimp_measure_tool_button_press    (GimpTool        *tool,
                                                   GimpCoords      *coords,
                                                   guint32          time,
                                                   GdkModifierType  state,
                                                   GimpDisplay     *gdisp);
static void     gimp_measure_tool_button_release  (GimpTool        *tool,
                                                   GimpCoords      *coords,
                                                   guint32          time,
                                                   GdkModifierType  state,
                                                   GimpDisplay     *gdisp);
static void     gimp_measure_tool_motion          (GimpTool        *tool,
                                                   GimpCoords      *coords,
                                                   guint32          time,
                                                   GdkModifierType  state,
                                                   GimpDisplay     *gdisp);
static gboolean gimp_measure_tool_key_press       (GimpTool        *tool,
                                                   GdkEventKey     *kevent,
                                                   GimpDisplay     *gdisp);
static void     gimp_measure_tool_cursor_update   (GimpTool        *tool,
                                                   GimpCoords      *coords,
                                                   GdkModifierType  state,
                                                   GimpDisplay     *gdisp);

static void     gimp_measure_tool_draw            (GimpDrawTool    *draw_tool);

static void     gimp_measure_tool_halt            (GimpMeasureTool *mtool);

static gdouble     gimp_measure_tool_get_angle     (gint             dx,
97
98
99
                                                    gint             dy,
                                                    gdouble          xres,
                                                    gdouble          yres);
100

101
102
103
static GtkWidget * gimp_measure_tool_dialog_new    (GimpMeasureTool *mtool);
static void        gimp_measure_tool_dialog_update (GimpMeasureTool *mtool,
                                                    GimpDisplay     *gdisp);
104

105

106
static GimpDrawToolClass *parent_class = NULL;
107
108
109


void
Nate Summers's avatar
Nate Summers committed
110
gimp_measure_tool_register (GimpToolRegisterCallback  callback,
111
                            gpointer                  data)
112
{
Nate Summers's avatar
Nate Summers committed
113
  (* callback) (GIMP_TYPE_MEASURE_TOOL,
114
115
                GIMP_TYPE_MEASURE_OPTIONS,
                gimp_measure_options_gui,
116
                0,
117
                "gimp-measure-tool",
118
                _("Measure"),
119
                _("Measure distances and angles"),
120
                N_("_Measure"), NULL,
121
                NULL, GIMP_HELP_TOOL_MEASURE,
Nate Summers's avatar
Nate Summers committed
122
                GIMP_STOCK_TOOL_MEASURE,
123
                data);
124
125
}

126
GType
127
128
gimp_measure_tool_get_type (void)
{
129
  static GType tool_type = 0;
130
131
132

  if (! tool_type)
    {
133
      static const GTypeInfo tool_info =
134
      {
135
        sizeof (GimpMeasureToolClass),
136
137
138
139
140
141
142
143
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_measure_tool_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpMeasureTool),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_measure_tool_init,
144
145
      };

146
      tool_type = g_type_register_static (GIMP_TYPE_DRAW_TOOL,
147
                                          "GimpMeasureTool",
148
                                          &tool_info, 0);
149
150
151
152
153
154
155
156
    }

  return tool_type;
}

static void
gimp_measure_tool_class_init (GimpMeasureToolClass *klass)
{
157
158
  GimpToolClass     *tool_class      = GIMP_TOOL_CLASS (klass);
  GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
159

160
  parent_class = g_type_class_peek_parent (klass);
161

162
163
164
165
  tool_class->control        = gimp_measure_tool_control;
  tool_class->button_press   = gimp_measure_tool_button_press;
  tool_class->button_release = gimp_measure_tool_button_release;
  tool_class->motion         = gimp_measure_tool_motion;
166
  tool_class->key_press      = gimp_measure_tool_key_press;
167
168
169
  tool_class->cursor_update  = gimp_measure_tool_cursor_update;

  draw_tool_class->draw      = gimp_measure_tool_draw;
170
171
172
173
174
}

static void
gimp_measure_tool_init (GimpMeasureTool *measure_tool)
{
175
  GimpTool *tool = GIMP_TOOL (measure_tool);
176

177
178
179
  gimp_tool_control_set_handles_empty_image (tool->control, TRUE);
  gimp_tool_control_set_tool_cursor         (tool->control,
                                             GIMP_TOOL_CURSOR_MEASURE);
180
181
}

182
static void
183
184
185
gimp_measure_tool_control (GimpTool       *tool,
                           GimpToolAction  action,
                           GimpDisplay    *gdisp)
186
{
187
188
189
190
191
  switch (action)
    {
    case PAUSE:
    case RESUME:
      break;
192

193
    case HALT:
194
      gimp_measure_tool_halt (GIMP_MEASURE_TOOL (tool));
195
      break;
196

197
198
    default:
      break;
199
    }
200

201
  GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
202
}
Sven Neumann's avatar
Sven Neumann committed
203
204

static void
205
206
207
208
209
gimp_measure_tool_button_press (GimpTool        *tool,
                                GimpCoords      *coords,
                                guint32          time,
                                GdkModifierType  state,
                                GimpDisplay     *gdisp)
Sven Neumann's avatar
Sven Neumann committed
210
{
211
  GimpMeasureTool    *mtool = GIMP_MEASURE_TOOL (tool);
212
213
214
  GimpMeasureOptions *options;
  GimpDisplayShell   *shell;
  gint                i;
215

216
  options = GIMP_MEASURE_OPTIONS (tool->tool_info->tool_options);
217

Michael Natterer's avatar
Michael Natterer committed
218
219
  shell = GIMP_DISPLAY_SHELL (gdisp->shell);

220
  /*  if we are changing displays, pop the statusbar of the old one  */
221
  if (gimp_tool_control_is_active (tool->control) && gdisp != tool->gdisp)
Sven Neumann's avatar
Sven Neumann committed
222
    {
223
      gimp_tool_pop_status (tool);
224
    }
225

226
  mtool->function = CREATING;
227

228
  if (gimp_tool_control_is_active (tool->control) && gdisp == tool->gdisp)
Sven Neumann's avatar
Sven Neumann committed
229
    {
230
231
232
      /*  if the cursor is in one of the handles,
       *  the new function will be moving or adding a new point or guide
       */
233
      for (i = 0; i < mtool->num_points; i++)
234
	{
235
	  if (gimp_draw_tool_on_handle (GIMP_DRAW_TOOL (tool), gdisp,
236
237
                                        coords->x,
                                        coords->y,
238
                                        GIMP_HANDLE_CIRCLE,
239
240
                                        mtool->x[i],
                                        mtool->y[i],
241
242
243
                                        TARGET, TARGET,
                                        GTK_ANCHOR_CENTER,
                                        FALSE))
244
	    {
245
	      if (state & (GDK_CONTROL_MASK | GDK_MOD1_MASK))
246
		{
247
		  GimpGuide *guide;
248
249
                  gboolean   create_hguide;
                  gboolean   create_vguide;
Michael Natterer's avatar
Michael Natterer committed
250

251
                  create_hguide = ((state & GDK_CONTROL_MASK) &&
252
253
                                   (mtool->y[i] ==
                                    CLAMP (mtool->y[i],
254
255
                                           0,
                                           gdisp->gimage->height)));
Michael Natterer's avatar
Michael Natterer committed
256

257
                  create_vguide = ((state & GDK_MOD1_MASK) &&
258
259
                                   (mtool->x[i] ==
                                    CLAMP (mtool->x[i],
260
261
262
263
                                           0,
                                           gdisp->gimage->width)));

		  if (create_hguide && create_vguide)
264
265
266
		    gimp_image_undo_group_start (gdisp->gimage,
                                                 GIMP_UNDO_GROUP_IMAGE_GUIDE,
                                                 _("Add Guides"));
267

268
		  if (create_hguide)
269
		    {
270
		      guide = gimp_image_add_hguide (gdisp->gimage,
271
                                                     mtool->y[i],
272
                                                     TRUE);
273
		      gimp_image_update_guide (gdisp->gimage, guide);
Michael Natterer's avatar
Michael Natterer committed
274
275
		    }

276
		  if (create_vguide)
277
		    {
278
		      guide = gimp_image_add_vguide (gdisp->gimage,
279
                                                     mtool->x[i],
280
                                                     TRUE);
281
		      gimp_image_update_guide (gdisp->gimage, guide);
282
		    }
283

284
		  if (create_hguide && create_vguide)
285
		    gimp_image_undo_group_end (gdisp->gimage);
286

287
		  if (create_hguide || create_vguide)
288
                    gimp_image_flush (gdisp->gimage);
289

290
		  mtool->function = GUIDING;
291
292
		  break;
		}
293

294
295
	      mtool->function = (state & GDK_SHIFT_MASK) ? ADDING : MOVING;
	      mtool->point = i;
296
297
298
	      break;
	    }
	}
299

300
      /*  adding to the middle point makes no sense  */
301
      if (i == 0 &&
302
303
          mtool->function == ADDING &&
          mtool->num_points == 3)
304
        {
305
          mtool->function = MOVING;
306
        }
307
308

      /*  if the function is still CREATING, we are outside the handles  */
309
      if (mtool->function == CREATING)
310
	{
311
	  if (mtool->num_points > 1 && (state & GDK_MOD1_MASK))
312
	    {
313
	      mtool->function = MOVING_ALL;
314

315
316
              mtool->last_x = coords->x;
              mtool->last_y = coords->y;
317
318
	    }
	}
Sven Neumann's avatar
Sven Neumann committed
319
    }
320

321
  if (mtool->function == CREATING)
Sven Neumann's avatar
Sven Neumann committed
322
    {
323
      if (gimp_tool_control_is_active (tool->control))
Sven Neumann's avatar
Sven Neumann committed
324
	{
325
326
327
328
	  gimp_draw_tool_stop (GIMP_DRAW_TOOL (mtool));

          mtool->x[0] = mtool->x[1] = mtool->x[2] = 0.0;
          mtool->y[0] = mtool->y[1] = mtool->y[2] = 0.0;
Sven Neumann's avatar
Sven Neumann committed
329

330
          gimp_measure_tool_dialog_update (mtool, gdisp);
Sven Neumann's avatar
Sven Neumann committed
331
	}
332

333
      /*  set the first point and go into ADDING mode  */
334
335
336
337
338
      mtool->x[0]       = coords->x;
      mtool->y[0]       = coords->y;
      mtool->point      = 0;
      mtool->num_points = 1;
      mtool->function   = ADDING;
Sven Neumann's avatar
Sven Neumann committed
339
340

      /*  set the gdisplay  */
341
      tool->gdisp = gdisp;
Sven Neumann's avatar
Sven Neumann committed
342

343
      if (gimp_tool_control_is_active (tool->control))
344
345
	{
	  gimp_tool_pop_status (tool);
346
	  gimp_tool_push_status (tool, " ");
347
        }
348
349
350
351
      else
        {
          gimp_tool_control_activate (tool->control);
        }
352
353

      gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), gdisp);
Sven Neumann's avatar
Sven Neumann committed
354
    }
355
356

  /*  create the info window if necessary  */
357
358
  if (! mtool->dialog && (options->use_info_window ||
                          ! GTK_WIDGET_VISIBLE (shell->statusbar)))
359
    {
360
361
362
      mtool->dialog = gimp_measure_tool_dialog_new (mtool);
      g_object_add_weak_pointer (G_OBJECT (mtool->dialog),
                                 (gpointer *) &mtool->dialog);
363
    }
364

365
366
  if (mtool->dialog)
    gimp_viewable_dialog_set_viewable (GIMP_VIEWABLE_DIALOG (mtool->dialog),
367
                                       GIMP_VIEWABLE (tool->gdisp->gimage));
Sven Neumann's avatar
Sven Neumann committed
368
369
370
}

static void
371
372
373
374
375
gimp_measure_tool_button_release (GimpTool        *tool,
                                  GimpCoords      *coords,
                                  guint32          time,
                                  GdkModifierType  state,
                                  GimpDisplay     *gdisp)
Sven Neumann's avatar
Sven Neumann committed
376
{
377
  GimpMeasureTool *measure_tool = GIMP_MEASURE_TOOL (tool);
378

Sven Neumann's avatar
Sven Neumann committed
379
  measure_tool->function = FINISHED;
Sven Neumann's avatar
Sven Neumann committed
380
381
382
}

static void
383
384
385
386
387
gimp_measure_tool_motion (GimpTool        *tool,
                          GimpCoords      *coords,
                          guint32          time,
                          GdkModifierType  state,
                          GimpDisplay     *gdisp)
Sven Neumann's avatar
Sven Neumann committed
388
{
389
  GimpMeasureTool    *mtool = GIMP_MEASURE_TOOL (tool);
390
391
392
393
  GimpMeasureOptions *options;
  gint                dx, dy;
  gint                i;
  gint                tmp;
Sven Neumann's avatar
Sven Neumann committed
394

395
  options = GIMP_MEASURE_OPTIONS (tool->tool_info->tool_options);
Sven Neumann's avatar
Sven Neumann committed
396

397
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (mtool));
Sven Neumann's avatar
Sven Neumann committed
398

399
  /*
400
401
   *  A few comments here, because this routine looks quite weird at first ...
   *
402
403
404
405
   *  The goal is to keep point 0, called the start point, to be
   *  always the one in the middle or, if there are only two points,
   *  the one that is fixed.  The angle is then always measured at
   *  this point.
406
407
   */

408
  switch (mtool->function)
Sven Neumann's avatar
Sven Neumann committed
409
    {
410
    case ADDING:
411
      switch (mtool->point)
Sven Neumann's avatar
Sven Neumann committed
412
	{
413
	case 0:  /*  we are adding to the start point  */
414
	  break;
415
416
417
418
419
420
421
422
	case 1:  /*  we are adding to the end point,
                     make it the new start point  */
	  tmp = mtool->x[0];
	  mtool->x[0] = mtool->x[1];
	  mtool->x[1] = tmp;
	  tmp = mtool->y[0];
	  mtool->y[0] = mtool->y[1];
	  mtool->y[1] = tmp;
423
	  break;
424
425
426
427
428
429
	case 2:  /*  we are adding to the third point,
                     make it the new start point  */
	  mtool->x[1] = mtool->x[0];
	  mtool->y[1] = mtool->y[0];
	  mtool->x[0] = mtool->x[2];
	  mtool->y[0] = mtool->y[2];
430
431
432
	  break;
	default:
	  break;
Sven Neumann's avatar
Sven Neumann committed
433
	}
434
435
436
      mtool->num_points = MIN (mtool->num_points + 1, 3);
      mtool->point = mtool->num_points - 1;
      mtool->function = MOVING;
437
      /*  no, don't break here!  */
438
439

    case MOVING:
440
441
442
      /*  if we are moving the start point and only have two,
          make it the end point  */
      if (mtool->num_points == 2 && mtool->point == 0)
443
	{
444
445
446
447
448
449
450
	  tmp = mtool->x[0];
	  mtool->x[0] = mtool->x[1];
	  mtool->x[1] = tmp;
	  tmp = mtool->y[0];
	  mtool->y[0] = mtool->y[1];
	  mtool->y[1] = tmp;
	  mtool->point = 1;
451
	}
452
      i = mtool->point;
Sven Neumann's avatar
Sven Neumann committed
453

454
455
      mtool->x[i] = ROUND (coords->x);
      mtool->y[i] = ROUND (coords->y);
456

457
      if (state & GDK_CONTROL_MASK)
Sven Neumann's avatar
Sven Neumann committed
458
	{
459
460
461
462
463
464
465
          gdouble  x = mtool->x[i];
          gdouble  y = mtool->y[i];

          gimp_tool_motion_constrain (mtool->x[0], mtool->y[0], &x, &y);

          mtool->x[i] = ROUND (x);
          mtool->y[i] = ROUND (y);
466
        }
Sven Neumann's avatar
Sven Neumann committed
467
468
      break;

469
    case MOVING_ALL:
470
471
      dx = ROUND (coords->x) - mtool->last_x;
      dy = ROUND (coords->y) - mtool->last_y;
472

473
      for (i = 0; i < mtool->num_points; i++)
474
	{
475
476
	  mtool->x[i] += dx;
	  mtool->y[i] += dy;
477
	}
478

479
480
      mtool->last_x = ROUND (coords->x);
      mtool->last_y = ROUND (coords->y);
481
      break;
482

483
484
    default:
      break;
485
486
    }

487
488
  if (mtool->function == MOVING)
    gimp_measure_tool_dialog_update (mtool, gdisp);
489

490
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (mtool));
Sven Neumann's avatar
Sven Neumann committed
491
492
}

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
static gboolean
gimp_measure_tool_key_press (GimpTool    *tool,
                             GdkEventKey *kevent,
                             GimpDisplay *gdisp)
{
  if (gdisp == tool->gdisp)
    {
      switch (kevent->keyval)
        {
        case GDK_Escape:
          gimp_measure_tool_halt (GIMP_MEASURE_TOOL (tool));
          return TRUE;

        default:
          break;
        }
    }

  return FALSE;
}

Sven Neumann's avatar
Sven Neumann committed
514
static void
515
516
517
518
gimp_measure_tool_cursor_update (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 GdkModifierType  state,
                                 GimpDisplay     *gdisp)
Sven Neumann's avatar
Sven Neumann committed
519
{
520
  GimpMeasureTool   *mtool     = GIMP_MEASURE_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
521
  gboolean           in_handle = FALSE;
522
523
  GimpCursorType     cursor    = GIMP_CURSOR_CROSSHAIR_SMALL;
  GimpCursorModifier modifier  = GIMP_CURSOR_MODIFIER_NONE;
524
  gint               i;
525

526
  if (gimp_tool_control_is_active (tool->control) && tool->gdisp == gdisp)
Sven Neumann's avatar
Sven Neumann committed
527
    {
528
      for (i = 0; i < mtool->num_points; i++)
529
	{
530
	  if (gimp_draw_tool_on_handle (GIMP_DRAW_TOOL (tool), gdisp,
531
532
                                        coords->x,
                                        coords->y,
533
                                        GIMP_HANDLE_CIRCLE,
534
535
                                        mtool->x[i],
                                        mtool->y[i],
536
537
538
                                        TARGET, TARGET,
                                        GTK_ANCHOR_CENTER,
                                        FALSE))
539
	    {
540
	      in_handle = TRUE;
541

542
	      if (state & GDK_CONTROL_MASK)
543
		{
544
		  if (state & GDK_MOD1_MASK)
545
		    cursor = GDK_BOTTOM_RIGHT_CORNER;
546
		  else
547
		    cursor = GDK_BOTTOM_SIDE;
548
549
		  break;
		}
550
551

	      if (state & GDK_MOD1_MASK)
552
		{
553
		  cursor = GDK_RIGHT_SIDE;
554
555
		  break;
		}
556

557
	      if (state & GDK_SHIFT_MASK)
558
		modifier = GIMP_CURSOR_MODIFIER_PLUS;
559
	      else
560
		modifier = GIMP_CURSOR_MODIFIER_MOVE;
561

562
	      if (i == 0 && mtool->num_points == 3 &&
563
564
		  modifier == GIMP_CURSOR_MODIFIER_PLUS)
		modifier = GIMP_CURSOR_MODIFIER_MOVE;
565
566
	      break;
	    }
567
	}
568

569
      if (! in_handle && mtool->num_points > 1 && state & GDK_MOD1_MASK)
570
        modifier = GIMP_CURSOR_MODIFIER_MOVE;
Sven Neumann's avatar
Sven Neumann committed
571
    }
572

573
574
  gimp_tool_control_set_cursor          (tool->control, cursor);
  gimp_tool_control_set_cursor_modifier (tool->control, modifier);
575
576

  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, gdisp);
Sven Neumann's avatar
Sven Neumann committed
577
578
579
}

static void
580
gimp_measure_tool_draw (GimpDrawTool *draw_tool)
Sven Neumann's avatar
Sven Neumann committed
581
{
582
583
  GimpMeasureTool *mtool = GIMP_MEASURE_TOOL (draw_tool);
  GimpTool        *tool  = GIMP_TOOL (draw_tool);
584
585
586
  gint             i;
  gint             angle1, angle2;
  gint             draw_arc = 0;
Sven Neumann's avatar
Sven Neumann committed
587

588
  for (i = 0; i < mtool->num_points; i++)
Sven Neumann's avatar
Sven Neumann committed
589
    {
590
      if (i == 0 && mtool->num_points == 3)
591
	{
592
593
          gimp_draw_tool_draw_handle (draw_tool,
                                      GIMP_HANDLE_CIRCLE,
594
595
                                      mtool->x[i],
                                      mtool->y[i],
596
597
598
599
                                      TARGET,
                                      TARGET,
                                      GTK_ANCHOR_CENTER,
                                      FALSE);
600
	}
601
      else
Sven Neumann's avatar
Sven Neumann committed
602
	{
603
604
          gimp_draw_tool_draw_handle (draw_tool,
                                      GIMP_HANDLE_CROSS,
605
606
                                      mtool->x[i],
                                      mtool->y[i],
607
608
609
610
                                      TARGET * 2,
                                      TARGET * 2,
                                      GTK_ANCHOR_CENTER,
                                      FALSE);
Sven Neumann's avatar
Sven Neumann committed
611
	}
612

613
      if (i > 0)
Sven Neumann's avatar
Sven Neumann committed
614
	{
615
          gimp_draw_tool_draw_line (draw_tool,
616
617
618
619
                                    mtool->x[0],
                                    mtool->y[0],
                                    mtool->x[i],
                                    mtool->y[i],
620
621
                                    FALSE);

622
	  /*  only draw the arc if the lines are long enough  */
623
          if (gimp_draw_tool_calc_distance (draw_tool, tool->gdisp,
624
625
626
627
                                            mtool->x[0],
                                            mtool->y[0],
                                            mtool->x[i],
                                            mtool->y[i]) > ARC_RADIUS)
628
629
630
            {
              draw_arc++;
            }
Sven Neumann's avatar
Sven Neumann committed
631
632
	}
    }
633

634
  if (mtool->num_points > 1 && draw_arc == mtool->num_points - 1)
Sven Neumann's avatar
Sven Neumann committed
635
    {
636
637
      angle1 = mtool->angle2 * 64.0;
      angle2 = (mtool->angle1 - mtool->angle2) * 64.0;
638

639
640
641
642
      if (angle2 > 11520)
	  angle2 -= 23040;
      if (angle2 < -11520)
	  angle2 += 23040;
643

644
645
      if (angle2 != 0)
	{
646
          gimp_draw_tool_draw_arc_by_anchor (draw_tool,
647
                                             FALSE,
648
649
                                             mtool->x[0],
                                             mtool->y[0],
650
                                             ARC_RADIUS,
651
                                             ARC_RADIUS,
652
                                             angle1, angle2,
653
                                             GTK_ANCHOR_CENTER,
654
                                             FALSE);
655

656
	  if (mtool->num_points == 2)
657
            {
658
659
660
661
662
              GimpDisplayShell *shell;
              gdouble           target;
              gdouble           arc_radius;

              shell = GIMP_DISPLAY_SHELL (tool->gdisp->shell);
663

664
665
              target     = FUNSCALEX (shell, (TARGET >> 1));
              arc_radius = FUNSCALEX (shell, ARC_RADIUS);
666

667
668
669
670
671
672
673
674
              gimp_draw_tool_draw_line (draw_tool,
                                        mtool->x[0],
                                        mtool->y[0],
                                        (mtool->x[1] >= mtool->x[0] ?
                                         mtool->x[0] + arc_radius + target :
                                         mtool->x[0] - arc_radius - target),
                                        mtool->y[0],
                                        FALSE);
675
            }
676
	}
Sven Neumann's avatar
Sven Neumann committed
677
    }
Sven Neumann's avatar
Sven Neumann committed
678
679
}

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
static void
gimp_measure_tool_halt (GimpMeasureTool *mtool)
{
  GimpTool *tool = GIMP_TOOL (mtool);

  if (mtool->dialog)
    gtk_widget_destroy (mtool->dialog);

  gimp_tool_pop_status (tool);

  if (gimp_draw_tool_is_active (GIMP_DRAW_TOOL (mtool)))
    gimp_draw_tool_stop (GIMP_DRAW_TOOL (mtool));

  if (gimp_tool_control_is_active (tool->control))
    gimp_tool_control_halt (tool->control);
}

697
static gdouble
698
699
700
701
gimp_measure_tool_get_angle (gint    dx,
                             gint    dy,
                             gdouble xres,
                             gdouble yres)
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
{
  gdouble angle;

  if (dx)
    angle = gimp_rad_to_deg (atan (((gdouble) (dy) / yres) /
				   ((gdouble) (dx) / xres)));
  else if (dy)
    angle = dy > 0 ? 270.0 : 90.0;
  else
    angle = 180.0;

  if (dx > 0)
    {
      if (dy > 0)
	angle = 360.0 - angle;
      else
	angle = -angle;
Sven Neumann's avatar
Sven Neumann committed
719
    }
720
721
722
723
724
725
  else
    {
      angle = 180.0 - angle;
    }

  return angle;
Sven Neumann's avatar
Sven Neumann committed
726
727
}

728
static void
729
730
gimp_measure_tool_dialog_update (GimpMeasureTool *mtool,
                                 GimpDisplay     *gdisp)
731
{
732
733
  GimpDisplayShell *shell = GIMP_DISPLAY_SHELL (gdisp->shell);
  GimpImage        *image = gdisp->gimage;
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
  gint              ax, ay;
  gint              bx, by;
  gdouble           theta1, theta2;
  gdouble           pixel_distance;
  gdouble           pixel_angle;
  gdouble           unit_distance;
  gdouble           unit_angle;
  gchar             format[128];
  gchar             buf[128];

  /*  calculate distance and angle  */
  ax = mtool->x[1] - mtool->x[0];
  ay = mtool->y[1] - mtool->y[0];

  if (mtool->num_points == 3)
    {
      bx = mtool->x[2] - mtool->x[0];
      by = mtool->y[2] - mtool->y[0];
    }
  else
    {
      bx = 0;
      by = 0;
    }

  pixel_distance = sqrt (SQR (ax - bx) + SQR (ay - by));
760
761
762
  unit_distance  = (_gimp_unit_get_factor (image->gimp, shell->unit) *
                    sqrt (SQR ((gdouble)(ax - bx) / image->xresolution) +
                          SQR ((gdouble)(ay - by) / image->yresolution)));
763
764
765
766

  if (mtool->num_points != 3)
    bx = ax > 0 ? 1 : -1;

767
768
  theta1 = gimp_measure_tool_get_angle (ax, ay, 1.0, 1.0);
  theta2 = gimp_measure_tool_get_angle (bx, by, 1.0, 1.0);
769
770
771
772
773

  pixel_angle = fabs (theta1 - theta2);
  if (pixel_angle > 180.0)
    pixel_angle = fabs (360.0 - pixel_angle);

774
775
776
777
  theta1 = gimp_measure_tool_get_angle (ax, ay,
                                        image->xresolution, image->yresolution);
  theta2 = gimp_measure_tool_get_angle (bx, by,
                                        image->xresolution, image->yresolution);
778

779
780
  mtool->angle1 = theta1;
  mtool->angle2 = theta2;
781
782
783
784
785

  unit_angle = fabs (theta1 - theta2);
  if (unit_angle > 180.0)
    unit_angle = fabs (360.0 - unit_angle);

786
  if (shell->unit == GIMP_UNIT_PIXEL)
787
788
    {
      g_snprintf (buf, sizeof (buf), "%.1f %s, %.2f \302\260",
789
                  pixel_distance, _("pixels"), pixel_angle);
790
791
792
793
    }
  else
    {
      g_snprintf (format, sizeof (format), "%%.%df %s, %%.2f \302\260",
794
795
                  _gimp_unit_get_digits (image->gimp, shell->unit),
                  _gimp_unit_get_plural (image->gimp, shell->unit));
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811

      g_snprintf (buf, sizeof (buf), format, unit_distance, unit_angle);
    }

  gimp_tool_pop_status (GIMP_TOOL (mtool));
  gimp_tool_push_status (GIMP_TOOL (mtool), buf);

  if (mtool->dialog)
    {
      g_snprintf (buf, sizeof (buf), "%.1f", pixel_distance);
      gtk_label_set_text (GTK_LABEL (mtool->distance_label[0]), buf);

      g_snprintf (buf, sizeof (buf), "%.2f", pixel_angle);
      gtk_label_set_text (GTK_LABEL (mtool->angle_label[0]), buf);

      g_snprintf (format, sizeof (format),
812
                  "%%.%df", _gimp_unit_get_digits (image->gimp, shell->unit));
813
814
815
816
      g_snprintf (buf, sizeof (buf), format, unit_distance);
      gtk_label_set_text (GTK_LABEL (mtool->distance_label[1]), buf);

      gtk_label_set_text (GTK_LABEL (mtool->unit_label[0]),
817
                          _gimp_unit_get_plural (image->gimp, shell->unit));
818
819
820
821
822
823
824
825
826
827
828
829
830
831

      if (fabs (unit_angle - pixel_angle) > 0.01)
        {
          g_snprintf (buf, sizeof (buf), "%.2f", unit_angle);
          gtk_label_set_text (GTK_LABEL (mtool->angle_label[1]), buf);

          gtk_label_set_text (GTK_LABEL (mtool->unit_label[1]), "\302\260");
        }
      else
        {
          gtk_label_set_text (GTK_LABEL (mtool->angle_label[1]), " ");
          gtk_label_set_text (GTK_LABEL (mtool->unit_label[1]),  " ");
        }

832
833
834
835
      if (GTK_WIDGET_VISIBLE (mtool->dialog))
        gdk_window_show (mtool->dialog->window);
      else
        gtk_widget_show (mtool->dialog);
836
    }
837
838
}

839
840
static GtkWidget *
gimp_measure_tool_dialog_new (GimpMeasureTool *mtool)
841
{
842
  GimpTool  *tool = GIMP_TOOL (mtool);
843
844
845
  GtkWidget *dialog;
  GtkWidget *table;
  GtkWidget *label;
846

847
848
  dialog = gimp_tool_dialog_new (tool->tool_info,
                                 NULL /* tool->gdisp->shell */,
849
                                 _("Measure Distances and Angles"),
850

851
                                 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
852

853
                                 NULL);
854

855
856
857
858
  g_signal_connect (dialog, "response",
                    G_CALLBACK (gtk_widget_destroy),
                    NULL);

859
  table = gtk_table_new (2, 5, TRUE);
860
861
862
863
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
  gtk_container_set_border_width (GTK_CONTAINER (table), 6);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), table);
864
865
866
867
  gtk_widget_show (table);


  label = gtk_label_new (_("Distance:"));
868
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
  gtk_widget_show (label);

  mtool->distance_label[0] = label = gtk_label_new ("0.0");
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);
  gtk_widget_show (label);

  label = gtk_label_new (_("pixels"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 2, 3, 0, 1);
  gtk_widget_show (label);

  mtool->distance_label[1] = label = gtk_label_new ("0.0");
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 3, 4, 0, 1);
  gtk_widget_show (label);

  mtool->unit_label[0] = label = gtk_label_new (" ");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 4, 5, 0, 1);
  gtk_widget_show (label);


  label = gtk_label_new (_("Angle:"));
894
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
  gtk_widget_show (label);

  mtool->angle_label[0] = label = gtk_label_new ("0.0");
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 1, 2);
  gtk_widget_show (label);

  label = gtk_label_new ("\302\260");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 2, 3, 1, 2);
  gtk_widget_show (label);

  mtool->angle_label[1] = label = gtk_label_new (" ");
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 3, 4, 1, 2);
  gtk_widget_show (label);

  mtool->unit_label[1] = label = gtk_label_new (" ");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 4, 5, 1, 2);
  gtk_widget_show (label);

  return dialog;
919
}