gimp/app/tools/gimpfuzzyselecttool.c
Sven Neumann 073e533a8a Finally landed the new GimpConfig based gimprc parser. It's not finished
2002-11-18  Sven Neumann  <sven@gimp.org>

	Finally landed the new GimpConfig based gimprc parser. It's not
	finished yet but we need to start somewhere. This release removes
	the old gimprc.[ch] files. The gimprc format changes slightly, but
	the changes are minimal. The Preferences dialog is temporarily
	disabled since it still needs to be ported. If you are are afraid,
	stay away from CVS for a few days ;-)

	* app/Makefile.am
	* app/gimprc.[ch]: removed the old gimprc system.

	* app/base/Makefile.am
	* app/base/base-config.[ch]: removed these files in favor of
	config/gimpbaseconfig.[ch].

	* app/core/Makefile.am
	* app/core/gimpcoreconfig.[ch]: removed these files in favor of
	config/gimpcoreconfig.[ch].

	* app/config/Makefile.am
	* app/config/config-types.h: moved typedefs into this new file.

	* app/config/gimpbaseconfig.[ch]
	* app/config/gimpcoreconfig.[ch]
	* app/config/gimpdisplayconfig.[ch]
	* app/config/gimpguiconfig.[ch]
	* app/config/gimprc.[ch]
	* app/config/test-config.c: brought into shape for real use.

	* app/base/base-types.h: include config/config-types.h here. Added
	a global GimpBaseConfig *base_config variable to ease migration.

	* app/gui/Makefile.am: temporarily disabled the preferences dialog.

	* app/app_procs.c
	* app/undo.c
	* app/undo_history.c
	* app/base/base.[ch]
	* app/base/gimphistogram.c
	* app/base/pixel-processor.c
	* app/base/temp-buf.c
	* app/base/tile-cache.c
	* app/core/core-types.h
	* app/core/gimp-documents.c
	* app/core/gimp.c
	* app/core/gimpbrush.c
	* app/core/gimpbrushgenerated.c
	* app/core/gimpcontext.c
	* app/core/gimpdrawable-transform.c
	* app/core/gimpimage-new.c
	* app/core/gimpimage.c
	* app/core/gimpimagefile.c
	* app/core/gimpmodules.c
	* app/core/gimppattern.c
	* app/display/Makefile.am
	* app/display/gimpdisplay-handlers.c
	* app/display/gimpdisplay.[ch]
	* app/display/gimpdisplayshell-callbacks.c
	* app/display/gimpdisplayshell-handlers.c
	* app/display/gimpdisplayshell-layer-select.c
	* app/display/gimpdisplayshell-render.c
	* app/display/gimpdisplayshell-scale.c
	* app/display/gimpdisplayshell-scroll.c
	* app/display/gimpdisplayshell-selection.c
	* app/display/gimpdisplayshell.[ch]
	* app/display/gimpnavigationview.c
	* app/file/file-save.c
	* app/gui/device-status-dialog.c
	* app/gui/dialogs-constructors.c
	* app/gui/file-commands.c
	* app/gui/file-new-dialog.c
	* app/gui/file-open-dialog.c
	* app/gui/file-save-dialog.c
	* app/gui/gui.c
	* app/gui/menus.c
	* app/gui/paths-dialog.c
	* app/gui/resize-dialog.c
	* app/gui/session.c
	* app/gui/test-commands.c
	* app/gui/tips-dialog.c
	* app/gui/tips-dialog.h
	* app/gui/user-install-dialog.c
	* app/gui/view-commands.c
	* app/paint/gimppaintcore.c
	* app/plug-in/plug-in.c
	* app/plug-in/plug-ins.c
	* app/tools/gimpbezierselecttool.c
	* app/tools/gimpbucketfilltool.c
	* app/tools/gimpcolorpickertool.c
	* app/tools/gimpcroptool.c
	* app/tools/gimpeditselectiontool.c
	* app/tools/gimpfuzzyselecttool.c
	* app/tools/gimpinktool.c
	* app/tools/gimpmagnifytool.c
	* app/tools/gimpmeasuretool.c
	* app/tools/gimppainttool.c
	* app/tools/gimppathtool.c
	* app/tools/gimptexttool.[ch]
	* app/tools/selection_options.c
	* app/tools/tools.c
	* app/tools/transform_options.c
	* app/widgets/gimphelp.c
	* app/widgets/gimpitemfactory.c
	* app/widgets/gimpselectioneditor.c
	* app/xcf/xcf-load.c
	* tools/pdbgen/pdb/fileops.pdb
	* tools/pdbgen/pdb/gimprc.pdb
	* tools/pdbgen/pdb/image.pdb
	* tools/pdbgen/pdb/layer.pdb
	* tools/pdbgen/pdb/transform_tools.pdb: use the new config system
	instead of the old gimprc stuff.

	* etc/gimprc.in
	* etc/gimprc_user.in: adapted to the new gimprc format. Will update
	the man-page later...

	* app/pdb/fileops_cmds.c
	* app/pdb/gimprc_cmds.c
	* app/pdb/image_cmds.c
	* app/pdb/layer_cmds.c
	* app/pdb/transform_tools_cmds.c
	* libgimp/gimpgimprc_pdb.c: regenerated.
