X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-data-store.c;h=45104f5cbb8ff519418fee88edf92e496e91652c;hb=4eb2e820b01d92ef662c741f5ac30f08bedd1de6;hp=5b40635b66606553f6545f342f56b5c67b6b63d8;hpb=1ce1429567835f83a712a8a76ab80db64360d742;p=pspp diff --git a/src/ui/gui/psppire-data-store.c b/src/ui/gui/psppire-data-store.c index 5b40635b66..f97b8eaf1c 100644 --- a/src/ui/gui/psppire-data-store.c +++ b/src/ui/gui/psppire-data-store.c @@ -1,12 +1,10 @@ -/* psppire-data-store.c - - PSPPIRE --- A Graphical User Interface for PSPP - Copyright (C) 2006 Free Software Foundation - Written by John Darrington +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2006, 2008, 2009, 2010, 2011, 2012, + 2013, 2016, 2017 Free Software Foundation - This program is free software; you can redistribute it and/or modify + 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 - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -15,56 +13,260 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include #include #include -#include +#include +#define _(msgid) gettext (msgid) +#define P_(msgid) msgid -#include -#include -#include +#include +#include +#include + +#include + +#include -#include "psppire-variable.h" #include "psppire-data-store.h" +#include #include "helper.h" #include #include #include #include +#include -#include -#include +#include -#define _(A) A -#define N_(A) A +#include "xalloc.h" +#include "xmalloca.h" +#include "value-variant.h" static void psppire_data_store_init (PsppireDataStore *data_store); static void psppire_data_store_class_init (PsppireDataStoreClass *class); -static void psppire_data_store_sheet_model_init (GSheetModelIface *iface); -static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface); + static void psppire_data_store_finalize (GObject *object); +static void psppire_data_store_dispose (GObject *object); + +static gboolean psppire_data_store_insert_case (PsppireDataStore *ds, + struct ccase *cc, + casenumber posn); -static const gchar *psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column); -static gboolean psppire_data_store_set_string(GSheetModel *model, - const gchar *text, gint row, gint column); +static gboolean psppire_data_store_data_in (PsppireDataStore *ds, + casenumber casenum, gint idx, + struct substring input, + const struct fmt_spec *fmt); -static gboolean psppire_data_store_clear_datum(GSheetModel *model, - gint row, gint column); +static GObjectClass *parent_class = NULL; -#define MIN_COLUMNS 10 +enum + { + ITEMS_CHANGED, + CASE_CHANGED, + n_SIGNALS + }; +static guint signals [n_SIGNALS]; -static GObjectClass *parent_class = NULL; +static gint +__tree_model_iter_n_children (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model); + + if (store->datasheet == NULL) + return 0; + + gint n = datasheet_get_n_rows (store->datasheet); + + return n; +} + +static GtkTreeModelFlags +__tree_model_get_flags (GtkTreeModel *model) +{ + g_return_val_if_fail (PSPPIRE_IS_DATA_STORE (model), (GtkTreeModelFlags) 0); + + return GTK_TREE_MODEL_LIST_ONLY; +} + +static gint +__tree_model_get_n_columns (GtkTreeModel *tree_model) +{ + PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model); + + return psppire_dict_get_var_cnt (store->dict); +} + + +static gboolean +__iter_nth_child (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent, + gint n) +{ + PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model); + + g_assert (parent == NULL); + g_return_val_if_fail (store, FALSE); + + if (!store->datasheet || n >= datasheet_get_n_rows (store->datasheet)) + { + iter->stamp = -1; + iter->user_data = NULL; + return FALSE; + } + + iter->user_data = GINT_TO_POINTER (n); + iter->stamp = store->stamp; + + return TRUE; +} + +/* Set the contents of OUT to reflect the information provided by IN, COL, and + ROW, for MODEL. Returns TRUE if successful. */ +gboolean +psppire_data_store_string_to_value (GtkTreeModel *model, gint col, gint row, + const gchar *in, GValue *out) +{ + PsppireDataStore *store = PSPPIRE_DATA_STORE (model); + + while (col >= psppire_dict_get_var_cnt (store->dict)) + { + const struct variable *var = + psppire_dict_insert_variable (store->dict, + psppire_dict_get_var_cnt (store->dict), + NULL); + g_return_val_if_fail (var, FALSE); + } + + const struct variable *variable = psppire_dict_get_variable (store->dict, col); + g_return_val_if_fail (variable, FALSE); + + const struct fmt_spec *fmt = var_get_print_format (variable); + + int width = var_get_width (variable); + + union value val; + value_init (&val, width); + char *xx = + data_in (ss_cstr (in), psppire_dict_encoding (store->dict), + fmt->type, &val, width, "UTF-8"); + + GVariant *vrnt = value_variant_new (&val, width); + value_destroy (&val, width); + + g_value_init (out, G_TYPE_VARIANT); + g_value_set_variant (out, vrnt); + free (xx); + return TRUE; +} + +static char * +unlabeled_value (PsppireDataStore *store, const struct variable *variable, const union value *val) +{ + const struct fmt_spec *fmt = var_get_print_format (variable); + return data_out (val, psppire_dict_encoding (store->dict), fmt); +} + +gchar * +psppire_data_store_value_to_string (gpointer unused, PsppireDataStore *store, gint col, gint row, const GValue *v) +{ + const struct variable *variable = psppire_dict_get_variable (store->dict, col); + g_return_val_if_fail (variable, g_strdup ("???")); + + GVariant *vrnt = g_value_get_variant (v); + union value val; + value_variant_get (&val, vrnt); + + char *out = unlabeled_value (store, variable, &val); + + value_destroy_from_variant (&val, vrnt); + + return out; +} + +gchar * +psppire_data_store_value_to_string_with_labels (gpointer unused, PsppireDataStore *store, gint col, gint row, const GValue *v) +{ + const struct variable *variable = psppire_dict_get_variable (store->dict, col); + g_return_val_if_fail (variable, g_strdup ("???")); + + GVariant *vrnt = g_value_get_variant (v); + union value val; + value_variant_get (&val, vrnt); + + char *out = NULL; + + const struct val_labs *vls = var_get_value_labels (variable); + struct val_lab *vl = val_labs_lookup (vls, &val); + if (vl != NULL) + out = strdup (val_lab_get_label (vl)); + else + out = unlabeled_value (store, variable, &val); + + value_destroy_from_variant (&val, vrnt); + + return out; +} + +static void +__get_value (GtkTreeModel *tree_model, + GtkTreeIter *iter, + gint column, + GValue *value) +{ + PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model); + + g_return_if_fail (iter->stamp == store->stamp); + + const struct variable *variable = psppire_dict_get_variable (store->dict, column); + if (NULL == variable) + return; + + g_value_init (value, G_TYPE_VARIANT); + + gint row = GPOINTER_TO_INT (iter->user_data); + + struct ccase *cc = datasheet_get_row (store->datasheet, row); + + const union value *val = case_data_idx (cc, var_get_case_index (variable)); + + GVariant *vv = value_variant_new (val, var_get_width (variable)); + + g_value_set_variant (value, vv); -inline GType + case_unref (cc); +} + + +static void +__tree_model_init (GtkTreeModelIface *iface) +{ + iface->get_flags = __tree_model_get_flags; + iface->get_n_columns = __tree_model_get_n_columns ; + iface->get_column_type = NULL; + iface->get_iter = NULL; + iface->iter_next = NULL; + iface->get_path = NULL; + iface->get_value = __get_value; + + iface->iter_children = NULL; + iface->iter_has_child = NULL; + iface->iter_n_children = __tree_model_iter_n_children; + iface->iter_nth_child = __iter_nth_child; + iface->iter_parent = NULL; +} + + +GType psppire_data_store_get_type (void) { static GType data_store_type = 0; @@ -84,38 +286,24 @@ psppire_data_store_get_type (void) (GInstanceInitFunc) psppire_data_store_init, }; - static const GInterfaceInfo sheet_model_info = - { - (GInterfaceInitFunc) psppire_data_store_sheet_model_init, - NULL, - NULL - }; - - static const GInterfaceInfo sheet_column_info = - { - (GInterfaceInitFunc) psppire_data_store_sheet_column_init, + static const GInterfaceInfo tree_model_info = { + (GInterfaceInitFunc) __tree_model_init, NULL, NULL }; - - - data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore", + data_store_type = g_type_register_static (G_TYPE_OBJECT, + "PsppireDataStore", &data_store_info, 0); - g_type_add_interface_static (data_store_type, - G_TYPE_SHEET_MODEL, - &sheet_model_info); - - g_type_add_interface_static (data_store_type, - G_TYPE_SHEET_COLUMN, - &sheet_column_info); - + g_type_add_interface_static (data_store_type, GTK_TYPE_TREE_MODEL, + &tree_model_info); } return data_store_type; } + static void psppire_data_store_class_init (PsppireDataStoreClass *class) { @@ -125,155 +313,142 @@ psppire_data_store_class_init (PsppireDataStoreClass *class) object_class = (GObjectClass*) class; object_class->finalize = psppire_data_store_finalize; -} - + object_class->dispose = psppire_data_store_dispose; + signals [ITEMS_CHANGED] = + g_signal_new ("items-changed", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, + psppire_marshal_VOID__UINT_UINT_UINT, + G_TYPE_NONE, + 3, + G_TYPE_UINT, /* Index of the start of the change */ + G_TYPE_UINT, /* The number of items deleted */ + G_TYPE_UINT); /* The number of items inserted */ -static gint -psppire_data_store_get_var_count (const GSheetModel *model) -{ - const PsppireDataStore *store = PSPPIRE_DATA_STORE(model); - - return psppire_dict_get_var_cnt(store->dict); + signals [CASE_CHANGED] = + g_signal_new ("case-changed", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__INT, + G_TYPE_NONE, + 1, + G_TYPE_INT); } -static gint -psppire_data_store_get_case_count (const GSheetModel *model) -{ - const PsppireDataStore *store = PSPPIRE_DATA_STORE(model); - - return psppire_case_array_get_n_cases(store->cases); -} -static void -psppire_data_store_init (PsppireDataStore *data_store) +casenumber +psppire_data_store_get_case_count (const PsppireDataStore *store) { - data_store->dict = 0; - data_store->cases = 0; + return datasheet_get_n_rows (store->datasheet); } -const PangoFontDescription * -psppire_data_store_get_font_desc(GSheetModel *model, - gint row, gint column) +size_t +psppire_data_store_get_value_count (const PsppireDataStore *store) { - PsppireDataStore *store = PSPPIRE_DATA_STORE(model); - - return store->font_desc; + return psppire_dict_get_value_cnt (store->dict); } - -static void -psppire_data_store_sheet_model_init (GSheetModelIface *iface) +const struct caseproto * +psppire_data_store_get_proto (const PsppireDataStore *store) { - iface->free_strings = TRUE; - iface->get_string = psppire_data_store_get_string; - iface->set_string = psppire_data_store_set_string; - iface->clear_datum = psppire_data_store_clear_datum; - iface->is_editable = NULL; - iface->is_visible = NULL; - iface->get_foreground = NULL; - iface->get_background = NULL; - iface->get_font_desc = psppire_data_store_get_font_desc; - iface->get_cell_border = NULL; - iface->get_column_count = psppire_data_store_get_var_count; - iface->get_row_count = psppire_data_store_get_case_count; + return psppire_dict_get_proto (store->dict); } -static -gboolean always_true() +static void +psppire_data_store_init (PsppireDataStore *data_store) { - return TRUE; + data_store->dict = NULL; + data_store->datasheet = NULL; + data_store->dispose_has_run = FALSE; + data_store->stamp = g_random_int (); } static void -delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data) +psppire_data_store_delete_value (PsppireDataStore *store, gint case_index) { - PsppireDataStore *store ; - - g_return_if_fail (data); - - store = PSPPIRE_DATA_STORE(data); - - g_assert(first >= 0); - - g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases); + g_return_if_fail (store->datasheet); + datasheet_delete_columns (store->datasheet, case_index, 1); + datasheet_insert_column (store->datasheet, NULL, -1, case_index); } +/* + A callback which occurs after a variable has been deleted. + */ static void -insert_case_callback(GtkWidget *w, gint casenum, gpointer data) +delete_variable_callback (GObject *obj, const struct variable *var UNUSED, + gint dict_index, gint case_index, + gpointer data) { - PsppireDataStore *store ; + PsppireDataStore *store = PSPPIRE_DATA_STORE (data); - g_return_if_fail (data); - - store = PSPPIRE_DATA_STORE(data); - - g_sheet_model_range_changed (G_SHEET_MODEL(store), - casenum, -1, - psppire_case_array_get_n_cases(store->cases), - -1); + psppire_data_store_delete_value (store, case_index); } +struct resize_datum_aux + { + const struct dictionary *dict; + const struct variable *new_variable; + const struct variable *old_variable; + }; static void -changed_case_callback(GtkWidget *w, gint casenum, gpointer data) +resize_datum (const union value *old, union value *new, const void *aux_) { - PsppireDataStore *store ; - g_return_if_fail (data); - - store = PSPPIRE_DATA_STORE(data); - - g_sheet_model_range_changed (G_SHEET_MODEL(store), - casenum, -1, - casenum, -1); - + const struct resize_datum_aux *aux = aux_; + int new_width = var_get_width (aux->new_variable); + const char *enc = dict_get_encoding (aux->dict); + const struct fmt_spec *newfmt = var_get_print_format (aux->new_variable); + char *s = data_out (old, enc, var_get_print_format (aux->old_variable)); + enum fmt_type type = (fmt_usable_for_input (newfmt->type) + ? newfmt->type + : FMT_DOLLAR); + free (data_in (ss_cstr (s), enc, type, new, new_width, enc)); + free (s); } - static void -delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data) +variable_changed_callback (GObject *obj, gint var_num, guint what, const struct variable *oldvar, + gpointer data) { - PsppireDataStore *store ; + PsppireDataStore *store = PSPPIRE_DATA_STORE (data); + struct variable *variable = psppire_dict_get_variable (store->dict, var_num); - g_return_if_fail (data); - - store = PSPPIRE_DATA_STORE(data); - - g_sheet_column_columns_deleted(G_SHEET_COLUMN(store), - var_num, n_vars); - - g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars); + if (what & VAR_TRAIT_WIDTH) + { + int posn = var_get_case_index (variable); + struct resize_datum_aux aux; + aux.old_variable = oldvar; + aux.new_variable = variable; + aux.dict = store->dict->dict; + datasheet_resize_column (store->datasheet, posn, var_get_width (variable), + resize_datum, &aux); + } } - static void -insert_variable_callback(GObject *obj, gint var_num, gpointer data) +insert_variable_callback (GObject *obj, gint var_num, gpointer data) { + struct variable *variable; PsppireDataStore *store; + gint posn; g_return_if_fail (data); - store = PSPPIRE_DATA_STORE(data); - - /* - g_sheet_model_range_changed (G_SHEET_MODEL(store), - casenum, -1, - psppire_case_array_get_n_cases(store->cases), - -1); - */ - - psppire_case_array_resize(store->cases, - dict_get_next_value_idx (store->dict->dict)); + store = PSPPIRE_DATA_STORE (data); - g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1); + variable = psppire_dict_get_variable (store->dict, var_num); + posn = var_get_case_index (variable); + psppire_data_store_insert_value (store, var_get_width (variable), posn); } - - - /** * psppire_data_store_new: * @dict: The dictionary for this data_store. @@ -282,31 +457,47 @@ insert_variable_callback(GObject *obj, gint var_num, gpointer data) * Return value: a new #PsppireDataStore **/ PsppireDataStore * -psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases) +psppire_data_store_new (PsppireDict *dict) { PsppireDataStore *retval; - retval = g_object_new (GTK_TYPE_DATA_STORE, NULL); + retval = g_object_new (PSPPIRE_TYPE_DATA_STORE, NULL); - retval->cases = cases; - g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback), - retval); + psppire_data_store_set_dictionary (retval, dict); - g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback), - retval); + return retval; +} +void +psppire_data_store_set_reader (PsppireDataStore *ds, + struct casereader *reader) +{ + gint i; + gint old_n = 0; + if ( ds->datasheet) + { + old_n = datasheet_get_n_rows (ds->datasheet); + datasheet_destroy (ds->datasheet); + } - g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback), - retval); + ds->datasheet = datasheet_create (reader); - psppire_data_store_set_dictionary(retval, dict); + gint new_n = datasheet_get_n_rows (ds->datasheet); + if ( ds->dict ) + for (i = 0 ; i < n_dict_signals; ++i ) + { + if ( ds->dict_handler_id [i] > 0) + { + g_signal_handler_unblock (ds->dict, + ds->dict_handler_id[i]); + } + } - return retval; + g_signal_emit (ds, signals[ITEMS_CHANGED], 0, 0, old_n, new_n); } - /** * psppire_data_store_replace_set_dictionary: * @data_store: The variable store @@ -316,390 +507,413 @@ psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases) * destroyed. **/ void -psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict) +psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict) { -#if 0 - if ( data_store->dict ) g_object_unref(data_store->dict); -#endif + int i; + + /* Disconnect any existing handlers */ + if ( data_store->dict ) + for (i = 0 ; i < n_dict_signals; ++i ) + { + g_signal_handler_disconnect (data_store->dict, + data_store->dict_handler_id[i]); + } data_store->dict = dict; - psppire_case_array_resize(data_store->cases, - dict_get_next_value_idx (data_store->dict->dict)); + if ( dict != NULL) + { + + data_store->dict_handler_id [VARIABLE_INSERTED] = + g_signal_connect (dict, "variable-inserted", + G_CALLBACK (insert_variable_callback), + data_store); + data_store->dict_handler_id [VARIABLE_DELETED] = + g_signal_connect (dict, "variable-deleted", + G_CALLBACK (delete_variable_callback), + data_store); + + data_store->dict_handler_id [VARIABLE_CHANGED] = + g_signal_connect (dict, "variable-changed", + G_CALLBACK (variable_changed_callback), + data_store); + } - g_signal_connect(dict, "variable-inserted", - G_CALLBACK(insert_variable_callback), - data_store); - g_signal_connect(dict, "variables-deleted", - G_CALLBACK(delete_variables_callback), - data_store); /* The entire model has changed */ - g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1); + + if ( data_store->dict ) + for (i = 0 ; i < n_dict_signals; ++i ) + { + if ( data_store->dict_handler_id [i] > 0) + { + g_signal_handler_block (data_store->dict, + data_store->dict_handler_id[i]); + } + } } static void psppire_data_store_finalize (GObject *object) { + PsppireDataStore *ds = PSPPIRE_DATA_STORE (object); + + if (ds->datasheet) + { + datasheet_destroy (ds->datasheet); + ds->datasheet = NULL; + } /* must chain up */ (* parent_class->finalize) (object); } -static const gchar * -psppire_data_store_get_string(GSheetModel *model, gint row, gint column) +static void +psppire_data_store_dispose (GObject *object) { - const char *text; - const struct fmt_spec *fp ; - const struct PsppireVariable *pv ; - const union value *v ; - GString *s; - PsppireDataStore *store = PSPPIRE_DATA_STORE(model); + PsppireDataStore *ds = PSPPIRE_DATA_STORE (object); - g_return_val_if_fail(store->dict, NULL); - g_return_val_if_fail(store->cases, NULL); - - if (column >= psppire_dict_get_var_cnt(store->dict)) - return NULL; + if (ds->dispose_has_run) + return; - if ( row >= psppire_case_array_get_n_cases(store->cases)) - return NULL; + psppire_data_store_set_dictionary (ds, NULL); + /* must chain up */ + (* parent_class->dispose) (object); - pv = psppire_dict_get_variable(store->dict, column); + ds->dispose_has_run = TRUE; +} - v = psppire_case_array_get_value(store->cases, row, - psppire_variable_get_index(pv)); - if ( store->show_labels) - { - const struct val_labs * vl = psppire_variable_get_value_labels(pv); - const gchar *label; - if ( (label = val_labs_find(vl, *v)) ) - { - return pspp_locale_to_utf8(label, -1, 0); - } - } +/* Insert a blank case before POSN */ +gboolean +psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn) +{ + gboolean result; + const struct caseproto *proto; + struct ccase *cc; + g_return_val_if_fail (ds, FALSE); - fp = psppire_variable_get_write_spec(pv); + proto = datasheet_get_proto (ds->datasheet); + g_return_val_if_fail (caseproto_get_n_widths (proto) > 0, FALSE); + g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE); - s = g_string_sized_new (fp->w + 1); - g_string_set_size(s, fp->w); - - memset(s->str, 0, fp->w); + cc = case_create (proto); + case_set_missing (cc); - g_assert(fp->w == s->len); - - /* Converts binary value V into printable form in the exactly - FP->W character in buffer S according to format specification - FP. No null terminator is appended to the buffer. */ - data_out (s->str, fp, v); + result = psppire_data_store_insert_case (ds, cc, posn); - - text = pspp_locale_to_utf8(s->str, fp->w, 0); - g_string_free(s, TRUE); + case_unref (cc); - return text; + return result; } - -static gboolean -set_null_string_value(union value *val, gpointer data) +gboolean +psppire_data_store_get_value (PsppireDataStore *store, + glong row, const struct variable *var, + union value *val) { - strcpy(val->s, ""); - return TRUE; -} + g_return_val_if_fail (store != NULL, FALSE); + g_return_val_if_fail (store->datasheet != NULL, FALSE); + g_return_val_if_fail (var != NULL, FALSE); -static gboolean -set_sysmis_value(union value *val, gpointer data) -{ - val->f = SYSMIS; - return TRUE; -} - - -static gboolean -psppire_data_store_clear_datum(GSheetModel *model, - gint row, gint col) - -{ - PsppireDataStore *store = PSPPIRE_DATA_STORE(model); - - const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col); + if (row < 0 || row >= datasheet_get_n_rows (store->datasheet)) + return FALSE; - const gint index = psppire_variable_get_index(pv) ; + int width = var_get_width (var); + value_init (val, width); + datasheet_get_value (store->datasheet, row, var_get_case_index (var), val); - if ( psppire_variable_get_type(pv) == NUMERIC) - psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0); - else - psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0); return TRUE; } -static gboolean -fillit(union value *val, gpointer data) -{ - struct data_in *d_in = data; - d_in->v = val; +gchar * +psppire_data_store_get_string (PsppireDataStore *store, + glong row, const struct variable *var, + bool use_value_label) +{ + gchar *string; + union value v; + int width = var_get_width (var); + if (! psppire_data_store_get_value (store, row, var, &v)) + return NULL; - if ( ! data_in(d_in) ) + string = NULL; + if (use_value_label) { - g_warning("Cant encode string\n"); - return FALSE; + const char *label = var_lookup_value_label (var, &v); + if (label != NULL) + string = g_strdup (label); } + if (string == NULL) + string = value_to_text (v, var); - return TRUE; + value_destroy (&v, width); + + return string; } -/* Attempts to update that part of the variable store which corresponds - to ROW, COL with the value TEXT. - Returns true if anything was updated, false otherwise. -*/ -static gboolean -psppire_data_store_set_string(GSheetModel *model, - const gchar *text, gint row, gint col) +/* Attempts to update that part of the variable store which corresponds to VAR + within ROW with the value TEXT. + + If USE_VALUE_LABEL is true, and TEXT is a value label for the column's + variable, then stores the value from that value label instead of the literal + TEXT. + + Returns true if anything was updated, false otherwise. */ +gboolean +psppire_data_store_set_string (PsppireDataStore *store, + const gchar *text, + glong row, const struct variable *var, + gboolean use_value_label) { - gint r; - PsppireDataStore *store = PSPPIRE_DATA_STORE(model); + gint case_index; + glong n_cases; + gboolean ok; - const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col); - g_return_val_if_fail(pv, FALSE); + n_cases = psppire_data_store_get_case_count (store); + if (row > n_cases) + return FALSE; + if (row == n_cases) + psppire_data_store_insert_new_case (store, row); - for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r ) + case_index = var_get_case_index (var); + if (use_value_label) { - gint c; - psppire_case_array_insert_case(store->cases, r, 0, 0); - - for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c ) - psppire_data_store_clear_datum(model, r, c); + const struct val_labs *vls = var_get_value_labels (var); + const union value *value = vls ? val_labs_find_value (vls, text) : NULL; + if (value) + ok = datasheet_put_value (store->datasheet, row, case_index, value); + else + ok = FALSE; } + else + ok = psppire_data_store_data_in (store, row, case_index, ss_cstr (text), + var_get_print_format (var)); - { - const gint index = psppire_variable_get_index(pv); - - struct data_in d_in; - d_in.s = text; - d_in.e = text + strlen(text); - d_in.v = 0; - d_in.f1 = d_in.f2 = 0; - d_in.format = * psppire_variable_get_write_spec(pv); - d_in.flags = 0; - - psppire_case_array_set_value(store->cases, row, index, fillit, &d_in); - } - - return TRUE; + if (ok) + g_signal_emit (store, signals [CASE_CHANGED], 0, row); + return ok; } -void -psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd) -{ - g_return_if_fail (store); - g_return_if_fail (PSPPIRE_IS_DATA_STORE (store)); - - store->font_desc = fd; - g_sheet_model_range_changed (G_SHEET_MODEL(store), - -1, -1, -1, -1); -} - void -psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels) +psppire_data_store_clear (PsppireDataStore *ds) { - g_return_if_fail (store); - g_return_if_fail (PSPPIRE_IS_DATA_STORE (store)); + datasheet_destroy (ds->datasheet); + ds->datasheet = NULL; - store->show_labels = show_labels; + psppire_dict_clear (ds->dict); - g_sheet_model_range_changed (G_SHEET_MODEL(store), - -1, -1, -1, -1); + g_signal_emit (ds, signals [ITEMS_CHANGED], 0, 0, -1, 0); } -static gboolean -write_case(const struct ccase *cc, - gpointer aux) +/* Return a casereader made from this datastore */ +struct casereader * +psppire_data_store_get_reader (PsppireDataStore *ds) { - struct sfm_writer *writer = aux; + int i; + struct casereader *reader ; - if ( ! sfm_write_case(writer, cc) ) - return FALSE; + if ( ds->dict ) + for (i = 0 ; i < n_dict_signals; ++i ) + { + g_signal_handler_block (ds->dict, + ds->dict_handler_id[i]); + } + reader = datasheet_make_reader (ds->datasheet); - return TRUE; + /* We must not reference this again */ + ds->datasheet = NULL; + + return reader; } -void -psppire_data_store_create_system_file(PsppireDataStore *store, - struct file_handle *handle) +/* Returns the CASENUMth case, or a null pointer on failure. + */ +struct ccase * +psppire_data_store_get_case (const PsppireDataStore *ds, + casenumber casenum) { - const struct sfm_write_options wo = { - true, /* writeable */ - false, /* dont compress */ - 3 /* version */ - }; - - struct sfm_writer *writer ; - - g_assert(handle); - - writer = sfm_open_writer(handle, store->dict->dict, wo); + g_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); - if ( ! writer) - return; + return datasheet_get_row (ds->datasheet, casenum); +} - psppire_case_array_iterate_case(store->cases, write_case, writer); - sfm_close_writer(writer); -} +gboolean +psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first, + casenumber n_cases) +{ + g_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); + g_return_val_if_fail (first + n_cases <= + psppire_data_store_get_case_count (ds), FALSE); -/* Column related funcs */ + datasheet_delete_rows (ds->datasheet, first, n_cases); -static gint -geometry_get_column_count(const GSheetColumn *geom) -{ - PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom); + g_signal_emit (ds, signals[ITEMS_CHANGED], 0, first, n_cases, 0); - return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict)); + return TRUE; } -/* Return the width that an 'M' character would occupy when typeset at - row, col */ -static guint -M_width(GtkSheet *sheet, gint row, gint col) -{ - GtkSheetCellAttr attributes; - PangoRectangle rect; - /* FIXME: make this a member of the data store */ - static PangoLayout *layout = 0; - gtk_sheet_get_attributes(sheet, row, col, &attributes); - if (! layout ) - layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M"); +/* Insert case CC into the case file before POSN */ +static gboolean +psppire_data_store_insert_case (PsppireDataStore *ds, + struct ccase *cc, + casenumber posn) +{ + bool result ; - g_assert(layout); - - pango_layout_set_font_description (layout, - attributes.font_desc); + g_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); - pango_layout_get_extents (layout, NULL, &rect); + cc = case_ref (cc); + result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1); -#if 0 - g_object_unref(G_OBJECT(layout)); -#endif + if ( result ) + { + g_signal_emit (ds, signals[ITEMS_CHANGED], 0, posn, 0, 1); + } + else + g_warning ("Cannot insert case at position %ld\n", posn); - return PANGO_PIXELS(rect.width); + return result; } -/* Return the number of pixels corresponding to a column of - WIDTH characters */ -static inline guint -columnWidthToPixels(GtkSheet *sheet, gint column, guint width) +/* Set the value of VAR in case CASENUM to V. + V must be the correct width for IDX. + Returns true if successful, false on I/O error. */ +gboolean +psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum, + const struct variable *var, const union value *v) { - return (M_width(sheet, 0, column) * width); -} + glong n_cases; + bool ok; + g_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); -static gint -geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet) -{ - const struct PsppireVariable *pv ; - PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom); + n_cases = psppire_data_store_get_case_count (ds); + if ( casenum > n_cases) + return FALSE; - if ( unit >= psppire_dict_get_var_cnt(ds->dict) ) - return 75; + if (casenum == n_cases) + psppire_data_store_insert_new_case (ds, casenum); - /* FIXME: We can optimise this by caching the widths until they're resized */ - pv = psppire_dict_get_variable(ds->dict, unit); + ok = datasheet_put_value (ds->datasheet, casenum, var_get_case_index (var), + v); + if (ok) + { + g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum); + g_signal_emit (ds, signals [ITEMS_CHANGED], 0, casenum, 1, 1); + } - return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv)); + return ok; } -static void -geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet) +/* Set the IDXth value of case C using D_IN */ +static gboolean +psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx, + struct substring input, const struct fmt_spec *fmt) { - PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom); - - struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit); + union value value; + int width; + bool ok; - psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit)); -} + PsppireDict *dict; + g_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); + g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE); -static GtkJustification -geometry_get_justification(const GSheetColumn *geom, gint unit) -{ - PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom); - const struct PsppireVariable *pv ; + dict = ds->dict; + width = fmt_var_width (fmt); + g_return_val_if_fail (caseproto_get_width ( + datasheet_get_proto (ds->datasheet), idx) == width, + FALSE); + value_init (&value, width); + ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value) + && data_in_msg (input, UTF8, fmt->type, &value, width, + dict_get_encoding (dict->dict)) + && datasheet_put_value (ds->datasheet, casenum, idx, &value)); + value_destroy (&value, width); - if ( unit >= psppire_dict_get_var_cnt(ds->dict) ) - return GTK_JUSTIFY_LEFT; - - pv = psppire_dict_get_variable(ds->dict, unit); - - /* Kludge: Happily GtkJustification is defined similarly - to enum alignment from pspp/variable.h */ - return psppire_variable_get_alignment(pv); + return ok; } - -static const gchar null_var_name[]=_("var"); - -static const gchar * -geometry_get_button_label(const GSheetColumn *geom, gint unit) +/* Resize the cases in the casefile, by inserting a value of the + given WIDTH into every one of them at the position immediately + preceding WHERE. +*/ +gboolean +psppire_data_store_insert_value (PsppireDataStore *ds, + gint width, gint where) { - const gchar *text; - struct PsppireVariable *pv ; - PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom); + union value value; - if ( unit >= psppire_dict_get_var_cnt(ds->dict) ) - return pspp_locale_to_utf8(null_var_name, -1, 0); + g_return_val_if_fail (ds, FALSE); - pv = psppire_dict_get_variable(ds->dict, unit); + g_assert (width >= 0); - text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0); + if ( ! ds->datasheet ) + ds->datasheet = datasheet_create (NULL); - return text; -} + value_init (&value, width); + value_set_missing (&value, width); + datasheet_insert_column (ds->datasheet, &value, width, where); + value_destroy (&value, width); -static gboolean -geometry_get_sensitivity(const GSheetColumn *geom, gint unit) + return TRUE; +} + +gboolean +psppire_data_store_filtered (PsppireDataStore *ds, + glong row) { - PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom); + union value val; + const struct dictionary *dict; + const struct variable *filter; - return (unit < psppire_dict_get_var_cnt(ds->dict)); -} + if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet)) + return FALSE; + dict = ds->dict->dict; + g_return_val_if_fail (dict, FALSE); + filter = dict_get_filter (dict); + if ( ! filter) + return FALSE; -static void -psppire_data_store_sheet_column_init (GSheetColumnIface *iface) -{ - iface->get_column_count = geometry_get_column_count; - iface->get_width = geometry_get_width; - iface->set_width = geometry_set_width; - iface->get_visibility = always_true; - iface->get_sensitivity = geometry_get_sensitivity; - iface->get_justification = geometry_get_justification; + g_return_val_if_fail (var_is_numeric (filter), FALSE); + value_init (&val, 0); + if ( ! datasheet_get_value (ds->datasheet, row, + var_get_case_index (filter), + &val) ) + return FALSE; - iface->get_button_label = geometry_get_button_label; + return (val.f == 0.0); }