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