gimp/app/display/gimpcanvas.c

551 lines
16 KiB
C
Raw Normal View History

/* GIMP - The GNU 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 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 <gtk/gtk.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "display-types.h"
#include "config/gimpdisplayconfig.h"
2010-08-31 22:29:49 +02:00
#include "widgets/gimpwidgets-utils.h"
#include "gimpcanvas.h"
#include "gimp-intl.h"
#define MAX_BATCH_SIZE 32000
enum
{
PROP_0,
PROP_CONFIG
};
/* local function prototypes */
static void gimp_canvas_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_canvas_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_canvas_unrealize (GtkWidget *widget);
static void gimp_canvas_style_set (GtkWidget *widget,
GtkStyle *prev_style);
static gboolean gimp_canvas_focus_in_event (GtkWidget *widget,
GdkEventFocus *event);
static gboolean gimp_canvas_focus_out_event (GtkWidget *widget,
GdkEventFocus *event);
static gboolean gimp_canvas_focus (GtkWidget *widget,
GtkDirectionType direction);
static GdkGC * gimp_canvas_gc_new (GimpCanvas *canvas,
GimpCanvasStyle style);
G_DEFINE_TYPE (GimpCanvas, gimp_canvas, GIMP_TYPE_OVERLAY_BOX)
#define parent_class gimp_canvas_parent_class
static void
gimp_canvas_class_init (GimpCanvasClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->set_property = gimp_canvas_set_property;
object_class->get_property = gimp_canvas_get_property;
widget_class->unrealize = gimp_canvas_unrealize;
widget_class->style_set = gimp_canvas_style_set;
widget_class->focus_in_event = gimp_canvas_focus_in_event;
widget_class->focus_out_event = gimp_canvas_focus_out_event;
widget_class->focus = gimp_canvas_focus;
g_object_class_install_property (object_class, PROP_CONFIG,
g_param_spec_object ("config", NULL, NULL,
GIMP_TYPE_DISPLAY_CONFIG,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
}
static void
gimp_canvas_init (GimpCanvas *canvas)
{
GtkWidget *widget = GTK_WIDGET (canvas);
gint i;
gtk_widget_set_double_buffered (widget, FALSE);
gtk_widget_set_can_focus (widget, TRUE);
gtk_widget_add_events (widget, GIMP_CANVAS_EVENT_MASK);
gtk_widget_set_extension_events (widget, GDK_EXTENSION_EVENTS_ALL);
for (i = 0; i < GIMP_CANVAS_NUM_STYLES; i++)
canvas->gc[i] = NULL;
}
static void
gimp_canvas_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpCanvas *canvas = GIMP_CANVAS (object);
switch (property_id)
{
case PROP_CONFIG:
canvas->config = g_value_get_object (value); /* don't dup */
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_canvas_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpCanvas *canvas = GIMP_CANVAS (object);
switch (property_id)
{
case PROP_CONFIG:
g_value_set_object (value, canvas->config);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_canvas_unrealize (GtkWidget *widget)
{
GimpCanvas *canvas = GIMP_CANVAS (widget);
gint i;
for (i = 0; i < GIMP_CANVAS_NUM_STYLES; i++)
{
if (canvas->gc[i])
{
g_object_unref (canvas->gc[i]);
canvas->gc[i] = NULL;
}
}
if (canvas->layout)
{
g_object_unref (canvas->layout);
canvas->layout = NULL;
}
GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}
static void
gimp_canvas_style_set (GtkWidget *widget,
GtkStyle *prev_style)
{
GimpCanvas *canvas = GIMP_CANVAS (widget);
GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
if (canvas->layout)
{
g_object_unref (canvas->layout);
canvas->layout = NULL;
}
}
static gboolean
gimp_canvas_focus_in_event (GtkWidget *widget,
GdkEventFocus *event)
{
/* don't allow the default impl to invalidate the whole widget,
* we don't draw a focus indicator anyway.
*/
return FALSE;
}
static gboolean
gimp_canvas_focus_out_event (GtkWidget *widget,
GdkEventFocus *event)
{
/* see focus-in-event
*/
return FALSE;
}
static gboolean
gimp_canvas_focus (GtkWidget *widget,
GtkDirectionType direction)
{
GtkWidget *focus = gtk_container_get_focus_child (GTK_CONTAINER (widget));
/* override GtkContainer's focus() implementation which would always
* give focus to the canvas because it is focussable. Instead, try
* navigating in the focussed overlay child first, and use
* GtkContainer's default implementation only if that fails (which
* happens when focus navigation leaves the overlay child).
*/
if (focus && gtk_widget_child_focus (focus, direction))
return TRUE;
return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
}
/* Returns: %TRUE if the XOR color is not white */
static gboolean
gimp_canvas_get_xor_color (GimpCanvas *canvas,
GdkColor *color)
{
guchar r, g, b;
gimp_rgb_get_uchar (&canvas->config->xor_color, &r, &g, &b);
color->red = (r << 8) | r;
color->green = (g << 8) | g;
color->blue = (b << 8) | b;
return (r != 255 || g != 255 || b != 255);
}
static GdkGC *
gimp_canvas_gc_new (GimpCanvas *canvas,
GimpCanvasStyle style)
{
GdkGC *gc;
GdkGCValues values;
GdkGCValuesMask mask = 0;
GdkColor fg = { 0, 0, 0, 0 };
GdkColor bg = { 0, 0, 0, 0 };
if (! gtk_widget_get_realized (GTK_WIDGET (canvas)))
return NULL;
switch (style)
{
case GIMP_CANVAS_STYLE_XOR_DOTTED:
mask |= GDK_GC_LINE_STYLE;
values.line_style = GDK_LINE_ON_OFF_DASH;
/* fallthrough */
case GIMP_CANVAS_STYLE_XOR:
mask |= GDK_GC_FUNCTION | GDK_GC_CAP_STYLE | GDK_GC_JOIN_STYLE;
if (gimp_canvas_get_xor_color (canvas, &fg))
values.function = GDK_XOR;
else
values.function = GDK_INVERT;
values.cap_style = GDK_CAP_NOT_LAST;
values.join_style = GDK_JOIN_MITER;
break;
default:
return NULL;
}
gc = gdk_gc_new_with_values (gtk_widget_get_window (GTK_WIDGET (canvas)),
&values, mask);
if (style == GIMP_CANVAS_STYLE_XOR_DOTTED)
{
gint8 one = 1;
gdk_gc_set_dashes (gc, 0, &one, 1);
}
switch (style)
{
default:
return gc;
case GIMP_CANVAS_STYLE_XOR_DOTTED:
case GIMP_CANVAS_STYLE_XOR:
break;
}
gdk_gc_set_rgb_fg_color (gc, &fg);
gdk_gc_set_rgb_bg_color (gc, &bg);
return gc;
}
static inline gboolean
gimp_canvas_ensure_style (GimpCanvas *canvas,
GimpCanvasStyle style)
{
return (canvas->gc[style] != NULL ||
(canvas->gc[style] = gimp_canvas_gc_new (canvas, style)) != NULL);
}
/* public functions */
/**
* gimp_canvas_new:
*
* Creates a new #GimpCanvas widget.
*
* The #GimpCanvas widget is a #GtkDrawingArea abstraction. It manages
* a set of graphic contexts for drawing on a GIMP display. If you
* draw using a #GimpCanvasStyle, #GimpCanvas makes sure that the
* associated #GdkGC is created. All drawing on the canvas needs to
* happen by means of the #GimpCanvas drawing functions. Besides from
* not needing a #GdkGC pointer, the #GimpCanvas drawing functions
* look and work like their #GdkDrawable counterparts. #GimpCanvas
* gracefully handles attempts to draw on the unrealized widget.
*
* Return value: a new #GimpCanvas widget
**/
GtkWidget *
gimp_canvas_new (GimpDisplayConfig *config)
{
g_return_val_if_fail (GIMP_IS_DISPLAY_CONFIG (config), NULL);
return g_object_new (GIMP_TYPE_CANVAS,
"name", "gimp-canvas",
"config", config,
NULL);
}
/**
* gimp_canvas_draw_line:
* @canvas: a #GimpCanvas widget
* @style: one of the enumerated #GimpCanvasStyle's.
* @x1: X coordinate of the first point
* @y1: Y coordinate of the first point
* @x2: X coordinate of the second point
* @y2: Y coordinate of the second point
*
* Draw a line connecting the specified points, using the specified
* style.
**/
void
gimp_canvas_draw_line (GimpCanvas *canvas,
GimpCanvasStyle style,
gint x1,
gint y1,
gint x2,
gint y2)
{
if (! gimp_canvas_ensure_style (canvas, style))
return;
gdk_draw_line (gtk_widget_get_window (GTK_WIDGET (canvas)),
canvas->gc[style],
x1, y1, x2, y2);
}
/**
* gimp_canvas_draw_lines:
* @canvas: a #GimpCanvas widget
* @style: one of the enumerated #GimpCanvasStyle's.
* @points: a #GdkPoint array.
* @num_points: the number of points in the array.
*
* Draws a set of lines connecting the specified points, in the
* specified style.
**/
void
gimp_canvas_draw_lines (GimpCanvas *canvas,
GimpCanvasStyle style,
GdkPoint *points,
gint num_points)
{
if (! gimp_canvas_ensure_style (canvas, style))
return;
gdk_draw_lines (gtk_widget_get_window (GTK_WIDGET (canvas)),
canvas->gc[style],
points, num_points);
}
/**
* gimp_canvas_draw_rectangle:
* @canvas: a #GimpCanvas widget
* @style: one of the enumerated #GimpCanvasStyle's.
* @filled: %TRUE if the rectangle is to be filled.
* @x: X coordinate of the upper left corner.
* @y: Y coordinate of the upper left corner.
* @width: width of the rectangle.
* @height: height of the rectangle.
*
* Draws a rectangle in the specified style.
**/
void
gimp_canvas_draw_rectangle (GimpCanvas *canvas,
GimpCanvasStyle style,
gboolean filled,
gint x,
gint y,
gint width,
gint height)
{
if (! gimp_canvas_ensure_style (canvas, style))
return;
gdk_draw_rectangle (gtk_widget_get_window (GTK_WIDGET (canvas)),
canvas->gc[style],
filled, x, y, width, height);
}
/**
* gimp_canvas_draw_arc:
* @canvas: a #GimpCanvas widget
* @style: one of the enumerated #GimpCanvasStyle's.
* @filled: %TRUE if the arc is to be filled, producing a 'pie slice'.
* @x: X coordinate of the left edge of the bounding rectangle.
* @y: Y coordinate of the top edge of the bounding rectangle.
* @width: width of the bounding rectangle.
* @height: height of the bounding rectangle.
* @angle1: the start angle of the arc.
* @angle2: the end angle of the arc.
*
* Draws an arc or pie slice, in the specified style.
**/
void
gimp_canvas_draw_arc (GimpCanvas *canvas,
GimpCanvasStyle style,
gboolean filled,
gint x,
gint y,
gint width,
gint height,
gint angle1,
gint angle2)
{
if (! gimp_canvas_ensure_style (canvas, style))
return;
gdk_draw_arc (gtk_widget_get_window (GTK_WIDGET (canvas)),
canvas->gc[style],
filled, x, y, width, height, angle1, angle2);
}
/**
* gimp_canvas_draw_segments:
* @canvas: a #GimpCanvas widget
* @style: one of the enumerated #GimpCanvasStyle's.
* @segments: a #GdkSegment array.
* @num_segments: the number of segments in the array.
*
* Draws a set of line segments in the specified style.
**/
void
gimp_canvas_draw_segments (GimpCanvas *canvas,
GimpCanvasStyle style,
GdkSegment *segments,
gint num_segments)
{
if (! gimp_canvas_ensure_style (canvas, style))
return;
while (num_segments >= MAX_BATCH_SIZE)
{
gdk_draw_segments (gtk_widget_get_window (GTK_WIDGET (canvas)),
canvas->gc[style],
segments, MAX_BATCH_SIZE);
num_segments -= MAX_BATCH_SIZE;
segments += MAX_BATCH_SIZE;
}
gdk_draw_segments (gtk_widget_get_window (GTK_WIDGET (canvas)),
canvas->gc[style],
segments, num_segments);
}
/**
* gimp_canvas_draw_text:
* @canvas: a #GimpCanvas widget
* @format: a standard printf() format string.
* @Varargs: the parameters to insert into the format string.
*
* Returns a layout which can be used for
* pango_cairo_show_layout(). The layout belongs to the canvas and
* should not be freed, not should a pointer to it be kept around
* after drawing.
*
* Returns: a #PangoLayout owned by the canvas.
**/
PangoLayout *
gimp_canvas_get_layout (GimpCanvas *canvas,
const gchar *format,
...)
{
va_list args;
gchar *text;
if (! canvas->layout)
canvas->layout = gtk_widget_create_pango_layout (GTK_WIDGET (canvas),
NULL);
va_start (args, format);
text = g_strdup_vprintf (format, args);
va_end (args);
pango_layout_set_text (canvas->layout, text, -1);
g_free (text);
return canvas->layout;
}
/**
* gimp_canvas_set_bg_color:
* @canvas: a #GimpCanvas widget
* @color: a color in #GimpRGB format
*
* Sets the background color of the canvas's window. This
* is the color the canvas is set to if it is cleared.
**/
void
gimp_canvas_set_bg_color (GimpCanvas *canvas,
GimpRGB *color)
{
GtkWidget *widget = GTK_WIDGET (canvas);
GdkColormap *colormap;
GdkColor gdk_color;
if (! gtk_widget_get_realized (widget))
return;
gimp_rgb_get_gdk_color (color, &gdk_color);
colormap = gdk_drawable_get_colormap (gtk_widget_get_window (widget));
g_return_if_fail (colormap != NULL);
gdk_colormap_alloc_color (colormap, &gdk_color, FALSE, TRUE);
gdk_window_set_background (gtk_widget_get_window (widget), &gdk_color);
}