1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009 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 <ui/gui/psppire-marshal.h>
22 #include <libpspp/tower.h>
23 #include <libpspp/pool.h>
24 #include "psppire-axis.h"
26 #include <libpspp/misc.h>
36 static guint signals[n_signals] ;
38 /* --- prototypes --- */
39 static void psppire_axis_class_init (PsppireAxisClass *class);
40 static void psppire_axis_init (PsppireAxis *axis);
41 static void psppire_axis_finalize (GObject *object);
44 /* --- variables --- */
45 static GObjectClass *parent_class = NULL;
50 struct tower_node pixel_node;
51 struct tower_node unit_node;
55 psppire_axis_dump (const PsppireAxis *a)
57 struct tower_node *n = tower_first (&a->unit_tower);
59 g_debug ("Axis %p", a);
62 const struct axis_node *an = tower_data (n, struct axis_node, unit_node);
63 const struct tower_node *pn = &an->pixel_node;
64 g_debug ("%ld units of height %g",
65 n->size, pn->size / (gdouble) n->size);
67 n = tower_next (&a->unit_tower, n);
72 /* Return the unit covered by PIXEL */
74 psppire_axis_unit_at_pixel (const PsppireAxis *a, glong pixel)
76 unsigned long int start;
81 glong size = tower_height (&a->pixel_tower);
83 g_return_val_if_fail (pixel >= 0, -1);
87 gint n_items = tower_height (&a->unit_tower);
88 glong extra = pixel - size;
90 return n_items - 1 + DIV_RND_UP (extra, a->default_size);
94 n = tower_lookup (&a->pixel_tower, pixel, &start);
95 an = tower_data (n, struct axis_node, pixel_node);
97 fraction = (pixel - start) / (gdouble) tower_node_get_size (&an->pixel_node);
99 return tower_node_get_level (&an->unit_node)
100 + fraction * tower_node_get_size (&an->unit_node);
105 psppire_axis_unit_count (const PsppireAxis *a)
110 actual_size = tower_height (&a->pixel_tower);
112 if ( actual_size < a->min_extent )
113 padding = DIV_RND_UP (a->min_extent - actual_size, a->default_size);
115 return tower_height (&a->unit_tower) + padding;
119 /* Return the starting pixel of UNIT */
121 psppire_axis_start_pixel (const PsppireAxis *a, gint unit)
124 struct tower_node *n ;
125 struct axis_node *an;
127 unsigned long int start;
129 gint the_count, size ;
131 the_count = tower_height (&a->unit_tower);
132 size = tower_height (&a->pixel_tower);
134 if ( unit >= the_count)
136 return size + (unit - the_count) * a->default_size;
142 if ( unit >= tower_height (&a->unit_tower))
145 n = tower_lookup (&a->unit_tower, unit, &start);
147 an = tower_data (n, struct axis_node, unit_node);
149 fraction = (unit - start) / (gdouble) tower_node_get_size (&an->unit_node);
151 return tower_node_get_level (&an->pixel_node) +
152 nearbyint (fraction * tower_node_get_size (&an->pixel_node));
156 psppire_axis_unit_size (const PsppireAxis *axis, gint unit)
158 struct tower_node *n ;
159 struct axis_node *an;
161 unsigned long int start;
163 if (unit >= tower_height (&axis->unit_tower))
164 return axis->default_size;
169 if ( unit >= tower_height (&axis->unit_tower))
172 n = tower_lookup (&axis->unit_tower, unit, &start);
174 an = tower_data (n, struct axis_node, unit_node);
176 return nearbyint (tower_node_get_size (&an->pixel_node)
177 / (gdouble) tower_node_get_size (&an->unit_node));
182 /* --- functions --- */
184 * psppire_axis_get_type:
185 * @returns: the type ID for accelerator groups.
188 psppire_axis_get_type (void)
190 static GType object_type = 0;
194 static const GTypeInfo object_info = {
195 sizeof (PsppireAxisClass),
196 (GBaseInitFunc) NULL,
197 (GBaseFinalizeFunc) NULL,
198 (GClassInitFunc) psppire_axis_class_init,
199 NULL, /* class_finalize */
200 NULL, /* class_data */
201 sizeof (PsppireAxis),
203 (GInstanceInitFunc) psppire_axis_init,
206 object_type = g_type_register_static (G_TYPE_OBJECT,
224 psppire_axis_get_property (GObject *object,
229 PsppireAxis *axis = PSPPIRE_AXIS (object);
233 case PROP_MIN_EXTENT:
234 g_value_set_long (value, axis->min_extent);
236 case PROP_DEFAULT_SIZE:
237 g_value_set_int (value, axis->default_size);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 psppire_axis_set_property (GObject *object,
252 PsppireAxis *axis = PSPPIRE_AXIS (object);
256 case PROP_MIN_EXTENT:
257 axis->min_extent = g_value_get_long (value);
259 case PROP_DEFAULT_SIZE:
260 axis->default_size = g_value_get_int (value);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270 psppire_axis_class_init (PsppireAxisClass *class)
272 GObjectClass *object_class = G_OBJECT_CLASS (class);
275 GParamSpec *min_extent_spec;
276 GParamSpec *default_size_spec;
278 parent_class = g_type_class_peek_parent (class);
280 object_class->set_property = psppire_axis_set_property;
281 object_class->get_property = psppire_axis_get_property;
285 g_param_spec_long ("minimum-extent",
287 "The smallest extent to which the axis will provide units (typically set to the height/width of the associated widget).",
290 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
292 g_object_class_install_property (object_class,
298 g_param_spec_int ("default-size",
300 "The size given to units which haven't been explicity inserted",
303 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
306 g_object_class_install_property (object_class,
311 signals[RESIZE_UNIT] =
312 g_signal_new ("resize-unit",
313 G_TYPE_FROM_CLASS (object_class),
317 psppire_marshal_VOID__INT_LONG,
325 object_class->finalize = psppire_axis_finalize;
330 psppire_axis_init (PsppireAxis *axis)
332 tower_init (&axis->pixel_tower);
333 tower_init (&axis->unit_tower);
335 axis->pool = pool_create ();
340 psppire_axis_finalize (GObject *object)
342 PsppireAxis *a = PSPPIRE_AXIS (object);
343 pool_destroy (a->pool);
345 G_OBJECT_CLASS (parent_class)->finalize (object);
350 * @returns: a new #PsppireAxis object
352 * Creates a new #PsppireAxis.
355 psppire_axis_new (void)
357 return g_object_new (G_TYPE_PSPPIRE_AXIS, NULL);
364 psppire_axis_append (PsppireAxis *a, gint size)
366 psppire_axis_append_n (a, 1, size);
371 psppire_axis_append_n (PsppireAxis *a, gint n_units, gint size)
373 struct axis_node *node;
378 node = pool_malloc (a->pool, sizeof *node);
380 tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
381 tower_insert (&a->pixel_tower, size * n_units, &node->pixel_node, NULL);
385 /* Split the node of both towers at POSN */
387 split (PsppireAxis *a, gint posn)
389 unsigned long int existing_unit_size;
390 unsigned long int existing_pixel_size;
391 unsigned long int start;
393 struct axis_node *new_node ;
394 struct tower_node *n;
395 struct axis_node *existing_node;
397 g_return_if_fail (posn <= tower_height (&a->unit_tower));
399 /* Nothing needs to be done */
400 if ( posn == 0 || posn == tower_height (&a->unit_tower))
403 n = tower_lookup (&a->unit_tower, posn, &start);
405 existing_node = tower_data (n, struct axis_node, unit_node);
407 /* Nothing needs to be done, if the range element is already split here */
408 if ( posn - start == 0)
411 existing_unit_size = tower_node_get_size (&existing_node->unit_node);
412 existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
414 fraction = (posn - start) / (gdouble) existing_unit_size;
416 new_node = pool_malloc (a->pool, sizeof (*new_node));
418 tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
420 tower_resize (&a->pixel_tower, &existing_node->pixel_node,
421 nearbyintf (fraction * existing_pixel_size));
423 tower_insert (&a->unit_tower,
424 existing_unit_size - (posn - start),
425 &new_node->unit_node,
426 tower_next (&a->unit_tower, &existing_node->unit_node));
429 tower_insert (&a->pixel_tower,
430 nearbyintf (existing_pixel_size * (1 - fraction)),
431 &new_node->pixel_node,
432 tower_next (&a->pixel_tower, &existing_node->pixel_node));
436 /* Insert a new unit of size SIZE before POSN */
438 psppire_axis_insert (PsppireAxis *a, gint posn, gint size)
440 struct axis_node *before = NULL;
441 struct axis_node *new_node;
443 g_return_if_fail ( posn >= 0);
444 g_return_if_fail ( posn <= tower_height (&a->unit_tower));
446 if ( posn < tower_height (&a->unit_tower))
448 unsigned long int start = 0;
449 struct tower_node *n;
453 n = tower_lookup (&a->unit_tower, posn, &start);
454 g_assert (posn == start);
456 before = tower_data (n, struct axis_node, unit_node);
459 new_node = pool_malloc (a->pool, sizeof (*new_node));
461 tower_insert (&a->unit_tower,
463 &new_node->unit_node,
464 before ? &before->unit_node : NULL);
466 tower_insert (&a->pixel_tower,
468 &new_node->pixel_node,
469 before ? &before->pixel_node : NULL);
473 /* Make the element at POSN singular.
474 Return a pointer to the node for this element */
475 static struct axis_node *
476 make_single (PsppireAxis *a, gint posn)
478 unsigned long int start;
479 struct tower_node *n;
481 g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
483 n = tower_lookup (&a->unit_tower, posn, &start);
485 if ( 1 != tower_node_get_size (n))
488 n = tower_lookup (&a->unit_tower, posn, &start);
490 if ( 1 != tower_node_get_size (n))
493 n = tower_lookup (&a->unit_tower, posn, &start);
497 g_assert (1 == tower_node_get_size (n));
499 return tower_data (n, struct axis_node, unit_node);
504 psppire_axis_resize (PsppireAxis *axis, gint posn, glong size)
506 struct axis_node *an;
507 g_return_if_fail (posn >= 0);
508 g_return_if_fail (size > 0);
510 /* Silently ignore this request if the position is greater than the number of
512 if (posn >= tower_height (&axis->unit_tower))
515 an = make_single (axis, posn);
517 tower_resize (&axis->pixel_tower, &an->pixel_node, size);
519 g_signal_emit (axis, signals[RESIZE_UNIT], 0, posn, size);
528 psppire_axis_clear (PsppireAxis *a)
530 pool_destroy (a->pool);
531 a->pool = pool_create ();
533 tower_init (&a->pixel_tower);
534 tower_init (&a->unit_tower);
540 psppire_axis_delete (PsppireAxis *a, gint first, gint n_units)
542 gint units_to_delete = n_units;
543 unsigned long int start;
544 struct tower_node *unit_node ;
545 g_return_if_fail (first + n_units <= tower_height (&a->unit_tower));
548 split (a, first + n_units);
550 unit_node = tower_lookup (&a->unit_tower, first, &start);
551 g_assert (start == first);
553 while (units_to_delete > 0)
555 struct tower_node *next_unit_node;
556 struct axis_node *an = tower_data (unit_node,
557 struct axis_node, unit_node);
559 g_assert (unit_node == &an->unit_node);
560 g_assert (unit_node->size <= n_units);
562 units_to_delete -= unit_node->size;
564 next_unit_node = tower_next (&a->unit_tower, unit_node);
566 tower_delete (&a->unit_tower, unit_node);
567 tower_delete (&a->pixel_tower, &an->pixel_node);
569 pool_free (a->pool, an);
571 unit_node = next_unit_node;