1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2013, 2014 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/>. */
17 /* This file implements a GtkTreeModel. It allows GtkComboBox and
18 GtkTreeView to display the names and non-empty cell ranges of the
19 sheets aka "Tables" of spreadsheet files.
20 It doesn't take any notice of the spreadsheet data itself.
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
33 #include "psppire-spreadsheet-model.h"
34 #include "data/spreadsheet-reader.h"
37 static void psppire_spreadsheet_model_init (PsppireSpreadsheetModel *
39 static void psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass
42 static void psppire_spreadsheet_model_finalize (GObject * object);
43 static void psppire_spreadsheet_model_dispose (GObject * object);
45 static GObjectClass *parent_class = NULL;
48 static void spreadsheet_tree_model_init (GtkTreeModelIface * iface);
52 psppire_spreadsheet_model_get_type (void)
54 static GType object_type = 0;
58 static const GTypeInfo spreadsheet_model_info = {
59 sizeof (PsppireSpreadsheetModelClass),
61 NULL, /* base_finalize */
62 (GClassInitFunc) psppire_spreadsheet_model_class_init,
63 NULL, /* class_finalize */
64 NULL, /* class_data */
65 sizeof (PsppireSpreadsheetModel),
67 (GInstanceInitFunc) psppire_spreadsheet_model_init,
70 static const GInterfaceInfo tree_model_info = {
71 (GInterfaceInitFunc) spreadsheet_tree_model_init,
76 object_type = g_type_register_static (G_TYPE_OBJECT,
77 "PsppireSpreadsheetModel",
78 &spreadsheet_model_info, 0);
80 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
97 psppire_spreadsheet_model_set_property (GObject * object,
102 PsppireSpreadsheetModel *spreadsheetModel =
103 PSPPIRE_SPREADSHEET_MODEL (object);
107 case PROP_SPREADSHEET:
108 spreadsheetModel->spreadsheet = g_value_get_pointer (value);
109 spreadsheet_ref (spreadsheetModel->spreadsheet);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 psppire_spreadsheet_model_dispose (GObject * object)
121 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object);
123 if (!spreadsheetModel->dispose_has_run)
125 spreadsheet_unref (spreadsheetModel->spreadsheet);
127 spreadsheetModel->dispose_has_run = TRUE;
132 psppire_spreadsheet_model_finalize (GObject * object)
134 // PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object);
138 psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass * class)
140 GObjectClass *object_class;
142 GParamSpec *spreadsheet_spec = g_param_spec_pointer ("spreadsheet",
144 "The spreadsheet that this model represents",
145 G_PARAM_CONSTRUCT_ONLY
148 parent_class = g_type_class_peek_parent (class);
149 object_class = (GObjectClass *) class;
151 object_class->set_property = psppire_spreadsheet_model_set_property;
153 g_object_class_install_property (object_class,
154 PROP_SPREADSHEET, spreadsheet_spec);
156 object_class->finalize = psppire_spreadsheet_model_finalize;
157 object_class->dispose = psppire_spreadsheet_model_dispose;
162 psppire_spreadsheet_model_init (PsppireSpreadsheetModel * spreadsheetModel)
164 spreadsheetModel->dispose_has_run = FALSE;
165 spreadsheetModel->stamp = g_random_int ();
170 psppire_spreadsheet_model_new (struct spreadsheet *sp)
172 return g_object_new (psppire_spreadsheet_model_get_type (),
173 "spreadsheet", sp, NULL);
180 tree_model_n_columns (GtkTreeModel * model)
182 return PSPPIRE_SPREADSHEET_MODEL_N_COLS;
185 static GtkTreeModelFlags
186 tree_model_get_flags (GtkTreeModel * model)
188 g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model),
189 (GtkTreeModelFlags) 0);
191 return GTK_TREE_MODEL_LIST_ONLY;
195 tree_model_column_type (GtkTreeModel * model, gint index)
197 g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model), (GType) 0);
198 g_return_val_if_fail (index < PSPPIRE_SPREADSHEET_MODEL_N_COLS, (GType) 0);
200 return G_TYPE_STRING;
205 tree_model_get_iter (GtkTreeModel * model, GtkTreeIter * iter,
208 PsppireSpreadsheetModel *spreadsheetModel =
209 PSPPIRE_SPREADSHEET_MODEL (model);
210 gint *indices, depth;
213 g_return_val_if_fail (path, FALSE);
215 depth = gtk_tree_path_get_depth (path);
217 g_return_val_if_fail (depth == 1, FALSE);
219 indices = gtk_tree_path_get_indices (path);
223 iter->stamp = spreadsheetModel->stamp;
224 iter->user_data = (gpointer) (intptr_t) n;
230 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
232 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
234 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, FALSE);
236 if ((intptr_t) iter->user_data >= spreadsheetModel->spreadsheet->n_sheets - 1)
238 iter->user_data = NULL;
250 tree_model_get_value (GtkTreeModel * model, GtkTreeIter * iter,
251 gint column, GValue * value)
253 PsppireSpreadsheetModel *spreadsheetModel =
254 PSPPIRE_SPREADSHEET_MODEL (model);
255 g_return_if_fail (column < PSPPIRE_SPREADSHEET_MODEL_N_COLS);
256 g_return_if_fail (iter->stamp == spreadsheetModel->stamp);
258 g_value_init (value, G_TYPE_STRING);
261 case PSPPIRE_SPREADSHEET_MODEL_COL_NAME:
264 spreadsheet_get_sheet_name (spreadsheetModel->spreadsheet,
265 (intptr_t) iter->user_data);
267 g_value_set_string (value, x);
270 case PSPPIRE_SPREADSHEET_MODEL_COL_RANGE:
273 spreadsheet_get_sheet_range (spreadsheetModel->spreadsheet,
274 (intptr_t) iter->user_data);
276 g_value_set_string (value, x ? x : _("(empty)"));
281 g_error ("%s:%d Invalid column in spreadsheet model",
288 tree_model_nth_child (GtkTreeModel * model, GtkTreeIter * iter,
289 GtkTreeIter * parent, gint n)
291 PsppireSpreadsheetModel *spreadsheetModel =
292 PSPPIRE_SPREADSHEET_MODEL (model);
297 if (n >= spreadsheetModel->spreadsheet->n_sheets)
300 iter->stamp = spreadsheetModel->stamp;
301 iter->user_data = (gpointer) (intptr_t) n;
307 tree_model_n_children (GtkTreeModel * model, GtkTreeIter * iter)
309 PsppireSpreadsheetModel *spreadsheetModel =
310 PSPPIRE_SPREADSHEET_MODEL (model);
313 return spreadsheetModel->spreadsheet->n_sheets;
319 tree_model_iter_has_child (GtkTreeModel *tree_model, GtkTreeIter *iter)
325 tree_model_get_path (GtkTreeModel * model, GtkTreeIter * iter)
327 PsppireSpreadsheetModel *spreadsheetModel =
328 PSPPIRE_SPREADSHEET_MODEL (model);
330 gint index = (intptr_t) iter->user_data;
332 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, NULL);
334 path = gtk_tree_path_new ();
336 gtk_tree_path_append_index (path, index);
343 tree_model_children (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *parent)
345 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
350 iter->stamp = spreadsheetModel->stamp;
359 spreadsheet_tree_model_init (GtkTreeModelIface * iface)
361 iface->get_flags = tree_model_get_flags;
362 iface->get_n_columns = tree_model_n_columns;
363 iface->get_column_type = tree_model_column_type;
364 iface->get_iter = tree_model_get_iter;
365 iface->iter_next = tree_model_iter_next;
366 iface->get_value = tree_model_get_value;
368 iface->iter_children = tree_model_children;
369 iface->iter_parent = NULL;
371 iface->get_path = tree_model_get_path;
372 iface->iter_has_child = tree_model_iter_has_child;
373 iface->iter_n_children = tree_model_n_children;
374 iface->iter_nth_child = tree_model_nth_child;