Connect to the "image-changed" signal of GimpContext and update default

2007-08-17  Martin Nordholts  <martinn@svn.gnome.org>

	* app/tools/gimpcroptool.c
	(gimp_crop_tool_constructor): Connect to the "image-changed"
	signal of GimpContext and update default aspect when image
	changes (fixes bug #417166), by using
	(gimp_crop_tool_update_default_fixed_ratio_options): which is a
	new function that does this. This function is called whenever it
	is time to update defualts.

	* app/tools/gimprectangletool.[ch]: Made
	gimp_rectangle_tool_get_constraint part of the rectangle tool API.

svn path=/trunk/; revision=23312
This commit is contained in:
Martin Nordholts 2007-08-17 18:29:09 +00:00 committed by Martin Nordholts
parent 3aed85c61d
commit eb06a11ea9
4 changed files with 144 additions and 42 deletions

View File

@ -1,3 +1,16 @@
2007-08-17 Martin Nordholts <martinn@svn.gnome.org>
* app/tools/gimpcroptool.c
(gimp_crop_tool_constructor): Connect to the "image-changed"
signal of GimpContext and update default aspect when image
changes (fixes bug #417166), by using
(gimp_crop_tool_update_default_fixed_ratio_options): which is a
new function that does this. This function is called whenever it
is time to update defualts.
* app/tools/gimprectangletool.[ch]: Made
gimp_rectangle_tool_get_constraint part of the rectangle tool API.
2007-08-17 Michael Natterer <mitch@gimp.org>
* plug-ins/print/print-page-layout.c: set a default page setup on

View File

@ -24,8 +24,11 @@
#include "tools-types.h"
#include "core/gimp.h"
#include "core/gimpimage.h"
#include "core/gimpimage-crop.h"
#include "core/gimpitem.h"
#include "core/gimptoolinfo.h"
#include "widgets/gimphelp-ids.h"
@ -75,10 +78,17 @@ static gboolean gimp_crop_tool_execute (GimpRectangleTool *rectang
gint w,
gint h);
static void gimp_crop_tool_update_default_fixed_ratio_options
(GimpCropTool *crop_tool);
static void gimp_crop_tool_notify_layer_only (GimpCropOptions *options,
GParamSpec *pspec,
GimpTool *tool);
static void gimp_crop_tool_image_changed (GimpContext *gimp_context,
GimpImage *image,
GimpCropTool *crop_tool);
G_DEFINE_TYPE_WITH_CODE (GimpCropTool, gimp_crop_tool, GIMP_TYPE_DRAW_TOOL,
G_IMPLEMENT_INTERFACE (GIMP_TYPE_RECTANGLE_TOOL,
@ -153,11 +163,25 @@ gimp_crop_tool_constructor (GType type,
{
GObject *object;
GimpCropOptions *options;
GimpContext *gimp_context;
GimpToolInfo *tool_info;
object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
gimp_rectangle_tool_constructor (object);
g_object_get (object,
"tool-info", &tool_info,
NULL);
gimp_context = gimp_get_user_context (tool_info->gimp);
g_signal_connect_object (gimp_context, "image-changed",
G_CALLBACK (gimp_crop_tool_image_changed),
GIMP_CROP_TOOL (object), 0);
options = GIMP_CROP_TOOL_GET_OPTIONS (object);
g_signal_connect_object (options, "notify::layer-only",
@ -169,6 +193,8 @@ gimp_crop_tool_constructor (GType type,
GIMP_RECTANGLE_CONSTRAIN_DRAWABLE :
GIMP_RECTANGLE_CONSTRAIN_IMAGE);
gimp_crop_tool_update_default_fixed_ratio_options (GIMP_CROP_TOOL (object));
return object;
}
@ -203,42 +229,9 @@ gimp_crop_tool_button_release (GimpTool *tool,
GimpButtonReleaseType release_type,
GimpDisplay *display)
{
GimpRectangleOptions *options = GIMP_RECTANGLE_TOOL_GET_OPTIONS (tool);
gimp_tool_push_status (tool, display, _("Click or press Enter to crop"));
if (tool->display != NULL)
{
gint crop_rectangle_width;
gint crop_rectangle_height;
/* There exists a pending crop rectangle, set default aspect ratio to the
* same aspect ratio of the rectangle.
*/
gimp_rectangle_tool_get_rectangle_size (GIMP_RECTANGLE_TOOL (tool),
&crop_rectangle_width,
&crop_rectangle_height);
if (crop_rectangle_width < 1.0)
crop_rectangle_width = 1.0;
if (crop_rectangle_height < 1.0)
crop_rectangle_height = 1.0;
g_object_set (options,
"default-aspect-numerator", (double) crop_rectangle_width,
"default-aspect-denominator", (double) crop_rectangle_height,
NULL);
}
else
{
/* There exist no rectangle, set default to 1:1 */
/* TODO: Should be canvas/layer width/height when bug #417166 is fixed. */
g_object_set (options,
"default-aspect-numerator", 1.0,
"default-aspect-denominator", 1.0,
NULL);
}
gimp_crop_tool_update_default_fixed_ratio_options (GIMP_CROP_TOOL (tool));
gimp_rectangle_tool_button_release (tool, coords, time, state, release_type,
display);
@ -324,11 +317,7 @@ gimp_crop_tool_execute (GimpRectangleTool *rectangle,
gimp_image_flush (image);
/* TODO: Should be canvas/layer width/height when bug #417166 is fixed. */
g_object_set (rectangle_options,
"default-aspect-numerator", 1.0,
"default-aspect-denominator", 1.0,
NULL);
gimp_crop_tool_update_default_fixed_ratio_options (GIMP_CROP_TOOL (tool));
return TRUE;
}
@ -336,6 +325,97 @@ gimp_crop_tool_execute (GimpRectangleTool *rectangle,
return TRUE;
}
/**
* gimp_crop_tool_update_default_fixed_ratio_options:
* @crop_tool:
*
* Sets the default aspect numerator/denominator to that of the current
* layer/image/pending crop rectangle.
*/
static void gimp_crop_tool_update_default_fixed_ratio_options (GimpCropTool *crop_tool)
{
GimpTool *tool;
GimpRectangleTool *rectangle_tool;
GimpRectangleOptions *rectangle_options;
gdouble default_numerator;
gdouble default_denominator;
tool = GIMP_TOOL (crop_tool);
rectangle_tool = GIMP_RECTANGLE_TOOL (tool);
rectangle_options = GIMP_RECTANGLE_TOOL_GET_OPTIONS (rectangle_tool);
if (tool->display != NULL)
{
gint crop_rectangle_width;
gint crop_rectangle_height;
/* There exists a pending crop rectangle, set default aspect ratio to the
* same aspect ratio of the rectangle.
*/
gimp_rectangle_tool_get_rectangle_size (rectangle_tool,
&crop_rectangle_width,
&crop_rectangle_height);
default_numerator = MAX (crop_rectangle_width, 1.0);
default_denominator = MAX (crop_rectangle_height, 1.0);
}
else
{
GimpRectangleConstraint constraint;
GimpContext *gimp_context;
GimpImage *image;
/* There exist no rectangle, set defaults */
gimp_context = gimp_get_user_context (tool->tool_info->gimp);
image = gimp_context_get_image (gimp_context);
if (image == NULL)
{
default_numerator = 1.0;
default_denominator = 1.0;
}
else
{
constraint = gimp_rectangle_tool_get_constraint (rectangle_tool);
switch (constraint)
{
case GIMP_RECTANGLE_CONSTRAIN_DRAWABLE:
{
GimpItem *item = GIMP_ITEM (gimp_image_get_active_layer (image));
if (item == NULL)
{
default_numerator = 1.0;
default_denominator = 1.0;
}
else
{
default_numerator = gimp_item_width (item);
default_denominator = gimp_item_height (item);
}
}
break;
case GIMP_RECTANGLE_CONSTRAIN_IMAGE:
default:
{
default_numerator = image->width;
default_denominator = image->height;
}
break;
}
}
}
g_object_set (rectangle_options,
"default-aspect-numerator", default_numerator,
"default-aspect-denominator", default_denominator,
NULL);
}
static void
gimp_crop_tool_notify_layer_only (GimpCropOptions *options,
GParamSpec *pspec,
@ -345,4 +425,14 @@ gimp_crop_tool_notify_layer_only (GimpCropOptions *options,
options->layer_only ?
GIMP_RECTANGLE_CONSTRAIN_DRAWABLE :
GIMP_RECTANGLE_CONSTRAIN_IMAGE);
gimp_crop_tool_update_default_fixed_ratio_options (GIMP_CROP_TOOL (tool));
}
static void
gimp_crop_tool_image_changed (GimpContext *gimp_context,
GimpImage *image,
GimpCropTool *crop_tool)
{
gimp_crop_tool_update_default_fixed_ratio_options (GIMP_CROP_TOOL (crop_tool));
}

View File

@ -167,9 +167,6 @@ static void gimp_rectangle_tool_iface_base_init (GimpRectangleToolInterface
static GimpRectangleToolPrivate *
gimp_rectangle_tool_get_private (GimpRectangleTool *rectangle);
GimpRectangleConstraint
gimp_rectangle_tool_get_constraint (GimpRectangleTool *rectangle);
/* Rectangle helper functions */
static void gimp_rectangle_tool_start (GimpRectangleTool *rectangle,
GimpDisplay *display);

View File

@ -124,6 +124,8 @@ gboolean gimp_rectangle_tool_execute (GimpRectangleTool *re
void gimp_rectangle_tool_cancel (GimpRectangleTool *rect_tool);
void gimp_rectangle_tool_set_constraint (GimpRectangleTool *rectangle,
GimpRectangleConstraint constraint);
GimpRectangleConstraint gimp_rectangle_tool_get_constraint
(GimpRectangleTool *rectangle);
GimpRectangleFunction gimp_rectangle_tool_get_function (GimpRectangleTool *rectangle);
void gimp_rectangle_tool_set_function (GimpRectangleTool *rectangle,
GimpRectangleFunction function);