1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2006 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 <gtksheet/gsheetmodel.h>
29 #include <gtksheet/gsheet-column-iface.h>
30 #include <gtksheet/gsheet-row-iface.h>
32 #include <pango/pango-context.h>
34 #include "psppire-data-store.h"
35 #include "psppire-case-file.h"
38 #include <data/dictionary.h>
39 #include <data/missing-values.h>
40 #include <data/value-labels.h>
41 #include <data/data-in.h>
42 #include <data/format.h>
45 static void psppire_data_store_init (PsppireDataStore *data_store);
46 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
47 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
48 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
49 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
51 static void psppire_data_store_finalize (GObject *object);
52 static void psppire_data_store_dispose (GObject *object);
54 static gboolean psppire_data_store_clear_datum (GSheetModel *model,
55 glong row, glong column);
58 #define MIN_COLUMNS 10
60 #define TRAILING_ROWS 10
62 static GObjectClass *parent_class = NULL;
68 static guint signals [n_SIGNALS];
72 psppire_data_store_get_type (void)
74 static GType data_store_type = 0;
78 static const GTypeInfo data_store_info =
80 sizeof (PsppireDataStoreClass),
82 NULL, /* base_finalize */
83 (GClassInitFunc) psppire_data_store_class_init,
84 NULL, /* class_finalize */
85 NULL, /* class_data */
86 sizeof (PsppireDataStore),
88 (GInstanceInitFunc) psppire_data_store_init,
91 static const GInterfaceInfo sheet_model_info =
93 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
98 static const GInterfaceInfo sheet_column_info =
100 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
105 static const GInterfaceInfo sheet_row_info =
107 (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
113 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
114 &data_store_info, 0);
116 g_type_add_interface_static (data_store_type,
120 g_type_add_interface_static (data_store_type,
124 g_type_add_interface_static (data_store_type,
129 return data_store_type;
134 psppire_data_store_class_init (PsppireDataStoreClass *class)
136 GObjectClass *object_class;
138 parent_class = g_type_class_peek_parent (class);
139 object_class = (GObjectClass*) class;
141 object_class->finalize = psppire_data_store_finalize;
142 object_class->dispose = psppire_data_store_dispose;
144 signals [FONT_CHANGED] =
145 g_signal_new ("font_changed",
146 G_TYPE_FROM_CLASS (class),
150 g_cclosure_marshal_VOID__VOID,
158 psppire_data_store_get_var_count (const GSheetModel *model)
160 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
162 return psppire_dict_get_var_cnt (store->dict);
166 psppire_data_store_get_case_count (const PsppireDataStore *store)
168 return psppire_case_file_get_case_count (store->case_file);
172 psppire_data_store_get_value_count (const PsppireDataStore *store)
174 return psppire_dict_get_value_cnt (store->dict);
178 psppire_data_store_get_case_count_wrapper (const GSheetModel *model)
180 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
181 return psppire_data_store_get_case_count (store);
185 psppire_data_store_init (PsppireDataStore *data_store)
187 data_store->dict = 0;
188 data_store->case_file = NULL;
189 data_store->width_of_m = 10;
190 data_store->dispose_has_run = FALSE;
193 const PangoFontDescription *
194 psppire_data_store_get_font_desc (const GSheetModel *model,
195 glong row, glong column)
197 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
199 return store->font_desc;
202 static inline gchar *
203 psppire_data_store_get_string_wrapper (const GSheetModel *model, glong row,
206 return psppire_data_store_get_string (PSPPIRE_DATA_STORE (model), row, column);
210 static inline gboolean
211 psppire_data_store_set_string_wrapper (GSheetModel *model,
213 glong row, glong column)
215 return psppire_data_store_set_string (PSPPIRE_DATA_STORE (model), text,
223 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
225 iface->free_strings = TRUE;
226 iface->get_string = psppire_data_store_get_string_wrapper;
227 iface->set_string = psppire_data_store_set_string_wrapper;
228 iface->clear_datum = psppire_data_store_clear_datum;
229 iface->is_editable = NULL;
230 iface->is_visible = NULL;
231 iface->get_foreground = NULL;
232 iface->get_background = NULL;
233 iface->get_font_desc = psppire_data_store_get_font_desc;
234 iface->get_cell_border = NULL;
235 iface->get_column_count = psppire_data_store_get_var_count;
236 iface->get_row_count = psppire_data_store_get_case_count_wrapper;
240 gboolean always_true ()
247 delete_cases_callback (GtkWidget *w,
248 casenumber first, casenumber n_cases, gpointer data)
250 PsppireDataStore *store ;
252 g_return_if_fail (data);
254 store = PSPPIRE_DATA_STORE (data);
256 g_assert (first >= 0);
258 g_sheet_model_rows_deleted (G_SHEET_MODEL (store), first, n_cases);
263 insert_case_callback (GtkWidget *w, casenumber casenum, gpointer data)
265 PsppireDataStore *store ;
267 g_return_if_fail (data);
269 store = PSPPIRE_DATA_STORE (data);
271 g_sheet_model_range_changed (G_SHEET_MODEL (store),
273 psppire_case_file_get_case_count (store->case_file),
276 g_sheet_model_rows_inserted (G_SHEET_MODEL (store), casenum, 1);
281 changed_case_callback (GtkWidget *w, gint casenum, gpointer data)
283 PsppireDataStore *store ;
284 g_return_if_fail (data);
286 store = PSPPIRE_DATA_STORE (data);
288 g_sheet_model_range_changed (G_SHEET_MODEL (store),
295 A callback which occurs after a variable has been deleted.
298 delete_variable_callback (GObject *obj, gint dict_index,
299 gint case_index, gint val_cnt,
302 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
304 g_sheet_model_columns_deleted (G_SHEET_MODEL (store), dict_index, 1);
306 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
312 variable_changed_callback (GObject *obj, gint var_num, gpointer data)
314 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
316 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
320 g_sheet_model_range_changed (G_SHEET_MODEL (store),
326 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
328 PsppireDataStore *store;
331 g_return_if_fail (data);
333 store = PSPPIRE_DATA_STORE (data);
337 struct variable *variable =
338 psppire_dict_get_variable (store->dict, var_num);
340 g_assert (variable != NULL);
342 posn = var_get_case_index (variable);
349 psppire_case_file_insert_values (store->case_file, 1, posn);
351 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
354 g_sheet_model_columns_inserted (G_SHEET_MODEL (store), var_num, 1);
359 dict_size_change_callback (GObject *obj,
360 gint posn, gint adjustment, gpointer data)
362 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
364 const struct variable *v = psppire_dict_get_variable (store->dict, posn);
366 const gint new_val_width = value_cnt_from_width (var_get_width (v));
368 if ( adjustment > 0 )
369 psppire_case_file_insert_values (store->case_file, adjustment,
370 new_val_width - adjustment +
371 var_get_case_index(v));
377 * psppire_data_store_new:
378 * @dict: The dictionary for this data_store.
381 * Return value: a new #PsppireDataStore
384 psppire_data_store_new (PsppireDict *dict)
386 PsppireDataStore *retval;
388 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
390 psppire_data_store_set_dictionary (retval, dict);
397 psppire_data_store_set_case_file (PsppireDataStore *ds,
401 if ( ds->case_file) g_object_unref (ds->case_file);
406 g_sheet_model_range_changed (G_SHEET_MODEL (ds),
409 for (i = 0 ; i < n_cf_signals ; ++i )
411 if ( ds->cf_handler_id [i] > 0 )
412 g_signal_handler_disconnect (ds->case_file,
413 ds->cf_handler_id[i]);
418 for (i = 0 ; i < n_dict_signals; ++i )
420 if ( ds->dict_handler_id [i] > 0)
422 g_signal_handler_unblock (ds->dict,
423 ds->dict_handler_id[i]);
427 ds->cf_handler_id [CASES_DELETED] =
428 g_signal_connect (ds->case_file, "cases-deleted",
429 G_CALLBACK (delete_cases_callback),
432 ds->cf_handler_id [CASE_INSERTED] =
433 g_signal_connect (ds->case_file, "case-inserted",
434 G_CALLBACK (insert_case_callback),
437 ds->cf_handler_id [CASE_CHANGED] =
438 g_signal_connect (ds->case_file, "case-changed",
439 G_CALLBACK (changed_case_callback),
445 * psppire_data_store_replace_set_dictionary:
446 * @data_store: The variable store
447 * @dict: The dictionary to set
449 * If a dictionary is already associated with the data-store, then it will be
453 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
457 /* Disconnect any existing handlers */
458 if ( data_store->dict )
459 for (i = 0 ; i < n_dict_signals; ++i )
461 g_signal_handler_disconnect (data_store->dict,
462 data_store->dict_handler_id[i]);
465 data_store->dict = dict;
470 data_store->dict_handler_id [VARIABLE_INSERTED] =
471 g_signal_connect (dict, "variable-inserted",
472 G_CALLBACK (insert_variable_callback),
475 data_store->dict_handler_id [VARIABLE_DELETED] =
476 g_signal_connect (dict, "variable-deleted",
477 G_CALLBACK (delete_variable_callback),
480 data_store->dict_handler_id [VARIABLE_CHANGED] =
481 g_signal_connect (dict, "variable-changed",
482 G_CALLBACK (variable_changed_callback),
485 data_store->dict_handler_id [SIZE_CHANGED] =
486 g_signal_connect (dict, "dict-size-changed",
487 G_CALLBACK (dict_size_change_callback),
493 /* The entire model has changed */
494 g_sheet_model_range_changed (G_SHEET_MODEL (data_store), -1, -1, -1, -1);
496 g_sheet_column_columns_changed (G_SHEET_COLUMN (data_store), 0, -1);
499 if ( data_store->dict )
500 for (i = 0 ; i < n_dict_signals; ++i )
502 if ( data_store->dict_handler_id [i] > 0)
504 g_signal_handler_block (data_store->dict,
505 data_store->dict_handler_id[i]);
511 psppire_data_store_finalize (GObject *object)
515 (* parent_class->finalize) (object);
520 psppire_data_store_dispose (GObject *object)
522 PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
524 if (ds->dispose_has_run)
527 if (ds->case_file) g_object_unref (ds->case_file);
530 (* parent_class->dispose) (object);
532 ds->dispose_has_run = TRUE;
537 psppire_data_store_delete_cases (PsppireDataStore *ds,
538 casenumber first, casenumber count)
540 g_return_val_if_fail (ds, FALSE);
542 return psppire_case_file_delete_cases (ds->case_file, count, first);
547 /* Insert a blank case before POSN */
549 psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
554 g_return_val_if_fail (ds, FALSE);
556 val_cnt = datasheet_get_column_cnt (ds->case_file->datasheet) ;
558 g_return_val_if_fail (val_cnt > 0, FALSE);
560 g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE);
562 case_create (&cc, val_cnt);
564 memset ( case_data_rw_idx (&cc, 0), 0, val_cnt * MAX_SHORT_STRING);
566 for (v = 0 ; v < psppire_dict_get_var_cnt (ds->dict) ; ++v)
568 const struct variable *pv = psppire_dict_get_variable (ds->dict, v);
569 if ( var_is_alpha (pv))
572 case_data_rw (&cc, pv)->f = SYSMIS;
575 result = psppire_case_file_insert_case (ds->case_file, &cc, posn);
584 psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column)
588 const struct fmt_spec *fp ;
589 const struct variable *pv ;
593 g_return_val_if_fail (store->dict, NULL);
594 g_return_val_if_fail (store->case_file, NULL);
596 if (column >= psppire_dict_get_var_cnt (store->dict))
599 if ( row >= psppire_case_file_get_case_count (store->case_file))
602 pv = psppire_dict_get_variable (store->dict, column);
606 idx = var_get_case_index (pv);
610 v = psppire_case_file_get_value (store->case_file, row, idx, NULL,
613 g_return_val_if_fail (v, NULL);
615 if ( store->show_labels)
617 const gchar *label = var_lookup_value_label (pv, v);
621 return pspp_locale_to_utf8 (label, -1, 0);
625 fp = var_get_write_format (pv);
627 s = g_string_sized_new (fp->w + 1);
628 g_string_set_size (s, fp->w);
630 memset (s->str, 0, fp->w);
632 g_assert (fp->w == s->len);
634 /* Converts binary value V into printable form in the exactly
635 FP->W character in buffer S according to format specification
636 FP. No null terminator is appended to the buffer. */
637 data_out (v, fp, s->str);
639 text = pspp_locale_to_utf8 (s->str, fp->w, 0);
640 g_string_free (s, TRUE);
650 psppire_data_store_clear_datum (GSheetModel *model,
651 glong row, glong col)
653 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
656 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
658 const gint index = var_get_case_index (pv) ;
660 if ( var_is_numeric (pv))
663 memcpy (v.s, "", MAX_SHORT_STRING);
665 psppire_case_file_set_value (store->case_file, row, index, &v,
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)
681 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
682 g_return_val_if_fail (pv, FALSE);
684 n_cases = psppire_data_store_get_case_count (store);
690 psppire_data_store_insert_new_case (store, row);
692 psppire_case_file_data_in (store->case_file, row,
693 var_get_case_index (pv), ss_cstr (text),
694 var_get_write_format (pv));
701 psppire_data_store_set_font (PsppireDataStore *store,
702 const PangoFontDescription *fd)
704 g_return_if_fail (store);
705 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
707 store->font_desc = fd;
709 store->width_of_m = calc_m_width (fd);
711 g_signal_emit (store, signals [FONT_CHANGED], 0);
714 g_sheet_model_range_changed (G_SHEET_MODEL (store),
720 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
722 g_return_if_fail (store);
723 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
725 store->show_labels = show_labels;
727 g_sheet_model_range_changed (G_SHEET_MODEL (store),
733 psppire_data_store_clear (PsppireDataStore *data_store)
735 psppire_case_file_clear (data_store->case_file);
737 psppire_dict_clear (data_store->dict);
742 /* Return a casereader made from this datastore */
744 psppire_data_store_get_reader (PsppireDataStore *ds)
747 struct casereader *reader ;
749 for (i = 0 ; i < n_cf_signals ; ++i )
751 g_signal_handler_disconnect (ds->case_file, ds->cf_handler_id[i]);
752 ds->cf_handler_id[i] = 0 ;
756 for (i = 0 ; i < n_dict_signals; ++i )
758 g_signal_handler_block (ds->dict,
759 ds->dict_handler_id[i]);
762 reader = psppire_case_file_make_reader (ds->case_file);
769 /* Column related funcs */
772 geometry_get_column_count (const GSheetColumn *geom)
774 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
776 return MAX (MIN_COLUMNS, psppire_dict_get_var_cnt (ds->dict));
782 geometry_get_width (const GSheetColumn *geom, glong unit)
784 const struct variable *pv ;
785 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
787 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
788 return ds->width_of_m * 8 ;
790 pv = psppire_dict_get_variable (ds->dict, unit);
793 return ds->width_of_m * 8 ;
795 return ds->width_of_m * var_get_display_width (pv);
799 geometry_set_width (GSheetColumn *geom, glong unit, gint width)
801 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
803 struct variable *pv = psppire_dict_get_variable (ds->dict, unit);
805 var_set_display_width (pv, width / ds->width_of_m );
810 static GtkJustification
811 geometry_get_justification (const GSheetColumn *geom, glong unit)
813 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
814 const struct variable *pv ;
817 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
818 return GTK_JUSTIFY_LEFT;
820 pv = psppire_dict_get_variable (ds->dict, unit);
822 return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
823 : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
824 : GTK_JUSTIFY_CENTER);
828 static const gchar null_var_name[]=N_("var");
831 geometry_get_column_button_label (const GSheetColumn *geom, glong unit)
834 struct variable *pv ;
835 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
837 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
838 return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
840 pv = psppire_dict_get_variable (ds->dict, unit);
842 text = pspp_locale_to_utf8 (var_get_name (pv), -1, 0);
849 geometry_get_column_subtitle (const GSheetColumn *geom, glong unit)
852 const struct variable *v ;
853 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
855 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
858 v = psppire_dict_get_variable (ds->dict, unit);
860 if ( ! var_has_label (v))
863 text = pspp_locale_to_utf8 (var_get_label (v), -1, 0);
870 geometry_get_sensitivity (const GSheetColumn *geom, glong unit)
872 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
874 return (unit < psppire_dict_get_var_cnt (ds->dict));
879 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
881 iface->get_column_count = geometry_get_column_count;
882 iface->get_width = geometry_get_width;
883 iface->set_width = geometry_set_width;
884 iface->get_visibility = always_true;
885 iface->get_sensitivity = geometry_get_sensitivity;
886 iface->get_justification = geometry_get_justification;
887 iface->get_button_label = geometry_get_column_button_label;
888 iface->get_subtitle = geometry_get_column_subtitle;
892 /* Row related funcs */
895 geometry_get_row_count (const GSheetRow *geom)
897 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
899 return TRAILING_ROWS + psppire_case_file_get_case_count (ds->case_file);
902 #define ROW_HEIGHT 25
905 geometry_get_height (const GSheetRow *geom, glong unit)
911 geometry_get_top_ypixel (const GSheetRow *geo, glong row)
913 return row * ROW_HEIGHT;
917 geometry_pixel_to_row (const GSheetRow *geo, guint pixel)
919 glong row = pixel / ROW_HEIGHT;
921 if (row >= geometry_get_row_count (geo))
922 row = geometry_get_row_count (geo) - 1;
929 geometry_get_row_sensitivity (const GSheetRow *geom, glong unit)
931 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
934 return (unit < psppire_case_file_get_case_count (ds->case_file));
939 geometry_get_row_button_label (const GSheetRow *geom, glong unit)
943 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
946 TRAILING_ROWS + psppire_case_file_get_case_count (ds->case_file))
949 s = g_strdup_printf (_("%ld"), unit + FIRST_CASE_NUMBER);
951 text = pspp_locale_to_utf8 (s, -1, 0);
959 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
961 iface->get_row_count = geometry_get_row_count;
963 iface->get_height = geometry_get_height;
964 iface->set_height = 0;
965 iface->get_visibility = always_true;
966 iface->get_sensitivity = geometry_get_row_sensitivity;
967 iface->top_ypixel = geometry_get_top_ypixel;
968 iface->pixel_to_row = geometry_pixel_to_row;
969 iface->get_button_label = geometry_get_row_button_label;