1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2013 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.
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
31 #include "psppire-spreadsheet-model.h"
32 #include "data/spreadsheet-reader.h"
35 static void psppire_spreadsheet_model_init (PsppireSpreadsheetModel *
37 static void psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass
40 static void psppire_spreadsheet_model_finalize (GObject * object);
41 static void psppire_spreadsheet_model_dispose (GObject * object);
43 static GObjectClass *parent_class = NULL;
46 static void spreadsheet_tree_model_init (GtkTreeModelIface * iface);
50 psppire_spreadsheet_model_get_type (void)
52 static GType object_type = 0;
56 static const GTypeInfo spreadsheet_model_info = {
57 sizeof (PsppireSpreadsheetModelClass),
59 NULL, /* base_finalize */
60 (GClassInitFunc) psppire_spreadsheet_model_class_init,
61 NULL, /* class_finalize */
62 NULL, /* class_data */
63 sizeof (PsppireSpreadsheetModel),
65 (GInstanceInitFunc) psppire_spreadsheet_model_init,
68 static const GInterfaceInfo tree_model_info = {
69 (GInterfaceInitFunc) spreadsheet_tree_model_init,
74 object_type = g_type_register_static (G_TYPE_OBJECT,
75 "PsppireSpreadsheetModel",
76 &spreadsheet_model_info, 0);
78 g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
95 psppire_spreadsheet_model_set_property (GObject * object,
100 PsppireSpreadsheetModel *spreadsheetModel =
101 PSPPIRE_SPREADSHEET_MODEL (object);
105 case PROP_SPREADSHEET:
106 spreadsheetModel->spreadsheet = g_value_get_pointer (value);
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 psppire_spreadsheet_model_dispose (GObject * object)
121 psppire_spreadsheet_model_finalize (GObject * object)
123 // PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object);
127 psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass * class)
129 GObjectClass *object_class;
131 GParamSpec *spreadsheet_spec = g_param_spec_pointer ("spreadsheet",
133 "The spreadsheet that this model represents",
134 G_PARAM_CONSTRUCT_ONLY
137 parent_class = g_type_class_peek_parent (class);
138 object_class = (GObjectClass *) class;
140 object_class->set_property = psppire_spreadsheet_model_set_property;
142 g_object_class_install_property (object_class,
143 PROP_SPREADSHEET, spreadsheet_spec);
145 object_class->finalize = psppire_spreadsheet_model_finalize;
146 object_class->dispose = psppire_spreadsheet_model_dispose;
151 psppire_spreadsheet_model_init (PsppireSpreadsheetModel * spreadsheetModel)
153 spreadsheetModel->dispose_has_run = FALSE;
154 spreadsheetModel->stamp = g_random_int ();
159 psppire_spreadsheet_model_new (struct spreadsheet *sp)
161 return g_object_new (psppire_spreadsheet_model_get_type (),
162 "spreadsheet", sp, NULL);
169 tree_model_n_columns (GtkTreeModel * model)
171 return PSPPIRE_SPREADSHEET_MODEL_N_COLS;
174 static GtkTreeModelFlags
175 tree_model_get_flags (GtkTreeModel * model)
177 g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model),
178 (GtkTreeModelFlags) 0);
180 return GTK_TREE_MODEL_LIST_ONLY;
184 tree_model_column_type (GtkTreeModel * model, gint index)
186 g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model), (GType) 0);
187 g_return_val_if_fail (index < PSPPIRE_SPREADSHEET_MODEL_N_COLS, (GType) 0);
189 return G_TYPE_STRING;
194 tree_model_get_iter (GtkTreeModel * model, GtkTreeIter * iter,
197 PsppireSpreadsheetModel *spreadsheetModel =
198 PSPPIRE_SPREADSHEET_MODEL (model);
199 gint *indices, depth;
202 g_return_val_if_fail (path, FALSE);
204 depth = gtk_tree_path_get_depth (path);
206 g_return_val_if_fail (depth == 1, FALSE);
208 indices = gtk_tree_path_get_indices (path);
212 iter->stamp = spreadsheetModel->stamp;
213 iter->user_data = (gpointer) n;
219 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
221 PsppireSpreadsheetModel *spreadsheetModel =
222 PSPPIRE_SPREADSHEET_MODEL (model);
223 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, FALSE);
228 if ((gint) iter->user_data >= spreadsheetModel->spreadsheet->n_sheets - 1)
230 iter->user_data = NULL;
242 tree_model_get_value (GtkTreeModel * model, GtkTreeIter * iter,
243 gint column, GValue * value)
245 PsppireSpreadsheetModel *spreadsheetModel =
246 PSPPIRE_SPREADSHEET_MODEL (model);
247 g_return_if_fail (column < PSPPIRE_SPREADSHEET_MODEL_N_COLS);
248 g_return_if_fail (iter->stamp == spreadsheetModel->stamp);
250 g_value_init (value, G_TYPE_STRING);
253 case PSPPIRE_SPREADSHEET_MODEL_COL_NAME:
256 spreadsheet_get_sheet_name (spreadsheetModel->spreadsheet,
257 (gint) iter->user_data);
259 g_value_set_string (value, x);
262 case PSPPIRE_SPREADSHEET_MODEL_COL_RANGE:
265 spreadsheet_get_sheet_range (spreadsheetModel->spreadsheet,
266 (gint) iter->user_data);
268 g_value_set_string (value, x ? x : _("(empty)"));
273 g_error ("%s:%d Invalid column in spreadsheet model",
280 tree_model_nth_child (GtkTreeModel * model, GtkTreeIter * iter,
281 GtkTreeIter * parent, gint n)
283 PsppireSpreadsheetModel *spreadsheetModel =
284 PSPPIRE_SPREADSHEET_MODEL (model);
289 if (n >= spreadsheetModel->spreadsheet->n_sheets)
292 iter->stamp = spreadsheetModel->stamp;
293 iter->user_data = (gpointer) n;
299 tree_model_n_children (GtkTreeModel * model, GtkTreeIter * iter)
301 PsppireSpreadsheetModel *spreadsheetModel =
302 PSPPIRE_SPREADSHEET_MODEL (model);
305 return spreadsheetModel->spreadsheet->n_sheets;
311 tree_model_iter_has_child (GtkTreeModel *tree_model, GtkTreeIter *iter)
317 tree_model_get_path (GtkTreeModel * model, GtkTreeIter * iter)
319 PsppireSpreadsheetModel *spreadsheetModel =
320 PSPPIRE_SPREADSHEET_MODEL (model);
322 gint index = (gint) iter->user_data;
324 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, NULL);
326 path = gtk_tree_path_new ();
328 gtk_tree_path_append_index (path, index);
335 tree_model_children (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *parent)
337 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
342 iter->stamp = spreadsheetModel->stamp;
351 spreadsheet_tree_model_init (GtkTreeModelIface * iface)
353 iface->get_flags = tree_model_get_flags;
354 iface->get_n_columns = tree_model_n_columns;
355 iface->get_column_type = tree_model_column_type;
356 iface->get_iter = tree_model_get_iter;
357 iface->iter_next = tree_model_iter_next;
358 iface->get_value = tree_model_get_value;
360 iface->iter_children = tree_model_children;
361 iface->iter_parent = NULL;
363 iface->get_path = tree_model_get_path;
364 iface->iter_has_child = tree_model_iter_has_child;
365 iface->iter_n_children = tree_model_n_children;
366 iface->iter_nth_child = tree_model_nth_child;