1 /* psppire-data-store.c
3 PSPPIRE --- A Graphical User Interface for PSPP
4 Copyright (C) 2006 Free Software Foundation
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #define _(msgid) gettext (msgid)
26 #define N_(msgid) msgid
28 #include <data/datasheet.h>
29 #include <data/data-out.h>
30 #include <data/variable.h>
32 #include <gtksheet/gtksheet.h>
33 #include <gtksheet/gsheetmodel.h>
34 #include <gtksheet/gsheet-column-iface.h>
36 #include <pango/pango-context.h>
38 #include "psppire-data-store.h"
39 #include "psppire-case-file.h"
42 #include <data/dictionary.h>
43 #include <data/missing-values.h>
44 #include <data/value-labels.h>
45 #include <data/data-in.h>
48 static void psppire_data_store_init (PsppireDataStore *data_store);
49 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
50 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
51 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
52 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
54 static void psppire_data_store_finalize (GObject *object);
56 static gboolean psppire_data_store_clear_datum (GSheetModel *model,
57 gint row, gint column);
60 #define MIN_COLUMNS 10
62 #define TRAILING_ROWS 10
64 static GObjectClass *parent_class = NULL;
70 static guint signals [n_SIGNALS];
74 psppire_data_store_get_type (void)
76 static GType data_store_type = 0;
80 static const GTypeInfo data_store_info =
82 sizeof (PsppireDataStoreClass),
84 NULL, /* base_finalize */
85 (GClassInitFunc) psppire_data_store_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
88 sizeof (PsppireDataStore),
90 (GInstanceInitFunc) psppire_data_store_init,
93 static const GInterfaceInfo sheet_model_info =
95 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
100 static const GInterfaceInfo sheet_column_info =
102 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
107 static const GInterfaceInfo sheet_row_info =
109 (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
115 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
116 &data_store_info, 0);
118 g_type_add_interface_static (data_store_type,
122 g_type_add_interface_static (data_store_type,
126 g_type_add_interface_static (data_store_type,
131 return data_store_type;
136 psppire_data_store_class_init (PsppireDataStoreClass *class)
138 GObjectClass *object_class;
140 parent_class = g_type_class_peek_parent (class);
141 object_class = (GObjectClass*) class;
143 object_class->finalize = psppire_data_store_finalize;
145 signals [FONT_CHANGED] =
146 g_signal_new ("font_changed",
147 G_TYPE_FROM_CLASS (class),
151 g_cclosure_marshal_VOID__VOID,
159 psppire_data_store_get_var_count (const GSheetModel *model)
161 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
163 return psppire_dict_get_var_cnt (store->dict);
167 psppire_data_store_get_case_count (const GSheetModel *model)
169 const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
171 return psppire_case_file_get_case_count (store->case_file);
176 psppire_data_store_init (PsppireDataStore *data_store)
178 data_store->dict = 0;
179 data_store->case_file = 0;
180 data_store->width_of_m = 10;
183 const PangoFontDescription *
184 psppire_data_store_get_font_desc (const GSheetModel *model,
185 gint row, gint column)
187 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
189 return store->font_desc;
192 static inline gchar *
193 psppire_data_store_get_string_wrapper (const GSheetModel *model, gint row,
196 return psppire_data_store_get_string (PSPPIRE_DATA_STORE (model), row, column);
200 static inline gboolean
201 psppire_data_store_set_string_wrapper (GSheetModel *model,
203 gint row, gint column)
205 return psppire_data_store_set_string (PSPPIRE_DATA_STORE (model), text,
213 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
215 iface->free_strings = TRUE;
216 iface->get_string = psppire_data_store_get_string_wrapper;
217 iface->set_string = psppire_data_store_set_string_wrapper;
218 iface->clear_datum = psppire_data_store_clear_datum;
219 iface->is_editable = NULL;
220 iface->is_visible = NULL;
221 iface->get_foreground = NULL;
222 iface->get_background = NULL;
223 iface->get_font_desc = psppire_data_store_get_font_desc;
224 iface->get_cell_border = NULL;
225 iface->get_column_count = psppire_data_store_get_var_count;
226 iface->get_row_count = psppire_data_store_get_case_count;
230 gboolean always_true ()
237 delete_cases_callback (GtkWidget *w, gint first, gint n_cases, gpointer data)
239 PsppireDataStore *store ;
241 g_return_if_fail (data);
243 store = PSPPIRE_DATA_STORE (data);
245 g_assert (first >= 0);
247 g_sheet_model_rows_deleted (G_SHEET_MODEL (store), first, n_cases);
252 insert_case_callback (GtkWidget *w, gint casenum, gpointer data)
254 PsppireDataStore *store ;
256 g_return_if_fail (data);
258 store = PSPPIRE_DATA_STORE (data);
260 g_sheet_model_range_changed (G_SHEET_MODEL (store),
262 psppire_case_file_get_case_count (store->case_file),
265 g_sheet_model_rows_inserted (G_SHEET_MODEL (store), casenum, 1);
270 changed_case_callback (GtkWidget *w, gint casenum, gpointer data)
272 PsppireDataStore *store ;
273 g_return_if_fail (data);
275 store = PSPPIRE_DATA_STORE (data);
277 g_sheet_model_range_changed (G_SHEET_MODEL (store),
284 delete_variables_callback (GObject *obj, gint var_num, gint n_vars, gpointer data)
286 PsppireDataStore *store ;
288 g_return_if_fail (data);
290 store = PSPPIRE_DATA_STORE (data);
292 g_sheet_model_columns_deleted (G_SHEET_MODEL (store), var_num, n_vars);
294 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
300 variable_changed_callback (GObject *obj, gint var_num, gpointer data)
302 PsppireDataStore *store;
304 g_return_if_fail (data);
306 store = PSPPIRE_DATA_STORE (data);
308 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
312 g_sheet_model_range_changed (G_SHEET_MODEL (store),
318 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
320 PsppireDataStore *store;
323 g_return_if_fail (data);
325 store = PSPPIRE_DATA_STORE (data);
329 struct variable *variable =
330 psppire_dict_get_variable (store->dict, var_num);
332 g_assert (variable != NULL);
334 posn = var_get_case_index (variable);
341 psppire_case_file_insert_values (store->case_file, 1, posn);
343 g_sheet_column_columns_changed (G_SHEET_COLUMN (store),
346 g_sheet_model_columns_inserted (G_SHEET_MODEL (store), var_num, 1);
351 dict_size_change_callback (GObject *obj,
352 gint posn, gint adjustment, gpointer data)
354 PsppireDataStore *store ;
356 g_return_if_fail (data);
358 store = PSPPIRE_DATA_STORE (data);
360 psppire_case_file_insert_values (store->case_file, adjustment, posn);
366 * psppire_data_store_new:
367 * @dict: The dictionary for this data_store.
370 * Return value: a new #PsppireDataStore
373 psppire_data_store_new (PsppireDict *dict)
375 PsppireDataStore *retval;
377 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
379 psppire_data_store_set_dictionary (retval, dict);
386 psppire_data_store_set_case_file (PsppireDataStore *data_store,
389 if ( data_store->case_file)
391 g_object_unref (data_store->case_file);
394 data_store->case_file = cf;
396 g_signal_connect (data_store->case_file, "cases-deleted",
397 G_CALLBACK (delete_cases_callback),
400 g_signal_connect (data_store->case_file, "case-inserted",
401 G_CALLBACK (insert_case_callback),
405 g_signal_connect (data_store->case_file, "case-changed",
406 G_CALLBACK (changed_case_callback),
413 * psppire_data_store_replace_set_dictionary:
414 * @data_store: The variable store
415 * @dict: The dictionary to set
417 * If a dictionary is already associated with the data-store, then it will be
421 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
423 data_store->dict = dict;
425 g_signal_connect (dict, "variable-inserted",
426 G_CALLBACK (insert_variable_callback),
429 g_signal_connect (dict, "variables-deleted",
430 G_CALLBACK (delete_variables_callback),
433 g_signal_connect (dict, "variable-changed",
434 G_CALLBACK (variable_changed_callback),
438 g_signal_connect (dict, "dict-size-changed",
439 G_CALLBACK (dict_size_change_callback),
442 /* The entire model has changed */
443 g_sheet_model_range_changed (G_SHEET_MODEL (data_store), -1, -1, -1, -1);
445 g_sheet_column_columns_changed (G_SHEET_COLUMN (data_store), 0, -1);
449 psppire_data_store_finalize (GObject *object)
453 (* parent_class->finalize) (object);
458 /* Insert a blank case before POSN */
460 psppire_data_store_insert_new_case (PsppireDataStore *ds, gint posn)
465 g_return_val_if_fail (ds, FALSE);
468 /* Opportunity for optimisation exists here when creating a blank case */
469 val_cnt = datasheet_get_column_cnt (ds->case_file->datasheet) ;
471 case_create (&cc, val_cnt);
473 memset ( case_data_rw_idx (&cc, 0), 0, val_cnt * MAX_SHORT_STRING);
475 for (v = 0 ; v < psppire_dict_get_var_cnt (ds->dict) ; ++v)
477 const struct variable *pv = psppire_dict_get_variable (ds->dict, v);
478 if ( var_is_alpha (pv))
481 case_data_rw (&cc, pv)->f = SYSMIS;
484 result = psppire_case_file_insert_case (ds->case_file, &cc, posn);
493 psppire_data_store_get_string (PsppireDataStore *store, gint row, gint column)
497 const struct fmt_spec *fp ;
498 const struct variable *pv ;
502 g_return_val_if_fail (store->dict, NULL);
503 g_return_val_if_fail (store->case_file, NULL);
505 if (column >= psppire_dict_get_var_cnt (store->dict))
508 if ( row >= psppire_case_file_get_case_count (store->case_file))
511 pv = psppire_dict_get_variable (store->dict, column);
515 idx = var_get_case_index (pv);
519 v = psppire_case_file_get_value (store->case_file, row, idx, NULL,
522 g_return_val_if_fail (v, NULL);
524 if ( store->show_labels)
526 const gchar *label = var_lookup_value_label (pv, v);
530 return pspp_locale_to_utf8 (label, -1, 0);
534 fp = var_get_write_format (pv);
536 s = g_string_sized_new (fp->w + 1);
537 g_string_set_size (s, fp->w);
539 memset (s->str, 0, fp->w);
541 g_assert (fp->w == s->len);
543 /* Converts binary value V into printable form in the exactly
544 FP->W character in buffer S according to format specification
545 FP. No null terminator is appended to the buffer. */
546 data_out (v, fp, s->str);
548 text = pspp_locale_to_utf8 (s->str, fp->w, 0);
549 g_string_free (s, TRUE);
559 psppire_data_store_clear_datum (GSheetModel *model,
563 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
566 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
568 const gint index = var_get_case_index (pv) ;
570 if ( var_is_numeric (pv))
573 memcpy (v.s, "", MAX_SHORT_STRING);
575 psppire_case_file_set_value (store->case_file, row, index, &v,
582 /* Attempts to update that part of the variable store which corresponds
583 to ROW, COL with the value TEXT.
584 Returns true if anything was updated, false otherwise.
587 psppire_data_store_set_string (PsppireDataStore *store,
588 const gchar *text, gint row, gint col)
590 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
591 g_return_val_if_fail (pv, FALSE);
594 /* Allow the user to insert a lot of blank cases, simply by skipping rows */
595 for (r = psppire_case_file_get_case_count (store->case_file); r <= row ; ++r)
600 psppire_case_array_insert_case (store->cases, r, 0, 0);
603 for (c = 0 ; c < psppire_dict_get_var_cnt (store->dict); ++c )
604 psppire_data_store_clear_datum (model, r, c);
608 psppire_case_file_data_in (store->case_file, row,
609 var_get_case_index (pv), ss_cstr (text),
610 var_get_write_format (pv));
617 psppire_data_store_set_font (PsppireDataStore *store,
618 const PangoFontDescription *fd)
620 g_return_if_fail (store);
621 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
623 store->font_desc = fd;
625 store->width_of_m = calc_m_width (fd);
627 g_signal_emit (store, signals [FONT_CHANGED], 0);
630 g_sheet_model_range_changed (G_SHEET_MODEL (store),
636 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
638 g_return_if_fail (store);
639 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
641 store->show_labels = show_labels;
643 g_sheet_model_range_changed (G_SHEET_MODEL (store),
649 psppire_data_store_clear (PsppireDataStore *data_store)
651 psppire_case_file_clear (data_store->case_file);
653 psppire_dict_clear (data_store->dict);
658 /* Return a casereader made from this datastore */
660 psppire_data_store_get_reader (PsppireDataStore *ds)
662 struct casereader *reader ;
664 reader = psppire_case_file_make_reader (ds->case_file);
671 /* Column related funcs */
674 geometry_get_column_count (const GSheetColumn *geom)
676 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
678 return MAX (MIN_COLUMNS, psppire_dict_get_var_cnt (ds->dict));
684 geometry_get_width (const GSheetColumn *geom, gint unit)
686 const struct variable *pv ;
687 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
689 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
690 return ds->width_of_m * 8 ;
692 pv = psppire_dict_get_variable (ds->dict, unit);
695 return ds->width_of_m * 8 ;
697 return ds->width_of_m * var_get_display_width (pv);
701 geometry_set_width (GSheetColumn *geom, gint unit, gint width)
703 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
705 struct variable *pv = psppire_dict_get_variable (ds->dict, unit);
707 var_set_display_width (pv, width / ds->width_of_m );
712 static GtkJustification
713 geometry_get_justification (const GSheetColumn *geom, gint unit)
715 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
716 const struct variable *pv ;
719 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
720 return GTK_JUSTIFY_LEFT;
722 pv = psppire_dict_get_variable (ds->dict, unit);
724 return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
725 : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
726 : GTK_JUSTIFY_CENTER);
730 static const gchar null_var_name[]=N_("var");
733 geometry_get_column_button_label (const GSheetColumn *geom, gint unit)
736 struct variable *pv ;
737 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
739 if ( unit >= psppire_dict_get_var_cnt (ds->dict) )
740 return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
742 pv = psppire_dict_get_variable (ds->dict, unit);
744 text = pspp_locale_to_utf8 (var_get_name (pv), -1, 0);
751 geometry_get_sensitivity (const GSheetColumn *geom, gint unit)
753 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
755 return (unit < psppire_dict_get_var_cnt (ds->dict));
760 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
762 iface->get_column_count = geometry_get_column_count;
763 iface->get_width = geometry_get_width;
764 iface->set_width = geometry_set_width;
765 iface->get_visibility = always_true;
766 iface->get_sensitivity = geometry_get_sensitivity;
767 iface->get_justification = geometry_get_justification;
768 iface->get_button_label = geometry_get_column_button_label;
772 /* Row related funcs */
775 geometry_get_row_count (const GSheetRow *geom, gpointer data)
777 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
779 return TRAILING_ROWS + psppire_case_file_get_case_count (ds->case_file);
784 geometry_get_height (const GSheetRow *geom, gint unit, gpointer data)
791 geometry_get_row_sensitivity (const GSheetRow *geom, gint unit, gpointer data)
793 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
796 return (unit < psppire_case_file_get_case_count (ds->case_file));
801 geometry_get_row_button_label (const GSheetRow *geom, gint unit, gpointer data)
805 PsppireDataStore *ds = PSPPIRE_DATA_STORE (geom);
808 TRAILING_ROWS + psppire_case_file_get_case_count (ds->case_file))
811 s = g_strdup_printf (_("%d"), unit);
813 text = pspp_locale_to_utf8 (s, -1, 0);
822 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
824 iface->get_row_count = geometry_get_row_count;
826 iface->get_height = geometry_get_height;
827 iface->set_height = 0;
828 iface->get_visibility = always_true;
829 iface->get_sensitivity = geometry_get_row_sensitivity;
831 iface->get_button_label = geometry_get_row_button_label;