/*
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <glib.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtkpixmap.h>

#if HAVE_GDK_PIXBUF
#  include <gdk-pixbuf/gdk-pixbuf.h>
#else
#if HAVE_GDK_IMLIB
#  include <gdk_imlib.h>
#endif
#endif /* HAVE_GDK_PIXBUF */

#include "intl.h"
#include "mainwindow.h"
#include "prefs_common.h"
#include "procmime.h"
#include "imageview.h"
#include "utils.h"

static void get_resized_size	(gint	 w,
				 gint	 h,
				 gint	 aw,
				 gint	 ah,
				 gint	*sw,
				 gint	*sh);

static gint button_press_cb	(GtkWidget	*widget,
				 GdkEventButton	*event,
				 gpointer	 data);
static void size_allocate_cb	(GtkWidget	*widget,
				 GtkAllocation	*allocation,
				 gpointer	 data);

ImageView *imageview_create(void)
{
	ImageView *imageview;
	GtkWidget *scrolledwin;

	debug_print(_("Creating image view...\n"));
	imageview = g_new0(ImageView, 1);

	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_AUTOMATIC);
	gtk_widget_set_usize(scrolledwin, prefs_common.mainview_width, -1);

	gtk_signal_connect(GTK_OBJECT(scrolledwin), "button_press_event",
			   GTK_SIGNAL_FUNC(button_press_cb), imageview);
	gtk_signal_connect(GTK_OBJECT(scrolledwin), "size_allocate",
			   GTK_SIGNAL_FUNC(size_allocate_cb), imageview);

	gtk_widget_show_all(scrolledwin);

	imageview->scrolledwin  = scrolledwin;
	imageview->image        = NULL;
	imageview->resize       = FALSE;

	return imageview;
}

void imageview_init(ImageView *imageview)
{
#if HAVE_GDK_IMLIB
	gdk_imlib_set_cache_info(0, 0);
#endif
}

#if HAVE_GDK_PIXBUF
void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
			  const gchar *file, gboolean resize)
{
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *mask;
	gint avail_width;
	gint avail_height;
	gint new_width;
	gint new_height;

	g_return_if_fail(imageview != NULL);
	g_return_if_fail(imageview->scrolledwin != NULL);
	g_return_if_fail(imageview->scrolledwin->parent != NULL);

	if (file) {
		imageview_clear(imageview);
		pixbuf = gdk_pixbuf_new_from_file(file);
		imageview->image_data = pixbuf;
	} else {
		pixbuf = (GdkPixbuf *)imageview->image_data;
	}

	if (!pixbuf) {
		g_warning(_("Can't load the image."));	
		return;
	}

	imageview->resize = resize;

	if (resize) {
		GdkPixbuf *pixbuf_scaled;

		avail_width = imageview->scrolledwin->parent->allocation.width;
		avail_height = imageview->scrolledwin->parent->allocation.height;
		if (avail_width > 8) avail_width -= 8;
		if (avail_height > 8) avail_height -= 8;

		get_resized_size(gdk_pixbuf_get_width(pixbuf),
				 gdk_pixbuf_get_height(pixbuf),
				 avail_width, avail_height,
				 &new_width, &new_height);

		pixbuf_scaled = gdk_pixbuf_scale_simple
			(pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
		gdk_pixbuf_render_pixmap_and_mask(pixbuf_scaled,
						  &pixmap, &mask, 0);
		gdk_pixbuf_unref(pixbuf_scaled);
	} else
		gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);

	if (!imageview->image) {
		imageview->image = gtk_pixmap_new(pixmap, mask);

		gtk_scrolled_window_add_with_viewport
			(GTK_SCROLLED_WINDOW(imageview->scrolledwin),
			 imageview->image);
	} else
		gtk_pixmap_set(GTK_PIXMAP(imageview->image), pixmap, mask);

	gdk_pixmap_unref(pixmap);

	gtk_widget_show(imageview->image);
}
#else
#if HAVE_GDK_IMLIB
void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
			  const gchar *file, gboolean resize)
{
	GdkImlibImage *im;
	GdkPixmap *pixmap;
	GdkBitmap *mask;
	gint avail_width;
	gint avail_height;
	gint new_width;
	gint new_height;

	g_return_if_fail(imageview != NULL);
	g_return_if_fail(imageview->scrolledwin != NULL);
	g_return_if_fail(imageview->scrolledwin->parent != NULL);

	if (file) {
		imageview_clear(imageview);
		im = gdk_imlib_load_image((gchar *)file);
		imageview->image_data = im;
	} else {
		im = (GdkImlibImage *)imageview->image_data;
	}

	if (!im) {
		g_warning(_("Can't load the image."));	
		return;
	}

	imageview->resize = resize;

	if (resize) {
		avail_width = imageview->scrolledwin->parent->allocation.width;
		avail_height = imageview->scrolledwin->parent->allocation.height;
		if (avail_width > 8) avail_width -= 8;
		if (avail_height > 8) avail_height -= 8;

		get_resized_size(im->rgb_width, im->rgb_height,
				 avail_width, avail_height,
				 &new_width, &new_height);
		gdk_imlib_render(im, new_width, new_height);
	} else
		gdk_imlib_render(im, im->rgb_width, im->rgb_height);

	pixmap = gdk_imlib_move_image(im);
	mask = gdk_imlib_move_mask(im);

	if (!imageview->image) {
		imageview->image = gtk_pixmap_new(pixmap, mask);

		gtk_scrolled_window_add_with_viewport
			(GTK_SCROLLED_WINDOW(imageview->scrolledwin),
			 imageview->image);
	} else
		gtk_pixmap_set(GTK_PIXMAP(imageview->image), pixmap, mask);

	gdk_pixmap_unref(pixmap);

	gtk_widget_show(imageview->image);
}
#else
void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
			  const gchar *file, gboolean resize)
{
}
#endif /* HAVE_GDK_IMLIB */
#endif /* HAVE_GDK_PIXBUF */

