Tested build process under ss-dev of OpenIndiana
[adg.git] / src / adg / adg-arrow.c
blob11a07856073598cede5943b9dc9b23679e8011fa
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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.
27 **/
29 /**
30 * AdgArrow:
32 * All fields are private and should not be used directly.
33 * Use its public methods instead.
34 **/
37 #include "adg-internal.h"
38 #include "adg-model.h"
39 #include "adg-trail.h"
40 #include "adg-path.h"
41 #include "adg-marker.h"
43 #include "adg-arrow.h"
44 #include "adg-arrow-private.h"
47 G_DEFINE_TYPE(AdgArrow, adg_arrow, ADG_TYPE_MARKER)
49 enum {
50 PROP_0,
51 PROP_ANGLE
55 static void _adg_get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void _adg_set_property (GObject *object,
60 guint prop_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void _adg_arrange (AdgEntity *entity);
64 static void _adg_render (AdgEntity *entity,
65 cairo_t *cr);
66 static AdgModel * _adg_create_model (AdgMarker *marker);
69 static void
70 adg_arrow_class_init(AdgArrowClass *klass)
72 GObjectClass *gobject_class;
73 AdgEntityClass *entity_class;
74 AdgMarkerClass *marker_class;
75 GParamSpec *param;
77 gobject_class = (GObjectClass *) klass;
78 entity_class = (AdgEntityClass *) klass;
79 marker_class = (AdgMarkerClass *) klass;
81 g_type_class_add_private(klass, sizeof(AdgArrowPrivate));
83 gobject_class->set_property = _adg_set_property;
84 gobject_class->get_property = _adg_get_property;
86 entity_class->arrange = _adg_arrange;
87 entity_class->render = _adg_render;
89 marker_class->create_model = _adg_create_model;
91 param = g_param_spec_double("angle",
92 P_("Arrow Angle"),
93 P_("The opening angle of the arrow"),
94 -G_PI, G_PI, G_PI / 6,
95 G_PARAM_READWRITE);
96 g_object_class_install_property(gobject_class, PROP_ANGLE, param);
99 static void
100 adg_arrow_init(AdgArrow *arrow)
102 AdgArrowPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(arrow,
103 ADG_TYPE_ARROW,
104 AdgArrowPrivate);
106 data->angle = G_PI/6;
108 arrow->data = data;
111 static void
112 _adg_get_property(GObject *object, guint prop_id,
113 GValue *value, GParamSpec *pspec)
115 AdgArrowPrivate *data = ((AdgArrow *) object)->data;
117 switch (prop_id) {
118 case PROP_ANGLE:
119 g_value_set_double(value, data->angle);
120 break;
121 default:
122 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
123 break;
127 static void
128 _adg_set_property(GObject *object, guint prop_id,
129 const GValue *value, GParamSpec *pspec)
131 AdgArrowPrivate *data = ((AdgArrow *) object)->data;
133 switch (prop_id) {
134 case PROP_ANGLE:
135 data->angle = cpml_angle(g_value_get_double(value));
136 break;
137 default:
138 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
139 break;
145 * adg_arrow_new:
146 * @trail: the #AdgTrail to arrow
148 * Creates a new undefined arrow entity. The position must be defined
149 * by setting the #AdgMarker:trail and #AdgMarker:pos properties.
150 * By default, an arrow as #AdgEntity:local-method set to #ADG_MIX_PARENT.
152 * Returns: the newly created arrow entity
154 AdgArrow *
155 adg_arrow_new(void)
157 return g_object_new(ADG_TYPE_ARROW,
158 "local-method", ADG_MIX_PARENT,
159 NULL);
163 * adg_arrow_new_with_trail:
164 * @trail: the #AdgTrail where the arrow should be added
165 * @pos: the position ratio on @trail
167 * Creates a new arrow on the first segment on @trail at position
168 * @pos, where @pos is a ratio of the @trail length (being %0 the
169 * start point, %1 the end point, %0.5 the middle point and so on).
170 * By default, an arrow as #AdgEntity:local-method set to #ADG_MIX_PARENT.
172 * Returns: the newly created arrow entity
174 AdgArrow *
175 adg_arrow_new_with_trail(AdgTrail *trail, gdouble pos)
177 return g_object_new(ADG_TYPE_ARROW,
178 "local-method", ADG_MIX_PARENT,
179 "trail", trail,
180 "n-segment", 1,
181 "pos", pos,
182 NULL);
186 * adg_arrow_set_angle:
187 * @arrow: an #AdgArrow
188 * @angle: the new angle
190 * Sets a new angle: @angle will be the new opening angle of @arrow.
191 * Changing the arrow angle will invalidate @arrow.
193 void
194 adg_arrow_set_angle(AdgArrow *arrow, gdouble angle)
196 g_return_if_fail(ADG_IS_ARROW(arrow));
197 g_object_set(arrow, "angle", angle, NULL);
201 * adg_arrow_get_angle:
202 * @arrow: an #AdgArrow
204 * Gets the current angle of @arrow.
206 * Returns: the arrow angle, in radians
208 gdouble
209 adg_arrow_get_angle(AdgArrow *arrow)
211 AdgArrowPrivate *data;
213 g_return_val_if_fail(ADG_IS_ARROW(arrow), 0);
215 data = arrow->data;
217 return data->angle;
221 static void
222 _adg_arrange(AdgEntity *entity)
224 /* TODO */
227 static void
228 _adg_render(AdgEntity *entity, cairo_t *cr)
230 AdgModel *model;
231 const cairo_path_t *cairo_path;
233 model = adg_marker_model((AdgMarker *) entity);
234 cairo_path = adg_trail_get_cairo_path((AdgTrail *) model);
236 if (cairo_path != NULL) {
237 cairo_save(cr);
238 cairo_transform(cr, adg_entity_get_global_matrix(entity));
239 cairo_transform(cr, adg_entity_get_local_matrix(entity));
240 cairo_append_path(cr, cairo_path);
241 cairo_restore(cr);
243 cairo_fill(cr);
247 static AdgModel *
248 _adg_create_model(AdgMarker *marker)
250 AdgArrowPrivate *data;
251 AdgPath *path;
252 CpmlPair p1, p2;
254 data = ((AdgArrow *) marker)->data;
255 path = adg_path_new();
256 cpml_vector_from_angle(&p1, data->angle / 2);
257 p2.x = p1.x;
258 p2.y = -p1.y;
260 adg_path_move_to_explicit(path, 0, 0);
261 adg_path_line_to(path, &p1);
262 adg_path_line_to(path, &p2);
263 adg_path_close(path);
265 return (AdgModel *) path;