Start of new branch involving major rewrite of lib/gtksheet/*
[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_visibility(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_visibility);
137
138   return (G_SHEET_ROW_GET_IFACE (row_geo)->get_visibility) (row_geo,
139                                                                   row);
140
141 }
142
143 gboolean
144 g_sheet_row_get_sensitivity(const GSheetRow *row_geo,
145                             glong row)
146 {
147   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), FALSE);
148
149   g_assert (G_SHEET_ROW_GET_IFACE (row_geo)->get_sensitivity);
150
151   return (G_SHEET_ROW_GET_IFACE (row_geo)->get_sensitivity) (row_geo,
152                                                              row);
153
154 }
155
156
157 GtkSheetButton *
158 g_sheet_row_get_button(const GSheetRow *row_geo,
159                        glong row)
160 {
161   GtkSheetButton *button  = gtk_sheet_button_new();
162
163   GSheetRowIface *iface = G_SHEET_ROW_GET_IFACE (row_geo);
164
165   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), FALSE);
166
167   if ( iface->get_button_label)
168     button->label = iface->get_button_label(row_geo, row);
169
170   return button;
171 }
172
173 gchar *
174 g_sheet_row_get_subtitle (const GSheetRow *row_geo, glong row)
175 {
176   g_return_val_if_fail (G_IS_SHEET_ROW (row_geo), NULL);
177
178   if ( ! G_SHEET_ROW_GET_IFACE (row_geo)->get_subtitle )
179     return NULL;
180
181   return (G_SHEET_ROW_GET_IFACE (row_geo)->get_subtitle) (row_geo, row);
182 }
183
184
185
186
187 glong
188 g_sheet_row_get_row_count (const GSheetRow *geo)
189 {
190   g_return_val_if_fail (G_IS_SHEET_ROW (geo), -1);
191
192   g_assert  ( G_SHEET_ROW_GET_IFACE (geo)->get_row_count);
193
194   return (G_SHEET_ROW_GET_IFACE (geo)->get_row_count) (geo);
195 }
196
197 /**
198  * g_sheet_row_start_pixel:
199  * @geo: the row model
200  * @row: the row number
201  * @sheet: pointer to the sheet
202  *
203  * Returns the top y pixel for ROW.
204  * Instances may override this method in order to achieve time and/or memory
205  * optmisation.
206  *
207  * Returns: the y coordinate of the top of the row.
208  */
209
210 gint
211 g_sheet_row_start_pixel(const GSheetRow *geo, glong row)
212 {
213   gint i;
214   gint start_pixel = 0;
215
216   g_return_val_if_fail (G_IS_SHEET_ROW (geo), -1);
217   g_return_val_if_fail (row >= 0, -1);
218   g_return_val_if_fail (row <
219                         g_sheet_row_get_row_count(geo),-1);
220
221   if ( G_SHEET_ROW_GET_IFACE(geo)->top_ypixel)
222     return (G_SHEET_ROW_GET_IFACE(geo)->top_ypixel)(geo, row);
223
224   for ( i = 0 ; i < row ; ++i )
225     {
226       if ( g_sheet_row_get_visibility(geo, i))
227         start_pixel += g_sheet_row_get_height(geo, i);
228     }
229
230   return start_pixel;
231 }
232
233
234 glong
235 g_sheet_row_pixel_to_row (const GSheetRow *geo, gint pixel)
236 {
237   gint i, cy;
238   g_return_val_if_fail (G_IS_SHEET_ROW (geo), -1);
239   g_return_val_if_fail (pixel >= 0, -1) ;
240
241   if ( G_SHEET_ROW_GET_IFACE(geo)->pixel_to_row)
242     return (G_SHEET_ROW_GET_IFACE(geo)->pixel_to_row)(geo, pixel);
243
244   cy = 0;
245   for (i = 0; i < g_sheet_row_get_row_count (geo); ++i )
246     {
247       if (pixel >= cy  &&
248           pixel <= (cy + g_sheet_row_get_height (geo, i)) &&
249           g_sheet_row_get_visibility (geo, i))
250         return i;
251
252       if(g_sheet_row_get_visibility (geo, i))
253         cy += g_sheet_row_get_height (geo, i);
254     }
255
256   /* no match */
257   return g_sheet_row_get_row_count (geo) - 1;
258 }
259
260
261
262 void
263 g_sheet_row_rows_deleted (GSheetRow *geo,
264                                  glong first, glong n_rows)
265 {
266   g_return_if_fail (G_IS_SHEET_ROW (geo));
267
268   g_signal_emit (geo, sheet_row_signals[ROWS_CHANGED], 0,
269                  first, n_rows);
270 }