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 /* 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;
106 g_return_val_if_fail (pixel >= 0, -1);
108 g_mutex_lock (a->mutex);
110 size = tower_height (&a->pixel_tower);
114 gint n_items = tower_height (&a->unit_tower);
115 glong extra = pixel - size;
117 unit = n_items - 1 + DIV_RND_UP (extra, a->default_size);
122 n = tower_lookup (&a->pixel_tower, pixel, &start);
123 an = tower_data (n, struct axis_node, pixel_node);
125 fraction = (pixel - start) / (gdouble) tower_node_get_size (&an->pixel_node);
127 unit = tower_node_get_level (&an->unit_node)
128 + fraction * tower_node_get_size (&an->unit_node);
132 g_mutex_unlock (a->mutex);
139 psppire_axis_unit_count (const PsppireAxis *a)
145 g_mutex_lock (a->mutex);
147 actual_size = tower_height (&a->pixel_tower);
149 if ( actual_size < a->min_extent )
150 filler = DIV_RND_UP (a->min_extent - actual_size, a->default_size);
152 n_units = tower_height (&a->unit_tower) + filler;
154 g_mutex_unlock (a->mutex);
160 /* Return the starting pixel of UNIT */
162 psppire_axis_start_pixel (const PsppireAxis *a, gint unit)
165 struct tower_node *n ;
166 struct axis_node *an;
169 unsigned long int start;
171 gint the_count, size ;
173 g_mutex_lock (a->mutex);
175 the_count = tower_height (&a->unit_tower);
176 size = tower_height (&a->pixel_tower);
178 if ( unit >= the_count)
180 pixel = size + (unit - the_count) * a->default_size;
187 if ( unit >= tower_height (&a->unit_tower))
190 n = tower_lookup (&a->unit_tower, unit, &start);
192 an = tower_data (n, struct axis_node, unit_node);
194 fraction = (unit - start) / (gdouble) tower_node_get_size (&an->unit_node);
196 pixel = tower_node_get_level (&an->pixel_node) +
197 nearbyint (fraction * tower_node_get_size (&an->pixel_node));
200 g_mutex_unlock (a->mutex);
204 g_mutex_unlock (a->mutex);
209 psppire_axis_unit_size (const PsppireAxis *axis, gint unit)
211 struct tower_node *n ;
212 struct axis_node *an;
215 unsigned long int start;
217 g_mutex_lock (axis->mutex);
219 if (unit >= tower_height (&axis->unit_tower))
221 size = axis->default_size;
231 if ( unit >= tower_height (&axis->unit_tower))
237 n = tower_lookup (&axis->unit_tower, unit, &start);
239 an = tower_data (n, struct axis_node, unit_node);
241 size = nearbyint (tower_node_get_size (&an->pixel_node)
242 / (gdouble) tower_node_get_size (&an->unit_node));
244 g_mutex_unlock (axis->mutex);
250 /* --- functions --- */
252 * psppire_axis_get_type:
253 * @returns: the type ID for accelerator groups.
256 psppire_axis_get_type (void)
258 static GType object_type = 0;
262 static const GTypeInfo object_info = {
263 sizeof (PsppireAxisClass),
264 (GBaseInitFunc) NULL,
265 (GBaseFinalizeFunc) NULL,
266 (GClassInitFunc) psppire_axis_class_init,
267 NULL, /* class_finalize */
268 NULL, /* class_data */
269 sizeof (PsppireAxis),
271 (GInstanceInitFunc) psppire_axis_init,
274 object_type = g_type_register_static (G_TYPE_OBJECT,
293 psppire_axis_get_property (GObject *object,
298 PsppireAxis *axis = PSPPIRE_AXIS (object);
303 g_value_set_int (value, axis->padding);
305 case PROP_MIN_EXTENT:
306 g_value_set_long (value, axis->min_extent);
308 case PROP_DEFAULT_SIZE:
309 g_value_set_int (value, axis->default_size);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319 psppire_axis_set_property (GObject *object,
324 PsppireAxis *axis = PSPPIRE_AXIS (object);
330 const gint old_value = axis->padding;
331 axis->padding = g_value_get_int (value);
332 axis_increment (axis, axis->padding - old_value);
335 case PROP_MIN_EXTENT:
336 axis->min_extent = g_value_get_long (value);
338 case PROP_DEFAULT_SIZE:
339 axis->default_size = g_value_get_int (value);
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349 psppire_axis_class_init (PsppireAxisClass *class)
351 GObjectClass *object_class = G_OBJECT_CLASS (class);
353 GParamSpec *padding_spec;
354 GParamSpec *min_extent_spec;
355 GParamSpec *default_size_spec;
357 parent_class = g_type_class_peek_parent (class);
359 object_class->set_property = psppire_axis_set_property;
360 object_class->get_property = psppire_axis_get_property;
364 g_param_spec_long ("minimum-extent",
366 "The smallest extent to which the axis will provide units (typically set to the height/width of the associated widget).",
369 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
371 g_object_class_install_property (object_class,
377 g_param_spec_int ("default-size",
379 "The size given to units which haven't been explicity inserted",
382 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
385 g_object_class_install_property (object_class,
390 g_param_spec_int ("padding",
392 "Extra space implicitly added to each unit",
395 G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE );
398 g_object_class_install_property (object_class,
404 signals[RESIZE_UNIT] =
405 g_signal_new ("resize-unit",
406 G_TYPE_FROM_CLASS (object_class),
410 psppire_marshal_VOID__INT_LONG,
418 object_class->finalize = psppire_axis_finalize;
423 psppire_axis_init (PsppireAxis *axis)
425 axis->mutex = g_mutex_new ();
426 tower_init (&axis->pixel_tower);
427 tower_init (&axis->unit_tower);
429 axis->pool = pool_create ();
435 psppire_axis_finalize (GObject *object)
437 PsppireAxis *a = PSPPIRE_AXIS (object);
438 pool_destroy (a->pool);
439 g_mutex_free (a->mutex);
441 G_OBJECT_CLASS (parent_class)->finalize (object);
446 * @returns: a new #PsppireAxis object
448 * Creates a new #PsppireAxis.
451 psppire_axis_new (void)
453 return g_object_new (G_TYPE_PSPPIRE_AXIS, NULL);
460 psppire_axis_append (PsppireAxis *a, gint size)
462 psppire_axis_append_n (a, 1, size);
466 /* Append N_UNITS of size SIZE to A.
467 The value of the "padding" property will be added to SIZE
468 unit before appending.
471 psppire_axis_append_n (PsppireAxis *a, gint n_units, gint size)
473 struct axis_node *node;
475 g_mutex_lock (a->mutex);
480 node = pool_malloc (a->pool, sizeof *node);
482 tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
483 tower_insert (&a->pixel_tower, (size + a->padding) * n_units,
484 &node->pixel_node, NULL);
487 g_mutex_unlock (a->mutex);
492 /* Split the node of both towers at POSN */
494 split (PsppireAxis *a, gint posn)
496 unsigned long int existing_unit_size;
497 unsigned long int existing_pixel_size;
498 unsigned long int start;
500 struct axis_node *new_node ;
501 struct tower_node *n;
502 struct axis_node *existing_node;
504 g_return_if_fail (posn <= tower_height (&a->unit_tower));
506 /* Nothing needs to be done */
507 if ( posn == 0 || posn == tower_height (&a->unit_tower))
510 n = tower_lookup (&a->unit_tower, posn, &start);
512 existing_node = tower_data (n, struct axis_node, unit_node);
514 /* Nothing needs to be done, if the range element is already split here */
515 if ( posn - start == 0)
518 existing_unit_size = tower_node_get_size (&existing_node->unit_node);
519 existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
521 fraction = (posn - start) / (gdouble) existing_unit_size;
523 new_node = pool_malloc (a->pool, sizeof (*new_node));
525 tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
527 tower_resize (&a->pixel_tower, &existing_node->pixel_node,
528 nearbyintf (fraction * existing_pixel_size));
530 tower_insert (&a->unit_tower,
531 existing_unit_size - (posn - start),
532 &new_node->unit_node,
533 tower_next (&a->unit_tower, &existing_node->unit_node));
536 tower_insert (&a->pixel_tower,
537 nearbyintf (existing_pixel_size * (1 - fraction)),
538 &new_node->pixel_node,
539 tower_next (&a->pixel_tower, &existing_node->pixel_node));
543 /* Insert a new unit of size SIZE before POSN.
544 The value of the "padding" property will be added to SIZE before
545 the unit is inserted.
548 psppire_axis_insert (PsppireAxis *a, gint posn, gint size)
550 struct axis_node *before = NULL;
551 struct axis_node *new_node;
553 g_return_if_fail ( posn >= 0);
554 g_return_if_fail ( posn <= tower_height (&a->unit_tower));
556 g_mutex_lock (a->mutex);
558 if ( posn < tower_height (&a->unit_tower))
560 unsigned long int start = 0;
561 struct tower_node *n;
565 n = tower_lookup (&a->unit_tower, posn, &start);
566 g_assert (posn == start);
568 before = tower_data (n, struct axis_node, unit_node);
571 new_node = pool_malloc (a->pool, sizeof (*new_node));
573 tower_insert (&a->unit_tower,
575 &new_node->unit_node,
576 before ? &before->unit_node : NULL);
578 tower_insert (&a->pixel_tower,
580 &new_node->pixel_node,
581 before ? &before->pixel_node : NULL);
583 g_mutex_unlock (a->mutex);
587 /* Make the element at POSN singular.
588 Return a pointer to the node for this element */
589 static struct axis_node *
590 make_single (PsppireAxis *a, gint posn)
592 unsigned long int start;
593 struct tower_node *n;
595 g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
597 n = tower_lookup (&a->unit_tower, posn, &start);
599 if ( 1 != tower_node_get_size (n))
602 n = tower_lookup (&a->unit_tower, posn, &start);
604 if ( 1 != tower_node_get_size (n))
607 n = tower_lookup (&a->unit_tower, posn, &start);
611 g_assert (1 == tower_node_get_size (n));
613 return tower_data (n, struct axis_node, unit_node);
618 Set the size of the unit at POSN to be SIZE plus the
619 current value of "padding"
622 psppire_axis_resize (PsppireAxis *axis, gint posn, glong size)
624 struct axis_node *an;
625 g_return_if_fail (posn >= 0);
626 g_return_if_fail (size > 0);
628 g_mutex_lock (axis->mutex);
630 /* Silently ignore this request if the position is greater than the number of
632 if (posn >= tower_height (&axis->unit_tower))
635 an = make_single (axis, posn);
637 tower_resize (&axis->pixel_tower, &an->pixel_node, size + axis->padding);
640 g_mutex_unlock (axis->mutex);
641 g_signal_emit (axis, signals[RESIZE_UNIT], 0, posn, size + axis->padding);
645 g_mutex_unlock (axis->mutex);
655 psppire_axis_clear (PsppireAxis *a)
657 g_mutex_lock (a->mutex);
658 pool_destroy (a->pool);
659 a->pool = pool_create ();
661 tower_init (&a->pixel_tower);
662 tower_init (&a->unit_tower);
663 g_mutex_unlock (a->mutex);
669 psppire_axis_delete (PsppireAxis *a, gint first, gint n_units)
671 gint units_to_delete = n_units;
672 unsigned long int start;
673 struct tower_node *unit_node ;
674 g_return_if_fail (first + n_units <= tower_height (&a->unit_tower));
676 g_mutex_lock (a->mutex);
679 split (a, first + n_units);
681 unit_node = tower_lookup (&a->unit_tower, first, &start);
682 g_assert (start == first);
684 while (units_to_delete > 0)
686 struct tower_node *next_unit_node;
687 struct axis_node *an = tower_data (unit_node,
688 struct axis_node, unit_node);
690 g_assert (unit_node == &an->unit_node);
691 g_assert (unit_node->size <= n_units);
693 units_to_delete -= unit_node->size;
695 next_unit_node = tower_next (&a->unit_tower, unit_node);
697 tower_delete (&a->unit_tower, unit_node);
698 tower_delete (&a->pixel_tower, &an->pixel_node);
700 pool_free (a->pool, an);
702 unit_node = next_unit_node;
704 g_mutex_unlock (a->mutex);