X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-data-store.c;h=65001dd170d98a3edaa903a619244b38e931b38f;hb=1485e3a189c42b1ac1339c063c85d9e74e503431;hp=f3c22337623ab52f1380304360939c906029423a;hpb=f79c3114efb645934af16035e489fdedc4cab85e;p=pspp-builds.git diff --git a/src/ui/gui/psppire-data-store.c b/src/ui/gui/psppire-data-store.c index f3c22337..65001dd1 100644 --- a/src/ui/gui/psppire-data-store.c +++ b/src/ui/gui/psppire-data-store.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2006, 2008 Free Software Foundation + Copyright (C) 2006, 2008, 2009 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 @@ -25,12 +25,13 @@ #include #include -#include +#include +#include #include #include "psppire-data-store.h" -#include "psppire-case-file.h" +#include #include "helper.h" #include @@ -39,24 +40,47 @@ #include #include +#include + +#include "xalloc.h" +#include "xmalloca.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_model_init (PsppireSheetModelIface *iface); static void psppire_data_store_finalize (GObject *object); static void psppire_data_store_dispose (GObject *object); -static gboolean psppire_data_store_clear_datum (GSheetModel *model, +static gboolean psppire_data_store_clear_datum (PsppireSheetModel *model, glong row, glong column); +static gboolean psppire_data_store_insert_case (PsppireDataStore *ds, + struct ccase *cc, + casenumber posn); + + +static gboolean psppire_data_store_data_in (PsppireDataStore *ds, + casenumber casenum, gint idx, + struct substring input, + const struct fmt_spec *fmt); + + + static GObjectClass *parent_class = NULL; -enum {FONT_CHANGED, - BACKEND_CHANGED, - n_SIGNALS}; +enum + { + BACKEND_CHANGED, + CASES_DELETED, + CASE_INSERTED, + CASE_CHANGED, + n_SIGNALS + }; static guint signals [n_SIGNALS]; @@ -89,11 +113,12 @@ psppire_data_store_get_type (void) }; - 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, + PSPPIRE_TYPE_SHEET_MODEL, &sheet_model_info); } @@ -113,8 +138,8 @@ psppire_data_store_class_init (PsppireDataStoreClass *class) object_class->finalize = psppire_data_store_finalize; object_class->dispose = psppire_data_store_dispose; - signals [FONT_CHANGED] = - g_signal_new ("font_changed", + signals [BACKEND_CHANGED] = + g_signal_new ("backend-changed", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, @@ -123,22 +148,63 @@ psppire_data_store_class_init (PsppireDataStoreClass *class) G_TYPE_NONE, 0); + signals [CASE_INSERTED] = + g_signal_new ("case-inserted", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__INT, + G_TYPE_NONE, + 1, + G_TYPE_INT); - signals [BACKEND_CHANGED] = - g_signal_new ("backend-changed", + + signals [CASE_CHANGED] = + g_signal_new ("case-changed", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, - g_cclosure_marshal_VOID__VOID, + g_cclosure_marshal_VOID__INT, G_TYPE_NONE, - 0); + 1, + G_TYPE_INT); + + signals [CASES_DELETED] = + g_signal_new ("cases-deleted", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, + psppire_marshal_VOID__INT_INT, + G_TYPE_NONE, + 2, + G_TYPE_INT, + G_TYPE_INT); } +static gboolean +psppire_data_store_insert_value (PsppireDataStore *ds, + gint width, gint where); + +static bool +psppire_data_store_get_value (const PsppireDataStore *ds, + casenumber casenum, size_t idx, + union value *value); + + +static gboolean +psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum, + gint idx, union value *v); + + + + static glong -psppire_data_store_get_var_count (const GSheetModel *model) +psppire_data_store_get_var_count (const PsppireSheetModel *model) { const PsppireDataStore *store = PSPPIRE_DATA_STORE (model); @@ -148,7 +214,7 @@ psppire_data_store_get_var_count (const GSheetModel *model) casenumber psppire_data_store_get_case_count (const PsppireDataStore *store) { - return psppire_case_file_get_case_count (store->case_file); + return datasheet_get_n_rows (store->datasheet); } size_t @@ -157,8 +223,14 @@ psppire_data_store_get_value_count (const PsppireDataStore *store) return psppire_dict_get_value_cnt (store->dict); } -casenumber -psppire_data_store_get_case_count_wrapper (const GSheetModel *model) +const struct caseproto * +psppire_data_store_get_proto (const PsppireDataStore *store) +{ + return psppire_dict_get_proto (store->dict); +} + +static casenumber +psppire_data_store_get_case_count_wrapper (const PsppireSheetModel *model) { const PsppireDataStore *store = PSPPIRE_DATA_STORE (model); return psppire_data_store_get_case_count (store); @@ -168,22 +240,12 @@ static void psppire_data_store_init (PsppireDataStore *data_store) { data_store->dict = 0; - data_store->case_file = NULL; - data_store->width_of_m = 10; + data_store->datasheet = NULL; data_store->dispose_has_run = FALSE; } -const PangoFontDescription * -psppire_data_store_get_font_desc (const GSheetModel *model, - glong row, glong column) -{ - PsppireDataStore *store = PSPPIRE_DATA_STORE (model); - - return store->font_desc; -} - static inline gchar * -psppire_data_store_get_string_wrapper (const GSheetModel *model, glong row, +psppire_data_store_get_string_wrapper (const PsppireSheetModel *model, glong row, glong column) { return psppire_data_store_get_string (PSPPIRE_DATA_STORE (model), row, column); @@ -191,7 +253,7 @@ psppire_data_store_get_string_wrapper (const GSheetModel *model, glong row, static inline gboolean -psppire_data_store_set_string_wrapper (GSheetModel *model, +psppire_data_store_set_string_wrapper (PsppireSheetModel *model, const gchar *text, glong row, glong column) { @@ -201,28 +263,26 @@ psppire_data_store_set_string_wrapper (GSheetModel *model, -static gchar * get_column_subtitle (const GSheetModel *model, gint col); -static gchar * get_column_button_label (const GSheetModel *model, gint col); -static gboolean get_column_sensitivity (const GSheetModel *model, gint col); -static GtkJustification get_column_justification (const GSheetModel *model, gint col); +static gchar * get_column_subtitle (const PsppireSheetModel *model, gint col); +static gchar * get_column_button_label (const PsppireSheetModel *model, gint col); +static gboolean get_column_sensitivity (const PsppireSheetModel *model, gint col); +static GtkJustification get_column_justification (const PsppireSheetModel *model, gint col); -static gchar * get_row_button_label (const GSheetModel *model, gint row); -static gboolean get_row_sensitivity (const GSheetModel *model, gint row); +static gchar * get_row_button_label (const PsppireSheetModel *model, gint row); +static gboolean get_row_sensitivity (const PsppireSheetModel *model, gint row); +static gboolean get_row_overstrike (const PsppireSheetModel *model, gint row); static void -psppire_data_store_sheet_model_init (GSheetModelIface *iface) +psppire_data_store_sheet_model_init (PsppireSheetModelIface *iface) { iface->free_strings = TRUE; iface->get_string = psppire_data_store_get_string_wrapper; iface->set_string = psppire_data_store_set_string_wrapper; 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_wrapper; @@ -233,53 +293,7 @@ psppire_data_store_sheet_model_init (GSheetModelIface *iface) iface->get_row_title = get_row_button_label; iface->get_row_sensitivity = get_row_sensitivity; -} - -static void -delete_cases_callback (GtkWidget *w, - casenumber first, casenumber n_cases, gpointer data) -{ - 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); -} - - -static void -insert_case_callback (GtkWidget *w, casenumber casenum, gpointer data) -{ - PsppireDataStore *store = PSPPIRE_DATA_STORE (data); - - g_return_if_fail (data); - - g_print ("%s\n", __FUNCTION__); - - g_sheet_model_range_changed (G_SHEET_MODEL (store), - casenum, -1, - psppire_case_file_get_case_count (store->case_file), - -1); - - g_sheet_model_rows_inserted (G_SHEET_MODEL (store), casenum, 1); -} - - -static void -changed_case_callback (GtkWidget *w, gint casenum, gpointer data) -{ - 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); + iface->get_row_overstrike = get_row_overstrike; } @@ -288,31 +302,33 @@ changed_case_callback (GtkWidget *w, gint casenum, gpointer data) */ static void delete_variable_callback (GObject *obj, gint dict_index, - gint case_index, gint val_cnt, + gint case_index, gint width, gpointer data) { PsppireDataStore *store = PSPPIRE_DATA_STORE (data); + + psppire_sheet_model_columns_deleted (PSPPIRE_SHEET_MODEL (store), dict_index, 1); + datasheet_delete_columns (store->datasheet, case_index, 1); + datasheet_insert_column (store->datasheet, NULL, -1, case_index); #if AXIS_TRANSITION - g_sheet_model_columns_deleted (G_SHEET_MODEL (store), dict_index, 1); - g_sheet_column_columns_changed (G_SHEET_COLUMN (store), + + psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store), dict_index, -1); #endif } - static void variable_changed_callback (GObject *obj, gint var_num, gpointer data) { +#if AXIS_TRANSITION PsppireDataStore *store = PSPPIRE_DATA_STORE (data); -#if AXIS_TRANSITION - g_sheet_column_columns_changed (G_SHEET_COLUMN (store), + psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store), var_num, 1); - - g_sheet_model_range_changed (G_SHEET_MODEL (store), + psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), -1, var_num, -1, var_num); #endif @@ -321,6 +337,7 @@ variable_changed_callback (GObject *obj, gint var_num, gpointer data) static void insert_variable_callback (GObject *obj, gint var_num, gpointer data) { + struct variable *variable; PsppireDataStore *store; gint posn; @@ -328,45 +345,64 @@ insert_variable_callback (GObject *obj, gint var_num, gpointer data) store = PSPPIRE_DATA_STORE (data); - if ( var_num > 0 ) - { - struct variable *variable = - psppire_dict_get_variable (store->dict, var_num); - - g_assert (variable != NULL); - - posn = var_get_case_index (variable); - } - else - { - posn = 0; - } - - psppire_case_file_insert_values (store->case_file, 1, posn); + 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); #if AXIS_TRANSITION - g_sheet_column_columns_changed (G_SHEET_COLUMN (store), + + psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store), var_num, 1); #endif - g_sheet_model_columns_inserted (G_SHEET_MODEL (store), var_num, 1); + psppire_sheet_model_columns_inserted (PSPPIRE_SHEET_MODEL (store), var_num, 1); } +struct resize_datum_aux + { + int old_width; + int new_width; + }; + + +void +resize_datum (const union value *old, union value *new, void *aux_) +{ + struct resize_datum_aux *aux = aux_; + + if (aux->new_width == 0) + { + /* FIXME: try to parse string as number. */ + new->f = SYSMIS; + } + else if (aux->old_width == 0) + { + /* FIXME: format number as string. */ + value_set_missing (new, aux->new_width); + } + else + value_copy_rpad (new, aux->new_width, old, aux->old_width, ' '); +} static void dict_size_change_callback (GObject *obj, - gint posn, gint adjustment, gpointer data) + gint var_num, gint old_width, gpointer data) { PsppireDataStore *store = PSPPIRE_DATA_STORE (data); + struct variable *variable; + int posn; - const struct variable *v = psppire_dict_get_variable (store->dict, posn); - - const gint new_val_width = value_cnt_from_width (var_get_width (v)); + variable = psppire_dict_get_variable (store->dict, var_num); + posn = var_get_case_index (variable); - if ( adjustment > 0 ) - psppire_case_file_insert_values (store->case_file, adjustment, - new_val_width - adjustment + - var_get_case_index(v)); + if (old_width != var_get_width (variable)) + { + struct resize_datum_aux aux; + aux.old_width = old_width; + aux.new_width = var_get_width (variable); + datasheet_resize_column (store->datasheet, posn, aux.new_width, + resize_datum, &aux); + } } @@ -390,25 +426,19 @@ psppire_data_store_new (PsppireDict *dict) return retval; } - void -psppire_data_store_set_case_file (PsppireDataStore *ds, - PsppireCaseFile *cf) +psppire_data_store_set_reader (PsppireDataStore *ds, + struct casereader *reader) { gint i; - if ( ds->case_file) g_object_unref (ds->case_file); - ds->case_file = cf; - g_sheet_model_range_changed (G_SHEET_MODEL (ds), - -1, -1, -1, -1); + if ( ds->datasheet) + datasheet_destroy (ds->datasheet); - for (i = 0 ; i < n_cf_signals ; ++i ) - { - if ( ds->cf_handler_id [i] > 0 ) - g_signal_handler_disconnect (ds->case_file, - ds->cf_handler_id[i]); - } + ds->datasheet = datasheet_create (reader); + psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (ds), + -1, -1, -1, -1); if ( ds->dict ) for (i = 0 ; i < n_dict_signals; ++i ) @@ -420,21 +450,6 @@ psppire_data_store_set_case_file (PsppireDataStore *ds, } } - ds->cf_handler_id [CASES_DELETED] = - g_signal_connect (ds->case_file, "cases-deleted", - G_CALLBACK (delete_cases_callback), - ds); - - ds->cf_handler_id [CASE_INSERTED] = - g_signal_connect (ds->case_file, "case-inserted", - G_CALLBACK (insert_case_callback), - ds); - - ds->cf_handler_id [CASE_CHANGED] = - g_signal_connect (ds->case_file, "case-changed", - G_CALLBACK (changed_case_callback), - ds); - g_signal_emit (ds, signals[BACKEND_CHANGED], 0); } @@ -489,10 +504,10 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di /* The entire model has changed */ - g_sheet_model_range_changed (G_SHEET_MODEL (data_store), -1, -1, -1, -1); + psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (data_store), -1, -1, -1, -1); #if AXIS_TRANSITION - g_sheet_column_columns_changed (G_SHEET_COLUMN (data_store), 0, -1); + psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (data_store), 0, -1); #endif if ( data_store->dict ) @@ -523,7 +538,11 @@ psppire_data_store_dispose (GObject *object) if (ds->dispose_has_run) return; - if (ds->case_file) g_object_unref (ds->case_file); + if (ds->datasheet) + { + datasheet_destroy (ds->datasheet); + ds->datasheet = NULL; + } /* must chain up */ (* parent_class->dispose) (object); @@ -532,48 +551,26 @@ psppire_data_store_dispose (GObject *object) } -gboolean -psppire_data_store_delete_cases (PsppireDataStore *ds, - casenumber first, casenumber count) -{ - g_return_val_if_fail (ds, FALSE); - - return psppire_case_file_delete_cases (ds->case_file, count, first); -} - - /* Insert a blank case before POSN */ gboolean psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn) { gboolean result; - gint val_cnt, v; - struct ccase cc; + const struct caseproto *proto; + struct ccase *cc; g_return_val_if_fail (ds, FALSE); - val_cnt = datasheet_get_column_cnt (ds->case_file->datasheet) ; - - g_return_val_if_fail (val_cnt > 0, FALSE); - + 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); - case_create (&cc, val_cnt); - - memset ( case_data_rw_idx (&cc, 0), 0, val_cnt * MAX_SHORT_STRING); + cc = case_create (proto); + case_set_missing (cc); - for (v = 0 ; v < psppire_dict_get_var_cnt (ds->dict) ; ++v) - { - const struct variable *pv = psppire_dict_get_variable (ds->dict, v); - if ( var_is_alpha (pv)) - continue; - - case_data_rw (&cc, pv)->f = SYSMIS; - } - - result = psppire_case_file_insert_case (ds->case_file, &cc, posn); + result = psppire_data_store_insert_case (ds, cc, posn); - case_destroy (&cc); + case_unref (cc); return result; } @@ -586,16 +583,19 @@ psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column) char *text; const struct fmt_spec *fp ; const struct variable *pv ; - union value *v ; - GString *s; + const struct dictionary *dict; + union value v; + int width; g_return_val_if_fail (store->dict, NULL); - g_return_val_if_fail (store->case_file, NULL); + g_return_val_if_fail (store->datasheet, NULL); + + dict = store->dict->dict; if (column >= psppire_dict_get_var_cnt (store->dict)) return NULL; - if ( row >= psppire_case_file_get_case_count (store->case_file)) + if ( row >= psppire_data_store_get_case_count (store)) return NULL; pv = psppire_dict_get_variable (store->dict, column); @@ -603,66 +603,54 @@ psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column) g_assert (pv); idx = var_get_case_index (pv); + width = var_get_width (pv); g_assert (idx >= 0); - v = psppire_case_file_get_value (store->case_file, row, idx, NULL, - var_get_width (pv)); - - g_return_val_if_fail (v, NULL); + value_init (&v, width); + if (!psppire_data_store_get_value (store, row, idx, &v)) + return NULL; if ( store->show_labels) { - const gchar *label = var_lookup_value_label (pv, v); + const gchar *label = var_lookup_value_label (pv, &v); if (label) { - free (v); - return pspp_locale_to_utf8 (label, -1, 0); + value_destroy (&v, width); + return g_strdup (label); } } fp = var_get_write_format (pv); - s = g_string_sized_new (fp->w + 1); - g_string_set_size (s, fp->w); - - memset (s->str, 0, fp->w); - - 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 (v, fp, s->str); - - text = pspp_locale_to_utf8 (s->str, fp->w, 0); - g_string_free (s, TRUE); + text = data_out (&v, dict_get_encoding (dict), fp); g_strchomp (text); - free (v); + value_destroy (&v, width); return text; } static gboolean -psppire_data_store_clear_datum (GSheetModel *model, +psppire_data_store_clear_datum (PsppireSheetModel *model, glong row, glong col) { PsppireDataStore *store = PSPPIRE_DATA_STORE (model); union value v; const struct variable *pv = psppire_dict_get_variable (store->dict, col); + int width = var_get_width (pv); const gint index = var_get_case_index (pv) ; - if ( var_is_numeric (pv)) - v.f = SYSMIS; - else - memcpy (v.s, "", MAX_SHORT_STRING); + value_init (&v, width); + value_set_missing (&v, width); + psppire_data_store_set_value (store, row, index, &v); + value_destroy (&v, width); + + psppire_sheet_model_range_changed (model, row, col, row, col); - psppire_case_file_set_value (store->case_file, row, index, &v, - var_get_width (pv)); return TRUE; } @@ -676,9 +664,11 @@ gboolean psppire_data_store_set_string (PsppireDataStore *store, const gchar *text, glong row, glong col) { + gchar *s; glong n_cases; const struct variable *pv = psppire_dict_get_variable (store->dict, col); - g_return_val_if_fail (pv, FALSE); + if ( NULL == pv) + return FALSE; n_cases = psppire_data_store_get_case_count (store); @@ -688,33 +678,20 @@ psppire_data_store_set_string (PsppireDataStore *store, if (row == n_cases) psppire_data_store_insert_new_case (store, row); - psppire_case_file_data_in (store->case_file, row, - var_get_case_index (pv), ss_cstr (text), - var_get_write_format (pv)); + s = recode_string (psppire_dict_encoding (store->dict), UTF8, text, -1); - return TRUE; -} - - -void -psppire_data_store_set_font (PsppireDataStore *store, - const PangoFontDescription *fd) -{ - g_return_if_fail (store); - g_return_if_fail (PSPPIRE_IS_DATA_STORE (store)); + psppire_data_store_data_in (store, row, + var_get_case_index (pv), ss_cstr (s), + var_get_write_format (pv)); + free (s); - store->font_desc = fd; -#if 0 - store->width_of_m = calc_m_width (fd); -#endif - g_signal_emit (store, signals [FONT_CHANGED], 0); + psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), row, col, row, col); - - g_sheet_model_range_changed (G_SHEET_MODEL (store), - -1, -1, -1, -1); + return TRUE; } + void psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels) { @@ -723,17 +700,20 @@ psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels) store->show_labels = show_labels; - g_sheet_model_range_changed (G_SHEET_MODEL (store), + psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), -1, -1, -1, -1); } void -psppire_data_store_clear (PsppireDataStore *data_store) +psppire_data_store_clear (PsppireDataStore *ds) { - psppire_case_file_clear (data_store->case_file); + datasheet_destroy (ds->datasheet); + ds->datasheet = NULL; - psppire_dict_clear (data_store->dict); + psppire_dict_clear (ds->dict); + + g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1); } @@ -745,12 +725,6 @@ psppire_data_store_get_reader (PsppireDataStore *ds) int i; struct casereader *reader ; - for (i = 0 ; i < n_cf_signals ; ++i ) - { - g_signal_handler_disconnect (ds->case_file, ds->cf_handler_id[i]); - ds->cf_handler_id[i] = 0 ; - } - if ( ds->dict ) for (i = 0 ; i < n_dict_signals; ++i ) { @@ -758,7 +732,10 @@ psppire_data_store_get_reader (PsppireDataStore *ds) ds->dict_handler_id[i]); } - reader = psppire_case_file_make_reader (ds->case_file); + reader = datasheet_make_reader (ds->datasheet); + + /* We must not reference this again */ + ds->datasheet = NULL; return reader; } @@ -775,28 +752,20 @@ static const gchar null_var_name[]=N_("var"); /* Row related funcs */ static gchar * -get_row_button_label (const GSheetModel *model, gint unit) +get_row_button_label (const PsppireSheetModel *model, gint unit) { - gchar *text; - gchar *s; - PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); - - s = g_strdup_printf (_("%d"), unit + 1); + // PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); - text = pspp_locale_to_utf8 (s, -1, 0); - - g_free (s); - - return text; + return g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER); } static gboolean -get_row_sensitivity (const GSheetModel *model, gint unit) +get_row_sensitivity (const PsppireSheetModel *model, gint unit) { PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); - return (unit < psppire_case_file_get_case_count (ds->case_file)); + return (unit < psppire_data_store_get_case_count (ds)); } @@ -805,9 +774,8 @@ get_row_sensitivity (const GSheetModel *model, gint unit) /* Column related stuff */ static gchar * -get_column_subtitle (const GSheetModel *model, gint col) +get_column_subtitle (const PsppireSheetModel *model, gint col) { - gchar *text; const struct variable *v ; PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); @@ -819,15 +787,12 @@ get_column_subtitle (const GSheetModel *model, gint col) if ( ! var_has_label (v)) return NULL; - text = pspp_locale_to_utf8 (var_get_label (v), -1, 0); - - return text; + return xstrdup (var_get_label (v)); } static gchar * -get_column_button_label (const GSheetModel *model, gint col) +get_column_button_label (const PsppireSheetModel *model, gint col) { - gchar *text; struct variable *pv ; PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); @@ -836,13 +801,14 @@ get_column_button_label (const GSheetModel *model, gint col) pv = psppire_dict_get_variable (ds->dict, col); - text = pspp_locale_to_utf8 (var_get_name (pv), -1, 0); + if (NULL == pv) + return NULL; - return text; + return xstrdup (var_get_name (pv)); } static gboolean -get_column_sensitivity (const GSheetModel *model, gint col) +get_column_sensitivity (const PsppireSheetModel *model, gint col) { PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); @@ -852,7 +818,7 @@ get_column_sensitivity (const GSheetModel *model, gint col) static GtkJustification -get_column_justification (const GSheetModel *model, gint col) +get_column_justification (const PsppireSheetModel *model, gint col) { PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); const struct variable *pv ; @@ -868,3 +834,193 @@ get_column_justification (const GSheetModel *model, gint col) } + + + + +/* Returns the CASENUMth case, or a null pointer on failure. + */ +struct ccase * +psppire_data_store_get_case (const PsppireDataStore *ds, + casenumber casenum) +{ + g_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); + + return datasheet_get_row (ds->datasheet, casenum); +} + + +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); + + + datasheet_delete_rows (ds->datasheet, first, n_cases); + + g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases); + psppire_sheet_model_rows_deleted (PSPPIRE_SHEET_MODEL (ds), first, n_cases); + + return TRUE; +} + + + +/* 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_return_val_if_fail (ds, FALSE); + g_return_val_if_fail (ds->datasheet, FALSE); + + case_ref (cc); + result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1); + + if ( result ) + { + g_signal_emit (ds, signals [CASE_INSERTED], 0, posn); + psppire_sheet_model_rows_inserted (PSPPIRE_SHEET_MODEL (ds), posn, 1); + } + else + g_warning ("Cannot insert case at position %ld\n", posn); + + return result; +} + + +/* Copies the IDXth value from case CASENUM into VALUE, which + must be of the correct width for IDX. + Returns true if successful, false on failure. */ +static bool +psppire_data_store_get_value (const PsppireDataStore *ds, + casenumber casenum, size_t idx, + union value *value) +{ + 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); + + return datasheet_get_value (ds->datasheet, casenum, idx, value); +} + + + +/* Set the IDXth value of case C to V. + V must be the correct width for IDX. + Returns true if successful, false on I/O error. */ +static gboolean +psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum, + gint idx, union value *v) +{ + bool ok; + + 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); + + ok = datasheet_put_value (ds->datasheet, casenum, idx, v); + if (ok) + g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum); + + return ok; +} + + + + +/* 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) +{ + union value value; + int width; + bool ok; + + 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); + + 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 (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, &value, width) + && datasheet_put_value (ds->datasheet, casenum, idx, &value)); + value_destroy (&value, width); + + if (ok) + g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum); + + return ok; +} + +/* 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. +*/ +static gboolean +psppire_data_store_insert_value (PsppireDataStore *ds, + gint width, gint where) +{ + union value value; + + g_return_val_if_fail (ds, FALSE); + + g_assert (width >= 0); + + if ( ! ds->datasheet ) + ds->datasheet = datasheet_create (NULL); + + value_init (&value, width); + if (width == 0) + value.f = 0; + else + value_set_missing (&value, width); + + datasheet_insert_column (ds->datasheet, &value, width, where); + + return TRUE; +} + +static gboolean +get_row_overstrike (const PsppireSheetModel *model, gint row) +{ + union value val; + PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); + + const struct dictionary *dict = ds->dict->dict; + + const struct variable *filter = dict_get_filter (dict); + + if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet)) + return FALSE; + + if ( ! filter) + return FALSE; + + g_assert (var_is_numeric (filter)); + + value_init (&val, 0); + if ( ! datasheet_get_value (ds->datasheet, row, + var_get_case_index (filter), + &val) ) + return FALSE; + + + return (val.f == 0.0); +}