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 unit_at_pixel (const PsppireAxis *axis, glong pixel)
46 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
48 unsigned long int start;
53 g_return_val_if_fail (pixel >= 0, -1);
55 n = tower_lookup (&a->pixel_tower, pixel, &start);
56 an = tower_data (n, struct axis_node, pixel_node);
58 fraction = (pixel - start) / (gfloat) tower_node_get_size (&an->pixel_node);
60 return tower_node_get_level (&an->unit_node)
61 + fraction * tower_node_get_size (&an->unit_node);
66 unit_count (const PsppireAxis *axis)
68 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
70 return tower_height (&a->unit_tower);
74 /* Returns the pixel at the start of UNIT */
76 start_pixel (const PsppireAxis *axis, gint unit)
79 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
80 struct tower_node *n ;
83 unsigned long int start;
88 if ( unit >= unit_count (axis))
91 n = tower_lookup (&a->unit_tower, unit, &start);
93 an = tower_data (n, struct axis_node, unit_node);
95 fraction = (unit - start) / (gfloat) tower_node_get_size (&an->unit_node);
97 return tower_node_get_level (&an->pixel_node) +
98 nearbyintf (fraction * tower_node_get_size (&an->pixel_node));
103 unit_size (const PsppireAxis *axis, gint unit)
105 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
106 struct tower_node *n ;
107 struct axis_node *an;
109 unsigned long int start;
114 if ( unit >= unit_count (axis))
117 n = tower_lookup (&a->unit_tower, unit, &start);
119 an = tower_data (n, struct axis_node, unit_node);
121 return nearbyintf (tower_node_get_size (&an->pixel_node)
122 / (float) tower_node_get_size (&an->unit_node));
127 total_size (const PsppireAxis *axis)
129 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
131 return tower_height (&a->pixel_tower);
138 psppire_impl_iface_init (PsppireAxisIface *iface)
140 iface->unit_size = unit_size;
141 iface->unit_count = unit_count;
142 iface->start_pixel = start_pixel;
143 iface->unit_at_pixel = unit_at_pixel;
144 iface->total_size = total_size;
147 /* --- functions --- */
149 * psppire_axis_impl_get_type:
150 * @returns: the type ID for accelerator groups.
153 psppire_axis_impl_get_type (void)
155 static GType object_type = 0;
159 static const GTypeInfo object_info = {
160 sizeof (PsppireAxisImplClass),
161 (GBaseInitFunc) NULL,
162 (GBaseFinalizeFunc) NULL,
163 (GClassInitFunc) psppire_axis_impl_class_init,
164 NULL, /* class_finalize */
165 NULL, /* class_data */
166 sizeof (PsppireAxisImpl),
168 (GInstanceInitFunc) psppire_axis_impl_init,
171 static const GInterfaceInfo interface_info =
173 (GInterfaceInitFunc) psppire_impl_iface_init,
179 object_type = g_type_register_static (G_TYPE_PSPPIRE_AXIS,
184 g_type_add_interface_static (object_type,
185 PSPPIRE_TYPE_AXIS_IFACE,
193 psppire_axis_impl_class_init (PsppireAxisImplClass *class)
195 GObjectClass *object_class = G_OBJECT_CLASS (class);
196 parent_class = g_type_class_peek_parent (class);
198 object_class->finalize = psppire_axis_impl_finalize;
203 psppire_axis_impl_init (PsppireAxisImpl *axis)
205 tower_init (&axis->pixel_tower);
206 tower_init (&axis->unit_tower);
208 axis->pool = pool_create ();
213 psppire_axis_impl_finalize (GObject *object)
215 PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (object);
216 pool_destroy (a->pool);
218 G_OBJECT_CLASS (parent_class)->finalize (object);
222 * psppire_axis_impl_new:
223 * @returns: a new #PsppireAxisImpl object
225 * Creates a new #PsppireAxisImpl.
228 psppire_axis_impl_new (void)
230 return g_object_new (G_TYPE_PSPPIRE_AXIS_IMPL, NULL);
237 psppire_axis_impl_append (PsppireAxisImpl *a, gint size)
239 psppire_axis_impl_append_n (a, 1, size);
244 psppire_axis_impl_append_n (PsppireAxisImpl *a, gint n_units, gint size)
246 struct axis_node *node = pool_alloc (a->pool, sizeof *node);
249 tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
250 tower_insert (&a->pixel_tower, size * n_units, &node->pixel_node, NULL);
254 /* Split the node of both towers at POSN */
256 split (PsppireAxisImpl *a, gint posn)
258 unsigned long int existing_unit_size;
259 unsigned long int existing_pixel_size;
260 unsigned long int start;
262 struct axis_node *new_node ;
263 struct tower_node *n = tower_lookup (&a->unit_tower, posn, &start);
265 struct axis_node *existing_node =
266 tower_data (n, struct axis_node, unit_node);
268 /* Nothing needs to be done, if the range element is already split here */
269 if ( posn - start == 0)
272 existing_unit_size = tower_node_get_size (&existing_node->unit_node);
273 existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
275 fraction = (posn - start) / (gfloat) existing_unit_size;
277 new_node = pool_alloc (a->pool, sizeof (*new_node));
279 tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
281 tower_resize (&a->pixel_tower, &existing_node->pixel_node,
282 nearbyintf (fraction * existing_pixel_size));
284 tower_insert (&a->unit_tower,
285 existing_unit_size - (posn - start),
286 &new_node->unit_node,
287 tower_next (&a->unit_tower, &existing_node->unit_node));
290 tower_insert (&a->pixel_tower,
291 nearbyintf (existing_pixel_size * (1 - fraction)),
292 &new_node->pixel_node,
293 tower_next (&a->pixel_tower, &existing_node->pixel_node));
297 /* Insert a new unit of size SIZE before POSN */
299 psppire_axis_impl_insert (PsppireAxisImpl *a, gint posn, gint size)
301 struct tower_node *n;
302 unsigned long int start;
303 struct axis_node *before;
304 struct axis_node *new_node = pool_alloc (a->pool, sizeof (*new_node));
308 n = tower_lookup (&a->unit_tower, posn, &start);
309 g_assert (posn == start);
311 before = tower_data (n, struct axis_node, unit_node);
313 tower_insert (&a->unit_tower,
315 &new_node->unit_node,
319 tower_insert (&a->pixel_tower,
321 &new_node->pixel_node,
322 &before->pixel_node);
326 /* Make the element at POSN singular.
327 Return a pointer to the node for this element */
328 static struct axis_node *
329 make_single (PsppireAxisImpl *a, gint posn)
331 unsigned long int start;
332 struct tower_node *n;
333 n = tower_lookup (&a->unit_tower, posn, &start);
335 if ( 1 != tower_node_get_size (n))
338 n = tower_lookup (&a->unit_tower, posn, &start);
340 if ( 1 != tower_node_get_size (n))
343 n = tower_lookup (&a->unit_tower, posn, &start);
347 g_assert (1 == tower_node_get_size (n));
350 return tower_data (n, struct axis_node, unit_node);
354 psppire_axis_impl_resize (PsppireAxisImpl *a, gint posn, gint size)
356 struct axis_node *an = make_single (a, posn);
358 tower_resize (&a->pixel_tower, &an->pixel_node, size);
364 psppire_axis_impl_clear (PsppireAxisImpl *a)
366 pool_destroy (a->pool);
367 a->pool = pool_create ();
369 tower_init (&a->pixel_tower);
370 tower_init (&a->unit_tower);
376 psppire_axis_impl_delete (PsppireAxisImpl *a, gint first, gint n_cases)
379 g_warning ("%s FIXME: This is an inefficient implementation", __FUNCTION__);
381 for (i = first; i < first + n_cases; ++i)
383 struct axis_node *an = make_single (a, i);
385 tower_delete (&a->unit_tower, &an->unit_node);
386 tower_delete (&a->pixel_tower, &an->pixel_node);