1997-11-25 06:05:25 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
1998-04-13 13:44:11 +08:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2000-07-30 00:12:40 +08:00
|
|
|
|
2000-12-17 05:37:03 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-03-29 07:39:32 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
#include <gtk/gtk.h>
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-08-06 00:08:19 +08:00
|
|
|
#include "libgimpwidgets/gimpwidgets.h"
|
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "tools-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-05-09 10:32:03 +08:00
|
|
|
#include "core/gimpchannel.h"
|
|
|
|
#include "core/gimpimage.h"
|
|
|
|
#include "core/gimpimage-mask.h"
|
2001-10-22 20:13:44 +08:00
|
|
|
#include "core/gimpimage-mask-select.h"
|
2001-11-20 21:53:21 +08:00
|
|
|
#include "core/gimptoolinfo.h"
|
2001-01-25 06:36:18 +08:00
|
|
|
|
2001-09-26 07:23:09 +08:00
|
|
|
#include "display/gimpdisplay.h"
|
2001-10-13 20:52:30 +08:00
|
|
|
#include "display/gimpdisplay-foreach.h"
|
2001-09-26 07:23:09 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
#include "gimpeditselectiontool.h"
|
|
|
|
#include "gimpfreeselecttool.h"
|
1999-04-13 01:55:06 +08:00
|
|
|
#include "selection_options.h"
|
2001-03-10 01:39:18 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
#include "floating_sel.h"
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
#include "libgimp/gimpintl.h"
|
|
|
|
|
2000-07-30 00:12:40 +08:00
|
|
|
|
2001-10-22 20:13:44 +08:00
|
|
|
#define DEFAULT_MAX_INC 1024
|
1999-04-09 06:25:54 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static void gimp_free_select_tool_class_init (GimpFreeSelectToolClass *klass);
|
|
|
|
static void gimp_free_select_tool_init (GimpFreeSelectTool *free_select);
|
2001-11-09 03:14:51 +08:00
|
|
|
static void gimp_free_select_tool_finalize (GObject *object);
|
|
|
|
|
|
|
|
static void gimp_free_select_tool_button_press (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
guint32 time,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp);
|
|
|
|
static void gimp_free_select_tool_button_release (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
guint32 time,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp);
|
|
|
|
static void gimp_free_select_tool_motion (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
guint32 time,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp);
|
|
|
|
|
|
|
|
static void gimp_free_select_tool_draw (GimpDrawTool *draw_tool);
|
2000-03-02 07:22:43 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static void gimp_free_select_tool_add_point (GimpFreeSelectTool *free_sel,
|
2001-11-09 03:14:51 +08:00
|
|
|
gdouble x,
|
|
|
|
gdouble y);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-12-31 12:07:42 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static GimpSelectionToolClass *parent_class = NULL;
|
1999-04-13 01:55:06 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
/* public functions */
|
1999-04-09 06:25:54 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
void
|
2001-11-21 07:00:47 +08:00
|
|
|
gimp_free_select_tool_register (Gimp *gimp,
|
|
|
|
GimpToolRegisterCallback callback)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-11-21 07:00:47 +08:00
|
|
|
(* callback) (gimp,
|
|
|
|
GIMP_TYPE_FREE_SELECT_TOOL,
|
|
|
|
selection_options_new,
|
|
|
|
FALSE,
|
|
|
|
"gimp:free_select_tool",
|
|
|
|
_("Free Select"),
|
|
|
|
_("Select hand-drawn regions"),
|
|
|
|
_("/Tools/Selection Tools/Free Select"), "F",
|
|
|
|
NULL, "tools/free_select.html",
|
|
|
|
GIMP_STOCK_TOOL_FREE_SELECT);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
GType
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_free_select_tool_get_type (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-08-14 22:53:55 +08:00
|
|
|
static GType tool_type = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
if (! tool_type)
|
2001-03-10 01:39:18 +08:00
|
|
|
{
|
2001-08-14 22:53:55 +08:00
|
|
|
static const GTypeInfo tool_info =
|
2001-03-10 01:39:18 +08:00
|
|
|
{
|
|
|
|
sizeof (GimpFreeSelectToolClass),
|
2001-08-14 22:53:55 +08:00
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gimp_free_select_tool_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GimpFreeSelectTool),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) gimp_free_select_tool_init,
|
2001-03-10 01:39:18 +08:00
|
|
|
};
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
tool_type = g_type_register_static (GIMP_TYPE_SELECTION_TOOL,
|
|
|
|
"GimpFreeSelectTool",
|
|
|
|
&tool_info, 0);
|
2001-03-10 01:39:18 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
return tool_type;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
/* private functions */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_free_select_tool_class_init (GimpFreeSelectToolClass *klass)
|
|
|
|
{
|
2001-08-14 22:53:55 +08:00
|
|
|
GObjectClass *object_class;
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpToolClass *tool_class;
|
|
|
|
GimpDrawToolClass *draw_tool_class;
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
tool_class = GIMP_TOOL_CLASS (klass);
|
|
|
|
draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
|
2001-03-10 01:39:18 +08:00
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
2001-03-10 01:39:18 +08:00
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
object_class->finalize = gimp_free_select_tool_finalize;
|
2001-03-10 01:39:18 +08:00
|
|
|
|
|
|
|
tool_class->button_press = gimp_free_select_tool_button_press;
|
|
|
|
tool_class->button_release = gimp_free_select_tool_button_release;
|
|
|
|
tool_class->motion = gimp_free_select_tool_motion;
|
|
|
|
|
|
|
|
draw_tool_class->draw = gimp_free_select_tool_draw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_free_select_tool_init (GimpFreeSelectTool *free_select)
|
|
|
|
{
|
|
|
|
GimpTool *tool;
|
|
|
|
GimpSelectionTool *select_tool;
|
|
|
|
|
|
|
|
tool = GIMP_TOOL (free_select);
|
|
|
|
select_tool = GIMP_SELECTION_TOOL (free_select);
|
|
|
|
|
|
|
|
tool->tool_cursor = GIMP_FREE_SELECT_TOOL_CURSOR;
|
|
|
|
tool->scroll_lock = TRUE; /* Do not allow scrolling */
|
|
|
|
|
|
|
|
free_select->points = NULL;
|
|
|
|
free_select->num_points = 0;
|
|
|
|
free_select->max_segs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-08-14 22:53:55 +08:00
|
|
|
gimp_free_select_tool_finalize (GObject *object)
|
2001-03-10 01:39:18 +08:00
|
|
|
{
|
|
|
|
GimpFreeSelectTool *free_sel;
|
|
|
|
|
|
|
|
free_sel = GIMP_FREE_SELECT_TOOL (object);
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
if (free_sel->points)
|
|
|
|
{
|
|
|
|
g_free (free_sel->points);
|
|
|
|
free_sel->points = NULL;
|
|
|
|
}
|
2001-03-10 01:39:18 +08:00
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2001-03-10 01:39:18 +08:00
|
|
|
}
|
|
|
|
|
2000-12-31 12:07:42 +08:00
|
|
|
static void
|
2001-11-09 03:14:51 +08:00
|
|
|
gimp_free_select_tool_button_press (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
guint32 time,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFreeSelectTool *free_sel;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel = GIMP_FREE_SELECT_TOOL (tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-09 03:14:51 +08:00
|
|
|
tool->state = ACTIVE;
|
|
|
|
tool->gdisp = gdisp;
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
switch (GIMP_SELECTION_TOOL (tool)->op)
|
2000-03-29 07:39:32 +08:00
|
|
|
{
|
|
|
|
case SELECTION_MOVE_MASK:
|
2001-11-09 03:14:51 +08:00
|
|
|
init_edit_selection (tool, gdisp, coords, EDIT_MASK_TRANSLATE);
|
2000-03-29 07:39:32 +08:00
|
|
|
return;
|
|
|
|
case SELECTION_MOVE:
|
2001-11-09 03:14:51 +08:00
|
|
|
init_edit_selection (tool, gdisp, coords, EDIT_MASK_TO_LAYER_TRANSLATE);
|
2000-03-29 07:39:32 +08:00
|
|
|
return;
|
2000-04-07 02:59:48 +08:00
|
|
|
default:
|
2000-04-26 08:17:54 +08:00
|
|
|
break;
|
2000-03-29 07:39:32 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel->num_points = 0;
|
|
|
|
|
2001-11-09 03:14:51 +08:00
|
|
|
gimp_free_select_tool_add_point (free_sel, coords->x, coords->y);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-02-03 20:10:23 +08:00
|
|
|
gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), gdisp);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-12-31 12:07:42 +08:00
|
|
|
static void
|
2001-11-09 03:14:51 +08:00
|
|
|
gimp_free_select_tool_button_release (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
guint32 time,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFreeSelectTool *free_sel;
|
2001-11-20 21:53:21 +08:00
|
|
|
SelectionOptions *sel_options;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel = GIMP_FREE_SELECT_TOOL (tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-20 21:53:21 +08:00
|
|
|
sel_options = (SelectionOptions *) tool->tool_info->tool_options;
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
tool->state = INACTIVE;
|
|
|
|
|
|
|
|
/* First take care of the case where the user "cancels" the action */
|
2001-11-09 03:14:51 +08:00
|
|
|
if (! (state & GDK_BUTTON3_MASK))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
if (free_sel->num_points == 1)
|
2000-07-30 00:12:40 +08:00
|
|
|
{
|
|
|
|
/* If there is a floating selection, anchor it */
|
2000-12-29 23:22:01 +08:00
|
|
|
if (gimp_image_floating_sel (gdisp->gimage))
|
|
|
|
floating_sel_anchor (gimp_image_floating_sel (gdisp->gimage));
|
2000-07-30 00:12:40 +08:00
|
|
|
/* Otherwise, clear the selection mask */
|
|
|
|
else
|
2001-11-29 06:42:19 +08:00
|
|
|
gimp_image_mask_clear (gdisp->gimage);
|
2001-03-10 01:39:18 +08:00
|
|
|
|
2000-07-30 00:12:40 +08:00
|
|
|
gdisplays_flush ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-22 20:13:44 +08:00
|
|
|
gimp_image_mask_select_polygon (gdisp->gimage,
|
2001-11-09 03:14:51 +08:00
|
|
|
free_sel->num_points,
|
|
|
|
free_sel->points,
|
2001-10-22 20:13:44 +08:00
|
|
|
GIMP_SELECTION_TOOL (tool)->op,
|
2001-11-20 21:53:21 +08:00
|
|
|
sel_options->antialias,
|
|
|
|
sel_options->feather,
|
|
|
|
sel_options->feather_radius,
|
|
|
|
sel_options->feather_radius);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gdisplays_flush ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-31 12:07:42 +08:00
|
|
|
static void
|
2001-11-09 03:14:51 +08:00
|
|
|
gimp_free_select_tool_motion (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
guint32 time,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFreeSelectTool *free_sel;
|
|
|
|
GimpSelectionTool *sel_tool;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel = GIMP_FREE_SELECT_TOOL (tool);
|
|
|
|
sel_tool = GIMP_SELECTION_TOOL (tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-03-02 07:22:43 +08:00
|
|
|
if (tool->state != ACTIVE)
|
|
|
|
return;
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
if (sel_tool->op == SELECTION_ANCHOR)
|
2000-07-30 00:12:40 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
sel_tool->op = SELECTION_REPLACE;
|
2000-07-30 00:12:40 +08:00
|
|
|
|
2001-11-09 03:14:51 +08:00
|
|
|
gimp_tool_cursor_update (tool, coords, state, gdisp);
|
2000-07-30 00:12:40 +08:00
|
|
|
}
|
|
|
|
|
2001-11-09 03:14:51 +08:00
|
|
|
gimp_free_select_tool_add_point (free_sel, coords->x, coords->y);
|
|
|
|
|
2001-11-12 22:45:58 +08:00
|
|
|
gimp_draw_tool_draw_line (GIMP_DRAW_TOOL (tool),
|
|
|
|
free_sel->points[free_sel->num_points - 2].x,
|
|
|
|
free_sel->points[free_sel->num_points - 2].y,
|
|
|
|
free_sel->points[free_sel->num_points - 1].x,
|
|
|
|
free_sel->points[free_sel->num_points - 1].y,
|
|
|
|
FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_free_select_tool_draw (GimpDrawTool *draw_tool)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFreeSelectTool *free_sel;
|
|
|
|
gint i;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-09 03:14:51 +08:00
|
|
|
free_sel = GIMP_FREE_SELECT_TOOL (draw_tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
for (i = 1; i < free_sel->num_points; i++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-11-12 22:45:58 +08:00
|
|
|
gimp_draw_tool_draw_line (draw_tool,
|
|
|
|
free_sel->points[i - 1].x,
|
|
|
|
free_sel->points[i - 1].y,
|
|
|
|
free_sel->points[i].x,
|
|
|
|
free_sel->points[i].y,
|
|
|
|
FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static void
|
|
|
|
gimp_free_select_tool_add_point (GimpFreeSelectTool *free_sel,
|
2001-11-09 03:14:51 +08:00
|
|
|
gdouble x,
|
|
|
|
gdouble y)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
if (free_sel->num_points >= free_sel->max_segs)
|
1999-04-13 01:55:06 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel->max_segs += DEFAULT_MAX_INC;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-09 03:14:51 +08:00
|
|
|
free_sel->points = g_realloc (free_sel->points,
|
|
|
|
sizeof (GimpVector2) * free_sel->max_segs);
|
2001-03-10 01:39:18 +08:00
|
|
|
}
|
2001-02-25 03:29:47 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel->points[free_sel->num_points].x = x;
|
|
|
|
free_sel->points[free_sel->num_points].y = y;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
free_sel->num_points++;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|