]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
Got pixbuf backend working again
[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_scale (EvDocument  *document,
111                            double       scale)
112 {
113         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
114
115         pixbuf_document->scale = scale;
116 }
117
118 static void
119 pixbuf_document_get_page_size (EvDocument   *document,
120                                int           page,
121                                int          *width,
122                                int          *height)
123 {
124         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
125
126         if (width)
127                 *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf) * pixbuf_document->scale;
128         if (height)
129                 *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * pixbuf_document->scale;
130 }
131
132 static GdkPixbuf*
133 pixbuf_document_render_pixbuf (EvDocument  *document)
134 {
135         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
136         return gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
137                                         gdk_pixbuf_get_width (pixbuf_document->pixbuf) * pixbuf_document->scale,
138                                         gdk_pixbuf_get_height (pixbuf_document->pixbuf) * pixbuf_document->scale,
139                                         GDK_INTERP_BILINEAR);
140 }
141
142 static void
143 pixbuf_document_finalize (GObject *object)
144 {
145         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
146
147         g_object_unref (pixbuf_document->pixbuf);
148         
149         G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
150 }
151
152 static void
153 pixbuf_document_set_property (GObject *object,
154                               guint prop_id,
155                               const GValue *value,
156                               GParamSpec *pspec)
157 {
158         switch (prop_id)
159         {
160                 case PROP_TITLE:
161                         /* read only */
162                         break;
163         }
164 }
165
166 static void
167 pixbuf_document_get_property (GObject *object,
168                               guint prop_id,
169                               GValue *value,
170                               GParamSpec *pspec)
171 {
172         switch (prop_id)
173         {
174                 case PROP_TITLE:
175                         g_value_set_string (value, NULL);
176                         break;
177         }
178 }
179
180 static void
181 pixbuf_document_class_init (PixbufDocumentClass *klass)
182 {
183         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184
185         gobject_class->finalize = pixbuf_document_finalize;
186         gobject_class->get_property = pixbuf_document_get_property;
187         gobject_class->set_property = pixbuf_document_set_property;
188
189         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
190 }
191
192 static char *
193 pixbuf_document_get_text (EvDocument *document, GdkRectangle *rect)
194 {
195         /* FIXME this method should not be in EvDocument */
196         g_warning ("pixbuf_document_get_text not implemented");
197         return NULL;
198 }
199
200
201 static EvLink *
202 pixbuf_document_get_link (EvDocument *document,
203                           int         x,
204                           int         y)
205 {
206         return NULL;
207 }
208
209 static void
210 pixbuf_document_document_iface_init (EvDocumentIface *iface)
211 {
212         iface->load = pixbuf_document_load;
213         iface->save = pixbuf_document_save;
214         iface->get_text = pixbuf_document_get_text;
215         iface->get_link = pixbuf_document_get_link;
216         iface->get_n_pages = pixbuf_document_get_n_pages;
217         iface->set_page = pixbuf_document_set_page;
218         iface->get_page = pixbuf_document_get_page;
219         iface->set_scale = pixbuf_document_set_scale;
220         iface->get_page_size = pixbuf_document_get_page_size;
221         iface->render_pixbuf = pixbuf_document_render_pixbuf;
222 }
223
224 static GdkPixbuf *
225 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
226                                           gint                    page,
227                                           gint                    size,
228                                           gboolean                border)
229 {
230         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
231         GdkPixbuf *pixbuf;
232         gdouble scale_factor;
233         gint height;
234         
235         scale_factor = (gdouble)size / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
236
237         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
238         
239         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, size, height,
240                                           GDK_INTERP_BILINEAR);
241         
242         return pixbuf;
243 }
244
245 static void
246 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
247                                            gint                  page,
248                                            gint                  suggested_width,
249                                            gint                  *width,
250                                            gint                  *height)
251 {
252         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
253         gdouble page_ratio;
254
255         page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
256                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
257         *width = suggested_width;
258         *height = (gint) (suggested_width * page_ratio);
259 }
260
261 static void
262 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
263 {
264         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
265         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
266 }
267
268
269 static void
270 pixbuf_document_init (PixbufDocument *pixbuf_document)
271 {
272         pixbuf_document->scale = 1.0;
273
274         pixbuf_document->x_offset = 0;
275         pixbuf_document->y_offset = 0;
276 }