This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / imageview.c
blob1c784939ba91c0c51ddc3705bfd231ddb2f3deb5
1 /*
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <glib.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkpixmap.h>
28 #if HAVE_GDK_PIXBUF
29 # include <gdk-pixbuf/gdk-pixbuf.h>
30 #else
31 #if HAVE_GDK_IMLIB
32 # include <gdk_imlib.h>
33 #endif
34 #endif /* HAVE_GDK_PIXBUF */
36 #include "intl.h"
37 #include "prefs_common.h"
38 #include "procmime.h"
39 #include "imageview.h"
40 #include "utils.h"
42 static void get_resized_size(gint w, gint h, gint aw, gint ah,
43 gint *sw, gint *sh);
46 ImageView *imageview_create(void)
48 ImageView *imageview;
49 GtkWidget *scrolledwin;
51 debug_print("Creating image view...\n");
52 imageview = g_new0(ImageView, 1);
54 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
55 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
56 GTK_POLICY_AUTOMATIC,
57 GTK_POLICY_AUTOMATIC);
58 gtk_widget_set_usize(scrolledwin, prefs_common.mainview_width, -1);
60 gtk_widget_show_all(scrolledwin);
62 imageview->scrolledwin = scrolledwin;
63 imageview->image = NULL;
65 return imageview;
68 void imageview_init(ImageView *imageview)
72 #if HAVE_GDK_PIXBUF
73 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
74 const gchar *file, gboolean resize)
76 GdkPixbuf *pixbuf;
77 GdkPixbuf *pixbuf_scaled;
78 GdkPixmap *pixmap;
79 GdkBitmap *mask;
80 gint avail_width;
81 gint avail_height;
82 gint new_width;
83 gint new_height;
85 g_return_if_fail(imageview != NULL);
87 imageview_clear(imageview);
89 pixbuf = gdk_pixbuf_new_from_file(file);
91 if (!pixbuf) {
92 g_warning("Can't load the image.");
93 return;
96 if (imageview->messageview->mainwin)
97 main_window_cursor_wait(imageview->messageview->mainwin);
99 if (resize) {
100 avail_width = imageview->scrolledwin->parent->allocation.width;
101 avail_height = imageview->scrolledwin->parent->allocation.height;
102 if (avail_width > 8) avail_width -= 8;
103 if (avail_height > 8) avail_height -= 8;
105 get_resized_size(gdk_pixbuf_get_width(pixbuf),
106 gdk_pixbuf_get_height(pixbuf),
107 avail_width, avail_height,
108 &new_width, &new_height);
110 pixbuf_scaled = gdk_pixbuf_scale_simple
111 (pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
112 gdk_pixbuf_unref(pixbuf);
113 pixbuf = pixbuf_scaled;
116 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);
118 if (!imageview->image) {
119 imageview->image = gtk_pixmap_new(pixmap, mask);
121 gtk_scrolled_window_add_with_viewport
122 (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
123 imageview->image);
124 } else
125 gtk_pixmap_set(GTK_PIXMAP(imageview->image), pixmap, mask);
127 gtk_widget_show(imageview->image);
129 gdk_pixbuf_unref(pixbuf);
131 if (imageview->messageview->mainwin)
132 main_window_cursor_normal(imageview->messageview->mainwin);
134 #else
135 #if HAVE_GDK_IMLIB
136 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
137 const gchar *file, gboolean resize)
139 GdkImlibImage *im;
140 gint avail_width;
141 gint avail_height;
142 gint new_width;
143 gint new_height;
145 g_return_if_fail(imageview != NULL);
147 imageview_clear(imageview);
149 im = gdk_imlib_load_image((gchar *)file);
151 if (!im) {
152 g_warning("Can't load the image.");
153 return;
156 if (imageview->messageview->mainwin)
157 main_window_cursor_wait(imageview->messageview->mainwin);
159 if (resize) {
160 avail_width = imageview->scrolledwin->parent->allocation.width;
161 avail_height = imageview->scrolledwin->parent->allocation.height;
162 if (avail_width > 8) avail_width -= 8;
163 if (avail_height > 8) avail_height -= 8;
165 get_resized_size(im->rgb_width, im->rgb_height,
166 avail_width, avail_height,
167 &new_width, &new_height);
168 } else {
169 new_width = im->rgb_width;
170 new_height = im->rgb_height;
173 gdk_imlib_render(im, new_width, new_height);
175 if (!imageview->image) {
176 imageview->image = gtk_pixmap_new(gdk_imlib_move_image(im),
177 gdk_imlib_move_mask(im));
179 gtk_scrolled_window_add_with_viewport
180 (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
181 imageview->image);
182 } else
183 gtk_pixmap_set(GTK_PIXMAP(imageview->image),
184 gdk_imlib_move_image(im),
185 gdk_imlib_move_mask(im));
187 gtk_widget_show(imageview->image);
189 gdk_imlib_destroy_image(im);
191 if (imageview->messageview->mainwin)
192 main_window_cursor_normal(imageview->messageview->mainwin);
194 #else
195 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
196 const gchar *file, gboolean resize)
199 #endif /* HAVE_GDK_IMLIB */
200 #endif /* HAVE_GDK_PIXBUF */
202 void imageview_clear(ImageView *imageview)
204 GtkAdjustment *hadj, *vadj;
206 if (imageview->image)
207 gtk_pixmap_set(GTK_PIXMAP(imageview->image), NULL, NULL);
208 hadj = gtk_scrolled_window_get_hadjustment
209 (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
210 gtk_adjustment_set_value(hadj, 0.0);
211 vadj = gtk_scrolled_window_get_vadjustment
212 (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
213 gtk_adjustment_set_value(vadj, 0.0);
216 void imageview_destroy(ImageView *imageview)
218 g_free(imageview);
221 static void get_resized_size(gint w, gint h, gint aw, gint ah,
222 gint *sw, gint *sh)
224 gfloat wratio = 1.0;
225 gfloat hratio = 1.0;
226 gfloat ratio = 1.0;
228 if (w > aw)
229 wratio = (gfloat)aw / (gfloat)w;
230 if (h > ah)
231 hratio = (gfloat)ah / (gfloat)h;
233 ratio = (wratio > hratio) ? hratio : wratio;
235 *sw = (gint)(w * ratio);
236 *sh = (gint)(h * ratio);
238 /* be paranoid */
239 if (*sw <= 0 || *sh <= 0) {
240 *sw = w;
241 *sh = h;