Replace gsheet-column interface by psppire-axis
[pspp-builds.git] / lib / gtksheet / psppire-axis.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 "psppire-axis.h"
23 #include <gtk/gtk.h>
24
25
26 /* --- prototypes --- */
27 static void psppire_axis_class_init (PsppireAxisClass   *class);
28 static void psppire_axis_init   (PsppireAxis            *axis);
29 static void psppire_axis_finalize   (GObject            *object);
30
31
32 /* --- variables --- */
33 static GObjectClass     *parent_class = NULL;
34
35 /* --- functions --- */
36 /**
37  * psppire_axis_get_type:
38  * @returns: the type ID for accelerator groups.
39  */
40 GType
41 psppire_axis_get_type (void)
42 {
43   static GType object_type = 0;
44
45   if (!object_type)
46     {
47       static const GTypeInfo object_info = {
48         sizeof (PsppireAxisClass),
49         (GBaseInitFunc) NULL,
50         (GBaseFinalizeFunc) NULL,
51         (GClassInitFunc) psppire_axis_class_init,
52         NULL,   /* class_finalize */
53         NULL,   /* class_data */
54         sizeof (PsppireAxis),
55         0,      /* n_preallocs */
56         (GInstanceInitFunc) psppire_axis_init,
57       };
58
59       object_type = g_type_register_static (G_TYPE_OBJECT,
60                                             "PsppireAxis",
61                                             &object_info, 0);
62     }
63
64   return object_type;
65 }
66
67
68 static void
69 psppire_axis_class_init (PsppireAxisClass *class)
70 {
71   GObjectClass *object_class = G_OBJECT_CLASS (class);
72
73   parent_class = g_type_class_peek_parent (class);
74
75   object_class->finalize = psppire_axis_finalize;
76 }
77
78 static void
79 psppire_axis_init (PsppireAxis *axis)
80 {
81   tower_init (&axis->tower);
82 }
83
84
85 static void
86 psppire_axis_finalize (GObject *object)
87 {
88   G_OBJECT_CLASS (parent_class)->finalize (object);
89 }
90
91 /**
92  * psppire_axis_new:
93  * @returns: a new #PsppireAxis object
94  *
95  * Creates a new #PsppireAxis.
96  */
97 PsppireAxis*
98 psppire_axis_new (gint w)
99 {
100   PsppireAxis *new_axis = g_object_new (G_TYPE_PSPPIRE_AXIS, NULL);
101
102   new_axis->width = w;
103
104   return new_axis;
105 }
106
107
108 gint
109 psppire_axis_unit_size (PsppireAxis *a, gint unit)
110 {
111   if ( a->width == -1)
112     {
113       const struct tower_node *node;
114       if  (unit >= tower_count (&a->tower))
115         return 0;
116
117       node = tower_get (&a->tower, unit);
118
119       return tower_node_get_size (node);
120     }
121
122   return a->width;
123 }
124
125 gint
126 psppire_axis_unit_count (PsppireAxis *a)
127 {
128   if (a->width == -1)
129     return tower_count (&a->tower);
130
131   return 600;
132 }
133
134 glong
135 psppire_axis_pixel_start (PsppireAxis *a, gint unit)
136 {
137   if ( a->width == -1 )
138     {
139       const struct tower_node *node;
140
141       if ( unit >= tower_count (&a->tower))
142         return tower_height (&a->tower);
143
144       node = tower_get (&a->tower, unit);
145
146       return  tower_node_get_level (node);
147     }
148
149   return a->width * unit;
150 }
151
152 gint
153 psppire_axis_get_unit_at_pixel (PsppireAxis *a, glong pixel)
154 {
155   if (a->width == -1)
156     {
157       const struct tower_node *node;
158       unsigned long int node_start;
159
160       if (pixel >= tower_height (&a->tower))
161         return tower_count (&a->tower);
162
163       node = tower_lookup (&a->tower, pixel, &node_start);
164
165       return tower_node_get_index (node);
166     }
167
168   return pixel / a->width;
169 }
170
171 void
172 psppire_axis_append (PsppireAxis *a, gint width)
173 {
174   struct tower_node *new = g_slice_alloc0 (sizeof *new);
175   tower_insert (&a->tower, width, new, NULL);
176 }
177
178
179
180
181