Removed the visibility predicate from the row/column models.
[pspp-builds.git] / lib / gtksheet / gsheet-row-iface.c
1 /* GSheetRow --- an abstract model of the row geometry of a
2  * GSheet widget.
3  * Copyright (C) 2006 Free Software Foundation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 #include <config.h>
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <glib/gprintf.h>
25 #include <gobject/gvaluecollector.h>
26 #include "gsheet-row-iface.h"
27 #include "gtkextra-marshal.h"
28
29
30 enum {
31   ROWS_CHANGED,
32   LAST_SIGNAL
33 };
34
35 static guint sheet_row_signals[LAST_SIGNAL];
36
37
38
39 static void g_sheet_row_base_init (gpointer g_class);
40
41
42 GType
43 g_sheet_row_get_type (void)
44 {
45   static GType sheet_row_type = 0;
46
47   if (! sheet_row_type)
48     {
49       static const GTypeInfo sheet_row_info =
50
51       {
52         sizeof (GSheetRowIface), /* class_size */
53         g_sheet_row_base_init,   /* base_init */
54         NULL,           /* base_finalize */
55         NULL,
56         NULL,           /* class_finalize */
57         NULL,           /* class_data */
58         0,
59         0,              /* n_preallocs */
60         NULL
61       };
62
63       sheet_row_type =
64         g_type_register_static (G_TYPE_INTERFACE, "GSheetRow",
65                                 &sheet_row_info, 0);
66
67       g_type_interface_add_prerequisite (sheet_row_type, G_TYPE_OBJECT);
68     }
69
70   return sheet_row_type;
71 }
72
73
74 static GtkSheetButton default_button;
75
76 static void
77 g_sheet_row_base_init (gpointer g_class)
78 {
79   static gboolean initialized = FALSE;
80
81   if (! initialized)
82     {
83
84       sheet_row_signals[ROWS_CHANGED] =
85         g_signal_new ("rows_changed",
86                       G_TYPE_SHEET_ROW,
87                       G_SIGNAL_RUN_LAST,
88                       G_STRUCT_OFFSET (GSheetRowIface, rows_changed),
89                       NULL, NULL,
90                       gtkextra_VOID__INT_INT,
91                       G_TYPE_NONE, 2,
92                       G_TYPE_INT,
93                       G_TYPE_INT);
94
95
96       default_button.state = GTK_STATE_NORMAL;
97       default_button.label = NULL;
98       default_button.label_visible = TRUE;
99       default_button.justification = GTK_JUSTIFY_FILL;
100
101       initialized = TRUE;
102     }
103 }
104
105 void
106 g_sheet_row_set_height (GSheetRow *row_geo,
107                         glong row, gint size)
108 {
109   g_return_if_fail (G_IS_SHEET_ROW (row_geo));
110
111   if ((G_SHEET_ROW_GET_IFACE (row_geo)->set_height) )
112     (G_SHEET_ROW_GET_IFACE (row_geo)->set_height) (row_geo, row,
113                                                         size);
114 }
115
116
117 gint
118 g_sheet_row_get_height     (const GSheetRow *row_geo,
119                             glong row)
120 {
121   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), -1);
122
123   g_assert (G_SHEET_ROW_GET_IFACE (row_geo)->get_height);
124
125   return (G_SHEET_ROW_GET_IFACE (row_geo)->get_height) (row_geo, row);
126 }
127
128
129
130 gboolean
131 g_sheet_row_get_sensitivity (const GSheetRow *row_geo,
132                             glong row)
133 {
134   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), FALSE);
135
136   g_assert (G_SHEET_ROW_GET_IFACE (row_geo)->get_sensitivity);
137
138   return (G_SHEET_ROW_GET_IFACE (row_geo)->get_sensitivity) (row_geo,
139                                                              row);
140 }
141
142
143 GtkSheetButton *
144 g_sheet_row_get_button (const GSheetRow *row_geo,
145                        glong row)
146 {
147   GtkSheetButton *button  = gtk_sheet_button_new ();
148
149   GSheetRowIface *iface = G_SHEET_ROW_GET_IFACE (row_geo);
150
151   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), FALSE);
152
153   if ( iface->get_button_label)
154     button->label = iface->get_button_label (row_geo, row);
155
156   return button;
157 }
158
159 gchar *
160 g_sheet_row_get_subtitle (const GSheetRow *row_geo, glong row)
161 {
162   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), NULL);
163
164   if ( ! G_SHEET_ROW_GET_IFACE (row_geo)->get_subtitle )
165     return NULL;
166
167   return (G_SHEET_ROW_GET_IFACE (row_geo)->get_subtitle) (row_geo, row);
168 }
169
170
171
172
173 glong
174 g_sheet_row_get_row_count (const GSheetRow *geo)
175 {
176   g_return_val_if_fail (G_IS_SHEET_ROW (geo), -1);
177
178   g_assert  ( G_SHEET_ROW_GET_IFACE (geo)->get_row_count);
179
180   return (G_SHEET_ROW_GET_IFACE (geo)->get_row_count) (geo);
181 }
182
183 /**
184  * g_sheet_row_start_pixel:
185  * @geo: the row model
186  * @row: the row number
187  * @sheet: pointer to the sheet
188  *
189  * Returns the top y pixel for ROW.
190  * ROW may point to the row BELOW the last row, in which case it should
191  * return the position where it would start if it existed.
192  * Instances may override this method in order to achieve time and/or memory
193  * optmisation.
194  *
195  * Returns: the y coordinate of the top of the row.
196  */
197
198 gint
199 g_sheet_row_start_pixel (const GSheetRow *geo, glong row)
200 {
201   gint i;
202   gint start_pixel = 0;
203
204   g_return_val_if_fail (G_IS_SHEET_ROW (geo), -1);
205   g_return_val_if_fail (row >= 0, -1);
206   g_return_val_if_fail (row <= g_sheet_row_get_row_count (geo), -1);
207
208   if ( G_SHEET_ROW_GET_IFACE (geo)->top_ypixel)
209     return (G_SHEET_ROW_GET_IFACE (geo)->top_ypixel)(geo, row);
210
211   for (i = 0; i < row; ++i)
212     {
213       start_pixel += g_sheet_row_get_height (geo, i);
214     }
215
216   return start_pixel;
217 }
218
219
220 glong
221 g_sheet_row_pixel_to_row (const GSheetRow *geo, gint pixel)
222 {
223   gint i, cy;
224   g_return_val_if_fail (G_IS_SHEET_ROW (geo), -1);
225   g_return_val_if_fail (pixel >= 0, -1) ;
226
227   if ( G_SHEET_ROW_GET_IFACE (geo)->pixel_to_row)
228     return (G_SHEET_ROW_GET_IFACE (geo)->pixel_to_row)(geo, pixel);
229
230   cy = 0;
231   for (i = 0; i < g_sheet_row_get_row_count (geo); ++i )
232     {
233       if (pixel >= cy  &&
234           pixel <= (cy + g_sheet_row_get_height (geo, i)))
235         return i;
236
237       cy += g_sheet_row_get_height (geo, i);
238     }
239
240   /* no match */
241   return g_sheet_row_get_row_count (geo) - 1;
242 }
243
244
245
246 void
247 g_sheet_row_rows_deleted (GSheetRow *geo,
248                                  glong first, glong n_rows)
249 {
250   g_return_if_fail (G_IS_SHEET_ROW (geo));
251
252   g_signal_emit (geo, sheet_row_signals[ROWS_CHANGED], 0,
253                  first, n_rows);
254 }