1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <libpspp/tower.h>
22 #include <libpspp/pool.h>
23 #include "psppire-axis-impl.h"
27 /* --- prototypes --- */
28 static void psppire_axis_impl_class_init (PsppireAxisImplClass *class);
29 static void psppire_axis_impl_init (PsppireAxisImpl *axis);
30 static void psppire_axis_impl_finalize (GObject *object);
33 /* --- variables --- */
34 static GObjectClass *parent_class = NULL;
39 struct tower_node pixel_node;
40 struct tower_node unit_node;
44 psppire_axis_impl_dump (const PsppireAxisImpl *a)
46 struct tower_node *n = tower_first (&a->unit_tower);
48 g_debug ("Axis %p", a);
51 const struct axis_node *an = tower_data (n, struct axis_node, unit_node);
52 const struct tower_node *pn = &an->pixel_node;
53 g_debug ("%ld units of height %g",
54 n->size, pn->size / (float) n->size);
56 n = tower_next (&a->unit_tower, n);
62 unit_at_pixel (const PsppireAxis *axis, glong pixel)
64 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
66 unsigned long int start;
71 g_return_val_if_fail (pixel >= 0, -1);
73 n = tower_lookup (&a->pixel_tower, pixel, &start);
74 an = tower_data (n, struct axis_node, pixel_node);
76 fraction = (pixel - start) / (gfloat) tower_node_get_size (&an->pixel_node);
78 return tower_node_get_level (&an->unit_node)
79 + fraction * tower_node_get_size (&an->unit_node);
84 unit_count (const PsppireAxis *axis)
86 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
88 return tower_height (&a->unit_tower);
92 /* Returns the pixel at the start of UNIT */
94 start_pixel (const PsppireAxis *axis, gint unit)
97 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
98 struct tower_node *n ;
101 unsigned long int start;
106 if ( unit >= unit_count (axis))
109 n = tower_lookup (&a->unit_tower, unit, &start);
111 an = tower_data (n, struct axis_node, unit_node);
113 fraction = (unit - start) / (gfloat) tower_node_get_size (&an->unit_node);
115 return tower_node_get_level (&an->pixel_node) +
116 nearbyintf (fraction * tower_node_get_size (&an->pixel_node));
121 unit_size (const PsppireAxis *axis, gint unit)
123 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
124 struct tower_node *n ;
125 struct axis_node *an;
127 unsigned long int start;
132 if ( unit >= unit_count (axis))
135 n = tower_lookup (&a->unit_tower, unit, &start);
137 an = tower_data (n, struct axis_node, unit_node);
139 return nearbyintf (tower_node_get_size (&an->pixel_node)
140 / (float) tower_node_get_size (&an->unit_node));
145 total_size (const PsppireAxis *axis)
147 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
149 return tower_height (&a->pixel_tower);
153 static void resize (PsppireAxis *axis, gint posn, glong size);
158 psppire_impl_iface_init (PsppireAxisIface *iface)
160 iface->unit_size = unit_size;
161 iface->unit_count = unit_count;
162 iface->start_pixel = start_pixel;
163 iface->unit_at_pixel = unit_at_pixel;
164 iface->total_size = total_size;
165 iface->resize = resize;
168 /* --- functions --- */
170 * psppire_axis_impl_get_type:
171 * @returns: the type ID for accelerator groups.
174 psppire_axis_impl_get_type (void)
176 static GType object_type = 0;
180 static const GTypeInfo object_info = {
181 sizeof (PsppireAxisImplClass),
182 (GBaseInitFunc) NULL,
183 (GBaseFinalizeFunc) NULL,
184 (GClassInitFunc) psppire_axis_impl_class_init,
185 NULL, /* class_finalize */
186 NULL, /* class_data */
187 sizeof (PsppireAxisImpl),
189 (GInstanceInitFunc) psppire_axis_impl_init,
192 static const GInterfaceInfo interface_info =
194 (GInterfaceInitFunc) psppire_impl_iface_init,
199 object_type = g_type_register_static (G_TYPE_PSPPIRE_AXIS,
203 g_type_add_interface_static (object_type,
204 PSPPIRE_TYPE_AXIS_IFACE,
212 psppire_axis_impl_class_init (PsppireAxisImplClass *class)
214 GObjectClass *object_class = G_OBJECT_CLASS (class);
215 parent_class = g_type_class_peek_parent (class);
217 object_class->finalize = psppire_axis_impl_finalize;
222 psppire_axis_impl_init (PsppireAxisImpl *axis)
224 tower_init (&axis->pixel_tower);
225 tower_init (&axis->unit_tower);
227 axis->pool = pool_create ();
232 psppire_axis_impl_finalize (GObject *object)
234 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (object);
235 pool_destroy (a->pool);
237 G_OBJECT_CLASS (parent_class)->finalize (object);
241 * psppire_axis_impl_new:
242 * @returns: a new #PsppireAxisImpl object
244 * Creates a new #PsppireAxisImpl.
247 psppire_axis_impl_new (void)
249 return g_object_new (G_TYPE_PSPPIRE_AXIS_IMPL, NULL);
256 psppire_axis_impl_append (PsppireAxisImpl *a, gint size)
258 psppire_axis_impl_append_n (a, 1, size);
263 psppire_axis_impl_append_n (PsppireAxisImpl *a, gint n_units, gint size)
265 struct axis_node *node;
270 node = pool_malloc (a->pool, sizeof *node);
272 tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
273 tower_insert (&a->pixel_tower, size * n_units, &node->pixel_node, NULL);
277 /* Split the node of both towers at POSN */
279 split (PsppireAxisImpl *a, gint posn)
281 unsigned long int existing_unit_size;
282 unsigned long int existing_pixel_size;
283 unsigned long int start;
285 struct axis_node *new_node ;
286 struct tower_node *n;
287 struct axis_node *existing_node;
289 g_return_if_fail (posn <= tower_height (&a->unit_tower));
291 /* Nothing needs to be done */
292 if ( posn == 0 || posn == tower_height (&a->unit_tower))
295 n = tower_lookup (&a->unit_tower, posn, &start);
297 existing_node = tower_data (n, struct axis_node, unit_node);
299 /* Nothing needs to be done, if the range element is already split here */
300 if ( posn - start == 0)
303 existing_unit_size = tower_node_get_size (&existing_node->unit_node);
304 existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
306 fraction = (posn - start) / (gfloat) existing_unit_size;
308 new_node = pool_malloc (a->pool, sizeof (*new_node));
310 tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
312 tower_resize (&a->pixel_tower, &existing_node->pixel_node,
313 nearbyintf (fraction * existing_pixel_size));
315 tower_insert (&a->unit_tower,
316 existing_unit_size - (posn - start),
317 &new_node->unit_node,
318 tower_next (&a->unit_tower, &existing_node->unit_node));
321 tower_insert (&a->pixel_tower,
322 nearbyintf (existing_pixel_size * (1 - fraction)),
323 &new_node->pixel_node,
324 tower_next (&a->pixel_tower, &existing_node->pixel_node));
328 /* Insert a new unit of size SIZE before POSN */
330 psppire_axis_impl_insert (PsppireAxisImpl *a, gint posn, gint size)
332 struct axis_node *before = NULL;
333 struct axis_node *new_node;
335 g_return_if_fail ( posn >= 0);
336 g_return_if_fail ( posn <= tower_height (&a->unit_tower));
338 if ( posn < tower_height (&a->unit_tower))
340 unsigned long int start = 0;
341 struct tower_node *n;
345 n = tower_lookup (&a->unit_tower, posn, &start);
346 g_assert (posn == start);
348 before = tower_data (n, struct axis_node, unit_node);
351 new_node = pool_malloc (a->pool, sizeof (*new_node));
353 tower_insert (&a->unit_tower,
355 &new_node->unit_node,
356 before ? &before->unit_node : NULL);
358 tower_insert (&a->pixel_tower,
360 &new_node->pixel_node,
361 before ? &before->pixel_node : NULL);
365 /* Make the element at POSN singular.
366 Return a pointer to the node for this element */
367 static struct axis_node *
368 make_single (PsppireAxisImpl *a, gint posn)
370 unsigned long int start;
371 struct tower_node *n;
373 g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
375 n = tower_lookup (&a->unit_tower, posn, &start);
377 if ( 1 != tower_node_get_size (n))
380 n = tower_lookup (&a->unit_tower, posn, &start);
382 if ( 1 != tower_node_get_size (n))
385 n = tower_lookup (&a->unit_tower, posn, &start);
389 g_assert (1 == tower_node_get_size (n));
391 return tower_data (n, struct axis_node, unit_node);
396 resize (PsppireAxis *axis, gint posn, glong size)
398 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
400 struct axis_node *an;
401 g_return_if_fail (posn >= 0);
402 g_return_if_fail (size > 0);
404 /* Silently ignore this request if the position is greater than the number of
406 if (posn >= tower_height (&a->unit_tower))
409 an = make_single (a, posn);
411 tower_resize (&a->pixel_tower, &an->pixel_node, size);
416 psppire_axis_impl_resize (PsppireAxisImpl *a, gint posn, gint size)
418 resize (PSPPIRE_AXIS (a), posn, size);
425 psppire_axis_impl_clear (PsppireAxisImpl *a)
427 pool_destroy (a->pool);
428 a->pool = pool_create ();
430 tower_init (&a->pixel_tower);
431 tower_init (&a->unit_tower);
437 psppire_axis_impl_delete (PsppireAxisImpl *a, gint first, gint n_units)
439 gint units_to_delete = n_units;
440 unsigned long int start;
441 struct tower_node *unit_node ;
442 g_return_if_fail (first + n_units <= tower_height (&a->unit_tower));
445 split (a, first + n_units);
447 unit_node = tower_lookup (&a->unit_tower, first, &start);
448 g_assert (start == first);
450 while (units_to_delete > 0)
452 struct tower_node *next_unit_node;
453 struct axis_node *an = tower_data (unit_node,
454 struct axis_node, unit_node);
456 g_assert (unit_node == &an->unit_node);
457 g_assert (unit_node->size <= n_units);
459 units_to_delete -= unit_node->size;
461 next_unit_node = tower_next (&a->unit_tower, unit_node);
463 tower_delete (&a->unit_tower, unit_node);
464 tower_delete (&a->pixel_tower, &an->pixel_node);
466 pool_free (a->pool, an);
468 unit_node = next_unit_node;