1158 lines
35 KiB
C
1158 lines
35 KiB
C
/* GIMP - The GNU Image Manipulation Program
|
|
* Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
|
|
*
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <gegl.h>
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
#include "tools-types.h"
|
|
|
|
#include "core/gimpimage.h"
|
|
|
|
#include "vectors/gimpanchor.h"
|
|
#include "vectors/gimpstroke.h"
|
|
#include "vectors/gimpvectors.h"
|
|
|
|
#include "display/gimpcanvas.h"
|
|
#include "display/gimpcanvasarc.h"
|
|
#include "display/gimpcanvasboundary.h"
|
|
#include "display/gimpcanvascorner.h"
|
|
#include "display/gimpcanvasguide.h"
|
|
#include "display/gimpcanvashandle.h"
|
|
#include "display/gimpcanvasline.h"
|
|
#include "display/gimpcanvaspolygon.h"
|
|
#include "display/gimpcanvasrectangle.h"
|
|
#include "display/gimpcanvastextcursor.h"
|
|
#include "display/gimpdisplay.h"
|
|
#include "display/gimpdisplayshell.h"
|
|
#include "display/gimpdisplayshell-transform.h"
|
|
|
|
#include "gimpdrawtool.h"
|
|
|
|
|
|
static void gimp_draw_tool_dispose (GObject *object);
|
|
|
|
static gboolean gimp_draw_tool_has_display (GimpTool *tool,
|
|
GimpDisplay *display);
|
|
static GimpDisplay * gimp_draw_tool_has_image (GimpTool *tool,
|
|
GimpImage *image);
|
|
static void gimp_draw_tool_control (GimpTool *tool,
|
|
GimpToolAction action,
|
|
GimpDisplay *display);
|
|
|
|
static void gimp_draw_tool_draw (GimpDrawTool *draw_tool);
|
|
static void gimp_draw_tool_undraw (GimpDrawTool *draw_tool);
|
|
static void gimp_draw_tool_real_draw (GimpDrawTool *draw_tool);
|
|
|
|
static inline void gimp_draw_tool_shift_to_north_west
|
|
(gdouble x,
|
|
gdouble y,
|
|
gint handle_width,
|
|
gint handle_height,
|
|
GtkAnchorType anchor,
|
|
gdouble *shifted_x,
|
|
gdouble *shifted_y);
|
|
static inline void gimp_draw_tool_shift_to_center
|
|
(gdouble x,
|
|
gdouble y,
|
|
gint handle_width,
|
|
gint handle_height,
|
|
GtkAnchorType anchor,
|
|
gdouble *shifted_x,
|
|
gdouble *shifted_y);
|
|
|
|
|
|
G_DEFINE_TYPE (GimpDrawTool, gimp_draw_tool, GIMP_TYPE_TOOL)
|
|
|
|
#define parent_class gimp_draw_tool_parent_class
|
|
|
|
|
|
static void
|
|
gimp_draw_tool_class_init (GimpDrawToolClass *klass)
|
|
{
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
|
|
|
|
object_class->dispose = gimp_draw_tool_dispose;
|
|
|
|
tool_class->has_display = gimp_draw_tool_has_display;
|
|
tool_class->has_image = gimp_draw_tool_has_image;
|
|
tool_class->control = gimp_draw_tool_control;
|
|
|
|
klass->draw = gimp_draw_tool_real_draw;
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_init (GimpDrawTool *draw_tool)
|
|
{
|
|
draw_tool->display = NULL;
|
|
draw_tool->paused_count = 0;
|
|
draw_tool->items = NULL;
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_dispose (GObject *object)
|
|
{
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
}
|
|
|
|
static gboolean
|
|
gimp_draw_tool_has_display (GimpTool *tool,
|
|
GimpDisplay *display)
|
|
{
|
|
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
|
|
|
|
return (display == draw_tool->display ||
|
|
GIMP_TOOL_CLASS (parent_class)->has_display (tool, display));
|
|
}
|
|
|
|
static GimpDisplay *
|
|
gimp_draw_tool_has_image (GimpTool *tool,
|
|
GimpImage *image)
|
|
{
|
|
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
|
|
GimpDisplay *display;
|
|
|
|
display = GIMP_TOOL_CLASS (parent_class)->has_image (tool, image);
|
|
|
|
if (! display && draw_tool->display)
|
|
{
|
|
if (image && gimp_display_get_image (draw_tool->display) == image)
|
|
display = draw_tool->display;
|
|
|
|
/* NULL image means any display */
|
|
if (! image)
|
|
display = draw_tool->display;
|
|
}
|
|
|
|
return display;
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_control (GimpTool *tool,
|
|
GimpToolAction action,
|
|
GimpDisplay *display)
|
|
{
|
|
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
|
|
|
|
switch (action)
|
|
{
|
|
case GIMP_TOOL_ACTION_PAUSE:
|
|
case GIMP_TOOL_ACTION_RESUME:
|
|
break;
|
|
|
|
case GIMP_TOOL_ACTION_HALT:
|
|
gimp_draw_tool_stop (draw_tool);
|
|
break;
|
|
}
|
|
|
|
GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_add_item (GimpDrawTool *draw_tool,
|
|
GimpCanvasItem *item)
|
|
{
|
|
draw_tool->items = g_list_append (draw_tool->items, g_object_ref (item));
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_clear_items (GimpDrawTool *draw_tool)
|
|
{
|
|
if (draw_tool->items)
|
|
{
|
|
g_list_foreach (draw_tool->items, (GFunc) g_object_unref, NULL);
|
|
g_list_free (draw_tool->items);
|
|
draw_tool->items = NULL;
|
|
}
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_invalidate_items (GimpDrawTool *draw_tool)
|
|
{
|
|
GimpDisplayShell *shell = gimp_display_get_shell (draw_tool->display);
|
|
GdkWindow *window = gtk_widget_get_window (shell->canvas);
|
|
GList *list;
|
|
|
|
for (list = draw_tool->items; list; list = g_list_next (list))
|
|
{
|
|
GimpCanvasItem *item = list->data;
|
|
GdkRegion *region;
|
|
|
|
region = gimp_canvas_item_get_extents (item, shell);
|
|
gdk_window_invalidate_region (window, region, TRUE);
|
|
gdk_region_destroy (region);
|
|
}
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_draw (GimpDrawTool *draw_tool)
|
|
{
|
|
if (draw_tool->display && draw_tool->paused_count == 0)
|
|
{
|
|
gimp_draw_tool_invalidate_items (draw_tool);
|
|
gimp_draw_tool_clear_items (draw_tool);
|
|
|
|
GIMP_DRAW_TOOL_GET_CLASS (draw_tool)->draw (draw_tool);
|
|
|
|
gimp_draw_tool_invalidate_items (draw_tool);
|
|
}
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_undraw (GimpDrawTool *draw_tool)
|
|
{
|
|
if (draw_tool->display)
|
|
{
|
|
gimp_draw_tool_invalidate_items (draw_tool);
|
|
gimp_draw_tool_clear_items (draw_tool);
|
|
}
|
|
}
|
|
|
|
static void
|
|
gimp_draw_tool_real_draw (GimpDrawTool *draw_tool)
|
|
{
|
|
/* the default implementation does nothing */
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_start (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display)
|
|
{
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
g_return_if_fail (GIMP_IS_DISPLAY (display));
|
|
|
|
gimp_draw_tool_stop (draw_tool);
|
|
|
|
draw_tool->display = display;
|
|
|
|
gimp_draw_tool_draw (draw_tool);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_stop (GimpDrawTool *draw_tool)
|
|
{
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
gimp_draw_tool_undraw (draw_tool);
|
|
|
|
draw_tool->display = NULL;
|
|
}
|
|
|
|
gboolean
|
|
gimp_draw_tool_is_active (GimpDrawTool *draw_tool)
|
|
{
|
|
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
|
|
|
|
return draw_tool->display != NULL;
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_pause (GimpDrawTool *draw_tool)
|
|
{
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
draw_tool->paused_count++;
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_resume (GimpDrawTool *draw_tool)
|
|
{
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
g_return_if_fail (draw_tool->paused_count > 0);
|
|
|
|
draw_tool->paused_count--;
|
|
|
|
gimp_draw_tool_draw (draw_tool);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_items (GimpDrawTool *draw_tool,
|
|
cairo_t *cr)
|
|
{
|
|
GimpDisplayShell *shell;
|
|
GdkWindow *window;
|
|
GList *list;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
g_return_if_fail (cr != NULL);
|
|
|
|
if (! gimp_draw_tool_is_active (draw_tool) ||
|
|
draw_tool->paused_count > 0)
|
|
return;
|
|
|
|
shell = gimp_display_get_shell (draw_tool->display);
|
|
window = gtk_widget_get_window (shell->canvas);
|
|
|
|
for (list = draw_tool->items; list; list = g_list_next (list))
|
|
{
|
|
GimpCanvasItem *item = list->data;
|
|
|
|
gimp_canvas_item_draw (item, shell, cr);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_calc_distance:
|
|
* @draw_tool: a #GimpDrawTool
|
|
* @display: a #GimpDisplay
|
|
* @x1: start point X in image coordinates
|
|
* @y1: start point Y in image coordinates
|
|
* @x2: end point X in image coordinates
|
|
* @y2: end point Y in image coordinates
|
|
*
|
|
* If you just need to compare distances, consider to use
|
|
* gimp_draw_tool_calc_distance_square() instead.
|
|
*
|
|
* Returns: the distance between the given points in display coordinates
|
|
**/
|
|
gdouble
|
|
gimp_draw_tool_calc_distance (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
gdouble x1,
|
|
gdouble y1,
|
|
gdouble x2,
|
|
gdouble y2)
|
|
{
|
|
return sqrt (gimp_draw_tool_calc_distance_square (draw_tool, display,
|
|
x1, y1, x2, y2));
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_calc_distance_square:
|
|
* @draw_tool: a #GimpDrawTool
|
|
* @display: a #GimpDisplay
|
|
* @x1: start point X in image coordinates
|
|
* @y1: start point Y in image coordinates
|
|
* @x2: end point X in image coordinates
|
|
* @y2: end point Y in image coordinates
|
|
*
|
|
* This function is more effective than gimp_draw_tool_calc_distance()
|
|
* as it doesn't perform a sqrt(). Use this if you just need to compare
|
|
* distances.
|
|
*
|
|
* Returns: the square of the distance between the given points in
|
|
* display coordinates
|
|
**/
|
|
gdouble
|
|
gimp_draw_tool_calc_distance_square (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
gdouble x1,
|
|
gdouble y1,
|
|
gdouble x2,
|
|
gdouble y2)
|
|
{
|
|
GimpDisplayShell *shell;
|
|
gdouble tx1, ty1;
|
|
gdouble tx2, ty2;
|
|
|
|
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), 0.0);
|
|
g_return_val_if_fail (GIMP_IS_DISPLAY (display), 0.0);
|
|
|
|
shell = gimp_display_get_shell (display);
|
|
|
|
gimp_display_shell_transform_xy_f (shell, x1, y1, &tx1, &ty1);
|
|
gimp_display_shell_transform_xy_f (shell, x2, y2, &tx2, &ty2);
|
|
|
|
return SQR (tx2 - tx1) + SQR (ty2 - ty1);
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_in_radius:
|
|
* @draw_tool: a #GimpDrawTool
|
|
* @display: a #GimpDisplay
|
|
* @x1: start point X in image coordinates
|
|
* @y1: start point Y in image coordinates
|
|
* @x2: end point X in image coordinates
|
|
* @y2: end point Y in image coordinates
|
|
* @radius: distance in screen coordinates, not image coordinates
|
|
*
|
|
* The points are in image space coordinates.
|
|
*
|
|
* Returns: %TRUE if the points are within radius of each other,
|
|
* %FALSE otherwise
|
|
**/
|
|
gboolean
|
|
gimp_draw_tool_in_radius (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
gdouble x1,
|
|
gdouble y1,
|
|
gdouble x2,
|
|
gdouble y2,
|
|
gint radius)
|
|
{
|
|
return (gimp_draw_tool_calc_distance_square (draw_tool, display,
|
|
x1, y1, x2, y2) < SQR (radius));
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_draw_line:
|
|
* @draw_tool: the #GimpDrawTool
|
|
* @x1: start point X in image coordinates
|
|
* @y1: start point Y in image coordinates
|
|
* @x2: end point X in image coordinates
|
|
* @y2: end point Y in image coordinates
|
|
*
|
|
* This function takes image space coordinates and transforms them to
|
|
* screen window coordinates, then draws a line between the resulting
|
|
* coordindates.
|
|
**/
|
|
void
|
|
gimp_draw_tool_draw_line (GimpDrawTool *draw_tool,
|
|
gdouble x1,
|
|
gdouble y1,
|
|
gdouble x2,
|
|
gdouble y2)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_line_new (x1, y1, x2, y2);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_draw_dashed_line:
|
|
* @draw_tool: the #GimpDrawTool
|
|
* @x1: start point X in image coordinates
|
|
* @y1: start point Y in image coordinates
|
|
* @x2: end point X in image coordinates
|
|
* @y2: end point Y in image coordinates
|
|
*
|
|
* This function takes image space coordinates and transforms them to
|
|
* screen window coordinates, then draws a dashed line between the
|
|
* resulting coordindates.
|
|
**/
|
|
void
|
|
gimp_draw_tool_draw_dashed_line (GimpDrawTool *draw_tool,
|
|
gdouble x1,
|
|
gdouble y1,
|
|
gdouble x2,
|
|
gdouble y2)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_line_new (x1, y1, x2, y2);
|
|
gimp_canvas_item_set_highlight (item, TRUE);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_draw_guide:
|
|
* @draw_tool: the #GimpDrawTool
|
|
* @orientation: the orientation of the guide line
|
|
* @position: the position of the guide line in image coordinates
|
|
*
|
|
* This function draws a guide line across the canvas.
|
|
**/
|
|
void
|
|
gimp_draw_tool_draw_guide_line (GimpDrawTool *draw_tool,
|
|
GimpOrientationType orientation,
|
|
gint position)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_guide_new (orientation, position);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_draw_rectangle:
|
|
* @draw_tool: the #GimpDrawTool
|
|
* @filled: whether to fill the rectangle
|
|
* @x: horizontal image coordinate
|
|
* @y: vertical image coordinate
|
|
* @width: width in image coordinates
|
|
* @height: height in image coordinates
|
|
*
|
|
* This function takes image space coordinates and transforms them to
|
|
* screen window coordinates, then draws the resulting rectangle.
|
|
**/
|
|
void
|
|
gimp_draw_tool_draw_rectangle (GimpDrawTool *draw_tool,
|
|
gboolean filled,
|
|
gdouble x,
|
|
gdouble y,
|
|
gdouble width,
|
|
gdouble height)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_rectangle_new (x, y, width, height, filled);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_arc (GimpDrawTool *draw_tool,
|
|
gboolean filled,
|
|
gdouble x,
|
|
gdouble y,
|
|
gdouble width,
|
|
gdouble height,
|
|
gint angle1,
|
|
gint angle2)
|
|
{
|
|
GimpCanvasItem *item;
|
|
gdouble a1, a2;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
a1 = (gdouble) angle1 / 64.0 / 180.0 * G_PI;
|
|
a2 = (gdouble) angle2 / 64.0 / 180.0 * G_PI;
|
|
|
|
item = gimp_canvas_arc_new (x + width / 2.0,
|
|
y + height / 2.0,
|
|
width / 2.0,
|
|
height / 2.0,
|
|
a1, a2,
|
|
FALSE);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_arc_by_anchor (GimpDrawTool *draw_tool,
|
|
gboolean filled,
|
|
gdouble x,
|
|
gdouble y,
|
|
gint width,
|
|
gint height,
|
|
gint angle1,
|
|
gint angle2,
|
|
GtkAnchorType anchor)
|
|
{
|
|
GimpCanvasItem *item;
|
|
gdouble a1, a2;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_handle_new (filled ?
|
|
GIMP_HANDLE_FILLED_CIRCLE :
|
|
GIMP_HANDLE_CIRCLE,
|
|
anchor, x, y, width, height);
|
|
|
|
a1 = (gdouble) angle1 / 64.0 / 180.0 * G_PI;
|
|
a2 = (gdouble) angle2 / 64.0 / 180.0 * G_PI;
|
|
|
|
gimp_canvas_handle_set_angles (GIMP_CANVAS_HANDLE (item), a1, a2);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_handle (GimpDrawTool *draw_tool,
|
|
GimpHandleType type,
|
|
gdouble x,
|
|
gdouble y,
|
|
gint width,
|
|
gint height,
|
|
GtkAnchorType anchor)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_handle_new (type, anchor, x, y, width, height);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_draw_corner:
|
|
* @draw_tool: the #GimpDrawTool
|
|
* @highlight:
|
|
* @put_outside: whether to put the handles on the outside of the rectangle
|
|
* @x1:
|
|
* @y1:
|
|
* @x2:
|
|
* @y2:
|
|
* @width: corner width
|
|
* @height: corner height
|
|
* @anchor: which corner to draw
|
|
*
|
|
* This function takes image space coordinates and transforms them to
|
|
* screen window coordinates. It draws a corner into an already drawn
|
|
* rectangle outline, taking care of not drawing over an already drawn line.
|
|
**/
|
|
void
|
|
gimp_draw_tool_draw_corner (GimpDrawTool *draw_tool,
|
|
gboolean highlight,
|
|
gboolean put_outside,
|
|
gdouble x1,
|
|
gdouble y1,
|
|
gdouble x2,
|
|
gdouble y2,
|
|
gint width,
|
|
gint height,
|
|
GtkAnchorType anchor)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_corner_new (x1, y1, x2 - x1, y2 - y1,
|
|
anchor, width, height, put_outside);
|
|
gimp_canvas_item_set_highlight (item, highlight);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_lines (GimpDrawTool *draw_tool,
|
|
const GimpVector2 *points,
|
|
gint n_points,
|
|
gboolean filled)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
if (points == NULL || n_points < 2)
|
|
return;
|
|
|
|
item = gimp_canvas_polygon_new (points, n_points, filled);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_strokes (GimpDrawTool *draw_tool,
|
|
const GimpCoords *points,
|
|
gint n_points,
|
|
gboolean filled)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
if (points == NULL || n_points < 2)
|
|
return;
|
|
|
|
item = gimp_canvas_polygon_new_from_coords (points, n_points, filled);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
/**
|
|
* gimp_draw_tool_draw_boundary:
|
|
* @draw_tool: a #GimpDrawTool
|
|
* @bound_segs: the sorted brush outline
|
|
* @n_bound_segs: the number of segments in @bound_segs
|
|
* @offset_x: x offset
|
|
* @offset_y: y offset
|
|
*
|
|
* Draw the boundary of the brush that @draw_tool uses. The boundary
|
|
* should be sorted with sort_boundary(), and @n_bound_segs should
|
|
* include the sentinel segments inserted by sort_boundary() that
|
|
* indicate the end of connected segment sequences (groups) .
|
|
*/
|
|
void
|
|
gimp_draw_tool_draw_boundary (GimpDrawTool *draw_tool,
|
|
const BoundSeg *bound_segs,
|
|
gint n_bound_segs,
|
|
gdouble offset_x,
|
|
gdouble offset_y)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
g_return_if_fail (n_bound_segs > 0);
|
|
g_return_if_fail (bound_segs != NULL);
|
|
|
|
item = gimp_canvas_boundary_new (bound_segs, n_bound_segs,
|
|
offset_x, offset_y);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
void
|
|
gimp_draw_tool_draw_text_cursor (GimpDrawTool *draw_tool,
|
|
PangoRectangle *cursor,
|
|
gboolean overwrite)
|
|
{
|
|
GimpCanvasItem *item;
|
|
|
|
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
|
|
|
|
item = gimp_canvas_text_cursor_new (cursor, overwrite);
|
|
|
|
gimp_draw_tool_add_item (draw_tool, item);
|
|
g_object_unref (item);
|
|
}
|
|
|
|
gboolean
|
|
gimp_draw_tool_on_handle (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
gdouble x,
|
|
gdouble y,
|
|
GimpHandleType type,
|
|
gdouble handle_x,
|
|
gdouble handle_y,
|
|
gint width,
|
|
gint height,
|
|
GtkAnchorType anchor)
|
|
{
|
|
GimpDisplayShell *shell;
|
|
gdouble tx, ty;
|
|
gdouble handle_tx, handle_ty;
|
|
|
|
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
|
|
g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
|
|
|
|
shell = gimp_display_get_shell (display);
|
|
|
|
gimp_display_shell_transform_xy_f (shell,
|
|
x, y,
|
|
&tx, &ty);
|
|
gimp_display_shell_transform_xy_f (shell,
|
|
handle_x, handle_y,
|
|
&handle_tx, &handle_ty);
|
|
|
|
switch (type)
|
|
{
|
|
case GIMP_HANDLE_SQUARE:
|
|
case GIMP_HANDLE_FILLED_SQUARE:
|
|
case GIMP_HANDLE_CROSS:
|
|
gimp_draw_tool_shift_to_north_west (handle_tx, handle_ty,
|
|
width, height,
|
|
anchor,
|
|
&handle_tx, &handle_ty);
|
|
|
|
return (tx == CLAMP (tx, handle_tx, handle_tx + width) &&
|
|
ty == CLAMP (ty, handle_ty, handle_ty + height));
|
|
|
|
case GIMP_HANDLE_CIRCLE:
|
|
case GIMP_HANDLE_FILLED_CIRCLE:
|
|
gimp_draw_tool_shift_to_center (handle_tx, handle_ty,
|
|
width, height,
|
|
anchor,
|
|
&handle_tx, &handle_ty);
|
|
|
|
/* FIXME */
|
|
if (width != height)
|
|
width = (width + height) / 2;
|
|
|
|
width /= 2;
|
|
|
|
return ((SQR (handle_tx - tx) + SQR (handle_ty - ty)) < SQR (width));
|
|
|
|
default:
|
|
g_warning ("%s: invalid handle type %d", G_STRFUNC, type);
|
|
break;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
gboolean
|
|
gimp_draw_tool_on_vectors_handle (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
GimpVectors *vectors,
|
|
const GimpCoords *coord,
|
|
gint width,
|
|
gint height,
|
|
GimpAnchorType preferred,
|
|
gboolean exclusive,
|
|
GimpAnchor **ret_anchor,
|
|
GimpStroke **ret_stroke)
|
|
{
|
|
GimpStroke *stroke = NULL;
|
|
GimpStroke *pref_stroke = NULL;
|
|
GimpAnchor *anchor = NULL;
|
|
GimpAnchor *pref_anchor = NULL;
|
|
gdouble dx, dy;
|
|
gdouble pref_mindist = -1;
|
|
gdouble mindist = -1;
|
|
|
|
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
|
|
g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
|
|
g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
|
|
g_return_val_if_fail (coord != NULL, FALSE);
|
|
|
|
if (ret_anchor) *ret_anchor = NULL;
|
|
if (ret_stroke) *ret_stroke = NULL;
|
|
|
|
while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke)))
|
|
{
|
|
GList *anchor_list = gimp_stroke_get_draw_anchors (stroke);
|
|
GList *list;
|
|
|
|
anchor_list = g_list_concat (gimp_stroke_get_draw_anchors (stroke),
|
|
gimp_stroke_get_draw_controls (stroke));
|
|
|
|
for (list = anchor_list; list; list = g_list_next (list))
|
|
{
|
|
dx = coord->x - GIMP_ANCHOR (list->data)->position.x;
|
|
dy = coord->y - GIMP_ANCHOR (list->data)->position.y;
|
|
|
|
if (mindist < 0 || mindist > dx * dx + dy * dy)
|
|
{
|
|
mindist = dx * dx + dy * dy;
|
|
anchor = GIMP_ANCHOR (list->data);
|
|
|
|
if (ret_stroke)
|
|
*ret_stroke = stroke;
|
|
}
|
|
|
|
if ((pref_mindist < 0 || pref_mindist > dx * dx + dy * dy) &&
|
|
GIMP_ANCHOR (list->data)->type == preferred)
|
|
{
|
|
pref_mindist = dx * dx + dy * dy;
|
|
pref_anchor = GIMP_ANCHOR (list->data);
|
|
pref_stroke = stroke;
|
|
}
|
|
}
|
|
|
|
g_list_free (anchor_list);
|
|
}
|
|
|
|
/* If the data passed into ret_anchor is a preferred anchor, return it. */
|
|
if (ret_anchor && *ret_anchor &&
|
|
gimp_draw_tool_on_handle (draw_tool, display,
|
|
coord->x,
|
|
coord->y,
|
|
GIMP_HANDLE_CIRCLE,
|
|
(*ret_anchor)->position.x,
|
|
(*ret_anchor)->position.y,
|
|
width, height,
|
|
GTK_ANCHOR_CENTER) &&
|
|
(*ret_anchor)->type == preferred)
|
|
{
|
|
if (ret_stroke) *ret_stroke = pref_stroke;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
if (pref_anchor && gimp_draw_tool_on_handle (draw_tool, display,
|
|
coord->x,
|
|
coord->y,
|
|
GIMP_HANDLE_CIRCLE,
|
|
pref_anchor->position.x,
|
|
pref_anchor->position.y,
|
|
width, height,
|
|
GTK_ANCHOR_CENTER))
|
|
{
|
|
if (ret_anchor) *ret_anchor = pref_anchor;
|
|
if (ret_stroke) *ret_stroke = pref_stroke;
|
|
|
|
return TRUE;
|
|
}
|
|
else if (!exclusive && anchor &&
|
|
gimp_draw_tool_on_handle (draw_tool, display,
|
|
coord->x,
|
|
coord->y,
|
|
GIMP_HANDLE_CIRCLE,
|
|
anchor->position.x,
|
|
anchor->position.y,
|
|
width, height,
|
|
GTK_ANCHOR_CENTER))
|
|
{
|
|
if (ret_anchor)
|
|
*ret_anchor = anchor;
|
|
|
|
/* *ret_stroke already set correctly. */
|
|
return TRUE;
|
|
}
|
|
|
|
if (ret_anchor)
|
|
*ret_anchor = NULL;
|
|
if (ret_stroke)
|
|
*ret_stroke = NULL;
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
gboolean
|
|
gimp_draw_tool_on_vectors_curve (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
GimpVectors *vectors,
|
|
const GimpCoords *coord,
|
|
gint width,
|
|
gint height,
|
|
GimpCoords *ret_coords,
|
|
gdouble *ret_pos,
|
|
GimpAnchor **ret_segment_start,
|
|
GimpAnchor **ret_segment_end,
|
|
GimpStroke **ret_stroke)
|
|
{
|
|
GimpStroke *stroke = NULL;
|
|
GimpAnchor *segment_start;
|
|
GimpAnchor *segment_end;
|
|
GimpCoords min_coords = GIMP_COORDS_DEFAULT_VALUES;
|
|
GimpCoords cur_coords;
|
|
gdouble min_dist, cur_dist, cur_pos;
|
|
|
|
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
|
|
g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
|
|
g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
|
|
g_return_val_if_fail (coord != NULL, FALSE);
|
|
|
|
if (ret_coords) *ret_coords = *coord;
|
|
if (ret_pos) *ret_pos = -1.0;
|
|
if (ret_segment_start) *ret_segment_start = NULL;
|
|
if (ret_segment_start) *ret_segment_end = NULL;
|
|
if (ret_stroke) *ret_stroke = NULL;
|
|
|
|
min_dist = -1.0;
|
|
|
|
while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke)))
|
|
{
|
|
cur_dist = gimp_stroke_nearest_point_get (stroke, coord, 1.0,
|
|
&cur_coords,
|
|
&segment_start,
|
|
&segment_end,
|
|
&cur_pos);
|
|
|
|
if (cur_dist >= 0 && (min_dist < 0 || cur_dist < min_dist))
|
|
{
|
|
min_dist = cur_dist;
|
|
min_coords = cur_coords;
|
|
|
|
if (ret_coords) *ret_coords = cur_coords;
|
|
if (ret_pos) *ret_pos = cur_pos;
|
|
if (ret_segment_start) *ret_segment_start = segment_start;
|
|
if (ret_segment_end) *ret_segment_end = segment_end;
|
|
if (ret_stroke) *ret_stroke = stroke;
|
|
}
|
|
}
|
|
|
|
if (min_dist >= 0 &&
|
|
gimp_draw_tool_on_handle (draw_tool, display,
|
|
coord->x,
|
|
coord->y,
|
|
GIMP_HANDLE_CIRCLE,
|
|
min_coords.x,
|
|
min_coords.y,
|
|
width, height,
|
|
GTK_ANCHOR_CENTER))
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
gboolean
|
|
gimp_draw_tool_on_vectors (GimpDrawTool *draw_tool,
|
|
GimpDisplay *display,
|
|
const GimpCoords *coords,
|
|
gint width,
|
|
gint height,
|
|
GimpCoords *ret_coords,
|
|
gdouble *ret_pos,
|
|
GimpAnchor **ret_segment_start,
|
|
GimpAnchor **ret_segment_end,
|
|
GimpStroke **ret_stroke,
|
|
GimpVectors **ret_vectors)
|
|
{
|
|
GList *all_vectors;
|
|
GList *list;
|
|
|
|
if (ret_coords) *ret_coords = *coords;
|
|
if (ret_pos) *ret_pos = -1.0;
|
|
if (ret_segment_start) *ret_segment_start = NULL;
|
|
if (ret_segment_end) *ret_segment_end = NULL;
|
|
if (ret_stroke) *ret_stroke = NULL;
|
|
if (ret_vectors) *ret_vectors = NULL;
|
|
|
|
all_vectors = gimp_image_get_vectors_list (gimp_display_get_image (display));
|
|
|
|
for (list = all_vectors; list; list = g_list_next (list))
|
|
{
|
|
GimpVectors *vectors = list->data;
|
|
|
|
if (! gimp_item_get_visible (GIMP_ITEM (vectors)))
|
|
continue;
|
|
|
|
if (gimp_draw_tool_on_vectors_curve (draw_tool,
|
|
display,
|
|
vectors, coords,
|
|
width, height,
|
|
ret_coords,
|
|
ret_pos,
|
|
ret_segment_start,
|
|
ret_segment_end,
|
|
ret_stroke))
|
|
{
|
|
if (ret_vectors)
|
|
*ret_vectors = vectors;
|
|
|
|
g_list_free (all_vectors);
|
|
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
g_list_free (all_vectors);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
/* private functions */
|
|
|
|
static inline void
|
|
gimp_draw_tool_shift_to_north_west (gdouble x,
|
|
gdouble y,
|
|
gint handle_width,
|
|
gint handle_height,
|
|
GtkAnchorType anchor,
|
|
gdouble *shifted_x,
|
|
gdouble *shifted_y)
|
|
{
|
|
switch (anchor)
|
|
{
|
|
case GTK_ANCHOR_CENTER:
|
|
x -= (handle_width >> 1);
|
|
y -= (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_NORTH:
|
|
x -= (handle_width >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_NORTH_WEST:
|
|
/* nothing, this is the default */
|
|
break;
|
|
|
|
case GTK_ANCHOR_NORTH_EAST:
|
|
x -= handle_width;
|
|
break;
|
|
|
|
case GTK_ANCHOR_SOUTH:
|
|
x -= (handle_width >> 1);
|
|
y -= handle_height;
|
|
break;
|
|
|
|
case GTK_ANCHOR_SOUTH_WEST:
|
|
y -= handle_height;
|
|
break;
|
|
|
|
case GTK_ANCHOR_SOUTH_EAST:
|
|
x -= handle_width;
|
|
y -= handle_height;
|
|
break;
|
|
|
|
case GTK_ANCHOR_WEST:
|
|
y -= (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_EAST:
|
|
x -= handle_width;
|
|
y -= (handle_height >> 1);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (shifted_x)
|
|
*shifted_x = x;
|
|
|
|
if (shifted_y)
|
|
*shifted_y = y;
|
|
}
|
|
|
|
static inline void
|
|
gimp_draw_tool_shift_to_center (gdouble x,
|
|
gdouble y,
|
|
gint handle_width,
|
|
gint handle_height,
|
|
GtkAnchorType anchor,
|
|
gdouble *shifted_x,
|
|
gdouble *shifted_y)
|
|
{
|
|
switch (anchor)
|
|
{
|
|
case GTK_ANCHOR_CENTER:
|
|
/* nothing, this is the default */
|
|
break;
|
|
|
|
case GTK_ANCHOR_NORTH:
|
|
y += (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_NORTH_WEST:
|
|
x += (handle_width >> 1);
|
|
y += (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_NORTH_EAST:
|
|
x -= (handle_width >> 1);
|
|
y += (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_SOUTH:
|
|
y -= (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_SOUTH_WEST:
|
|
x += (handle_width >> 1);
|
|
y -= (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_SOUTH_EAST:
|
|
x -= (handle_width >> 1);
|
|
y -= (handle_height >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_WEST:
|
|
x += (handle_width >> 1);
|
|
break;
|
|
|
|
case GTK_ANCHOR_EAST:
|
|
x -= (handle_width >> 1);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (shifted_x)
|
|
*shifted_x = x;
|
|
|
|
if (shifted_y)
|
|
*shifted_y = y;
|
|
}
|