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 ;
590 g_return_val_if_fail (store->dict, NULL);
591 g_return_val_if_fail (store->datasheet, NULL);
593 if (column >= psppire_dict_get_var_cnt (store->dict))
596 if ( row >= psppire_data_store_get_case_count (store))
599 pv = psppire_dict_get_variable (store->dict, column);
603 idx = var_get_case_index (pv);
604 width = var_get_width (pv);
608 value_init (&v, width);
609 if (!psppire_data_store_get_value (store, row, idx, &v))
612 if ( store->show_labels)
614 const gchar *label = var_lookup_value_label (pv, &v);
617 value_destroy (&v, width);
618 return recode_string (UTF8, psppire_dict_encoding (store->dict),
623 fp = var_get_write_format (pv);
625 s = g_string_sized_new (fp->w + 1);
626 g_string_set_size (s, fp->w);
628 memset (s->str, 0, fp->w);
630 g_assert (fp->w == s->len);
632 /* Converts binary value V into printable form in the exactly
633 FP->W character in buffer S according to format specification
634 FP. No null terminator is appended to the buffer. */
635 data_out (&v, fp, s->str);
637 text = recode_string (UTF8, psppire_dict_encoding (store->dict),
639 g_string_free (s, TRUE);
643 value_destroy (&v, width);
649 psppire_data_store_clear_datum (PsppireSheetModel *model,
650 glong row, glong col)
652 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
655 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
656 int width = var_get_width (pv);
658 const gint index = var_get_case_index (pv) ;
660 value_init (&v, width);
661 value_set_missing (&v, width);
662 psppire_data_store_set_value (store, row, index, &v);
663 value_destroy (&v, width);
665 psppire_sheet_model_range_changed (model, row, col, row, col);
672 /* Attempts to update that part of the variable store which corresponds
673 to ROW, COL with the value TEXT.
674 Returns true if anything was updated, false otherwise.
677 psppire_data_store_set_string (PsppireDataStore *store,
678 const gchar *text, glong row, glong col)
682 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
686 n_cases = psppire_data_store_get_case_count (store);
692 psppire_data_store_insert_new_case (store, row);
694 s = recode_string (psppire_dict_encoding (store->dict), UTF8, text, -1);
696 psppire_data_store_data_in (store, row,
697 var_get_case_index (pv), ss_cstr (s),
698 var_get_write_format (pv));
701 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), row, col, row, col);
709 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
711 g_return_if_fail (store);
712 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
714 store->show_labels = show_labels;
716 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
722 psppire_data_store_clear (PsppireDataStore *ds)
724 datasheet_destroy (ds->datasheet);
725 ds->datasheet = NULL;
727 psppire_dict_clear (ds->dict);
729 g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1);
734 /* Return a casereader made from this datastore */
736 psppire_data_store_get_reader (PsppireDataStore *ds)
739 struct casereader *reader ;
742 for (i = 0 ; i < n_dict_signals; ++i )
744 g_signal_handler_block (ds->dict,
745 ds->dict_handler_id[i]);
748 reader = datasheet_make_reader (ds->datasheet);
750 /* We must not reference this again */
751 ds->datasheet = NULL;
758 /* Column related funcs */
761 static const gchar null_var_name[]=N_("var");
765 /* Row related funcs */
768 get_row_button_label (const PsppireSheetModel *model, gint unit)
770 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
771 gchar *s = g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER);
773 gchar *text = recode_string (UTF8, psppire_dict_encoding (ds->dict),
783 get_row_sensitivity (const PsppireSheetModel *model, gint unit)
785 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
787 return (unit < psppire_data_store_get_case_count (ds));
793 /* Column related stuff */
796 get_column_subtitle (const PsppireSheetModel *model, gint col)
799 const struct variable *v ;
800 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
802 if ( col >= psppire_dict_get_var_cnt (ds->dict) )
805 v = psppire_dict_get_variable (ds->dict, col);
807 if ( ! var_has_label (v))
810 text = recode_string (UTF8, psppire_dict_encoding (ds->dict),
811 var_get_label (v), -1);
817 get_column_button_label (const PsppireSheetModel *model, gint col)
820 struct variable *pv ;
821 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
823 if ( col >= psppire_dict_get_var_cnt (ds->dict) )
824 return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
826 pv = psppire_dict_get_variable (ds->dict, col);
828 text = recode_string (UTF8, psppire_dict_encoding (ds->dict),
829 var_get_name (pv), -1);
835 get_column_sensitivity (const PsppireSheetModel *model, gint col)
837 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
839 return (col < psppire_dict_get_var_cnt (ds->dict));
844 static GtkJustification
845 get_column_justification (const PsppireSheetModel *model, gint col)
847 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
848 const struct variable *pv ;
850 if ( col >= psppire_dict_get_var_cnt (ds->dict) )
851 return GTK_JUSTIFY_LEFT;
853 pv = psppire_dict_get_variable (ds->dict, col);
855 return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
856 : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
857 : GTK_JUSTIFY_CENTER);
865 /* Returns the CASENUMth case, or a null pointer on failure.
868 psppire_data_store_get_case (const PsppireDataStore *ds,
871 g_return_val_if_fail (ds, FALSE);
872 g_return_val_if_fail (ds->datasheet, FALSE);
874 return datasheet_get_row (ds->datasheet, casenum);
879 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
882 g_return_val_if_fail (ds, FALSE);
883 g_return_val_if_fail (ds->datasheet, FALSE);
885 g_return_val_if_fail (first + n_cases <=
886 psppire_data_store_get_case_count (ds), FALSE);
889 datasheet_delete_rows (ds->datasheet, first, n_cases);
891 g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases);
892 psppire_sheet_model_rows_deleted (PSPPIRE_SHEET_MODEL (ds), first, n_cases);
899 /* Insert case CC into the case file before POSN */
901 psppire_data_store_insert_case (PsppireDataStore *ds,
907 g_return_val_if_fail (ds, FALSE);
908 g_return_val_if_fail (ds->datasheet, FALSE);
911 result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
915 g_signal_emit (ds, signals [CASE_INSERTED], 0, posn);
916 psppire_sheet_model_rows_inserted (PSPPIRE_SHEET_MODEL (ds), posn, 1);
919 g_warning ("Cannot insert case at position %ld\n", posn);
925 /* Copies the IDXth value from case CASENUM into VALUE, which
926 must be of the correct width for IDX.
927 Returns true if successful, false on failure. */
929 psppire_data_store_get_value (const PsppireDataStore *ds,
930 casenumber casenum, size_t idx,
933 g_return_val_if_fail (ds, false);
934 g_return_val_if_fail (ds->datasheet, false);
935 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), false);
937 return datasheet_get_value (ds->datasheet, casenum, idx, value);
942 /* Set the IDXth value of case C to V.
943 V must be the correct width for IDX.
944 Returns true if successful, false on I/O error. */
946 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
947 gint idx, union value *v)
951 g_return_val_if_fail (ds, FALSE);
952 g_return_val_if_fail (ds->datasheet, FALSE);
954 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
956 ok = datasheet_put_value (ds->datasheet, casenum, idx, v);
958 g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
966 /* Set the IDXth value of case C using D_IN */
968 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
969 struct substring input, const struct fmt_spec *fmt)
975 g_return_val_if_fail (ds, FALSE);
976 g_return_val_if_fail (ds->datasheet, FALSE);
978 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
980 width = fmt_var_width (fmt);
981 g_return_val_if_fail (caseproto_get_width (
982 datasheet_get_proto (ds->datasheet), idx) == width,
984 value_init (&value, width);
985 ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
986 && data_in (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, &value, width)
987 && datasheet_put_value (ds->datasheet, casenum, idx, &value));
988 value_destroy (&value, width);
991 g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
996 /* Resize the cases in the casefile, by inserting a value of the
997 given WIDTH into every one of them at the position immediately
1001 psppire_data_store_insert_value (PsppireDataStore *ds,
1002 gint width, gint where)
1006 g_return_val_if_fail (ds, FALSE);
1008 g_assert (width >= 0);
1010 if ( ! ds->datasheet )
1011 ds->datasheet = datasheet_create (NULL);
1013 value_init (&value, width);
1017 value_set_missing (&value, width);
1019 datasheet_insert_column (ds->datasheet, &value, width, where);
1025 get_row_overstrike (const PsppireSheetModel *model, gint row)
1028 PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
1030 const struct dictionary *dict = ds->dict->dict;
1032 const struct variable *filter = dict_get_filter (dict);
1034 if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
1040 g_assert (var_is_numeric (filter));
1042 value_init (&val, 0);
1043 if ( ! datasheet_get_value (ds->datasheet, row,
1044 var_get_case_index (filter),
1049 return (val.f == 0.0);