Statistics
| Revision:

root / src / imageview.c @ 3036

History | View | Annotate | Download (6.1 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2005 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 <glib/gi18n.h>
26
#include <gtk/gtkscrolledwindow.h>
27
#include <gtk/gtkimage.h>
28
#include <gdk-pixbuf/gdk-pixbuf.h>
29
30
#include "mainwindow.h"
31
#include "prefs_common.h"
32
#include "procmime.h"
33
#include "imageview.h"
34
#include "utils.h"
35
36
static gboolean get_resized_size(gint         w,
37
                                 gint         h,
38
                                 gint         aw,
39
                                 gint         ah,
40
                                 gint        *sw,
41
                                 gint        *sh);
42
43
static gint button_press_cb        (GtkWidget        *widget,
44
                                 GdkEventButton        *event,
45
                                 gpointer         data);
46
static void size_allocate_cb        (GtkWidget        *widget,
47
                                 GtkAllocation        *allocation,
48
                                 gpointer         data);
49
50
ImageView *imageview_create(void)
51
{
52
        ImageView *imageview;
53
        GtkWidget *scrolledwin;
54
55
        debug_print(_("Creating image view...\n"));
56
        imageview = g_new0(ImageView, 1);
57
58
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
59
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
60
                                       GTK_POLICY_AUTOMATIC,
61
                                       GTK_POLICY_AUTOMATIC);
62
        gtk_widget_set_size_request
63
                (scrolledwin, prefs_common.mainview_width, -1);
64
65
        g_signal_connect(G_OBJECT(scrolledwin), "button_press_event",
66
                         G_CALLBACK(button_press_cb), imageview);
67
        g_signal_connect(G_OBJECT(scrolledwin), "size_allocate",
68
                         G_CALLBACK(size_allocate_cb), imageview);
69
70
        gtk_widget_show_all(scrolledwin);
71
72
        imageview->scrolledwin  = scrolledwin;
73
        imageview->image        = NULL;
74
        imageview->image_data   = NULL;
75
        imageview->resize       = FALSE;
76
        imageview->resizing     = FALSE;
77
78
        return imageview;
79
}
80
81
void imageview_init(ImageView *imageview)
82
{
83
}
84
85
void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
86
                          const gchar *file, gboolean resize)
