1c784939ba91c0c51ddc3705bfd231ddb2f3deb5
[claws.git] / src / imageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkpixmap.h>
27
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 */
35
36 #include "intl.h"
37 #include "prefs_common.h"
38 #include "procmime.h"
39 #include "imageview.h"
40 #include "utils.h"
41
42 static void get_resized_size(gint w, gint h, gint aw, gint ah,
43                              gint *sw, gint *sh);
44
45
46 ImageView *imageview_create(void)
47 {
48         ImageView *imageview;
49         GtkWidget *scrolledwin;
50
51         debug_print("Creating image view...\n");
52         imageview = g_new0(ImageView, 1);
53
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);
59
60         gtk_widget_show_all(scrolledwin);
61
62         imageview->scrolledwin  = scrolledwin;
63         imageview->image        = NULL;
64
65         return imageview;
66 }
67
68 void imageview_init(ImageView *imageview)
69 {
70 }
71
72 #if HAVE_GDK_PIXBUF
73 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
74                           const gchar *file, gboolean resize)
75 {
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;
84
85         g_return_if_fail(imageview != NULL);
86
87         imageview_clear(imageview);
88
89         pixbuf = gdk_pixbuf_new_from_file(file);
90
91         if (!pixbuf) {
92                 g_warning("Can't load the image.");     
93                 return;
94         }
95
96         if (imageview->messageview->mainwin)
97                 main_window_cursor_wait(imageview->messageview->mainwin);
98
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;
104
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);
109
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;
114         }
115
116         gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);
117
118         if (!imageview->image) {
119                 imageview->image = gtk_pixmap_new(pixmap, mask);
120
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);
126
127         gtk_widget_show(imageview->image);
128
129         gdk_pixbuf_unref(pixbuf);
130
131         if (imageview->messageview->mainwin)
132                 main_window_cursor_normal(imageview->messageview->mainwin);
133 }
134 #else
135 #if HAVE_GDK_IMLIB
136 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
137                           const gchar *file, gboolean resize)
138 {
139         GdkImlibImage *im;
140         gint avail_width;
141         gint avail_height;
142         gint new_width;
143         gint new_height;
144
145         g_return_if_fail(imageview != NULL);
146
147         imageview_clear(imageview);
148
149         im = gdk_imlib_load_image((gchar *)file);
150
151         if (!im) {
152                 g_warning("Can't load the image.");     
153                 return;
154         }
155
156         if (imageview->messageview->mainwin)
157                 main_window_cursor_wait(imageview->messageview->mainwin);
158
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;
164
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;
171         }
172
173         gdk_imlib_render(im, new_width, new_height);
174
175         if (!imageview->image) {
176                 imageview->image = gtk_pixmap_new(gdk_imlib_move_image(im),
177                                                   gdk_imlib_move_mask(im));
178
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));      
186
187         gtk_widget_show(imageview->image);
188
189         gdk_imlib_destroy_image(im);
190
191         if (imageview->messageview->mainwin)
192                 main_window_cursor_normal(imageview->messageview->mainwin);
193 }
194 #else
195 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
196                           const gchar *file, gboolean resize)
197 {
198 }
199 #endif /* HAVE_GDK_IMLIB */
200 #endif /* HAVE_GDK_PIXBUF */
201
202 void imageview_clear(ImageView *imageview)
203 {
204         GtkAdjustment *hadj, *vadj;
205
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);
214 }
215
216 void imageview_destroy(ImageView *imageview)
217 {
218         g_free(imageview);
219 }
220
221 static void get_resized_size(gint w, gint h, gint aw, gint ah,
222                              gint *sw, gint *sh)
223 {
224         gfloat wratio = 1.0;
225         gfloat hratio = 1.0;
226         gfloat ratio  = 1.0;
227
228         if (w > aw)
229                 wratio = (gfloat)aw / (gfloat)w;
230         if (h > ah)
231                 hratio = (gfloat)ah / (gfloat)h;
232
233         ratio = (wratio > hratio) ? hratio : wratio;
234
235         *sw = (gint)(w * ratio);
236         *sh = (gint)(h * ratio);
237
238         /* be paranoid */
239         if (*sw <= 0 || *sh <= 0) {
240                 *sw = w;
241                 *sh = h;
242         }
243 }