From 5e2f39b645004aca051d511b2f93afeefe9bf59e Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 26 Oct 2007 20:58:08 +0000 Subject: [PATCH] Use new helper functions to decide if the cursor is on a handle (or 2007-10-26 Martin Nordholts * app/tools/gimprectangletool.c (gimp_rectangle_tool_oper_update): Use new helper functions to decide if the cursor is on a handle (or outside of the rectangle + handle bounds) to ease the introduction of having handles on the outside of the pending rectangle. (gimp_rectangle_tool_coord_outside) (gimp_rectangle_tool_coord_on_handle): New helper functions. svn path=/trunk/; revision=23959 --- ChangeLog | 10 ++ app/tools/gimprectangletool.c | 320 +++++++++++++++++++++------------- 2 files changed, 207 insertions(+), 123 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8c779f837e..7270270f2d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2007-10-26 Martin Nordholts + + * app/tools/gimprectangletool.c (gimp_rectangle_tool_oper_update): + Use new helper functions to decide if the cursor is on a + handle (or outside of the rectangle + handle bounds) to ease the + introduction of having handles on the outside of the pending + rectangle. + (gimp_rectangle_tool_coord_outside) + (gimp_rectangle_tool_coord_on_handle): New helper functions. + 2007-10-26 Sven Neumann * app/pdb/gimpprocedure.c (gimp_procedure_validate_args): don't pass diff --git a/app/tools/gimprectangletool.c b/app/tools/gimprectangletool.c index 1f39194ba7..74bfb6c2fe 100644 --- a/app/tools/gimprectangletool.c +++ b/app/tools/gimprectangletool.c @@ -190,6 +190,12 @@ static void gimp_rectangle_tool_rectangle_changed (GimpRectangleTool static void gimp_rectangle_tool_auto_shrink (GimpRectangleTool *rectangle); +static gboolean gimp_rectangle_tool_coord_outside (GimpRectangleTool *rectangle_tool, + GimpCoords *coords); + +static gboolean gimp_rectangle_tool_coord_on_handle (GimpRectangleTool *rectangle_tool, + GimpCoords *coords, + GtkAnchorType anchor); static GtkAnchorType gimp_rectangle_tool_get_anchor (GimpRectangleToolPrivate *private); @@ -1432,13 +1438,13 @@ gimp_rectangle_tool_oper_update (GimpTool *tool, GimpDisplay *display) { GimpRectangleToolPrivate *private; - GimpDrawTool *draw_tool; + GimpRectangleTool *rectangle_tool; gint function; g_return_if_fail (GIMP_IS_RECTANGLE_TOOL (tool)); - private = GIMP_RECTANGLE_TOOL_GET_PRIVATE (tool); - draw_tool = GIMP_DRAW_TOOL (tool); + private = GIMP_RECTANGLE_TOOL_GET_PRIVATE (tool); + rectangle_tool = GIMP_RECTANGLE_TOOL (tool); if (tool->display != display) return; @@ -1449,130 +1455,70 @@ gimp_rectangle_tool_oper_update (GimpTool *tool, return; } - if (coords->x > private->x1 && coords->x < private->x2 && - coords->y > private->y1 && coords->y < private->y2) + if (gimp_rectangle_tool_coord_outside (rectangle_tool, coords)) { - GimpDisplayShell *shell = GIMP_DISPLAY_SHELL (tool->display->shell); - gint w = private->x2 - private->x1; - gint h = private->y2 - private->y1; - gint tw = w * shell->scale_x; - gint th = h * shell->scale_y; - - if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x1, private->y1, - private->handle_w, - private->handle_h, - GTK_ANCHOR_NORTH_WEST, - FALSE)) - { - function = RECT_RESIZING_UPPER_LEFT; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x2, private->y2, - private->handle_w, - private->handle_h, - GTK_ANCHOR_SOUTH_EAST, - FALSE)) - { - function = RECT_RESIZING_LOWER_RIGHT; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x2, private->y1, - private->handle_w, - private->handle_h, - GTK_ANCHOR_NORTH_EAST, - FALSE)) - { - function = RECT_RESIZING_UPPER_RIGHT; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x1, private->y2, - private->handle_w, - private->handle_h, - GTK_ANCHOR_SOUTH_WEST, - FALSE)) - { - function = RECT_RESIZING_LOWER_LEFT; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x1, private->y1 + h / 2, - private->handle_w, - private->left_and_right_handle_h, - GTK_ANCHOR_WEST, - FALSE)) - { - function = RECT_RESIZING_LEFT; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x2, private->y1 + h / 2, - private->handle_w, - private->left_and_right_handle_h, - GTK_ANCHOR_EAST, - FALSE)) - { - function = RECT_RESIZING_RIGHT; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x1 + w / 2, private->y1, - private->top_and_bottom_handle_w, - private->handle_h, - GTK_ANCHOR_NORTH, - FALSE)) - { - function = RECT_RESIZING_TOP; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x1 + w / 2, private->y2, - private->top_and_bottom_handle_w, - private->handle_h, - GTK_ANCHOR_SOUTH, - FALSE)) - { - function = RECT_RESIZING_BOTTOM; - } - else if (gimp_draw_tool_on_handle (draw_tool, display, - coords->x, coords->y, - GIMP_HANDLE_SQUARE, - private->x1 + w / 2, - private->y1 + h / 2, - tw - private->handle_w * 2, - th - private->handle_h * 2, - GTK_ANCHOR_CENTER, - FALSE)) - { - function = RECT_MOVING; - } - else - { - /* FIXME: This is currently the only measure done to make - * this area dead. In the final code the concrete rectangle - * tools will have to be written to handle this state. - */ - function = RECT_DEAD; - } + /* The cursor is outside of the rectangle, clicking should + * create a new rectangle. + */ + function = RECT_CREATING; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_NORTH_WEST)) + { + function = RECT_RESIZING_UPPER_LEFT; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_SOUTH_EAST)) + { + function = RECT_RESIZING_LOWER_RIGHT; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_NORTH_EAST)) + { + function = RECT_RESIZING_UPPER_RIGHT; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_SOUTH_WEST)) + { + function = RECT_RESIZING_LOWER_LEFT; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_WEST)) + { + function = RECT_RESIZING_LEFT; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_EAST)) + { + function = RECT_RESIZING_RIGHT; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_NORTH)) + { + function = RECT_RESIZING_TOP; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_SOUTH)) + { + function = RECT_RESIZING_BOTTOM; + } + else if (gimp_rectangle_tool_coord_on_handle (rectangle_tool, + coords, + GTK_ANCHOR_CENTER)) + { + function = RECT_MOVING; } else { - /* otherwise, the new function will be creating, since we want - * to start a new rectangle - */ - function = RECT_CREATING; + function = RECT_DEAD; } gimp_rectangle_tool_set_function (GIMP_RECTANGLE_TOOL (tool), function); @@ -2465,6 +2411,134 @@ gimp_rectangle_tool_auto_shrink (GimpRectangleTool *rectangle) gimp_rectangle_tool_update_options (rectangle, tool->display); } +/** + * gimp_rectangle_tool_coord_outside: + * + * Returns: %TRUE if the coord is outside the rectange bounds + * including any outside handles. + */ +static gboolean +gimp_rectangle_tool_coord_outside (GimpRectangleTool *rectangle_tool, + GimpCoords *coord) +{ + GimpRectangleToolPrivate *private; + + private = GIMP_RECTANGLE_TOOL_GET_PRIVATE (rectangle_tool); + + return coord->x < private->x1 || + coord->x > private->x2 || + coord->y < private->y1 || + coord->y > private->y2; +} + +/** + * gimp_rectangle_tool_coord_on_handle: + * + * Returns: %TRUE if the coord is on the handle that corresponds to + * @anchor. + */ +static gboolean +gimp_rectangle_tool_coord_on_handle (GimpRectangleTool *rectangle_tool, + GimpCoords *coords, + GtkAnchorType anchor) +{ + GimpRectangleToolPrivate *private; + GimpTool *tool; + GimpDisplayShell *shell; + GimpDrawTool *draw_tool; + gint w; + gint h; + gint tw; + gint th; + gint handle_x; + gint handle_y; + gint handle_width; + gint handle_height; + + private = GIMP_RECTANGLE_TOOL_GET_PRIVATE (rectangle_tool); + tool = GIMP_TOOL (rectangle_tool); + shell = GIMP_DISPLAY_SHELL (tool->display->shell); + draw_tool = GIMP_DRAW_TOOL (rectangle_tool); + w = private->x2 - private->x1; + h = private->y2 - private->y1; + tw = w * shell->scale_x; + th = h * shell->scale_y; + + switch (anchor) + { + case GTK_ANCHOR_NORTH_WEST: + handle_x = private->x1; + handle_y = private->y1; + handle_width = private->handle_w; + handle_height = private->handle_h; + break; + + case GTK_ANCHOR_SOUTH_EAST: + handle_x = private->x2; + handle_y = private->y2; + handle_width = private->handle_w; + handle_height = private->handle_h; + break; + + case GTK_ANCHOR_NORTH_EAST: + handle_x = private->x2; + handle_y = private->y1; + handle_width = private->handle_w; + handle_height = private->handle_h; + break; + + case GTK_ANCHOR_SOUTH_WEST: + handle_x = private->x1; + handle_y = private->y2; + handle_width = private->handle_w; + handle_height = private->handle_h; + break; + + case GTK_ANCHOR_WEST: + handle_x = private->x1; + handle_y = private->y1 + h / 2; + handle_width = private->handle_w; + handle_height = private->left_and_right_handle_h; + break; + + case GTK_ANCHOR_EAST: + handle_x = private->x2; + handle_y = private->y1 + h / 2; + handle_width = private->handle_w; + handle_height = private->left_and_right_handle_h; + break; + + case GTK_ANCHOR_NORTH: + handle_x = private->x1 + w / 2; + handle_y = private->y1; + handle_width = private->top_and_bottom_handle_w; + handle_height = private->handle_h; + break; + + case GTK_ANCHOR_SOUTH: + handle_x = private->x1 + w / 2; + handle_y = private->y2; + handle_width = private->top_and_bottom_handle_w; + handle_height = private->handle_h; + break; + + case GTK_ANCHOR_CENTER: + handle_x = private->x1 + w / 2; + handle_y = private->y1 + h / 2; + handle_width = tw - private->handle_w * 2; + handle_height = th - private->handle_h * 2; + break; + } + + return gimp_draw_tool_on_handle (draw_tool, shell->display, + coords->x, coords->y, + GIMP_HANDLE_SQUARE, + handle_x, handle_y, + handle_width, handle_height, + anchor, + FALSE); +} + static GtkAnchorType gimp_rectangle_tool_get_anchor (GimpRectangleToolPrivate *private) {