]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
7eeb8fdaf986e699b568151e8775e536976818f6
[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 struct _PixbufDocumentClass
24 {
25         GObjectClass parent_class;
26 };
27
28 struct _PixbufDocument
29 {
30         GObject parent_instance;
31
32         GdkPixbuf *pixbuf;
33         GdkDrawable *target;
34         gdouble scale;
35
36         gint x_offset, y_offset;
37 };
38
39 typedef struct _PixbufDocumentClass PixbufDocumentClass;
40
41 static void pixbuf_document_document_iface_init (EvDocumentIface *iface);
42 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
43
44 G_DEFINE_TYPE_WITH_CODE (PixbufDocument, pixbuf_document, G_TYPE_OBJECT,
45                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
46                                                   pixbuf_document_document_iface_init);
47                          G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
48                                                 pixbuf_document_document_thumbnails_iface_init)                            
49                                    });
50
51 static GObjectClass *parent_class;
52
53 static gboolean
54 pixbuf_document_load (EvDocument  *document,
55                       const char  *uri,
56                       GError     **error)
57 {
58         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
59         
60         gchar *filename;
61         GdkPixbuf *pixbuf;
62
63         /* FIXME: We could actually load uris  */
64         filename = g_filename_from_uri (uri, NULL, error);
65         if (!filename)
66                 return FALSE;
67         
68         pixbuf = gdk_pixbuf_new_from_file (filename, error);
69
70         if (!pixbuf)
71                 return FALSE;
72
73         pixbuf_document->pixbuf = pixbuf;
74         
75         return TRUE;
76 }
77
78 static int
79 pixbuf_document_get_n_pages (EvDocument  *document)
80 {
81         return 1;
82 }
83
84 static void
85 pixbuf_document_set_page (EvDocument  *document,
86                           int          page)
87 {
88         /* Do nothing */
89 }
90
91 static int
92 pixbuf_document_get_page (EvDocument  *document)
93 {
94         return 1;
95 }
96
97 static void
98 pixbuf_document_set_target (EvDocument  *document,
99                             GdkDrawable *target)
100 {
101         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
102
103         pixbuf_document->target = target;
104 }
105
106 static void
107 pixbuf_document_set_scale (EvDocument  *document,
108                            double       scale)
109 {
110         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
111
112         pixbuf_document->scale = scale;
113 }
114
115 static void
116 pixbuf_document_set_page_offset (EvDocument  *document,
117                               int          x,
118                               int          y)
119 {
120         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
121
122         pixbuf_document->x_offset = x;
123         pixbuf_document->y_offset = y;
124 }
125
126 static void
127 pixbuf_document_get_page_size (EvDocument   *document,
128                                int          *width,
129                                int          *height)
130 {
131         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
132
133         *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
134         *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
135 }
136
137 static void
138 pixbuf_document_render (EvDocument  *document,
139                         int          clip_x,
140                         int          clip_y,
141                         int          clip_width,
142                         int          clip_height)
143 {
144         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
145
146         if (pixbuf_document->scale == 1.0) {
147                 gdk_draw_pixbuf (pixbuf_document->target, NULL, pixbuf_document->pixbuf,
148                                  clip_x, clip_y,
149                                  clip_x, clip_y,
150                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
151                                  0, 0);
152         }
153         else {
154                 GdkPixbuf *tmp_pixbuf;
155
156                 tmp_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf_document->pixbuf),
157                                              gdk_pixbuf_get_has_alpha (pixbuf_document->pixbuf),
158                                              gdk_pixbuf_get_bits_per_sample (pixbuf_document->pixbuf),
159                                              clip_width, clip_height);
160
161                 gdk_pixbuf_scale (pixbuf_document->pixbuf, tmp_pixbuf, 0, 0, clip_width, clip_height,
162                                   clip_x * pixbuf_document->scale,
163                                   clip_y * pixbuf_document->scale,
164                                   pixbuf_document->scale, pixbuf_document->scale,
165                                   GDK_INTERP_BILINEAR);
166                 
167                 gdk_draw_pixbuf (pixbuf_document->target, NULL, tmp_pixbuf,
168                                  0, 0,
169                                  clip_x + pixbuf_document->x_offset,
170                                  clip_y + pixbuf_document->y_offset,
171                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
172                                  0, 0);
173
174                 g_object_unref (tmp_pixbuf);
175         }
176 }
177
178 static void
179 pixbuf_document_begin_find (EvDocument   *document,
180                             const char   *search_string,
181                             gboolean      case_sensitive)
182 {
183         
184 }
185
186 static void
187 pixbuf_document_end_find (EvDocument   *document)
188 {
189 }
190
191 static void
192 pixbuf_document_finalize (GObject *object)
193 {
194         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
195
196         g_object_unref (pixbuf_document->pixbuf);
197         
198         G_OBJECT_CLASS (parent_class)->finalize (object);
199 }
200
201 static void
202 pixbuf_document_class_init (PixbufDocumentClass *klass)
203 {
204         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
205
206         parent_class = g_type_class_peek_parent (klass);
207         
208         gobject_class->finalize = pixbuf_document_finalize;
209 }
210
211 static void
212 pixbuf_document_document_iface_init (EvDocumentIface *iface)
213 {
214         iface->load = pixbuf_document_load;
215         iface->get_n_pages = pixbuf_document_get_n_pages;
216         iface->set_page = pixbuf_document_set_page;
217         iface->get_page = pixbuf_document_get_page;
218         iface->set_scale = pixbuf_document_set_scale;
219         iface->set_target = pixbuf_document_set_target;
220         iface->set_page_offset = pixbuf_document_set_page_offset;
221         iface->get_page_size = pixbuf_document_get_page_size;
222         iface->render = pixbuf_document_render;
223         iface->begin_find = pixbuf_document_begin_find;
224         iface->end_find = pixbuf_document_end_find;
225 }
226
227 static GdkPixbuf *
228 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
229                                           gint                   page,
230                                           gint                   width)
231 {
232         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
233         GdkPixbuf *pixbuf;
234         gdouble scale_factor;
235         gint height;
236         
237         scale_factor = (gdouble)width / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
238
239         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
240         
241         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, width, height,
242                                           GDK_INTERP_BILINEAR);
243         
244         return pixbuf;
245 }
246
247
248 static void
249 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
250 {
251         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
252 }
253
254
255 static void
256 pixbuf_document_init (PixbufDocument *pixbuf_document)
257 {
258         pixbuf_document->scale = 1.0;
259
260         pixbuf_document->x_offset = 10;
261         pixbuf_document->y_offset = 10;
262 }