]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.h
Convert EvDocument interface into an abstract class
[evince.git] / libdocument / ev-document.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  *  Copyright (C) 2009 Carlos Garcia Campos
4  *  Copyright (C) 2000-2003 Marco Pesenti Gritti
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  *  $Id$
21  */
22
23 #if !defined (__EV_EVINCE_DOCUMENT_H_INSIDE__) && !defined (EVINCE_COMPILATION)
24 #error "Only <evince-document.h> can be included directly."
25 #endif
26
27 #ifndef EV_DOCUMENT_H
28 #define EV_DOCUMENT_H
29
30 #include <glib-object.h>
31 #include <glib.h>
32 #include <gdk/gdk.h>
33 #include <cairo.h>
34
35 #include "ev-document-info.h"
36 #include "ev-page.h"
37 #include "ev-render-context.h"
38
39 G_BEGIN_DECLS
40
41 #define EV_TYPE_DOCUMENT            (ev_document_get_type ())
42 #define EV_DOCUMENT(o)              (G_TYPE_CHECK_INSTANCE_CAST ((o), EV_TYPE_DOCUMENT, EvDocument))
43 #define EV_DOCUMENT_CLASS(k)        (G_TYPE_CHECK_CLASS_CAST((k), EV_TYPE_DOCUMENT, EvDocumentClass))
44 #define EV_IS_DOCUMENT(o)           (G_TYPE_CHECK_INSTANCE_TYPE ((o), EV_TYPE_DOCUMENT))
45 #define EV_IS_DOCUMENT_CLASS(k)     (G_TYPE_CHECK_CLASS_TYPE ((k), EV_TYPE_DOCUMENT))
46 #define EV_DOCUMENT_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), EV_TYPE_DOCUMENT, EvDocumentClass))
47
48 typedef struct _EvDocument        EvDocument;
49 typedef struct _EvDocumentClass   EvDocumentClass;
50 typedef struct _EvPageCache       EvPageCache;
51 typedef struct _EvPageCacheClass  EvPageCacheClass;
52
53 #define EV_DOCUMENT_ERROR ev_document_error_quark ()
54 #define EV_DOC_MUTEX_LOCK (ev_document_doc_mutex_lock ())
55 #define EV_DOC_MUTEX_UNLOCK (ev_document_doc_mutex_unlock ())
56
57 typedef enum
58 {
59         EV_DOCUMENT_ERROR_INVALID,
60         EV_DOCUMENT_ERROR_ENCRYPTED
61 } EvDocumentError;
62
63 typedef struct {
64         double x;
65         double y;
66 } EvPoint;
67
68 typedef struct _EvRectangle EvRectangle;
69
70 struct _EvDocument
71 {
72         GObject base;
73 };
74
75 struct _EvDocumentClass
76 {
77         GObjectClass base_class;
78
79         /* Virtual Methods  */
80         gboolean          (* load)            (EvDocument      *document,
81                                                const char      *uri,
82                                                GError         **error);
83         gboolean          (* save)            (EvDocument      *document,
84                                                const char      *uri,
85                                                GError         **error);
86         gint              (* get_n_pages)     (EvDocument      *document);
87         EvPage          * (* get_page)        (EvDocument      *document,
88                                                gint             index);
89         void              (* get_page_size)   (EvDocument      *document,
90                                                EvPage          *page,
91                                                double          *width,
92                                                double          *height);
93         gchar           * (* get_page_label)  (EvDocument      *document,
94                                                EvPage          *page);
95         cairo_surface_t * (* render)          (EvDocument      *document,
96                                                EvRenderContext *rc);
97         EvDocumentInfo  * (* get_info)        (EvDocument      *document);
98 };
99
100 GType            ev_document_get_type         (void) G_GNUC_CONST;
101 GQuark           ev_document_error_quark      (void);
102
103 /* Document mutex */
104 GMutex          *ev_document_get_doc_mutex    (void);
105 void             ev_document_doc_mutex_lock   (void);
106 void             ev_document_doc_mutex_unlock (void);
107 gboolean         ev_document_doc_mutex_trylock(void);
108
109 /* FontConfig mutex */
110 GMutex          *ev_document_get_fc_mutex     (void);
111 void             ev_document_fc_mutex_lock    (void);
112 void             ev_document_fc_mutex_unlock  (void);
113 gboolean         ev_document_fc_mutex_trylock (void);
114
115 EvDocumentInfo  *ev_document_get_info         (EvDocument      *document);
116 gboolean         ev_document_load             (EvDocument      *document,
117                                                const char      *uri,
118                                                GError         **error);
119 gboolean         ev_document_save             (EvDocument      *document,
120                                                const char      *uri,
121                                                GError         **error);
122 gint             ev_document_get_n_pages      (EvDocument      *document);
123 EvPage          *ev_document_get_page         (EvDocument      *document,
124                                                gint             index);
125 void             ev_document_get_page_size    (EvDocument      *document,
126                                                EvPage          *page,
127                                                double          *width,
128                                                double          *height);
129 gchar           *ev_document_get_page_label   (EvDocument      *document,
130                                                EvPage          *page);
131 cairo_surface_t *ev_document_render           (EvDocument      *document,
132                                                EvRenderContext *rc);
133
134 gint            ev_rect_cmp                   (EvRectangle     *a,
135                                                EvRectangle     *b);
136
137 #define EV_TYPE_RECTANGLE (ev_rectangle_get_type ())
138 struct _EvRectangle
139 {
140         gdouble x1;
141         gdouble y1;
142         gdouble x2;
143         gdouble y2;
144 };
145
146 GType        ev_rectangle_get_type (void) G_GNUC_CONST;
147 EvRectangle *ev_rectangle_new      (void);
148 EvRectangle *ev_rectangle_copy     (EvRectangle *ev_rect);
149 void         ev_rectangle_free     (EvRectangle *ev_rect);
150
151 /* convenience macro to ease interface addition in the CODE
152  * section of EV_BACKEND_REGISTER_WITH_CODE (this macro relies on
153  * the g_define_type_id present within EV_BACKEND_REGISTER_WITH_CODE()).
154  * usage example:
155  * EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
156  *                          EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
157  *                                                 pdf_document_document_thumbnails_iface_init));
158  */
159 #define EV_BACKEND_IMPLEMENT_INTERFACE(TYPE_IFACE, iface_init) {                \
160         const GInterfaceInfo g_implement_interface_info = {                     \
161                 (GInterfaceInitFunc) iface_init, NULL, NULL                     \
162         };                                                                      \
163         g_type_module_add_interface (module,                                    \
164                                      g_define_type_id,                          \
165                                      TYPE_IFACE,                                \
166                                      &g_implement_interface_info);              \
167 }
168
169 /*
170  * Utility macro used to register backends
171  *
172  * use: EV_BACKEND_REGISTER_WITH_CODE(BackendName, backend_name, CODE)
173  */
174 #define EV_BACKEND_REGISTER_WITH_CODE(BackendName, backend_name, CODE)          \
175                                                                                 \
176 static GType g_define_type_id = 0;                                              \
177                                                                                 \
178 GType                                                                           \
179 backend_name##_get_type (void)                                                  \
180 {                                                                               \
181         return g_define_type_id;                                                \
182 }                                                                               \
183                                                                                 \
184 static void     backend_name##_init              (BackendName        *self);    \
185 static void     backend_name##_class_init        (BackendName##Class *klass);   \
186 static gpointer backend_name##_parent_class = NULL;                             \
187 static void     backend_name##_class_intern_init (gpointer klass)               \
188 {                                                                               \
189         backend_name##_parent_class = g_type_class_peek_parent (klass);         \
190         backend_name##_class_init ((BackendName##Class *) klass);               \
191 }                                                                               \
192                                                                                 \
193 G_MODULE_EXPORT GType                                                           \
194 register_evince_backend (GTypeModule *module)                                   \
195 {                                                                               \
196         const GTypeInfo our_info = {                                            \
197                 sizeof (BackendName##Class),                                    \
198                 NULL, /* base_init */                                           \
199                 NULL, /* base_finalize */                                       \
200                 (GClassInitFunc) backend_name##_class_intern_init,              \
201                 NULL,                                                           \
202                 NULL, /* class_data */                                          \
203                 sizeof (BackendName),                                           \
204                 0, /* n_preallocs */                                            \
205                 (GInstanceInitFunc) backend_name##_init                         \
206         };                                                                      \
207                                                                                 \
208         /* Initialise the i18n stuff */                                         \
209         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);                       \
210         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");                     \
211                                                                                 \
212         g_define_type_id = g_type_module_register_type (module,                 \
213                                                         EV_TYPE_DOCUMENT,       \
214                                                         #BackendName,           \
215                                                         &our_info,              \
216                                                         (GTypeFlags)0);         \
217                                                                                 \
218         CODE                                                                    \
219                                                                                 \
220         return g_define_type_id;                                                \
221 }
222
223 /*
224  * Utility macro used to register backend
225  *
226  * use: EV_BACKEND_REGISTER(BackendName, backend_name)
227  */
228 #define EV_BACKEND_REGISTER(BackendName, backend_name)                  \
229         EV_BACKEND_REGISTER_WITH_CODE(BackendName, backend_name, ;)
230
231 /*
232  * A convenience macro for boxed type implementations, which defines a
233  * type_name_get_type() function registering the boxed type.
234  */
235 #define EV_DEFINE_BOXED_TYPE(TypeName, type_name, copy_func, free_func)               \
236 GType                                                                                 \
237 type_name##_get_type (void)                                                           \
238 {                                                                                     \
239         static volatile gsize g_define_type_id__volatile = 0;                         \
240         if (g_once_init_enter (&g_define_type_id__volatile)) {                        \
241                 GType g_define_type_id =                                              \
242                     g_boxed_type_register_static (g_intern_static_string (#TypeName), \
243                                                   (GBoxedCopyFunc) copy_func,         \
244                                                   (GBoxedFreeFunc) free_func);        \
245                 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);    \
246         }                                                                             \
247         return g_define_type_id__volatile;                                            \
248 }
249
250 /* A convenience macro for GTypeInterface definitions, which declares
251  * a default vtable initialization function and defines a *_get_type()
252  * function.
253  *
254  * The macro expects the interface initialization function to have the
255  * name <literal>t_n ## _default_init</literal>, and the interface
256  * structure to have the name <literal>TN ## Interface</literal>.
257  */
258 #define EV_DEFINE_INTERFACE(TypeName, type_name, TYPE_PREREQ)                                \
259 static void     type_name##_class_init        (TypeName##Iface *klass);                      \
260                                                                                              \
261 GType                                                                                        \
262 type_name##_get_type (void)                                                                  \
263 {                                                                                            \
264         static volatile gsize g_define_type_id__volatile = 0;                                \
265         if (g_once_init_enter (&g_define_type_id__volatile)) {                               \
266                 GType g_define_type_id =                                                     \
267                     g_type_register_static_simple (G_TYPE_INTERFACE,                         \
268                                                    g_intern_static_string (#TypeName),       \
269                                                    sizeof (TypeName##Iface),                 \
270                                                    (GClassInitFunc)type_name##_class_init,   \
271                                                    0,                                        \
272                                                    (GInstanceInitFunc)NULL,                  \
273                                                    (GTypeFlags) 0);                          \
274                 if (TYPE_PREREQ)                                                             \
275                         g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ);   \
276                 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);           \
277         }                                                                                    \
278         return g_define_type_id__volatile;                                                   \
279 }
280
281 /*
282  * A convenience macro for boxed type implementations, which defines a
283  * type_name_get_type() function registering the boxed type.
284  */
285 #define EV_DEFINE_BOXED_TYPE(TypeName, type_name, copy_func, free_func)               \
286 GType                                                                                 \
287 type_name##_get_type (void)                                                           \
288 {                                                                                     \
289         static volatile gsize g_define_type_id__volatile = 0;                         \
290         if (g_once_init_enter (&g_define_type_id__volatile)) {                        \
291                 GType g_define_type_id =                                              \
292                     g_boxed_type_register_static (g_intern_static_string (#TypeName), \
293                                                   (GBoxedCopyFunc) copy_func,         \
294                                                   (GBoxedFreeFunc) free_func);        \
295                 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);    \
296         }                                                                             \
297         return g_define_type_id__volatile;                                            \
298 }
299                 
300 G_END_DECLS
301
302 #endif /* EV_DOCUMENT_H */