]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
0aabb55f537d5172eee03589cd0bcaee09cfbdaf
[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
40         gint x_offset, y_offset;
41 };
42
43 typedef struct _PixbufDocumentClass PixbufDocumentClass;
44
45 static void pixbuf_document_document_iface_init (EvDocumentIface *iface);
46 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
47
48 G_DEFINE_TYPE_WITH_CODE (PixbufDocument, pixbuf_document, G_TYPE_OBJECT,
49                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
50                                                   pixbuf_document_document_iface_init);
51                          G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
52                                                 pixbuf_document_document_thumbnails_iface_init)                            
53                                    });
54
55 static gboolean
56 pixbuf_document_load (EvDocument  *document,
57                       const char  *uri,
58                       GError     **error)
59 {
60         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
61         
62         gchar *filename;
63         GdkPixbuf *pixbuf;
64
65         /* FIXME: We could actually load uris  */
66         filename = g_filename_from_uri (uri, NULL, error);
67         if (!filename)
68                 return FALSE;
69         
70         pixbuf = gdk_pixbuf_new_from_file (filename, error);
71
72         if (!pixbuf)
73                 return FALSE;
74
75         pixbuf_document->pixbuf = pixbuf;
76         
77         return TRUE;
78 }
79
80 static gboolean
81 pixbuf_document_save (EvDocument  *document,
82                       const char  *uri,
83                       GError     **error)
84 {
85         g_warning ("pixbuf_document_save not implemented"); /* FIXME */
86         return TRUE;
87 }
88
89 static int
90 pixbuf_document_get_n_pages (EvDocument  *document)
91 {
92         return 1;
93 }
94
95 static void
96 pixbuf_document_get_page_size (EvDocument   *document,
97                                int           page,
98                                double       *width,
99                                double       *height)
100 {
101         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
102
103         if (width)
104                 *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
105         if (height)
106                 *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
107
108         printf ("get_page_size, page=%d, *width=%f, *height=%f\n",
109                 page, *width, *height);
110 }
111
112 static GdkPixbuf*
113 pixbuf_document_render_pixbuf (EvDocument  *document, int page, double scale)
114 {
115         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
116         return gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
117                                         gdk_pixbuf_get_width (pixbuf_document->pixbuf) * scale,
118                                         gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale,
119                                         GDK_INTERP_BILINEAR);
120 }
121
122 static void
123 pixbuf_document_finalize (GObject *object)
124 {
125         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
126
127         g_object_unref (pixbuf_document->pixbuf);
128         
129         G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
130 }
131
132 static void
133 pixbuf_document_set_property (GObject *object,
134                               guint prop_id,
135                               const GValue *value,
136                               GParamSpec *pspec)
137 {
138         switch (prop_id)
139         {
140                 case PROP_TITLE:
141                         /* read only */
142                         break;
143         }
144 }
145
146 static void
147 pixbuf_document_get_property (GObject *object,
148                               guint prop_id,
149                               GValue *value,
150                               GParamSpec *pspec)
151 {
152         switch (prop_id)
153         {
154                 case PROP_TITLE:
155                         g_value_set_string (value, NULL);
156                         break;
157         }
158 }
159
160 static void
161 pixbuf_document_class_init (PixbufDocumentClass *klass)
162 {
163         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164
165         gobject_class->finalize = pixbuf_document_finalize;
166         gobject_class->get_property = pixbuf_document_get_property;
167         gobject_class->set_property = pixbuf_document_set_property;
168
169         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
170 }
171
172 static gboolean
173 pixbuf_document_can_get_text (EvDocument *document)
174 {
175         return FALSE;
176 }
177
178 static void
179 pixbuf_document_document_iface_init (EvDocumentIface *iface)
180 {
181         iface->load = pixbuf_document_load;
182         iface->save = pixbuf_document_save;
183         iface->can_get_text = pixbuf_document_can_get_text;
184         iface->get_n_pages = pixbuf_document_get_n_pages;
185         iface->get_page_size = pixbuf_document_get_page_size;
186         iface->render_pixbuf = pixbuf_document_render_pixbuf;
187 }
188
189 static GdkPixbuf *
190 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
191                                           gint                    page,
192                                           gint                    size,
193                                           gboolean                border)
194 {
195         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
196         GdkPixbuf *pixbuf;
197         gdouble scale_factor;
198         gint height;
199         
200         scale_factor = (gdouble)size / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
201
202         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
203         
204         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, size, height,
205                                           GDK_INTERP_BILINEAR);
206         
207         return pixbuf;
208 }
209
210 static void
211 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
212                                            gint                  page,
213                                            gint                  suggested_width,
214                                            gint                  *width,
215                                            gint                  *height)
216 {
217         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
218         gdouble page_ratio;
219
220         page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
221                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
222         *width = suggested_width;
223         *height = (gint) (suggested_width * page_ratio);
224 }
225
226 static void
227 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
228 {
229         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
230         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
231 }
232
233
234 static void
235 pixbuf_document_init (PixbufDocument *pixbuf_document)
236 {
237         pixbuf_document->x_offset = 0;
238         pixbuf_document->y_offset = 0;
239 }