2001-03-02 15:24:12 +00: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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
|
2005-05-07 13:24:47 +00:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2001-11-22 13:01:26 +00:00
|
|
|
#include "libgimpwidgets/gimpwidgets.h"
|
|
|
|
|
2001-05-09 22:34:59 +00:00
|
|
|
#include "tools-types.h"
|
2001-03-02 15:24:12 +00:00
|
|
|
|
2003-09-03 15:13:19 +00:00
|
|
|
#include "core/gimpchannel.h"
|
2001-05-09 02:32:03 +00:00
|
|
|
#include "core/gimpimage.h"
|
2005-07-11 19:21:52 +00:00
|
|
|
#include "core/gimppickable.h"
|
2001-11-21 03:54:09 +00:00
|
|
|
#include "core/gimptoolinfo.h"
|
2001-05-09 02:32:03 +00:00
|
|
|
|
2001-09-25 23:23:09 +00:00
|
|
|
#include "display/gimpdisplay.h"
|
|
|
|
|
2002-03-03 10:38:37 +00:00
|
|
|
#include "gimpeditselectiontool.h"
|
2001-03-02 15:24:12 +00:00
|
|
|
#include "gimpselectiontool.h"
|
2003-02-08 21:12:03 +00:00
|
|
|
#include "gimpselectionoptions.h"
|
2003-04-15 16:05:52 +00:00
|
|
|
#include "gimptoolcontrol.h"
|
2001-03-02 15:24:12 +00:00
|
|
|
|
2005-05-07 13:24:47 +00:00
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
2001-03-02 15:24:12 +00:00
|
|
|
|
2005-05-07 13:24:47 +00:00
|
|
|
static void gimp_selection_tool_control (GimpTool *tool,
|
|
|
|
GimpToolAction action,
|
|
|
|
GimpDisplay *gdisp);
|
2004-07-26 14:50:51 +00:00
|
|
|
static void gimp_selection_tool_modifier_key (GimpTool *tool,
|
|
|
|
GdkModifierType key,
|
|
|
|
gboolean press,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp);
|
|
|
|
static void gimp_selection_tool_oper_update (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp);
|
|
|
|
static void gimp_selection_tool_cursor_update (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp);
|
2001-03-02 15:24:12 +00:00
|
|
|
|
|
|
|
|
2005-12-13 09:13:50 +00:00
|
|
|
G_DEFINE_TYPE (GimpSelectionTool, gimp_selection_tool, GIMP_TYPE_DRAW_TOOL);
|
2001-03-02 15:24:12 +00:00
|
|
|
|
2005-12-13 09:13:50 +00:00
|
|
|
#define parent_class gimp_selection_tool_parent_class
|
2001-03-02 15:24:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_selection_tool_class_init (GimpSelectionToolClass *klass)
|
|
|
|
{
|
2004-03-10 12:45:11 +00:00
|
|
|
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
|
2001-03-02 15:24:12 +00:00
|
|
|
|
2005-05-07 13:24:47 +00:00
|
|
|
tool_class->control = gimp_selection_tool_control;
|
2001-11-21 03:54:09 +00:00
|
|
|
tool_class->modifier_key = gimp_selection_tool_modifier_key;
|
2004-06-12 18:41:52 +00:00
|
|
|
tool_class->key_press = gimp_edit_selection_tool_key_press;
|
2001-03-02 15:24:12 +00:00
|
|
|
tool_class->oper_update = gimp_selection_tool_oper_update;
|
2001-11-08 19:14:51 +00:00
|
|
|
tool_class->cursor_update = gimp_selection_tool_cursor_update;
|
2001-03-02 15:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_selection_tool_init (GimpSelectionTool *selection_tool)
|
|
|
|
{
|
2001-11-21 03:54:09 +00:00
|
|
|
selection_tool->op = SELECTION_REPLACE;
|
|
|
|
selection_tool->saved_op = SELECTION_REPLACE;
|
|
|
|
}
|
|
|
|
|
2005-05-07 13:24:47 +00:00
|
|
|
static void
|
|
|
|
gimp_selection_tool_control (GimpTool *tool,
|
|
|
|
GimpToolAction action,
|
|
|
|
GimpDisplay *gdisp)
|
|
|
|
{
|
|
|
|
if (action == HALT)
|
|
|
|
gimp_tool_pop_status (tool, gdisp);
|
|
|
|
|
|
|
|
GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
|
|
|
|
}
|
|
|
|
|
2001-11-21 03:54:09 +00:00
|
|
|
static void
|
|
|
|
gimp_selection_tool_modifier_key (GimpTool *tool,
|
|
|
|
GdkModifierType key,
|
|
|
|
gboolean press,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp)
|
|
|
|
{
|
2004-07-26 14:50:51 +00:00
|
|
|
GimpSelectionTool *selection_tool = GIMP_SELECTION_TOOL (tool);
|
2003-02-05 14:39:40 +00:00
|
|
|
GimpSelectionOptions *options;
|
2001-11-21 03:54:09 +00:00
|
|
|
|
2004-07-26 14:50:51 +00:00
|
|
|
options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
|
2001-11-21 03:54:09 +00:00
|
|
|
|
|
|
|
if (key == GDK_SHIFT_MASK || key == GDK_CONTROL_MASK)
|
|
|
|
{
|
2004-07-13 16:36:29 +00:00
|
|
|
SelectOps button_op = options->operation;
|
2003-09-17 11:16:55 +00:00
|
|
|
|
2001-11-21 03:54:09 +00:00
|
|
|
if (press)
|
|
|
|
{
|
2002-03-14 22:42:50 +00:00
|
|
|
if (key == (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)))
|
2001-11-21 03:54:09 +00:00
|
|
|
{
|
2002-03-14 22:42:50 +00:00
|
|
|
/* first modifier pressed */
|
|
|
|
|
2003-02-07 17:12:21 +00:00
|
|
|
selection_tool->saved_op = options->operation;
|
2001-11-21 03:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-14 22:42:50 +00:00
|
|
|
if (! (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)))
|
2001-11-21 03:54:09 +00:00
|
|
|
{
|
2002-03-14 22:42:50 +00:00
|
|
|
/* last modifier released */
|
|
|
|
|
2001-11-22 13:01:26 +00:00
|
|
|
button_op = selection_tool->saved_op;
|
2001-11-21 03:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK))
|
|
|
|
{
|
2001-11-22 13:01:26 +00:00
|
|
|
button_op = SELECTION_INTERSECT;
|
2001-11-21 03:54:09 +00:00
|
|
|
}
|
|
|
|
else if (state & GDK_SHIFT_MASK)
|
|
|
|
{
|
2001-11-22 13:01:26 +00:00
|
|
|
button_op = SELECTION_ADD;
|
2001-11-21 03:54:09 +00:00
|
|
|
}
|
|
|
|
else if (state & GDK_CONTROL_MASK)
|
|
|
|
{
|
2002-03-17 22:54:26 +00:00
|
|
|
button_op = SELECTION_SUBTRACT;
|
2001-11-21 03:54:09 +00:00
|
|
|
}
|
|
|
|
|
2003-02-07 17:12:21 +00:00
|
|
|
if (button_op != options->operation)
|
2001-11-21 03:54:09 +00:00
|
|
|
{
|
2003-02-28 01:14:30 +00:00
|
|
|
g_object_set (options, "operation", button_op, NULL);
|
2001-11-21 03:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-08 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_selection_tool_oper_update (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp)
|
|
|
|
{
|
2004-07-26 14:50:51 +00:00
|
|
|
GimpSelectionTool *selection_tool = GIMP_SELECTION_TOOL (tool);
|
2003-02-05 14:39:40 +00:00
|
|
|
GimpSelectionOptions *options;
|
2003-09-03 15:13:19 +00:00
|
|
|
GimpChannel *selection;
|
2003-02-05 14:39:40 +00:00
|
|
|
GimpLayer *layer;
|
|
|
|
GimpLayer *floating_sel;
|
2005-05-07 13:24:47 +00:00
|
|
|
const gchar *status = NULL;
|
2003-05-16 12:10:47 +00:00
|
|
|
gboolean move_layer = FALSE;
|
|
|
|
gboolean move_floating_sel = FALSE;
|
2001-11-08 19:14:51 +00:00
|
|
|
|
2004-07-26 14:50:51 +00:00
|
|
|
options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
|
2001-11-21 03:54:09 +00:00
|
|
|
|
2003-09-03 15:13:19 +00:00
|
|
|
selection = gimp_image_get_mask (gdisp->gimage);
|
|
|
|
layer = gimp_image_pick_correlate_layer (gdisp->gimage,
|
|
|
|
coords->x, coords->y);
|
2001-11-08 19:14:51 +00:00
|
|
|
floating_sel = gimp_image_floating_sel (gdisp->gimage);
|
|
|
|
|
2003-05-16 12:10:47 +00:00
|
|
|
if (layer)
|
|
|
|
{
|
|
|
|
if (floating_sel)
|
|
|
|
{
|
|
|
|
if (layer == floating_sel)
|
|
|
|
move_floating_sel = TRUE;
|
|
|
|
}
|
2005-07-11 19:21:52 +00:00
|
|
|
else if (gimp_pickable_get_opacity_at (GIMP_PICKABLE (selection),
|
|
|
|
coords->x, coords->y))
|
2003-05-16 12:10:47 +00:00
|
|
|
{
|
|
|
|
move_layer = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && move_layer)
|
|
|
|
{
|
|
|
|
selection_tool->op = SELECTION_MOVE_COPY; /* move a copy of the selection */
|
|
|
|
}
|
2003-09-03 15:13:19 +00:00
|
|
|
else if ((state & GDK_MOD1_MASK) && ! gimp_channel_is_empty (selection))
|
2001-11-08 19:14:51 +00:00
|
|
|
{
|
|
|
|
selection_tool->op = SELECTION_MOVE_MASK; /* move the selection mask */
|
|
|
|
}
|
|
|
|
else if (! (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) &&
|
2003-05-16 12:10:47 +00:00
|
|
|
(move_layer || move_floating_sel))
|
2001-11-08 19:14:51 +00:00
|
|
|
{
|
|
|
|
selection_tool->op = SELECTION_MOVE; /* move the selection */
|
|
|
|
}
|
|
|
|
else if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK))
|
|
|
|
{
|
|
|
|
selection_tool->op = SELECTION_INTERSECT; /* intersect with selection */
|
|
|
|
}
|
|
|
|
else if (state & GDK_SHIFT_MASK)
|
|
|
|
{
|
|
|
|
selection_tool->op = SELECTION_ADD; /* add to the selection */
|
|
|
|
}
|
|
|
|
else if (state & GDK_CONTROL_MASK)
|
|
|
|
{
|
2002-03-17 22:54:26 +00:00
|
|
|
selection_tool->op = SELECTION_SUBTRACT; /* subtract from the selection */
|
2001-11-08 19:14:51 +00:00
|
|
|
}
|
|
|
|
else if (floating_sel)
|
|
|
|
{
|
|
|
|
selection_tool->op = SELECTION_ANCHOR; /* anchor the selection */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-02-07 17:12:21 +00:00
|
|
|
selection_tool->op = options->operation;
|
2001-11-08 19:14:51 +00:00
|
|
|
}
|
2005-05-07 13:24:47 +00:00
|
|
|
|
|
|
|
if (! gimp_enum_get_value (GIMP_TYPE_CHANNEL_OPS, selection_tool->op,
|
|
|
|
NULL, NULL, &status, NULL))
|
|
|
|
{
|
|
|
|
switch (selection_tool->op)
|
|
|
|
{
|
|
|
|
case SELECTION_MOVE_MASK:
|
|
|
|
status = _("Move the selection mask");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SELECTION_MOVE:
|
|
|
|
status = _("Move the selected pixels");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SELECTION_MOVE_COPY:
|
|
|
|
status = _("Move a copy of the selected pixels");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SELECTION_ANCHOR:
|
|
|
|
status = _("Anchor the floating selection");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_return_if_reached ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-15 18:23:33 +00:00
|
|
|
gimp_tool_push_status (tool, gdisp, status);
|
2001-03-02 15:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-11-08 19:14:51 +00:00
|
|
|
gimp_selection_tool_cursor_update (GimpTool *tool,
|
|
|
|
GimpCoords *coords,
|
|
|
|
GdkModifierType state,
|
|
|
|
GimpDisplay *gdisp)
|
2001-03-02 15:24:12 +00:00
|
|
|
{
|
2004-03-10 12:45:11 +00:00
|
|
|
GimpSelectionTool *selection_tool = GIMP_SELECTION_TOOL (tool);
|
2002-02-04 17:43:01 +00:00
|
|
|
GimpToolCursorType tool_cursor;
|
|
|
|
GimpCursorModifier cmodifier;
|
2001-03-02 15:24:12 +00:00
|
|
|
|
2002-05-03 11:31:08 +00:00
|
|
|
tool_cursor = gimp_tool_control_get_tool_cursor (tool->control);
|
2002-02-04 17:43:01 +00:00
|
|
|
cmodifier = GIMP_CURSOR_MODIFIER_NONE;
|
2001-10-31 21:20:09 +00:00
|
|
|
|
2001-03-02 15:24:12 +00:00
|
|
|
switch (selection_tool->op)
|
|
|
|
{
|
|
|
|
case SELECTION_ADD:
|
2002-02-04 17:43:01 +00:00
|
|
|
cmodifier = GIMP_CURSOR_MODIFIER_PLUS;
|
2001-03-02 15:24:12 +00:00
|
|
|
break;
|
2002-03-17 22:54:26 +00:00
|
|
|
case SELECTION_SUBTRACT:
|
2002-02-04 17:43:01 +00:00
|
|
|
cmodifier = GIMP_CURSOR_MODIFIER_MINUS;
|
2001-03-02 15:24:12 +00:00
|
|
|
break;
|
2004-03-10 12:45:11 +00:00
|
|
|
case SELECTION_INTERSECT:
|
2002-02-04 17:43:01 +00:00
|
|
|
cmodifier = GIMP_CURSOR_MODIFIER_INTERSECT;
|
2001-03-02 15:24:12 +00:00
|
|
|
break;
|
|
|
|
case SELECTION_REPLACE:
|
|
|
|
break;
|
|
|
|
case SELECTION_MOVE_MASK:
|
2002-02-04 17:43:01 +00:00
|
|
|
cmodifier = GIMP_CURSOR_MODIFIER_MOVE;
|
2001-03-02 15:24:12 +00:00
|
|
|
break;
|
|
|
|
case SELECTION_MOVE:
|
2003-05-16 12:10:47 +00:00
|
|
|
case SELECTION_MOVE_COPY:
|
2004-06-04 23:08:29 +00:00
|
|
|
tool_cursor = GIMP_TOOL_CURSOR_MOVE;
|
2001-03-02 15:24:12 +00:00
|
|
|
break;
|
|
|
|
case SELECTION_ANCHOR:
|
2002-02-04 17:43:01 +00:00
|
|
|
cmodifier = GIMP_CURSOR_MODIFIER_ANCHOR;
|
2001-03-02 15:24:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-04-21 07:31:12 +00:00
|
|
|
|
2005-05-07 13:24:47 +00:00
|
|
|
gimp_tool_set_cursor (tool, gdisp, GIMP_CURSOR_MOUSE, tool_cursor, cmodifier);
|
2001-03-02 15:24:12 +00:00
|
|
|
}
|
2004-07-26 14:50:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gimp_selection_tool_start_edit (GimpSelectionTool *sel_tool,
|
|
|
|
GimpCoords *coords)
|
|
|
|
{
|
|
|
|
GimpTool *tool;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_SELECTION_TOOL (sel_tool), FALSE);
|
|
|
|
g_return_val_if_fail (coords != NULL, FALSE);
|
|
|
|
|
|
|
|
tool = GIMP_TOOL (sel_tool);
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_DISPLAY (tool->gdisp), FALSE);
|
|
|
|
g_return_val_if_fail (gimp_tool_control_is_active (tool->control), FALSE);
|
|
|
|
|
|
|
|
switch (sel_tool->op)
|
|
|
|
{
|
|
|
|
case SELECTION_MOVE_MASK:
|
|
|
|
gimp_edit_selection_tool_start (tool, tool->gdisp, coords,
|
2004-10-06 21:04:13 +00:00
|
|
|
GIMP_TRANSLATE_MODE_MASK, FALSE);
|
2004-07-26 14:50:51 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case SELECTION_MOVE:
|
|
|
|
gimp_edit_selection_tool_start (tool, tool->gdisp, coords,
|
2004-10-06 21:04:13 +00:00
|
|
|
GIMP_TRANSLATE_MODE_MASK_TO_LAYER, FALSE);
|
2004-07-26 14:50:51 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case SELECTION_MOVE_COPY:
|
|
|
|
gimp_edit_selection_tool_start (tool, tool->gdisp, coords,
|
2004-10-06 21:04:13 +00:00
|
|
|
GIMP_TRANSLATE_MODE_MASK_COPY_TO_LAYER,
|
|
|
|
FALSE);
|
2004-07-26 14:50:51 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|