[AdgADim] Removed PARENT_CLASS define
[adg.git] / adg / adg-style.c
blobdda6552f22ab8a569ff8af54da64a98d4b8c28c2
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:style
23 * @title: AdgStyle
24 * @short_description: The base class of all styling objects
26 * This is the fundamental abstract class from which all customization object
27 * must be derived.
30 #include "adg-style.h"
31 #include "adg-style-private.h"
32 #include "adg-line-style.h"
33 #include "adg-font-style.h"
34 #include "adg-arrow-style.h"
35 #include "adg-enums.h"
36 #include "adg-util.h"
37 #include "adg-intl.h"
39 #include <string.h>
41 #define PARENT_CLASS ((GObjectClass *) adg_style_parent_class)
44 enum {
45 PROP_0,
46 PROP_PATTERN
50 static void get_property (GObject *object,
51 guint prop_id,
52 GValue *value,
53 GParamSpec *pspec);
54 static void set_property (GObject *object,
55 guint prop_id,
56 const GValue *value,
57 GParamSpec *pspec);
58 static GPtrArray * get_pool (void);
59 static void apply (AdgStyle *style,
60 cairo_t *cr);
61 static void set_pattern (AdgStyle *style,
62 AdgPattern *pattern);
65 G_DEFINE_ABSTRACT_TYPE(AdgStyle, adg_style, G_TYPE_OBJECT)
68 static void
69 adg_style_class_init(AdgStyleClass *klass)
71 GObjectClass *gobject_class;
72 GParamSpec *param;
74 gobject_class = (GObjectClass *) klass;
76 g_type_class_add_private(klass, sizeof(AdgStylePrivate));
78 gobject_class->get_property = get_property;
79 gobject_class->set_property = set_property;
81 klass->get_pool = get_pool;
82 klass->apply = apply;
84 param = g_param_spec_boxed("pattern",
85 P_("Pattern"),
86 P_("The pattern associated to this style"),
87 ADG_TYPE_PATTERN, G_PARAM_READWRITE);
88 g_object_class_install_property(gobject_class, PROP_PATTERN, param);
91 static void
92 adg_style_init(AdgStyle *style)
94 AdgStylePrivate *priv =
95 G_TYPE_INSTANCE_GET_PRIVATE(style, ADG_TYPE_STYLE,
96 AdgStylePrivate);
98 priv->pattern = NULL;
100 style->priv = priv;
103 static void
104 get_property(GObject *object,
105 guint prop_id, GValue *value, GParamSpec *pspec)
107 AdgStyle *style = (AdgStyle *) object;
109 switch (prop_id) {
110 case PROP_PATTERN:
111 g_value_set_boxed(value, style->priv->pattern);
112 break;
113 default:
114 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
115 break;
119 static void
120 set_property(GObject *object,
121 guint prop_id, const GValue *value, GParamSpec *pspec)
123 AdgStyle *style = (AdgStyle *) object;
125 switch (prop_id) {
126 case PROP_PATTERN:
127 set_pattern(style, g_value_get_boxed(value));
128 break;
129 default:
130 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
131 break;
137 * adg_style_register_id:
138 * @style: an #AdgStyle derived instance
140 * Registers a new style in the internal register.
142 * Return value: the new id associated to this style or %0 on errors
144 AdgStyleId
145 adg_style_register_id(AdgStyle *style)
147 GPtrArray *pool;
149 g_return_val_if_fail(ADG_IS_STYLE(style), 0);
151 pool = ADG_STYLE_GET_CLASS(style)->get_pool();
152 g_return_val_if_fail(pool != NULL, 0);
154 g_ptr_array_add(pool, style);
156 return pool->len - 1;
160 * adg_style_from_id:
161 * @type: the style type id
162 * @id: the id to get
164 * Gets the preregistered style identified by @id of @style family.
166 * Return value: the requested style or %NULL on errors
168 AdgStyle *
169 adg_style_from_id(GType type, AdgStyleId id)
171 AdgStyleClass *klass;
172 GPtrArray *pool;
174 klass = g_type_class_ref(type);
175 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), NULL);
176 pool = klass->get_pool();
178 /* If @type is valid, @klass will be referenced by the pool items */
179 g_type_class_unref(klass);
181 if (id > pool->len)
182 return NULL;
184 return (AdgStyle *) g_ptr_array_index(pool, id);
187 AdgStyle *
188 adg_style_get_default(AdgStyleClass *klass)
190 GPtrArray *pool;
192 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), NULL);
194 pool = klass->get_pool();
195 return (AdgStyle *) g_ptr_array_index(pool, 0);
199 * adg_style_apply:
200 * @style: an #AdgStyle derived object
201 * @cr: the cairo context
203 * Applies @style to @cr so the next rendering will be done accordling to
204 * this style directives.
206 void
207 adg_style_apply(AdgStyle *style, cairo_t *cr)
209 g_return_if_fail(ADG_IS_STYLE(style));
210 g_return_if_fail(cr != NULL);
212 return ADG_STYLE_GET_CLASS(style)->apply(style, cr);
216 * adg_style_get_pattern:
217 * @style: an #AdgStyle object
219 * Gets the pattern binded to this style. The returned pattern refers to
220 * an internally managed struct that must not be modified or freed.
222 * Return value: the requested pattern or %NULL on no pattern or errors
224 const AdgPattern *
225 adg_style_get_pattern(AdgStyle *style)
227 g_return_val_if_fail(ADG_IS_STYLE(style), NULL);
229 return style->priv->pattern;
233 * adg_style_set_pattern:
234 * @style: an #AdgStyle object
235 * @pattern: the new pattern
237 * Sets a new pattern for this style. This operation will release one
238 * reference on the old pattern (if any) and will add a new reference
239 * to @pattern.
241 * A %NULL pattern is allowed: it means the previous pattern is kept
242 * during the rendering process.
244 void
245 adg_style_set_pattern(AdgStyle *style, AdgPattern *pattern)
247 g_return_if_fail(ADG_IS_STYLE(style));
248 g_return_if_fail(pattern != NULL);
250 set_pattern(style, pattern);
251 g_object_notify((GObject *) style, "pattern");
255 static GPtrArray *
256 get_pool(void)
258 g_warning("Uninmplemented get_pool()");
259 return NULL;
262 static void
263 apply(AdgStyle *style, cairo_t *cr)
265 if (style->priv->pattern != NULL)
266 cairo_set_source(cr, style->priv->pattern);
269 static void
270 set_pattern(AdgStyle *style, AdgPattern *pattern)
272 if (style->priv->pattern != NULL)
273 cairo_pattern_destroy(style->priv->pattern);
275 if (pattern != NULL)
276 cairo_pattern_reference(pattern);
278 style->priv->pattern = pattern;