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/gtksheet.h>
29 #include <gtksheet/gsheetmodel.h>
30 #include <gtksheet/gsheet-column-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>
44 static void psppire_data_store_init (PsppireDataStore *data_store);
45 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
46 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
47 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
48 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
50 static void psppire_data_store_finalize (GObject *object);
52 static gboolean psppire_data_store_clear_datum (GSheetModel *model,
53 gint row, gint column);
56 #define MIN_COLUMNS 10
58 #define TRAILING_ROWS 10
60 static GObjectClass *parent_class = NULL;
66 static guint signals [n_SIGNALS];
70 psppire_data_store_get_type (void)
72 static GType data_store_type = 0;
76 static const GTypeInfo data_store_info =
78 sizeof (PsppireDataStoreClass),
80 NULL, /* base_finalize */
81 (GClassInitFunc) psppire_data_store_class_init,
82 NULL, /* class_finalize */
83 NULL, /* class_data */
84 sizeof (PsppireDataStore),
86 (GInstanceInitFunc) psppire_data_store_init,
89 static const GInterfaceInfo sheet_model_info =
91 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
96 static const GInterfaceInfo sheet_column_info =
98 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
103 static const GInterfaceInfo sheet_row_info =
105 (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
111 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
112 &data_store_info, 0);
114 g_type_add_interface_static (data_store_type,
118 g_type_add_interface_static (data_store_type,
122 g_type_add_interface_static (data_store_type,
127 return data_store_type;
132 psppire_data_store_class_init (PsppireDataStoreClass *class)
134 GObjectClass *object_class;
136 parent_class = g_type_class_peek_parent (class);
137 object_class = (GObjectClass*) class;
139 object_class->finalize = psppire_data_store_finalize;
141 signals [FONT_CHANGED] =
142 g_signal_new ("font_changed",
143 G_TYPE_FROM_CLASS (class),
147 g_cclosure_marshal_VOID__VOID,
155 psppire_data_store_get_var_count (const GSheetModel *model)
157 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
159 return psppire_dict_get_var_cnt (store->dict);
163 psppire_data_store_get_case_count (PsppireDataStore *store)
165 return psppire_case_file_get_case_count (store->case_file);
169 psppire_data_store_get_case_count_from_model (const GSheetModel *model)
171 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
173 return psppire_case_file_get_case_count (store->case_file);
180 psppire_data_store_init (PsppireDataStore *data_store)
182 data_store->dict = 0;
183 data_store->case_file = 0;
184 data_store->width_of_m = 10;
187 const PangoFontDescription *
188 psppire_data_store_get_font_desc (const GSheetModel *model,
189 gint row, gint column)
191 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
193 return store->font_desc;
196 static inline gchar *
197 psppire_data_store_get_string_wrapper (const GSheetModel *model, gint row,
200 return psppire_data_store_get_string (PSPPIRE_DATA_STORE (model), row, column);
204 static inline gboolean
205 psppire_data_store_set_string_wrapper (GSheetModel *model,
207 gint row, gint column)
209 return psppire_data_store_set_string (PSPPIRE_DATA_STORE (model), text,
217 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
219 iface->free_strings = TRUE;
220 iface->get_string = psppire_data_store_get_string_wrapper;
221 iface->set_string = psppire_data_store_set_string_wrapper;
222 iface->clear_datum = psppire_data_store_clear_datum;
223 iface->is_editable = NULL;
224 iface->is_visible = NULL;
225 iface->get_foreground = NULL;
226 iface->get_background = NULL;
227 iface->get_font_desc = psppire_data_store_get_font_desc;
228 iface->get_cell_border = NULL;
229 iface->get_column_count = psppire_data_store_get_var_count;
230 iface->get_row_count = psppire_data_store_get_case_count_from_model;
234 gboolean always_true ()
241 delete_cases_callback (GtkWidget *w, gint first, gint n_cases, gpointer data)
243 PsppireDataStore *store ;
245 g_return_if_fail (data);
247 store = PSPPIRE_DATA_STORE (data);
249 g_assert (first >= 0);
251 g_sheet_model_rows_deleted (G_SHEET_MODEL (store), first, n_cases);
256 insert_case_callback (GtkWidget *w, gint casenum, gpointer data)
258 PsppireDataStore *store ;
260 g_return_if_fail (data);
262 store = PSPPIRE_DATA_STORE (data);
264 g_sheet_model_range_changed (G_SHEET_MODEL (store),
266 psppire_case_file_get_case_count (store->case_file),
269 g_sheet_model_rows_inserted (G_SHEET_MODEL (store), casenum, 1);
274 changed_case_callback (GtkWidget *w, gint casenum, gpointer data)
276 PsppireDataStore *store ;
277 g_return_if_fail (data);
279 store = PSPPIRE_DATA_STORE (data);
281 g_sheet_model_range_changed (G_SHEET_MODEL (store),
288 delete_variables_callback (GObject *obj, gint var_num, gint n_vars, gpointer data)
290 PsppireDataStore *store ;
292 g_return_if_fail (data);
294 store = PSPPIRE_DATA_STORE (data);
296 g_sheet_model_columns_deleted (G_SHEET_MODEL (store), var_num, n_vars);
298 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
304 variable_changed_callback (GObject *obj, gint var_num, gpointer data)
306 PsppireDataStore *store;
308 g_return_if_fail (data);
310 store = PSPPIRE_DATA_STORE (data);
312 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
316 g_sheet_model_range_changed (G_SHEET_MODEL (store),
322 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
324 PsppireDataStore *store;
327 g_return_if_fail (data);
329 store = PSPPIRE_DATA_STORE (data);
333 struct variable *variable =
334 psppire_dict_get_variable (store->dict, var_num);
336 g_assert (variable != NULL);
338 posn = var_get_case_index (variable);
345 psppire_case_file_insert_values (store->case_file, 1, posn);
347 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
350 g_sheet_model_columns_inserted (G_SHEET_MODEL (store), var_num, 1);
355 dict_size_change_callback (GObject *obj,
356 gint posn, gint adjustment, gpointer data)
358 PsppireDataStore *store ;
360 g_return_if_fail (data);
362 store = PSPPIRE_DATA_STORE (data);
364 psppire_case_file_insert_values (store->case_file, adjustment, posn);
370 * psppire_data_store_new:
371 * @dict: The dictionary for this data_store.
374 * Return value: a new #PsppireDataStore
377 psppire_data_store_new (PsppireDict *dict)
379 PsppireDataStore *retval;
381 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
383 psppire_data_store_set_dictionary (retval, dict);
390 psppire_data_store_set_case_file (PsppireDataStore *data_store,
393 if ( data_store->case_file)
395 g_object_unref (data_store->case_file);
398 data_store->case_file = cf;
400 g_signal_connect (data_store->case_file, "cases-deleted",
401 G_CALLBACK (delete_cases_callback),
404 g_signal_connect (data_store->case_file, "case-inserted",
405 G_CALLBACK (insert_case_callback),
409 g_signal_connect (data_store->case_file, "case-changed",
410 G_CALLBACK (changed_case_callback),
417 * psppire_data_store_replace_set_dictionary:
418 * @data_store: The variable store
419 * @dict: The dictionary to set
421 * If a dictionary is already associated with the data-store, then it will be
425 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
427 data_store->dict = dict;
429 g_signal_connect (dict, "variable-inserted",
430 G_CALLBACK (insert_variable_callback),
433 g_signal_connect (dict, "variables-deleted",
434 G_CALLBACK (delete_variables_callback),
437 g_signal_connect (dict, "variable-changed",
438 G_CALLBACK (variable_changed_callback),
442 g_signal_connect (dict, "dict-size-changed",
443 G_CALLBACK (dict_size_change_callback),
446 /* The entire model has changed */
447 g_sheet_model_range_changed (G_SHEET_MODEL (data_store), -1, -1, -1, -1);
449 g_sheet_column_columns_changed (G_SHEET_COLUMN (data_store), 0, -1);
453 psppire_data_store_finalize (GObject *object)
457 (* parent_class->finalize) (object);
462 /* Insert a blank case before POSN */
464 psppire_data_store_insert_new_case (PsppireDataStore *ds, gint posn)
469 g_return_val_if_fail (ds, FALSE);
472 /* Opportunity for optimisation exists here when creating a blank case */
473 val_cnt = datasheet_get_column_cnt (ds->case_file->datasheet) ;
475 case_create (&cc, val_cnt);
477 memset ( case_data_rw_idx (&cc, 0), 0, val_cnt * MAX_SHORT_STRING);
479 for (v = 0 ; v < psppire_dict_get_var_cnt (ds->dict) ; ++v)
481 const struct variable *pv = psppire_dict_get_variable (ds->dict, v);
482 if ( var_is_alpha (pv))
485 case_data_rw (&cc, pv)->f = SYSMIS;
488 result = psppire_case_file_insert_case (ds->case_file, &cc, posn);
497 psppire_data_store_get_string (PsppireDataStore *store, gint row, gint column)
501 const struct fmt_spec *fp ;
502 const struct variable *pv ;
506 g_return_val_if_fail (store->dict, NULL);
507 g_return_val_if_fail (store->case_file, NULL);
509 if (column >= psppire_dict_get_var_cnt (store->dict))
512 if ( row >= psppire_case_file_get_case_count (store->case_file))
515 pv = psppire_dict_get_variable (store->dict, column);
519 idx = var_get_case_index (pv);
523 v = psppire_case_file_get_value (store->case_file, row, idx, NULL,
526 g_return_val_if_fail (v, NULL);
528 if ( store->show_labels)
530 const gchar *label = var_lookup_value_label (pv, v);
534 return pspp_locale_to_utf8 (label, -1, 0);
538 fp = var_get_write_format (pv);
540 s = g_string_sized_new (fp->w + 1);
541 g_string_set_size (s, fp->w);
543 memset (s->str, 0, fp->w);
545 g_assert (fp->w == s->len);
547 /* Converts binary value V into printable form in the exactly
548 FP->W character in buffer S according to format specification
549 FP. No null terminator is appended to the buffer. */
550 data_out (v, fp, s->str);
552 text = pspp_locale_to_utf8 (s->str, fp->w, 0);
553 g_string_free (s, TRUE);
563 psppire_data_store_clear_datum (GSheetModel *model,
567 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
570 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
572 const gint index = var_get_case_index (pv) ;
574 if ( var_is_numeric (pv))
577 memcpy (v.s, "", MAX_SHORT_STRING);
579 psppire_case_file_set_value (store->case_file, row, index, &v,
586 /* Attempts to update that part of the variable store which corresponds
587 to ROW, COL with the value TEXT.
588 Returns true if anything was updated, false otherwise.
591 psppire_data_store_set_string (PsppireDataStore *store,
592 const gchar *text, gint row, gint col)
594 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
595 g_return_val_if_fail (pv, FALSE);
598 /* Allow the user to insert a lot of blank cases, simply by skipping rows */
599 for (r = psppire_case_file_get_case_count (store->case_file); r <= row ; ++r)
604 psppire_case_array_insert_case (store->cases, r, 0, 0);
607 for (c = 0 ; c < psppire_dict_get_var_cnt (store->dict); ++c )
608 psppire_data_store_clear_datum (model, r, c);
612 psppire_case_file_data_in (store->case_file, row,
613 var_get_case_index (pv), ss_cstr (text),
614 var_get_write_format (pv));
621 psppire_data_store_set_font (PsppireDataStore *store,
622 const PangoFontDescription *fd)
624 g_return_if_fail (store);
625 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
627 store->font_desc = fd;
629 store->width_of_m = calc_m_width (fd);
631 g_signal_emit (store, signals [FONT_CHANGED], 0);
634 g_sheet_model_range_changed (G_SHEET_MODEL (store),
640 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
642 g_return_if_fail (store);
643 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
645 store->show_labels = show_labels;
647 g_sheet_model_range_changed (G_SHEET_MODEL (store),
653 psppire_data_store_clear (PsppireDataStore *data_store)
655 psppire_case_file_clear (data_store->case_file);
657 psppire_dict_clear (data_store->dict);
662 /* Return a casereader made from this datastore */
664 psppire_data_store_get_reader (PsppireDataStore *ds)
666 struct casereader *reader ;
668 reader = psppire_case_file_make_reader (ds->case_file);
675 /* Column related funcs */
678 geometry_get_column_count (const GSheetColumn *geom)
680 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
682 return MAX (MIN_COLUMNS, psppire_dict_get_var_cnt (ds->dict));
688 geometry_get_width (const GSheetColumn *geom, gint unit)
690 const struct variable *pv ;
691 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
693 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
694 return ds->width_of_m * 8 ;
696 pv = psppire_dict_get_variable (ds->dict, unit);
699 return ds->width_of_m * 8 ;
701 return ds->width_of_m * var_get_display_width (pv);
705 geometry_set_width (GSheetColumn *geom, gint unit, gint width)
707 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
709 struct variable *pv = psppire_dict_get_variable (ds->dict, unit);
711 var_set_display_width (pv, width / ds->width_of_m );
716 static GtkJustification
717 geometry_get_justification (const GSheetColumn *geom, gint unit)
719 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
720 const struct variable *pv ;
723 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
724 return GTK_JUSTIFY_LEFT;
726 pv = psppire_dict_get_variable (ds->dict, unit);
728 return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
729 : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
730 : GTK_JUSTIFY_CENTER);
734 static const gchar null_var_name[]=N_("var");
737 geometry_get_column_button_label (const GSheetColumn *geom, gint unit)
740 struct variable *pv ;
741 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
743 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
744 return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
746 pv = psppire_dict_get_variable (ds->dict, unit);
748 text = pspp_locale_to_utf8 (var_get_name (pv), -1, 0);
755 geometry_get_column_subtitle (const GSheetColumn *geom, gint unit)
758 const struct variable *v ;
759 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
761 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
764 v = psppire_dict_get_variable (ds->dict, unit);
766 if ( ! var_has_label (v))
769 text = pspp_locale_to_utf8 (var_get_label (v), -1, 0);
776 geometry_get_sensitivity (const GSheetColumn *geom, gint unit)
778 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
780 return (unit < psppire_dict_get_var_cnt (ds->dict));
785 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
787 iface->get_column_count = geometry_get_column_count;
788 iface->get_width = geometry_get_width;
789 iface->set_width = geometry_set_width;
790 iface->get_visibility = always_true;
791 iface->get_sensitivity = geometry_get_sensitivity;
792 iface->get_justification = geometry_get_justification;
793 iface->get_button_label = geometry_get_column_button_label;
794 iface->get_subtitle = geometry_get_column_subtitle;
798 /* Row related funcs */
801 geometry_get_row_count (const GSheetRow *geom, gpointer data)
803 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
805 return TRAILING_ROWS + psppire_case_file_get_case_count (ds->case_file);
810 geometry_get_height (const GSheetRow *geom, gint unit, gpointer data)
817 geometry_get_row_sensitivity (const GSheetRow *geom, gint unit, gpointer data)
819 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
822 return (unit < psppire_case_file_get_case_count (ds->case_file));
827 geometry_get_row_button_label (const GSheetRow *geom, gint unit, gpointer data)
831 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
834 TRAILING_ROWS + psppire_case_file_get_case_count (ds->case_file))
837 s = g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER);
839 text = pspp_locale_to_utf8 (s, -1, 0);
848 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
850 iface->get_row_count = geometry_get_row_count;
852 iface->get_height = geometry_get_height;
853 iface->set_height = 0;
854 iface->get_visibility = always_true;
855 iface->get_sensitivity = geometry_get_row_sensitivity;
857 iface->get_button_label = geometry_get_row_button_label;