rework image viewer
[claws.git] / src / image_viewer.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2020 the Claws Mail team and 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "procmime.h"
30 #include "file-utils.h"
31 #include "utils.h"
32 #include "mimeview.h"
33
34 #include "prefs_common.h"
35
36 typedef struct _ImageViewer ImageViewer;
37
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);
44 struct _ImageViewer
45 {
46         MimeViewer mimeviewer;
47
48         gchar     *file;
49         MimeInfo  *mimeinfo;
50         gboolean   resize_img;
51         gboolean   fit_img_height;
52
53         GtkWidget *scrolledwin;
54         GtkWidget *image;
55         GtkWidget *notebook;
56         GtkWidget *filename;
57         GtkWidget *filesize;
58         GtkWidget *error_lbl;
59         GtkWidget *error_msg;
60         GtkWidget *content_type;
61         GtkWidget *load_button;
62 };
63
64 static GtkWidget *image_viewer_get_widget(MimeViewer *_mimeviewer)
65 {
66         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
67
68         debug_print("image_viewer_get_widget\n");
69
70         return imageviewer->notebook;
71 }
72
73 static void image_viewer_load_image(ImageViewer *imageviewer)
74 {
75         GtkAllocation allocation;
76         GdkPixbufAnimation *animation = NULL;
77         GdkPixbuf *pixbuf = NULL;
78         GError *error = NULL;
79         GInputStream *stream;
80
81         cm_return_if_fail(imageviewer != NULL);
82
83         if (imageviewer->mimeinfo == NULL)
84                 return;
85
86         stream = procmime_get_part_as_inputstream(imageviewer->mimeinfo);
87         if (stream == NULL) {
88                 g_warning("Couldn't get image MIME part");
89                 return;
90         }
91
92 #if GDK_PIXBUF_MINOR >= 28
93         animation = gdk_pixbuf_animation_new_from_stream(stream, NULL, &error);
94 #else
95         pixbuf = gdk_pixbuf_new_from_stream(stream, NULL, &error);
96 #endif
97         g_object_unref(stream);
98
99         if (error != NULL) {
100                 g_warning("Couldn't load image: %s\n", error->message);
101                 g_error_free(error);
102                 return;
103         }
104
105 #if GDK_PIXBUF_MINOR >= 28
106         if (gdk_pixbuf_animation_is_static_image(animation)
107             || imageviewer->resize_img || imageviewer->fit_img_height) {
108                 pixbuf = gdk_pixbuf_animation_get_static_image(animation);
109                 g_object_ref(pixbuf);
110                 g_object_unref(animation);
111                 animation = NULL;
112 #else
113         if (imageviewer->resize_img || imageviewer->fit_img_height) {
114 #endif
115
116                 if (imageviewer->resize_img) {
117                         gtk_widget_get_allocation(imageviewer->scrolledwin, &allocation);
118                         pixbuf = claws_load_pixbuf_fitting(pixbuf, FALSE,
119                                 imageviewer->fit_img_height,
120                                 allocation.width,
121                                 allocation.height);
122                 }
123                 else
124                         pixbuf = claws_load_pixbuf_fitting(pixbuf, FALSE, imageviewer->fit_img_height, -1, -1);
125         }
126
127         if (error && !pixbuf && !animation) {
128                 gtk_label_set_text(GTK_LABEL(imageviewer->error_lbl), _("Error:"));
129                 gtk_label_set_text(GTK_LABEL(imageviewer->error_msg), error->message);
130                 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 0);
131                 gtk_widget_hide(imageviewer->load_button);
132                 g_error_free(error);
133         }
134         if (!pixbuf && !animation) {
135                 g_warning("Can't load the image.");     
136                 return;
137         }
138
139         if (animation)
140                 gtk_image_set_from_animation(GTK_IMAGE(imageviewer->image), animation);
141         else
142                 gtk_image_set_from_pixbuf(GTK_IMAGE(imageviewer->image), pixbuf);
143
144         g_signal_handlers_block_by_func(G_OBJECT(imageviewer->scrolledwin), 
145                          G_CALLBACK(scrolledwin_resize_cb), imageviewer);
146
147
148         gtk_widget_show(imageviewer->image);
149         GTK_EVENTS_FLUSH();
150         g_signal_handlers_unblock_by_func(G_OBJECT(imageviewer->scrolledwin), 
151                          G_CALLBACK(scrolledwin_resize_cb), imageviewer);
152
153         if (pixbuf)
154                 g_object_unref(pixbuf);
155         if (animation)
156                 g_object_unref(animation);
157 }
158
159 static void image_viewer_set_notebook_page(MimeViewer *_mimeviewer)
160 {
161         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
162
163         if (!prefs_common.display_img)
164                 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 0);
165         else
166                 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 1);
167 }
168
169 static void image_viewer_show_mimepart(MimeViewer *_mimeviewer, const gchar *file, MimeInfo *mimeinfo)
170 {
171         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
172
173         debug_print("image_viewer_show_mimepart\n");
174
175         image_viewer_clear_viewer(_mimeviewer);
176         g_free(imageviewer->file);
177         imageviewer->file = g_strdup(file);
178         imageviewer->mimeinfo = mimeinfo;
179
180         gtk_label_set_text(GTK_LABEL(imageviewer->filename),
181                            (procmime_mimeinfo_get_parameter(mimeinfo, "name") != NULL)?
182                            procmime_mimeinfo_get_parameter(mimeinfo, "name") :
183                            procmime_mimeinfo_get_parameter(mimeinfo, "filename"));
184         gtk_label_set_text(GTK_LABEL(imageviewer->filesize), to_human_readable((goffset)mimeinfo->length));
185         gtk_label_set_text(GTK_LABEL(imageviewer->content_type), mimeinfo->subtype);
186         gtk_label_set_text(GTK_LABEL(imageviewer->error_lbl), "");
187         gtk_label_set_text(GTK_LABEL(imageviewer->error_msg), "");
188
189         if (prefs_common.display_img)
190                 image_viewer_load_image(imageviewer);
191 }
192
193 static void image_viewer_clear_viewer(MimeViewer *_mimeviewer)
194 {
195         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
196         GtkAdjustment *hadj, *vadj;
197
198         debug_print("image_viewer_clear_viewer\n");
199
200         image_viewer_set_notebook_page(_mimeviewer);
201
202         if (imageviewer->scrolledwin) {
203                 hadj = gtk_scrolled_window_get_hadjustment
204                         (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin));
205                 if (hadj) {
206                         gtk_adjustment_set_value(hadj, 0.0);
207                         gtk_adjustment_changed(hadj);
208                 }
209                 vadj = gtk_scrolled_window_get_vadjustment
210                         (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin));
211                 if (vadj) {
212                         gtk_adjustment_set_value(vadj, 0.0);
213                         gtk_adjustment_changed(vadj);
214                 }
215         }
216         g_free(imageviewer->file);
217         imageviewer->file = NULL;
218         imageviewer->mimeinfo = NULL;
219         imageviewer->resize_img = prefs_common.resize_img;
220         imageviewer->fit_img_height = prefs_common.fit_img_height;
221 }
222
223 static void image_viewer_destroy_viewer(MimeViewer *_mimeviewer)
224 {
225         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
226
227         debug_print("image_viewer_destroy_viewer\n");
228
229         image_viewer_clear_viewer(_mimeviewer);
230         g_object_unref(imageviewer->notebook);
231         g_free(imageviewer);
232 }
233
234 void image_viewer_get_resized_size(gint w, gint h, gint aw, gint ah,
235                              gint *sw, gint *sh)
236 {
237         gfloat wratio = 1.0;
238         gfloat hratio = 1.0;
239         gfloat ratio  = 1.0;
240
241         if (w > aw)
242                 wratio = (gfloat)aw / (gfloat)w;
243         if (h > ah)
244                 hratio = (gfloat)ah / (gfloat)h;
245
246         ratio = (wratio > hratio) ? hratio : wratio;
247
248         *sw = (gint)(w * ratio);
249         *sh = (gint)(h * ratio);
250
251         /* be paranoid */
252         if (*sw <= 0 || *sh <= 0) {
253                 *sw = w;
254                 *sh = h;
255         }
256 }
257
258 static void load_cb(GtkButton *button, ImageViewer *imageviewer)
259 {
260         gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 1);
261         image_viewer_load_image(imageviewer);
262 }
263
264 static gboolean scrolledwin_button_cb(GtkWidget *scrolledwin, GdkEventButton *event,
265                                       ImageViewer *imageviewer)
266 {
267         if (event->button == 1 && imageviewer->image) {
268                 imageviewer->resize_img = !imageviewer->resize_img;
269                 image_viewer_load_image(imageviewer);
270                 return TRUE;
271         } else if (event->button == 3 && imageviewer->image) {
272                 imageviewer->fit_img_height = !imageviewer->fit_img_height;
273                 image_viewer_load_image(imageviewer);
274                 return TRUE;
275         }
276         return FALSE;
277 }
278
279 static void scrolledwin_resize_cb(GtkWidget *scrolledwin, GtkAllocation *alloc,
280                                   ImageViewer *imageviewer)
281 {
282         if (imageviewer->resize_img)
283                 image_viewer_load_image(imageviewer);
284 }
285
286 static MimeViewer *image_viewer_create(void)
287 {
288         ImageViewer *imageviewer;
289         /*
290          *  glade generated code (do not touch)
291          */
292         GtkWidget *notebook;
293         GtkWidget *table1;
294         GtkWidget *label3;
295         GtkWidget *label4;
296         GtkWidget *filename;
297         GtkWidget *filesize;
298         GtkWidget *load_button;
299         GtkWidget *label5;
300         GtkWidget *content_type;
301         GtkWidget *scrolledwin;
302         GtkWidget *error_lbl;
303         GtkWidget *error_msg;
304
305         notebook = gtk_notebook_new();
306         gtk_widget_show(notebook);
307         gtk_widget_set_can_focus(notebook, FALSE);
308         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
309         gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
310
311         table1 = gtk_table_new(5, 3, FALSE);
312         gtk_widget_show(table1);
313         gtk_container_add(GTK_CONTAINER(notebook), table1);
314         gtk_container_set_border_width(GTK_CONTAINER(table1), 8);
315         gtk_table_set_row_spacings(GTK_TABLE(table1), 4);
316         gtk_table_set_col_spacings(GTK_TABLE(table1), 4);
317
318         label3 = gtk_label_new(_("Filename:"));
319         gtk_widget_show(label3);
320         gtk_table_attach(GTK_TABLE(table1), label3, 0, 1, 0, 1,
321                          (GtkAttachOptions) (GTK_FILL),
322                          (GtkAttachOptions) (0), 0, 0);
323         gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
324
325         label4 = gtk_label_new(_("Filesize:"));
326         gtk_widget_show(label4);
327         gtk_table_attach(GTK_TABLE(table1), label4, 0, 1, 1, 2,
328                          (GtkAttachOptions) (GTK_FILL),
329                          (GtkAttachOptions) (0), 0, 0);
330         gtk_misc_set_alignment(GTK_MISC(label4), 0, 0.5);
331
332         filename = gtk_label_new("");
333         gtk_widget_show(filename);
334         gtk_table_attach(GTK_TABLE(table1), filename, 1, 3, 0, 1,
335                          (GtkAttachOptions) (GTK_FILL),
336                          (GtkAttachOptions) (0), 0, 0);
337         gtk_misc_set_alignment(GTK_MISC(filename), 0, 0.5);
338
339         filesize = gtk_label_new("");
340         gtk_widget_show(filesize);
341         gtk_table_attach(GTK_TABLE(table1), filesize, 1, 3, 1, 2,
342                          (GtkAttachOptions) (GTK_FILL),
343                          (GtkAttachOptions) (0), 0, 0);
344         gtk_misc_set_alignment(GTK_MISC(filesize), 0, 0.5);
345
346         label5 = gtk_label_new(_("Content-Type:"));
347         gtk_widget_show(label5);
348         gtk_table_attach(GTK_TABLE(table1), label5, 0, 1, 2, 3,
349                          (GtkAttachOptions) (GTK_FILL),
350                          (GtkAttachOptions) (0), 0, 0);
351         gtk_misc_set_alignment(GTK_MISC(label5), 0, 0.5);
352
353         content_type = gtk_label_new("");
354         gtk_widget_show(content_type);
355         gtk_table_attach(GTK_TABLE(table1), content_type, 1, 3, 2, 3,
356                          (GtkAttachOptions) (GTK_FILL),
357                          (GtkAttachOptions) (0), 0, 0);
358         gtk_misc_set_alignment(GTK_MISC(content_type), 0, 0.5);
359
360         error_lbl = gtk_label_new("");
361         gtk_widget_show(error_lbl);
362         gtk_table_attach(GTK_TABLE(table1), error_lbl, 0, 1, 3, 4,
363                          (GtkAttachOptions) (GTK_FILL),
364                          (GtkAttachOptions) (0), 0, 0);
365         gtk_misc_set_alignment(GTK_MISC(error_lbl), 0, 0.5);
366
367         error_msg = gtk_label_new("");
368         gtk_widget_show(error_msg);
369         gtk_table_attach(GTK_TABLE(table1), error_msg, 1, 3, 3, 4,
370                          (GtkAttachOptions) (GTK_FILL),
371                          (GtkAttachOptions) (0), 0, 0);
372         gtk_misc_set_alignment(GTK_MISC(error_msg), 0, 0.5);
373
374         load_button = gtk_button_new_with_label(_("Load Image"));
375         gtk_widget_show(load_button);
376         gtk_table_attach(GTK_TABLE(table1), load_button, 0, 1, 4, 5,
377                          (GtkAttachOptions) (GTK_FILL),
378                          (GtkAttachOptions) (0), 0, 0);
379
380         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
381         gtk_widget_show(scrolledwin);
382         gtk_container_add(GTK_CONTAINER(notebook), scrolledwin);
383         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
384                                        GTK_POLICY_AUTOMATIC,
385                                        GTK_POLICY_AUTOMATIC);
386         /*
387          *  end of glade code
388          */
389
390         debug_print("Creating image view...\n");
391         imageviewer = g_new0(ImageViewer, 1);
392         imageviewer->mimeviewer.factory = &image_viewer_factory;
393
394         imageviewer->mimeviewer.get_widget = image_viewer_get_widget;
395         imageviewer->mimeviewer.show_mimepart = image_viewer_show_mimepart;
396         imageviewer->mimeviewer.clear_viewer = image_viewer_clear_viewer;
397         imageviewer->mimeviewer.destroy_viewer = image_viewer_destroy_viewer;
398         imageviewer->mimeviewer.get_selection = NULL;
399
400         imageviewer->resize_img   = prefs_common.resize_img;
401         imageviewer->fit_img_height   = prefs_common.fit_img_height;
402
403         imageviewer->scrolledwin  = scrolledwin;
404         imageviewer->image = gtk_image_new();
405         gtk_scrolled_window_add_with_viewport
406                 (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin),
407                  imageviewer->image);
408         imageviewer->notebook     = notebook;
409         imageviewer->filename     = filename;
410         imageviewer->filesize     = filesize;
411         imageviewer->content_type = content_type;
412         imageviewer->error_msg    = error_msg;
413         imageviewer->error_lbl    = error_lbl;
414         imageviewer->load_button = load_button;
415
416         g_object_ref(notebook);
417
418         g_signal_connect(G_OBJECT(load_button), "clicked",
419                          G_CALLBACK(load_cb), imageviewer);
420         g_signal_connect(G_OBJECT(scrolledwin), "button-press-event",
421                          G_CALLBACK(scrolledwin_button_cb), imageviewer);
422         g_signal_connect(G_OBJECT(scrolledwin), "size-allocate",
423                          G_CALLBACK(scrolledwin_resize_cb), imageviewer);
424
425         image_viewer_set_notebook_page((MimeViewer *)imageviewer);
426
427         return (MimeViewer *) imageviewer;
428 }
429
430 static gchar *content_types[] =
431         {"image/*", NULL};
432
433 MimeViewerFactory image_viewer_factory =
434 {
435         content_types,
436         0,
437         
438         image_viewer_create,
439 };
440
441 void image_viewer_init(void)
442 {
443         mimeview_register_viewer_factory(&image_viewer_factory);
444 }
445
446 void image_viewer_done(void)
447 {
448         mimeview_unregister_viewer_factory(&image_viewer_factory);
449 }