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-hetero.h"
27 /* --- prototypes --- */
28 static void psppire_axis_hetero_class_init (PsppireAxisHeteroClass *class);
29 static void psppire_axis_hetero_init (PsppireAxisHetero *axis);
30 static void psppire_axis_hetero_finalize (GObject *object);
33 /* --- variables --- */
34 static GObjectClass *parent_class = NULL;
38 get_unit_at_pixel (const PsppireAxis *a, glong pixel)
40 PsppireAxisHetero *ah = PSPPIRE_AXIS_HETERO (a);
41 const struct tower_node *node;
42 unsigned long int node_start;
44 if (pixel >= tower_height (&ah->tower))
45 return tower_count (&ah->tower);
47 node = tower_lookup (&ah->tower, pixel, &node_start);
49 return tower_node_get_index (node);
54 unit_count (const PsppireAxis *a)
56 PsppireAxisHetero *ah = PSPPIRE_AXIS_HETERO (a);
57 return tower_count (&ah->tower);
62 pixel_start (const PsppireAxis *a, gint unit)
64 PsppireAxisHetero *ah = PSPPIRE_AXIS_HETERO (a);
65 const struct tower_node *node;
67 if ( unit >= tower_count (&ah->tower))
68 return tower_height (&ah->tower);
70 node = tower_get (&ah->tower, unit);
72 return tower_node_get_level (node);
77 unit_size (const PsppireAxis *a, gint unit)
79 PsppireAxisHetero *ah = PSPPIRE_AXIS_HETERO (a);
80 const struct tower_node *node;
81 if (unit >= tower_count (&ah->tower))
84 node = tower_get (&ah->tower, unit);
86 return tower_node_get_size (node);
91 total_size (const PsppireAxis *a)
94 PsppireAxisHetero *ah = PSPPIRE_AXIS_HETERO (a);
96 s = tower_height (&ah->tower);
101 psppire_hetero_iface_init (PsppireAxisIface *iface)
103 iface->unit_size = unit_size;
104 iface->unit_count = unit_count;
105 iface->pixel_start = pixel_start;
106 iface->get_unit_at_pixel = get_unit_at_pixel;
107 iface->total_size = total_size;
110 /* --- functions --- */
112 * psppire_axis_hetero_get_type:
113 * @returns: the type ID for accelerator groups.
116 psppire_axis_hetero_get_type (void)
118 static GType object_type = 0;
122 static const GTypeInfo object_info = {
123 sizeof (PsppireAxisHeteroClass),
124 (GBaseInitFunc) NULL,
125 (GBaseFinalizeFunc) NULL,
126 (GClassInitFunc) psppire_axis_hetero_class_init,
127 NULL, /* class_finalize */
128 NULL, /* class_data */
129 sizeof (PsppireAxisHetero),
131 (GInstanceInitFunc) psppire_axis_hetero_init,
135 static const GInterfaceInfo interface_info =
137 (GInterfaceInitFunc) psppire_hetero_iface_init,
142 object_type = g_type_register_static (G_TYPE_PSPPIRE_AXIS,
146 g_type_add_interface_static (object_type,
147 PSPPIRE_TYPE_AXIS_IFACE,
155 psppire_axis_hetero_class_init (PsppireAxisHeteroClass *class)
157 GObjectClass *object_class = G_OBJECT_CLASS (class);
158 parent_class = g_type_class_peek_parent (class);
160 object_class->finalize = psppire_axis_hetero_finalize;
165 psppire_axis_hetero_init (PsppireAxisHetero *axis)
168 psppire_axis_hetero_clear (axis);
173 psppire_axis_hetero_finalize (GObject *object)
175 PsppireAxisHetero *a = PSPPIRE_AXIS_HETERO (object);
176 pool_destroy (a->pool);
177 G_OBJECT_CLASS (parent_class)->finalize (object);
181 * psppire_axis_hetero_new:
182 * @returns: a new #PsppireAxisHetero object
184 * Creates a new #PsppireAxisHetero.
187 psppire_axis_hetero_new (void)
189 return g_object_new (G_TYPE_PSPPIRE_AXIS_HETERO, NULL);
194 psppire_axis_hetero_append (PsppireAxisHetero *a, gint size)
196 struct tower_node *new ;
198 g_return_if_fail (PSPPIRE_IS_AXIS_HETERO (a));
200 new = pool_malloc (a->pool, sizeof *new);
202 tower_insert (&a->tower, size, new, NULL);
207 /* Insert a new unit of size SIZE before position POSN */
209 psppire_axis_hetero_insert (PsppireAxisHetero *a, gint size, gint posn)
211 struct tower_node *new;
212 struct tower_node *before = NULL;
214 g_return_if_fail (PSPPIRE_IS_AXIS_HETERO (a));
216 new = pool_malloc (a->pool, sizeof *new);
218 if ( posn != tower_count (&a->tower))
219 before = tower_get (&a->tower, posn);
221 tower_insert (&a->tower, size, new, before);
226 psppire_axis_hetero_remove (PsppireAxisHetero *a, gint posn)
228 struct tower_node *node;
230 g_return_if_fail (PSPPIRE_IS_AXIS_HETERO (a));
232 node = tower_get (&a->tower, posn);
234 tower_delete (&a->tower, node);
236 pool_free (a->pool, node);
241 psppire_axis_hetero_resize_unit (PsppireAxisHetero *a, gint size, gint posn)
243 struct tower_node *node;
245 g_return_if_fail (PSPPIRE_IS_AXIS_HETERO (a));
247 node = tower_get (&a->tower, posn);
249 tower_resize (&a->tower, node, size);
254 psppire_axis_hetero_clear (PsppireAxisHetero *a)
256 g_return_if_fail (PSPPIRE_IS_AXIS_HETERO (a));
258 pool_destroy (a->pool);
259 a->pool = pool_create ();
260 tower_init (&a->tower);