gimp/app/tools/gimpmeasuretool.c

839 lines
23 KiB
C
Raw Normal View History

1999-08-15 23:59:06 +08:00
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Measure tool
* Copyright (C) 1999 Sven Neumann <sven@gimp.org>
*
1999-08-15 23:59:06 +08:00
* 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"
app/appenv.h New file. Includes <math.h>. Move G_PI, RINT(), ROUND() etc 1999-09-01 Tor Lillqvist <tml@iki.fi> * app/appenv.h * libgimp/gimpmath.h: New file. Includes <math.h>. Move G_PI, RINT(), ROUND() etc from app/appenv.h here, so plug-ins can use them, too. Remove some commented-out old stuff in appenv.h. * libgimp/gimp.h: Include gimpmath.h. * libgimp/gimp.c (gimp_main): Win32: Don't install signal handlers, we can't do anything useful in the handler ourselves anyway (it would be nice to print out a backtrace, but that seems pretty hard to do, even if not impossible). Let Windows inform the user about the crash. If the plug-in was compiled with MSVC, and the user also has it, she is offered a chance to start the debugger automatically anyway. * app/*several*.c: Include gimpmath.h for G_PI etc. Don't include <math.h>, as gimpmath.h includes it. * plug-ins/*/*many*.c: Include config.h. Don't include <math.h>. Remove all the duplicated definitions of G_PI and rint(). Use RINT() instead of rint(). * app/app_procs.[ch]: app_exit() takes a gboolean. * app/batch.c * app/commands.c * app/interface.c: Call app_exit() with FALSE or TRUE. * app/main.c (on_error): Call gimp_fatal_error. (main): Don't install any signal handler on Win32 here, either. * app/errors.c (gimp_fatal_error, gimp_terminate): Win32: Format the message and call MessageBox with it. g_on_error_query doesn't do anything useful on Win32, and printf'ing a message to stdout or stderr doesn't do anything, either, in a windowing application.
1999-09-02 04:30:56 +08:00
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "apptypes.h"
1999-08-15 23:59:06 +08:00
#include "appenv.h"
#include "draw_core.h"
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include "gdisplay.h"
#include "gimpimage.h"
1999-10-27 02:27:27 +08:00
#include "gimpui.h"
#include "info_dialog.h"
1999-08-15 23:59:06 +08:00
#include "measure.h"
#include "undo.h"
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include "tools.h"
#include "tool_options.h"
1999-08-15 23:59:06 +08:00
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include "libgimp/gimpintl.h"
/* definitions */
#define TARGET 8
#define ARC_RADIUS 30
#define ARC_RADIUS_2 1100
1999-08-15 23:59:06 +08:00
#define STATUSBAR_SIZE 128
/* maximum information buffer size */
#define MAX_INFO_BUF 16
1999-08-15 23:59:06 +08:00
/* possible measure functions */
typedef enum
{
CREATING,
ADDING,
MOVING,
MOVING_ALL,
GUIDING,
FINISHED
} MeasureFunction;
1999-08-15 23:59:06 +08:00
/* the measure structure */
typedef struct _MeasureTool MeasureTool;
1999-08-15 23:59:06 +08:00
struct _MeasureTool
{
DrawCore *core; /* draw core */
MeasureFunction function; /* what are we doing? */
gint last_x; /* last x coordinate */
gint last_y; /* last y coordinate */
gint point; /* what are we manipulating? */
gint num_points; /* how many points? */
gint x[3]; /* three x coordinates */
gint y[3]; /* three y coordinates */
gdouble angle1; /* first angle */
gdouble angle2; /* second angle */
guint context_id; /* for the statusbar */
1999-08-15 23:59:06 +08:00
};
/* the measure tool options */
typedef struct _MeasureOptions MeasureOptions;
struct _MeasureOptions
{
ToolOptions tool_options;
gboolean use_info_window;
gboolean use_info_window_d;
GtkWidget *use_info_window_w;
};
/* local function prototypes */
static void measure_tool_button_press (Tool *tool,
GdkEventButton *bevent,
GDisplay *gdisp);
static void measure_tool_button_release (Tool *tool,
GdkEventButton *bevent,
GDisplay *gdisp);
static void measure_tool_motion (Tool *tool,
GdkEventMotion *mevent,
GDisplay *gdisp);
static void measure_tool_cursor_update (Tool *tool,
GdkEventMotion *mevent,
GDisplay *gdisp);
static void measure_tool_control (Tool *tool,
ToolAction action,
GDisplay *gdisp);
static void measure_tool_info_window_close_callback (GtkWidget *widget,
gpointer data);
static void measure_tool_info_update (void);
static MeasureOptions *measure_tool_options = NULL;
/* the measure tool info window */
static InfoDialog *measure_tool_info = NULL;
static gchar distance_buf[MAX_INFO_BUF];
static gchar angle_buf[MAX_INFO_BUF];
1999-08-15 23:59:06 +08:00
static void
measure_tool_options_reset (void)
{
MeasureOptions *options = measure_tool_options;
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (options->use_info_window_w),
options->use_info_window_d);
}
static MeasureOptions *
measure_tool_options_new (void)
{
MeasureOptions *options;
GtkWidget *vbox;
/* the new measure tool options structure */
options = g_new (MeasureOptions, 1);
tool_options_init ((ToolOptions *) options,
_("Measure Tool"),
measure_tool_options_reset);
options->use_info_window = options->use_info_window_d = FALSE;
/* the main vbox */
vbox = options->tool_options.main_vbox;
/* the use_info_window toggle button */
options->use_info_window_w =
gtk_check_button_new_with_label (_("Use Info Window"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (options->use_info_window_w),
options->use_info_window_d);
gtk_box_pack_start (GTK_BOX (vbox), options->use_info_window_w, FALSE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (options->use_info_window_w), "toggled",
GTK_SIGNAL_FUNC (gimp_toggle_button_update),
&options->use_info_window);
gtk_widget_show (options->use_info_window_w);
return options;
}
1999-08-15 23:59:06 +08:00
static gdouble
measure_get_angle (gint dx,
gint dy,
gdouble xres,
gdouble yres)
{
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;
}
else
{
angle = 180.0 - angle;
}
return angle;
}
1999-08-15 23:59:06 +08:00
static void
measure_tool_button_press (Tool *tool,
GdkEventButton *bevent,
GDisplay *gdisp)
1999-08-15 23:59:06 +08:00
{
MeasureTool *measure_tool;
gint x[3];
gint y[3];
gint i;
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
/* if we are changing displays, pop the statusbar of the old one */
if (tool->state == ACTIVE && gdisp != tool->gdisp)
1999-08-15 23:59:06 +08:00
{
GDisplay *old_gdisp = tool->gdisp;
gtk_statusbar_pop (GTK_STATUSBAR (old_gdisp->statusbar),
measure_tool->context_id);
gtk_statusbar_push (GTK_STATUSBAR (gdisp->statusbar),
measure_tool->context_id, (""));
}
measure_tool->function = CREATING;
if (tool->state == ACTIVE && gdisp == tool->gdisp)
1999-08-15 23:59:06 +08:00
{
/* if the cursor is in one of the handles,
* the new function will be moving or adding a new point or guide
*/
for (i = 0; i < measure_tool->num_points; i++)
{
gdisplay_transform_coords (gdisp,
measure_tool->x[i], measure_tool->y[i],
&x[i], &y[i], FALSE);
if (bevent->x == CLAMP (bevent->x, x[i] - TARGET, x[i] + TARGET) &&
bevent->y == CLAMP (bevent->y, y[i] - TARGET, y[i] + TARGET))
{
if (bevent->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK))
{
2000-04-13 08:38:04 +08:00
gboolean undo_group;
Guide *guide;
undo_group = (bevent->state & GDK_CONTROL_MASK &&
bevent->state & GDK_MOD1_MASK &&
(measure_tool->y[i] ==
CLAMP (measure_tool->y[i],
0, gdisp->gimage->height)) &&
(measure_tool->x[i] ==
CLAMP (measure_tool->x[i],
0, gdisp->gimage->width)));
if (undo_group)
undo_push_group_start (gdisp->gimage, GUIDE_UNDO);
if (bevent->state & GDK_CONTROL_MASK &&
2000-04-13 08:38:04 +08:00
(measure_tool->y[i] ==
CLAMP (measure_tool->y[i], 0, gdisp->gimage->height)))
{
guide = gimp_image_add_hguide (gdisp->gimage);
undo_push_guide (gdisp->gimage, guide);
guide->position = measure_tool->y[i];
gdisplays_expose_guide (gdisp->gimage, guide);
2000-04-13 08:38:04 +08:00
}
if (bevent->state & GDK_MOD1_MASK &&
2000-04-13 08:38:04 +08:00
(measure_tool->x[i] ==
CLAMP (measure_tool->x[i], 0, gdisp->gimage->width)))
{
guide = gimp_image_add_vguide (gdisp->gimage);
undo_push_guide (gdisp->gimage, guide);
guide->position = measure_tool->x[i];
gdisplays_expose_guide (gdisp->gimage, guide);
}
2000-04-13 08:38:04 +08:00
if (undo_group)
undo_push_group_end (gdisp->gimage);
gdisplays_flush ();
measure_tool->function = GUIDING;
break;
}
measure_tool->function = (bevent->state & GDK_SHIFT_MASK) ? ADDING : MOVING;
measure_tool->point = i;
break;
}
}
/* adding to the middle point makes no sense */
if (i == 0 && measure_tool->function == ADDING && measure_tool->num_points == 3)
measure_tool->function = MOVING;
/* if the function is still CREATING, we are outside the handles */
if (measure_tool->function == CREATING)
{
if (measure_tool->num_points > 1 && bevent->state & GDK_MOD1_MASK)
{
measure_tool->function = MOVING_ALL;
gdisplay_untransform_coords (gdisp, bevent->x, bevent->y,
&measure_tool->last_x, &measure_tool->last_y,
TRUE, FALSE);
}
}
1999-08-15 23:59:06 +08:00
}
1999-08-15 23:59:06 +08:00
if (measure_tool->function == CREATING)
{
if (tool->state == ACTIVE)
2000-03-02 08:03:28 +08:00
{
/* reset everything */
draw_core_stop (measure_tool->core, tool);
gtk_statusbar_pop (GTK_STATUSBAR (gdisp->statusbar),
measure_tool->context_id);
gtk_statusbar_push (GTK_STATUSBAR (gdisp->statusbar),
measure_tool->context_id, "");
2000-03-02 08:03:28 +08:00
distance_buf[0] = '\0';
angle_buf[0] = '\0';
if (measure_tool_info)
measure_tool_info_update ();
}
else
{
/* initialize the statusbar display */
measure_tool->context_id =
gtk_statusbar_get_context_id (GTK_STATUSBAR (gdisp->statusbar),
"measure");
2000-03-02 08:03:28 +08:00
}
/* set the first point and go into ADDING mode */
gdisplay_untransform_coords (gdisp, bevent->x, bevent->y,
&measure_tool->x[0], &measure_tool->y[0],
TRUE, FALSE);
measure_tool->point = 0;
measure_tool->num_points = 1;
measure_tool->function = ADDING;
1999-08-15 23:59:06 +08:00
/* set the gdisplay */
tool->gdisp = gdisp;
1999-08-15 23:59:06 +08:00
/* start drawing the measure tool */
draw_core_start (measure_tool->core, gdisp->canvas->window, tool);
}
/* create the info window if necessary */
if (! measure_tool_info &&
(measure_tool_options->use_info_window ||
! GTK_WIDGET_VISIBLE (gdisp->statusarea)))
{
measure_tool_info = info_dialog_new (_("Measure Tool"),
tools_help_func, NULL);
info_dialog_add_label (measure_tool_info, _("Distance:"), distance_buf);
info_dialog_add_label (measure_tool_info, _("Angle:"), angle_buf);
gimp_dialog_create_action_area (GTK_DIALOG (measure_tool_info->shell),
_("Close"),
measure_tool_info_window_close_callback,
measure_tool_info, NULL, NULL, TRUE, FALSE,
NULL);
}
1999-08-15 23:59:06 +08:00
gdk_pointer_grab (gdisp->canvas->window, FALSE,
GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON1_MOTION_MASK |
GDK_BUTTON_RELEASE_MASK,
1999-08-15 23:59:06 +08:00
NULL, NULL, bevent->time);
tool->state = ACTIVE;
/* set the pointer to the crosshair,
* so one actually sees the cursor position
*/
gdisplay_install_tool_cursor (gdisp, GIMP_CROSSHAIR_SMALL_CURSOR,
MEASURE,
CURSOR_MODIFIER_NONE,
FALSE);
1999-08-15 23:59:06 +08:00
}
static void
measure_tool_button_release (Tool *tool,
GdkEventButton *bevent,
GDisplay *gdisp)
1999-08-15 23:59:06 +08:00
{
MeasureTool *measure_tool;
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
1999-08-16 02:44:15 +08:00
measure_tool->function = FINISHED;
1999-08-15 23:59:06 +08:00
gdk_pointer_ungrab (bevent->time);
gdk_flush ();
}
static void
measure_tool_motion (Tool *tool,
GdkEventMotion *mevent,
GDisplay *gdisp)
1999-08-15 23:59:06 +08:00
{
MeasureTool *measure_tool;
gint x, y;
gint ax, ay;
gint bx, by;
gint dx, dy;
gint i;
gint tmp;
gdouble angle;
gdouble distance;
gchar status_str[STATUSBAR_SIZE];
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
/* undraw the current tool */
draw_core_pause (measure_tool->core, tool);
/* get the coordinates */
gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, TRUE, FALSE);
/*
* A few comments here, because this routine looks quite weird at first ...
*
* 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.
*/
1999-08-15 23:59:06 +08:00
switch (measure_tool->function)
{
case ADDING:
switch (measure_tool->point)
1999-08-15 23:59:06 +08:00
{
case 0: /* we are adding to the start point */
break;
case 1: /* we are adding to the end point, make it the new start point */
tmp = measure_tool->x[0];
measure_tool->x[0] = measure_tool->x[1];
measure_tool->x[1] = tmp;
tmp = measure_tool->y[0];
measure_tool->y[0] = measure_tool->y[1];
measure_tool->y[1] = tmp;
break;
case 2: /* we are adding to the third point, make it the new start point */
measure_tool->x[1] = measure_tool->x[0];
measure_tool->y[1] = measure_tool->y[0];
measure_tool->x[0] = measure_tool->x[2];
measure_tool->y[0] = measure_tool->y[2];
break;
default:
break;
1999-08-15 23:59:06 +08:00
}
measure_tool->num_points = MIN (measure_tool->num_points + 1, 3);
measure_tool->point = measure_tool->num_points - 1;
measure_tool->function = MOVING;
/* no, don't break here! */
case MOVING:
/* if we are moving the start point and only have two, make it the end point */
if (measure_tool->num_points == 2 && measure_tool->point == 0)
{
tmp = measure_tool->x[0];
measure_tool->x[0] = measure_tool->x[1];
measure_tool->x[1] = tmp;
tmp = measure_tool->y[0];
measure_tool->y[0] = measure_tool->y[1];
measure_tool->y[1] = tmp;
measure_tool->point = 1;
}
i = measure_tool->point;
1999-08-15 23:59:06 +08:00
measure_tool->x[i] = x;
measure_tool->y[i] = y;
/* restrict to horizontal/vertical movements, if modifiers are pressed */
1999-08-15 23:59:06 +08:00
if (mevent->state & GDK_MOD1_MASK)
{
if (mevent->state & GDK_CONTROL_MASK)
{
dx = measure_tool->x[i] - measure_tool->x[0];
dy = measure_tool->y[i] - measure_tool->y[0];
1999-09-21 17:11:42 +08:00
measure_tool->x[i] = measure_tool->x[0] +
(dx > 0 ? MAX (abs (dx), abs (dy)) : - MAX (abs (dx), abs (dy)));
measure_tool->y[i] = measure_tool->y[0] +
(dy > 0 ? MAX (abs (dx), abs (dy)) : - MAX (abs (dx), abs (dy)));
1999-08-15 23:59:06 +08:00
}
else
measure_tool->x[i] = measure_tool->x[0];
1999-08-15 23:59:06 +08:00
}
else if (mevent->state & GDK_CONTROL_MASK)
measure_tool->y[i] = measure_tool->y[0];
1999-08-15 23:59:06 +08:00
break;
case MOVING_ALL:
dx = x - measure_tool->last_x;
dy = y - measure_tool->last_y;
for (i = 0; i < measure_tool->num_points; i++)
{
measure_tool->x[i] += dx;
measure_tool->y[i] += dy;
}
measure_tool->last_x = x;
measure_tool->last_y = y;
break;
default:
break;
}
if (measure_tool->function == MOVING)
1999-08-15 23:59:06 +08:00
{
/* calculate distance and angle */
ax = measure_tool->x[1] - measure_tool->x[0];
ay = measure_tool->y[1] - measure_tool->y[0];
if (measure_tool->num_points == 3)
{
bx = measure_tool->x[2] - measure_tool->x[0];
by = measure_tool->y[2] - measure_tool->y[0];
}
else
{
bx = 0;
by = 0;
}
1999-08-16 02:44:15 +08:00
if (gdisp->dot_for_dot)
{
distance = sqrt (SQR (ax - bx) + SQR (ay - by));
if (measure_tool->num_points != 3)
bx = ax > 0 ? 1 : -1;
measure_tool->angle1 = measure_get_angle (ax, ay, 1.0, 1.0);
measure_tool->angle2 = measure_get_angle (bx, by, 1.0, 1.0);
angle = fabs (measure_tool->angle1 - measure_tool->angle2);
if (angle > 180.0)
angle = fabs (360.0 - angle);
g_snprintf (status_str, STATUSBAR_SIZE, "%.1f %s, %.2f %s",
distance, _("pixels"), angle, _("degrees"));
if (measure_tool_options)
{
g_snprintf (distance_buf, MAX_INFO_BUF, "%.1f %s", distance, _("pixels"));
g_snprintf (angle_buf, MAX_INFO_BUF, "%.2f %s", angle, _("degrees"));
}
}
else /* show real world units */
{
gchar *format_str =
g_strdup_printf ("%%.%df %s, %%.2f %s",
gimp_unit_get_digits (gdisp->gimage->unit),
gimp_unit_get_symbol (gdisp->gimage->unit),
_("degrees"));
distance = gimp_unit_get_factor (gdisp->gimage->unit) *
sqrt (SQR ((gdouble)(ax - bx) / gdisp->gimage->xresolution) +
SQR ((gdouble)(ay - by) / gdisp->gimage->yresolution));
if (measure_tool->num_points != 3)
bx = ax > 0 ? 1 : -1;
measure_tool->angle1 = measure_get_angle (ax, ay,
gdisp->gimage->xresolution,
gdisp->gimage->yresolution);
measure_tool->angle2 = measure_get_angle (bx, by,
gdisp->gimage->xresolution,
gdisp->gimage->yresolution);
angle = fabs (measure_tool->angle1 - measure_tool->angle2);
if (angle > 180.0)
angle = fabs (360.0 - angle);
g_snprintf (status_str, STATUSBAR_SIZE, format_str, distance , angle);
g_free (format_str);
if (measure_tool_options)
{
gchar *format_str =
g_strdup_printf ("%%.%df %s",
gimp_unit_get_digits (gdisp->gimage->unit),
gimp_unit_get_symbol (gdisp->gimage->unit));
g_snprintf (distance_buf, MAX_INFO_BUF, format_str, distance);
g_snprintf (angle_buf, MAX_INFO_BUF, "%.2f %s", angle, _("degrees"));
g_free (format_str);
}
}
/* show info in statusbar */
gtk_statusbar_pop (GTK_STATUSBAR (gdisp->statusbar),
measure_tool->context_id);
gtk_statusbar_push (GTK_STATUSBAR (gdisp->statusbar),
measure_tool->context_id,
status_str);
/* and in the info window */
if (measure_tool_info)
measure_tool_info_update ();
1999-08-15 23:59:06 +08:00
} /* measure_tool->function == MOVING */
1999-08-15 23:59:06 +08:00
/* redraw the current tool */
draw_core_resume (measure_tool->core, tool);
}
static void
measure_tool_cursor_update (Tool *tool,
GdkEventMotion *mevent,
GDisplay *gdisp)
1999-08-15 23:59:06 +08:00
{
MeasureTool *measure_tool;
gint x[3];
gint y[3];
gint i;
gboolean in_handle = FALSE;
1999-08-15 23:59:06 +08:00
GdkCursorType ctype = GIMP_CROSSHAIR_SMALL_CURSOR;
CursorModifier cmodifier = CURSOR_MODIFIER_NONE;
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
if (tool->state == ACTIVE && tool->gdisp == gdisp)
1999-08-15 23:59:06 +08:00
{
for (i = 0; i < measure_tool->num_points; i++)
{
gdisplay_transform_coords (gdisp, measure_tool->x[i], measure_tool->y[i],
&x[i], &y[i], FALSE);
if (mevent->x == CLAMP (mevent->x, x[i] - TARGET, x[i] + TARGET) &&
mevent->y == CLAMP (mevent->y, y[i] - TARGET, y[i] + TARGET))
{
in_handle = TRUE;
if (mevent->state & GDK_CONTROL_MASK)
{
if (mevent->state & GDK_MOD1_MASK)
ctype = GDK_BOTTOM_RIGHT_CORNER;
else
ctype = GDK_BOTTOM_SIDE;
break;
}
if (mevent->state & GDK_MOD1_MASK)
{
ctype = GDK_RIGHT_SIDE;
break;
}
if (mevent->state & GDK_SHIFT_MASK)
cmodifier = CURSOR_MODIFIER_PLUS;
else
cmodifier = CURSOR_MODIFIER_MOVE;
if (i == 0 && measure_tool->num_points == 3 &&
cmodifier == CURSOR_MODIFIER_PLUS)
cmodifier = CURSOR_MODIFIER_MOVE;
break;
}
}
if (!in_handle && measure_tool->num_points > 1 &&
mevent->state & GDK_MOD1_MASK)
cmodifier = CURSOR_MODIFIER_MOVE;
1999-08-15 23:59:06 +08:00
}
gdisplay_install_tool_cursor (gdisp, ctype,
MEASURE,
cmodifier,
FALSE);
1999-08-15 23:59:06 +08:00
}
static void
measure_tool_draw (Tool *tool)
{
MeasureTool *measure_tool;
gint x[3];
gint y[3];
gint i;
gint angle1, angle2;
gint draw_arc = 0;
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
for (i = 0; i < measure_tool->num_points; i++)
1999-08-16 02:44:15 +08:00
{
gdisplay_transform_coords (tool->gdisp,
measure_tool->x[i], measure_tool->y[i],
&x[i], &y[i], FALSE);
if (i == 0 && measure_tool->num_points == 3)
{
gdk_draw_arc (measure_tool->core->win, measure_tool->core->gc, FALSE,
x[i] - (TARGET >> 1), y[i] - (TARGET >> 1),
TARGET, TARGET, 0, 23040);
}
else
1999-08-16 02:44:15 +08:00
{
gdk_draw_line (measure_tool->core->win, measure_tool->core->gc,
x[i] - TARGET, y[i],
x[i] + TARGET, y[i]);
gdk_draw_line (measure_tool->core->win, measure_tool->core->gc,
x[i], y[i] - TARGET,
x[i], y[i] + TARGET);
1999-08-16 02:44:15 +08:00
}
if (i > 0)
1999-08-16 02:44:15 +08:00
{
gdk_draw_line (measure_tool->core->win, measure_tool->core->gc,
x[0], y[0],
x[i], y[i]);
/* only draw the arc if the lines are long enough */
if ((SQR (x[i] - x[0]) + SQR (y[i] - y[0])) > ARC_RADIUS_2)
draw_arc++;
1999-08-16 02:44:15 +08:00
}
}
if (measure_tool->num_points > 1 && draw_arc == measure_tool->num_points - 1)
1999-08-16 02:44:15 +08:00
{
angle1 = measure_tool->angle2 * 64.0;
angle2 = (measure_tool->angle1 - measure_tool->angle2) * 64.0;
if (angle2 > 11520)
angle2 -= 23040;
if (angle2 < -11520)
angle2 += 23040;
if (angle2 != 0)
{
gdk_draw_arc (measure_tool->core->win, measure_tool->core->gc, FALSE,
x[0] - ARC_RADIUS, y[0] - ARC_RADIUS,
2 * ARC_RADIUS, 2 * ARC_RADIUS,
angle1, angle2);
if (measure_tool->num_points == 2)
gdk_draw_line (measure_tool->core->win, measure_tool->core->gc,
x[0], y[0],
x[1] - x[0] <= 0 ? x[0] - ARC_RADIUS - (TARGET >> 1) :
x[0] + ARC_RADIUS + (TARGET >> 1),
y[0]);
}
1999-08-16 02:44:15 +08:00
}
1999-08-15 23:59:06 +08:00
}
static void
measure_tool_control (Tool *tool,
ToolAction action,
GDisplay *gdisp)
1999-08-15 23:59:06 +08:00
{
MeasureTool *measure_tool;
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
switch (action)
{
case PAUSE:
draw_core_pause (measure_tool->core, tool);
break;
case RESUME:
draw_core_resume (measure_tool->core, tool);
break;
case HALT:
gtk_statusbar_pop (GTK_STATUSBAR (gdisp->statusbar),
measure_tool->context_id);
1999-08-15 23:59:06 +08:00
draw_core_stop (measure_tool->core, tool);
tool->state = INACTIVE;
break;
default:
break;
}
}
static void
measure_tool_info_update (void)
{
info_dialog_update (measure_tool_info);
info_dialog_popup (measure_tool_info);
}
static void
measure_tool_info_window_close_callback (GtkWidget *widget,
gpointer client_data)
{
info_dialog_free (measure_tool_info);
measure_tool_info = NULL;
}
1999-08-15 23:59:06 +08:00
Tool *
tools_new_measure_tool (void)
{
Tool *tool;
MeasureTool *private;
1999-08-15 23:59:06 +08:00
/* The tool options */
if (! measure_tool_options)
1999-08-15 23:59:06 +08:00
{
measure_tool_options = measure_tool_options_new ();
tools_register (MEASURE, (ToolOptions *) measure_tool_options);
1999-08-15 23:59:06 +08:00
}
tool = tools_new_tool (MEASURE);
allocate the tool's paint_core with g_new0. This prevents us from having 2000-06-22 Matt Wilson <msw@redhat.com> * app/paint_core.c (paint_core_new): allocate the tool's paint_core with g_new0. This prevents us from having cruft in unused tools. Systems with sensitive FPUs (Alpha) will raise exception in the paint_core_cursor_update if paint_core->last{x,y} are messy. * app/bezier_select.c (tools_new_bezier_select) * app/blend.c (blend_options_new) * app/brightness_contrast.c (tools_new_brightness_contrast) * app/bucket_fill.c (tools_new_bucket_fill) * app/by_color_select.c (tools_new_by_color_select) * app/color_balance.c (tools_new_color_balance) * app/color_panel.c (color_panel_new) * app/color_picker.c (tools_new_color_picker) * app/crop.c (tools_new_crop) * app/curves.c (tools_new_curves) * app/ellipse_select.c (tools_new_ellipse_select) * app/free_select.c (tools_new_free_select) * app/fuzzy_select.c (tools_new_fuzzy_select) * app/histogram_tool.c (tools_new_histogram_tool) * app/hue_saturation.c (tools_new_hue_saturation) * app/ink.c (tools_new_ink) * app/iscissors.c (tools_new_iscissors) * app/levels.c (tools_new_levels) * app/magnify.c (tools_new_magnify) * app/measure.c (tools_new_measure_tool) * app/move.c (tools_new_move_tool) * app/path_tool.c (tools_new_path_tool) * app/posterize.c (tools_new_posterize) * app/rect_select.c (tools_new_rect_select) * app/resize.c (resize_widget_new) * app/threshold.c (tools_new_threshold) * app/transform_core.c (transform_core_new) * app/xinput_airbrush.c (tools_new_xinput_airbrush): likewise (it can only help and it really isn't slow.) * app/color_area.c: #include <string.h> for memcpy declaration * app/gimphelp.c: #include <string.h> for strlen declaration
2000-06-23 08:14:07 +08:00
private = g_new0 (MeasureTool, 1);
1999-08-15 23:59:06 +08:00
private->core = draw_core_new (measure_tool_draw);
private->num_points = 0;
private->function = CREATING;
1999-08-15 23:59:06 +08:00
tool->private = (void *) private;
tool->preserve = TRUE; /* Preserve tool across drawable changes */
1999-08-15 23:59:06 +08:00
tool->button_press_func = measure_tool_button_press;
tool->button_release_func = measure_tool_button_release;
tool->motion_func = measure_tool_motion;
tool->cursor_update_func = measure_tool_cursor_update;
tool->control_func = measure_tool_control;
return tool;
}
void
tools_free_measure_tool (Tool *tool)
{
MeasureTool *measure_tool;
1999-08-15 23:59:06 +08:00
measure_tool = (MeasureTool *) tool->private;
if (tool->state == ACTIVE)
{
gtk_statusbar_pop (GTK_STATUSBAR (tool->gdisp->statusbar),
measure_tool->context_id);
1999-08-15 23:59:06 +08:00
draw_core_stop (measure_tool->core, tool);
}
draw_core_free (measure_tool->core);
if (measure_tool_info)
{
info_dialog_free (measure_tool_info);
measure_tool_info = NULL;
}
1999-08-15 23:59:06 +08:00
g_free (measure_tool);
}