1 /* PsppireSheetModel --- an abstract model for the PsppireSheet widget.
2 Copyright (C) 2006, 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/>. */
20 #include "psppire-sheetmodel.h"
21 #include <ui/gui/psppire-marshal.h>
32 static guint sheet_model_signals[LAST_SIGNAL] = { 0 };
35 static void psppire_sheet_model_base_init (gpointer g_class);
39 psppire_sheet_model_get_type (void)
41 static GType sheet_model_type = 0;
43 if (! sheet_model_type)
45 static const GTypeInfo sheet_model_info =
47 sizeof (PsppireSheetModelIface), /* class_size */
48 psppire_sheet_model_base_init, /* base_init */
49 NULL, /* base_finalize */
51 NULL, /* class_finalize */
52 NULL, /* class_data */
59 g_type_register_static (G_TYPE_INTERFACE, "PsppireSheetModel",
60 &sheet_model_info, 0);
62 g_type_interface_add_prerequisite (sheet_model_type, G_TYPE_OBJECT);
65 return sheet_model_type;
69 psppire_sheet_model_base_init (gpointer g_class)
71 static gboolean initialized = FALSE;
75 sheet_model_signals[RANGE_CHANGED] =
76 g_signal_new ("range_changed",
77 PSPPIRE_TYPE_SHEET_MODEL,
79 G_STRUCT_OFFSET (PsppireSheetModelIface, range_changed),
81 psppire_marshal_VOID__INT_INT_INT_INT,
90 sheet_model_signals[ROWS_INSERTED] =
91 g_signal_new ("rows_inserted",
92 PSPPIRE_TYPE_SHEET_MODEL,
94 G_STRUCT_OFFSET (PsppireSheetModelIface, rows_inserted),
96 psppire_marshal_VOID__INT_INT,
102 sheet_model_signals[ROWS_DELETED] =
103 g_signal_new ("rows_deleted",
104 PSPPIRE_TYPE_SHEET_MODEL,
106 G_STRUCT_OFFSET (PsppireSheetModelIface, rows_deleted),
108 psppire_marshal_VOID__INT_INT,
113 sheet_model_signals[COLUMNS_INSERTED] =
114 g_signal_new ("columns_inserted",
115 PSPPIRE_TYPE_SHEET_MODEL,
117 G_STRUCT_OFFSET (PsppireSheetModelIface, columns_inserted),
119 psppire_marshal_VOID__INT_INT,
125 sheet_model_signals[COLUMNS_DELETED] =
126 g_signal_new ("columns_deleted",
127 PSPPIRE_TYPE_SHEET_MODEL,
129 G_STRUCT_OFFSET (PsppireSheetModelIface, columns_deleted),
131 psppire_marshal_VOID__INT_INT,
143 * psppire_sheet_model_free_strings
144 * @sheet_model: A #PsppireSheetModel
146 * Returns: True if strings obtained with get_string should be freed by the
147 * sheet when no longer required.
150 psppire_sheet_model_free_strings (const PsppireSheetModel *sheet_model)
152 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model), FALSE);
154 return PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->free_strings;
159 * psppire_sheet_model_get_string:
160 * @sheet_model: A #PsppireSheetModel
161 * @row: The row of the cell to be retrieved.
162 * @column: The column of the cell to be retrieved.
164 * Retrieves the datum at location ROW, COLUMN in the form of a string.
165 * Returns: The string representation of the datum, or NULL on error.
168 psppire_sheet_model_get_string (const PsppireSheetModel *sheet_model,
169 glong row, glong column)
171 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model), 0);
173 g_assert (PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->get_string);
175 return (PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->get_string) (sheet_model, row, column);
179 * psppire_sheet_model_set_string
180 * @sheet_model: A #PsppireSheetModel
181 * @text: The text describing the datum to be set.
182 * @row: The row of the cell to be cleared.
183 * @column: The column of the cell to be cleared.
185 * Sets the datum at a location from a string.
186 * Returns: TRUE if the datum was changed, FALSE otherwise.
189 psppire_sheet_model_set_string (PsppireSheetModel *sheet_model,
191 glong row, glong column)
193 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model), FALSE);
195 g_assert (PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->set_string);
197 return PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->set_string (sheet_model,
204 * psppire_sheet_model_datum_clear:
205 * @sheet_model: A #PsppireSheetModel
206 * @row: The row of the cell to be cleared.
207 * @column: The column of the cell to be cleared.
209 * Called when the datum at a location is to be cleared.
210 * Returns: TRUE if the datum was cleared, FALSE otherwise.
213 psppire_sheet_model_datum_clear (PsppireSheetModel *sheet_model,
214 glong row, glong column)
216 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model), FALSE);
218 g_assert (PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->clear_datum);
220 return PSPPIRE_SHEET_MODEL_GET_IFACE (sheet_model)->clear_datum (sheet_model,
226 * psppire_sheet_model_range_changed:
227 * @sheet_model: A #PsppireSheetModel
228 * @range: The #PsppireSheetRange range of cells which have changed.
230 * Emits the "range_changed" signal on @sheet_model.
233 psppire_sheet_model_range_changed (PsppireSheetModel *sheet_model,
234 glong row0, glong col0,
235 glong rowi, glong coli)
237 g_return_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model));
239 g_signal_emit (sheet_model, sheet_model_signals[RANGE_CHANGED], 0,
240 row0, col0, rowi, coli);
247 * psppire_sheet_model_rows_inserted:
248 * @sheet_model: A #PsppireSheetModel
249 * @row: The row before which the new rows should be inserted.
250 * @n_rows: The number of rows to insert.
252 * Emits the "rows_inserted" signal on @sheet_model.
255 psppire_sheet_model_rows_inserted (PsppireSheetModel *sheet_model,
256 glong row, glong n_rows)
258 g_return_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model));
260 g_signal_emit (sheet_model, sheet_model_signals[ROWS_INSERTED], 0,
266 * psppire_sheet_model_columns_inserted:
267 * @sheet_model: A #PsppireSheetModel
268 * @column: The column before which the new columns should be inserted.
269 * @n_columns: The number of columns to insert.
271 * Emits the "columns_inserted" signal on @sheet_model.
274 psppire_sheet_model_columns_inserted (PsppireSheetModel *sheet_model,
275 glong column, glong n_columns)
277 g_return_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model));
279 g_signal_emit (sheet_model, sheet_model_signals[COLUMNS_INSERTED], 0,
287 * psppire_sheet_model_rows_deleted:
288 * @sheet_model: A #PsppireSheetModel
289 * @row: The first row to be deleted.
290 * @n_rows: The number of rows to delete.
292 * Emits the "rows_deleted" signal on @sheet_model.
295 psppire_sheet_model_rows_deleted (PsppireSheetModel *sheet_model,
296 glong row, glong n_rows)
298 g_return_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model));
300 g_signal_emit (sheet_model, sheet_model_signals[ROWS_DELETED], 0,
307 * psppire_sheet_model_columns_deleted:
308 * @sheet_model: A #PsppireSheetModel
309 * @column: The first column to be deleted.
310 * @n_columns: The number of columns to delete.
312 * Emits the "columns_deleted" signal on @sheet_model.
315 psppire_sheet_model_columns_deleted (PsppireSheetModel *sheet_model,
316 glong column, glong n_columns)
318 g_return_if_fail (PSPPIRE_IS_SHEET_MODEL (sheet_model));
320 g_signal_emit (sheet_model, sheet_model_signals[COLUMNS_DELETED], 0,
329 * psppire_sheet_model_is_editable:
330 * @sheet_model: A #PsppireSheetModel
332 * @column: The column
334 * Returns: TRUE if the cell is editable, FALSE otherwise
337 psppire_sheet_model_is_editable (const PsppireSheetModel *model,
338 glong row, glong column)
340 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), TRUE);
342 if ( ! PSPPIRE_SHEET_MODEL_GET_IFACE (model)->is_editable )
345 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->is_editable (model,
351 * psppire_sheet_model_get_foreground:
352 * @sheet_model: A #PsppireSheetModel
354 * @column: The column
356 * Returns the foreground colour of the cell at @row, @column
357 * The color is unallocated. It will be allocated by the viewing object.
360 psppire_sheet_model_get_foreground (const PsppireSheetModel *model,
361 glong row, glong column)
363 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
365 if ( ! PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_foreground )
368 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_foreground (model,
373 * psppire_sheet_model_get_background:
374 * @sheet_model: A #PsppireSheetModel
376 * @column: The column
378 * Returns the background colour of the cell at @row, @column
379 * The color is unallocated. It will be allocated by the viewing object.
382 psppire_sheet_model_get_background (const PsppireSheetModel *model,
383 glong row, glong column)
385 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
387 if ( ! PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_background )
390 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_background (model,
395 * psppire_sheet_model_get_justification:
396 * @sheet_model: A #PsppireSheetModel
398 * @column: The column
400 * Returns the justification of the cell at @row, @column
401 * Returns: the justification, or NULL on error.
403 const GtkJustification *
404 psppire_sheet_model_get_justification (const PsppireSheetModel *model,
405 glong row, glong column)
407 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
409 if ( ! PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_justification)
412 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_justification (model,
418 * psppire_sheet_model_get_column_count:
419 * @model: A #PsppireSheetModel
421 * Returns the total number of columns represented by the model
424 psppire_sheet_model_get_column_count (const PsppireSheetModel *model)
426 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), -1);
428 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_count (model);
432 * psppire_sheet_model_get_row_count:
433 * @model: A #PsppireSheetModel
435 * Returns the total number of rows represented by the model
438 psppire_sheet_model_get_row_count(const PsppireSheetModel *model)
440 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), -1);
442 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_count (model);
447 /* Column related functions */
449 psppire_sheet_model_get_column_sensitivity (const PsppireSheetModel *model, gint col)
451 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), FALSE);
453 if ( NULL == PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_sensitivity)
456 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_sensitivity (model, col);
461 psppire_sheet_model_get_column_subtitle (const PsppireSheetModel *model,
464 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
465 g_return_val_if_fail (col >= 0, NULL);
467 if ( NULL == PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_subtitle)
470 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_subtitle (model, col);
475 psppire_sheet_model_get_column_button (const PsppireSheetModel *model,
478 PsppireSheetButton *button = psppire_sheet_button_new ();
480 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
482 if ( PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_title)
483 button->label = PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_title (model, col);
485 button->overstruck = FALSE;
491 psppire_sheet_model_get_column_justification (const PsppireSheetModel *model,
494 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), GTK_JUSTIFY_LEFT);
496 if ( PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_justification)
497 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_column_justification (model, col);
499 return GTK_JUSTIFY_LEFT;
505 psppire_sheet_model_get_row_sensitivity (const PsppireSheetModel *model, gint row)
507 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), FALSE);
509 if ( NULL == PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_sensitivity)
512 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_sensitivity (model, row);
518 psppire_sheet_model_get_row_subtitle (const PsppireSheetModel *model,
521 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
523 if ( NULL == PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_subtitle)
526 return PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_subtitle (model, row);
531 psppire_sheet_model_get_row_button (const PsppireSheetModel *model,
534 PsppireSheetButton *button = psppire_sheet_button_new ();
536 g_return_val_if_fail (PSPPIRE_IS_SHEET_MODEL (model), NULL);
538 if ( PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_title)
540 PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_title (model, row);
542 if ( PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_overstrike)
544 PSPPIRE_SHEET_MODEL_GET_IFACE (model)->get_row_overstrike (model, row);