2002-11-18 20:50:31 +00:00

475 lines
15 KiB
C

/* 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 <stdlib.h>
#include <gtk/gtk.h>
#include "libgimpwidgets/gimpwidgets.h"
#include "libgimpmath/gimpmath.h"
#include "tools-types.h"
#include "base/boundary.h"
#include "base/pixel-region.h"
#include "core/gimpchannel.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimage-contiguous-region.h"
#include "core/gimpimage-mask.h"
#include "core/gimpimage-mask-select.h"
#include "core/gimplayer-floating-sel.h"
#include "core/gimptoolinfo.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "gimpeditselectiontool.h"
#include "gimpfuzzyselecttool.h"
#include "selection_options.h"
#include "libgimp/gimpintl.h"
static void gimp_fuzzy_select_tool_class_init (GimpFuzzySelectToolClass *klass);
static void gimp_fuzzy_select_tool_init (GimpFuzzySelectTool *fuzzy_select);
static void gimp_fuzzy_select_tool_finalize (GObject *object);
static void gimp_fuzzy_select_tool_button_press (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_fuzzy_select_tool_button_release (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_fuzzy_select_tool_motion (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_fuzzy_select_tool_draw (GimpDrawTool *draw_tool);
static GdkSegment *
gimp_fuzzy_select_tool_calculate (GimpFuzzySelectTool *fuzzy_sel,
GimpDisplay *gdisp,
gint *num_segs);
static GimpSelectionToolClass *parent_class = NULL;
/* public functions */
void
gimp_fuzzy_select_tool_register (GimpToolRegisterCallback callback,
gpointer data)
{
(* callback) (GIMP_TYPE_FUZZY_SELECT_TOOL,
selection_options_new,
FALSE,
"gimp-fuzzy-select-tool",
_("Fuzzy Select"),
_("Select contiguous regions"),
_("/Tools/Selection Tools/Fuzzy Select"), "Z",
NULL, "tools/fuzzy_select.html",
GIMP_STOCK_TOOL_FUZZY_SELECT,
data);
}
GType
gimp_fuzzy_select_tool_get_type (void)
{
static GType tool_type = 0;
if (! tool_type)
{
static const GTypeInfo tool_info =
{
sizeof (GimpFuzzySelectToolClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gimp_fuzzy_select_tool_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GimpFuzzySelectTool),
0, /* n_preallocs */
(GInstanceInitFunc) gimp_fuzzy_select_tool_init,
};
tool_type = g_type_register_static (GIMP_TYPE_SELECTION_TOOL,
"GimpFuzzySelectTool",
&tool_info, 0);
}
return tool_type;
}
/* private functions */
static void
gimp_fuzzy_select_tool_class_init (GimpFuzzySelectToolClass *klass)
{
GObjectClass *object_class;
GimpToolClass *tool_class;
GimpDrawToolClass *draw_tool_class;
object_class = G_OBJECT_CLASS (klass);
tool_class = GIMP_TOOL_CLASS (klass);
draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gimp_fuzzy_select_tool_finalize;
tool_class->button_press = gimp_fuzzy_select_tool_button_press;
tool_class->button_release = gimp_fuzzy_select_tool_button_release;
tool_class->motion = gimp_fuzzy_select_tool_motion;
draw_tool_class->draw = gimp_fuzzy_select_tool_draw;
}
static void
gimp_fuzzy_select_tool_init (GimpFuzzySelectTool *fuzzy_select)
{
GimpTool *tool;
tool = GIMP_TOOL (fuzzy_select);
gimp_tool_control_set_scroll_lock (tool->control, TRUE);
gimp_tool_control_set_motion_mode (tool->control, GIMP_MOTION_MODE_COMPRESS);
gimp_tool_control_set_tool_cursor (tool->control, GIMP_FUZZY_SELECT_TOOL_CURSOR);
fuzzy_select->x = 0;
fuzzy_select->y = 0;
fuzzy_select->first_x = 0;
fuzzy_select->first_y = 0;
fuzzy_select->first_threshold = 0.0;
fuzzy_select->fuzzy_mask = NULL;
fuzzy_select->segs = NULL;
fuzzy_select->num_segs = 0;
}
static void
gimp_fuzzy_select_tool_finalize (GObject *object)
{
GimpFuzzySelectTool *fuzzy_sel;
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (object);
if (fuzzy_sel->fuzzy_mask)
{
g_object_unref (G_OBJECT (fuzzy_sel->fuzzy_mask));
fuzzy_sel->fuzzy_mask = NULL;
}
if (fuzzy_sel->segs)
{
g_free (fuzzy_sel->segs);
fuzzy_sel->segs = NULL;
fuzzy_sel->num_segs = 0;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_fuzzy_select_tool_button_press (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp)
{
GimpFuzzySelectTool *fuzzy_sel;
SelectionOptions *sel_options;
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (tool);
sel_options = (SelectionOptions *) tool->tool_info->tool_options;
fuzzy_sel->x = coords->x;
fuzzy_sel->y = coords->y;
fuzzy_sel->first_x = fuzzy_sel->x;
fuzzy_sel->first_y = fuzzy_sel->y;
fuzzy_sel->first_threshold = sel_options->threshold;
gimp_tool_control_activate (tool->control);
tool->gdisp = gdisp;
switch (GIMP_SELECTION_TOOL (tool)->op)
{
case SELECTION_MOVE_MASK:
init_edit_selection (tool, gdisp, coords, EDIT_MASK_TRANSLATE);
return;
case SELECTION_MOVE:
init_edit_selection (tool, gdisp, coords, EDIT_MASK_TO_LAYER_TRANSLATE);
return;
default:
break;
}
/* calculate the region boundary */
fuzzy_sel->segs = gimp_fuzzy_select_tool_calculate (fuzzy_sel, gdisp,
&fuzzy_sel->num_segs);
gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), gdisp);
}
static void
gimp_fuzzy_select_tool_button_release (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp)
{
GimpFuzzySelectTool *fuzzy_sel;
SelectionOptions *sel_options;
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (tool);
sel_options = (SelectionOptions *) tool->tool_info->tool_options;
gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
gimp_tool_control_halt (tool->control); /* sets paused_count to 0 -- is this ok? */
/* First take care of the case where the user "cancels" the action */
if (! (state & GDK_BUTTON3_MASK))
{
gint off_x, off_y;
if (GIMP_SELECTION_TOOL (tool)->op == SELECTION_ANCHOR)
{
/* If there is a floating selection, anchor it */
if (gimp_image_floating_sel (gdisp->gimage))
floating_sel_anchor (gimp_image_floating_sel (gdisp->gimage));
/* Otherwise, clear the selection mask */
else
gimp_image_mask_clear (gdisp->gimage);
gimp_image_flush (gdisp->gimage);
return;
}
if (sel_options->sample_merged)
{
off_x = 0;
off_y = 0;
}
else
{
GimpDrawable *drawable;
drawable = gimp_image_active_drawable (gdisp->gimage);
gimp_drawable_offsets (drawable, &off_x, &off_y);
}
gimp_image_mask_select_channel (gdisp->gimage,
fuzzy_sel->fuzzy_mask,
off_x,
off_y,
GIMP_SELECTION_TOOL (tool)->op,
sel_options->feather,
sel_options->feather_radius,
sel_options->feather_radius);
g_object_unref (G_OBJECT (fuzzy_sel->fuzzy_mask));
fuzzy_sel->fuzzy_mask = NULL;
gimp_image_flush (gdisp->gimage);
}
/* If the segment array is allocated, free it */
if (fuzzy_sel->segs)
{
g_free (fuzzy_sel->segs);
fuzzy_sel->segs = NULL;
fuzzy_sel->num_segs = 0;
}
}
static void
gimp_fuzzy_select_tool_motion (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp)
{
GimpFuzzySelectTool *fuzzy_sel;
GimpSelectionTool *sel_tool;
SelectionOptions *sel_options;
GdkSegment *new_segs;
gint num_new_segs;
gint diff_x, diff_y;
gdouble diff;
static guint32 last_time = 0;
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (tool);
sel_tool = GIMP_SELECTION_TOOL (tool);
sel_options = (SelectionOptions *) tool->tool_info->tool_options;
if (!gimp_tool_control_is_active (tool->control))
return;
/* don't let the events come in too fast, ignore below a delay of 100 ms */
if (ABS (time - last_time) < 100)
return;
last_time = time;
diff_x = coords->x - fuzzy_sel->first_x;
diff_y = coords->y - fuzzy_sel->first_y;
diff = ((ABS (diff_x) > ABS (diff_y)) ? diff_x : diff_y) / 2.0;
gtk_adjustment_set_value (GTK_ADJUSTMENT (sel_options->threshold_w),
fuzzy_sel->first_threshold + diff);
/* calculate the new fuzzy boundary */
new_segs = gimp_fuzzy_select_tool_calculate (fuzzy_sel, gdisp, &num_new_segs);
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
/* make sure the XSegment array is freed before we assign the new one */
if (fuzzy_sel->segs)
g_free (fuzzy_sel->segs);
fuzzy_sel->segs = new_segs;
fuzzy_sel->num_segs = num_new_segs;
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static void
gimp_fuzzy_select_tool_draw (GimpDrawTool *draw_tool)
{
GimpFuzzySelectTool *fuzzy_sel;
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (draw_tool);
if (fuzzy_sel->segs)
gdk_draw_segments (draw_tool->win,
draw_tool->gc,
fuzzy_sel->segs,
fuzzy_sel->num_segs);
}
static GdkSegment *
gimp_fuzzy_select_tool_calculate (GimpFuzzySelectTool *fuzzy_sel,
GimpDisplay *gdisp,
gint *num_segs)
{
GimpTool *tool;
GimpDisplayShell *shell;
SelectionOptions *sel_options;
PixelRegion maskPR;
GimpChannel *new;
GdkSegment *segs;
BoundSeg *bsegs;
GimpDrawable *drawable;
gint i;
gint x, y;
tool = GIMP_TOOL (fuzzy_sel);
shell = GIMP_DISPLAY_SHELL (gdisp->shell);
sel_options = (SelectionOptions *) tool->tool_info->tool_options;
drawable = gimp_image_active_drawable (gdisp->gimage);
gimp_display_shell_set_override_cursor (shell, GDK_WATCH);
x = fuzzy_sel->x;
y = fuzzy_sel->y;
if (! sel_options->sample_merged)
{
gint off_x, off_y;
gimp_drawable_offsets (drawable, &off_x, &off_y);
x -= off_x;
y -= off_y;
}
new = gimp_image_contiguous_region_by_seed (gdisp->gimage, drawable,
sel_options->sample_merged,
sel_options->antialias,
sel_options->threshold,
sel_options->select_transparent,
x, y);
if (fuzzy_sel->fuzzy_mask)
g_object_unref (G_OBJECT (fuzzy_sel->fuzzy_mask));
fuzzy_sel->fuzzy_mask = new;
/* calculate and allocate a new XSegment array which represents the boundary
* of the color-contiguous region
*/
pixel_region_init (&maskPR,
gimp_drawable_data (GIMP_DRAWABLE (fuzzy_sel->fuzzy_mask)),
0, 0,
gimp_drawable_width (GIMP_DRAWABLE (fuzzy_sel->fuzzy_mask)),
gimp_drawable_height (GIMP_DRAWABLE (fuzzy_sel->fuzzy_mask)),
FALSE);
bsegs = find_mask_boundary (&maskPR, num_segs, WithinBounds,
0, 0,
gimp_drawable_width (GIMP_DRAWABLE (fuzzy_sel->fuzzy_mask)),
gimp_drawable_height (GIMP_DRAWABLE (fuzzy_sel->fuzzy_mask)));
segs = g_new (GdkSegment, *num_segs);
for (i = 0; i < *num_segs; i++)
{
gimp_display_shell_transform_xy (shell,
bsegs[i].x1, bsegs[i].y1,
&x, &y,
! sel_options->sample_merged);
segs[i].x1 = x;
segs[i].y1 = y;
gimp_display_shell_transform_xy (shell,
bsegs[i].x2, bsegs[i].y2,
&x, &y,
! sel_options->sample_merged);
segs[i].x2 = x;
segs[i].y2 = y;
}
g_free (bsegs);
gimp_display_shell_unset_override_cursor (shell);
return segs;
}