66846d2b960cfb18ccb589144cbafc4dcabf882e
[claws.git] / src / plugins / image_viewer / viewer.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 "procmime.h"
38 #include "utils.h"
39 #include "mimeview.h"
40
41 #include "viewerprefs.h"
42
43 typedef struct _ImageViewer ImageViewer;
44
45 MimeViewerFactory image_viewer_factory;
46 static void image_viewer_get_resized_size(gint w, gint h, gint aw, gint ah,
47                                           gint * sw, gint * sh);
48 static void image_viewer_clear_viewer(MimeViewer *imageviewer);
49
50 struct _ImageViewer
51 {
52         MimeViewer mimeviewer;
53
54         GtkWidget *scrolledwin;
55         GtkWidget *image;
56 };
57
58 static GtkWidget *image_viewer_get_widget(MimeViewer *_mimeviewer)
59 {
60         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
61
62         debug_print("image_viewer_get_widget\n");
63
64         return imageviewer->scrolledwin;
65 }
66
67 #if HAVE_GDK_PIXBUF
68 static void image_viewer_show_mimepart(MimeViewer *_mimeviewer, const gchar *file, MimeInfo *mimeinfo)
69 {
70         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
71         GdkPixbuf *pixbuf;
72         GdkPixbuf *pixbuf_scaled;
73         GdkPixmap *pixmap;
74         GdkBitmap *mask;
75         gint avail_width;
76         gint avail_height;
77         gint new_width;
78         gint new_height;
79         gchar *imgfile;
80
81         debug_print("image_viewer_show_mimepart\n");
82
83         g_return_if_fail(imageviewer != NULL);
84
85         image_viewer_clear_viewer(_mimeviewer);
86
87         imgfile = procmime_get_tmp_file_name(mimeinfo);
88         if (procmime_get_part(imgfile, file, mimeinfo) < 0) {
89                 g_warning("Can't get mimepart file");   
90                 g_free(imgfile);
91                 return;
92         }
93
94         pixbuf = gdk_pixbuf_new_from_file(imgfile);
95         unlink(imgfile);
96         g_free(imgfile);
97         if (!pixbuf) {
98                 g_warning("Can't load the image.");     
99                 return;
100         }
101
102         if (imageviewerprefs.resize_img) {
103                 avail_width = imageviewer->scrolledwin->parent->allocation.width;
104                 avail_height = imageviewer->scrolledwin->parent->allocation.height;
105                 if (avail_width > 8) avail_width -= 8;
106                 if (avail_height > 8) avail_height -= 8;
107
108                 image_viewer_get_resized_size(gdk_pixbuf_get_width(pixbuf),
109                                  gdk_pixbuf_get_height(pixbuf),
110                                  avail_width, avail_height,
111                                  &new_width, &new_height);
112
113                 pixbuf_scaled = gdk_pixbuf_scale_simple
114                         (pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
115                 gdk_pixbuf_unref(pixbuf);
116                 pixbuf = pixbuf_scaled;
117         }
118
119         gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);
120
121         if (!imageviewer->image) {
122                 imageviewer->image = gtk_pixmap_new(pixmap, mask);
123
124                 gtk_scrolled_window_add_with_viewport
125                         (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin),
126                          imageviewer->image);
127         } else
128                 gtk_pixmap_set(GTK_PIXMAP(imageviewer->image), pixmap, mask);
129
130         gtk_widget_show(imageviewer->image);
131
132         gdk_pixbuf_unref(pixbuf);
133 }
134 #else
135 #if HAVE_GDK_IMLIB
136 static void image_viewer_show_mimepart(MimeViewer *_mimeviewer, const gchar *file, MimeInfo *mimeinfo)
137 {
138         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
139         GdkImlibImage *im;
140         gint avail_width;
141         gint avail_height;
142         gint new_width;
143         gint new_height;
144         gchar *imgfile;
145
146         debug_print("image_viewer_show_mimepart\n");
147
148         g_return_if_fail(imageviewer != NULL);
149
150         image_viewer_clear_viewer(imageviewer);
151
152         imgfile = procmime_get_tmp_file_name(mimeinfo);
153         if (procmime_get_part(imgfile, file, mimeinfo) < 0) {
154                 g_warning("Can't get mimepart file");   
155                 g_free(imgfile);
156                 return;
157         }
158
159         im = gdk_imlib_load_image(imgfile);
160         unlink(imgfile);
161         g_free(imgfile);
162         if (!im) {
163                 g_warning("Can't load the image.");     
164                 return;
165         }
166
167         if (imageviewerprefs.resize_img) {
168                 avail_width = imageviewer->scrolledwin->parent->allocation.width;
169                 avail_height = imageviewer->scrolledwin->parent->allocation.height;
170                 if (avail_width > 8) avail_width -= 8;
171                 if (avail_height > 8) avail_height -= 8;
172
173                 image_viewer_get_resized_size(im->rgb_width, im->rgb_height,
174                                  avail_width, avail_height,
175                                  &new_width, &new_height);
176         } else {
177                 new_width = im->rgb_width;
178                 new_height = im->rgb_height;
179         }
180
181         gdk_imlib_render(im, new_width, new_height);
182
183         if (!imageviewer->image) {
184                 imageviewer->image = gtk_pixmap_new(gdk_imlib_move_image(im),
185                                                     gdk_imlib_move_mask(im));
186
187                 gtk_scrolled_window_add_with_viewport
188                         (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin),
189                          imageviewer->image);
190         } else
191                 gtk_pixmap_set(GTK_PIXMAP(imageviewer->image),
192                                gdk_imlib_move_image(im),
193                                gdk_imlib_move_mask(im));      
194
195         gtk_widget_show(imageviewer->image);
196
197         gdk_imlib_destroy_image(im);
198 }
199 #endif /* HAVE_GDK_IMLIB */
200 #endif /* HAVE_GDK_PIXBUF */
201
202 static void image_viewer_clear_viewer(MimeViewer *_mimeviewer)
203 {
204         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
205         GtkAdjustment *hadj, *vadj;
206
207         debug_print("image_viewer_clear_viewer\n");
208
209         if (imageviewer->image)
210                 gtk_pixmap_set(GTK_PIXMAP(imageviewer->image), NULL, NULL);
211         hadj = gtk_scrolled_window_get_hadjustment
212                 (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin));
213         gtk_adjustment_set_value(hadj, 0.0);
214         vadj = gtk_scrolled_window_get_vadjustment
215                 (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin));
216         gtk_adjustment_set_value(vadj, 0.0);
217 }
218
219 static void image_viewer_destroy_viewer(MimeViewer *_mimeviewer)
220 {
221         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
222
223         debug_print("image_viewer_destroy_viewer\n");
224
225         image_viewer_clear_viewer(_mimeviewer);
226         gtk_widget_unref(imageviewer->scrolledwin);
227         g_free(imageviewer);
228 }
229
230 static void image_viewer_get_resized_size(gint w, gint h, gint aw, gint ah,
231                              gint *sw, gint *sh)
232 {
233         gfloat wratio = 1.0;
234         gfloat hratio = 1.0;
235         gfloat ratio  = 1.0;
236
237         if (w > aw)
238                 wratio = (gfloat)aw / (gfloat)w;
239         if (h > ah)
240                 hratio = (gfloat)ah / (gfloat)h;
241
242         ratio = (wratio > hratio) ? hratio : wratio;
243
244         *sw = (gint)(w * ratio);
245         *sh = (gint)(h * ratio);
246
247         /* be paranoid */
248         if (*sw <= 0 || *sh <= 0) {
249                 *sw = w;
250                 *sh = h;
251         }
252 }
253
254 MimeViewer *image_viewer_create(void)
255 {
256         ImageViewer *imageviewer;
257         GtkWidget *scrolledwin;
258
259         debug_print("Creating image view...\n");
260         imageviewer = g_new0(ImageViewer, 1);
261         imageviewer->mimeviewer.factory = &image_viewer_factory;
262
263         imageviewer->mimeviewer.get_widget = image_viewer_get_widget;
264         imageviewer->mimeviewer.show_mimepart = image_viewer_show_mimepart;
265         imageviewer->mimeviewer.clear_viewer = image_viewer_clear_viewer;
266         imageviewer->mimeviewer.destroy_viewer = image_viewer_destroy_viewer;
267
268         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
269         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
270                                        GTK_POLICY_AUTOMATIC,
271                                        GTK_POLICY_AUTOMATIC);
272         gtk_widget_ref(scrolledwin);
273
274         gtk_widget_show_all(scrolledwin);
275
276         imageviewer->scrolledwin  = scrolledwin;
277         imageviewer->image        = NULL;
278
279         return (MimeViewer *) imageviewer;
280 }
281
282 MimeViewerFactory image_viewer_factory =
283 {
284         "image/*",
285         0,
286         
287         image_viewer_create,
288 };
289
290 void image_viewer_init(void)
291 {
292 #if HAVE_GDK_IMLIB
293         gdk_imlib_init();
294         gtk_widget_push_visual(gdk_imlib_get_visual());
295         gtk_widget_push_colormap(gdk_imlib_get_colormap());
296 #endif
297
298         mimeview_register_viewer_factory(&image_viewer_factory);
299 }
300
301 void image_viewer_done(void)
302 {
303         mimeview_unregister_viewer_factory(&image_viewer_factory);
304 }