1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010 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 /* Increment the size of every unit by INC.
73 Note that INC is signed. So if INC is negative,
74 then size will end up smaller.
77 axis_increment (PsppireAxis *axis, gint inc)
79 struct tower_node *n = tower_first (&axis->pixel_tower);
83 struct axis_node *an = tower_data (n, struct axis_node, pixel_node);
84 struct tower_node *pn = &an->pixel_node;
85 const gint existing_size = tower_node_get_size (pn);
87 tower_resize (&axis->pixel_tower, pn, existing_size + inc *
88 tower_node_get_size (&an->unit_node));
90 n = tower_next (&axis->pixel_tower, n);
95 /* Return the unit covered by PIXEL */
97 psppire_axis_unit_at_pixel (const PsppireAxis *a, glong pixel)
99 unsigned long int start;
100 struct tower_node *n;
101 struct axis_node *an;
104 glong size = tower_height (&a->pixel_tower);
106 g_return_val_if_fail (pixel >= 0, -1);
110 gint n_items = tower_height (&a->unit_tower);
111 glong extra = pixel - size;
113 return n_items - 1 + DIV_RND_UP (extra, a->default_size);
117 n = tower_lookup (&a->pixel_tower, pixel, &start);
118 an = tower_data (n, struct axis_node, pixel_node);
120 fraction = (pixel - start) / (gdouble) tower_node_get_size (&an->pixel_node);
122 return tower_node_get_level (&an->unit_node)
123 + fraction * tower_node_get_size (&an->unit_node);
128 psppire_axis_unit_count (const PsppireAxis *a)
133 actual_size = tower_height (&a->pixel_tower);
135 if ( actual_size < a->min_extent )
136 filler = DIV_RND_UP (a->min_extent - actual_size, a->default_size);
138 return tower_height (&a->unit_tower) + filler;
142 /* Return the starting pixel of UNIT */
144 psppire_axis_start_pixel (const PsppireAxis *a, gint unit)
147 struct tower_node *n ;
148 struct axis_node *an;
150 unsigned long int start;
152 gint the_count, size ;
154 the_count = tower_height (&a->unit_tower);
155 size = tower_height (&a->pixel_tower);
157 if ( unit >= the_count)
159 return size + (unit - the_count) * a->default_size;
165 if ( unit >= tower_height (&a->unit_tower))
168 n = tower_lookup (&a->unit_tower, unit, &start);
170 an = tower_data (n, struct axis_node, unit_node);
172 fraction = (unit - start) / (gdouble) tower_node_get_size (&an->unit_node);
174 return tower_node_get_level (&an->pixel_node) +
175 rint (fraction * tower_node_get_size (&an->pixel_node));
179 psppire_axis_unit_size (const PsppireAxis *axis, gint unit)
181 struct tower_node *n ;
182 struct axis_node *an;
184 unsigned long int start;
186 if (unit >= tower_height (&axis->unit_tower))
187 return axis->default_size;
192 if ( unit >= tower_height (&axis->unit_tower))
195 n = tower_lookup (&axis->unit_tower, unit, &start);
197 an = tower_data (n, struct axis_node, unit_node);
199 return rint (tower_node_get_size (&an->pixel_node)
200 / (gdouble) tower_node_get_size (&an->unit_node));
205 /* --- functions --- */
207 * psppire_axis_get_type:
208 * @returns: the type ID for accelerator groups.
211 psppire_axis_get_type (void)
213 static GType object_type = 0;
217 static const GTypeInfo object_info = {
218 sizeof (PsppireAxisClass),
219 (GBaseInitFunc) NULL,
220 (GBaseFinalizeFunc) NULL,
221 (GClassInitFunc) psppire_axis_class_init,
222 NULL, /* class_finalize */
223 NULL, /* class_data */
224 sizeof (PsppireAxis),
226 (GInstanceInitFunc) psppire_axis_init,
229 object_type = g_type_register_static (G_TYPE_OBJECT,
248 psppire_axis_get_property (GObject *object,
253 PsppireAxis *axis = PSPPIRE_AXIS (object);
258 g_value_set_int (value, axis->padding);
260 case PROP_MIN_EXTENT:
261 g_value_set_long (value, axis->min_extent);
263 case PROP_DEFAULT_SIZE:
264 g_value_set_int (value, axis->default_size);
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 psppire_axis_set_property (GObject *object,
279 PsppireAxis *axis = PSPPIRE_AXIS (object);
285 const gint old_value = axis->padding;
286 axis->padding = g_value_get_int (value);
287 axis_increment (axis, axis->padding - old_value);
290 case PROP_MIN_EXTENT:
291 axis->min_extent = g_value_get_long (value);
293 case PROP_DEFAULT_SIZE:
294 axis->default_size = g_value_get_int (value);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 psppire_axis_class_init (PsppireAxisClass *class)
306 GObjectClass *object_class = G_OBJECT_CLASS (class);
308 GParamSpec *padding_spec;
309 GParamSpec *min_extent_spec;
310 GParamSpec *default_size_spec;
312 parent_class = g_type_class_peek_parent (class);
314 object_class->set_property = psppire_axis_set_property;
315 object_class->get_property = psppire_axis_get_property;
319 g_param_spec_long ("minimum-extent",
321 "The smallest extent to which the axis will provide units (typically set to the height/width of the associated widget).",
324 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
326 g_object_class_install_property (object_class,
332 g_param_spec_int ("default-size",
334 "The size given to units which haven't been explicity inserted",
337 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
340 g_object_class_install_property (object_class,
345 g_param_spec_int ("padding",
347 "Extra space implicitly added to each unit",
350 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
353 g_object_class_install_property (object_class,
359 signals[RESIZE_UNIT] =
360 g_signal_new ("resize-unit",
361 G_TYPE_FROM_CLASS (object_class),
365 psppire_marshal_VOID__INT_LONG,
373 object_class->finalize = psppire_axis_finalize;
378 psppire_axis_init (PsppireAxis *axis)
380 tower_init (&axis->pixel_tower);
381 tower_init (&axis->unit_tower);
383 axis->pool = pool_create ();
389 psppire_axis_finalize (GObject *object)
391 PsppireAxis *a = PSPPIRE_AXIS (object);
392 pool_destroy (a->pool);
394 G_OBJECT_CLASS (parent_class)->finalize (object);
399 * @returns: a new #PsppireAxis object
401 * Creates a new #PsppireAxis.
404 psppire_axis_new (void)
406 return g_object_new (G_TYPE_PSPPIRE_AXIS, NULL);
413 psppire_axis_append (PsppireAxis *a, gint size)
415 psppire_axis_append_n (a, 1, size);
419 /* Append N_UNITS of size SIZE to A.
420 The value of the "padding" property will be added to SIZE
421 unit before appending.
424 psppire_axis_append_n (PsppireAxis *a, gint n_units, gint size)
426 struct axis_node *node;
431 node = pool_malloc (a->pool, sizeof *node);
433 tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
434 tower_insert (&a->pixel_tower, (size + a->padding) * n_units,
435 &node->pixel_node, NULL);
439 /* Split the node of both towers at POSN */
441 split (PsppireAxis *a, gint posn)
443 unsigned long int existing_unit_size;
444 unsigned long int existing_pixel_size;
445 unsigned long int start;
447 struct axis_node *new_node ;
448 struct tower_node *n;
449 struct axis_node *existing_node;
451 g_return_if_fail (posn <= tower_height (&a->unit_tower));
453 /* Nothing needs to be done */
454 if ( posn == 0 || posn == tower_height (&a->unit_tower))
457 n = tower_lookup (&a->unit_tower, posn, &start);
459 existing_node = tower_data (n, struct axis_node, unit_node);
461 /* Nothing needs to be done, if the range element is already split here */
462 if ( posn - start == 0)
465 existing_unit_size = tower_node_get_size (&existing_node->unit_node);
466 existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
468 fraction = (posn - start) / (gdouble) existing_unit_size;
470 new_node = pool_malloc (a->pool, sizeof (*new_node));
472 tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
474 tower_resize (&a->pixel_tower, &existing_node->pixel_node,
475 rintf (fraction * existing_pixel_size));
477 tower_insert (&a->unit_tower,
478 existing_unit_size - (posn - start),
479 &new_node->unit_node,
480 tower_next (&a->unit_tower, &existing_node->unit_node));
483 tower_insert (&a->pixel_tower,
484 rintf (existing_pixel_size * (1 - fraction)),
485 &new_node->pixel_node,
486 tower_next (&a->pixel_tower, &existing_node->pixel_node));
490 /* Insert a new unit of size SIZE before POSN.
491 The value of the "padding" property will be added to SIZE before
492 the unit is inserted.
495 psppire_axis_insert (PsppireAxis *a, gint posn, gint size)
497 struct axis_node *before = NULL;
498 struct axis_node *new_node;
500 g_return_if_fail ( posn >= 0);
501 g_return_if_fail ( posn <= tower_height (&a->unit_tower));
503 if ( posn < tower_height (&a->unit_tower))
505 unsigned long int start = 0;
506 struct tower_node *n;
510 n = tower_lookup (&a->unit_tower, posn, &start);
511 g_assert (posn == start);
513 before = tower_data (n, struct axis_node, unit_node);
516 new_node = pool_malloc (a->pool, sizeof (*new_node));
518 tower_insert (&a->unit_tower,
520 &new_node->unit_node,
521 before ? &before->unit_node : NULL);
523 tower_insert (&a->pixel_tower,
525 &new_node->pixel_node,
526 before ? &before->pixel_node : NULL);
530 /* Make the element at POSN singular.
531 Return a pointer to the node for this element */
532 static struct axis_node *
533 make_single (PsppireAxis *a, gint posn)
535 unsigned long int start;
536 struct tower_node *n;
538 g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
540 n = tower_lookup (&a->unit_tower, posn, &start);
542 if ( 1 != tower_node_get_size (n))
545 n = tower_lookup (&a->unit_tower, posn, &start);
547 if ( 1 != tower_node_get_size (n))
550 n = tower_lookup (&a->unit_tower, posn, &start);
554 g_assert (1 == tower_node_get_size (n));
556 return tower_data (n, struct axis_node, unit_node);
561 Set the size of the unit at POSN to be SIZE plus the
562 current value of "padding"
565 psppire_axis_resize (PsppireAxis *axis, gint posn, glong size)
567 struct axis_node *an;
568 g_return_if_fail (posn >= 0);
569 g_return_if_fail (size > 0);
571 /* Silently ignore this request if the position is greater than the number of
573 if (posn >= tower_height (&axis->unit_tower))
576 an = make_single (axis, posn);
578 tower_resize (&axis->pixel_tower, &an->pixel_node, size + axis->padding);
580 g_signal_emit (axis, signals[RESIZE_UNIT], 0, posn, size + axis->padding);
589 psppire_axis_clear (PsppireAxis *a)
591 pool_destroy (a->pool);
592 a->pool = pool_create ();
594 tower_init (&a->pixel_tower);
595 tower_init (&a->unit_tower);
601 psppire_axis_delete (PsppireAxis *a, gint first, gint n_units)
603 gint units_to_delete = n_units;
604 unsigned long int start;
605 struct tower_node *unit_node ;
606 g_return_if_fail (first + n_units <= tower_height (&a->unit_tower));
609 split (a, first + n_units);
611 unit_node = tower_lookup (&a->unit_tower, first, &start);
612 g_assert (start == first);
614 while (units_to_delete > 0)
616 struct tower_node *next_unit_node;
617 struct axis_node *an = tower_data (unit_node,
618 struct axis_node, unit_node);
620 g_assert (unit_node == &an->unit_node);
621 g_assert (unit_node->size <= n_units);
623 units_to_delete -= unit_node->size;
625 next_unit_node = tower_next (&a->unit_tower, unit_node);
627 tower_delete (&a->unit_tower, unit_node);
628 tower_delete (&a->pixel_tower, &an->pixel_node);
630 pool_free (a->pool, an);
632 unit_node = next_unit_node;