2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkpixmap.h>
29 # include <gdk-pixbuf/gdk-pixbuf.h>
32 # include <gdk_imlib.h>
34 #endif /* HAVE_GDK_PIXBUF */
37 #include "mainwindow.h"
38 #include "prefs_common.h"
40 #include "imageview.h"
43 static void get_resized_size(gint w, gint h, gint aw, gint ah,
47 ImageView *imageview_create(void)
50 GtkWidget *scrolledwin;
52 debug_print("Creating image view...\n");
53 imageview = g_new0(ImageView, 1);
55 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
56 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
58 GTK_POLICY_AUTOMATIC);
59 gtk_widget_set_usize(scrolledwin, prefs_common.mainview_width, -1);
61 gtk_widget_show_all(scrolledwin);
63 imageview->scrolledwin = scrolledwin;
64 imageview->image = NULL;
69 void imageview_init(ImageView *imageview)
74 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
75 const gchar *file, gboolean resize)
78 GdkPixbuf *pixbuf_scaled;
86 g_return_if_fail(imageview != NULL);
88 imageview_clear(imageview);
90 pixbuf = gdk_pixbuf_new_from_file(file);
93 g_warning(_("Can't load the image."));
97 if (imageview->messageview->mainwin)
98 main_window_cursor_wait(imageview->messageview->mainwin);
101 avail_width = imageview->scrolledwin->parent->allocation.width;
102 avail_height = imageview->scrolledwin->parent->allocation.height;
103 if (avail_width > 8) avail_width -= 8;
104 if (avail_height > 8) avail_height -= 8;
106 get_resized_size(gdk_pixbuf_get_width(pixbuf),
107 gdk_pixbuf_get_height(pixbuf),
108 avail_width, avail_height,
109 &new_width, &new_height);
111 pixbuf_scaled = gdk_pixbuf_scale_simple
112 (pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
113 gdk_pixbuf_unref(pixbuf);
114 pixbuf = pixbuf_scaled;
117 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);
119 if (!imageview->image) {
120 imageview->image = gtk_pixmap_new(pixmap, mask);
122 gtk_scrolled_window_add_with_viewport
123 (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
126 gtk_pixmap_set(GTK_PIXMAP(imageview->image), pixmap, mask);
128 gtk_widget_show(imageview->image);
130 gdk_pixbuf_unref(pixbuf);
132 if (imageview->messageview->mainwin)
133 main_window_cursor_normal(imageview->messageview->mainwin);
137 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
138 const gchar *file, gboolean resize)
146 g_return_if_fail(imageview != NULL);
148 imageview_clear(imageview);
150 im = gdk_imlib_load_image((gchar *)file);
153 g_warning(_("Can't load the image."));
157 if (imageview->messageview->mainwin)
158 main_window_cursor_wait(imageview->messageview->mainwin);
161 avail_width = imageview->scrolledwin->parent->allocation.width;
162 avail_height = imageview->scrolledwin->parent->allocation.height;
163 if (avail_width > 8) avail_width -= 8;
164 if (avail_height > 8) avail_height -= 8;
166 get_resized_size(im->rgb_width, im->rgb_height,
167 avail_width, avail_height,
168 &new_width, &new_height);
170 new_width = im->rgb_width;
171 new_height = im->rgb_height;
174 gdk_imlib_render(im, new_width, new_height);
176 if (!imageview->image) {
177 imageview->image = gtk_pixmap_new(gdk_imlib_move_image(im),
178 gdk_imlib_move_mask(im));
180 gtk_scrolled_window_add_with_viewport
181 (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
184 gtk_pixmap_set(GTK_PIXMAP(imageview->image),
185 gdk_imlib_move_image(im),
186 gdk_imlib_move_mask(im));
188 gtk_widget_show(imageview->image);
190 gdk_imlib_destroy_image(im);
192 if (imageview->messageview->mainwin)
193 main_window_cursor_normal(imageview->messageview->mainwin);
196 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
197 const gchar *file, gboolean resize)
200 #endif /* HAVE_GDK_IMLIB */
201 #endif /* HAVE_GDK_PIXBUF */
203 void imageview_clear(ImageView *imageview)
205 GtkAdjustment *hadj, *vadj;
207 if (imageview->image)
208 gtk_pixmap_set(GTK_PIXMAP(imageview->image), NULL, NULL);
209 hadj = gtk_scrolled_window_get_hadjustment
210 (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
211 gtk_adjustment_set_value(hadj, 0.0);
212 vadj = gtk_scrolled_window_get_vadjustment
213 (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
214 gtk_adjustment_set_value(vadj, 0.0);
217 void imageview_destroy(ImageView *imageview)
222 static void get_resized_size(gint w, gint h, gint aw, gint ah,
230 wratio = (gfloat)aw / (gfloat)w;
232 hratio = (gfloat)ah / (gfloat)h;
234 ratio = (wratio > hratio) ? hratio : wratio;
236 *sw = (gint)(w * ratio);
237 *sh = (gint)(h * ratio);
240 if (*sw <= 0 || *sh <= 0) {