* src/summaryview.c
[claws.git] / src / imageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 "mainwindow.h"
38 #include "prefs_common.h"
39 #include "procmime.h"
40 #include "imageview.h"
41 #include "utils.h"
42
43 ImageView *imageview_create(void)
44 {
45         ImageView *imageview;
46         GtkWidget *scrolledwin;
47
48         debug_print(_("Creating image view...\n"));
49         imageview = g_new0(ImageView, 1);
50
51         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
52         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
53                                        GTK_POLICY_AUTOMATIC,
54                                        GTK_POLICY_AUTOMATIC);
55         gtk_widget_set_usize(scrolledwin, prefs_common.mainview_width, -1);
56
57         gtk_widget_show_all(scrolledwin);
58
59         imageview->scrolledwin  = scrolledwin;
60         imageview->image        = NULL;
61
62         return imageview;
63 }
64
65 void imageview_init(ImageView *imageview)
66 {
67 }
68
69 #if HAVE_GDK_PIXBUF
70 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
71                           const gchar *file)
72 {
73         GdkPixbuf *pixbuf;
74         GdkPixmap *pixmap;
75         GdkBitmap *mask;
76
77         imageview_clear(imageview);
78
79         pixbuf = gdk_pixbuf_new_from_file(file);
80
81         if (!pixbuf) {
82                 g_warning(_("Can't load the image."));  
83                 return;
84         }
85
86         if (imageview->messageview->mainwin)
87                 main_window_cursor_wait(imageview->messageview->mainwin);
88
89         gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 0);
90
91         if (!imageview->image) {
92                 imageview->image = gtk_pixmap_new(pixmap, mask);
93
94                 gtk_scrolled_window_add_with_viewport
95                         (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
96                          imageview->image);
97         } else
98                 gtk_pixmap_set(GTK_PIXMAP(imageview->image), pixmap, mask);
99
100         gtk_widget_show(imageview->image);
101
102         gdk_pixbuf_unref(pixbuf);
103
104         if (imageview->messageview->mainwin)
105                 main_window_cursor_normal(imageview->messageview->mainwin);
106 }
107 #else
108 #if HAVE_GDK_IMLIB
109 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
110                           const gchar *file)
111 {
112         GdkImlibImage *im;
113
114         imageview_clear(imageview);
115
116         im = gdk_imlib_load_image((gchar *)file);
117
118         if (!im) {
119                 g_warning(_("Can't load the image."));  
120                 return;
121         }
122
123         if (imageview->messageview->mainwin)
124                 main_window_cursor_wait(imageview->messageview->mainwin);
125
126         gdk_imlib_render(im, im->rgb_width, im->rgb_height);
127
128         if (!imageview->image) {
129                 imageview->image = gtk_pixmap_new(gdk_imlib_move_image(im),
130                                                   gdk_imlib_move_mask(im));
131
132                 gtk_scrolled_window_add_with_viewport
133                         (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
134                          imageview->image);
135         } else
136                 gtk_pixmap_set(GTK_PIXMAP(imageview->image),
137                                gdk_imlib_move_image(im),
138                                gdk_imlib_move_mask(im));      
139
140         gtk_widget_show(imageview->image);
141
142         gdk_imlib_destroy_image(im);
143
144         if (imageview->messageview->mainwin)
145                 main_window_cursor_normal(imageview->messageview->mainwin);
146 }
147 #else
148 void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
149                           const gchar *file)
150 {
151 }
152 #endif /* HAVE_GDK_IMLIB */
153 #endif /* HAVE_GDK_PIXBUF */
154
155 void imageview_clear(ImageView *imageview)
156 {
157         GtkAdjustment *hadj, *vadj;
158
159         if (imageview->image)
160                 gtk_pixmap_set(GTK_PIXMAP(imageview->image), NULL, NULL);
161         hadj = gtk_scrolled_window_get_hadjustment
162                 (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
163         gtk_adjustment_set_value(hadj, 0.0);
164         vadj = gtk_scrolled_window_get_vadjustment
165                 (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
166         gtk_adjustment_set_value(vadj, 0.0);
167 }
168
169 void imageview_destroy(ImageView *imageview)
170 {
171         g_free(imageview);
172 }