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>
30 #include "file-utils.h"
34 #include "prefs_common.h"
36 typedef struct _ImageViewer ImageViewer
;
38 MimeViewerFactory image_viewer_factory
;
39 void image_viewer_get_resized_size(gint w
, gint h
, gint aw
, gint ah
,
40 gint
* sw
, gint
* sh
);
41 static void image_viewer_clear_viewer(MimeViewer
*imageviewer
);
42 static void scrolledwin_resize_cb(GtkWidget
*scrolledwin
, GtkAllocation
*alloc
,
43 ImageViewer
*imageviewer
);
46 MimeViewer mimeviewer
;
52 GtkWidget
*scrolledwin
;
59 GtkWidget
*content_type
;
60 GtkWidget
*load_button
;
63 static GtkWidget
*image_viewer_get_widget(MimeViewer
*_mimeviewer
)
65 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
67 debug_print("image_viewer_get_widget\n");
69 return imageviewer
->notebook
;
72 static void image_viewer_load_image(ImageViewer
*imageviewer
)
74 GtkAllocation allocation
;
75 GdkPixbufAnimation
*animation
= NULL
;
76 GdkPixbuf
*pixbuf
= NULL
;
80 cm_return_if_fail(imageviewer
!= NULL
);
82 if (imageviewer
->mimeinfo
== NULL
)
85 stream
= procmime_get_part_as_inputstream(imageviewer
->mimeinfo
);
87 g_warning("Couldn't get image MIME part");
91 #if GDK_PIXBUF_MINOR >= 28
92 animation
= gdk_pixbuf_animation_new_from_stream(stream
, NULL
, &error
);
94 pixbuf
= gdk_pixbuf_new_from_stream(stream
, NULL
, &error
);
96 g_object_unref(stream
);
99 g_warning("Couldn't load image: %s\n", error
->message
);
104 #if GDK_PIXBUF_MINOR >= 28
105 if (gdk_pixbuf_animation_is_static_image(animation
)
106 || imageviewer
->resize_img
) {
107 pixbuf
= gdk_pixbuf_animation_get_static_image(animation
);
108 g_object_ref(pixbuf
);
109 g_object_unref(animation
);
112 if (imageviewer
->resize_img
) {
115 if (imageviewer
->resize_img
) {
116 gtk_widget_get_allocation(
117 gtk_widget_get_parent(imageviewer
->notebook
), &allocation
);
118 pixbuf
= claws_load_pixbuf_fitting(pixbuf
,
123 pixbuf
= claws_load_pixbuf_fitting(pixbuf
, -1, -1);
126 if (error
&& !pixbuf
&& !animation
) {
127 gtk_label_set_text(GTK_LABEL(imageviewer
->error_lbl
), _("Error:"));
128 gtk_label_set_text(GTK_LABEL(imageviewer
->error_msg
), error
->message
);
129 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 0);
130 gtk_widget_hide(imageviewer
->load_button
);
133 if (!pixbuf
&& !animation
) {
134 g_warning("Can't load the image.");
139 gtk_image_set_from_animation(GTK_IMAGE(imageviewer
->image
), animation
);
141 gtk_image_set_from_pixbuf(GTK_IMAGE(imageviewer
->image
), pixbuf
);
143 g_signal_handlers_block_by_func(G_OBJECT(imageviewer
->scrolledwin
),
144 G_CALLBACK(scrolledwin_resize_cb
), imageviewer
);
147 gtk_widget_show(imageviewer
->image
);
149 g_signal_handlers_unblock_by_func(G_OBJECT(imageviewer
->scrolledwin
),
150 G_CALLBACK(scrolledwin_resize_cb
), imageviewer
);
153 g_object_unref(pixbuf
);
155 g_object_unref(animation
);
158 static void image_viewer_set_notebook_page(MimeViewer
*_mimeviewer
)
160 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
162 if (!prefs_common
.display_img
)
163 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 0);
165 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 1);
168 static void image_viewer_show_mimepart(MimeViewer
*_mimeviewer
, const gchar
*file
, MimeInfo
*mimeinfo
)
170 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
172 debug_print("image_viewer_show_mimepart\n");
174 image_viewer_clear_viewer(_mimeviewer
);
175 g_free(imageviewer
->file
);
176 imageviewer
->file
= g_strdup(file
);
177 imageviewer
->mimeinfo
= mimeinfo
;
179 gtk_label_set_text(GTK_LABEL(imageviewer
->filename
),
180 (procmime_mimeinfo_get_parameter(mimeinfo
, "name") != NULL
)?
181 procmime_mimeinfo_get_parameter(mimeinfo
, "name") :
182 procmime_mimeinfo_get_parameter(mimeinfo
, "filename"));
183 gtk_label_set_text(GTK_LABEL(imageviewer
->filesize
), to_human_readable((goffset
)mimeinfo
->length
));
184 gtk_label_set_text(GTK_LABEL(imageviewer
->content_type
), mimeinfo
->subtype
);
185 gtk_label_set_text(GTK_LABEL(imageviewer
->error_lbl
), "");
186 gtk_label_set_text(GTK_LABEL(imageviewer
->error_msg
), "");
188 if (prefs_common
.display_img
)
189 image_viewer_load_image(imageviewer
);
192 static void image_viewer_clear_viewer(MimeViewer
*_mimeviewer
)
194 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
195 GtkAdjustment
*hadj
, *vadj
;
197 debug_print("image_viewer_clear_viewer\n");
199 image_viewer_set_notebook_page(_mimeviewer
);
201 if (imageviewer
->scrolledwin
) {
202 hadj
= gtk_scrolled_window_get_hadjustment
203 (GTK_SCROLLED_WINDOW(imageviewer
->scrolledwin
));
205 gtk_adjustment_set_value(hadj
, 0.0);
206 gtk_adjustment_changed(hadj
);
208 vadj
= gtk_scrolled_window_get_vadjustment
209 (GTK_SCROLLED_WINDOW(imageviewer
->scrolledwin
));
211 gtk_adjustment_set_value(vadj
, 0.0);
212 gtk_adjustment_changed(vadj
);
215 g_free(imageviewer
->file
);
216 imageviewer
->file
= NULL
;
217 imageviewer
->mimeinfo
= NULL
;
218 imageviewer
->resize_img
= prefs_common
.resize_img
;
221 static void image_viewer_destroy_viewer(MimeViewer
*_mimeviewer
)
223 ImageViewer
*imageviewer
= (ImageViewer
*) _mimeviewer
;
225 debug_print("image_viewer_destroy_viewer\n");
227 image_viewer_clear_viewer(_mimeviewer
);
228 g_object_unref(imageviewer
->notebook
);
232 void image_viewer_get_resized_size(gint w
, gint h
, gint aw
, gint ah
,
240 wratio
= (gfloat
)aw
/ (gfloat
)w
;
242 hratio
= (gfloat
)ah
/ (gfloat
)h
;
244 ratio
= (wratio
> hratio
) ? hratio
: wratio
;
246 *sw
= (gint
)(w
* ratio
);
247 *sh
= (gint
)(h
* ratio
);
250 if (*sw
<= 0 || *sh
<= 0) {
256 static void load_cb(GtkButton
*button
, ImageViewer
*imageviewer
)
258 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer
->notebook
), 1);
259 image_viewer_load_image(imageviewer
);
262 static gboolean
scrolledwin_button_cb(GtkWidget
*scrolledwin
, GdkEventButton
*event
,
263 ImageViewer
*imageviewer
)
265 if (event
->button
== 1 && imageviewer
->image
) {
266 imageviewer
->resize_img
= !imageviewer
->resize_img
;
267 image_viewer_load_image(imageviewer
);
273 static void scrolledwin_resize_cb(GtkWidget
*scrolledwin
, GtkAllocation
*alloc
,
274 ImageViewer
*imageviewer
)
276 if (imageviewer
->resize_img
)
277 image_viewer_load_image(imageviewer
);
280 static MimeViewer
*image_viewer_create(void)
282 ImageViewer
*imageviewer
;
284 * glade generated code (do not touch)
292 GtkWidget
*load_button
;
294 GtkWidget
*content_type
;
295 GtkWidget
*scrolledwin
;
296 GtkWidget
*error_lbl
;
297 GtkWidget
*error_msg
;
299 notebook
= gtk_notebook_new();
300 gtk_widget_show(notebook
);
301 gtk_widget_set_can_focus(notebook
, FALSE
);
302 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook
), FALSE
);
303 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook
), FALSE
);
305 table1
= gtk_table_new(5, 3, FALSE
);
306 gtk_widget_show(table1
);
307 gtk_container_add(GTK_CONTAINER(notebook
), table1
);
308 gtk_container_set_border_width(GTK_CONTAINER(table1
), 8);
309 gtk_table_set_row_spacings(GTK_TABLE(table1
), 4);
310 gtk_table_set_col_spacings(GTK_TABLE(table1
), 4);
312 label3
= gtk_label_new(_("Filename:"));
313 gtk_widget_show(label3
);
314 gtk_table_attach(GTK_TABLE(table1
), label3
, 0, 1, 0, 1,
315 (GtkAttachOptions
) (GTK_FILL
),
316 (GtkAttachOptions
) (0), 0, 0);
317 gtk_misc_set_alignment(GTK_MISC(label3
), 0, 0.5);
319 label4
= gtk_label_new(_("Filesize:"));
320 gtk_widget_show(label4
);
321 gtk_table_attach(GTK_TABLE(table1
), label4
, 0, 1, 1, 2,
322 (GtkAttachOptions
) (GTK_FILL
),
323 (GtkAttachOptions
) (0), 0, 0);
324 gtk_misc_set_alignment(GTK_MISC(label4
), 0, 0.5);
326 filename
= gtk_label_new("");
327 gtk_widget_show(filename
);
328 gtk_table_attach(GTK_TABLE(table1
), filename
, 1, 3, 0, 1,
329 (GtkAttachOptions
) (GTK_FILL
),
330 (GtkAttachOptions
) (0), 0, 0);
331 gtk_misc_set_alignment(GTK_MISC(filename
), 0, 0.5);
333 filesize
= gtk_label_new("");
334 gtk_widget_show(filesize
);
335 gtk_table_attach(GTK_TABLE(table1
), filesize
, 1, 3, 1, 2,
336 (GtkAttachOptions
) (GTK_FILL
),
337 (GtkAttachOptions
) (0), 0, 0);
338 gtk_misc_set_alignment(GTK_MISC(filesize
), 0, 0.5);
340 label5
= gtk_label_new(_("Content-Type:"));
341 gtk_widget_show(label5
);
342 gtk_table_attach(GTK_TABLE(table1
), label5
, 0, 1, 2, 3,
343 (GtkAttachOptions
) (GTK_FILL
),
344 (GtkAttachOptions
) (0), 0, 0);
345 gtk_misc_set_alignment(GTK_MISC(label5
), 0, 0.5);
347 content_type
= gtk_label_new("");
348 gtk_widget_show(content_type
);
349 gtk_table_attach(GTK_TABLE(table1
), content_type
, 1, 3, 2, 3,
350 (GtkAttachOptions
) (GTK_FILL
),
351 (GtkAttachOptions
) (0), 0, 0);
352 gtk_misc_set_alignment(GTK_MISC(content_type
), 0, 0.5);
354 error_lbl
= gtk_label_new("");
355 gtk_widget_show(error_lbl
);
356 gtk_table_attach(GTK_TABLE(table1
), error_lbl
, 0, 1, 3, 4,
357 (GtkAttachOptions
) (GTK_FILL
),
358 (GtkAttachOptions
) (0), 0, 0);
359 gtk_misc_set_alignment(GTK_MISC(error_lbl
), 0, 0.5);
361 error_msg
= gtk_label_new("");
362 gtk_widget_show(error_msg
);
363 gtk_table_attach(GTK_TABLE(table1
), error_msg
, 1, 3, 3, 4,
364 (GtkAttachOptions
) (GTK_FILL
),
365 (GtkAttachOptions
) (0), 0, 0);
366 gtk_misc_set_alignment(GTK_MISC(error_msg
), 0, 0.5);
368 load_button
= gtk_button_new_with_label(_("Load Image"));
369 gtk_widget_show(load_button
);
370 gtk_table_attach(GTK_TABLE(table1
), load_button
, 0, 1, 4, 5,
371 (GtkAttachOptions
) (GTK_FILL
),
372 (GtkAttachOptions
) (0), 0, 0);
374 scrolledwin
= gtk_scrolled_window_new(NULL
, NULL
);
375 gtk_widget_show(scrolledwin
);
376 gtk_container_add(GTK_CONTAINER(notebook
), scrolledwin
);
377 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin
),
378 GTK_POLICY_AUTOMATIC
,
379 GTK_POLICY_AUTOMATIC
);
384 debug_print("Creating image view...\n");
385 imageviewer
= g_new0(ImageViewer
, 1);
386 imageviewer
->mimeviewer
.factory
= &image_viewer_factory
;
388 imageviewer
->mimeviewer
.get_widget
= image_viewer_get_widget
;
389 imageviewer
->mimeviewer
.show_mimepart
= image_viewer_show_mimepart
;
390 imageviewer
->mimeviewer
.clear_viewer
= image_viewer_clear_viewer
;
391 imageviewer
->mimeviewer
.destroy_viewer
= image_viewer_destroy_viewer
;
392 imageviewer
->mimeviewer
.get_selection
= NULL
;
394 imageviewer
->resize_img
= prefs_common
.resize_img
;
396 imageviewer
->scrolledwin
= scrolledwin
;
397 imageviewer
->image
= gtk_image_new();
398 gtk_scrolled_window_add_with_viewport
399 (GTK_SCROLLED_WINDOW(imageviewer
->scrolledwin
),
401 imageviewer
->notebook
= notebook
;
402 imageviewer
->filename
= filename
;
403 imageviewer
->filesize
= filesize
;
404 imageviewer
->content_type
= content_type
;
405 imageviewer
->error_msg
= error_msg
;
406 imageviewer
->error_lbl
= error_lbl
;
407 imageviewer
->load_button
= load_button
;
409 g_object_ref(notebook
);
411 g_signal_connect(G_OBJECT(load_button
), "clicked",
412 G_CALLBACK(load_cb
), imageviewer
);
413 g_signal_connect(G_OBJECT(scrolledwin
), "button-press-event",
414 G_CALLBACK(scrolledwin_button_cb
), imageviewer
);
415 g_signal_connect(G_OBJECT(scrolledwin
), "size-allocate",
416 G_CALLBACK(scrolledwin_resize_cb
), imageviewer
);
418 image_viewer_set_notebook_page((MimeViewer
*)imageviewer
);
420 return (MimeViewer
*) imageviewer
;
423 static gchar
*content_types
[] =
426 MimeViewerFactory image_viewer_factory
=
434 void image_viewer_init(void)
436 mimeview_register_viewer_factory(&image_viewer_factory
);
439 void image_viewer_done(void)
441 mimeview_unregister_viewer_factory(&image_viewer_factory
);