]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
9e6958dbaaf65956edcf9ae53e8c1f3f8032db31
[evince.git] / pixbuf / pixbuf-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2004, Anders Carlsson <andersca@gnome.org>
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, or (at your option)
8  * 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 #include "pixbuf-document.h"
21 #include "ev-document-thumbnails.h"
22
23 enum {
24         PROP_0,
25         PROP_TITLE
26 };
27
28 struct _PixbufDocumentClass
29 {
30         GObjectClass parent_class;
31 };
32
33 struct _PixbufDocument
34 {
35         GObject parent_instance;
36
37         GdkPixbuf *pixbuf;
38         GdkDrawable *target;
39         gdouble scale;
40
41         gint x_offset, y_offset;
42 };
43
44 typedef struct _PixbufDocumentClass PixbufDocumentClass;
45
46 static void pixbuf_document_document_iface_init (EvDocumentIface *iface);
47 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
48
49 G_DEFINE_TYPE_WITH_CODE (PixbufDocument, pixbuf_document, G_TYPE_OBJECT,
50                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
51                                                   pixbuf_document_document_iface_init);
52                          G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
53                                                 pixbuf_document_document_thumbnails_iface_init)                            
54                                    });
55
56 static GObjectClass *parent_class;
57
58 static gboolean
59 pixbuf_document_load (EvDocument  *document,
60                       const char  *uri,
61                       GError     **error)
62 {
63         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
64         
65         gchar *filename;
66         GdkPixbuf *pixbuf;
67
68         /* FIXME: We could actually load uris  */
69         filename = g_filename_from_uri (uri, NULL, error);
70         if (!filename)
71                 return FALSE;
72         
73         pixbuf = gdk_pixbuf_new_from_file (filename, error);
74
75         if (!pixbuf)
76                 return FALSE;
77
78         pixbuf_document->pixbuf = pixbuf;
79         
80         return TRUE;
81 }
82
83 static gboolean
84 pixbuf_document_save (EvDocument  *document,
85                       const char  *uri,
86                       GError     **error)
87 {
88         g_warning ("pixbuf_document_save not implemented"); /* FIXME */
89         return TRUE;
90 }
91
92 static int
93 pixbuf_document_get_n_pages (EvDocument  *document)
94 {
95         return 1;
96 }
97
98 static void
99 pixbuf_document_set_page (EvDocument  *document,
100                           int          page)
101 {
102         /* Do nothing */
103 }
104
105 static int
106 pixbuf_document_get_page (EvDocument  *document)
107 {
108         return 1;
109 }
110
111 static void
112 pixbuf_document_set_target (EvDocument  *document,
113                             GdkDrawable *target)
114 {
115         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
116
117         pixbuf_document->target = target;
118 }
119
120 static void
121 pixbuf_document_set_scale (EvDocument  *document,
122                            double       scale)
123 {
124         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
125
126         pixbuf_document->scale = scale;
127 }
128
129 static void
130 pixbuf_document_set_page_offset (EvDocument  *document,
131                               int          x,
132                               int          y)
133 {
134         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
135
136         pixbuf_document->x_offset = x;
137         pixbuf_document->y_offset = y;
138 }
139
140 static void
141 pixbuf_document_get_page_size (EvDocument   *document,
142                                int          *width,
143                                int          *height)
144 {
145         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
146
147         if (width)
148                 *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
149         if (height)
150                 *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
151 }
152
153 static void
154 pixbuf_document_render (EvDocument  *document,
155                         int          clip_x,
156                         int          clip_y,
157                         int          clip_width,
158                         int          clip_height)
159 {
160         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
161
162         if (pixbuf_document->scale == 1.0) {
163                 gdk_draw_pixbuf (pixbuf_document->target, NULL, pixbuf_document->pixbuf,
164                                  clip_x, clip_y,
165                                  clip_x, clip_y,
166                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
167                                  0, 0);
168         }
169         else {
170                 GdkPixbuf *tmp_pixbuf;
171
172                 tmp_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf_document->pixbuf),
173                                              gdk_pixbuf_get_has_alpha (pixbuf_document->pixbuf),
174                                              gdk_pixbuf_get_bits_per_sample (pixbuf_document->pixbuf),
175                                              clip_width, clip_height);
176
177                 gdk_pixbuf_scale (pixbuf_document->pixbuf, tmp_pixbuf, 0, 0, clip_width, clip_height,
178                                   clip_x * pixbuf_document->scale,
179                                   clip_y * pixbuf_document->scale,
180                                   pixbuf_document->scale, pixbuf_document->scale,
181                                   GDK_INTERP_BILINEAR);
182                 
183                 gdk_draw_pixbuf (pixbuf_document->target, NULL, tmp_pixbuf,
184                                  0, 0,
185                                  clip_x + pixbuf_document->x_offset,
186                                  clip_y + pixbuf_document->y_offset,
187                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
188                                  0, 0);
189
190                 g_object_unref (tmp_pixbuf);
191         }
192 }
193
194 static void
195 pixbuf_document_finalize (GObject *object)
196 {
197         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
198
199         g_object_unref (pixbuf_document->pixbuf);
200         
201         G_OBJECT_CLASS (parent_class)->finalize (object);
202 }
203
204 static void
205 pixbuf_document_set_property (GObject *object,
206                               guint prop_id,
207                               const GValue *value,
208                               GParamSpec *pspec)
209 {
210         switch (prop_id)
211         {
212                 case PROP_TITLE:
213                         /* read only */
214                         break;
215         }
216 }
217
218 static void
219 pixbuf_document_get_property (GObject *object,
220                               guint prop_id,
221                               GValue *value,
222                               GParamSpec *pspec)
223 {
224         switch (prop_id)
225         {
226                 case PROP_TITLE:
227                         g_value_set_string (value, NULL);
228                         break;
229         }
230 }
231
232 static void
233 pixbuf_document_class_init (PixbufDocumentClass *klass)
234 {
235         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236
237         parent_class = g_type_class_peek_parent (klass);
238         
239         gobject_class->finalize = pixbuf_document_finalize;
240         gobject_class->get_property = pixbuf_document_get_property;
241         gobject_class->set_property = pixbuf_document_set_property;
242
243         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
244 }
245
246 static char *
247 pixbuf_document_get_text (EvDocument *document, GdkRectangle *rect)
248 {
249         /* FIXME this method should not be in EvDocument */
250         g_warning ("pixbuf_document_get_text not implemented");
251         return NULL;
252 }
253
254
255 static EvLink *
256 pixbuf_document_get_link (EvDocument *document,
257                           int         x,
258                           int         y)
259 {
260         return NULL;
261 }
262
263 static void
264 pixbuf_document_document_iface_init (EvDocumentIface *iface)
265 {
266         iface->load = pixbuf_document_load;
267         iface->save = pixbuf_document_save;
268         iface->get_text = pixbuf_document_get_text;
269         iface->get_link = pixbuf_document_get_link;
270         iface->get_n_pages = pixbuf_document_get_n_pages;
271         iface->set_page = pixbuf_document_set_page;
272         iface->get_page = pixbuf_document_get_page;
273         iface->set_scale = pixbuf_document_set_scale;
274         iface->set_target = pixbuf_document_set_target;
275         iface->set_page_offset = pixbuf_document_set_page_offset;
276         iface->get_page_size = pixbuf_document_get_page_size;
277         iface->render = pixbuf_document_render;
278 }
279
280 static GdkPixbuf *
281 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
282                                           gint                   page,
283                                           gint                   width)
284 {
285         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
286         GdkPixbuf *pixbuf;
287         gdouble scale_factor;
288         gint height;
289         
290         scale_factor = (gdouble)width / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
291
292         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
293         
294         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, width, height,
295                                           GDK_INTERP_BILINEAR);
296         
297         return pixbuf;
298 }
299
300 static void
301 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
302                                            gint                  page,
303                                            gint                  suggested_width,
304                                            gint                  *width,
305                                            gint                  *height)
306 {
307         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
308         gdouble page_ratio;
309
310         page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
311                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
312         *width = suggested_width;
313         *height = (gint) (suggested_width * page_ratio);
314 }
315
316 static void
317 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
318 {
319         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
320         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
321 }
322
323
324 static void
325 pixbuf_document_init (PixbufDocument *pixbuf_document)
326 {
327         pixbuf_document->scale = 1.0;
328
329         pixbuf_document->x_offset = 10;
330         pixbuf_document->y_offset = 10;
331 }