]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-annotation.c
[libdocument] Use G_DEFINE_INTERFACE() macro to define EvAnnotationMarkup iface
[evince.git] / libdocument / ev-annotation.c
1 /* ev-annotation.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
5  * Copyright (C) 2007 IƱigo Martinez <inigomartinez@gmail.com>
6  *
7  * Evince is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Evince is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23
24 #include "ev-annotation.h"
25
26
27 static void ev_annotation_markup_default_init          (EvAnnotationMarkupInterface *iface);
28 static void ev_annotation_text_markup_iface_init       (EvAnnotationMarkupInterface *iface);
29 static void ev_annotation_attachment_markup_iface_init (EvAnnotationMarkupInterface *iface);
30
31 enum {
32         PROP_0,
33         PROP_LABEL,
34         PROP_OPACITY,
35         PROP_HAS_POPUP,
36         PROP_RECTANGLE,
37         PROP_IS_OPEN
38 };
39
40 G_DEFINE_ABSTRACT_TYPE (EvAnnotation, ev_annotation, G_TYPE_OBJECT)
41 G_DEFINE_INTERFACE (EvAnnotationMarkup, ev_annotation_markup, EV_TYPE_ANNOTATION)
42 G_DEFINE_TYPE_WITH_CODE (EvAnnotationText, ev_annotation_text, EV_TYPE_ANNOTATION,
43          {
44                  G_IMPLEMENT_INTERFACE (EV_TYPE_ANNOTATION_MARKUP,
45                                         ev_annotation_text_markup_iface_init);
46          });
47 G_DEFINE_TYPE_WITH_CODE (EvAnnotationAttachment, ev_annotation_attachment, EV_TYPE_ANNOTATION,
48          {
49                  G_IMPLEMENT_INTERFACE (EV_TYPE_ANNOTATION_MARKUP,
50                                         ev_annotation_attachment_markup_iface_init);
51          });
52
53 /* EvAnnotation */
54 static void
55 ev_annotation_finalize (GObject *object)
56 {
57         EvAnnotation *annot = EV_ANNOTATION (object);
58
59         if (annot->page) {
60                 g_object_unref (annot->page);
61                 annot->page = NULL;
62         }
63
64         if (annot->contents) {
65                 g_free (annot->contents);
66                 annot->contents = NULL;
67         }
68
69         if (annot->name) {
70                 g_free (annot->name);
71                 annot->name = NULL;
72         }
73
74         if (annot->modified) {
75                 g_free (annot->modified);
76                 annot->modified = NULL;
77         }
78
79         G_OBJECT_CLASS (ev_annotation_parent_class)->finalize (object);
80 }
81
82 static void
83 ev_annotation_init (EvAnnotation *annot)
84 {
85 }
86
87 static void
88 ev_annotation_class_init (EvAnnotationClass *klass)
89 {
90         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
91
92         g_object_class->finalize = ev_annotation_finalize;
93 }
94
95 /* EvAnnotationMarkup */
96 typedef struct {
97         gchar   *label;
98         gdouble  opacity;
99         gboolean has_popup;
100         gboolean is_open;
101         EvRectangle *rectangle;
102 } EvAnnotationMarkupProps;
103
104 static void
105 ev_annotation_markup_default_init (EvAnnotationMarkupInterface *iface)
106 {
107         static gboolean initialized = FALSE;
108
109         if (!initialized) {
110                 g_object_interface_install_property (iface,
111                                                      g_param_spec_string ("label",
112                                                                           "Label",
113                                                                           "Label of the markup annotation",
114                                                                           NULL,
115                                                                           G_PARAM_READWRITE));
116                 g_object_interface_install_property (iface,
117                                                      g_param_spec_double ("opacity",
118                                                                           "Opacity",
119                                                                           "Opacity of the markup annotation",
120                                                                           0,
121                                                                           G_MAXDOUBLE,
122                                                                           0,
123                                                                           G_PARAM_READWRITE));
124                 g_object_interface_install_property (iface,
125                                                      g_param_spec_boolean ("has_popup",
126                                                                            "Has popup",
127                                                                            "Whether the markup annotation has "
128                                                                            "a popup window associated",
129                                                                            TRUE,
130                                                                            G_PARAM_READWRITE));
131                 g_object_interface_install_property (iface,
132                                                      g_param_spec_boxed ("rectangle",
133                                                                          "Rectangle",
134                                                                          "The Rectangle of the popup associated "
135                                                                          "to the markup annotation",
136                                                                          EV_TYPE_RECTANGLE,
137                                                                          G_PARAM_READWRITE));
138                 g_object_interface_install_property (iface,
139                                                      g_param_spec_boolean ("is_open",
140                                                                            "Is open",
141                                                                            "Whether the popup associated to "
142                                                                            "the markup annotation is open",
143                                                                            FALSE,
144                                                                            G_PARAM_READWRITE));
145                 initialized = TRUE;
146         }
147 }
148
149 static void
150 ev_annotation_markup_props_free (EvAnnotationMarkupProps *props)
151 {
152         g_free (props->label);
153         ev_rectangle_free (props->rectangle);
154         g_slice_free (EvAnnotationMarkupProps, props);
155 }
156
157 static EvAnnotationMarkupProps *
158 ev_annotation_markup_get_properties (EvAnnotationMarkup *markup)
159 {
160         EvAnnotationMarkupProps *props;
161         static GQuark props_key = 0;
162
163         if (!props_key)
164                 props_key = g_quark_from_static_string ("ev-annotation-markup-props");
165
166         props = g_object_get_qdata (G_OBJECT (markup), props_key);
167         if (!props) {
168                 props = g_slice_new0 (EvAnnotationMarkupProps);
169                 g_object_set_qdata_full (G_OBJECT (markup),
170                                          props_key, props,
171                                          (GDestroyNotify) ev_annotation_markup_props_free);
172         }
173
174         return props;
175 }
176
177 static void
178 ev_annotation_markup_set_property (GObject      *object,
179                                    guint         prop_id,
180                                    const GValue *value,
181                                    GParamSpec   *pspec)
182 {
183         EvAnnotationMarkupProps *props;
184
185         props = ev_annotation_markup_get_properties (EV_ANNOTATION_MARKUP (object));
186
187         switch (prop_id) {
188         case PROP_LABEL:
189                 g_free (props->label);
190                 props->label = g_value_dup_string (value);
191                 break;
192         case PROP_OPACITY:
193                 props->opacity = g_value_get_double (value);
194                 break;
195         case PROP_HAS_POPUP:
196                 props->has_popup = g_value_get_boolean (value);
197                 break;
198         case PROP_RECTANGLE:
199                 ev_rectangle_free (props->rectangle);
200                 props->rectangle = g_value_dup_boxed (value);
201                 break;
202         case PROP_IS_OPEN:
203                 props->is_open = g_value_get_boolean (value);
204                 break;
205         default:
206                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207         }
208 }
209
210 static void
211 ev_annotation_markup_get_property (GObject    *object,
212                                    guint       prop_id,
213                                    GValue     *value,
214                                    GParamSpec *pspec)
215 {
216         EvAnnotationMarkupProps *props;
217
218         props = ev_annotation_markup_get_properties (EV_ANNOTATION_MARKUP (object));
219
220         switch (prop_id) {
221         case PROP_LABEL:
222                 g_value_set_string (value, props->label);
223                 break;
224         case PROP_OPACITY:
225                 g_value_set_double (value, props->opacity);
226                 break;
227         case PROP_HAS_POPUP:
228                 g_value_set_boolean (value, props->has_popup);
229                 break;
230         case PROP_RECTANGLE:
231                 g_value_set_boxed (value, props->rectangle);
232                 break;
233         case PROP_IS_OPEN:
234                 g_value_set_boolean (value, props->is_open);
235                 break;
236         default:
237                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238         }
239 }
240
241 static void
242 ev_annotation_markup_class_install_properties (GObjectClass *klass)
243 {
244         klass->set_property = ev_annotation_markup_set_property;
245         klass->get_property = ev_annotation_markup_get_property;
246
247         g_object_class_override_property (klass, PROP_LABEL, "label");
248         g_object_class_override_property (klass, PROP_OPACITY, "opacity");
249         g_object_class_override_property (klass, PROP_HAS_POPUP, "has_popup");
250         g_object_class_override_property (klass, PROP_RECTANGLE, "rectangle");
251         g_object_class_override_property (klass, PROP_IS_OPEN, "is_open");
252 }
253
254 gchar *
255 ev_annotation_markup_get_label (EvAnnotationMarkup *markup)
256 {
257         gchar *retval;
258
259         g_return_val_if_fail (EV_IS_ANNOTATION_MARKUP (markup), NULL);
260
261         g_object_get (G_OBJECT (markup), "label", &retval, NULL);
262
263         return retval;
264 }
265
266 void
267 ev_annotation_markup_set_label (EvAnnotationMarkup *markup,
268                                 const gchar        *label)
269 {
270         g_return_if_fail (EV_IS_ANNOTATION_MARKUP (markup));
271         g_return_if_fail (label != NULL);
272
273         g_object_set (G_OBJECT (markup), "label", label, NULL);
274 }
275
276 gdouble
277 ev_annotation_markup_get_opacity (EvAnnotationMarkup *markup)
278 {
279         gdouble retval;
280
281         g_return_val_if_fail (EV_IS_ANNOTATION_MARKUP (markup), 0.0);
282
283         g_object_get (G_OBJECT (markup), "opacity", &retval, NULL);
284
285         return retval;
286 }
287
288 void
289 ev_annotation_markup_set_opacity (EvAnnotationMarkup *markup,
290                                   gdouble             opacity)
291 {
292         g_return_if_fail (EV_IS_ANNOTATION_MARKUP (markup));
293
294         g_object_set (G_OBJECT (markup), "opacity", opacity, NULL);
295 }
296
297 gboolean
298 ev_annotation_markup_has_popup (EvAnnotationMarkup *markup)
299 {
300         gboolean retval;
301
302         g_return_val_if_fail (EV_IS_ANNOTATION_MARKUP (markup), FALSE);
303
304         g_object_get (G_OBJECT (markup), "has_popup", &retval, NULL);
305
306         return retval;
307 }
308
309 void
310 ev_annotation_markup_get_rectangle (EvAnnotationMarkup *markup,
311                                     EvRectangle        *ev_rect)
312 {
313         EvRectangle *r;
314
315         g_return_if_fail (EV_IS_ANNOTATION_MARKUP (markup));
316         g_return_if_fail (ev_rect != NULL);
317
318         g_object_get (G_OBJECT (markup), "rectangle", &r, NULL);
319         *ev_rect = *r;
320 }
321
322 gboolean
323 ev_annotation_markup_get_is_open (EvAnnotationMarkup *markup)
324 {
325         gboolean retval;
326
327         g_return_val_if_fail (EV_IS_ANNOTATION_MARKUP (markup), FALSE);
328
329         g_object_get (G_OBJECT (markup), "is_open", &retval, NULL);
330
331         return retval;
332 }
333
334 void
335 ev_annotation_markup_set_is_open (EvAnnotationMarkup *markup,
336                                   gboolean            is_open)
337 {
338         g_return_if_fail (EV_IS_ANNOTATION_MARKUP (markup));
339
340         g_object_set (G_OBJECT (markup), "is_open", is_open, NULL);
341 }
342
343 /* EvAnnotationText */
344 static void
345 ev_annotation_text_init (EvAnnotationText *annot)
346 {
347 }
348
349 static void
350 ev_annotation_text_class_init (EvAnnotationTextClass *klass)
351 {
352         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
353
354         ev_annotation_markup_class_install_properties (g_object_class);
355 }
356
357 static void
358 ev_annotation_text_markup_iface_init (EvAnnotationMarkupInterface *iface)
359 {
360 }
361
362 EvAnnotation *
363 ev_annotation_text_new (EvPage *page)
364 {
365         EvAnnotation *annot;
366
367         annot = EV_ANNOTATION (g_object_new (EV_TYPE_ANNOTATION_TEXT, NULL));
368         annot->page = g_object_ref (page);
369
370         return annot;
371 }
372
373 /* EvAnnotationAttachment */
374 static void
375 ev_annotation_attachment_finalize (GObject *object)
376 {
377         EvAnnotationAttachment *annot = EV_ANNOTATION_ATTACHMENT (object);
378
379         if (annot->attachment) {
380                 g_object_unref (annot->attachment);
381                 annot->attachment = NULL;
382         }
383
384         G_OBJECT_CLASS (ev_annotation_attachment_parent_class)->finalize (object);
385 }
386
387 static void
388 ev_annotation_attachment_init (EvAnnotationAttachment *annot)
389 {
390 }
391
392 static void
393 ev_annotation_attachment_class_init (EvAnnotationAttachmentClass *klass)
394 {
395         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
396
397         ev_annotation_markup_class_install_properties (g_object_class);
398
399         g_object_class->finalize = ev_annotation_attachment_finalize;
400 }
401
402 static void
403 ev_annotation_attachment_markup_iface_init (EvAnnotationMarkupInterface *iface)
404 {
405 }
406
407 EvAnnotation *
408 ev_annotation_attachment_new (EvPage       *page,
409                               EvAttachment *attachment)
410 {
411         EvAnnotation *annot;
412
413         g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
414
415         annot = EV_ANNOTATION (g_object_new (EV_TYPE_ANNOTATION_ATTACHMENT, NULL));
416         annot->page = g_object_ref (page);
417         EV_ANNOTATION_ATTACHMENT (annot)->attachment = g_object_ref (attachment);
418
419         return annot;
420 }