gimp/app/tools/gimpblendtool.c
Michael Natterer 18d9161eea Get rid of the "current_context" which was in fact just a bunch of global
2004-04-15  Michael Natterer  <mitch@gimp.org>

	Get rid of the "current_context" which was in fact just a bunch of
	global variables. Instead, pass the needed context all the way
	from the GUI and the PDB to the core. This is a prerequisite for
	macro recording and generally helps separating the various
	subsystems from each other. Work in progress...

	* app/core/gimp.[ch]: removed member "current_context" and
	gimp_[get|set]_current_context().

	* app/core/gimp-edit.[ch]
	* app/core/gimpdrawable-blend.[ch]
	* app/core/gimpdrawable-bucket-fill.[ch]
	* app/core/gimpdrawable-offset.[ch]
	* app/core/gimpdrawable-transform.[ch]
	* app/core/gimpimage-crop.[ch]
	* app/core/gimpimage-flip.[ch]
	* app/core/gimpimage-merge.[ch]
	* app/core/gimpimage-resize.[ch]
	* app/core/gimpimage-rotate.[ch]
	* app/core/gimpimage.[ch]
	* app/core/gimpimagefile.[ch]
	* app/core/gimpitem-linked.[ch]
	* app/core/gimpitem.[ch]
	* app/core/gimplayer.[ch]
	* app/core/gimpselection.[ch]
	* app/core/gimptemplate.[ch]
	* app/file/file-open.[ch]
	* app/file/file-save.[ch]
	* app/pdb/procedural_db.[ch]
	* app/text/gimptext-compat.[ch]
	* app/text/gimptextlayer-transform.[ch]
	* app/gui/brush-select.[ch]
	* app/gui/font-select.[ch]
	* app/gui/gradient-select.[ch]
	* app/gui/palette-select.[ch]
	* app/gui/pattern-select.[ch]: added tons of "GimpContext *context"
	parameters and use the passed context instead of
	gimp_get_current_context().

	* app/app_procs.c
	* app/batch.c
	* app/core/gimpchannel.c
	* app/core/gimpdrawable.c
	* app/paint/gimperaser.c
	* app/paint/gimppaintbrush.c
	* app/plug-in/plug-in-message.c
	* app/plug-in/plug-ins.c
	* app/text/gimptextlayer.c
	* app/tools/gimpblendtool.c
	* app/tools/gimpbucketfilltool.c
	* app/tools/gimpcroptool.c
	* app/tools/gimpeditselectiontool.c
	* app/tools/gimpfliptool.c
	* app/tools/gimpinktool.c
	* app/tools/gimptransformtool.c
	* app/vectors/gimpvectors.c
	* app/gui/convert-dialog.c
	* app/gui/drawable-commands.c
	* app/gui/edit-commands.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/image-commands.c
	* app/gui/layers-commands.c
	* app/gui/offset-dialog.c
	* app/gui/select-commands.c
	* app/gui/vectors-commands.c
	* app/widgets/gimpdnd.c
	* app/widgets/gimpdocumentview.c
	* app/widgets/gimphelp.c
	* app/widgets/gimpthumbbox.c: pass gimp_get_user_context() or
	GIMP_CONTEXT(tool_options) or whatever is the right context
	to the changed core functions.

	* tools/pdbgen/app.pl: pass "GimpContext *context" to all
	generated PDB invokers.

	* tools/pdbgen/pdb/brush_select.pdb
	* tools/pdbgen/pdb/brushes.pdb
	* tools/pdbgen/pdb/drawable.pdb
	* tools/pdbgen/pdb/edit.pdb
	* tools/pdbgen/pdb/font_select.pdb
	* tools/pdbgen/pdb/gradient_select.pdb
	* tools/pdbgen/pdb/gradients.pdb
	* tools/pdbgen/pdb/image.pdb
	* tools/pdbgen/pdb/layer.pdb
	* tools/pdbgen/pdb/paint_tools.pdb
	* tools/pdbgen/pdb/palette.pdb
	* tools/pdbgen/pdb/palette_select.pdb
	* tools/pdbgen/pdb/palettes.pdb
	* tools/pdbgen/pdb/paths.pdb
	* tools/pdbgen/pdb/pattern_select.pdb
	* tools/pdbgen/pdb/patterns.pdb
	* tools/pdbgen/pdb/selection.pdb
	* tools/pdbgen/pdb/text_tool.pdb
	* tools/pdbgen/pdb/transform_tools.pdb: pass the new context
	parameter to the changed core functions.

	* app/pdb/*_cmds.c: regenerated.
2004-04-14 23:37:34 +00:00

393 lines
12 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 "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "tools-types.h"
#include "core/gimp.h"
#include "core/gimpdrawable.h"
#include "core/gimpdrawable-blend.h"
#include "core/gimpgradient.h"
#include "core/gimpimage.h"
#include "core/gimptoolinfo.h"
#include "widgets/gimphelp-ids.h"
#include "display/gimpdisplay.h"
#include "display/gimpprogress.h"
#include "gimpblendoptions.h"
#include "gimpblendtool.h"
#include "gimptoolcontrol.h"
#include "gimp-intl.h"
#define TARGET_SIZE 15
/* local function prototypes */
static void gimp_blend_tool_class_init (GimpBlendToolClass *klass);
static void gimp_blend_tool_init (GimpBlendTool *blend_tool);
static void gimp_blend_tool_button_press (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_blend_tool_button_release (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_blend_tool_motion (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_blend_tool_cursor_update (GimpTool *tool,
GimpCoords *coords,
GdkModifierType state,
GimpDisplay *gdisp);
static void gimp_blend_tool_draw (GimpDrawTool *draw_tool);
/* private variables */
static GimpDrawToolClass *parent_class = NULL;
/* public functions */
void
gimp_blend_tool_register (GimpToolRegisterCallback callback,
gpointer data)
{
(* callback) (GIMP_TYPE_BLEND_TOOL,
GIMP_TYPE_BLEND_OPTIONS,
gimp_blend_options_gui,
GIMP_CONTEXT_OPACITY_MASK |
GIMP_CONTEXT_PAINT_MODE_MASK |
GIMP_CONTEXT_GRADIENT_MASK,
"gimp-blend-tool",
_("Blend"),
_("Fill with a color gradient"),
N_("/Tools/Paint Tools/Blen_d"), "L",
NULL, GIMP_HELP_TOOL_BLEND,
GIMP_STOCK_TOOL_BLEND,
data);
}
GType
gimp_blend_tool_get_type (void)
{
static GType tool_type = 0;
if (! tool_type)
{
static const GTypeInfo tool_info =
{
sizeof (GimpBlendToolClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gimp_blend_tool_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GimpBlendTool),
0, /* n_preallocs */
(GInstanceInitFunc) gimp_blend_tool_init,
};
tool_type = g_type_register_static (GIMP_TYPE_DRAW_TOOL,
"GimpBlendTool",
&tool_info, 0);
}
return tool_type;
}
/* private functions */
static void
gimp_blend_tool_class_init (GimpBlendToolClass *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);
tool_class->button_press = gimp_blend_tool_button_press;
tool_class->button_release = gimp_blend_tool_button_release;
tool_class->motion = gimp_blend_tool_motion;
tool_class->cursor_update = gimp_blend_tool_cursor_update;
draw_tool_class->draw = gimp_blend_tool_draw;
}
static void
gimp_blend_tool_init (GimpBlendTool *blend_tool)
{
GimpTool *tool;
tool = GIMP_TOOL (blend_tool);
gimp_tool_control_set_scroll_lock (tool->control, TRUE);
gimp_tool_control_set_tool_cursor (tool->control, GIMP_BLEND_TOOL_CURSOR);
}
static void
gimp_blend_tool_button_press (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp)
{
GimpBlendTool *blend_tool;
GimpDrawable *drawable;
gint off_x, off_y;
blend_tool = GIMP_BLEND_TOOL (tool);
drawable = gimp_image_active_drawable (gdisp->gimage);
switch (gimp_drawable_type (drawable))
{
case GIMP_INDEXED_IMAGE: case GIMP_INDEXEDA_IMAGE:
g_message (_("Blend: Invalid for indexed images."));
return;
break;
default:
break;
}
gimp_item_offsets (GIMP_ITEM (drawable), &off_x, &off_y);
blend_tool->endx = blend_tool->startx = coords->x - off_x;
blend_tool->endy = blend_tool->starty = coords->y - off_y;
tool->gdisp = gdisp;
gimp_tool_control_activate (tool->control);
/* initialize the statusbar display */
gimp_tool_push_status_coords (tool, _("Blend: "), 0, ", ", 0);
/* Start drawing the blend tool */
gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), gdisp);
}
static void
gimp_blend_tool_button_release (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp)
{
GimpBlendTool *blend_tool;
GimpPaintOptions *paint_options;
GimpBlendOptions *options;
GimpContext *context;
GimpImage *gimage;
GimpProgress *progress;
blend_tool = GIMP_BLEND_TOOL (tool);
paint_options = GIMP_PAINT_OPTIONS (tool->tool_info->tool_options);
options = GIMP_BLEND_OPTIONS (paint_options);
context = GIMP_CONTEXT (options);
gimage = gdisp->gimage;
gimp_tool_pop_status (tool);
gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
gimp_tool_control_halt (tool->control);
/* if the 3rd button isn't pressed, fill the selected region */
if (! (state & GDK_BUTTON3_MASK) &&
((blend_tool->startx != blend_tool->endx) ||
(blend_tool->starty != blend_tool->endy)))
{
progress = gimp_progress_start (gdisp, _("Blending..."), FALSE,
NULL, NULL);
gimp_drawable_blend (gimp_image_active_drawable (gimage),
context,
GIMP_CUSTOM_MODE,
gimp_context_get_paint_mode (context),
options->gradient_type,
gimp_context_get_opacity (context),
options->offset,
paint_options->gradient_options->gradient_repeat,
paint_options->gradient_options->gradient_reverse,
options->supersample,
options->supersample_depth,
options->supersample_threshold,
options->dither,
blend_tool->startx,
blend_tool->starty,
blend_tool->endx,
blend_tool->endy,
progress ? gimp_progress_update_and_flush : NULL,
progress);
if (progress)
gimp_progress_end (progress);
gimp_image_flush (gimage);
}
}
static void
gimp_blend_tool_motion (GimpTool *tool,
GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *gdisp)
{
GimpBlendTool *blend_tool;
gint off_x, off_y;
blend_tool = GIMP_BLEND_TOOL (tool);
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (gdisp->gimage)),
&off_x, &off_y);
/* Get the current coordinates */
blend_tool->endx = coords->x - off_x;
blend_tool->endy = coords->y - off_y;
/* Restrict to multiples of 15 degrees if ctrl is pressed */
if (state & GDK_CONTROL_MASK)
{
gint tangens2[6] = { 34, 106, 196, 334, 618, 1944 };
gint cosinus[7] = { 256, 247, 222, 181, 128, 66, 0 };
gint dx, dy, i, radius, frac;
dx = blend_tool->endx - blend_tool->startx;
dy = blend_tool->endy - blend_tool->starty;
if (dy)
{
radius = sqrt (SQR (dx) + SQR (dy));
frac = abs ((dx << 8) / dy);
for (i = 0; i < 6; i++)
{
if (frac < tangens2[i])
break;
}
dx = dx > 0 ?
(cosinus[6-i] * radius) >> 8 : - ((cosinus[6-i] * radius) >> 8);
dy = dy > 0 ?
(cosinus[i] * radius) >> 8 : - ((cosinus[i] * radius) >> 8);
}
blend_tool->endx = blend_tool->startx + dx;
blend_tool->endy = blend_tool->starty + dy;
}
gimp_tool_pop_status (tool);
gimp_tool_push_status_coords (tool,
_("Blend: "),
blend_tool->endx - blend_tool->startx,
", ",
blend_tool->endy - blend_tool->starty);
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static void
gimp_blend_tool_cursor_update (GimpTool *tool,
GimpCoords *coords,
GdkModifierType state,
GimpDisplay *gdisp)
{
switch (gimp_drawable_type (gimp_image_active_drawable (gdisp->gimage)))
{
case GIMP_INDEXED_IMAGE:
case GIMP_INDEXEDA_IMAGE:
gimp_tool_control_set_cursor (tool->control, GIMP_BAD_CURSOR);
break;
default:
gimp_tool_control_set_cursor (tool->control, GIMP_MOUSE_CURSOR);
break;
}
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, gdisp);
}
static void
gimp_blend_tool_draw (GimpDrawTool *draw_tool)
{
GimpBlendTool *blend_tool;
blend_tool = GIMP_BLEND_TOOL (draw_tool);
/* Draw start target */
gimp_draw_tool_draw_handle (draw_tool,
GIMP_HANDLE_CROSS,
floor (blend_tool->startx) + 0.5,
floor (blend_tool->starty) + 0.5,
TARGET_SIZE,
TARGET_SIZE,
GTK_ANCHOR_CENTER,
TRUE);
/* Draw end target */
gimp_draw_tool_draw_handle (draw_tool,
GIMP_HANDLE_CROSS,
floor (blend_tool->endx) + 0.5,
floor (blend_tool->endy) + 0.5,
TARGET_SIZE,
TARGET_SIZE,
GTK_ANCHOR_CENTER,
TRUE);
/* Draw the line between the start and end coords */
gimp_draw_tool_draw_line (draw_tool,
floor (blend_tool->startx) + 0.5,
floor (blend_tool->starty) + 0.5,
floor (blend_tool->endx) + 0.5,
floor (blend_tool->endy) + 0.5,
TRUE);
}