gimp/app/core/gimp-edit.c
Michael Natterer 5f63e079b8 Chopped up the display stuff (beware: unfinished)...
2001-10-31  Michael Natterer  <mitch@gimp.org>

	Chopped up the display stuff (beware: unfinished)...

	The plan is that GimpDisplay is the object which collects
	updates from the image, compresses them and waits for the GIMP
	to be idle to actually paint them. It should be a non-GUI object
	which is the model for the actual widget to connect to.

	GimpDisplayShell has all the widgets and handles painting and
	exposing of the result. Nobody should actually be required to
	update ot look at it as it should be a view on the GimpDisplay
	object.

	Much stuff is still in the wrong place and the functions don't
	follow their files' filename namespace any more. More to come...

	* app/display/Makefile.am
	* app/display/gimpdisplay-ops.[ch]: removed. It's functions didn't
	belong together anyway.

	* app/display/gimpdisplay-area.[ch]: new files: the GimpArea
	functions.

	* app/display/gimpdisplay-handlers.[ch]: new files: signal
	handlers for GimpImage signals. Mostly from app/gui.c.

	* app/display/gimpdisplay.[ch]: removed all widgets and other
	GUI stuff. There is still much undecided here...

	* app/display/gimpdisplayshell.[ch]: actually use the object and
	filled it with all the stuff from GimpDisplay.

	* app/display/gimpdisplay-callbacks.[ch]
	* app/display/gimpdisplay-foreach.[ch]
	* app/display/gimpdisplay-render.c
	* app/display/gimpdisplay-scale.[ch]
	* app/display/gimpdisplay-scroll.[ch]
	* app/display/gimpdisplay-selection.c: changed accordingly.

	* app/core/gimp.[ch]: return a GimpObject from
	gimp_create_display() so it can be used as single GUI independent
	point to create displays, require the initial scale as parameter.

	* app/core/gimpcontext.c: changed the ugly EEKWrapper according to
	the GimpDisplay structure changes. Bugfix: set the image to NULL
	in gimp_context_display_destroyed().

	* app/core/gimpedit.c
	* app/core/gimpimage-new.c: changed gimp_create_display() calls
	accordingly.

	* app/core/gimpimage-convert.c: invalidate the layer & image
	previews here, not in the caller.

	* app/core/gimpimage-crop.c: update the whole image after cropping.

	* app/core/gimpimage.[ch]: added gimp_image_find_guide(),
	gimp_image_snap_point() and gimp_image_snap_rectangle(). Added
	"resolution_changed" and "unit_changed" signals and corresp.
	public convenience functions to emit them.

	* app/core/gimplayer.c: emit the image's "alpha_changed" signal
	when adding alpha to the bottom (and only) layer of the image.

	* app/gimpprogress.c
	* app/image_map.c
	* app/nav_window.c
	* app/qmask.c
	* app/undo.c
	* app/user_install.c: changed accordingly.

	* app/gui/edit-commands.c
	* app/gui/file-commands.c
	* app/gui/file-open-dialog.c
	* app/gui/image-commands.c
	* app/gui/info-window.c
	* app/gui/preferences-dialog.c
	* app/gui/toolbox.c
	* app/gui/view-commands.c: ditto.

	* app/gui/gui.[ch]: removed most gimp->images handlers as the
	displays connect to them themselves now. chaged gui_display_new()
	according to the gimp_create_display() changes.
	Added gui_get_screen_resolution().

	* app/tools/gimpbezierselecttool.c
	* app/tools/gimpblendtool.c
	* app/tools/gimpbucketfilltool.c
	* app/tools/gimpbycolorselecttool.c
	* app/tools/gimpclonetool.c
	* app/tools/gimpcolorpickertool.c
	* app/tools/gimpcroptool.c
	* app/tools/gimpdrawtool.c
	* app/tools/gimpeditselectiontool.c
	* app/tools/gimpfliptool.c
	* app/tools/gimpfreeselecttool.c
	* app/tools/gimpfuzzyselecttool.c
	* app/tools/gimpinktool.c
	* app/tools/gimpiscissorstool.c
	* app/tools/gimpmagnifytool.c
	* app/tools/gimpmeasuretool.c
	* app/tools/gimpmovetool.c
	* app/tools/gimppainttool.c
	* app/tools/gimppathtool.c
	* app/tools/gimprectselecttool.c
	* app/tools/gimpselectiontool.c
	* app/tools/gimptexttool.c
	* app/tools/gimptool.c
	* app/tools/gimptransformtool.c
	* app/tools/xinput_airbrush.c: lots of changes because GimpDisplay
	has become two objects. Lots of gdisp->shell casting uglyness
	added. This is fine because exactly these parts will have to go
	away.

	(GimpDisplay will provide methods for XOR drawing upon the display
	in image coordinates without the need to transform coordinates all
	the time. Also the tools shouldn't see GdkEvents but get more
	useful virtual functions which speak in image coordinates too).

	* app/widgets/gimpcomponentlistitem.c: removed a now useless image
	update.

	* tools/pdbgen/pdb/display.pdb: use gimp_create_display().

	* app/pdb/display_cmds.c: regenerated.
