2004-09-29 07:23:09 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpaspectpreview.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"
|
|
|
|
|
|
|
|
#include "gimpuitypes.h"
|
|
|
|
|
|
|
|
#include "gimp.h"
|
|
|
|
|
|
|
|
#include "libgimp-intl.h"
|
|
|
|
|
|
|
|
#include "gimpaspectpreview.h"
|
|
|
|
|
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_DRAWABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void gimp_aspect_preview_get_property (GObject *object,
|
2006-04-12 18:53:28 +08:00
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
2006-02-26 08:37:50 +08:00
|
|
|
static void gimp_aspect_preview_set_property (GObject *object,
|
2006-04-12 18:53:28 +08:00
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
2006-02-26 08:37:50 +08:00
|
|
|
static void gimp_aspect_preview_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *prev_style);
|
|
|
|
static void gimp_aspect_preview_draw (GimpPreview *preview);
|
|
|
|
static void gimp_aspect_preview_draw_buffer (GimpPreview *preview,
|
|
|
|
const guchar *buffer,
|
|
|
|
gint rowstride);
|
|
|
|
|
|
|
|
static void gimp_aspect_preview_set_drawable (GimpAspectPreview *preview,
|
2006-04-12 18:53:28 +08:00
|
|
|
GimpDrawable *drawable);
|
2004-09-29 23:33:02 +08:00
|
|
|
|
2004-09-29 07:23:09 +08:00
|
|
|
|
2005-12-21 04:35:23 +08:00
|
|
|
G_DEFINE_TYPE (GimpAspectPreview, gimp_aspect_preview, GIMP_TYPE_PREVIEW);
|
2004-09-29 07:23:09 +08:00
|
|
|
|
2005-12-21 04:35:23 +08:00
|
|
|
#define parent_class gimp_aspect_preview_parent_class
|
2004-09-29 07:23:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_aspect_preview_class_init (GimpAspectPreviewClass *klass)
|
|
|
|
{
|
2006-02-26 08:37:50 +08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2004-09-29 07:23:09 +08:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
GimpPreviewClass *preview_class = GIMP_PREVIEW_CLASS (klass);
|
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
object_class->get_property = gimp_aspect_preview_get_property;
|
|
|
|
object_class->set_property = gimp_aspect_preview_set_property;
|
|
|
|
|
2004-09-29 23:33:02 +08:00
|
|
|
widget_class->style_set = gimp_aspect_preview_style_set;
|
|
|
|
|
|
|
|
preview_class->draw = gimp_aspect_preview_draw;
|
|
|
|
preview_class->draw_buffer = gimp_aspect_preview_draw_buffer;
|
2006-02-26 08:37:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GimpAspectPreview:drawable:
|
|
|
|
*
|
|
|
|
* Since: GIMP 2.4
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class, PROP_DRAWABLE,
|
|
|
|
g_param_spec_pointer ("drawable", NULL, NULL,
|
2006-04-12 18:53:28 +08:00
|
|
|
GIMP_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY));
|
2004-09-29 07:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_aspect_preview_init (GimpAspectPreview *preview)
|
|
|
|
{
|
|
|
|
g_object_set (GIMP_PREVIEW (preview)->area,
|
|
|
|
"check-size", gimp_check_size (),
|
|
|
|
"check-type", gimp_check_type (),
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
static void
|
|
|
|
gimp_aspect_preview_get_property (GObject *object,
|
2006-04-12 18:53:28 +08:00
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2006-02-26 08:37:50 +08:00
|
|
|
{
|
|
|
|
GimpAspectPreview *preview = GIMP_ASPECT_PREVIEW (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_DRAWABLE:
|
|
|
|
g_value_set_pointer (value, preview->drawable);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_aspect_preview_set_property (GObject *object,
|
2006-04-12 18:53:28 +08:00
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2006-02-26 08:37:50 +08:00
|
|
|
{
|
|
|
|
GimpAspectPreview *preview = GIMP_ASPECT_PREVIEW (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_DRAWABLE:
|
|
|
|
gimp_aspect_preview_set_drawable (preview,
|
2006-04-12 18:53:28 +08:00
|
|
|
g_value_get_pointer (value));
|
2006-02-26 08:37:50 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-29 07:23:09 +08:00
|
|
|
static void
|
|
|
|
gimp_aspect_preview_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *prev_style)
|
|
|
|
{
|
2004-09-29 23:33:02 +08:00
|
|
|
GimpPreview *preview = GIMP_PREVIEW (widget);
|
|
|
|
GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;
|
|
|
|
gint size;
|
|
|
|
gint width, height;
|
2004-09-29 07:23:09 +08:00
|
|
|
|
|
|
|
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,
|
|
|
|
NULL);
|
2004-09-29 23:33:02 +08:00
|
|
|
width = gimp_drawable_width (drawable->drawable_id);
|
|
|
|
height = gimp_drawable_height (drawable->drawable_id);
|
2004-09-29 07:23:09 +08:00
|
|
|
|
|
|
|
if (width > height)
|
|
|
|
{
|
2004-09-29 23:33:02 +08:00
|
|
|
preview->width = MIN (width, size);
|
|
|
|
preview->height = (height * preview->width) / width;
|
2004-09-29 07:23:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-09-29 23:33:02 +08:00
|
|
|
preview->height = MIN (height, size);
|
|
|
|
preview->width = (width * preview->height) / height;
|
2004-09-29 07:23:09 +08:00
|
|
|
}
|
2004-09-29 23:33:02 +08:00
|
|
|
|
|
|
|
gtk_widget_set_size_request (preview->area,
|
|
|
|
preview->width, preview->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_aspect_preview_draw (GimpPreview *preview)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_ASPECT_PREVIEW (preview));
|
|
|
|
|
|
|
|
gimp_preview_area_fill (GIMP_PREVIEW_AREA (preview->area),
|
|
|
|
0, 0,
|
|
|
|
preview->width,
|
|
|
|
preview->height,
|
|
|
|
0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_aspect_preview_draw_buffer (GimpPreview *preview,
|
|
|
|
const guchar *buffer,
|
|
|
|
gint rowstride)
|
|
|
|
{
|
2004-10-05 01:40:45 +08:00
|
|
|
GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;
|
|
|
|
gint32 image_id;
|
|
|
|
|
|
|
|
image_id = gimp_drawable_get_image (drawable->drawable_id);
|
2004-09-29 23:33:02 +08:00
|
|
|
|
2004-10-05 01:40:45 +08:00
|
|
|
if (gimp_selection_is_empty (image_id))
|
|
|
|
{
|
|
|
|
gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview->area),
|
|
|
|
0, 0,
|
|
|
|
preview->width, preview->height,
|
|
|
|
gimp_drawable_type (drawable->drawable_id),
|
|
|
|
buffer,
|
|
|
|
rowstride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
guchar *sel;
|
|
|
|
guchar *src;
|
|
|
|
gint selection_id;
|
2004-10-05 02:23:43 +08:00
|
|
|
gint width, height;
|
|
|
|
gint bpp;
|
2004-10-05 01:40:45 +08:00
|
|
|
|
|
|
|
selection_id = gimp_image_get_selection (image_id);
|
|
|
|
|
|
|
|
width = preview->width;
|
|
|
|
height = preview->height;
|
2004-10-05 02:23:43 +08:00
|
|
|
|
2004-10-05 01:40:45 +08:00
|
|
|
src = gimp_drawable_get_thumbnail_data (drawable->drawable_id,
|
|
|
|
&width, &height, &bpp);
|
|
|
|
sel = gimp_drawable_get_thumbnail_data (selection_id,
|
|
|
|
&width, &height, &bpp);
|
|
|
|
|
|
|
|
gimp_preview_area_mask (GIMP_PREVIEW_AREA (preview->area),
|
|
|
|
0, 0, preview->width, preview->height,
|
|
|
|
gimp_drawable_type (drawable->drawable_id),
|
|
|
|
src, width * drawable->bpp,
|
|
|
|
buffer, rowstride,
|
|
|
|
sel, width);
|
|
|
|
|
|
|
|
g_free (sel);
|
|
|
|
g_free (src);
|
|
|
|
}
|
2004-09-29 07:23:09 +08:00
|
|
|
}
|
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
static void
|
|
|
|
gimp_aspect_preview_set_drawable (GimpAspectPreview *preview,
|
2006-04-12 18:53:28 +08:00
|
|
|
GimpDrawable *drawable)
|
2006-02-26 08:37:50 +08:00
|
|
|
{
|
|
|
|
gint width, height;
|
|
|
|
gint max_width, max_height;
|
|
|
|
|
|
|
|
preview->drawable = drawable;
|
|
|
|
|
|
|
|
width = gimp_drawable_width (drawable->drawable_id);
|
|
|
|
height = gimp_drawable_height (drawable->drawable_id);
|
|
|
|
|
|
|
|
if (width > height)
|
|
|
|
{
|
|
|
|
max_width = MIN (width, 512);
|
|
|
|
max_height = (height * max_width) / width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_height = MIN (height, 512);
|
|
|
|
max_width = (width * max_height) / height;
|
|
|
|
}
|
|
|
|
gimp_preview_set_bounds (GIMP_PREVIEW (preview),
|
2006-04-12 18:53:28 +08:00
|
|
|
0, 0, max_width, max_height);
|
2006-02-26 08:37:50 +08:00
|
|
|
|
|
|
|
if (height > 0)
|
|
|
|
g_object_set (GIMP_PREVIEW (preview)->frame,
|
2006-04-12 18:53:28 +08:00
|
|
|
"ratio", (gdouble) width / (gdouble) height,
|
|
|
|
NULL);
|
2006-02-26 08:37:50 +08:00
|
|
|
}
|
|
|
|
|
2004-10-27 19:38:15 +08:00
|
|
|
static void
|
|
|
|
gimp_aspect_preview_notify_update (GimpPreview *preview,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gboolean *toggle)
|
|
|
|
{
|
|
|
|
*toggle = gimp_preview_get_update (preview);
|
|
|
|
}
|
|
|
|
|
2005-10-05 21:50:09 +08:00
|
|
|
/**
|
|
|
|
* gimp_aspect_preview_new:
|
|
|
|
* @drawable: a #GimpDrawable
|
|
|
|
* @toggle: pointer to a #gboolean variable to sync with the "Preview"
|
|
|
|
* check-button or %NULL
|
|
|
|
*
|
|
|
|
* Creates a new #GimpAspectPreview widget for @drawable. See also
|
|
|
|
* gimp_drawable_preview_new().
|
|
|
|
*
|
|
|
|
* Since: GIMP 2.2
|
|
|
|
**/
|
2004-09-29 07:23:09 +08:00
|
|
|
GtkWidget *
|
|
|
|
gimp_aspect_preview_new (GimpDrawable *drawable,
|
|
|
|
gboolean *toggle)
|
|
|
|
{
|
2006-02-26 08:37:50 +08:00
|
|
|
GtkWidget *preview;
|
2004-09-29 07:23:09 +08:00
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
g_return_val_if_fail (drawable != NULL, NULL);
|
2004-09-29 07:23:09 +08:00
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
preview = g_object_new (GIMP_TYPE_ASPECT_PREVIEW,
|
2006-04-12 18:53:28 +08:00
|
|
|
"drawable", drawable,
|
|
|
|
NULL);
|
2004-09-29 07:23:09 +08:00
|
|
|
|
2004-10-27 19:38:15 +08:00
|
|
|
if (toggle)
|
|
|
|
{
|
|
|
|
gimp_preview_set_update (GIMP_PREVIEW (preview), *toggle);
|
|
|
|
|
|
|
|
g_signal_connect (preview, "notify::update",
|
|
|
|
G_CALLBACK (gimp_aspect_preview_notify_update),
|
|
|
|
toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-26 08:37:50 +08:00
|
|
|
return preview;
|
2004-09-29 07:23:09 +08:00
|
|
|
}
|