sync with 0.8.6cvs5
[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 "mainwindow.h"
38 #include "prefs_common.h"
39 #include "procmime.h"
40 #include "imageview.h"
41 #include "utils.h"
42
43 static void get_resized_size(gint w, gint h, gint aw, gint ah,
44                              gint *sw, gint *sh);
45
46
47 ImageView *imageview_create(void)
48 {
49         ImageView *imageview;
50         GtkWidget *scrolledwin;
51
52         debug_print("Creating image view...\n");
53         imageview = g_new0(ImageView, 1);
54
55         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
56         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
57                                        GTK_POLICY_AUTOMATIC,
58                                        GTK_POLICY_AUTOMATIC);
59         gtk_widget_set_usize(scrolledwin, prefs_common.mainview_width, -1);
60
61         gtk_widget_show_all(scrolledwin);
62
63         imageview->scrolledwin  = scrolledwin;
64         imageview->image        = NULL;
65
66         return imageview;
67 }
68
69 void imageview_init(ImageView *imageview)
70 {
71 }
72
73 #if HAVE_GDK_PIXBUF
74 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
75                           const gchar *file, gboolean resize)
76 {
77         GdkPixbuf *pixbuf;
78         GdkPixbuf *pixbuf_scaled;
79         GdkPixmap *pixmap;
80         GdkBitmap *mask;
81         gint avail_width;
82         gint avail_height;
83         gint new_width;
84         gint new_height;
85
86         g_return_if_fail(imageview != NULL);
87
88         imageview_clear(imageview);
89
90         pixbuf = gdk_pixbuf_new_from_file(file);
91
92         if (!pixbuf) {
93                 g_warning(_("Can't load the image."));  
94                 return;
95         }
96
97         if (imageview->messageview->mainwin)
98                 main_window_cursor_wait(imageview->messageview->mainwin);
99
100         if (resize) {
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;
105
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);
110
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;
115         }
116
117         gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);
118
119         if (!imageview->image) {
120                 imageview->image = gtk_pixmap_new(pixmap, mask);
121
122                 gtk_scrolled_window_add_with_viewport
123                         (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
124                          imageview->image);
125         } else
126                 gtk_pixmap_set(GTK_PIXMAP(imageview->image), pixmap, mask);
127
128         gtk_widget_show(imageview->image);
129
130         gdk_pixbuf_unref(pixbuf);
131
132         if (imageview->messageview->mainwin)
133                 main_window_cursor_normal(imageview->messageview->mainwin);
134 }
135 #else
136 #if HAVE_GDK_IMLIB
137 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
138                           const gchar *file, gboolean resize)
139 {
140         GdkImlibImage *im;
141         gint avail_width;
142         gint avail_height;
143         gint new_width;
144         gint new_height;
145
146         g_return_if_fail(imageview != NULL);
147
148         imageview_clear(imageview);
149
150         im = gdk_imlib_load_image((gchar *)file);
151
152         if (!im) {
153                 g_warning(_("Can't load the image."));  
154                 return;
155         }
156
157         if (imageview->messageview->mainwin)
158                 main_window_cursor_wait(imageview->messageview->mainwin);
159
160         if (resize) {
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;
165
166                 get_resized_size(im->rgb_width, im->rgb_height,
167                                  avail_width, avail_height,
168                                  &new_width, &new_height);
169         } else {
170                 new_width = im->rgb_width;
171                 new_height = im->rgb_height;
172         }
173
174         gdk_imlib_render(im, new_width, new_height);
175
176         if (!imageview->image) {
177                 imageview->image = gtk_pixmap_new(gdk_imlib_move_image(im),
178                                                   gdk_imlib_move_mask(im));
179
180                 gtk_scrolled_window_add_with_viewport
181                         (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
182                          imageview->image);
183         } else
184                 gtk_pixmap_set(GTK_PIXMAP(imageview->image),
185                                gdk_imlib_move_image(im),
186                                gdk_imlib_move_mask(im));      
187
188         gtk_widget_show(imageview->image);
189
190         gdk_imlib_destroy_image(im);
191
192         if (imageview->messageview->mainwin)
193                 main_window_cursor_normal(imageview->messageview->mainwin);
194 }
195 #else
196 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
197                           const gchar *file)
198 {
199 }
200 #endif /* HAVE_GDK_IMLIB */
201 #endif /* HAVE_GDK_PIXBUF */
202
203 void imageview_clear(ImageView *imageview)
204 {
205         GtkAdjustment *hadj, *vadj;
206
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);
215 }
216
217 void imageview_destroy(ImageView *imageview)
218 {
219         g_free(imageview);
220 }
221
222 static void get_resized_size(gint w, gint h, gint aw, gint ah,
223                              gint *sw, gint *sh)
224 {
225         gfloat wratio = 1.0;
226         gfloat hratio = 1.0;
227         gfloat ratio  = 1.0;
228
229         if (w > aw)
230                 wratio = (gfloat)aw / (gfloat)w;
231         if (h > ah)
232                 hratio = (gfloat)ah / (gfloat)h;
233
234         ratio = (wratio > hratio) ? hratio : wratio;
235
236         *sw = (gint)(w * ratio);
237         *sh = (gint)(h * ratio);
238
239         /* be paranoid */
240         if (*sw <= 0 || *sh <= 0) {
241                 *sw = w;
242                 *sh = h;
243         }
244 }