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,
200 object_type = g_type_register_static (G_TYPE_PSPPIRE_AXIS,
205 g_type_add_interface_static (object_type,
206 PSPPIRE_TYPE_AXIS_IFACE,
214 psppire_axis_impl_class_init (PsppireAxisImplClass *class)
216 GObjectClass *object_class = G_OBJECT_CLASS (class);
217 parent_class = g_type_class_peek_parent (class);
219 object_class->finalize = psppire_axis_impl_finalize;
224 psppire_axis_impl_init (PsppireAxisImpl *axis)
226 tower_init (&axis->pixel_tower);
227 tower_init (&axis->unit_tower);
229 axis->pool = pool_create ();
234 psppire_axis_impl_finalize (GObject *object)
236 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (object);
237 pool_destroy (a->pool);
239 G_OBJECT_CLASS (parent_class)->finalize (object);
243 * psppire_axis_impl_new:
244 * @returns: a new #PsppireAxisImpl object
246 * Creates a new #PsppireAxisImpl.
249 psppire_axis_impl_new (void)
251 return g_object_new (G_TYPE_PSPPIRE_AXIS_IMPL, NULL);
258 psppire_axis_impl_append (PsppireAxisImpl *a, gint size)
260 psppire_axis_impl_append_n (a, 1, size);
265 psppire_axis_impl_append_n (PsppireAxisImpl *a, gint n_units, gint size)
267 struct axis_node *node;
269 g_return_if_fail (n_units > 0);
271 node = pool_alloc (a->pool, sizeof *node);
273 tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
274 tower_insert (&a->pixel_tower, size * n_units, &node->pixel_node, NULL);
278 /* Split the node of both towers at POSN */
280 split (PsppireAxisImpl *a, gint posn)
282 unsigned long int existing_unit_size;
283 unsigned long int existing_pixel_size;
284 unsigned long int start;
286 struct axis_node *new_node ;
287 struct tower_node *n;
288 struct axis_node *existing_node;
290 g_return_if_fail (posn <= tower_height (&a->unit_tower));
292 /* Nothing needs to be done */
293 if ( posn == 0 || posn == tower_height (&a->unit_tower))
296 n = tower_lookup (&a->unit_tower, posn, &start);
298 existing_node = tower_data (n, struct axis_node, unit_node);
300 /* Nothing needs to be done, if the range element is already split here */
301 if ( posn - start == 0)
304 existing_unit_size = tower_node_get_size (&existing_node->unit_node);
305 existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
307 fraction = (posn - start) / (gfloat) existing_unit_size;
309 new_node = pool_alloc (a->pool, sizeof (*new_node));
311 tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
313 tower_resize (&a->pixel_tower, &existing_node->pixel_node,
314 nearbyintf (fraction * existing_pixel_size));
316 tower_insert (&a->unit_tower,
317 existing_unit_size - (posn - start),
318 &new_node->unit_node,
319 tower_next (&a->unit_tower, &existing_node->unit_node));
322 tower_insert (&a->pixel_tower,
323 nearbyintf (existing_pixel_size * (1 - fraction)),
324 &new_node->pixel_node,
325 tower_next (&a->pixel_tower, &existing_node->pixel_node));
329 /* Insert a new unit of size SIZE before POSN */
331 psppire_axis_impl_insert (PsppireAxisImpl *a, gint posn, gint size)
333 struct axis_node *before = NULL;
334 struct axis_node *new_node;
336 g_return_if_fail ( posn < tower_height (&a->unit_tower));
337 g_return_if_fail ( posn >= 0);
339 new_node = pool_alloc (a->pool, sizeof (*new_node));
343 unsigned long int start = 0;
344 struct tower_node *n;
348 n = tower_lookup (&a->unit_tower, posn, &start);
349 g_assert (posn == start);
351 before = tower_data (n, struct axis_node, unit_node);
354 tower_insert (&a->unit_tower,
356 &new_node->unit_node,
357 before ? &before->unit_node : NULL);
360 tower_insert (&a->pixel_tower,
362 &new_node->pixel_node,
363 before ? &before->pixel_node : NULL);
367 /* Make the element at POSN singular.
368 Return a pointer to the node for this element */
369 static struct axis_node *
370 make_single (PsppireAxisImpl *a, gint posn)
372 unsigned long int start;
373 struct tower_node *n;
375 g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
377 n = tower_lookup (&a->unit_tower, posn, &start);
379 if ( 1 != tower_node_get_size (n))
382 n = tower_lookup (&a->unit_tower, posn, &start);
384 if ( 1 != tower_node_get_size (n))
387 n = tower_lookup (&a->unit_tower, posn, &start);
391 g_assert (1 == tower_node_get_size (n));
394 return tower_data (n, struct axis_node, unit_node);
398 resize (PsppireAxis *axis, gint posn, glong size)
400 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
402 struct axis_node *an;
403 g_return_if_fail (posn >= 0);
405 /* Silently ignore this request if the position is greater than the number of
407 if (posn >= tower_height (&a->unit_tower))
410 an = make_single (a, posn);
412 tower_resize (&a->pixel_tower, &an->pixel_node, size);
417 psppire_axis_impl_resize (PsppireAxisImpl *a, gint posn, gint size)
419 resize (PSPPIRE_AXIS (a), posn, size);
426 psppire_axis_impl_clear (PsppireAxisImpl *a)
428 pool_destroy (a->pool);
429 a->pool = pool_create ();
431 tower_init (&a->pixel_tower);
432 tower_init (&a->unit_tower);
438 psppire_axis_impl_delete (PsppireAxisImpl *a, gint first, gint n_units)
440 gint units_to_delete = n_units;
441 unsigned long int start;
442 g_return_if_fail (first + n_units < tower_height (&a->unit_tower));
445 split (a, first + n_units);
447 struct tower_node *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);
467 unit_node = next_unit_node;