docs: updated copyright to 2012
[adg.git] / src / adg / adg-point.c
blob991a81cfcfea3e3ce237b26dae2b72ef0262c020
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.
21 /**
22 * SECTION:adg-point
23 * @Section_Id:AdgPoint
24 * @title: 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.
35 * Since: 1.0
36 **/
38 /**
39 * AdgPoint:
41 * This is an opaque struct: all its fields are privates.
43 * Since: 1.0
44 **/
47 #include "adg-internal.h"
48 #include "adg-model.h"
49 #include <string.h>
51 #include "adg-point.h"
54 struct _AdgPoint {
55 AdgPair pair;
56 AdgModel *model;
57 gchar *name;
58 gboolean is_uptodate;
62 GType
63 adg_point_get_type(void)
65 static int type = 0;
67 if (G_UNLIKELY(type == 0))
68 type = g_boxed_type_register_static("AdgPoint",
69 (GBoxedCopyFunc) adg_point_dup,
70 (GBoxedFreeFunc) adg_point_destroy);
72 return type;
75 /**
76 * adg_point_new:
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
83 * Since: 1.0
84 **/
85 AdgPoint *
86 adg_point_new(void)
88 return g_new0(AdgPoint, 1);
91 /**
92 * adg_point_dup:
93 * @src: an #AdgPoint
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
103 * Since: 1.0
105 AdgPoint *
106 adg_point_dup(const AdgPoint *src)
108 AdgPoint *point;
110 g_return_val_if_fail(src != NULL, NULL);
112 if (src->model)
113 g_object_ref(src->model);
115 point = g_memdup(src, sizeof(AdgPoint));
116 point->name = g_strdup(src->name);
118 return point;
122 * adg_point_destroy:
123 * @point: an #AdgPoint
125 * Destroys the @point instance, unreferencing the internal model if
126 * @point is linked to a named pair.
128 * Since: 1.0
130 void
131 adg_point_destroy(AdgPoint *point)
133 g_return_if_fail(point != NULL);
135 adg_point_unset(point);
136 g_free(point);
140 * adg_point_copy:
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.
149 * Since: 1.0
151 void
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)
164 g_free(point->name);
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.
179 * Since: 1.0
181 void
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.
199 * Since: 1.0
201 void
202 adg_point_set_pair_explicit(AdgPoint *point, gdouble x, gdouble y)
204 g_return_if_fail(point != NULL);
206 adg_point_unset(point);
207 point->pair.x = x;
208 point->pair.y = y;
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)
221 * is unreferenced.
223 * Since: 1.0
225 void
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)
235 return;
237 g_object_ref(model);
239 if (point->model) {
240 /* Remove the old named pair */
241 g_object_unref(point->model);
242 g_free(point->name);
245 /* Set the new named pair */
246 point->is_uptodate = FALSE;
247 point->model = model;
248 point->name = g_strdup(name);
252 * adg_point_unset:
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()
259 * will return %NULL.
261 * Since: 1.0
263 void
264 adg_point_unset(AdgPoint *point)
266 g_return_if_fail(point != NULL);
268 if (point->model) {
269 /* Remove the old named pair */
270 g_object_unref(point->model);
271 g_free(point->name);
274 point->is_uptodate = FALSE;
275 point->model = NULL;
276 point->name = NULL;
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
291 * Since: 1.0
293 const AdgPair *
294 adg_point_get_pair(AdgPoint *point)
296 g_return_val_if_fail(point != NULL, NULL);
298 if (!point->is_uptodate) {
299 const AdgPair *pair;
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"),
305 G_STRLOC);
306 return NULL;
309 pair = adg_model_get_named_pair(point->model, point->name);
311 /* "Named pair not found" condition: return NULL without warnings */
312 if (pair == NULL)
313 return NULL;
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.
332 * Since: 1.0
334 AdgModel *
335 adg_point_get_model(AdgPoint *point)
337 g_return_val_if_fail(point != NULL, NULL);
338 return point->model;
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
351 * Since: 1.0
353 const gchar *
354 adg_point_get_name(AdgPoint *point)
356 g_return_val_if_fail(point != NULL, NULL);
357 return point->name;
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.
368 * Since: 1.0
370 void
371 adg_point_invalidate(AdgPoint *point)
373 g_return_if_fail(point != NULL);
375 if (point->model)
376 point->is_uptodate = FALSE;
380 * adg_point_equal:
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:
389 * |[
390 * cpml_pair_equal(adg_point_get_pair(point1), adg_point_get_pair(point2));
391 * ]|
393 * %NULL values are handled gracefully.
395 * Returns: %TRUE if @point1 is equal to @point2, %FALSE otherwise
397 * Since: 1.0
399 gboolean
400 adg_point_equal(const AdgPoint *point1, const AdgPoint *point2)
402 if (point1 == point2)
403 return TRUE;
405 if (point1 == NULL || point2 == NULL)
406 return FALSE;
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)
411 return FALSE;
413 /* Handle points bound to named pairs */
414 if (point1->model)
415 return g_strcmp0(point1->name, point2->name) == 0;
417 /* Handle points with explicit coordinates */
418 return cpml_pair_equal(&point1->pair, &point2->pair);