1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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 * @Section_Id:AdgPoint
25 * @short_description: A struct holding x, y coordinates
26 * (either named or explicit)
28 * AdgPoint is an opaque structure that manages 2D coordinates,
29 * either set explicitely throught adg_point_set_pair() and
30 * adg_point_set_pair_explicit() or taken from a model with
31 * adg_point_set_pair_from_model(). It can be thought as an
32 * #AdgPair on steroid, because it adds named pair support to
33 * a simple pair, enabling coordinates depending on #AdgModel.
41 * This is an opaque struct: all its fields are privates.
47 #include "adg-internal.h"
48 #include "adg-model.h"
51 #include "adg-point.h"
63 adg_point_get_type(void)
67 if (G_UNLIKELY(type
== 0))
68 type
= g_boxed_type_register_static("AdgPoint",
69 (GBoxedCopyFunc
) adg_point_dup
,
70 (GBoxedFreeFunc
) adg_point_destroy
);
78 * Creates a new empty #AdgPoint. The returned pointer
79 * should be freed with adg_point_destroy() when no longer needed.
81 * Returns: a newly created #AdgPoint
88 return g_new0(AdgPoint
, 1);
95 * Duplicates @src. This operation also adds a new reference
96 * to the internal model if @src is linked to a named pair.
98 * The returned value should be freed with adg_point_destroy()
99 * when no longer needed.
101 * Returns: the duplicated #AdgPoint struct or %NULL on errors
106 adg_point_dup(const AdgPoint
*src
)
110 g_return_val_if_fail(src
!= NULL
, NULL
);
113 g_object_ref(src
->model
);
115 point
= g_memdup(src
, sizeof(AdgPoint
));
116 point
->name
= g_strdup(src
->name
);
123 * @point: an #AdgPoint
125 * Destroys the @point instance, unreferencing the internal model if
126 * @point is linked to a named pair.
131 adg_point_destroy(AdgPoint
*point
)
133 g_return_if_fail(point
!= NULL
);
135 adg_point_unset(point
);
141 * @point: an #AdgPoint
142 * @src: the source point to copy
144 * Copies @src into @point. If the old content of @point was linked
145 * to the named pair of a model, the reference to that model is
146 * dropped. Similary, if @src is a named pair, a new reference to
147 * the new model is added.
152 adg_point_copy(AdgPoint
*point
, const AdgPoint
*src
)
154 g_return_if_fail(point
!= NULL
);
155 g_return_if_fail(src
!= NULL
);
157 if (src
->model
!= NULL
)
158 g_object_ref(src
->model
);
160 if (point
->model
!= NULL
)
161 g_object_unref(point
->model
);
163 if (point
->name
!= NULL
)
166 memcpy(point
, src
, sizeof(AdgPoint
));
167 point
->name
= g_strdup(src
->name
);
171 * adg_point_set_pair:
172 * @point: an #AdgPoint
173 * @pair: the #AdgPair to use
175 * Sets an explicit pair in @point by using the given @pair. If
176 * @point was linked to a named pair in a model, this link is
177 * dropped before setting the pair.
182 adg_point_set_pair(AdgPoint
*point
, const AdgPair
*pair
)
184 g_return_if_fail(point
!= NULL
);
185 g_return_if_fail(pair
!= NULL
);
187 adg_point_set_pair_explicit(point
, pair
->x
, pair
->y
);
191 * adg_point_set_pair_explicit:
192 * @point: an #AdgPoint
193 * @x: the x coordinate of the point
194 * @y: the y coordinate of the point
196 * Works in the same way of adg_point_set_pair() but accept direct numbers
197 * instead of an #AdgPair structure.
202 adg_point_set_pair_explicit(AdgPoint
*point
, gdouble x
, gdouble y
)
204 g_return_if_fail(point
!= NULL
);
206 adg_point_unset(point
);
209 point
->is_uptodate
= TRUE
;
213 * adg_point_set_pair_from_model:
214 * @point: an #AdgPoint
215 * @model: the #AdgModel
216 * @name: the id of a named pair in @model
218 * Links the @name named pair of @model to @point, so any subsequent
219 * call to adg_point_get_pair() will return the named pair value.
220 * A new reference is added to @model while the previous model (if any)
226 adg_point_set_pair_from_model(AdgPoint
*point
,
227 AdgModel
*model
, const gchar
*name
)
229 g_return_if_fail(point
!= NULL
);
230 g_return_if_fail(ADG_IS_MODEL(model
));
231 g_return_if_fail(name
!= NULL
);
233 /* Return if the new named pair is the same of the old one */
234 if (model
== point
->model
&& strcmp(point
->name
, name
) == 0)
240 /* Remove the old named pair */
241 g_object_unref(point
->model
);
245 /* Set the new named pair */
246 point
->is_uptodate
= FALSE
;
247 point
->model
= model
;
248 point
->name
= g_strdup(name
);
253 * @point: a pointer to an #AdgPoint
255 * Unsets @point by resetting the internal "is_uptodate" flag.
256 * This also means @point is unlinked from the model if it is
257 * a named pair. In any cases, after this call the content of
258 * @point is undefined, that is calling adg_point_get_pair()
264 adg_point_unset(AdgPoint
*point
)
266 g_return_if_fail(point
!= NULL
);
269 /* Remove the old named pair */
270 g_object_unref(point
->model
);
274 point
->is_uptodate
= FALSE
;
280 * adg_point_get_pair:
281 * @point: an #AdgPoint
283 * #AdgPoint is an evolution of the pair concept but internally the
284 * relevant data is still stored in an #AdgPair struct. This function
285 * gets the pointer to this struct, updating the value prior to
286 * return it if needed (that is, if @point is linked to a not up
287 * to date named pair).
289 * Returns: the pair of @point or %NULL if the named pair does not exist
294 adg_point_get_pair(AdgPoint
*point
)
296 g_return_val_if_fail(point
!= NULL
, NULL
);
298 if (!point
->is_uptodate
) {
301 if (point
->model
== NULL
) {
302 /* A point with explicit coordinates not up to date
303 * is an unexpected condition */
304 g_warning(_("%s: trying to get a pair from an undefined point"),
309 pair
= adg_model_get_named_pair(point
->model
, point
->name
);
311 /* "Named pair not found" condition: return NULL without warnings */
315 cpml_pair_copy(&point
->pair
, pair
);
316 point
->is_uptodate
= TRUE
;
319 return (AdgPair
*) point
;
323 * adg_point_get_model:
324 * @point: an #AdgPoint
326 * Gets the source model of the named pair bound to @point, or
327 * returns %NULL if @point is an explicit pair. The returned
328 * value is owned by @point.
330 * Returns: (transfer none): an #AdgModel or %NULL.
335 adg_point_get_model(AdgPoint
*point
)
337 g_return_val_if_fail(point
!= NULL
, NULL
);
342 * adg_point_get_name:
343 * @point: an #AdgPoint
345 * Gets the name of the named pair bound to @point, or returns
346 * %NULL if @point is an explicit pair. The returned value is
347 * owned by @point and should not be modified or freed.
349 * Returns: the name of the named pair or %NULL
354 adg_point_get_name(AdgPoint
*point
)
356 g_return_val_if_fail(point
!= NULL
, NULL
);
361 * adg_point_invalidate:
362 * @point: an #AdgPoint
364 * Invalidates @point, forcing a refresh of its internal #AdgPair if
365 * the point is linked to a named pair. If @point is explicitely set,
366 * this function has no effect.
371 adg_point_invalidate(AdgPoint
*point
)
373 g_return_if_fail(point
!= NULL
);
376 point
->is_uptodate
= FALSE
;
381 * @point1: the first point to compare
382 * @point2: the second point to compare
384 * Compares @point1 and @point2 and returns %TRUE if the points are
385 * equals. The comparison is made by matching also where the points
386 * are bound. If you want to compare only the coordinates, use
387 * cpml_pair_equal() directly on their pairs:
390 * cpml_pair_equal(adg_point_get_pair(point1), adg_point_get_pair(point2));
393 * %NULL values are handled gracefully.
395 * Returns: %TRUE if @point1 is equal to @point2, %FALSE otherwise
400 adg_point_equal(const AdgPoint
*point1
, const AdgPoint
*point2
)
402 if (point1
== point2
)
405 if (point1
== NULL
|| point2
== NULL
)
408 /* Check if the points are not bound to the same model
409 * or if only one of the two points is explicit */
410 if (point1
->model
!= point2
->model
)
413 /* Handle points bound to named pairs */
415 return g_strcmp0(point1
->name
, point2
->name
) == 0;
417 /* Handle points with explicit coordinates */
418 return cpml_pair_equal(&point1
->pair
, &point2
->pair
);