2004-08-30 22:10:26 +00:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpdrawablepreview.c
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "libgimpwidgets/gimpwidgets.h"
|
|
|
|
|
2004-08-31 09:16:47 +00:00
|
|
|
#include "gimpuitypes.h"
|
|
|
|
|
|
|
|
#include "gimp.h"
|
|
|
|
|
2004-08-30 22:10:26 +00:00
|
|
|
#include "gimpdrawablepreview.h"
|
|
|
|
|
|
|
|
|
2004-09-01 02:36:42 +00:00
|
|
|
#define SELECTION_BORDER 2
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-08-31 19:58:25 +00:00
|
|
|
|
|
|
|
static void gimp_drawable_preview_class_init (GimpDrawablePreviewClass *klass);
|
2004-09-03 12:19:26 +00:00
|
|
|
static void gimp_drawable_preview_init (GimpDrawablePreview *preview);
|
2004-08-31 19:58:25 +00:00
|
|
|
|
2004-08-31 21:15:03 +00:00
|
|
|
static void gimp_drawable_preview_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *prev_style);
|
|
|
|
static void gimp_drawable_preview_draw_original (GimpPreview *preview);
|
2004-08-30 22:10:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
static GimpPreviewClass *parent_class = NULL;
|
|
|
|
|
2004-08-30 22:49:06 +00:00
|
|
|
|
2004-08-30 22:10:26 +00:00
|
|
|
GType
|
|
|
|
gimp_drawable_preview_get_type (void)
|
|
|
|
{
|
2004-08-30 22:49:06 +00:00
|
|
|
static GType preview_type = 0;
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-08-30 22:49:06 +00:00
|
|
|
if (!preview_type)
|
2004-08-30 22:10:26 +00:00
|
|
|
{
|
|
|
|
static const GTypeInfo drawable_preview_info =
|
|
|
|
{
|
|
|
|
sizeof (GimpDrawablePreviewClass),
|
2004-08-30 22:49:06 +00:00
|
|
|
(GBaseInitFunc) NULL,
|
2004-08-30 22:10:26 +00:00
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gimp_drawable_preview_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GimpDrawablePreview),
|
|
|
|
0, /* n_preallocs */
|
2004-09-03 12:19:26 +00:00
|
|
|
(GInstanceInitFunc) gimp_drawable_preview_init
|
2004-08-30 22:10:26 +00:00
|
|
|
};
|
|
|
|
|
2004-08-30 22:49:06 +00:00
|
|
|
preview_type = g_type_register_static (GIMP_TYPE_PREVIEW,
|
|
|
|
"GimpDrawablePreview",
|
|
|
|
&drawable_preview_info, 0);
|
2004-08-30 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
2004-08-30 22:49:06 +00:00
|
|
|
return preview_type;
|
2004-08-30 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_drawable_preview_class_init (GimpDrawablePreviewClass *klass)
|
|
|
|
{
|
2004-08-31 21:15:03 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2004-08-30 22:49:06 +00:00
|
|
|
GimpPreviewClass *preview_class = GIMP_PREVIEW_CLASS (klass);
|
|
|
|
|
2004-08-30 22:10:26 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2004-08-31 21:15:03 +00:00
|
|
|
widget_class->style_set = gimp_drawable_preview_style_set;
|
|
|
|
|
|
|
|
preview_class->draw = gimp_drawable_preview_draw_original;
|
|
|
|
}
|
|
|
|
|
2004-09-03 12:19:26 +00:00
|
|
|
static void
|
|
|
|
gimp_drawable_preview_init (GimpDrawablePreview *preview)
|
|
|
|
{
|
|
|
|
g_object_set (GIMP_PREVIEW (preview)->area,
|
|
|
|
"check-size", gimp_check_size (),
|
|
|
|
"check-type", gimp_check_type (),
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2004-08-31 21:15:03 +00:00
|
|
|
static void
|
|
|
|
gimp_drawable_preview_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *prev_style)
|
|
|
|
{
|
|
|
|
GimpPreview *preview = GIMP_PREVIEW (widget);
|
|
|
|
gint width = preview->xmax - preview->xmin;
|
|
|
|
gint height = preview->ymax - preview->ymin;
|
|
|
|
gint size;
|
|
|
|
|
|
|
|
if (GTK_WIDGET_CLASS (parent_class)->style_set)
|
|
|
|
GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
|
|
|
|
|
|
|
|
gtk_widget_style_get (widget,
|
|
|
|
"size", &size,
|
2004-08-31 21:29:30 +00:00
|
|
|
NULL);
|
2004-08-31 21:15:03 +00:00
|
|
|
|
|
|
|
gtk_widget_set_size_request (GIMP_PREVIEW (preview)->area,
|
|
|
|
MIN (width, size), MIN (height, size));
|
2004-08-30 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-08-31 14:29:25 +00:00
|
|
|
gimp_drawable_preview_draw_original (GimpPreview *preview)
|
2004-08-30 22:10:26 +00:00
|
|
|
{
|
2004-08-30 22:49:06 +00:00
|
|
|
GimpDrawablePreview *drawable_preview = GIMP_DRAWABLE_PREVIEW (preview);
|
|
|
|
GimpDrawable *drawable = drawable_preview->drawable;
|
2004-08-30 22:10:26 +00:00
|
|
|
guchar *buffer;
|
2004-08-30 22:49:06 +00:00
|
|
|
GimpPixelRgn srcPR;
|
|
|
|
guint rowstride;
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-09-01 12:10:09 +00:00
|
|
|
if (! drawable)
|
|
|
|
return;
|
|
|
|
|
2004-08-30 22:49:06 +00:00
|
|
|
rowstride = preview->width * drawable->bpp;
|
2004-08-30 22:10:26 +00:00
|
|
|
buffer = g_new (guchar, rowstride * preview->height);
|
|
|
|
|
|
|
|
preview->xoff = CLAMP (preview->xoff,
|
|
|
|
0, preview->xmax - preview->xmin - preview->width);
|
|
|
|
preview->yoff = CLAMP (preview->yoff,
|
|
|
|
0, preview->ymax - preview->ymin - preview->height);
|
2004-08-30 22:49:06 +00:00
|
|
|
|
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable,
|
2004-08-30 22:10:26 +00:00
|
|
|
preview->xoff + preview->xmin,
|
|
|
|
preview->yoff + preview->ymin,
|
|
|
|
preview->width, preview->height,
|
|
|
|
FALSE, FALSE);
|
2004-08-30 22:49:06 +00:00
|
|
|
|
2004-08-30 22:10:26 +00:00
|
|
|
gimp_pixel_rgn_get_rect (&srcPR, buffer,
|
|
|
|
preview->xoff + preview->xmin,
|
|
|
|
preview->yoff + preview->ymin,
|
|
|
|
preview->width, preview->height);
|
|
|
|
|
|
|
|
gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview->area),
|
|
|
|
0, 0, preview->width, preview->height,
|
2004-08-30 22:49:06 +00:00
|
|
|
gimp_drawable_type (drawable->drawable_id),
|
2004-08-30 22:10:26 +00:00
|
|
|
buffer,
|
|
|
|
rowstride);
|
|
|
|
g_free (buffer);
|
|
|
|
}
|
|
|
|
|
2004-08-31 19:58:25 +00:00
|
|
|
static void
|
|
|
|
gimp_drawable_preview_set_drawable (GimpDrawablePreview *drawable_preview,
|
|
|
|
GimpDrawable *drawable)
|
|
|
|
{
|
|
|
|
GimpPreview *preview = GIMP_PREVIEW (drawable_preview);
|
2004-09-01 02:10:40 +00:00
|
|
|
gint width = gimp_drawable_width (drawable->drawable_id);
|
|
|
|
gint height = gimp_drawable_height (drawable->drawable_id);
|
|
|
|
gint x1, x2;
|
|
|
|
gint y1, y2;
|
2004-08-31 19:58:25 +00:00
|
|
|
|
|
|
|
drawable_preview->drawable = drawable;
|
|
|
|
|
2004-09-01 02:10:40 +00:00
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
|
|
|
|
|
|
|
preview->xmin = MAX (x1 - SELECTION_BORDER, 0);
|
|
|
|
preview->ymin = MAX (y1 - SELECTION_BORDER, 0);
|
|
|
|
preview->xmax = MIN (x2 + SELECTION_BORDER, width);
|
|
|
|
preview->ymax = MIN (y2 + SELECTION_BORDER, height);
|
2004-08-31 19:58:25 +00:00
|
|
|
}
|
2004-08-30 22:49:06 +00:00
|
|
|
|
2004-09-01 14:01:10 +00:00
|
|
|
static void
|
|
|
|
gimp_drawable_preview_notify_update (GimpPreview *preview,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gboolean *toggle)
|
|
|
|
{
|
|
|
|
*toggle = gimp_preview_get_update (preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-30 22:10:26 +00:00
|
|
|
/**
|
|
|
|
* gimp_drawable_preview_new:
|
2004-08-30 22:49:06 +00:00
|
|
|
* @drawable: a #GimpDrawable
|
2004-09-01 14:01:10 +00:00
|
|
|
* @toggle: pointer to a #gboolean variable to sync with the "Preview"
|
|
|
|
* check-button or %NULL
|
2004-08-30 22:10:26 +00:00
|
|
|
*
|
2004-09-01 14:01:10 +00:00
|
|
|
* Creates a new #GimpDrawablePreview widget for @drawable. If
|
|
|
|
* updating the preview takes considerable time, you will want to
|
|
|
|
* store the state of the "Preview" check-button in the plug-in
|
|
|
|
* data. For convenience you can pass a pointer to the #gboolean as
|
|
|
|
* @toggle.
|
2004-08-30 22:10:26 +00:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the new #GimpDrawablePreview widget.
|
2004-08-30 22:49:06 +00:00
|
|
|
*
|
|
|
|
* Since: GIMP 2.2
|
2004-08-30 22:10:26 +00:00
|
|
|
**/
|
|
|
|
GtkWidget *
|
2004-09-01 14:01:10 +00:00
|
|
|
gimp_drawable_preview_new (GimpDrawable *drawable,
|
|
|
|
gboolean *toggle)
|
2004-08-30 22:10:26 +00:00
|
|
|
{
|
2004-08-31 19:58:25 +00:00
|
|
|
GimpDrawablePreview *preview;
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-08-30 22:49:06 +00:00
|
|
|
g_return_val_if_fail (drawable != NULL, NULL);
|
|
|
|
|
2004-08-31 19:58:25 +00:00
|
|
|
preview = g_object_new (GIMP_TYPE_DRAWABLE_PREVIEW, NULL);
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-09-01 14:01:10 +00:00
|
|
|
if (toggle)
|
|
|
|
{
|
|
|
|
gimp_preview_set_update (GIMP_PREVIEW (preview), *toggle);
|
2004-08-30 22:49:06 +00:00
|
|
|
|
2004-09-01 14:01:10 +00:00
|
|
|
g_signal_connect (preview, "notify::update",
|
|
|
|
G_CALLBACK (gimp_drawable_preview_notify_update),
|
|
|
|
toggle);
|
|
|
|
}
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-08-31 19:58:25 +00:00
|
|
|
gimp_drawable_preview_set_drawable (preview, drawable);
|
2004-08-30 22:10:26 +00:00
|
|
|
|
|
|
|
return GTK_WIDGET (preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_drawable_preview_draw:
|
2004-08-31 09:16:47 +00:00
|
|
|
* @preview: a #GimpDrawablePreview widget
|
|
|
|
* @buf:
|
2004-08-30 22:10:26 +00:00
|
|
|
*
|
2004-08-31 09:16:47 +00:00
|
|
|
* Since: GIMP 2.2
|
2004-08-30 22:10:26 +00:00
|
|
|
**/
|
|
|
|
void
|
2004-08-31 09:16:47 +00:00
|
|
|
gimp_drawable_preview_draw (GimpDrawablePreview *preview,
|
2004-08-30 22:49:06 +00:00
|
|
|
guchar *buf)
|
2004-08-30 22:10:26 +00:00
|
|
|
{
|
2004-08-31 09:16:47 +00:00
|
|
|
GimpPreview *gimp_preview;
|
2004-08-30 22:49:06 +00:00
|
|
|
GimpDrawable *drawable;
|
|
|
|
|
2004-08-31 09:16:47 +00:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE_PREVIEW (preview));
|
2004-09-03 11:18:56 +00:00
|
|
|
g_return_if_fail (preview->drawable != NULL);
|
2004-08-30 22:49:06 +00:00
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
|
2004-08-31 09:16:47 +00:00
|
|
|
gimp_preview = GIMP_PREVIEW (preview);
|
|
|
|
drawable = preview->drawable;
|
2004-08-30 22:10:26 +00:00
|
|
|
|
2004-08-31 09:16:47 +00:00
|
|
|
gimp_preview_area_draw (GIMP_PREVIEW_AREA (gimp_preview->area),
|
|
|
|
0, 0, gimp_preview->width, gimp_preview->height,
|
2004-08-30 22:49:06 +00:00
|
|
|
gimp_drawable_type (drawable->drawable_id),
|
2004-08-30 22:10:26 +00:00
|
|
|
buf,
|
2004-08-31 09:16:47 +00:00
|
|
|
gimp_preview->width * drawable->bpp);
|
2004-08-30 22:10:26 +00:00
|
|
|
}
|