adg: avoid invalid access to private data
[adg.git] / src / adg / adg-arrow.c
blob4b6417b1246fd29c748cfc9ddf803350a40fa0be
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2019 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:adg-arrow
23 * @short_description: Arrow rendering related stuff
25 * Contains parameters on how to draw arrows, providing a way to register a
26 * custom rendering callback.
28 * <note><para>
29 * By default, the #AdgEntity:local-mix property is set to #ADG_MIX_PARENT
30 * on #AdgArrow entities.
31 * </para></note>
33 * Since: 1.0
34 **/
36 /**
37 * AdgArrow:
39 * All fields are private and should not be used directly.
40 * Use its public methods instead.
42 * Since: 1.0
43 **/
46 #include "adg-internal.h"
47 #include "adg-model.h"
48 #include "adg-trail.h"
49 #include "adg-path.h"
50 #include "adg-marker.h"
52 #include "adg-arrow.h"
53 #include "adg-arrow-private.h"
56 G_DEFINE_TYPE(AdgArrow, adg_arrow, ADG_TYPE_MARKER)
58 enum {
59 PROP_0,
60 PROP_ANGLE
64 static void _adg_get_property (GObject *object,
65 guint prop_id,
66 GValue *value,
67 GParamSpec *pspec);
68 static void _adg_set_property (GObject *object,
69 guint prop_id,
70 const GValue *value,
71 GParamSpec *pspec);
72 static void _adg_arrange (AdgEntity *entity);
73 static void _adg_render (AdgEntity *entity,
74 cairo_t *cr);
75 static AdgModel * _adg_create_model (AdgMarker *marker);
78 static void
79 adg_arrow_class_init(AdgArrowClass *klass)
81 GObjectClass *gobject_class;
82 AdgEntityClass *entity_class;
83 AdgMarkerClass *marker_class;
84 GParamSpec *param;
86 gobject_class = (GObjectClass *) klass;
87 entity_class = (AdgEntityClass *) klass;
88 marker_class = (AdgMarkerClass *) klass;
90 g_type_class_add_private(klass, sizeof(AdgArrowPrivate));
92 gobject_class->set_property = _adg_set_property;
93 gobject_class->get_property = _adg_get_property;
95 entity_class->arrange = _adg_arrange;
96 entity_class->render = _adg_render;
98 marker_class->create_model = _adg_create_model;
100 param = g_param_spec_double("angle",
101 P_("Arrow Angle"),
102 P_("The opening angle of the arrow"),
103 -G_PI, G_PI, G_PI / 6,
104 G_PARAM_READWRITE);
105 g_object_class_install_property(gobject_class, PROP_ANGLE, param);
108 static void
109 adg_arrow_init(AdgArrow *arrow)
111 AdgArrowPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(arrow,
112 ADG_TYPE_ARROW,
113 AdgArrowPrivate);
115 data->angle = G_PI/6;
117 arrow->data = data;
119 adg_entity_set_local_mix((AdgEntity *) arrow, ADG_MIX_PARENT);
122 static void
123 _adg_get_property(GObject *object, guint prop_id,
124 GValue *value, GParamSpec *pspec)
126 AdgArrowPrivate *data = ((AdgArrow *) object)->data;
128 switch (prop_id) {
129 case PROP_ANGLE:
130 g_value_set_double(value, data->angle);
131 break;
132 default:
133 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
134 break;
138 static void
139 _adg_set_property(GObject *object, guint prop_id,
140 const GValue *value, GParamSpec *pspec)
142 AdgArrowPrivate *data = ((AdgArrow *) object)->data;
144 switch (prop_id) {
145 case PROP_ANGLE:
146 data->angle = cpml_angle(g_value_get_double(value));
147 break;
148 default:
149 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
150 break;
156 * adg_arrow_new:
158 * Creates a new undefined arrow entity. The position must be defined
159 * by setting the #AdgMarker:trail and #AdgMarker:pos properties.
161 * Returns: the newly created arrow entity
163 * Since: 1.0
165 AdgArrow *
166 adg_arrow_new(void)
168 return g_object_new(ADG_TYPE_ARROW, NULL);
172 * adg_arrow_new_with_trail:
173 * @trail: the #AdgTrail where the arrow should be added
174 * @pos: the position ratio on @trail
176 * Creates a new arrow on the first segment on @trail at position
177 * @pos, where @pos is a ratio of the @trail length (being 0 the
178 * start point, 1 the end point, 0.5 the middle point and so on).
179 * By default, an arrow as #AdgEntity:local-mix set to #ADG_MIX_PARENT.
181 * Returns: the newly created arrow entity
183 * Since: 1.0
185 AdgArrow *
186 adg_arrow_new_with_trail(AdgTrail *trail, gdouble pos)
188 return g_object_new(ADG_TYPE_ARROW,
189 "local-mix", ADG_MIX_PARENT,
190 "trail", trail,
191 "n-segment", 1,
192 "pos", pos,
193 NULL);
197 * adg_arrow_set_angle:
198 * @arrow: an #AdgArrow
199 * @angle: the new angle
201 * Sets a new angle: @angle will be the new opening angle of @arrow.
202 * Changing the arrow angle will invalidate @arrow.
204 * Since: 1.0
206 void
207 adg_arrow_set_angle(AdgArrow *arrow, gdouble angle)
209 g_return_if_fail(ADG_IS_ARROW(arrow));
210 g_object_set(arrow, "angle", angle, NULL);
214 * adg_arrow_get_angle:
215 * @arrow: an #AdgArrow
217 * Gets the current angle of @arrow.
219 * Returns: the arrow angle, in radians
221 * Since: 1.0
223 gdouble
224 adg_arrow_get_angle(AdgArrow *arrow)
226 AdgArrowPrivate *data;
228 g_return_val_if_fail(ADG_IS_ARROW(arrow), 0);
230 data = arrow->data;
232 return data->angle;
236 static void
237 _adg_arrange(AdgEntity *entity)
239 AdgModel *model;
240 const CpmlExtents *extents;
241 CpmlExtents new_extents;
243 model = adg_marker_model((AdgMarker *) entity);
244 if (model == NULL)
245 return;
247 extents = adg_trail_get_extents((AdgTrail *) model);
248 if (extents == NULL)
249 return;
251 cpml_extents_copy(&new_extents, extents);
252 cpml_extents_transform(&new_extents, adg_entity_get_local_matrix(entity));
253 adg_entity_set_extents(entity, &new_extents);
256 static void
257 _adg_render(AdgEntity *entity, cairo_t *cr)
259 AdgModel *model;
260 const cairo_path_t *cairo_path;
262 model = adg_marker_model((AdgMarker *) entity);
263 if (model == NULL)
264 return;
266 cairo_path = adg_trail_get_cairo_path((AdgTrail *) model);
268 if (cairo_path != NULL) {
269 cairo_save(cr);
270 cairo_transform(cr, adg_entity_get_global_matrix(entity));
271 cairo_transform(cr, adg_entity_get_local_matrix(entity));
272 cairo_append_path(cr, cairo_path);
273 cairo_restore(cr);
275 cairo_fill(cr);
279 static AdgModel *
280 _adg_create_model(AdgMarker *marker)
282 AdgArrowPrivate *data;
283 AdgPath *path;
284 CpmlPair p1, p2;
286 data = ((AdgArrow *) marker)->data;
287 path = adg_path_new();
288 cpml_vector_from_angle(&p1, data->angle / 2);
289 p2.x = p1.x;
290 p2.y = -p1.y;
292 adg_path_move_to_explicit(path, 0, 0);
293 adg_path_line_to(path, &p1);
294 adg_path_line_to(path, &p2);
295 adg_path_close(path);
297 return (AdgModel *) path;