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