Statistics
| Revision:

root / src / imageview.c @ 3036

History | View | Annotate | Download (6.1 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 155 hiro
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
#ifdef HAVE_CONFIG_H
21 1 hiro
#  include "config.h"
22 1 hiro
#endif
23 1 hiro
24 1 hiro
#include <glib.h>
25 92 hiro
#include <glib/gi18n.h>
26 1 hiro
#include <gtk/gtkscrolledwindow.h>
27 1 hiro
#include <gtk/gtkimage.h>
28 155 hiro
#include <gdk-pixbuf/gdk-pixbuf.h>
29 1 hiro
30 1 hiro
#include "mainwindow.h"
31 1 hiro
#include "prefs_common.h"
32 1 hiro
#include "procmime.h"
33 1 hiro
#include "imageview.h"
34 1 hiro
#include "utils.h"
35 1 hiro
36 233 hiro
static gboolean get_resized_size(gint         w,
37 1 hiro
                                 gint         h,
38 1 hiro
                                 gint         aw,
39 1 hiro
                                 gint         ah,
40 1 hiro
                                 gint        *sw,
41 1 hiro
                                 gint        *sh);
42 1 hiro
43 1 hiro
static gint button_press_cb        (GtkWidget        *widget,
44 1 hiro
                                 GdkEventButton        *event,
45 1 hiro
                                 gpointer         data);
46 1 hiro
static void size_allocate_cb        (GtkWidget        *widget,
47 1 hiro
                                 GtkAllocation        *allocation,
48 1 hiro
                                 gpointer         data);
49 1 hiro
50 1 hiro
ImageView *imageview_create(void)
51 1 hiro
{
52 1 hiro
        ImageView *imageview;
53 1 hiro
        GtkWidget *scrolledwin;
54 1 hiro
55 1 hiro
        debug_print(_("Creating image view...\n"));
56 1 hiro
        imageview = g_new0(ImageView, 1);
57 1 hiro
58 1 hiro
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
59 1 hiro
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
60 1 hiro
                                       GTK_POLICY_AUTOMATIC,
61 1 hiro
                                       GTK_POLICY_AUTOMATIC);
62 1 hiro
        gtk_widget_set_size_request
63 1 hiro
                (scrolledwin, prefs_common.mainview_width, -1);
64 1 hiro
65 1 hiro
        g_signal_connect(G_OBJECT(scrolledwin), "button_press_event",
66 1 hiro
                         G_CALLBACK(button_press_cb), imageview);
67 1 hiro
        g_signal_connect(G_OBJECT(scrolledwin), "size_allocate",
68 1 hiro
                         G_CALLBACK(size_allocate_cb), imageview);
69 1 hiro
70 1 hiro
        gtk_widget_show_all(scrolledwin);
71 1 hiro
72 1 hiro
        imageview->scrolledwin  = scrolledwin;
73 1 hiro
        imageview->image        = NULL;
74 1 hiro
        imageview->image_data   = NULL;
75 1 hiro
        imageview->resize       = FALSE;
76 1 hiro
        imageview->resizing     = FALSE;
77 1 hiro
78 1 hiro
        return imageview;
79 1 hiro
}
80 1 hiro
81 1 hiro
void imageview_init(ImageView *imageview)
82 1 hiro
{
83 1 hiro
}
84 1 hiro
85 1 hiro
void imageview_show_image(ImageView *imageview, MimeInfo *mimeinfo,
86 1 hiro
                          const gchar *file, gboolean resize)