2001-10-31 21:18:57 +00:00

392 lines
9.9 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 <glib-object.h>
#include "core-types.h"
#include "base/pixel-region.h"
#include "base/tile-manager.h"
#include "base/tile-manager-crop.h"
#include "paint-funcs/paint-funcs.h"
#include "gimp.h"
#include "gimpbuffer.h"
#include "gimpchannel.h"
#include "gimpcontext.h"
#include "gimpedit.h"
#include "gimpimage.h"
#include "gimpimage-mask.h"
#include "gimpimage-new.h"
#include "gimplayer.h"
#include "gimplist.h"
#include "floating_sel.h"
#include "undo.h"
#include "libgimp/gimpintl.h"
TileManager *
gimp_edit_cut (GimpImage *gimage,
GimpDrawable *drawable)
{
TileManager *cut;
TileManager *cropped_cut;
gboolean empty;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
/* Start a group undo */
undo_push_group_start (gimage, EDIT_CUT_UNDO);
/* See if the gimage mask is empty */
empty = gimage_mask_is_empty (gimage);
/* Next, cut the mask portion from the gimage */
cut = gimage_mask_extract (gimage, drawable, TRUE, FALSE, TRUE);
if (cut)
gimp_image_new_set_have_current_cut_buffer (gimage->gimp);
/* Only crop if the gimage mask wasn't empty */
if (cut && ! empty)
{
cropped_cut = tile_manager_crop (cut, 0);
if (cropped_cut != cut)
{
tile_manager_destroy (cut);
cut = NULL;
}
}
else if (cut)
cropped_cut = cut;
else
cropped_cut = NULL;
/* end the group undo */
undo_push_group_end (gimage);
if (cropped_cut)
{
/* Free the old global edit buffer */
if (gimage->gimp->global_buffer)
tile_manager_destroy (gimage->gimp->global_buffer);
/* Set the global edit buffer */
gimage->gimp->global_buffer = cropped_cut;
}
return cropped_cut;
}
TileManager *
gimp_edit_copy (GimpImage *gimage,
GimpDrawable *drawable)
{
TileManager *copy;
TileManager *cropped_copy;
gboolean empty;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
/* See if the gimage mask is empty */
empty = gimage_mask_is_empty (gimage);
/* First, copy the masked portion of the gimage */
copy = gimage_mask_extract (gimage, drawable, FALSE, FALSE, TRUE);
if (copy)
gimp_image_new_set_have_current_cut_buffer (gimage->gimp);
/* Only crop if the gimage mask wasn't empty */
if (copy && ! empty)
{
cropped_copy = tile_manager_crop (copy, 0);
if (cropped_copy != copy)
{
tile_manager_destroy (copy);
copy = NULL;
}
}
else if (copy)
cropped_copy = copy;
else
cropped_copy = NULL;
if (cropped_copy)
{
/* Free the old global edit buffer */
if (gimage->gimp->global_buffer)
tile_manager_destroy (gimage->gimp->global_buffer);
/* Set the global edit buffer */
gimage->gimp->global_buffer = cropped_copy;
}
return cropped_copy;
}
GimpLayer *
gimp_edit_paste (GimpImage *gimage,
GimpDrawable *drawable,
TileManager *paste,
gboolean paste_into)
{
GimpLayer *layer;
gint x1, y1, x2, y2;
gint cx, cy;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (! drawable || GIMP_IS_DRAWABLE (drawable), NULL);
/* Make a new layer: if drawable == NULL,
* user is pasting into an empty image.
*/
if (drawable != NULL)
layer = gimp_layer_new_from_tiles (gimage,
gimp_drawable_type_with_alpha (drawable),
paste,
_("Pasted Layer"),
OPAQUE_OPACITY, NORMAL_MODE);
else
layer = gimp_layer_new_from_tiles (gimage,
gimp_image_base_type_with_alpha (gimage),
paste,
_("Pasted Layer"),
OPAQUE_OPACITY, NORMAL_MODE);
if (! layer)
return NULL;
/* Start a group undo */
undo_push_group_start (gimage, EDIT_PASTE_UNDO);
/* Set the offsets to the center of the image */
if (drawable != NULL)
{
gimp_drawable_offsets (drawable, &cx, &cy);
gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
cx += (x1 + x2) >> 1;
cy += (y1 + y2) >> 1;
}
else
{
cx = gimage->width >> 1;
cy = gimage->height >> 1;
}
GIMP_DRAWABLE (layer)->offset_x = cx - (GIMP_DRAWABLE (layer)->width >> 1);
GIMP_DRAWABLE (layer)->offset_y = cy - (GIMP_DRAWABLE (layer)->height >> 1);
/* If there is a selection mask clear it--
* this might not always be desired, but in general,
* it seems like the correct behavior.
*/
if (! gimage_mask_is_empty (gimage) && ! paste_into)
gimp_channel_clear (gimp_image_get_mask (gimage));
/* if there's a drawable, add a new floating selection */
if (drawable != NULL)
{
floating_sel_attach (layer, drawable);
}
else
{
gimp_drawable_set_gimage (GIMP_DRAWABLE (layer), gimage);
gimp_image_add_layer (gimage, layer, 0);
}
/* end the group undo */
undo_push_group_end (gimage);
return layer;
}
GimpImage *
gimp_edit_paste_as_new (Gimp *gimp,
GimpImage *invoke,
TileManager *paste)
{
GimpImage *gimage;
GimpLayer *layer;
/* create a new image (always of type RGB) */
gimage = gimp_create_image (gimp,
tile_manager_width (paste),
tile_manager_height (paste),
RGB,
TRUE);
gimp_image_undo_disable (gimage);
if (invoke)
{
gimp_image_set_resolution (gimage,
invoke->xresolution, invoke->yresolution);
gimp_image_set_unit (gimage, invoke->unit);
}
layer = gimp_layer_new_from_tiles (gimage,
gimp_image_base_type_with_alpha (gimage),
paste,
_("Pasted Layer"),
OPAQUE_OPACITY, NORMAL_MODE);
if (layer)
{
/* add the new layer to the image */
gimp_drawable_set_gimage (GIMP_DRAWABLE (layer), gimage);
gimp_image_add_layer (gimage, layer, 0);
gimp_image_undo_enable (gimage);
gimp_create_display (gimp, gimage, 0x0101);
g_object_unref (G_OBJECT (gimage));
return gimage;
}
return NULL;
}
gboolean
gimp_edit_clear (GimpImage *gimage,
GimpDrawable *drawable)
{
TileManager *buf_tiles;
PixelRegion bufPR;
gint x1, y1, x2, y2;
guchar col[MAX_CHANNELS];
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
gimp_image_get_background (gimage, drawable, col);
if (gimp_drawable_has_alpha (drawable))
col [gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
if (!(x2 - x1) || !(y2 - y1))
return TRUE; /* nothing to do, but the clear succeded */
buf_tiles = tile_manager_new ((x2 - x1), (y2 - y1),
gimp_drawable_bytes (drawable));
pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), TRUE);
color_region (&bufPR, col);
pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), FALSE);
gimp_image_apply_image (gimage, drawable, &bufPR, TRUE, OPAQUE_OPACITY,
ERASE_MODE, NULL, x1, y1);
/* update the image */
gimp_drawable_update (drawable,
x1, y1,
(x2 - x1), (y2 - y1));
/* free the temporary tiles */
tile_manager_destroy (buf_tiles);
return TRUE;
}
gboolean
gimp_edit_fill (GimpImage *gimage,
GimpDrawable *drawable,
GimpFillType fill_type)
{
TileManager *buf_tiles;
PixelRegion bufPR;
gint x1, y1, x2, y2;
guchar col[MAX_CHANNELS];
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
if (gimp_drawable_has_alpha (drawable))
col [gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
switch (fill_type)
{
case FOREGROUND_FILL:
gimp_image_get_foreground (gimage, drawable, col);
break;
case BACKGROUND_FILL:
gimp_image_get_background (gimage, drawable, col);
break;
case WHITE_FILL:
col[RED_PIX] = 255;
col[GREEN_PIX] = 255;
col[BLUE_PIX] = 255;
break;
case TRANSPARENT_FILL:
col[RED_PIX] = 0;
col[GREEN_PIX] = 0;
col[BLUE_PIX] = 0;
if (gimp_drawable_has_alpha (drawable))
col [gimp_drawable_bytes (drawable) - 1] = TRANSPARENT_OPACITY;
break;
case NO_FILL:
return TRUE; /* nothing to do, but the fill succeded */
default:
g_warning ("%s(): unknown fill type", G_GNUC_FUNCTION);
gimp_image_get_background (gimage, drawable, col);
break;
}
gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
if (!(x2 - x1) || !(y2 - y1))
return TRUE; /* nothing to do, but the fill succeded */
buf_tiles = tile_manager_new ((x2 - x1), (y2 - y1),
gimp_drawable_bytes (drawable));
pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), TRUE);
color_region (&bufPR, col);
pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), FALSE);
gimp_image_apply_image (gimage, drawable, &bufPR, TRUE, OPAQUE_OPACITY,
NORMAL_MODE, NULL, x1, y1);
/* update the image */
gimp_drawable_update (drawable,
x1, y1,
(x2 - x1), (y2 - y1));
/* free the temporary tiles */
tile_manager_destroy (buf_tiles);
return TRUE;
}