]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
Updated German translation.
[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 int
84 pixbuf_document_get_n_pages (EvDocument  *document)
85 {
86         return 1;
87 }
88
89 static void
90 pixbuf_document_set_page (EvDocument  *document,
91                           int          page)
92 {
93         /* Do nothing */
94 }
95
96 static int
97 pixbuf_document_get_page (EvDocument  *document)
98 {
99         return 1;
100 }
101
102 static void
103 pixbuf_document_set_target (EvDocument  *document,
104                             GdkDrawable *target)
105 {
106         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
107
108         pixbuf_document->target = target;
109 }
110
111 static void
112 pixbuf_document_set_scale (EvDocument  *document,
113                            double       scale)
114 {
115         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
116
117         pixbuf_document->scale = scale;
118 }
119
120 static void
121 pixbuf_document_set_page_offset (EvDocument  *document,
122                               int          x,
123                               int          y)
124 {
125         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
126
127         pixbuf_document->x_offset = x;
128         pixbuf_document->y_offset = y;
129 }
130
131 static void
132 pixbuf_document_get_page_size (EvDocument   *document,
133                                int          *width,
134                                int          *height)
135 {
136         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
137
138         if (width)
139                 *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
140         if (height)
141                 *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
142 }
143
144 static void
145 pixbuf_document_render (EvDocument  *document,
146                         int          clip_x,
147                         int          clip_y,
148                         int          clip_width,
149                         int          clip_height)
150 {
151         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
152
153         if (pixbuf_document->scale == 1.0) {
154                 gdk_draw_pixbuf (pixbuf_document->target, NULL, pixbuf_document->pixbuf,
155                                  clip_x, clip_y,
156                                  clip_x, clip_y,
157                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
158                                  0, 0);
159         }
160         else {
161                 GdkPixbuf *tmp_pixbuf;
162
163                 tmp_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf_document->pixbuf),
164                                              gdk_pixbuf_get_has_alpha (pixbuf_document->pixbuf),
165                                              gdk_pixbuf_get_bits_per_sample (pixbuf_document->pixbuf),
166                                              clip_width, clip_height);
167
168                 gdk_pixbuf_scale (pixbuf_document->pixbuf, tmp_pixbuf, 0, 0, clip_width, clip_height,
169                                   clip_x * pixbuf_document->scale,
170                                   clip_y * pixbuf_document->scale,
171                                   pixbuf_document->scale, pixbuf_document->scale,
172                                   GDK_INTERP_BILINEAR);
173                 
174                 gdk_draw_pixbuf (pixbuf_document->target, NULL, tmp_pixbuf,
175                                  0, 0,
176                                  clip_x + pixbuf_document->x_offset,
177                                  clip_y + pixbuf_document->y_offset,
178                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
179                                  0, 0);
180
181                 g_object_unref (tmp_pixbuf);
182         }
183 }
184
185 static void
186 pixbuf_document_finalize (GObject *object)
187 {
188         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
189
190         g_object_unref (pixbuf_document->pixbuf);
191         
192         G_OBJECT_CLASS (parent_class)->finalize (object);
193 }
194
195 static void
196 pixbuf_document_set_property (GObject *object,
197                               guint prop_id,
198                               const GValue *value,
199                               GParamSpec *pspec)
200 {
201         switch (prop_id)
202         {
203                 case PROP_TITLE:
204                         /* read only */
205                         break;
206         }
207 }
208
209 static void
210 pixbuf_document_get_property (GObject *object,
211                               guint prop_id,
212                               GValue *value,
213                               GParamSpec *pspec)
214 {
215         switch (prop_id)
216         {
217                 case PROP_TITLE:
218                         g_value_set_string (value, NULL);
219                         break;
220         }
221 }
222
223 static void
224 pixbuf_document_class_init (PixbufDocumentClass *klass)
225 {
226         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
227
228         parent_class = g_type_class_peek_parent (klass);
229         
230         gobject_class->finalize = pixbuf_document_finalize;
231         gobject_class->get_property = pixbuf_document_get_property;
232         gobject_class->set_property = pixbuf_document_set_property;
233
234         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
235 }
236
237 static EvLink *
238 pixbuf_document_get_link (EvDocument *document,
239                           int         x,
240                           int         y)
241 {
242         return NULL;
243 }
244
245 static void
246 pixbuf_document_document_iface_init (EvDocumentIface *iface)
247 {
248         iface->load = pixbuf_document_load;
249         iface->get_link = pixbuf_document_get_link;
250         iface->get_n_pages = pixbuf_document_get_n_pages;
251         iface->set_page = pixbuf_document_set_page;
252         iface->get_page = pixbuf_document_get_page;
253         iface->set_scale = pixbuf_document_set_scale;
254         iface->set_target = pixbuf_document_set_target;
255         iface->set_page_offset = pixbuf_document_set_page_offset;
256         iface->get_page_size = pixbuf_document_get_page_size;
257         iface->render = pixbuf_document_render;
258 }
259
260 static GdkPixbuf *
261 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
262                                           gint                   page,
263                                           gint                   width)
264 {
265         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
266         GdkPixbuf *pixbuf;
267         gdouble scale_factor;
268         gint height;
269         
270         scale_factor = (gdouble)width / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
271
272         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
273         
274         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, width, height,
275                                           GDK_INTERP_BILINEAR);
276         
277         return pixbuf;
278 }
279
280 static void
281 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
282                                            gint                  page,
283                                            gint                  suggested_width,
284                                            gint                  *width,
285                                            gint                  *height)
286 {
287         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
288         gdouble page_ratio;
289
290         page_ratio = gdk_pixbuf_get_height (pixbuf_document->pixbuf) /
291                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
292         *width = suggested_width;
293         *height = (gint) (suggested_width * page_ratio);
294 }
295
296 static void
297 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
298 {
299         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
300         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
301 }
302
303
304 static void
305 pixbuf_document_init (PixbufDocument *pixbuf_document)
306 {
307         pixbuf_document->scale = 1.0;
308
309         pixbuf_document->x_offset = 10;
310         pixbuf_document->y_offset = 10;
311 }