]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
b83da789791c741c9d41259033b3174cacf2388d
[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 char *
173 pixbuf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
174 {
175         /* FIXME this method should not be in EvDocument */
176         g_warning ("pixbuf_document_get_text not implemented");
177         return NULL;
178 }
179
180 static void
181 pixbuf_document_document_iface_init (EvDocumentIface *iface)
182 {
183         iface->load = pixbuf_document_load;
184         iface->save = pixbuf_document_save;
185         iface->get_text = pixbuf_document_get_text;
186         iface->get_n_pages = pixbuf_document_get_n_pages;
187         iface->get_page_size = pixbuf_document_get_page_size;
188         iface->render_pixbuf = pixbuf_document_render_pixbuf;
189 }
190
191 static GdkPixbuf *
192 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
193                                           gint                    page,
194                                           gint                    size,
195                                           gboolean                border)
196 {
197         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
198         GdkPixbuf *pixbuf;
199         gdouble scale_factor;
200         gint height;
201         
202         scale_factor = (gdouble)size / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
203
204         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
205         
206         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, size, height,
207                                           GDK_INTERP_BILINEAR);
208         
209         return pixbuf;
210 }
211
212 static void
213 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
214                                            gint                  page,
215                                            gint                  suggested_width,
216                                            gint                  *width,
217                                            gint                  *height)
218 {
219         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
220         gdouble page_ratio;
221
222         page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
223                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
224         *width = suggested_width;
225         *height = (gint) (suggested_width * page_ratio);
226 }
227
228 static void
229 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
230 {
231         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
232         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
233 }
234
235
236 static void
237 pixbuf_document_init (PixbufDocument *pixbuf_document)
238 {
239         pixbuf_document->x_offset = 0;
240         pixbuf_document->y_offset = 0;
241 }