X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-spreadsheet-model.c;h=26c803f3a670a90d3923db358422e3a78c209634;hb=65d602ed236d685ffec00ad1552a193cf47b2e4d;hp=5c004a8446f90999e1ca17e1ab8570d1c8e3a407;hpb=920ccda5e43b81b2118caf37784f6edaa568e246;p=pspp diff --git a/src/ui/gui/psppire-spreadsheet-model.c b/src/ui/gui/psppire-spreadsheet-model.c index 5c004a8446..26c803f3a6 100644 --- a/src/ui/gui/psppire-spreadsheet-model.c +++ b/src/ui/gui/psppire-spreadsheet-model.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2013 Free Software Foundation + Copyright (C) 2013, 2014 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,10 +14,17 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +/* This file implements a GtkTreeModel. It allows GtkComboBox and + GtkTreeView to display the names and non-empty cell ranges of the + sheets aka "Tables" of spreadsheet files. + It doesn't take any notice of the spreadsheet data itself. +*/ #include #include +#include + #include #define _(msgid) gettext (msgid) #define N_(msgid) msgid @@ -26,57 +33,18 @@ #include "psppire-spreadsheet-model.h" #include "data/spreadsheet-reader.h" - -static void psppire_spreadsheet_model_init (PsppireSpreadsheetModel * - spreadsheetModel); -static void psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass - * class); - static void psppire_spreadsheet_model_finalize (GObject * object); static void psppire_spreadsheet_model_dispose (GObject * object); static GObjectClass *parent_class = NULL; - static void spreadsheet_tree_model_init (GtkTreeModelIface * iface); - -GType -psppire_spreadsheet_model_get_type (void) -{ - static GType object_type = 0; - - if (!object_type) - { - static const GTypeInfo spreadsheet_model_info = { - sizeof (PsppireSpreadsheetModelClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) psppire_spreadsheet_model_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (PsppireSpreadsheetModel), - 0, - (GInstanceInitFunc) psppire_spreadsheet_model_init, - }; - - static const GInterfaceInfo tree_model_info = { - (GInterfaceInitFunc) spreadsheet_tree_model_init, - NULL, - NULL - }; - - object_type = g_type_register_static (G_TYPE_OBJECT, - "PsppireSpreadsheetModel", - &spreadsheet_model_info, 0); - - g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL, - &tree_model_info); - } - - return object_type; -} - +G_DEFINE_TYPE_WITH_CODE (PsppireSpreadsheetModel,\ + psppire_spreadsheet_model,\ + G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL, + spreadsheet_tree_model_init)) /* Properties */ enum @@ -92,13 +60,18 @@ psppire_spreadsheet_model_set_property (GObject * object, const GValue * value, GParamSpec * pspec) { - PsppireSpreadsheetModel *spreadsheetModel = + PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object); switch (prop_id) { case PROP_SPREADSHEET: - spreadsheetModel->spreadsheet = g_value_get_pointer (value); + { + struct spreadsheet *old = spreadsheetModel->spreadsheet; + spreadsheetModel->spreadsheet = spreadsheet_ref (g_value_get_pointer (value)); + if (old) + spreadsheet_unref (old); + } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -110,6 +83,14 @@ psppire_spreadsheet_model_set_property (GObject * object, static void psppire_spreadsheet_model_dispose (GObject * object) { + PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object); + + if (!spreadsheetModel->dispose_has_run) + { + spreadsheet_unref (spreadsheetModel->spreadsheet); + + spreadsheetModel->dispose_has_run = TRUE; + } } static void @@ -121,7 +102,7 @@ psppire_spreadsheet_model_finalize (GObject * object) static void psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass * class) { - GObjectClass *object_class; + GObjectClass *object_class = G_OBJECT_CLASS (class); GParamSpec *spreadsheet_spec = g_param_spec_pointer ("spreadsheet", "Spreadsheet", @@ -130,7 +111,7 @@ psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass * class) | G_PARAM_WRITABLE); parent_class = g_type_class_peek_parent (class); - object_class = (GObjectClass *) class; + object_class->set_property = psppire_spreadsheet_model_set_property; @@ -181,6 +162,9 @@ tree_model_column_type (GtkTreeModel * model, gint index) g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model), (GType) 0); g_return_val_if_fail (index < PSPPIRE_SPREADSHEET_MODEL_N_COLS, (GType) 0); + if (index == PSPPIRE_SPREADSHEET_MODEL_COL_SHEET_ROWS) + return G_TYPE_UINT; + return G_TYPE_STRING; } @@ -205,7 +189,7 @@ tree_model_get_iter (GtkTreeModel * model, GtkTreeIter * iter, n = indices[0]; iter->stamp = spreadsheetModel->stamp; - iter->user_data = (gpointer) n; + iter->user_data = (gpointer) (intptr_t) n; return TRUE; } @@ -213,21 +197,19 @@ tree_model_get_iter (GtkTreeModel * model, GtkTreeIter * iter, static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter) { - PsppireSpreadsheetModel *spreadsheetModel = - PSPPIRE_SPREADSHEET_MODEL (model); + PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model); + g_assert (iter); g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, FALSE); - if (iter == NULL) - return FALSE; - - if ((gint) iter->user_data >= spreadsheetModel->spreadsheet->n_sheets - 1) + if ((intptr_t) iter->user_data >= + spreadsheet_get_sheet_n_sheets (spreadsheetModel->spreadsheet) - 1) { iter->user_data = NULL; iter->stamp = 0; return FALSE; } - iter->user_data++; + iter->user_data = GINT_TO_POINTER (GPOINTER_TO_INT (iter->user_data) + 1); return TRUE; } @@ -242,28 +224,49 @@ tree_model_get_value (GtkTreeModel * model, GtkTreeIter * iter, g_return_if_fail (column < PSPPIRE_SPREADSHEET_MODEL_N_COLS); g_return_if_fail (iter->stamp == spreadsheetModel->stamp); - g_value_init (value, G_TYPE_STRING); switch (column) { case PSPPIRE_SPREADSHEET_MODEL_COL_NAME: { + g_value_init (value, G_TYPE_STRING); const char *x = spreadsheet_get_sheet_name (spreadsheetModel->spreadsheet, - (gint) iter->user_data); - + (intptr_t) iter->user_data); + g_value_set_string (value, x); } break; case PSPPIRE_SPREADSHEET_MODEL_COL_RANGE: { + g_value_init (value, G_TYPE_STRING); char *x = spreadsheet_get_sheet_range (spreadsheetModel->spreadsheet, - (gint) iter->user_data); + (intptr_t) iter->user_data); g_value_set_string (value, x ? x : _("(empty)")); g_free (x); } break; + case PSPPIRE_SPREADSHEET_MODEL_COL_SHEET_ROWS: + { + g_value_init (value, G_TYPE_UINT); + unsigned int rows = + spreadsheet_get_sheet_n_rows (spreadsheetModel->spreadsheet, + (intptr_t) iter->user_data); + + g_value_set_uint (value, rows); + } + break; + case PSPPIRE_SPREADSHEET_MODEL_COL_SHEET_COLUMNS: + { + g_value_init (value, G_TYPE_UINT); + unsigned int columns = + spreadsheet_get_sheet_n_columns (spreadsheetModel->spreadsheet, + (intptr_t) iter->user_data); + + g_value_set_uint (value, columns); + } + break; default: g_error ("%s:%d Invalid column in spreadsheet model", __FILE__, __LINE__); @@ -281,11 +284,11 @@ tree_model_nth_child (GtkTreeModel * model, GtkTreeIter * iter, if (parent) return FALSE; - if (n >= spreadsheetModel->spreadsheet->n_sheets) + if (n >= spreadsheet_get_sheet_n_sheets (spreadsheetModel->spreadsheet)) return FALSE; iter->stamp = spreadsheetModel->stamp; - iter->user_data = (gpointer) n; + iter->user_data = (gpointer) (intptr_t) n; return TRUE; } @@ -297,7 +300,7 @@ tree_model_n_children (GtkTreeModel * model, GtkTreeIter * iter) PSPPIRE_SPREADSHEET_MODEL (model); if (iter == NULL) - return spreadsheetModel->spreadsheet->n_sheets; + return spreadsheet_get_sheet_n_sheets (spreadsheetModel->spreadsheet); return 0; } @@ -314,7 +317,7 @@ tree_model_get_path (GtkTreeModel * model, GtkTreeIter * iter) PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model); GtkTreePath *path; - gint index = (gint) iter->user_data; + gint index = (intptr_t) iter->user_data; g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, NULL); @@ -336,7 +339,7 @@ tree_model_children (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *parent iter->stamp = spreadsheetModel->stamp; iter->user_data = 0; - + return TRUE; }