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"
36 static void psppire_spreadsheet_model_finalize (GObject * object);
37 static void psppire_spreadsheet_model_dispose (GObject * object);
39 static GObjectClass *parent_class = NULL;
41 static void spreadsheet_tree_model_init (GtkTreeModelIface * iface);
43 G_DEFINE_TYPE_WITH_CODE (PsppireSpreadsheetModel,\
44 psppire_spreadsheet_model,\
46 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
47 spreadsheet_tree_model_init))
58 psppire_spreadsheet_model_set_property (GObject * object,
63 PsppireSpreadsheetModel *spreadsheetModel =
64 PSPPIRE_SPREADSHEET_MODEL (object);
68 case PROP_SPREADSHEET:
70 struct spreadsheet *old = spreadsheetModel->spreadsheet;
71 spreadsheetModel->spreadsheet = spreadsheet_ref (g_value_get_pointer (value));
73 spreadsheet_unref (old);
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 psppire_spreadsheet_model_dispose (GObject * object)
86 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object);
88 if (!spreadsheetModel->dispose_has_run)
90 spreadsheet_unref (spreadsheetModel->spreadsheet);
92 spreadsheetModel->dispose_has_run = TRUE;
97 psppire_spreadsheet_model_finalize (GObject * object)
99 // PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object);
103 psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass * class)
105 GObjectClass *object_class = G_OBJECT_CLASS (class);
107 GParamSpec *spreadsheet_spec = g_param_spec_pointer ("spreadsheet",
109 "The spreadsheet that this model represents",
110 G_PARAM_CONSTRUCT_ONLY
113 parent_class = g_type_class_peek_parent (class);
116 object_class->set_property = psppire_spreadsheet_model_set_property;
118 g_object_class_install_property (object_class,
119 PROP_SPREADSHEET, spreadsheet_spec);
121 object_class->finalize = psppire_spreadsheet_model_finalize;
122 object_class->dispose = psppire_spreadsheet_model_dispose;
127 psppire_spreadsheet_model_init (PsppireSpreadsheetModel * spreadsheetModel)
129 spreadsheetModel->dispose_has_run = FALSE;
130 spreadsheetModel->stamp = g_random_int ();
135 psppire_spreadsheet_model_new (struct spreadsheet *sp)
137 return g_object_new (psppire_spreadsheet_model_get_type (),
138 "spreadsheet", sp, NULL);
145 tree_model_n_columns (GtkTreeModel * model)
147 return PSPPIRE_SPREADSHEET_MODEL_N_COLS;
150 static GtkTreeModelFlags
151 tree_model_get_flags (GtkTreeModel * model)
153 g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model),
154 (GtkTreeModelFlags) 0);
156 return GTK_TREE_MODEL_LIST_ONLY;
160 tree_model_column_type (GtkTreeModel * model, gint index)
162 g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model), (GType) 0);
163 g_return_val_if_fail (index < PSPPIRE_SPREADSHEET_MODEL_N_COLS, (GType) 0);
165 if (index == PSPPIRE_SPREADSHEET_MODEL_COL_SHEET_ROWS)
168 return G_TYPE_STRING;
173 tree_model_get_iter (GtkTreeModel * model, GtkTreeIter * iter,
176 PsppireSpreadsheetModel *spreadsheetModel =
177 PSPPIRE_SPREADSHEET_MODEL (model);
178 gint *indices, depth;
181 g_return_val_if_fail (path, FALSE);
183 depth = gtk_tree_path_get_depth (path);
185 g_return_val_if_fail (depth == 1, FALSE);
187 indices = gtk_tree_path_get_indices (path);
191 iter->stamp = spreadsheetModel->stamp;
192 iter->user_data = (gpointer) (intptr_t) n;
198 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
200 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
202 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, FALSE);
204 if ((intptr_t) iter->user_data >=
205 spreadsheet_get_sheet_n_sheets (spreadsheetModel->spreadsheet) - 1)
207 iter->user_data = NULL;
212 iter->user_data = GINT_TO_POINTER (GPOINTER_TO_INT (iter->user_data) + 1);
219 tree_model_get_value (GtkTreeModel * model, GtkTreeIter * iter,
220 gint column, GValue * value)
222 PsppireSpreadsheetModel *spreadsheetModel =
223 PSPPIRE_SPREADSHEET_MODEL (model);
224 g_return_if_fail (column < PSPPIRE_SPREADSHEET_MODEL_N_COLS);
225 g_return_if_fail (iter->stamp == spreadsheetModel->stamp);
229 case PSPPIRE_SPREADSHEET_MODEL_COL_NAME:
231 g_value_init (value, G_TYPE_STRING);
233 spreadsheet_get_sheet_name (spreadsheetModel->spreadsheet,
234 (intptr_t) iter->user_data);
236 g_value_set_string (value, x);
239 case PSPPIRE_SPREADSHEET_MODEL_COL_RANGE:
241 g_value_init (value, G_TYPE_STRING);
243 spreadsheet_get_sheet_range (spreadsheetModel->spreadsheet,
244 (intptr_t) iter->user_data);
246 g_value_set_string (value, x ? x : _("(empty)"));
250 case PSPPIRE_SPREADSHEET_MODEL_COL_SHEET_ROWS:
252 g_value_init (value, G_TYPE_UINT);
254 spreadsheet_get_sheet_n_rows (spreadsheetModel->spreadsheet,
255 (intptr_t) iter->user_data);
257 g_value_set_uint (value, rows);
260 case PSPPIRE_SPREADSHEET_MODEL_COL_SHEET_COLUMNS:
262 g_value_init (value, G_TYPE_UINT);
263 unsigned int columns =
264 spreadsheet_get_sheet_n_columns (spreadsheetModel->spreadsheet,
265 (intptr_t) iter->user_data);
267 g_value_set_uint (value, columns);
271 g_error ("%s:%d Invalid column in spreadsheet model",
278 tree_model_nth_child (GtkTreeModel * model, GtkTreeIter * iter,
279 GtkTreeIter * parent, gint n)
281 PsppireSpreadsheetModel *spreadsheetModel =
282 PSPPIRE_SPREADSHEET_MODEL (model);
287 if (n >= spreadsheet_get_sheet_n_sheets (spreadsheetModel->spreadsheet))
290 iter->stamp = spreadsheetModel->stamp;
291 iter->user_data = (gpointer) (intptr_t) n;
297 tree_model_n_children (GtkTreeModel * model, GtkTreeIter * iter)
299 PsppireSpreadsheetModel *spreadsheetModel =
300 PSPPIRE_SPREADSHEET_MODEL (model);
303 return spreadsheet_get_sheet_n_sheets (spreadsheetModel->spreadsheet);
309 tree_model_iter_has_child (GtkTreeModel *tree_model, GtkTreeIter *iter)
315 tree_model_get_path (GtkTreeModel * model, GtkTreeIter * iter)
317 PsppireSpreadsheetModel *spreadsheetModel =
318 PSPPIRE_SPREADSHEET_MODEL (model);
320 gint index = (intptr_t) iter->user_data;
322 g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, NULL);
324 path = gtk_tree_path_new ();
326 gtk_tree_path_append_index (path, index);
333 tree_model_children (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *parent)
335 PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
340 iter->stamp = spreadsheetModel->stamp;
349 spreadsheet_tree_model_init (GtkTreeModelIface * iface)
351 iface->get_flags = tree_model_get_flags;
352 iface->get_n_columns = tree_model_n_columns;
353 iface->get_column_type = tree_model_column_type;
354 iface->get_iter = tree_model_get_iter;
355 iface->iter_next = tree_model_iter_next;
356 iface->get_value = tree_model_get_value;
358 iface->iter_children = tree_model_children;
359 iface->iter_parent = NULL;
361 iface->get_path = tree_model_get_path;
362 iface->iter_has_child = tree_model_iter_has_child;
363 iface->iter_n_children = tree_model_n_children;
364 iface->iter_nth_child = tree_model_nth_child;