void imageview_clear(ImageView *imageview)
{
	GtkAdjustment *hadj, *vadj;

	if (imageview->image)
		gtk_pixmap_set(GTK_PIXMAP(imageview->image), NULL, NULL);
	hadj = gtk_scrolled_window_get_hadjustment
		(GTK_SCROLLED_WINDOW(imageview->scrolledwin));
	gtk_adjustment_set_value(hadj, 0.0);
	vadj = gtk_scrolled_window_get_vadjustment
		(GTK_SCROLLED_WINDOW(imageview->scrolledwin));
	gtk_adjustment_set_value(vadj, 0.0);

	if (imageview->image_data) {
#if HAVE_GDK_PIXBUF
		gdk_pixbuf_unref((GdkPixbuf *)imageview->image_data);
#elif HAVE_GDK_IMLIB
		gdk_imlib_destroy_image((GdkImlibImage *)imageview->image_data);
#endif
		imageview->image_data = NULL;
	}
}

void imageview_destroy(ImageView *imageview)
{
	imageview_clear(imageview);
	g_free(imageview);
}

static void get_resized_size(gint w, gint h, gint aw, gint ah,
			     gint *sw, gint *sh)
{
	gfloat wratio = 1.0;
	gfloat hratio = 1.0;
	gfloat ratio  = 1.0;

	if (w <= aw && h <= ah) {
		*sw = w;
		*sh = h;
		return;
	}

	if (w > aw)
		wratio = (gfloat)aw / (gfloat)w;
	if (h > ah)
		hratio = (gfloat)ah / (gfloat)h;

	ratio = (wratio > hratio) ? hratio : wratio;

	*sw = (gint)(w * ratio);
	*sh = (gint)(h * ratio);

	/* restrict minimum size */
	if (*sw < 16 || *sh < 16) {
		wratio = 16.0 / (gfloat)w;
		hratio = 16.0 / (gfloat)h;
		ratio = (wratio > hratio) ? wratio : hratio;
		if (ratio >= 1.0) {
			*sw = w;
			*sh = h;
		} else {
			*sw = (gint)(w * ratio);
			*sh = (gint)(h * ratio);
		}
	}
}

static gint button_press_cb(GtkWidget *widget, GdkEventButton *event,
			    gpointer data)
{
	ImageView *imageview = (ImageView *)data;

	if (event->button == 1 && imageview->image) {
		imageview_show_image(imageview, NULL, NULL, !imageview->resize);
		return TRUE;
	}
	return FALSE;
}

static void size_allocate_cb(GtkWidget *widget,GtkAllocation *allocation,
			     gpointer data)
{
	ImageView *imageview = (ImageView *)data;

	if (imageview->resize && imageview->scrolledwin->parent)
		imageview_show_image(imageview, NULL, NULL, TRUE);
}
