[AdgArrowStyle] Hidden private struct
[adg.git] / adg / adg-style.c
blob66294903f11c2106efc498ed52098fd65a5d4fe7
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>
42 enum {
43 PROP_0,
44 PROP_PATTERN
48 static void get_property (GObject *object,
49 guint prop_id,
50 GValue *value,
51 GParamSpec *pspec);
52 static void set_property (GObject *object,
53 guint prop_id,
54 const GValue *value,
55 GParamSpec *pspec);
56 static GPtrArray * get_pool (void);
57 static void apply (AdgStyle *style,
58 cairo_t *cr);
59 static void set_pattern (AdgStyle *style,
60 AdgPattern *pattern);
63 G_DEFINE_ABSTRACT_TYPE(AdgStyle, adg_style, G_TYPE_OBJECT)
66 static void
67 adg_style_class_init(AdgStyleClass *klass)
69 GObjectClass *gobject_class;
70 GParamSpec *param;
72 gobject_class = (GObjectClass *) klass;
74 g_type_class_add_private(klass, sizeof(AdgStylePrivate));
76 gobject_class->get_property = get_property;
77 gobject_class->set_property = set_property;
79 klass->get_pool = get_pool;
80 klass->apply = apply;
82 param = g_param_spec_boxed("pattern",
83 P_("Pattern"),
84 P_("The pattern associated to this style"),
85 ADG_TYPE_PATTERN, G_PARAM_READWRITE);
86 g_object_class_install_property(gobject_class, PROP_PATTERN, param);
89 static void
90 adg_style_init(AdgStyle *style)
92 AdgStylePrivate *priv =
93 G_TYPE_INSTANCE_GET_PRIVATE(style, ADG_TYPE_STYLE,
94 AdgStylePrivate);
96 priv->pattern = NULL;
98 style->priv = priv;
101 static void
102 get_property(GObject *object,
103 guint prop_id, GValue *value, GParamSpec *pspec)
105 AdgStyle *style = (AdgStyle *) object;
107 switch (prop_id) {
108 case PROP_PATTERN:
109 g_value_set_boxed(value, style->priv->pattern);
110 break;
111 default:
112 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
113 break;
117 static void
118 set_property(GObject *object,
119 guint prop_id, const GValue *value, GParamSpec *pspec)
121 AdgStyle *style = (AdgStyle *) object;
123 switch (prop_id) {
124 case PROP_PATTERN:
125 set_pattern(style, g_value_get_boxed(value));
126 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
129 break;
135 * adg_style_register_id:
136 * @style: an #AdgStyle derived instance
138 * Registers a new style in the internal register.
140 * Return value: the new id associated to this style or %0 on errors
142 AdgStyleId
143 adg_style_register_id(AdgStyle *style)
145 GPtrArray *pool;
147 g_return_val_if_fail(ADG_IS_STYLE(style), 0);
149 pool = ADG_STYLE_GET_CLASS(style)->get_pool();
150 g_return_val_if_fail(pool != NULL, 0);
152 g_ptr_array_add(pool, style);
154 return pool->len - 1;
158 * adg_style_from_id:
159 * @type: the style type id
160 * @id: the id to get
162 * Gets the preregistered style identified by @id of @style family.
164 * Return value: the requested style or %NULL on errors
166 AdgStyle *
167 adg_style_from_id(GType type, AdgStyleId id)
169 AdgStyleClass *klass;
170 GPtrArray *pool;
172 klass = g_type_class_ref(type);
173 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), NULL);
174 pool = klass->get_pool();
176 /* If @type is valid, @klass will be referenced by the pool items */
177 g_type_class_unref(klass);
179 if (id > pool->len)
180 return NULL;
182 return (AdgStyle *) g_ptr_array_index(pool, id);
185 AdgStyle *
186 adg_style_get_default(AdgStyleClass *klass)
188 GPtrArray *pool;
190 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), NULL);
192 pool = klass->get_pool();
193 return (AdgStyle *) g_ptr_array_index(pool, 0);
197 * adg_style_apply:
198 * @style: an #AdgStyle derived object
199 * @cr: the cairo context
201 * Applies @style to @cr so the next rendering will be done accordling to
202 * this style directives.
204 void
205 adg_style_apply(AdgStyle *style, cairo_t *cr)
207 g_return_if_fail(ADG_IS_STYLE(style));
208 g_return_if_fail(cr != NULL);
210 return ADG_STYLE_GET_CLASS(style)->apply(style, cr);
214 * adg_style_get_pattern:
215 * @style: an #AdgStyle object
217 * Gets the pattern binded to this style. The returned pattern refers to
218 * an internally managed struct that must not be modified or freed.
220 * Return value: the requested pattern or %NULL on no pattern or errors
222 const AdgPattern *
223 adg_style_get_pattern(AdgStyle *style)
225 g_return_val_if_fail(ADG_IS_STYLE(style), NULL);
227 return style->priv->pattern;
231 * adg_style_set_pattern:
232 * @style: an #AdgStyle object
233 * @pattern: the new pattern
235 * Sets a new pattern for this style. This operation will release one
236 * reference on the old pattern (if any) and will add a new reference
237 * to @pattern.
239 * A %NULL pattern is allowed: it means the previous pattern is kept
240 * during the rendering process.
242 void
243 adg_style_set_pattern(AdgStyle *style, AdgPattern *pattern)
245 g_return_if_fail(ADG_IS_STYLE(style));
246 g_return_if_fail(pattern != NULL);
248 set_pattern(style, pattern);
249 g_object_notify((GObject *) style, "pattern");
253 static GPtrArray *
254 get_pool(void)
256 g_warning("Uninmplemented get_pool()");
257 return NULL;
260 static void
261 apply(AdgStyle *style, cairo_t *cr)
263 if (style->priv->pattern != NULL)
264 cairo_set_source(cr, style->priv->pattern);
267 static void
268 set_pattern(AdgStyle *style, AdgPattern *pattern)
270 if (style->priv->pattern != NULL)
271 cairo_pattern_destroy(style->priv->pattern);
273 if (pattern != NULL)
274 cairo_pattern_reference(pattern);
276 style->priv->pattern = pattern;