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