2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2003-11-10 07:24:40 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2003-11-10 07:24:40 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-11-10 07:24:40 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-11-10 07:24:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2007-03-23 17:25:28 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
2008-03-26 21:08:31 +08:00
|
|
|
#include "libgimpwidgets/gimpwidgets.h"
|
2007-03-23 17:25:28 +08:00
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
#include "display-types.h"
|
|
|
|
|
2007-03-23 17:25:28 +08:00
|
|
|
#include "config/gimpdisplayconfig.h"
|
|
|
|
|
2010-09-01 04:29:49 +08:00
|
|
|
#include "widgets/gimpwidgets-utils.h"
|
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
#include "gimpcanvas.h"
|
|
|
|
|
2008-03-19 04:24:49 +08:00
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
|
2010-03-04 00:35:50 +08:00
|
|
|
#define MAX_BATCH_SIZE 32000
|
|
|
|
|
|
|
|
|
2007-03-23 17:25:28 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2008-03-19 04:24:49 +08:00
|
|
|
PROP_CONFIG
|
2007-03-23 17:25:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
/* local function prototypes */
|
|
|
|
|
2009-11-03 20:58:36 +08:00
|
|
|
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);
|
2010-06-13 00:39:21 +08:00
|
|
|
static gboolean gimp_canvas_focus (GtkWidget *widget,
|
|
|
|
GtkDirectionType direction);
|
2009-11-03 20:58:36 +08:00
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
|
2009-10-19 04:19:29 +08:00
|
|
|
G_DEFINE_TYPE (GimpCanvas, gimp_canvas, GIMP_TYPE_OVERLAY_BOX)
|
2005-12-11 03:24:36 +08:00
|
|
|
|
|
|
|
#define parent_class gimp_canvas_parent_class
|
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_canvas_class_init (GimpCanvasClass *klass)
|
|
|
|
{
|
2007-03-23 17:25:28 +08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2003-11-10 07:24:40 +08:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2009-11-03 20:58:36 +08:00
|
|
|
object_class->set_property = gimp_canvas_set_property;
|
|
|
|
object_class->get_property = gimp_canvas_get_property;
|
2007-03-23 17:25:28 +08:00
|
|
|
|
2009-11-03 20:58:36 +08:00
|
|
|
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;
|
2010-06-13 00:39:21 +08:00
|
|
|
widget_class->focus = gimp_canvas_focus;
|
2007-03-23 17:25:28 +08:00
|
|
|
|
2008-03-19 04:24:49 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_CONFIG,
|
|
|
|
g_param_spec_object ("config", NULL, NULL,
|
|
|
|
GIMP_TYPE_DISPLAY_CONFIG,
|
2007-03-23 17:25:28 +08:00
|
|
|
GIMP_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY));
|
2003-11-10 07:24:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_canvas_init (GimpCanvas *canvas)
|
|
|
|
{
|
2008-08-22 07:55:58 +08:00
|
|
|
GtkWidget *widget = GTK_WIDGET (canvas);
|
|
|
|
|
2009-10-18 01:51:33 +08:00
|
|
|
gtk_widget_set_can_focus (widget, TRUE);
|
2009-10-19 04:19:29 +08:00
|
|
|
gtk_widget_add_events (widget, GIMP_CANVAS_EVENT_MASK);
|
2008-08-22 07:55:58 +08:00
|
|
|
gtk_widget_set_extension_events (widget, GDK_EXTENSION_EVENTS_ALL);
|
2003-11-10 07:24:40 +08:00
|
|
|
}
|
|
|
|
|
2007-03-23 17:25:28 +08:00
|
|
|
static void
|
|
|
|
gimp_canvas_set_property (GObject *object,
|
2009-10-19 04:19:29 +08:00
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2007-03-23 17:25:28 +08:00
|
|
|
{
|
|
|
|
GimpCanvas *canvas = GIMP_CANVAS (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2008-03-19 04:24:49 +08:00
|
|
|
case PROP_CONFIG:
|
|
|
|
canvas->config = g_value_get_object (value); /* don't dup */
|
2007-03-23 17:25:28 +08:00
|
|
|
break;
|
2010-09-25 03:03:51 +08:00
|
|
|
|
2007-03-23 17:25:28 +08:00
|
|
|
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)
|
|
|
|
{
|
2008-03-19 04:24:49 +08:00
|
|
|
case PROP_CONFIG:
|
|
|
|
g_value_set_object (value, canvas->config);
|
2007-03-23 17:25:28 +08:00
|
|
|
break;
|
2010-09-25 03:03:51 +08:00
|
|
|
|
2007-03-23 17:25:28 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-10 07:24:40 +08:00
|
|
|
static void
|
|
|
|
gimp_canvas_unrealize (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GimpCanvas *canvas = GIMP_CANVAS (widget);
|
2003-11-10 09:10:46 +08:00
|
|
|
|
2005-03-09 08:23:19 +08:00
|
|
|
if (canvas->layout)
|
|
|
|
{
|
|
|
|
g_object_unref (canvas->layout);
|
|
|
|
canvas->layout = NULL;
|
|
|
|
}
|
|
|
|
|
2003-11-11 03:35:56 +08:00
|
|
|
GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
|
|
|
|
}
|
2003-11-10 09:10:46 +08:00
|
|
|
|
2007-11-14 22:42:05 +08:00
|
|
|
static void
|
|
|
|
gimp_canvas_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *prev_style)
|
|
|
|
{
|
|
|
|
GimpCanvas *canvas = GIMP_CANVAS (widget);
|
|
|
|
|
2008-03-30 07:43:39 +08:00
|
|
|
GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
|
2007-11-14 22:42:05 +08:00
|
|
|
|
|
|
|
if (canvas->layout)
|
|
|
|
{
|
|
|
|
g_object_unref (canvas->layout);
|
|
|
|
canvas->layout = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 20:58:36 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_canvas_focus_in_event (GtkWidget *widget,
|
|
|
|
GdkEventFocus *event)
|
|
|
|
{
|
2009-11-05 02:32:13 +08:00
|
|
|
/* don't allow the default impl to invalidate the whole widget,
|
|
|
|
* we don't draw a focus indicator anyway.
|
|
|
|
*/
|
2009-11-03 20:58:36 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_canvas_focus_out_event (GtkWidget *widget,
|
|
|
|
GdkEventFocus *event)
|
|
|
|
{
|
2009-11-05 02:32:13 +08:00
|
|
|
/* see focus-in-event
|
|
|
|
*/
|
2009-11-03 20:58:36 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-13 00:39:21 +08:00
|
|
|
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
|
2010-06-13 00:46:30 +08:00
|
|
|
* happens when focus navigation leaves the overlay child).
|
2010-06-13 00:39:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (focus && gtk_widget_child_focus (focus, direction))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
|
|
|
|
}
|
|
|
|
|
2003-11-11 03:35:56 +08:00
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
2003-11-12 05:35:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_canvas_new:
|
|
|
|
*
|
2004-07-16 18:07:30 +08:00
|
|
|
* Creates a new #GimpCanvas widget.
|
2003-11-12 05:35:50 +08:00
|
|
|
*
|
2004-07-16 18:07:30 +08:00
|
|
|
* The #GimpCanvas widget is a #GtkDrawingArea abstraction. It manages
|
2003-11-12 05:35:50 +08:00
|
|
|
* 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
|
|
|
|
**/
|
2003-11-10 07:24:40 +08:00
|
|
|
GtkWidget *
|
2008-03-19 04:24:49 +08:00
|
|
|
gimp_canvas_new (GimpDisplayConfig *config)
|
2003-11-10 07:24:40 +08:00
|
|
|
{
|
2008-03-19 04:24:49 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_DISPLAY_CONFIG (config), NULL);
|
2007-03-23 17:25:28 +08:00
|
|
|
|
2003-11-11 03:35:56 +08:00
|
|
|
return g_object_new (GIMP_TYPE_CANVAS,
|
2008-03-19 04:24:49 +08:00
|
|
|
"name", "gimp-canvas",
|
|
|
|
"config", config,
|
2003-11-11 03:35:56 +08:00
|
|
|
NULL);
|
2003-11-10 09:10:46 +08:00
|
|
|
}
|
|
|
|
|
2005-03-09 08:23:19 +08:00
|
|
|
/**
|
2010-09-25 01:08:19 +08:00
|
|
|
* gimp_canvas_get_layout:
|
2005-03-23 21:53:31 +08:00
|
|
|
* @canvas: a #GimpCanvas widget
|
|
|
|
* @format: a standard printf() format string.
|
|
|
|
* @Varargs: the parameters to insert into the format string.
|
2005-03-09 08:23:19 +08:00
|
|
|
*
|
2010-08-12 19:13:15 +08:00
|
|
|
* 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.
|
2005-03-09 08:23:19 +08:00
|
|
|
**/
|
2010-08-12 19:13:15 +08:00
|
|
|
PangoLayout *
|
|
|
|
gimp_canvas_get_layout (GimpCanvas *canvas,
|
|
|
|
const gchar *format,
|
|
|
|
...)
|
2005-03-09 08:23:19 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
if (! canvas->layout)
|
2005-03-23 21:53:31 +08:00
|
|
|
canvas->layout = gtk_widget_create_pango_layout (GTK_WIDGET (canvas),
|
|
|
|
NULL);
|
2005-03-09 08:23:19 +08:00
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
text = g_strdup_vprintf (format, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
pango_layout_set_text (canvas->layout, text, -1);
|
|
|
|
g_free (text);
|
|
|
|
|
2010-08-12 19:13:15 +08:00
|
|
|
return canvas->layout;
|
2005-03-09 08:23:19 +08:00
|
|
|
}
|
|
|
|
|
2004-07-16 07:02:52 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
2003-11-11 22:50:07 +08:00
|
|
|
void
|
|
|
|
gimp_canvas_set_bg_color (GimpCanvas *canvas,
|
|
|
|
GimpRGB *color)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (canvas);
|
|
|
|
GdkColormap *colormap;
|
|
|
|
GdkColor gdk_color;
|
|
|
|
|
2010-04-20 01:21:07 +08:00
|
|
|
if (! gtk_widget_get_realized (widget))
|
2003-11-11 22:50:07 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
gimp_rgb_get_gdk_color (color, &gdk_color);
|
|
|
|
|
2009-03-23 00:03:46 +08:00
|
|
|
colormap = gdk_drawable_get_colormap (gtk_widget_get_window (widget));
|
2003-11-11 22:50:07 +08:00
|
|
|
g_return_if_fail (colormap != NULL);
|
|
|
|
gdk_colormap_alloc_color (colormap, &gdk_color, FALSE, TRUE);
|
|
|
|
|
2009-03-23 00:03:46 +08:00
|
|
|
gdk_window_set_background (gtk_widget_get_window (widget), &gdk_color);
|
2010-10-03 03:13:31 +08:00
|
|
|
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (canvas));
|
2003-11-11 22:50:07 +08:00
|
|
|
}
|