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.
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.
32 * All fields are private and should not be used directly.
33 * Use its public methods instead.
37 #include "adg-internal.h"
38 #include "adg-model.h"
39 #include "adg-trail.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
)
55 static void _adg_get_property (GObject
*object
,
59 static void _adg_set_property (GObject
*object
,
63 static void _adg_arrange (AdgEntity
*entity
);
64 static void _adg_render (AdgEntity
*entity
,
66 static AdgModel
* _adg_create_model (AdgMarker
*marker
);
70 adg_arrow_class_init(AdgArrowClass
*klass
)
72 GObjectClass
*gobject_class
;
73 AdgEntityClass
*entity_class
;
74 AdgMarkerClass
*marker_class
;
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",
93 P_("The opening angle of the arrow"),
94 -G_PI
, G_PI
, G_PI
/ 6,
96 g_object_class_install_property(gobject_class
, PROP_ANGLE
, param
);
100 adg_arrow_init(AdgArrow
*arrow
)
102 AdgArrowPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(arrow
,
106 data
->angle
= G_PI
/6;
112 _adg_get_property(GObject
*object
, guint prop_id
,
113 GValue
*value
, GParamSpec
*pspec
)
115 AdgArrowPrivate
*data
= ((AdgArrow
*) object
)->data
;
119 g_value_set_double(value
, data
->angle
);
122 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
128 _adg_set_property(GObject
*object
, guint prop_id
,
129 const GValue
*value
, GParamSpec
*pspec
)
131 AdgArrowPrivate
*data
= ((AdgArrow
*) object
)->data
;
135 data
->angle
= cpml_angle(g_value_get_double(value
));
138 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
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
157 return g_object_new(ADG_TYPE_ARROW
,
158 "local-method", ADG_MIX_PARENT
,
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
175 adg_arrow_new_with_trail(AdgTrail
*trail
, gdouble pos
)
177 return g_object_new(ADG_TYPE_ARROW
,
178 "local-method", ADG_MIX_PARENT
,
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.
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
209 adg_arrow_get_angle(AdgArrow
*arrow
)
211 AdgArrowPrivate
*data
;
213 g_return_val_if_fail(ADG_IS_ARROW(arrow
), 0);
222 _adg_arrange(AdgEntity
*entity
)
228 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
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
) {
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
);
248 _adg_create_model(AdgMarker
*marker
)
250 AdgArrowPrivate
*data
;
254 data
= ((AdgArrow
*) marker
)->data
;
255 path
= adg_path_new();
256 cpml_vector_from_angle(&p1
, data
->angle
/ 2);
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
;