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.
25 #include <gtk/gtkscrolledwindow.h>
26 #include <gtk/gtkpixmap.h>
29 # include <gdk-pixbuf/gdk-pixbuf.h>
32 # include <gdk_imlib.h>
34 #endif /* HAVE_GDK_PIXBUF */
37 #include "prefs_common.h"
39 #include "imageview.h"
42 static void get_resized_size(gint w
, gint h
, gint aw
, gint ah
,
46 ImageView
*imageview_create(void)
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
),
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
;
68 void imageview_init(ImageView
*imageview
)
73 void imageview_show_image(ImageView
*imageview
, MimeInfo
*mimeinfo
,
74 const gchar
*file
, gboolean resize
)
77 GdkPixbuf
*pixbuf_scaled
;
85 g_return_if_fail(imageview
!= NULL
);
87 imageview_clear(imageview
);
89 pixbuf
= gdk_pixbuf_new_from_file(file
);
92 g_warning("Can't load the image.");
96 if (imageview
->messageview
->mainwin
)
97 main_window_cursor_wait(imageview
->messageview
->mainwin
);
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
),
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
);
136 void imageview_show_image(ImageView
*imageview
, MimeInfo
*mimeinfo
,
137 const gchar
*file
, gboolean resize
)
145 g_return_if_fail(imageview
!= NULL
);
147 imageview_clear(imageview
);
149 im
= gdk_imlib_load_image((gchar
*)file
);
152 g_warning("Can't load the image.");
156 if (imageview
->messageview
->mainwin
)
157 main_window_cursor_wait(imageview
->messageview
->mainwin
);
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
);
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
),
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
);
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
)
221 static void get_resized_size(gint w
, gint h
, gint aw
, gint ah
,
229 wratio
= (gfloat
)aw
/ (gfloat
)w
;
231 hratio
= (gfloat
)ah
/ (gfloat
)h
;
233 ratio
= (wratio
> hratio
) ? hratio
: wratio
;
235 *sw
= (gint
)(w
* ratio
);
236 *sh
= (gint
)(h
* ratio
);
239 if (*sw
<= 0 || *sh
<= 0) {