/*
* e-image-chooser.c
*
* This program 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) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see
*
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#include
#include
#include
#include
#include "e-image-chooser.h"
#include "e-util/e-util.h"
#define E_IMAGE_CHOOSER_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_IMAGE_CHOOSER, EImageChooserPrivate))
struct _EImageChooserPrivate {
GtkWidget *frame;
GtkWidget *image;
gchar *image_buf;
gint image_buf_size;
gint image_width;
gint image_height;
};
enum {
CHANGED,
LAST_SIGNAL
};
static gpointer parent_class;
static guint signals[LAST_SIGNAL];
#define URI_LIST_TYPE "text/uri-list"
static gboolean
set_image_from_data (EImageChooser *chooser,
gchar *data,
gint length)
{
GdkPixbufLoader *loader;
GdkPixbuf *pixbuf;
gfloat scale;
gint new_height;
gint new_width;
loader = gdk_pixbuf_loader_new ();
gdk_pixbuf_loader_write (loader, (guchar *) data, length, NULL);
gdk_pixbuf_loader_close (loader, NULL);
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (pixbuf)
g_object_ref (pixbuf);
g_object_unref (loader);
if (pixbuf == NULL)
return FALSE;
new_height = gdk_pixbuf_get_height (pixbuf);
new_width = gdk_pixbuf_get_width (pixbuf);
if (chooser->priv->image_height == 0
&& chooser->priv->image_width == 0) {
scale = 1.0;
} else if (chooser->priv->image_height < new_height
|| chooser->priv->image_width < new_width) {
/* we need to scale down */
if (new_height > new_width)
scale = (gfloat)chooser->priv->image_height / new_height;
else
scale = (gfloat)chooser->priv->image_width / new_width;
} else {
/* we need to scale up */
if (new_height > new_width)
scale = (gfloat)new_height / chooser->priv->image_height;
else
scale = (gfloat)new_width / chooser->priv->image_width;
}
if (scale == 1.0) {
gtk_image_set_from_pixbuf (
GTK_IMAGE (chooser->priv->image), pixbuf);
chooser->priv->image_width = new_width;
chooser->priv->image_height = new_height;
} else {
GdkPixbuf *scaled;
GdkPixbuf *composite;
new_width *= scale;
new_height *= scale;
new_width = MIN (new_width, chooser->priv->image_width);
new_height = MIN (new_height, chooser->priv->image_height);
scaled = gdk_pixbuf_scale_simple (
pixbuf, new_width, new_height,
GDK_INTERP_BILINEAR);
composite = gdk_pixbuf_new (
GDK_COLORSPACE_RGB, TRUE,
gdk_pixbuf_get_bits_per_sample (pixbuf),
chooser->priv->image_width,
chooser->priv->image_height);
gdk_pixbuf_fill (composite, 0x00000000);
gdk_pixbuf_copy_area (
scaled, 0, 0, new_width, new_height,
composite,
chooser->priv->image_width / 2 - new_width / 2,
chooser->priv->image_height / 2 - new_height / 2);
gtk_image_set_from_pixbuf (
GTK_IMAGE (chooser->priv->image), composite);
g_object_unref (scaled);
g_object_unref (composite);
}
g_object_unref (pixbuf);
g_free (chooser->priv->image_buf);
chooser->priv->image_buf = data;
chooser->priv->image_buf_size = length;
g_signal_emit (chooser, signals[CHANGED], 0);
return TRUE;
}
static gboolean
image_drag_motion_cb (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
guint time,
EImageChooser *chooser)
{
GtkFrame *frame;
GList *p;
frame = GTK_FRAME (chooser->priv->frame);
for (p = context->targets; p != NULL; p = p->next) {
gchar *possible_type;
possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
if (!strcmp (possible_type, URI_LIST_TYPE)) {
g_free (possible_type);
gdk_drag_status (context, GDK_ACTION_COPY, time);
gtk_frame_set_shadow_type (frame, GTK_SHADOW_IN);
return TRUE;
}
g_free (possible_type);
}
gtk_frame_set_shadow_type (frame, GTK_SHADOW_NONE);
return FALSE;
}
static void
image_drag_leave_cb (GtkWidget *widget,
GdkDragContext *context,
guint time,
EImageChooser *chooser)
{
GtkFrame *frame;
frame = GTK_FRAME (chooser->priv->frame);
gtk_frame_set_shadow_type (frame, GTK_SHADOW_NONE);
}
static gboolean
image_drag_drop_cb (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
guint time,
EImageChooser *chooser)
{
GtkFrame *frame;
GList *p;
frame = GTK_FRAME (chooser->priv->frame);
if (context->targets == NULL) {
gtk_frame_set_shadow_type (frame, GTK_SHADOW_NONE);
return FALSE;
}
for (p = context->targets; p != NULL; p = p->next) {
gchar *possible_type;
possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
if (!strcmp (possible_type, URI_LIST_TYPE)) {
g_free (possible_type);
gtk_drag_get_data (
widget, context,
GDK_POINTER_TO_ATOM (p->data), time);
gtk_frame_set_shadow_type (frame, GTK_SHADOW_NONE);
return TRUE;
}
g_free (possible_type);
}
gtk_frame_set_shadow_type (frame, GTK_SHADOW_NONE);
return FALSE;
}
static void
image_drag_data_received_cb (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time,
EImageChooser *chooser)
{
gboolean handled = FALSE;
gchar **uris;
gchar *buf = NULL;
gsize read = 0;
GError *error = NULL;
uris = gtk_selection_data_get_uris (selection_data);
if (uris == NULL)
goto exit;
if (e_util_read_file (uris[0], TRUE, &buf, &read, &error) && read > 0 && buf)
handled = set_image_from_data (chooser, buf, read);
g_free (buf);
g_strfreev (uris);
if (error) {
g_warning ("%s", error->message);
g_error_free (error);
}
exit:
gtk_drag_finish (context, handled, FALSE, time);
}
static void
image_chooser_dispose (GObject *object)
{
EImageChooserPrivate *priv;
priv = E_IMAGE_CHOOSER_GET_PRIVATE (object);
if (priv->frame != NULL) {
g_object_unref (priv->frame);
priv->frame = NULL;
}
if (priv->image != NULL) {
g_object_unref (priv->image);
priv->image = NULL;
}
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
image_chooser_finalize (GObject *object)
{
EImageChooserPrivate *priv;
priv = E_IMAGE_CHOOSER_GET_PRIVATE (object);
g_free (priv->image_buf);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
e_image_chooser_class_init (EImageChooserClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (EImageChooserPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->dispose = image_chooser_dispose;
object_class->finalize = image_chooser_finalize;
signals[CHANGED] = g_signal_new (
"changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EImageChooserClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
e_image_chooser_init (EImageChooser *chooser)
{
GtkWidget *container;
GtkWidget *widget;
chooser->priv = E_IMAGE_CHOOSER_GET_PRIVATE (chooser);
container = GTK_WIDGET (chooser);
widget = gtk_frame_new ("");
gtk_frame_set_shadow_type (GTK_FRAME (widget), GTK_SHADOW_NONE);
gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
chooser->priv->frame = g_object_ref (widget);
gtk_widget_show (widget);
container = widget;
widget = gtk_alignment_new (0, 0, 0, 0);
gtk_container_add (GTK_CONTAINER (container), widget);
gtk_widget_show (widget);
container = widget;
widget = gtk_image_new ();
gtk_container_add (GTK_CONTAINER (container), widget);
chooser->priv->image = g_object_ref (widget);
gtk_widget_show (widget);
gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
gtk_drag_dest_add_uri_targets (widget);
g_signal_connect (
widget, "drag-motion",
G_CALLBACK (image_drag_motion_cb), chooser);
g_signal_connect (
widget, "drag-leave",
G_CALLBACK (image_drag_leave_cb), chooser);
g_signal_connect (
widget, "drag-drop",
G_CALLBACK (image_drag_drop_cb), chooser);
g_signal_connect (
widget, "drag-data-received",
G_CALLBACK (image_drag_data_received_cb), chooser);
}
GType
e_image_chooser_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static const GTypeInfo type_info = {
sizeof (EImageChooserClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) e_image_chooser_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (EImageChooser),
0, /* n_preallocs */
(GInstanceInitFunc) e_image_chooser_init,
NULL /* value_table */
};
type = g_type_register_static (
GTK_TYPE_VBOX, "EImageChooser", &type_info, 0);
}
return type;
}
GtkWidget *
e_image_chooser_new (void)
{
return g_object_new (E_TYPE_IMAGE_CHOOSER, NULL);
}
gboolean
e_image_chooser_set_from_file (EImageChooser *chooser,
const gchar *filename)
{
gchar *data;
gsize data_length;
g_return_val_if_fail (E_IS_IMAGE_CHOOSER (chooser), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
if (!g_file_get_contents (filename, &data, &data_length, NULL))
return FALSE;
if (!set_image_from_data (chooser, data, data_length))
g_free (data);
return TRUE;
}
gboolean
e_image_chooser_get_image_data (EImageChooser *chooser,
gchar **data,
gsize *data_length)
{
g_return_val_if_fail (E_IS_IMAGE_CHOOSER (chooser), FALSE);
g_return_val_if_fail (data != NULL, FALSE);
g_return_val_if_fail (data_length != NULL, FALSE);
*data_length = chooser->priv->image_buf_size;
*data = g_malloc (*data_length);
memcpy (*data, chooser->priv->image_buf, *data_length);
return TRUE;
}
gboolean
e_image_chooser_set_image_data (EImageChooser *chooser,
gchar *data,
gsize data_length)
{
gchar *buf;
g_return_val_if_fail (E_IS_IMAGE_CHOOSER (chooser), FALSE);
g_return_val_if_fail (data != NULL, FALSE);
/* yuck, a copy... */
buf = g_malloc (data_length);
memcpy (buf, data, data_length);
if (!set_image_from_data (chooser, buf, data_length)) {
g_free (buf);
return FALSE;
}
return TRUE;
}