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 =
233 PSPPIRE_SPREADSHEET_MODEL (model);
234 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, FALSE);
239 if ((intptr_t) iter->user_data >= spreadsheetModel->spreadsheet->n_sheets - 1)
241 iter->user_data = NULL;
253 tree_model_get_value (GtkTreeModel * model, GtkTreeIter * iter,
254 gint column, GValue * value)
256 PsppireSpreadsheetModel *spreadsheetModel =
257 PSPPIRE_SPREADSHEET_MODEL (model);
258 g_return_if_fail (column < PSPPIRE_SPREADSHEET_MODEL_N_COLS);
259 g_return_if_fail (iter->stamp == spreadsheetModel->stamp);
261 g_value_init (value, G_TYPE_STRING);
264 case PSPPIRE_SPREADSHEET_MODEL_COL_NAME:
267 spreadsheet_get_sheet_name (spreadsheetModel->spreadsheet,
268 (intptr_t) iter->user_data);
270 g_value_set_string (value, x);
273 case PSPPIRE_SPREADSHEET_MODEL_COL_RANGE:
276 spreadsheet_get_sheet_range (spreadsheetModel->spreadsheet,
277 (intptr_t) iter->user_data);
279 g_value_set_string (value, x ? x : _("(empty)"));
284 g_error ("%s:%d Invalid column in spreadsheet model",
291 tree_model_nth_child (GtkTreeModel * model, GtkTreeIter * iter,
292 GtkTreeIter * parent, gint n)
294 PsppireSpreadsheetModel *spreadsheetModel =
295 PSPPIRE_SPREADSHEET_MODEL (model);
300 if (n >= spreadsheetModel->spreadsheet->n_sheets)
303 iter->stamp = spreadsheetModel->stamp;
304 iter->user_data = (gpointer) (intptr_t) n;
310 tree_model_n_children (GtkTreeModel * model, GtkTreeIter * iter)
312 PsppireSpreadsheetModel *spreadsheetModel =
313 PSPPIRE_SPREADSHEET_MODEL (model);
316 return spreadsheetModel->spreadsheet->n_sheets;
322 tree_model_iter_has_child (GtkTreeModel *tree_model, GtkTreeIter *iter)
328 tree_model_get_path (GtkTreeModel * model, GtkTreeIter * iter)
330 PsppireSpreadsheetModel *spreadsheetModel =
331 PSPPIRE_SPREADSHEET_MODEL (model);
333 gint index = (intptr_t) iter->user_data;
335 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, NULL);
337 path = gtk_tree_path_new ();
339 gtk_tree_path_append_index (path, index);
346 tree_model_children (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *parent)
348 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
353 iter->stamp = spreadsheetModel->stamp;
362 spreadsheet_tree_model_init (GtkTreeModelIface * iface)
364 iface->get_flags = tree_model_get_flags;
365 iface->get_n_columns = tree_model_n_columns;
366 iface->get_column_type = tree_model_column_type;
367 iface->get_iter = tree_model_get_iter;
368 iface->iter_next = tree_model_iter_next;
369 iface->get_value = tree_model_get_value;
371 iface->iter_children = tree_model_children;
372 iface->iter_parent = NULL;
374 iface->get_path = tree_model_get_path;
375 iface->iter_has_child = tree_model_iter_has_child;
376 iface->iter_n_children = tree_model_n_children;
377 iface->iter_nth_child = tree_model_nth_child;