b722a87b16c4379a69b9ae4c5198c9b417328725
[pspp-builds.git] / lib / gtksheet / psppire-axis-impl.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include <libpspp/tower.h>
22 #include <libpspp/pool.h>
23 #include "psppire-axis-impl.h"
24 #include <math.h>
25
26
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);
31
32
33 /* --- variables --- */
34 static GObjectClass     *parent_class = NULL;
35
36
37 struct axis_node
38 {
39   struct tower_node pixel_node;
40   struct tower_node unit_node;
41 };
42
43 static gint
44 unit_at_pixel (const PsppireAxis *axis, glong pixel)
45 {
46   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
47
48   unsigned long int start;
49   struct tower_node *n;
50   struct axis_node *an;
51   gfloat fraction;
52
53   g_return_val_if_fail (pixel >= 0, -1);
54
55   n = tower_lookup (&a->pixel_tower, pixel, &start);
56   an = tower_data (n, struct axis_node, pixel_node);
57
58   fraction = (pixel - start) / (gfloat) tower_node_get_size (&an->pixel_node);
59
60   return  tower_node_get_level (&an->unit_node)
61     + fraction * tower_node_get_size (&an->unit_node);
62 }
63
64
65 static gint
66 unit_count (const PsppireAxis *axis)
67 {
68   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
69
70   return tower_height (&a->unit_tower);
71 }
72
73
74 /* Returns the pixel at the start of UNIT */
75 static glong
76 start_pixel (const PsppireAxis *axis, gint unit)
77 {
78   gfloat fraction;
79   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
80   struct tower_node *n ;
81   struct axis_node *an;
82
83   unsigned long int start;
84
85   if ( unit < 0)
86     return -1;
87
88   if ( unit >= unit_count (axis))
89     return -1;
90
91   n = tower_lookup (&a->unit_tower, unit, &start);
92
93   an = tower_data (n, struct axis_node, unit_node);
94
95   fraction = (unit - start) / (gfloat) tower_node_get_size (&an->unit_node);
96
97   return  tower_node_get_level (&an->pixel_node) +
98     nearbyintf (fraction * tower_node_get_size (&an->pixel_node));
99 }
100
101
102 static gint
103 unit_size (const PsppireAxis *axis, gint unit)
104 {
105   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
106   struct tower_node *n ;
107   struct axis_node *an;
108
109   unsigned long int start;
110
111   if ( unit < 0)
112     return 0;
113
114   if ( unit >= unit_count (axis))
115     return 0;
116
117   n = tower_lookup (&a->unit_tower, unit, &start);
118
119   an = tower_data (n, struct axis_node, unit_node);
120
121   return nearbyintf (tower_node_get_size (&an->pixel_node)
122                      / (float) tower_node_get_size (&an->unit_node));
123 }
124
125
126 static glong
127 total_size (const PsppireAxis *axis)
128 {
129   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
130
131   return tower_height (&a->pixel_tower);
132 }
133
134
135 static void resize (PsppireAxis *axis, gint posn, glong size);
136
137
138
139 static void
140 psppire_impl_iface_init (PsppireAxisIface *iface)
141 {
142   iface->unit_size = unit_size;
143   iface->unit_count = unit_count;
144   iface->start_pixel = start_pixel;
145   iface->unit_at_pixel = unit_at_pixel;
146   iface->total_size = total_size;
147   iface->resize = resize;
148 }
149
150 /* --- functions --- */
151 /**
152  * psppire_axis_impl_get_type:
153  * @returns: the type ID for accelerator groups.
154  */
155 GType
156 psppire_axis_impl_get_type (void)
157 {
158   static GType object_type = 0;
159
160   if (!object_type)
161     {
162       static const GTypeInfo object_info = {
163         sizeof (PsppireAxisImplClass),
164         (GBaseInitFunc) NULL,
165         (GBaseFinalizeFunc) NULL,
166         (GClassInitFunc) psppire_axis_impl_class_init,
167         NULL,   /* class_finalize */
168         NULL,   /* class_data */
169         sizeof (PsppireAxisImpl),
170         0,      /* n_preallocs */
171         (GInstanceInitFunc) psppire_axis_impl_init,
172       };
173
174       static const GInterfaceInfo interface_info =
175       {
176         (GInterfaceInitFunc) psppire_impl_iface_init,
177         NULL,
178         NULL
179       };
180
181
182       object_type = g_type_register_static (G_TYPE_PSPPIRE_AXIS,
183                                             "PsppireAxisImpl",
184                                             &object_info, 0);
185
186
187       g_type_add_interface_static (object_type,
188                                    PSPPIRE_TYPE_AXIS_IFACE,
189                                    &interface_info);
190     }
191
192   return object_type;
193 }
194
195 static void
196 psppire_axis_impl_class_init (PsppireAxisImplClass *class)
197 {
198   GObjectClass *object_class = G_OBJECT_CLASS (class);
199   parent_class = g_type_class_peek_parent (class);
200
201   object_class->finalize = psppire_axis_impl_finalize;
202 }
203
204
205 static void
206 psppire_axis_impl_init (PsppireAxisImpl *axis)
207 {
208   tower_init (&axis->pixel_tower);
209   tower_init (&axis->unit_tower);
210
211   axis->pool = pool_create ();
212 }
213
214
215 static void
216 psppire_axis_impl_finalize (GObject *object)
217 {
218   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (object);
219   pool_destroy (a->pool);
220
221   G_OBJECT_CLASS (parent_class)->finalize (object);
222 }
223
224 /**
225  * psppire_axis_impl_new:
226  * @returns: a new #PsppireAxisImpl object
227  *
228  * Creates a new #PsppireAxisImpl.
229  */
230 PsppireAxisImpl*
231 psppire_axis_impl_new (void)
232 {
233   return g_object_new (G_TYPE_PSPPIRE_AXIS_IMPL, NULL);
234 }
235
236
237 \f
238
239 void
240 psppire_axis_impl_append (PsppireAxisImpl *a, gint size)
241 {
242   psppire_axis_impl_append_n (a, 1, size);
243 }
244
245
246 void
247 psppire_axis_impl_append_n (PsppireAxisImpl *a, gint n_units, gint size)
248 {
249   struct axis_node *node;
250
251   g_return_if_fail (n_units > 0);
252
253   node = pool_alloc (a->pool, sizeof *node);
254
255   tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
256   tower_insert (&a->pixel_tower, size * n_units, &node->pixel_node, NULL);
257 }
258
259
260 /* Split the node of both towers at POSN */
261 static void
262 split (PsppireAxisImpl *a, gint posn)
263 {
264   unsigned long int existing_unit_size;
265   unsigned long int existing_pixel_size;
266   unsigned long int start;
267   gfloat fraction;
268   struct axis_node *new_node ;
269   struct tower_node *n;
270   struct axis_node *existing_node;
271
272   g_return_if_fail (posn <= tower_height (&a->unit_tower));
273
274   /* Nothing needs to be done */
275   if ( posn == 0 || posn  == tower_height (&a->unit_tower))
276     return;
277
278   n = tower_lookup (&a->unit_tower, posn, &start);
279
280   existing_node = tower_data (n, struct axis_node, unit_node);
281
282   /* Nothing needs to be done, if the range element is already split here */
283   if ( posn - start == 0)
284     return;
285
286   existing_unit_size = tower_node_get_size (&existing_node->unit_node);
287   existing_pixel_size = tower_node_get_size (&existing_node->pixel_node);
288
289   fraction = (posn - start) / (gfloat) existing_unit_size;
290
291   new_node = pool_alloc (a->pool, sizeof (*new_node));
292
293   tower_resize (&a->unit_tower, &existing_node->unit_node, posn - start);
294
295   tower_resize (&a->pixel_tower, &existing_node->pixel_node,
296                 nearbyintf (fraction * existing_pixel_size));
297
298   tower_insert (&a->unit_tower,
299                 existing_unit_size - (posn - start),
300                 &new_node->unit_node,
301                 tower_next (&a->unit_tower, &existing_node->unit_node));
302
303
304   tower_insert (&a->pixel_tower,
305                 nearbyintf (existing_pixel_size * (1 - fraction)),
306                 &new_node->pixel_node,
307                 tower_next (&a->pixel_tower, &existing_node->pixel_node));
308 }
309
310
311 /* Insert a new unit of size SIZE before POSN */
312 void
313 psppire_axis_impl_insert (PsppireAxisImpl *a, gint posn, gint size)
314 {
315   struct axis_node *before = NULL;
316   struct axis_node *new_node;
317
318   g_return_if_fail ( posn < tower_height (&a->unit_tower));
319   g_return_if_fail ( posn >= 0);
320
321   new_node = pool_alloc (a->pool, sizeof (*new_node));
322
323   if ( posn > 0)
324     {
325       unsigned long int start = 0;
326       struct tower_node *n;
327
328       split (a, posn);
329
330       n = tower_lookup (&a->unit_tower, posn, &start);
331       g_assert (posn == start);
332
333       before = tower_data (n, struct axis_node, unit_node);
334     }
335
336   tower_insert (&a->unit_tower,
337                 1,
338                 &new_node->unit_node,
339                 before ? &before->unit_node : NULL);
340
341
342   tower_insert (&a->pixel_tower,
343                 size,
344                 &new_node->pixel_node,
345                 before ? &before->pixel_node : NULL);
346 }
347
348
349 /* Make the element at POSN singular.
350    Return a pointer to the node for this element */
351 static struct axis_node *
352 make_single (PsppireAxisImpl *a, gint posn)
353 {
354   unsigned long int start;
355   struct tower_node *n;
356
357   g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
358
359   n = tower_lookup (&a->unit_tower, posn, &start);
360
361   if ( 1 != tower_node_get_size (n))
362     {
363       split (a, posn + 1);
364       n = tower_lookup (&a->unit_tower, posn, &start);
365
366       if ( 1 != tower_node_get_size (n))
367         {
368           split (a, posn);
369           n = tower_lookup (&a->unit_tower, posn, &start);
370         }
371     }
372
373   g_assert (1 == tower_node_get_size (n));
374
375
376   return tower_data (n, struct axis_node, unit_node);
377 }
378
379 static void
380 resize (PsppireAxis *axis, gint posn, glong size)
381 {
382   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
383
384   struct axis_node *an;
385   g_return_if_fail (posn >= 0);
386
387   /* Silently ignore this request if the position is greater than the number of
388      units in the axis */
389   if (posn >= tower_height (&a->unit_tower))
390     return ;
391
392   an = make_single (a, posn);
393
394   tower_resize (&a->pixel_tower, &an->pixel_node, size);
395 }
396
397
398 void
399 psppire_axis_impl_resize (PsppireAxisImpl *a, gint posn, gint size)
400 {
401   resize (PSPPIRE_AXIS (a), posn, size);
402 }
403
404
405
406
407 void
408 psppire_axis_impl_clear (PsppireAxisImpl *a)
409 {
410   pool_destroy (a->pool);
411   a->pool = pool_create ();
412
413   tower_init (&a->pixel_tower);
414   tower_init (&a->unit_tower);
415 }
416
417
418
419 void
420 psppire_axis_impl_delete (PsppireAxisImpl *a, gint first, gint n_units)
421 {
422   gint i;
423   g_warning ("%s FIXME: This is an inefficient implementation", __FUNCTION__);
424
425   g_return_if_fail (first + n_units < tower_height (&a->unit_tower));
426
427   for (i = first; i < first + n_units; ++i)
428     {
429       struct axis_node *an = make_single (a, first);
430
431       tower_delete (&a->unit_tower, &an->unit_node);
432       tower_delete (&a->pixel_tower, &an->pixel_node);
433     }
434 }