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/casefile.h>
29 #include <data/case.h>
30 #include <data/data-out.h>
31 #include <data/variable.h>
33 #include <gtksheet/gtksheet.h>
34 #include <gtksheet/gsheetmodel.h>
35 #include <gtksheet/gsheet-column-iface.h>
37 #include <pango/pango-context.h>
39 #include "psppire-data-store.h"
40 #include "psppire-case-file.h"
43 #include <data/dictionary.h>
44 #include <data/missing-values.h>
45 #include <data/value-labels.h>
46 #include <data/data-in.h>
48 #include <data/file-handle-def.h>
49 #include <data/sys-file-writer.h>
53 static void psppire_data_store_init (PsppireDataStore *data_store);
54 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
55 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
56 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
57 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
59 static void psppire_data_store_finalize (GObject *object);
61 static gchar *psppire_data_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
63 static gboolean psppire_data_store_set_string(GSheetModel *model,
64 const gchar *text, gint row, gint column);
66 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
67 gint row, gint column);
70 #define MIN_COLUMNS 10
72 #define TRAILING_ROWS 10
74 static GObjectClass *parent_class = NULL;
80 static guint signal[n_SIGNALS];
84 psppire_data_store_get_type (void)
86 static GType data_store_type = 0;
90 static const GTypeInfo data_store_info =
92 sizeof (PsppireDataStoreClass),
94 NULL, /* base_finalize */
95 (GClassInitFunc) psppire_data_store_class_init,
96 NULL, /* class_finalize */
97 NULL, /* class_data */
98 sizeof (PsppireDataStore),
100 (GInstanceInitFunc) psppire_data_store_init,
103 static const GInterfaceInfo sheet_model_info =
105 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
110 static const GInterfaceInfo sheet_column_info =
112 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
117 static const GInterfaceInfo sheet_row_info =
119 (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
125 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
126 &data_store_info, 0);
128 g_type_add_interface_static (data_store_type,
132 g_type_add_interface_static (data_store_type,
136 g_type_add_interface_static (data_store_type,
141 return data_store_type;
146 psppire_data_store_class_init (PsppireDataStoreClass *class)
148 GObjectClass *object_class;
150 parent_class = g_type_class_peek_parent (class);
151 object_class = (GObjectClass*) class;
153 object_class->finalize = psppire_data_store_finalize;
155 signal[FONT_CHANGED] =
156 g_signal_new ("font_changed",
157 G_TYPE_FROM_CLASS(class),
161 g_cclosure_marshal_VOID__VOID,
169 psppire_data_store_get_var_count (const GSheetModel *model)
171 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
173 return psppire_dict_get_var_cnt(store->dict);
177 psppire_data_store_get_case_count (const GSheetModel *model)
179 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
181 return psppire_case_file_get_case_count(store->case_file);
186 psppire_data_store_init (PsppireDataStore *data_store)
188 data_store->dict = 0;
189 data_store->case_file = 0;
190 data_store->width_of_m = 10;
193 const PangoFontDescription *
194 psppire_data_store_get_font_desc(const GSheetModel *model,
195 gint row, gint column)
197 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
199 return store->font_desc;
204 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
206 iface->free_strings = TRUE;
207 iface->get_string = psppire_data_store_get_string;
208 iface->set_string = psppire_data_store_set_string;
209 iface->clear_datum = psppire_data_store_clear_datum;
210 iface->is_editable = NULL;
211 iface->is_visible = NULL;
212 iface->get_foreground = NULL;
213 iface->get_background = NULL;
214 iface->get_font_desc = psppire_data_store_get_font_desc;
215 iface->get_cell_border = NULL;
216 iface->get_column_count = psppire_data_store_get_var_count;
217 iface->get_row_count = psppire_data_store_get_case_count;
221 gboolean always_true()
228 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
230 PsppireDataStore *store ;
232 g_return_if_fail (data);
234 store = PSPPIRE_DATA_STORE(data);
236 g_assert(first >= 0);
238 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
243 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
245 PsppireDataStore *store ;
247 g_return_if_fail (data);
249 store = PSPPIRE_DATA_STORE(data);
251 g_sheet_model_range_changed (G_SHEET_MODEL(store),
253 psppire_case_file_get_case_count(store->case_file),
256 g_sheet_model_rows_inserted (G_SHEET_MODEL(store), casenum, 1);
261 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
263 PsppireDataStore *store ;
264 g_return_if_fail (data);
266 store = PSPPIRE_DATA_STORE(data);
268 g_sheet_model_range_changed (G_SHEET_MODEL(store),
275 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
277 PsppireDataStore *store ;
279 g_return_if_fail (data);
281 store = PSPPIRE_DATA_STORE(data);
283 g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
285 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
290 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
292 PsppireDataStore *store;
295 g_return_if_fail (data);
297 store = PSPPIRE_DATA_STORE(data);
301 struct variable *variable;
302 variable = psppire_dict_get_variable(store->dict, var_num);
304 posn = var_get_case_index (variable);
311 psppire_case_file_insert_values(store->case_file, 1, posn);
313 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
316 g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
321 dict_size_change_callback(GObject *obj,
322 gint posn, gint adjustment, gpointer data)
324 PsppireDataStore *store ;
326 g_return_if_fail (data);
328 store = PSPPIRE_DATA_STORE(data);
330 psppire_case_file_insert_values (store->case_file, adjustment, posn);
336 * psppire_data_store_new:
337 * @dict: The dictionary for this data_store.
340 * Return value: a new #PsppireDataStore
343 psppire_data_store_new (PsppireDict *dict)
345 PsppireDataStore *retval;
347 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
349 psppire_data_store_set_dictionary(retval, dict);
358 * psppire_data_store_replace_set_dictionary:
359 * @data_store: The variable store
360 * @dict: The dictionary to set
362 * If a dictionary is already associated with the data-store, then it will be
366 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
368 gint var_cnt = psppire_dict_get_next_value_idx(dict);
370 data_store->dict = dict;
372 if ( data_store->case_file)
374 g_object_unref(data_store->case_file);
375 data_store->case_file = 0;
378 data_store->case_file = psppire_case_file_new(var_cnt);
380 g_signal_connect(data_store->case_file, "cases-deleted",
381 G_CALLBACK(delete_cases_callback),
384 g_signal_connect(data_store->case_file, "case-inserted",
385 G_CALLBACK(insert_case_callback),
389 g_signal_connect(data_store->case_file, "case-changed",
390 G_CALLBACK(changed_case_callback),
393 g_signal_connect(dict, "variable-inserted",
394 G_CALLBACK(insert_variable_callback),
397 g_signal_connect(dict, "variables-deleted",
398 G_CALLBACK(delete_variables_callback),
401 g_signal_connect (dict, "dict-size-changed",
402 G_CALLBACK(dict_size_change_callback),
405 /* The entire model has changed */
406 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
408 g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
412 psppire_data_store_finalize (GObject *object)
416 (* parent_class->finalize) (object);
421 /* Insert a blank case before POSN */
423 psppire_data_store_insert_new_case(PsppireDataStore *ds, gint posn)
428 g_return_val_if_fail (ds, FALSE);
431 /* Opportunity for optimisation exists here when creating a blank case */
432 val_cnt = casefile_get_value_cnt(ds->case_file->flexifile) ;
434 case_create (&cc, val_cnt);
436 memset ( case_data_rw_idx (&cc, 0), 0, val_cnt * MAX_SHORT_STRING);
438 for (v = 0 ; v < psppire_dict_get_var_cnt (ds->dict) ; ++v)
440 const struct variable *pv = psppire_dict_get_variable (ds->dict, v);
441 if ( var_is_alpha (pv))
444 case_data_rw (&cc, pv)->f = SYSMIS;
447 result = psppire_case_file_insert_case (ds->case_file, &cc, posn);
456 psppire_data_store_get_string (const GSheetModel *model, gint row, gint column)
460 const struct fmt_spec *fp ;
461 const struct variable *pv ;
462 const union value *v ;
464 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
466 g_return_val_if_fail (store->dict, NULL);
467 g_return_val_if_fail (store->case_file, NULL);
469 if (column >= psppire_dict_get_var_cnt (store->dict))
472 if ( row >= psppire_case_file_get_case_count (store->case_file))
475 pv = psppire_dict_get_variable (store->dict, column);
477 idx = var_get_case_index (pv);
479 v = psppire_case_file_get_value (store->case_file, row, idx);
481 g_return_val_if_fail(v, NULL);
483 if ( store->show_labels)
485 const struct val_labs * vl = var_get_value_labels (pv);
488 if ( (label = val_labs_find(vl, *v)) )
490 return pspp_locale_to_utf8(label, -1, 0);
494 fp = var_get_write_format (pv);
496 s = g_string_sized_new (fp->w + 1);
497 g_string_set_size (s, fp->w);
499 memset (s->str, 0, fp->w);
501 g_assert (fp->w == s->len);
503 /* Converts binary value V into printable form in the exactly
504 FP->W character in buffer S according to format specification
505 FP. No null terminator is appended to the buffer. */
506 data_out (v, fp, s->str);
508 text = pspp_locale_to_utf8 (s->str, fp->w, 0);
509 g_string_free (s, TRUE);
518 psppire_data_store_clear_datum (GSheetModel *model,
522 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
525 const struct variable *pv = psppire_dict_get_variable (store->dict, col);
527 const gint index = var_get_case_index (pv) ;
529 if ( var_is_numeric (pv))
532 memcpy(v.s, "", MAX_SHORT_STRING);
534 psppire_case_file_set_value(store->case_file, row, index, &v,
541 /* Attempts to update that part of the variable store which corresponds
542 to ROW, COL with the value TEXT.
543 Returns true if anything was updated, false otherwise.
546 psppire_data_store_set_string(GSheetModel *model,
547 const gchar *text, gint row, gint col)
549 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
551 const struct variable *pv = psppire_dict_get_variable(store->dict, col);
552 g_return_val_if_fail(pv, FALSE);
555 /* Allow the user to insert a lot of blank cases, simply by skipping rows */
556 for(r = psppire_case_file_get_case_count(store->case_file); r <= row ; ++r)
561 psppire_case_array_insert_case(store->cases, r, 0, 0);
564 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
565 psppire_data_store_clear_datum(model, r, c);
569 psppire_case_file_data_in (store->case_file, row,
570 var_get_case_index (pv), ss_cstr (text),
571 var_get_write_format (pv));
578 psppire_data_store_set_font(PsppireDataStore *store,
579 const PangoFontDescription *fd)
581 g_return_if_fail (store);
582 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
584 store->font_desc = fd;
586 store->width_of_m = calc_m_width(fd);
588 g_signal_emit(store, signal[FONT_CHANGED], 0);
591 g_sheet_model_range_changed (G_SHEET_MODEL(store),
597 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
599 g_return_if_fail (store);
600 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
602 store->show_labels = show_labels;
604 g_sheet_model_range_changed (G_SHEET_MODEL(store),
610 /* FIXME: There's no reason to actually have this function.
611 It should be done by a procedure */
613 psppire_data_store_create_system_file(PsppireDataStore *store,
614 struct file_handle *handle)
617 const struct sfm_write_options wo = {
618 true, /* writeable */
619 false, /* dont compress */
623 struct sfm_writer *writer ;
627 writer = sfm_open_writer(handle, store->dict->dict, wo);
633 var_cnt = psppire_data_store_get_var_count (G_SHEET_MODEL(store));
635 for (i = 0 ; i < psppire_case_file_get_case_count(store->case_file); ++i )
639 case_create (&c, var_cnt);
640 psppire_case_file_get_case (store->case_file, i, &c);
641 sfm_write_case (writer, &c);
646 sfm_close_writer(writer);
652 psppire_data_store_clear(PsppireDataStore *data_store)
654 psppire_case_file_clear(data_store->case_file);
656 psppire_dict_clear(data_store->dict);
662 /* Column related funcs */
665 geometry_get_column_count(const GSheetColumn *geom)
667 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
669 return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
675 geometry_get_width(const GSheetColumn *geom, gint unit)
677 const struct variable *pv ;
678 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
680 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
681 return ds->width_of_m * 8 ;
683 pv = psppire_dict_get_variable (ds->dict, unit);
686 return ds->width_of_m * 8 ;
688 return ds->width_of_m * var_get_display_width (pv);
692 geometry_set_width(GSheetColumn *geom, gint unit, gint width)
694 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
696 struct variable *pv = psppire_dict_get_variable (ds->dict, unit);
698 var_set_display_width (pv, width / ds->width_of_m );
703 static GtkJustification
704 geometry_get_justification(const GSheetColumn *geom, gint unit)
706 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
707 const struct variable *pv ;
710 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
711 return GTK_JUSTIFY_LEFT;
713 pv = psppire_dict_get_variable(ds->dict, unit);
715 return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
716 : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
717 : GTK_JUSTIFY_CENTER);
721 static const gchar null_var_name[]=N_("var");
724 geometry_get_column_button_label(const GSheetColumn *geom, gint unit)
727 struct variable *pv ;
728 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
730 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
731 return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
733 pv = psppire_dict_get_variable (ds->dict, unit);
735 text = pspp_locale_to_utf8 (var_get_name (pv), -1, 0);
742 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
744 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
746 return (unit < psppire_dict_get_var_cnt(ds->dict));
751 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
753 iface->get_column_count = geometry_get_column_count;
754 iface->get_width = geometry_get_width;
755 iface->set_width = geometry_set_width;
756 iface->get_visibility = always_true;
757 iface->get_sensitivity = geometry_get_sensitivity;
758 iface->get_justification = geometry_get_justification;
759 iface->get_button_label = geometry_get_column_button_label;
763 /* Row related funcs */
766 geometry_get_row_count(const GSheetRow *geom, gpointer data)
768 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
770 return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
775 geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
782 geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
784 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
787 return (unit < psppire_case_file_get_case_count(ds->case_file));
792 geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
796 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
799 TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
802 s = g_strdup_printf(_("%d"), unit);
804 text = pspp_locale_to_utf8(s, -1, 0);
813 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
815 iface->get_row_count = geometry_get_row_count;
817 iface->get_height = geometry_get_height;
818 iface->set_height = 0;
819 iface->get_visibility = always_true;
820 iface->get_sensitivity = geometry_get_row_sensitivity;
822 iface->get_button_label = geometry_get_row_button_label;