2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
33 #include "prefs_common.h"
35 typedef struct _ImageViewer ImageViewer
;
37 MimeViewerFactory image_viewer_factory
;
38 void image_viewer_get_resized_size(gint w
, gint h
, gint aw
, gint ah
,
39 gint
* sw
, gint
* sh
);
40 static void image_viewer_clear_viewer(MimeViewer
*imageviewer
);
41 static void scrolledwin_resize_cb(GtkWidget
*scrolledwin
, GtkAllocation
*alloc
,
42 ImageViewer
*imageviewer
);
45 MimeViewer mimeviewer
;
51 GtkWidget
*scrolledwin
;
58 GtkWidget
*content_type
;
59 GtkWidget
*load_button
;
62 static GtkWidget
*image_viewer_get_widget(MimeViewer
*_mimeviewer
)
64 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
66 debug_print("image_viewer_get_widget\n");
68 return imageviewer
->notebook
;
71 static void image_viewer_load_file(ImageViewer
*imageviewer
, const gchar
*imgfile
)
73 GtkAllocation allocation
;
74 GdkPixbufAnimation
*animation
= NULL
;
75 GdkPixbuf
*pixbuf
= NULL
;
78 debug_print("image_viewer_show_mimepart\n");
80 animation
= gdk_pixbuf_animation_new_from_file(imgfile
, &error
);
81 if (gdk_pixbuf_animation_is_static_image(animation
)
82 || imageviewer
->resize_img
) {
83 pixbuf
= gdk_pixbuf_animation_get_static_image(animation
);
85 g_object_unref(animation
);
88 if (imageviewer
->resize_img
) {
89 gtk_widget_get_allocation(
90 gtk_widget_get_parent(imageviewer
->notebook
), &allocation
);
91 pixbuf
= claws_load_pixbuf_fitting(pixbuf
,
96 pixbuf
= claws_load_pixbuf_fitting(pixbuf
, -1, -1);
99 if (error
&& !pixbuf
&& !animation
) {
100 gtk_label_set_text(GTK_LABEL(imageviewer
->error_lbl
), _("Error:"));
101 gtk_label_set_text(GTK_LABEL(imageviewer
->error_msg
), error
->message
);
102 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 0);
103 gtk_widget_hide(imageviewer
->load_button
);
106 if (!pixbuf
&& !animation
) {
107 g_warning("Can't load the image.");
112 gtk_image_set_from_animation(GTK_IMAGE(imageviewer
->image
), animation
);
114 gtk_image_set_from_pixbuf(GTK_IMAGE(imageviewer
->image
), pixbuf
);
116 g_signal_handlers_block_by_func(G_OBJECT(imageviewer
->scrolledwin
),
117 G_CALLBACK(scrolledwin_resize_cb
), imageviewer
);
120 gtk_widget_show(imageviewer
->image
);
122 g_signal_handlers_unblock_by_func(G_OBJECT(imageviewer
->scrolledwin
),
123 G_CALLBACK(scrolledwin_resize_cb
), imageviewer
);
126 g_object_unref(pixbuf
);
128 g_object_unref(animation
);
131 static void image_viewer_set_notebook_page(MimeViewer
*_mimeviewer
)
133 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
135 if (!prefs_common
.display_img
)
136 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 0);
138 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 1);
141 static void image_viewer_load_image(ImageViewer
*imageviewer
)
145 if (imageviewer
->mimeinfo
== NULL
)
148 imgfile
= procmime_get_tmp_file_name(imageviewer
->mimeinfo
);
149 if (procmime_get_part(imgfile
, imageviewer
->mimeinfo
) < 0) {
150 g_warning("Can't get mimepart file");
154 image_viewer_load_file(imageviewer
, imgfile
);
155 claws_unlink(imgfile
);
159 static void image_viewer_show_mimepart(MimeViewer
*_mimeviewer
, const gchar
*file
, MimeInfo
*mimeinfo
)
161 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
163 image_viewer_clear_viewer(_mimeviewer
);
164 g_free(imageviewer
->file
);
165 imageviewer
->file
= g_strdup(file
);
166 imageviewer
->mimeinfo
= mimeinfo
;
168 gtk_label_set_text(GTK_LABEL(imageviewer
->filename
),
169 procmime_mimeinfo_get_parameter(mimeinfo
, "name"));
170 gtk_label_set_text(GTK_LABEL(imageviewer
->filesize
), to_human_readable((goffset
)mimeinfo
->length
));
171 gtk_label_set_text(GTK_LABEL(imageviewer
->content_type
), mimeinfo
->subtype
);
172 gtk_label_set_text(GTK_LABEL(imageviewer
->error_lbl
), "");
173 gtk_label_set_text(GTK_LABEL(imageviewer
->error_msg
), "");
175 if (prefs_common
.display_img
)
176 image_viewer_load_image(imageviewer
);
179 static void image_viewer_clear_viewer(MimeViewer
*_mimeviewer
)
181 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
182 GtkAdjustment
*hadj
, *vadj
;
184 debug_print("image_viewer_clear_viewer\n");
186 image_viewer_set_notebook_page(_mimeviewer
);
188 if (imageviewer
->scrolledwin
) {
189 hadj
= gtk_scrolled_window_get_hadjustment
190 (GTK_SCROLLED_WINDOW(imageviewer
->scrolledwin
));
192 gtk_adjustment_set_value(hadj
, 0.0);
193 gtk_adjustment_changed(hadj
);
195 vadj
= gtk_scrolled_window_get_vadjustment
196 (GTK_SCROLLED_WINDOW(imageviewer
->scrolledwin
));
198 gtk_adjustment_set_value(vadj
, 0.0);
199 gtk_adjustment_changed(vadj
);
202 g_free(imageviewer
->file
);
203 imageviewer
->file
= NULL
;
204 imageviewer
->mimeinfo
= NULL
;
205 imageviewer
->resize_img
= prefs_common
.resize_img
;
208 static void image_viewer_destroy_viewer(MimeViewer
*_mimeviewer
)
210 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
212 debug_print("image_viewer_destroy_viewer\n");
214 image_viewer_clear_viewer(_mimeviewer
);
215 g_object_unref(imageviewer
->notebook
);
219 void image_viewer_get_resized_size(gint w
, gint h
, gint aw
, gint ah
,
227 wratio
= (gfloat
)aw
/ (gfloat
)w
;
229 hratio
= (gfloat
)ah
/ (gfloat
)h
;
231 ratio
= (wratio
> hratio
) ? hratio
: wratio
;
233 *sw
= (gint
)(w
* ratio
);
234 *sh
= (gint
)(h
* ratio
);
237 if (*sw
<= 0 || *sh
<= 0) {
243 static void load_cb(GtkButton
*button
, ImageViewer
*imageviewer
)
245 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 1);
246 image_viewer_load_image(imageviewer
);
249 static gboolean
scrolledwin_button_cb(GtkWidget
*scrolledwin
, GdkEventButton
*event
,
250 ImageViewer
*imageviewer
)
252 if (event
->button
== 1 && imageviewer
->image
) {
253 imageviewer
->resize_img
= !imageviewer
->resize_img
;
254 image_viewer_load_image(imageviewer
);
260 static void scrolledwin_resize_cb(GtkWidget
*scrolledwin
, GtkAllocation
*alloc
,
261 ImageViewer
*imageviewer
)
263 if (imageviewer
->resize_img
)
264 image_viewer_load_image(imageviewer
);
267 static MimeViewer
*image_viewer_create(void)
269 ImageViewer
*imageviewer
;
271 * glade generated code (do not touch)
279 GtkWidget
*load_button
;
281 GtkWidget
*content_type
;
282 GtkWidget
*scrolledwin
;
283 GtkWidget
*error_lbl
;
284 GtkWidget
*error_msg
;
286 notebook
= gtk_notebook_new();
287 gtk_widget_show(notebook
);
288 gtkut_widget_set_can_focus(notebook
, FALSE
);
289 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook
), FALSE
);
290 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook
), FALSE
);
292 table1
= gtk_table_new(5, 3, FALSE
);
293 gtk_widget_show(table1
);
294 gtk_container_add(GTK_CONTAINER(notebook
), table1
);
295 gtk_container_set_border_width(GTK_CONTAINER(table1
), 8);
296 gtk_table_set_row_spacings(GTK_TABLE(table1
), 4);
297 gtk_table_set_col_spacings(GTK_TABLE(table1
), 4);
299 label3
= gtk_label_new(_("Filename:"));
300 gtk_widget_show(label3
);
301 gtk_table_attach(GTK_TABLE(table1
), label3
, 0, 1, 0, 1,
302 (GtkAttachOptions
) (GTK_FILL
),
303 (GtkAttachOptions
) (0), 0, 0);
304 gtk_misc_set_alignment(GTK_MISC(label3
), 0, 0.5);
306 label4
= gtk_label_new(_("Filesize:"));
307 gtk_widget_show(label4
);
308 gtk_table_attach(GTK_TABLE(table1
), label4
, 0, 1, 1, 2,
309 (GtkAttachOptions
) (GTK_FILL
),
310 (GtkAttachOptions
) (0), 0, 0);
311 gtk_misc_set_alignment(GTK_MISC(label4
), 0, 0.5);
313 filename
= gtk_label_new("");
314 gtk_widget_show(filename
);
315 gtk_table_attach(GTK_TABLE(table1
), filename
, 1, 3, 0, 1,
316 (GtkAttachOptions
) (GTK_FILL
),
317 (GtkAttachOptions
) (0), 0, 0);
318 gtk_misc_set_alignment(GTK_MISC(filename
), 0, 0.5);
320 filesize
= gtk_label_new("");
321 gtk_widget_show(filesize
);
322 gtk_table_attach(GTK_TABLE(table1
), filesize
, 1, 3, 1, 2,
323 (GtkAttachOptions
) (GTK_FILL
),
324 (GtkAttachOptions
) (0), 0, 0);
325 gtk_misc_set_alignment(GTK_MISC(filesize
), 0, 0.5);
327 label5
= gtk_label_new(_("Content-Type:"));
328 gtk_widget_show(label5
);
329 gtk_table_attach(GTK_TABLE(table1
), label5
, 0, 1, 2, 3,
330 (GtkAttachOptions
) (GTK_FILL
),
331 (GtkAttachOptions
) (0), 0, 0);
332 gtk_misc_set_alignment(GTK_MISC(label5
), 0, 0.5);
334 content_type
= gtk_label_new("");
335 gtk_widget_show(content_type
);
336 gtk_table_attach(GTK_TABLE(table1
), content_type
, 1, 3, 2, 3,
337 (GtkAttachOptions
) (GTK_FILL
),
338 (GtkAttachOptions
) (0), 0, 0);
339 gtk_misc_set_alignment(GTK_MISC(content_type
), 0, 0.5);
341 error_lbl
= gtk_label_new("");
342 gtk_widget_show(error_lbl
);
343 gtk_table_attach(GTK_TABLE(table1
), error_lbl
, 0, 1, 3, 4,
344 (GtkAttachOptions
) (GTK_FILL
),
345 (GtkAttachOptions
) (0), 0, 0);
346 gtk_misc_set_alignment(GTK_MISC(error_lbl
), 0, 0.5);
348 error_msg
= gtk_label_new("");
349 gtk_widget_show(error_msg
);
350 gtk_table_attach(GTK_TABLE(table1
), error_msg
, 1, 3, 3, 4,
351 (GtkAttachOptions
) (GTK_FILL
),
352 (GtkAttachOptions
) (0), 0, 0);
353 gtk_misc_set_alignment(GTK_MISC(error_msg
), 0, 0.5);
355 load_button
= gtk_button_new_with_label(_("Load Image"));
356 gtk_widget_show(load_button
);
357 gtk_table_attach(GTK_TABLE(table1
), load_button
, 0, 1, 4, 5,
358 (GtkAttachOptions
) (GTK_FILL
),
359 (GtkAttachOptions
) (0), 0, 0);
361 scrolledwin
= gtk_scrolled_window_new(NULL
, NULL
);
362 gtk_widget_show(scrolledwin
);
363 gtk_container_add(GTK_CONTAINER(notebook
), scrolledwin
);
364 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin
),
365 GTK_POLICY_AUTOMATIC
,
366 GTK_POLICY_AUTOMATIC
);
371 debug_print("Creating image view...\n");
372 imageviewer
= g_new0(ImageViewer
, 1);
373 imageviewer
->mimeviewer
.factory
= &image_viewer_factory
;
375 imageviewer
->mimeviewer
.get_widget
= image_viewer_get_widget
;
376 imageviewer
->mimeviewer
.show_mimepart
= image_viewer_show_mimepart
;
377 imageviewer
->mimeviewer
.clear_viewer
= image_viewer_clear_viewer
;
378 imageviewer
->mimeviewer
.destroy_viewer
= image_viewer_destroy_viewer
;
379 imageviewer
->mimeviewer
.get_selection
= NULL
;
381 imageviewer
->resize_img
= prefs_common
.resize_img
;
383 imageviewer
->scrolledwin
= scrolledwin
;
384 imageviewer
->image
= gtk_image_new();
385 gtk_scrolled_window_add_with_viewport
386 (GTK_SCROLLED_WINDOW(imageviewer
->scrolledwin
),
388 imageviewer
->notebook
= notebook
;
389 imageviewer
->filename
= filename
;
390 imageviewer
->filesize
= filesize
;
391 imageviewer
->content_type
= content_type
;
392 imageviewer
->error_msg
= error_msg
;
393 imageviewer
->error_lbl
= error_lbl
;
394 imageviewer
->load_button
= load_button
;
396 g_object_ref(notebook
);
398 g_signal_connect(G_OBJECT(load_button
), "clicked",
399 G_CALLBACK(load_cb
), imageviewer
);
400 g_signal_connect(G_OBJECT(scrolledwin
), "button-press-event",
401 G_CALLBACK(scrolledwin_button_cb
), imageviewer
);
402 g_signal_connect(G_OBJECT(scrolledwin
), "size-allocate",
403 G_CALLBACK(scrolledwin_resize_cb
), imageviewer
);
405 image_viewer_set_notebook_page((MimeViewer
*)imageviewer
);
407 return (MimeViewer
*) imageviewer
;
410 static gchar
*content_types
[] =
413 MimeViewerFactory image_viewer_factory
=
421 void image_viewer_init(void)
423 mimeview_register_viewer_factory(&image_viewer_factory
);
426 void image_viewer_done(void)
428 mimeview_unregister_viewer_factory(&image_viewer_factory
);