2004-08-31 06:10:26 +08: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 17:16:47 +08:00
|
|
|
#include "gimpuitypes.h"
|
|
|
|
|
|
|
|
#include "gimp.h"
|
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
#include "gimpdrawablepreview.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define PREVIEW_SIZE (128)
|
|
|
|
|
2004-08-31 22:29:25 +08:00
|
|
|
static void gimp_drawable_preview_class_init (GimpDrawablePreviewClass *klass);
|
|
|
|
static void gimp_drawable_preview_draw_original (GimpPreview *preview);
|
2004-08-31 06:10:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
static GimpPreviewClass *parent_class = NULL;
|
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
GType
|
|
|
|
gimp_drawable_preview_get_type (void)
|
|
|
|
{
|
2004-08-31 06:49:06 +08:00
|
|
|
static GType preview_type = 0;
|
2004-08-31 06:10:26 +08:00
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
if (!preview_type)
|
2004-08-31 06:10:26 +08:00
|
|
|
{
|
|
|
|
static const GTypeInfo drawable_preview_info =
|
|
|
|
{
|
|
|
|
sizeof (GimpDrawablePreviewClass),
|
2004-08-31 06:49:06 +08:00
|
|
|
(GBaseInitFunc) NULL,
|
2004-08-31 06:10:26 +08:00
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gimp_drawable_preview_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GimpDrawablePreview),
|
|
|
|
0, /* n_preallocs */
|
2004-08-31 06:49:06 +08:00
|
|
|
NULL /* instance_init */
|
2004-08-31 06:10:26 +08:00
|
|
|
};
|
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
preview_type = g_type_register_static (GIMP_TYPE_PREVIEW,
|
|
|
|
"GimpDrawablePreview",
|
|
|
|
&drawable_preview_info, 0);
|
2004-08-31 06:10:26 +08:00
|
|
|
}
|
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
return preview_type;
|
2004-08-31 06:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_drawable_preview_class_init (GimpDrawablePreviewClass *klass)
|
|
|
|
{
|
2004-08-31 06:49:06 +08:00
|
|
|
GimpPreviewClass *preview_class = GIMP_PREVIEW_CLASS (klass);
|
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2004-08-31 22:29:25 +08:00
|
|
|
preview_class->draw = gimp_drawable_preview_draw_original;
|
2004-08-31 06:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-08-31 22:29:25 +08:00
|
|
|
gimp_drawable_preview_draw_original (GimpPreview *preview)
|
2004-08-31 06:10:26 +08:00
|
|
|
{
|
2004-08-31 06:49:06 +08:00
|
|
|
GimpDrawablePreview *drawable_preview = GIMP_DRAWABLE_PREVIEW (preview);
|
|
|
|
GimpDrawable *drawable = drawable_preview->drawable;
|
2004-08-31 06:10:26 +08:00
|
|
|
guchar *buffer;
|
2004-08-31 06:49:06 +08:00
|
|
|
GimpPixelRgn srcPR;
|
|
|
|
guint rowstride;
|
2004-08-31 06:10:26 +08:00
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
rowstride = preview->width * drawable->bpp;
|
2004-08-31 06:10:26 +08: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-31 06:49:06 +08:00
|
|
|
|
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable,
|
2004-08-31 06:10:26 +08:00
|
|
|
preview->xoff + preview->xmin,
|
|
|
|
preview->yoff + preview->ymin,
|
|
|
|
preview->width, preview->height,
|
|
|
|
FALSE, FALSE);
|
2004-08-31 06:49:06 +08:00
|
|
|
|
2004-08-31 06:10:26 +08: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-31 06:49:06 +08:00
|
|
|
gimp_drawable_type (drawable->drawable_id),
|
2004-08-31 06:10:26 +08:00
|
|
|
buffer,
|
|
|
|
rowstride);
|
|
|
|
g_free (buffer);
|
|
|
|
}
|
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
/**
|
|
|
|
* gimp_drawable_preview_new:
|
2004-08-31 06:49:06 +08:00
|
|
|
* @drawable: a #GimpDrawable
|
2004-08-31 06:10:26 +08:00
|
|
|
*
|
2004-08-31 06:49:06 +08:00
|
|
|
* Creates a new #GimpDrawablePreview widget for @drawable.
|
2004-08-31 06:10:26 +08:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the new #GimpDrawablePreview widget.
|
2004-08-31 06:49:06 +08:00
|
|
|
*
|
|
|
|
* Since: GIMP 2.2
|
2004-08-31 06:10:26 +08:00
|
|
|
**/
|
|
|
|
GtkWidget *
|
|
|
|
gimp_drawable_preview_new (GimpDrawable *drawable)
|
|
|
|
{
|
|
|
|
GimpDrawablePreview *drawable_preview;
|
|
|
|
GimpPreview *preview;
|
|
|
|
gint sel_width, sel_height;
|
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
g_return_val_if_fail (drawable != NULL, NULL);
|
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
drawable_preview = g_object_new (GIMP_TYPE_DRAWABLE_PREVIEW, NULL);
|
|
|
|
drawable_preview->drawable = drawable;
|
2004-08-31 06:49:06 +08:00
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
preview = GIMP_PREVIEW (drawable_preview);
|
|
|
|
|
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id,
|
|
|
|
&preview->xmin, &preview->ymin,
|
|
|
|
&preview->xmax, &preview->ymax);
|
|
|
|
|
|
|
|
sel_width = preview->xmax - preview->xmin;
|
|
|
|
sel_height = preview->ymax - preview->ymin;
|
2004-08-31 06:49:06 +08:00
|
|
|
preview->width = MIN (sel_width, PREVIEW_SIZE);
|
2004-08-31 06:10:26 +08:00
|
|
|
preview->height = MIN (sel_height, PREVIEW_SIZE);
|
|
|
|
|
|
|
|
gtk_range_set_increments (GTK_RANGE (preview->hscr),
|
|
|
|
1.0,
|
|
|
|
MIN (preview->width, sel_width));
|
|
|
|
gtk_range_set_range (GTK_RANGE (preview->hscr),
|
2004-08-31 22:29:25 +08:00
|
|
|
0, sel_width - preview->width - 1);
|
2004-08-31 06:10:26 +08:00
|
|
|
gtk_range_set_increments (GTK_RANGE (preview->vscr),
|
|
|
|
1.0,
|
|
|
|
MIN (preview->height, sel_height));
|
|
|
|
gtk_range_set_range (GTK_RANGE (preview->vscr),
|
2004-08-31 22:29:25 +08:00
|
|
|
0, sel_height - preview->height - 1);
|
2004-08-31 06:10:26 +08:00
|
|
|
|
|
|
|
gtk_widget_set_size_request (preview->area,
|
|
|
|
preview->width, preview->height);
|
2004-08-31 06:49:06 +08:00
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
return GTK_WIDGET (preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_drawable_preview_new_with_toggle:
|
2004-08-31 06:49:06 +08:00
|
|
|
* @drawable: a #GimpDrawable
|
|
|
|
* @toggle:
|
2004-08-31 06:10:26 +08:00
|
|
|
*
|
2004-08-31 06:49:06 +08:00
|
|
|
* Creates a new #GimpDrawablePreview widget for @drawable.
|
2004-08-31 06:10:26 +08:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the new #GimpDrawablePreview widget.
|
2004-08-31 06:49:06 +08:00
|
|
|
*
|
|
|
|
* Since: GIMP 2.2
|
2004-08-31 06:10:26 +08:00
|
|
|
**/
|
|
|
|
GtkWidget *
|
|
|
|
gimp_drawable_preview_new_with_toggle (GimpDrawable *drawable,
|
|
|
|
gboolean *toggle)
|
|
|
|
{
|
|
|
|
GimpDrawablePreview *drawable_preview;
|
|
|
|
GimpPreview *preview;
|
|
|
|
gint sel_width, sel_height;
|
|
|
|
|
2004-08-31 06:49:06 +08:00
|
|
|
g_return_val_if_fail (drawable != NULL, NULL);
|
|
|
|
g_return_val_if_fail (toggle != NULL, NULL);
|
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
drawable_preview = g_object_new (GIMP_TYPE_DRAWABLE_PREVIEW,
|
2004-08-31 22:29:25 +08:00
|
|
|
"show_update_toggle", TRUE,
|
|
|
|
"update", *toggle,
|
2004-08-31 06:10:26 +08:00
|
|
|
NULL);
|
2004-08-31 06:49:06 +08:00
|
|
|
|
2004-08-31 06:10:26 +08:00
|
|
|
drawable_preview->drawable = drawable;
|
|
|
|
preview = GIMP_PREVIEW (drawable_preview);
|
|
|
|
|
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id,
|
|
|
|
&preview->xmin, &preview->ymin,
|
|
|
|
&preview->xmax, &preview->ymax);
|
|
|
|
|
|
|
|
sel_width = preview->xmax - preview->xmin;
|
|
|
|
sel_height = preview->ymax - preview->ymin;
|
2004-08-31 06:49:06 +08:00
|
|
|
preview->width = MIN (sel_width, PREVIEW_SIZE);
|
2004-08-31 06:10:26 +08:00
|
|
|
preview->height = MIN (sel_height, PREVIEW_SIZE);
|
|
|
|
|
|
|
|
gtk_range_set_increments (GTK_RANGE (preview->hscr),
|
|
|
|
1.0,
|
|
|
|
MIN (preview->width, sel_width));
|
|
|
|
gtk_range_set_range (GTK_RANGE (preview->hscr),
|
2004-08-31 22:29:25 +08:00
|
|
|
0, sel_width -preview->width - 1);
|
2004-08-31 06:10:26 +08:00
|
|
|
gtk_range_set_increments (GTK_RANGE (preview->vscr),
|
|
|
|
1.0,
|
|
|
|
MIN (preview->height, sel_height));
|
|
|
|
gtk_range_set_range (GTK_RANGE (preview->vscr),
|
2004-08-31 22:29:25 +08:00
|
|
|
0, sel_height - preview->height - 1);
|
2004-08-31 06:10:26 +08:00
|
|
|
|
|
|
|
gtk_widget_set_size_request (preview->area,
|
|
|
|
preview->width, preview->height);
|
|
|
|
|
|
|
|
g_signal_connect (preview->toggle_update, "toggled",
|
|
|
|
G_CALLBACK (gimp_toggle_button_update), toggle);
|
|
|
|
|
|
|
|
return GTK_WIDGET (preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_drawable_preview_draw:
|
2004-08-31 17:16:47 +08:00
|
|
|
* @preview: a #GimpDrawablePreview widget
|
|
|
|
* @buf:
|
2004-08-31 06:10:26 +08:00
|
|
|
*
|
2004-08-31 17:16:47 +08:00
|
|
|
* Since: GIMP 2.2
|
2004-08-31 06:10:26 +08:00
|
|
|
**/
|
|
|
|
void
|
2004-08-31 17:16:47 +08:00
|
|
|
gimp_drawable_preview_draw (GimpDrawablePreview *preview,
|
2004-08-31 06:49:06 +08:00
|
|
|
guchar *buf)
|
2004-08-31 06:10:26 +08:00
|
|
|
{
|
2004-08-31 17:16:47 +08:00
|
|
|
GimpPreview *gimp_preview;
|
2004-08-31 06:49:06 +08:00
|
|
|
GimpDrawable *drawable;
|
|
|
|
|
2004-08-31 17:16:47 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE_PREVIEW (preview));
|
2004-08-31 06:49:06 +08:00
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
|
2004-08-31 17:16:47 +08:00
|
|
|
gimp_preview = GIMP_PREVIEW (preview);
|
|
|
|
drawable = preview->drawable;
|
2004-08-31 06:10:26 +08:00
|
|
|
|
2004-08-31 17:16:47 +08:00
|
|
|
gimp_preview_area_draw (GIMP_PREVIEW_AREA (gimp_preview->area),
|
|
|
|
0, 0, gimp_preview->width, gimp_preview->height,
|
2004-08-31 06:49:06 +08:00
|
|
|
gimp_drawable_type (drawable->drawable_id),
|
2004-08-31 06:10:26 +08:00
|
|
|
buf,
|
2004-08-31 17:16:47 +08:00
|
|
|
gimp_preview->width * drawable->bpp);
|
2004-08-31 06:10:26 +08:00
|
|
|
}
|
|
|
|
|