[docs] NEWS updated
[adg.git] / adg / adg-style.c
blob399b094cccbd8344075c91cd5b6127ad4d51ae03
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.
20 /**
21 * SECTION:style
22 * @title: AdgStyle
23 * @short_description: The base class of all styling objects
25 * This is the fundamental abstract class from which all customization object
26 * must be derived.
29 #include "adg-style.h"
30 #include "adg-style-private.h"
31 #include "adg-line-style.h"
32 #include "adg-font-style.h"
33 #include "adg-arrow-style.h"
34 #include "adg-enums.h"
35 #include "adg-util.h"
36 #include "adg-intl.h"
38 #include <string.h>
40 #define PARENT_CLASS ((GObjectClass *) adg_style_parent_class)
43 enum {
44 PROP_0,
45 PROP_PATTERN
49 static void get_property (GObject *object,
50 guint prop_id,
51 GValue *value,
52 GParamSpec *pspec);
53 static void set_property (GObject *object,
54 guint prop_id,
55 const GValue *value,
56 GParamSpec *pspec);
57 static GPtrArray * get_pool (void);
58 static void apply (AdgStyle *style,
59 cairo_t *cr);
60 static void set_pattern (AdgStyle *style,
61 AdgPattern *pattern);
64 G_DEFINE_ABSTRACT_TYPE(AdgStyle, adg_style, G_TYPE_OBJECT)
67 static void
68 adg_style_class_init(AdgStyleClass *klass)
70 GObjectClass *gobject_class;
71 GParamSpec *param;
73 gobject_class = (GObjectClass *) klass;
75 g_type_class_add_private(klass, sizeof(AdgStylePrivate));
77 gobject_class->get_property = get_property;
78 gobject_class->set_property = set_property;
80 klass->get_pool = get_pool;
81 klass->apply = apply;
83 param = g_param_spec_boxed("pattern",
84 P_("Pattern"),
85 P_("The pattern associated to this style"),
86 ADG_TYPE_PATTERN, G_PARAM_READWRITE);
87 g_object_class_install_property(gobject_class, PROP_PATTERN, param);
90 static void
91 adg_style_init(AdgStyle *style)
93 AdgStylePrivate *priv =
94 G_TYPE_INSTANCE_GET_PRIVATE(style, ADG_TYPE_STYLE,
95 AdgStylePrivate);
97 priv->pattern = NULL;
99 style->priv = priv;
102 static void
103 get_property(GObject *object,
104 guint prop_id, GValue *value, GParamSpec *pspec)
106 AdgStyle *style = (AdgStyle *) object;
108 switch (prop_id) {
109 case PROP_PATTERN:
110 g_value_set_boxed(value, style->priv->pattern);
111 break;
112 default:
113 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
114 break;
118 static void
119 set_property(GObject *object,
120 guint prop_id, const GValue *value, GParamSpec *pspec)
122 AdgStyle *style = (AdgStyle *) object;
124 switch (prop_id) {
125 case PROP_PATTERN:
126 set_pattern(style, g_value_get_boxed(value));
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
130 break;
136 * adg_style_register_id:
137 * @style: an #AdgStyle derived instance
139 * Registers a new style in the internal register.
141 * Return value: the new id associated to this style or %0 on errors
143 AdgStyleId
144 adg_style_register_id(AdgStyle *style)
146 GPtrArray *pool;
148 g_return_val_if_fail(ADG_IS_STYLE(style), 0);
150 pool = ADG_STYLE_GET_CLASS(style)->get_pool();
151 g_return_val_if_fail(pool != NULL, 0);
153 g_ptr_array_add(pool, style);
155 return pool->len - 1;
159 * adg_style_from_id:
160 * @type: the style type id
161 * @id: the id to get
163 * Gets the preregistered style identified by @id of @style family.
165 * Return value: the requested style or %NULL on errors
167 AdgStyle *
168 adg_style_from_id(GType type, AdgStyleId id)
170 AdgStyleClass *klass;
171 GPtrArray *pool;
173 klass = g_type_class_ref(type);
174 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), NULL);
175 pool = klass->get_pool();
177 /* If @type is valid, @klass will be referenced by the pool items */
178 g_type_class_unref(klass);
180 if (id > pool->len)
181 return NULL;
183 return (AdgStyle *) g_ptr_array_index(pool, id);
186 AdgStyle *
187 adg_style_get_default(AdgStyleClass *klass)
189 GPtrArray *pool;
191 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), NULL);
193 pool = klass->get_pool();
194 return (AdgStyle *) g_ptr_array_index(pool, 0);
198 * adg_style_apply:
199 * @style: an #AdgStyle derived object
200 * @cr: the cairo context
202 * Applies @style to @cr so the next rendering will be done accordling to
203 * this style directives.
205 void
206 adg_style_apply(AdgStyle *style, cairo_t *cr)
208 g_return_if_fail(ADG_IS_STYLE(style));
209 g_return_if_fail(cr != NULL);
211 return ADG_STYLE_GET_CLASS(style)->apply(style, cr);
215 * adg_style_get_pattern:
216 * @style: an #AdgStyle object
218 * Gets the pattern binded to this style. The returned pattern refers to
219 * an internally managed struct that must not be modified or freed.
221 * Return value: the requested pattern or %NULL on no pattern or errors
223 const AdgPattern *
224 adg_style_get_pattern(AdgStyle *style)
226 g_return_val_if_fail(ADG_IS_STYLE(style), NULL);
228 return style->priv->pattern;
232 * adg_style_set_pattern:
233 * @style: an #AdgStyle object
234 * @pattern: the new pattern
236 * Sets a new pattern for this style. This operation will release one
237 * reference on the old pattern (if any) and will add a new reference
238 * to @pattern.
240 * A %NULL pattern is allowed: it means the previous pattern is kept
241 * during the rendering process.
243 void
244 adg_style_set_pattern(AdgStyle *style, AdgPattern *pattern)
246 g_return_if_fail(ADG_IS_STYLE(style));
247 g_return_if_fail(pattern != NULL);
249 set_pattern(style, pattern);
250 g_object_notify((GObject *) style, "pattern");
254 static GPtrArray *
255 get_pool(void)
257 g_warning("Uninmplemented get_pool()");
258 return NULL;
261 static void
262 apply(AdgStyle *style, cairo_t *cr)
264 if (style->priv->pattern != NULL)
265 cairo_set_source(cr, style->priv->pattern);
268 static void
269 set_pattern(AdgStyle *style, AdgPattern *pattern)
271 if (style->priv->pattern != NULL)
272 cairo_pattern_destroy(style->priv->pattern);
274 if (pattern != NULL)
275 cairo_pattern_reference(pattern);
277 style->priv->pattern = pattern;