Implemented adg_style_from_id() and tested on AdgLineStyle
[adg.git] / adg / adg-style.c
blobb1295da4ebae423b65c713fc4599dd12cd4be42f
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2008, 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 Library 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 * Library 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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 /**
22 * SECTION:style
23 * @title: AdgStyle
24 * @short_description: The base class of all styling objects.
27 #include "adg-style.h"
28 #include "adg-style-private.h"
29 #include "adg-line-style.h"
30 #include "adg-font-style.h"
31 #include "adg-arrow-style.h"
32 #include "adg-enums.h"
33 #include "adg-util.h"
34 #include "adg-intl.h"
36 #include <string.h>
38 #define PARENT_CLASS ((GObjectClass *) adg_style_parent_class)
41 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);
57 static AdgStyle*from_id (gint id);
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->from_id = from_id;
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,
87 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 = 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,
105 GValue *value,
106 GParamSpec *pspec)
108 AdgStyle *style = (AdgStyle *) object;
110 switch (prop_id)
112 case PROP_PATTERN:
113 g_value_set_boxed (value, style->priv->pattern);
114 break;
115 default:
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 break;
121 static void
122 set_property (GObject *object,
123 guint prop_id,
124 const GValue *value,
125 GParamSpec *pspec)
127 AdgStyle *style = (AdgStyle *) object;
129 switch (prop_id)
131 case PROP_PATTERN:
132 set_pattern (style, g_value_get_boxed (value));
133 break;
134 default:
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 break;
142 * adg_style_from_id:
143 * @style_type: the type id from which obtain the style
144 * @id: the id to get
146 * Gets the preregistered style identified by @id of @style_type family.
148 * Return value: the requested style or %NULL on errors
150 AdgStyle *
151 adg_style_from_id (GType style_type,
152 gint id)
154 AdgStyleClass *klass = g_type_class_ref (style_type);
156 g_return_val_if_fail (ADG_IS_STYLE_CLASS (klass), NULL);
158 return klass->from_id (id);
162 * adg_style_apply:
163 * @style: an #AdgStyle derived object
164 * @cr: the cairo context
166 * Applies @style to @cr so the next rendering will be done accordling to
167 * this style directives.
169 void
170 adg_style_apply (AdgStyle *style,
171 cairo_t *cr)
173 g_return_if_fail (ADG_IS_STYLE (style));
174 g_return_if_fail (cr != NULL);
176 return ADG_STYLE_GET_CLASS (style)->apply (style, cr);
180 * adg_style_get_pattern:
181 * @style: an #AdgStyle object
183 * Gets the pattern binded to this style. The returned pattern refers to
184 * an internally managed struct that must not be modified or freed.
186 * Return value: the requested pattern or %NULL on no pattern or errors
188 const AdgPattern *
189 adg_style_get_pattern (AdgStyle *style)
191 g_return_val_if_fail (ADG_IS_STYLE (style), NULL);
193 return style->priv->pattern;
197 * adg_style_set_pattern:
198 * @style: an #AdgStyle object
199 * @pattern: the new pattern
201 * Sets a new pattern for this style. This operation will release one
202 * reference on the old pattern (if any) and will add a new reference
203 * to @pattern.
205 * A %NULL pattern is allowed: it means the previous pattern is kept
206 * during the rendering process.
208 void
209 adg_style_set_pattern (AdgStyle *style,
210 AdgPattern *pattern)
212 g_return_if_fail (ADG_IS_STYLE (style));
213 g_return_if_fail (pattern != NULL);
215 set_pattern (style, pattern);
216 g_object_notify ((GObject *) style, "pattern");
220 static AdgStyle *
221 from_id (gint id)
223 g_warning ("Uninmplemented from_id()");
224 return NULL;
227 static void
228 apply (AdgStyle *style,
229 cairo_t *cr)
231 if (style->priv->pattern != NULL)
232 cairo_set_source (cr, style->priv->pattern);
235 static void
236 set_pattern (AdgStyle *style,
237 AdgPattern *pattern)
239 if (style->priv->pattern != NULL)
240 cairo_pattern_destroy (style->priv->pattern);
242 if (pattern != NULL)
243 cairo_pattern_reference (pattern);
245 style->priv->pattern = pattern;