1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2006, 2008, 2009 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/>. */
21 #define _(msgid) gettext (msgid)
22 #define N_(msgid) msgid
24 #include <data/datasheet.h>
25 #include <data/data-out.h>
26 #include <data/variable.h>
28 #include <ui/gui/sheet/psppire-sheetmodel.h>
29 #include <ui/gui/psppire-marshal.h>
31 #include <pango/pango-context.h>
33 #include "psppire-data-store.h"
34 #include <libpspp/i18n.h>
37 #include <data/dictionary.h>
38 #include <data/missing-values.h>
39 #include <data/value-labels.h>
40 #include <data/data-in.h>
41 #include <data/format.h>
43 #include <math/sort.h>
50 static void psppire_data_store_init (PsppireDataStore *data_store);
51 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
52 static void psppire_data_store_sheet_model_init (PsppireSheetModelIface *iface);
54 static void psppire_data_store_finalize (GObject *object);
55 static void psppire_data_store_dispose (GObject *object);
57 static gboolean psppire_data_store_clear_datum (PsppireSheetModel *model,
58 glong row, glong column);
61 static gboolean psppire_data_store_insert_case (PsppireDataStore *ds,
66 static gboolean psppire_data_store_data_in (PsppireDataStore *ds,
67 casenumber casenum, gint idx,
68 struct substring input,
69 const struct fmt_spec *fmt);
73 static GObjectClass *parent_class = NULL;
85 static guint signals [n_SIGNALS];
89 psppire_data_store_get_type (void)
91 static GType data_store_type = 0;
95 static const GTypeInfo data_store_info =
97 sizeof (PsppireDataStoreClass),
99 NULL, /* base_finalize */
100 (GClassInitFunc) psppire_data_store_class_init,
101 NULL, /* class_finalize */
102 NULL, /* class_data */
103 sizeof (PsppireDataStore),
105 (GInstanceInitFunc) psppire_data_store_init,
108 static const GInterfaceInfo sheet_model_info =
110 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
116 data_store_type = g_type_register_static (G_TYPE_OBJECT,
118 &data_store_info, 0);
120 g_type_add_interface_static (data_store_type,
121 PSPPIRE_TYPE_SHEET_MODEL,
126 return data_store_type;
131 psppire_data_store_class_init (PsppireDataStoreClass *class)
133 GObjectClass *object_class;
135 parent_class = g_type_class_peek_parent (class);
136 object_class = (GObjectClass*) class;
138 object_class->finalize = psppire_data_store_finalize;
139 object_class->dispose = psppire_data_store_dispose;
141 signals [BACKEND_CHANGED] =
142 g_signal_new ("backend-changed",
143 G_TYPE_FROM_CLASS (class),
147 g_cclosure_marshal_VOID__VOID,
151 signals [CASE_INSERTED] =
152 g_signal_new ("case-inserted",
153 G_TYPE_FROM_CLASS (class),
157 g_cclosure_marshal_VOID__INT,
163 signals [CASE_CHANGED] =
164 g_signal_new ("case-changed",
165 G_TYPE_FROM_CLASS (class),
169 g_cclosure_marshal_VOID__INT,
174 signals [CASES_DELETED] =
175 g_signal_new ("cases-deleted",
176 G_TYPE_FROM_CLASS (class),
180 psppire_marshal_VOID__INT_INT,
190 psppire_data_store_insert_value (PsppireDataStore *ds,
191 gint width, gint where);
194 psppire_data_store_get_value (const PsppireDataStore *ds,
195 casenumber casenum, size_t idx,
200 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
201 gint idx, union value *v);
207 psppire_data_store_get_var_count (const PsppireSheetModel *model)
209 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
211 return psppire_dict_get_var_cnt (store->dict);
215 psppire_data_store_get_case_count (const PsppireDataStore *store)
217 return datasheet_get_n_rows (store->datasheet);
221 psppire_data_store_get_value_count (const PsppireDataStore *store)
223 return psppire_dict_get_value_cnt (store->dict);
226 const struct caseproto *
227 psppire_data_store_get_proto (const PsppireDataStore *store)
229 return psppire_dict_get_proto (store->dict);
233 psppire_data_store_get_case_count_wrapper (const PsppireSheetModel *model)
235 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
236 return psppire_data_store_get_case_count (store);
240 psppire_data_store_init (PsppireDataStore *data_store)
242 data_store->dict = 0;
243 data_store->datasheet = NULL;
244 data_store->dispose_has_run = FALSE;
247 static inline gchar *
248 psppire_data_store_get_string_wrapper (const PsppireSheetModel *model, glong row,
251 return psppire_data_store_get_string (PSPPIRE_DATA_STORE (model), row, column);
255 static inline gboolean
256 psppire_data_store_set_string_wrapper (PsppireSheetModel *model,
258 glong row, glong column)
260 return psppire_data_store_set_string (PSPPIRE_DATA_STORE (model), text,
266 static gchar * get_column_subtitle (const PsppireSheetModel *model, gint col);
267 static gchar * get_column_button_label (const PsppireSheetModel *model, gint col);
268 static gboolean get_column_sensitivity (const PsppireSheetModel *model, gint col);
269 static GtkJustification get_column_justification (const PsppireSheetModel *model, gint col);
271 static gchar * get_row_button_label (const PsppireSheetModel *model, gint row);
272 static gboolean get_row_sensitivity (const PsppireSheetModel *model, gint row);
273 static gboolean get_row_overstrike (const PsppireSheetModel *model, gint row);
277 psppire_data_store_sheet_model_init (PsppireSheetModelIface *iface)
279 iface->free_strings = TRUE;
280 iface->get_string = psppire_data_store_get_string_wrapper;
281 iface->set_string = psppire_data_store_set_string_wrapper;
282 iface->clear_datum = psppire_data_store_clear_datum;
283 iface->is_editable = NULL;
284 iface->get_foreground = NULL;
285 iface->get_background = NULL;
286 iface->get_column_count = psppire_data_store_get_var_count;
287 iface->get_row_count = psppire_data_store_get_case_count_wrapper;
289 iface->get_column_subtitle = get_column_subtitle;
290 iface->get_column_title = get_column_button_label;
291 iface->get_column_sensitivity = get_column_sensitivity;
292 iface->get_column_justification = get_column_justification;
294 iface->get_row_title = get_row_button_label;
295 iface->get_row_sensitivity = get_row_sensitivity;
296 iface->get_row_overstrike = get_row_overstrike;
301 A callback which occurs after a variable has been deleted.
304 delete_variable_callback (GObject *obj, gint dict_index,
305 gint case_index, gint width,
308 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
311 psppire_sheet_model_columns_deleted (PSPPIRE_SHEET_MODEL (store), dict_index, 1);
312 datasheet_delete_columns (store->datasheet, case_index, 1);
313 datasheet_insert_column (store->datasheet, NULL, -1, case_index);
317 psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
323 variable_changed_callback (GObject *obj, gint var_num, gpointer data)
326 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
328 psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
331 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
338 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
340 struct variable *variable;
341 PsppireDataStore *store;
344 g_return_if_fail (data);
346 store = PSPPIRE_DATA_STORE (data);
348 variable = psppire_dict_get_variable (store->dict, var_num);
349 posn = var_get_case_index (variable);
350 psppire_data_store_insert_value (store, var_get_width (variable), posn);
354 psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
358 psppire_sheet_model_columns_inserted (PSPPIRE_SHEET_MODEL (store), var_num, 1);
361 struct resize_datum_aux
369 resize_datum (const union value *old, union value *new, void *aux_)
371 struct resize_datum_aux *aux = aux_;
373 if (aux->new_width == 0)
375 /* FIXME: try to parse string as number. */
378 else if (aux->old_width == 0)
380 /* FIXME: format number as string. */
381 value_set_missing (new, aux->new_width);
384 value_copy_rpad (new, aux->new_width, old, aux->old_width, ' ');
388 dict_size_change_callback (GObject *obj,
389 gint var_num, gint old_width, gpointer data)
391 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
392 struct variable *variable;
395 variable = psppire_dict_get_variable (store->dict, var_num);
396 posn = var_get_case_index (variable);
398 if (old_width != var_get_width (variable))
400 struct resize_datum_aux aux;
401 aux.old_width = old_width;
402 aux.new_width = var_get_width (variable);
403 datasheet_resize_column (store->datasheet, posn, aux.new_width,
411 * psppire_data_store_new:
412 * @dict: The dictionary for this data_store.
415 * Return value: a new #PsppireDataStore
418 psppire_data_store_new (PsppireDict *dict)
420 PsppireDataStore *retval;
422 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
424 psppire_data_store_set_dictionary (retval, dict);
430 psppire_data_store_set_reader (PsppireDataStore *ds,
431 struct casereader *reader)
436 datasheet_destroy (ds->datasheet);
438 ds->datasheet = datasheet_create (reader);
440 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (ds),
444 for (i = 0 ; i < n_dict_signals; ++i )
446 if ( ds->dict_handler_id [i] > 0)
448 g_signal_handler_unblock (ds->dict,
449 ds->dict_handler_id[i]);
453 g_signal_emit (ds, signals[BACKEND_CHANGED], 0);
458 * psppire_data_store_replace_set_dictionary:
459 * @data_store: The variable store
460 * @dict: The dictionary to set
462 * If a dictionary is already associated with the data-store, then it will be
466 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
470 /* Disconnect any existing handlers */
471 if ( data_store->dict )
472 for (i = 0 ; i < n_dict_signals; ++i )
474 g_signal_handler_disconnect (data_store->dict,
475 data_store->dict_handler_id[i]);
478 data_store->dict = dict;
483 data_store->dict_handler_id [VARIABLE_INSERTED] =
484 g_signal_connect (dict, "variable-inserted",
485 G_CALLBACK (insert_variable_callback),
488 data_store->dict_handler_id [VARIABLE_DELETED] =
489 g_signal_connect (dict, "variable-deleted",
490 G_CALLBACK (delete_variable_callback),
493 data_store->dict_handler_id [VARIABLE_CHANGED] =
494 g_signal_connect (dict, "variable-changed",
495 G_CALLBACK (variable_changed_callback),
498 data_store->dict_handler_id [SIZE_CHANGED] =
499 g_signal_connect (dict, "dict-size-changed",
500 G_CALLBACK (dict_size_change_callback),
506 /* The entire model has changed */
507 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (data_store), -1, -1, -1, -1);
510 psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (data_store), 0, -1);
513 if ( data_store->dict )
514 for (i = 0 ; i < n_dict_signals; ++i )
516 if ( data_store->dict_handler_id [i] > 0)
518 g_signal_handler_block (data_store->dict,
519 data_store->dict_handler_id[i]);
525 psppire_data_store_finalize (GObject *object)
529 (* parent_class->finalize) (object);
534 psppire_data_store_dispose (GObject *object)
536 PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
538 if (ds->dispose_has_run)
543 datasheet_destroy (ds->datasheet);
544 ds->datasheet = NULL;
548 (* parent_class->dispose) (object);
550 ds->dispose_has_run = TRUE;
555 /* Insert a blank case before POSN */
557 psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
560 const struct caseproto *proto;
562 g_return_val_if_fail (ds, FALSE);
564 proto = datasheet_get_proto (ds->datasheet);
565 g_return_val_if_fail (caseproto_get_n_widths (proto) > 0, FALSE);
566 g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE);
568 cc = case_create (proto);
569 case_set_missing (cc);
571 result = psppire_data_store_insert_case (ds, cc, posn);
580 psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column)
584 const struct fmt_spec *fp ;
585 const struct variable *pv ;
586 const struct dictionary *dict;
590 g_return_val_if_fail (store->dict, NULL);
591 g_return_val_if_fail (store->datasheet, NULL);
593 dict = store->dict->dict;
595 if (column >= psppire_dict_get_var_cnt (store->dict))
598 if ( row >= psppire_data_store_get_case_count (store))
601 pv = psppire_dict_get_variable (store->dict, column);
605 idx = var_get_case_index (pv);
606 width = var_get_width (pv);
610 value_init (&v, width);
611 if (!psppire_data_store_get_value (store, row, idx, &v))
614 if ( store->show_labels)
616 const gchar *label = var_lookup_value_label (pv, &v);
619 value_destroy (&v, width);
620 return g_strdup (label);
624 fp = var_get_write_format (pv);
626 text = data_out (&v, dict_get_encoding (dict), fp);
630 value_destroy (&v, width);
636 psppire_data_store_clear_datum (PsppireSheetModel *model,
637 glong row, glong col)
639 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
642 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
643 int width = var_get_width (pv);
645 const gint index = var_get_case_index (pv) ;
647 value_init (&v, width);
648 value_set_missing (&v, width);
649 psppire_data_store_set_value (store, row, index, &v);
650 value_destroy (&v, width);
652 psppire_sheet_model_range_changed (model, row, col, row, col);
659 /* Attempts to update that part of the variable store which corresponds
660 to ROW, COL with the value TEXT.
661 Returns true if anything was updated, false otherwise.
664 psppire_data_store_set_string (PsppireDataStore *store,
665 const gchar *text, glong row, glong col)
668 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
672 n_cases = psppire_data_store_get_case_count (store);
678 psppire_data_store_insert_new_case (store, row);
680 psppire_data_store_data_in (store, row,
681 var_get_case_index (pv), ss_cstr (text),
682 var_get_write_format (pv));
684 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), row, col, row, col);
692 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
694 g_return_if_fail (store);
695 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
697 store->show_labels = show_labels;
699 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
705 psppire_data_store_clear (PsppireDataStore *ds)
707 datasheet_destroy (ds->datasheet);
708 ds->datasheet = NULL;
710 psppire_dict_clear (ds->dict);
712 g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1);
717 /* Return a casereader made from this datastore */
719 psppire_data_store_get_reader (PsppireDataStore *ds)
722 struct casereader *reader ;
725 for (i = 0 ; i < n_dict_signals; ++i )
727 g_signal_handler_block (ds->dict,
728 ds->dict_handler_id[i]);
731 reader = datasheet_make_reader (ds->datasheet);
733 /* We must not reference this again */
734 ds->datasheet = NULL;
741 /* Column related funcs */
744 static const gchar null_var_name[]=N_("var");
748 /* Row related funcs */
751 get_row_button_label (const PsppireSheetModel *model, gint unit)
753 // PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
755 return g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER);
760 get_row_sensitivity (const PsppireSheetModel *model, gint unit)
762 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
764 return (unit < psppire_data_store_get_case_count (ds));
770 /* Column related stuff */
773 get_column_subtitle (const PsppireSheetModel *model, gint col)
775 const struct variable *v ;
776 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
778 if ( col >= psppire_dict_get_var_cnt (ds->dict) )
781 v = psppire_dict_get_variable (ds->dict, col);
783 if ( ! var_has_label (v))
786 return xstrdup (var_get_label (v));
790 get_column_button_label (const PsppireSheetModel *model, gint col)
792 struct variable *pv ;
793 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
795 if ( col >= psppire_dict_get_var_cnt (ds->dict) )
796 return xstrdup (gettext (null_var_name));
798 pv = psppire_dict_get_variable (ds->dict, col);
803 return xstrdup (var_get_name (pv));
807 get_column_sensitivity (const PsppireSheetModel *model, gint col)
809 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
811 return (col < psppire_dict_get_var_cnt (ds->dict));
816 static GtkJustification
817 get_column_justification (const PsppireSheetModel *model, gint col)
819 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
820 const struct variable *pv ;
822 if ( col >= psppire_dict_get_var_cnt (ds->dict) )
823 return GTK_JUSTIFY_LEFT;
825 pv = psppire_dict_get_variable (ds->dict, col);
827 return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
828 : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
829 : GTK_JUSTIFY_CENTER);
837 /* Returns the CASENUMth case, or a null pointer on failure.
840 psppire_data_store_get_case (const PsppireDataStore *ds,
843 g_return_val_if_fail (ds, FALSE);
844 g_return_val_if_fail (ds->datasheet, FALSE);
846 return datasheet_get_row (ds->datasheet, casenum);
851 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
854 g_return_val_if_fail (ds, FALSE);
855 g_return_val_if_fail (ds->datasheet, FALSE);
857 g_return_val_if_fail (first + n_cases <=
858 psppire_data_store_get_case_count (ds), FALSE);
861 datasheet_delete_rows (ds->datasheet, first, n_cases);
863 g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases);
864 psppire_sheet_model_rows_deleted (PSPPIRE_SHEET_MODEL (ds), first, n_cases);
871 /* Insert case CC into the case file before POSN */
873 psppire_data_store_insert_case (PsppireDataStore *ds,
879 g_return_val_if_fail (ds, FALSE);
880 g_return_val_if_fail (ds->datasheet, FALSE);
883 result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
887 g_signal_emit (ds, signals [CASE_INSERTED], 0, posn);
888 psppire_sheet_model_rows_inserted (PSPPIRE_SHEET_MODEL (ds), posn, 1);
891 g_warning ("Cannot insert case at position %ld\n", posn);
897 /* Copies the IDXth value from case CASENUM into VALUE, which
898 must be of the correct width for IDX.
899 Returns true if successful, false on failure. */
901 psppire_data_store_get_value (const PsppireDataStore *ds,
902 casenumber casenum, size_t idx,
905 g_return_val_if_fail (ds, false);
906 g_return_val_if_fail (ds->datasheet, false);
907 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), false);
909 return datasheet_get_value (ds->datasheet, casenum, idx, value);
914 /* Set the IDXth value of case C to V.
915 V must be the correct width for IDX.
916 Returns true if successful, false on I/O error. */
918 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
919 gint idx, union value *v)
923 g_return_val_if_fail (ds, FALSE);
924 g_return_val_if_fail (ds->datasheet, FALSE);
926 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
928 ok = datasheet_put_value (ds->datasheet, casenum, idx, v);
930 g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
938 /* Set the IDXth value of case C using D_IN */
940 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
941 struct substring input, const struct fmt_spec *fmt)
949 g_return_val_if_fail (ds, FALSE);
950 g_return_val_if_fail (ds->datasheet, FALSE);
952 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
956 width = fmt_var_width (fmt);
957 g_return_val_if_fail (caseproto_get_width (
958 datasheet_get_proto (ds->datasheet), idx) == width,
960 value_init (&value, width);
961 ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
962 && data_in (input, UTF8, fmt->type, 0, 0, 0,
963 dict->dict, &value, width)
964 && datasheet_put_value (ds->datasheet, casenum, idx, &value));
965 value_destroy (&value, width);
968 g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
973 /* Resize the cases in the casefile, by inserting a value of the
974 given WIDTH into every one of them at the position immediately
978 psppire_data_store_insert_value (PsppireDataStore *ds,
979 gint width, gint where)
983 g_return_val_if_fail (ds, FALSE);
985 g_assert (width >= 0);
987 if ( ! ds->datasheet )
988 ds->datasheet = datasheet_create (NULL);
990 value_init (&value, width);
994 value_set_missing (&value, width);
996 datasheet_insert_column (ds->datasheet, &value, width, where);
1002 get_row_overstrike (const PsppireSheetModel *model, gint row)
1005 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
1007 const struct dictionary *dict = ds->dict->dict;
1009 const struct variable *filter = dict_get_filter (dict);
1011 if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
1017 g_assert (var_is_numeric (filter));
1019 value_init (&val, 0);
1020 if ( ! datasheet_get_value (ds->datasheet, row,
1021 var_get_case_index (filter),
1026 return (val.f == 0.0);