87
{
88
        GdkPixbuf *pixbuf;
89
        GError *error = NULL;
90
91
        g_return_if_fail(imageview != NULL);
92
        g_return_if_fail(imageview->scrolledwin != NULL);
93
        g_return_if_fail(imageview->scrolledwin->parent != NULL);
94
95
        if (file) {
96
                imageview_clear(imageview);
97
                pixbuf = gdk_pixbuf_new_from_file(file, &error);
98
                imageview->image_data = pixbuf;
99
        } else {
100
                pixbuf = (GdkPixbuf *)imageview->image_data;
101
        }
102
103
        if (error != NULL) {
104
                g_warning("%s\n", error->message);
105
                g_error_free(error);
106
        }
107
108
        if (!pixbuf) {
109
                g_warning(_("Can't load the image."));        
110
                return;
111
        }
112
113
        imageview->resize = resize;
114
115
        if (resize) {
116
                pixbuf = imageview_get_resized_pixbuf
117
                        (pixbuf, imageview->scrolledwin->parent, 8);
118
        } else
119
                g_object_ref(pixbuf);
120
121
        if (!imageview->image) {
122
                imageview->image = gtk_image_new_from_pixbuf(pixbuf);
123
124
                gtk_scrolled_window_add_with_viewport
125
                        (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
126
                         imageview->image);
127
        } else
128
                gtk_image_set_from_pixbuf(GTK_IMAGE(imageview->image), pixbuf);
129
130
        g_object_unref(pixbuf);
131
132
        gtk_widget_show(imageview->image);
133
}
134
135
void imageview_clear(ImageView *imageview)
136
{
137
        GtkAdjustment *hadj, *vadj;
138
139
        if (imageview->image)
140
                gtk_image_set_from_pixmap(GTK_IMAGE(imageview->image),
141
                                          NULL, NULL);
142
        hadj = gtk_scrolled_window_get_hadjustment
143
                (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
144
        gtk_adjustment_set_value(hadj, 0.0);
145
        vadj = gtk_scrolled_window_get_vadjustment
146
                (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
147
        gtk_adjustment_set_value(vadj, 0.0);
148
149
        if (imageview->image_data) {
150
                g_object_unref(imageview->image_data);
151
                imageview->image_data = NULL;
152
        }
153
}
154
155
void imageview_destroy(ImageView *imageview)
156
{
157
        imageview_clear(imageview);
158
        g_free(imageview);
159
}
160
161
GdkPixbuf *imageview_get_resized_pixbuf(GdkPixbuf *pixbuf, GtkWidget *parent,
162
                                        gint margin)
163
{
164
        gint avail_width;
165
        gint avail_height;
166
        gint new_width;
167
        gint new_height;
168
169
        avail_width = parent->allocation.width;
170
        avail_height = parent->allocation.height;
171
        if (avail_width > margin) avail_width -= margin;
172
        if (avail_height > margin) avail_height -= margin;
173
174
        if (get_resized_size(gdk_pixbuf_get_width(pixbuf),
175
                             gdk_pixbuf_get_height(pixbuf),
176
                             avail_width, avail_height,
177
                             &new_width, &new_height))
178
                return gdk_pixbuf_scale_simple
179
                        (pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
180
181
        g_object_ref(pixbuf);
182
        return pixbuf;
183
}
184
185
static gboolean get_resized_size(gint w, gint h, gint aw, gint ah,
186
                                 gint *sw, gint *sh)
187
{
188
        gfloat wratio = 1.0;
189
        gfloat hratio = 1.0;
190
        gfloat ratio  = 1.0;
191
192
        if (w <= aw && h <= ah) {
193
                *sw = w;
194
                *sh = h;
195
                return FALSE;
196
        }
197
198
        if (w > aw)
199
                wratio = (gfloat)aw / (gfloat)w;
200
        if (h > ah)
201
                hratio = (gfloat)ah / (gfloat)h;
202
203
        ratio = (wratio > hratio) ? hratio : wratio;
204
205
        *sw = (gint)(w * ratio);
206
        *sh = (gint)(h * ratio);
207
208
        /* restrict minimum size */
209
        if (*sw < 16 || *sh < 16) {
210
                wratio = 16.0 / (gfloat)w;
211
                hratio = 16.0 / (gfloat)h;
212
                ratio = (wratio > hratio) ? wratio : hratio;
213
                if (ratio >= 1.0) {
214
                        *sw = w;
215
                        *sh = h;
216
                } else {
217
                        *sw = (gint)(w * ratio);
218
                        *sh = (gint)(h * ratio);
219
                }
220
        }
221
222
        return TRUE;
223
}
224
225
static gint button_press_cb(GtkWidget *widget, GdkEventButton *event,
226
                            gpointer data)
227
{
228
        ImageView *imageview = (ImageView *)data;
229
230
        if (event->button == 1 && imageview->image) {
231
                imageview_show_image(imageview, NULL, NULL, !imageview->resize);
232
                return TRUE;
233
        }
234
        return FALSE;
235
}
236
237
static void size_allocate_cb(GtkWidget *widget,GtkAllocation *allocation,
238
                             gpointer data)
239
{
240
        ImageView *imageview = (ImageView *)data;
241
242
        if (imageview->resize) {
243
                if (imageview->resizing) {
244
                        imageview->resizing = FALSE;
245
                        return;
246
                }
247
                if (!imageview->scrolledwin->parent)
248
                        return;
249
                if (!imageview->image_data)
250
                        return;
251
                imageview_show_image(imageview, NULL, NULL, TRUE);
252
                imageview->resizing = TRUE;
253
        }
254
}