Implemented patterns in AdgStyle
[adg.git] / adg / adg-style.c
blob075af2f9056db8cc7c0aa331dd2c93fcdc53e345
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 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->apply = apply;
81 param = g_param_spec_boxed ("pattern",
82 P_("Pattern"),
83 P_("The pattern associated to this style"),
84 ADG_TYPE_PATTERN,
85 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 = G_TYPE_INSTANCE_GET_PRIVATE (style, ADG_TYPE_STYLE,
93 AdgStylePrivate);
95 priv->pattern = NULL;
97 style->priv = priv;
100 static void
101 get_property (GObject *object,
102 guint prop_id,
103 GValue *value,
104 GParamSpec *pspec)
106 AdgStyle *style = (AdgStyle *) object;
108 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,
122 const GValue *value,
123 GParamSpec *pspec)
125 AdgStyle *style = (AdgStyle *) object;
127 switch (prop_id)
129 case PROP_PATTERN:
130 set_pattern (style, g_value_get_boxed (value));
131 break;
132 default:
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 break;
140 * adg_style_apply:
141 * @style: an #AdgStyle derived object
142 * @cr: the cairo context
144 * Applies @style to @cr so the next rendering will be done accordling to
145 * this style directives.
147 void
148 adg_style_apply (AdgStyle *style,
149 cairo_t *cr)
151 g_return_if_fail (ADG_IS_STYLE (style));
152 g_return_if_fail (cr != NULL);
154 return ADG_STYLE_GET_CLASS (style)->apply (style, cr);
158 * adg_style_get_pattern:
159 * @style: an #AdgStyle object
161 * Gets the pattern binded to this style. The returned pattern refers to
162 * an internally managed struct that must not be modified or freed.
164 * Return value: the requested pattern or %NULL on no pattern or errors
166 const AdgPattern *
167 adg_style_get_pattern (AdgStyle *style)
169 g_return_val_if_fail (ADG_IS_STYLE (style), NULL);
171 return style->priv->pattern;
175 * adg_style_set_pattern:
176 * @style: an #AdgStyle object
177 * @pattern: the new pattern
179 * Sets a new pattern for this style. This operation will release one
180 * reference on the old pattern (if any) and will add a new reference
181 * to @pattern.
183 * A %NULL pattern is allowed: it means the previous pattern is kept
184 * during the rendering process.
186 void
187 adg_style_set_pattern (AdgStyle *style,
188 AdgPattern *pattern)
190 g_return_if_fail (ADG_IS_STYLE (style));
191 g_return_if_fail (pattern != NULL);
193 set_pattern (style, pattern);
194 g_object_notify ((GObject *) style, "pattern");
198 static void
199 apply (AdgStyle *style,
200 cairo_t *cr)
202 if (style->priv->pattern != NULL)
203 cairo_set_source (cr, style->priv->pattern);
206 static void
207 set_pattern (AdgStyle *style,
208 AdgPattern *pattern)
210 if (style->priv->pattern != NULL)
211 cairo_pattern_destroy (style->priv->pattern);
213 if (pattern != NULL)
214 cairo_pattern_reference (pattern);
216 style->priv->pattern = pattern;