87 1 hiro
{
88 1 hiro
        GdkPixbuf *pixbuf;
89 1 hiro
        GError *error = NULL;
90 1 hiro
91 1 hiro
        g_return_if_fail(imageview != NULL);
92 379 hiro
        g_return_if_fail(imageview->scrolledwin != NULL);
93 379 hiro
        g_return_if_fail(imageview->scrolledwin->parent != NULL);
94 1 hiro
95 1 hiro
        if (file) {
96 1 hiro
                imageview_clear(imageview);
97 1 hiro
                pixbuf = gdk_pixbuf_new_from_file(file, &error);
98 1 hiro
                imageview->image_data = pixbuf;
99 1 hiro
        } else {
100 1 hiro
                pixbuf = (GdkPixbuf *)imageview->image_data;
101 1 hiro
        }
102 1 hiro
103 1 hiro
        if (error != NULL) {
104 1 hiro
                g_warning("%s\n", error->message);
105 1 hiro
                g_error_free(error);
106 1 hiro
        }
107 1 hiro
108 1 hiro
        if (!pixbuf) {
109 1 hiro
                g_warning(_("Can't load the image."));
110 1 hiro
                return;
111 1 hiro
        }
112 1 hiro
113 1 hiro
        imageview->resize = resize;
114 1 hiro
115 1 hiro
        if (resize) {
116 155 hiro
                pixbuf = imageview_get_resized_pixbuf
117 155 hiro
                        (pixbuf, imageview->scrolledwin->parent, 8);
118 1 hiro
        } else
119 1 hiro
                g_object_ref(pixbuf);
120 1 hiro
121 1 hiro
        if (!imageview->image) {
122 1 hiro
                imageview->image = gtk_image_new_from_pixbuf(pixbuf);
123 1 hiro
124 1 hiro
                gtk_scrolled_window_add_with_viewport
125 1 hiro
                        (GTK_SCROLLED_WINDOW(imageview->scrolledwin),
126 1 hiro
                         imageview->image);
127 1 hiro
        } else
128 1 hiro
                gtk_image_set_from_pixbuf(GTK_IMAGE(imageview->image), pixbuf);
129 1 hiro
130 155 hiro
        g_object_unref(pixbuf);
131 1 hiro
132 1 hiro
        gtk_widget_show(imageview->image);
133 1 hiro
}
134 1 hiro
135 1 hiro
void imageview_clear(ImageView *imageview)
136 1 hiro
{
137 1 hiro
        GtkAdjustment *hadj, *vadj;
138 1 hiro
139 1 hiro
        if (imageview->image)
140 1 hiro
                gtk_image_set_from_pixmap(GTK_IMAGE(imageview->image),
141 1 hiro
                                          NULL, NULL);
142 1 hiro
        hadj = gtk_scrolled_window_get_hadjustment
143 1 hiro
                (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
144 1 hiro
        gtk_adjustment_set_value(hadj, 0.0);
145 1 hiro
        vadj = gtk_scrolled_window_get_vadjustment
146 1 hiro
                (GTK_SCROLLED_WINDOW(imageview->scrolledwin));
147 1 hiro
        gtk_adjustment_set_value(vadj, 0.0);
148 1 hiro
149 1 hiro
        if (imageview->image_data) {
150 155 hiro
                g_object_unref(imageview->image_data);
151 1 hiro
                imageview->image_data = NULL;
152 1 hiro
        }
153 1 hiro
}
154 1 hiro
155 1 hiro
void imageview_destroy(ImageView *imageview)
156 1 hiro
{
157 1 hiro
        imageview_clear(imageview);
158 1 hiro
        g_free(imageview);
159 1 hiro
}
160 1 hiro
161 155 hiro
GdkPixbuf *imageview_get_resized_pixbuf(GdkPixbuf *pixbuf, GtkWidget *parent,
162 155 hiro
                                        gint margin)
163 155 hiro
{
164 155 hiro
        gint avail_width;
165 155 hiro
        gint avail_height;
166 155 hiro
        gint new_width;
167 155 hiro
        gint new_height;
168 155 hiro
169 155 hiro
        avail_width = parent->allocation.width;
170 155 hiro
        avail_height = parent->allocation.height;
171 155 hiro
        if (avail_width > margin) avail_width -= margin;
172 155 hiro
        if (avail_height > margin) avail_height -= margin;
173 155 hiro
174 233 hiro
        if (get_resized_size(gdk_pixbuf_get_width(pixbuf),
175 233 hiro
                             gdk_pixbuf_get_height(pixbuf),
176 233 hiro
                             avail_width, avail_height,
177 233 hiro
                             &new_width, &new_height))
178 233 hiro
                return gdk_pixbuf_scale_simple
179 233 hiro
                        (pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
180 155 hiro
181 233 hiro
        g_object_ref(pixbuf);
182 233 hiro
        return pixbuf;
183 155 hiro
}
184 155 hiro
185 233 hiro
static gboolean get_resized_size(gint w, gint h, gint aw, gint ah,
186 233 hiro
                                 gint *sw, gint *sh)
187 1 hiro
{
188 1 hiro
        gfloat wratio = 1.0;
189 1 hiro
        gfloat hratio = 1.0;
190 1 hiro
        gfloat ratio  = 1.0;
191 1 hiro
192 1 hiro
        if (w <= aw && h <= ah) {
193 1 hiro
                *sw = w;
194 1 hiro
                *sh = h;
195 233 hiro
                return FALSE;
196 1 hiro
        }
197 1 hiro
198 1 hiro
        if (w > aw)
199 1 hiro
                wratio = (gfloat)aw / (gfloat)w;
200 1 hiro
        if (h > ah)
201 1 hiro
                hratio = (gfloat)ah / (gfloat)h;
202 1 hiro
203 1 hiro
        ratio = (wratio > hratio) ? hratio : wratio;
204 1 hiro
205 1 hiro
        *sw = (gint)(w * ratio);
206 1 hiro
        *sh = (gint)(h * ratio);
207 1 hiro
208 1 hiro
        /* restrict minimum size */
209 1 hiro
        if (*sw < 16 || *sh < 16) {
210 1 hiro
                wratio = 16.0 / (gfloat)w;
211 1 hiro
                hratio = 16.0 / (gfloat)h;
212 1 hiro
                ratio = (wratio > hratio) ? wratio : hratio;
213 1 hiro
                if (ratio >= 1.0) {
214 1 hiro
                        *sw = w;
215 1 hiro
                        *sh = h;
216 1 hiro
                } else {
217 1 hiro
                        *sw = (gint)(w * ratio);
218 1 hiro
                        *sh = (gint)(h * ratio);
219 1 hiro
                }
220 1 hiro
        }
221 233 hiro
222 233 hiro
        return TRUE;
223 1 hiro
}
224 1 hiro
225 1 hiro
static gint button_press_cb(GtkWidget *widget, GdkEventButton *event,
226 1 hiro
                            gpointer data)
227 1 hiro
{
228 1 hiro
        ImageView *imageview = (ImageView *)data;
229 1 hiro
230 1 hiro
        if (event->button == 1 && imageview->image) {
231 1 hiro
                imageview_show_image(imageview, NULL, NULL, !imageview->resize);
232 1 hiro
                return TRUE;
233 1 hiro
        }
234 1 hiro
        return FALSE;
235 1 hiro
}
236 1 hiro
237 1 hiro
static void size_allocate_cb(GtkWidget *widget,GtkAllocation *allocation,
238 1 hiro
                             gpointer data)
239 1 hiro
{
240 1 hiro
        ImageView *imageview = (ImageView *)data;
241 1 hiro
242 1 hiro
        if (imageview->resize) {
243 1 hiro
                if (imageview->resizing) {
244 1 hiro
                        imageview->resizing = FALSE;
245 1 hiro
                        return;
246 1 hiro
                }
247 379 hiro
                if (!imageview->scrolledwin->parent)
248 379 hiro
                        return;
249 778 hiro
                if (!imageview->image_data)
250 778 hiro
                        return;
251 1 hiro
                imageview_show_image(imageview, NULL, NULL, TRUE);
252 1 hiro
                imageview->resizing = TRUE;
253 1 hiro
        }
254 1 hiro
}