[AdgTrail] Avoid recursion caused by bad callbacks
[adg.git] / adg / adg-arrow.c
blob414425d9be272bf25b4ece0ee919286375e621a4
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.
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-arrow.h"
38 #include "adg-arrow-private.h"
39 #include "adg-path.h"
40 #include "adg-intl.h"
43 enum {
44 PROP_0,
45 PROP_ANGLE
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 void arrange (AdgEntity *entity);
58 static void render (AdgEntity *entity,
59 cairo_t *cr);
60 static AdgModel * create_model (AdgMarker *marker);
61 static gboolean set_angle (AdgArrow *arrow,
62 gdouble angle);
65 G_DEFINE_TYPE(AdgArrow, adg_arrow, ADG_TYPE_MARKER);
68 static void
69 adg_arrow_class_init(AdgArrowClass *klass)
71 GObjectClass *gobject_class;
72 AdgEntityClass *entity_class;
73 AdgMarkerClass *marker_class;
74 GParamSpec *param;
76 gobject_class = (GObjectClass *) klass;
77 entity_class = (AdgEntityClass *) klass;
78 marker_class = (AdgMarkerClass *) klass;
80 g_type_class_add_private(klass, sizeof(AdgArrowPrivate));
82 gobject_class->set_property = set_property;
83 gobject_class->get_property = get_property;
85 entity_class->arrange = arrange;
86 entity_class->render = render;
88 marker_class->create_model = create_model;
90 param = g_param_spec_double("angle",
91 P_("Arrow Angle"),
92 P_("The opening angle of the arrow"),
93 0, G_PI*2, G_PI/6,
94 G_PARAM_READWRITE);
95 g_object_class_install_property(gobject_class, PROP_ANGLE, param);
98 static void
99 adg_arrow_init(AdgArrow *arrow)
101 AdgArrowPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(arrow,
102 ADG_TYPE_ARROW,
103 AdgArrowPrivate);
105 data->angle = G_PI/6;
107 arrow->data = data;
110 static void
111 get_property(GObject *object,
112 guint prop_id, GValue *value, GParamSpec *pspec)
114 AdgArrowPrivate *data = ((AdgArrow *) object)->data;
116 switch (prop_id) {
117 case PROP_ANGLE:
118 g_value_set_double(value, data->angle);
119 break;
120 default:
121 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
122 break;
126 static void
127 set_property(GObject *object,
128 guint prop_id, const GValue *value, GParamSpec *pspec)
130 AdgArrow *arrow = (AdgArrow *) object;
132 switch (prop_id) {
133 case PROP_ANGLE:
134 set_angle(arrow, g_value_get_double(value));
135 break;
136 default:
137 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
138 break;
144 * adg_arrow_set_angle:
145 * @arrow: an #AdgArrow
146 * @angle: the new angle
148 * Sets a new angle: @angle will be the new opening angle of @arrow.
149 * Changing the arrow angle will invalidate @arrow.
151 void
152 adg_arrow_set_angle(AdgArrow *arrow, gdouble angle)
154 g_return_if_fail(ADG_IS_ARROW(arrow));
156 if (set_angle(arrow, angle))
157 g_object_notify((GObject *) arrow, "angle");
161 * adg_arrow_get_angle:
162 * @arrow: an #AdgArrow
164 * Gets the current angle of @arrow.
166 * Returns: the arrow angle, in radians
168 gdouble
169 adg_arrow_get_angle(AdgArrow *arrow)
171 AdgArrowPrivate *data;
173 g_return_val_if_fail(ADG_IS_ARROW(arrow), 0);
175 data = arrow->data;
177 return data->angle;
181 static void
182 arrange(AdgEntity *entity)
184 /* TODO */
187 static void
188 render(AdgEntity *entity, cairo_t *cr)
190 AdgModel *model;
191 const cairo_path_t *cairo_path;
193 model = adg_marker_model((AdgMarker *) entity);
194 cairo_path = adg_trail_get_cairo_path((AdgTrail *) model);
196 if (cairo_path != NULL) {
197 cairo_save(cr);
198 cairo_transform(cr, adg_entity_get_local_matrix(entity));
199 cairo_append_path(cr, cairo_path);
200 cairo_restore(cr);
202 cairo_fill(cr);
206 static AdgModel *
207 create_model(AdgMarker *marker)
209 AdgArrowPrivate *data;
210 AdgPath *path;
211 CpmlPair p1, p2;
213 data = ((AdgArrow *) marker)->data;
214 path = adg_path_new();
215 cpml_vector_from_angle(&p1, data->angle / 2);
216 p2.x = p1.x;
217 p2.y = -p1.y;
219 adg_path_move_to_explicit(path, 0, 0);
220 adg_path_line_to(path, &p1);
221 adg_path_line_to(path, &p2);
222 adg_path_close(path);
224 return (AdgModel *) path;
227 static gboolean
228 set_angle(AdgArrow *arrow, gdouble angle)
230 AdgArrowPrivate *data = arrow->data;
232 if (angle == data->angle)
233 return FALSE;
235 data->angle = angle;
236 adg_entity_invalidate((AdgEntity *) arrow);
238 return TRUE;