1 /* psppire-data-store.c
3 PSPPIRE --- A Graphical User Interface for PSPP
4 Copyright (C) 2006 Free Software Foundation
5 Written by John Darrington
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #define _(msgid) gettext(msgid)
27 #define N_(msgid) msgid
29 #include <data/casefile.h>
30 #include <data/case.h>
32 #include <gtksheet/gtksheet.h>
33 #include <gtksheet/gsheetmodel.h>
34 #include <gtksheet/gsheet-column-iface.h>
36 #include "psppire-variable.h"
37 #include "psppire-data-store.h"
40 #include <data/dictionary.h>
41 #include <data/missing-values.h>
42 #include <data/value-labels.h>
43 #include <data/data-in.h>
45 #include <data/file-handle-def.h>
46 #include <data/sys-file-writer.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 (GSheetModelIface *iface);
53 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
54 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
56 static void psppire_data_store_finalize (GObject *object);
58 static gchar *psppire_data_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
60 static gboolean psppire_data_store_set_string(GSheetModel *model,
61 const gchar *text, gint row, gint column);
63 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
64 gint row, gint column);
67 #define MIN_COLUMNS 10
69 #define TRAILING_ROWS 10
71 static GObjectClass *parent_class = NULL;
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;
135 psppire_data_store_class_init (PsppireDataStoreClass *class)
137 GObjectClass *object_class;
139 parent_class = g_type_class_peek_parent (class);
140 object_class = (GObjectClass*) class;
142 object_class->finalize = psppire_data_store_finalize;
148 psppire_data_store_get_var_count (const GSheetModel *model)
150 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
152 return psppire_dict_get_var_cnt(store->dict);
156 psppire_data_store_get_case_count (const GSheetModel *model)
158 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
160 return psppire_case_file_get_case_count(store->case_file);
165 psppire_data_store_init (PsppireDataStore *data_store)
167 data_store->dict = 0;
168 data_store->case_file = 0;
171 const PangoFontDescription *
172 psppire_data_store_get_font_desc(const GSheetModel *model,
173 gint row, gint column)
175 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
177 return store->font_desc;
182 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
184 iface->free_strings = TRUE;
185 iface->get_string = psppire_data_store_get_string;
186 iface->set_string = psppire_data_store_set_string;
187 iface->clear_datum = psppire_data_store_clear_datum;
188 iface->is_editable = NULL;
189 iface->is_visible = NULL;
190 iface->get_foreground = NULL;
191 iface->get_background = NULL;
192 iface->get_font_desc = psppire_data_store_get_font_desc;
193 iface->get_cell_border = NULL;
194 iface->get_column_count = psppire_data_store_get_var_count;
195 iface->get_row_count = psppire_data_store_get_case_count;
199 gboolean always_true()
206 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
208 PsppireDataStore *store ;
210 g_return_if_fail (data);
212 store = PSPPIRE_DATA_STORE(data);
214 g_assert(first >= 0);
216 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
221 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
223 PsppireDataStore *store ;
225 g_return_if_fail (data);
227 store = PSPPIRE_DATA_STORE(data);
229 g_sheet_model_range_changed (G_SHEET_MODEL(store),
231 psppire_case_file_get_case_count(store->case_file),
234 g_sheet_model_rows_inserted (G_SHEET_MODEL(store), casenum, 1);
239 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
241 PsppireDataStore *store ;
242 g_return_if_fail (data);
244 store = PSPPIRE_DATA_STORE(data);
246 g_sheet_model_range_changed (G_SHEET_MODEL(store),
253 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
255 PsppireDataStore *store ;
257 g_return_if_fail (data);
259 store = PSPPIRE_DATA_STORE(data);
261 g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
263 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
268 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
270 PsppireDataStore *store;
272 g_return_if_fail (data);
274 store = PSPPIRE_DATA_STORE(data);
277 g_sheet_model_range_changed (G_SHEET_MODEL(store),
279 psppire_case_array_get_n_cases(store->cases),
284 psppire_case_array_resize(store->cases,
285 dict_get_next_value_idx (store->dict->dict));
288 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
292 g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
299 * psppire_data_store_new:
300 * @dict: The dictionary for this data_store.
303 * Return value: a new #PsppireDataStore
306 psppire_data_store_new (PsppireDict *dict)
308 PsppireDataStore *retval;
310 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
312 psppire_data_store_set_dictionary(retval, dict);
321 * psppire_data_store_replace_set_dictionary:
322 * @data_store: The variable store
323 * @dict: The dictionary to set
325 * If a dictionary is already associated with the data-store, then it will be
329 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
331 gint var_cnt = psppire_dict_get_next_value_idx(dict);
333 if ( data_store->dict ) g_object_unref(data_store->dict);
336 data_store->dict = dict;
338 if ( data_store->case_file)
340 g_object_unref(data_store->case_file);
341 data_store->case_file = 0;
344 data_store->case_file = psppire_case_file_new(var_cnt);
346 g_signal_connect(data_store->case_file, "cases-deleted",
347 G_CALLBACK(delete_cases_callback),
350 g_signal_connect(data_store->case_file, "case-inserted",
351 G_CALLBACK(insert_case_callback),
355 g_signal_connect(data_store->case_file, "case-changed",
356 G_CALLBACK(changed_case_callback),
360 g_signal_connect(dict, "variable-inserted",
361 G_CALLBACK(insert_variable_callback),
364 g_signal_connect(dict, "variables-deleted",
365 G_CALLBACK(delete_variables_callback),
368 /* The entire model has changed */
369 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
371 g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
375 psppire_data_store_finalize (GObject *object)
379 (* parent_class->finalize) (object);
384 psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
388 const struct fmt_spec *fp ;
389 const struct PsppireVariable *pv ;
390 const union value *v ;
392 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
394 g_return_val_if_fail(store->dict, NULL);
395 g_return_val_if_fail(store->case_file, NULL);
397 if (column >= psppire_dict_get_var_cnt(store->dict))
400 if ( row >= psppire_case_file_get_case_count(store->case_file))
403 pv = psppire_dict_get_variable(store->dict, column);
405 idx = psppire_variable_get_fv(pv);
407 v = psppire_case_file_get_value(store->case_file, row, idx);
409 if ( store->show_labels)
411 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
414 if ( (label = val_labs_find(vl, *v)) )
416 return pspp_locale_to_utf8(label, -1, 0);
420 fp = psppire_variable_get_write_spec(pv);
422 s = g_string_sized_new (fp->w + 1);
423 g_string_set_size(s, fp->w);
425 memset(s->str, 0, fp->w);
427 g_assert(fp->w == s->len);
429 /* Converts binary value V into printable form in the exactly
430 FP->W character in buffer S according to format specification
431 FP. No null terminator is appended to the buffer. */
432 data_out (s->str, fp, v);
434 text = pspp_locale_to_utf8(s->str, fp->w, 0);
435 g_string_free(s, TRUE);
442 psppire_data_store_clear_datum(GSheetModel *model,
446 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
449 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
451 const gint index = psppire_variable_get_fv(pv) ;
453 if ( psppire_variable_get_type(pv) == NUMERIC)
456 memcpy(v.s, "", MAX_SHORT_STRING);
458 psppire_case_file_set_value(store->case_file, row, index, &v);
464 /* Attempts to update that part of the variable store which corresponds
465 to ROW, COL with the value TEXT.
466 Returns true if anything was updated, false otherwise.
469 psppire_data_store_set_string(GSheetModel *model,
470 const gchar *text, gint row, gint col)
472 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
474 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
475 g_return_val_if_fail(pv, FALSE);
478 /* Allow the user to insert a lot of blank cases, simply by skipping rows */
479 for(r = psppire_case_file_get_case_count(store->case_file); r <= row ; ++r)
484 psppire_case_array_insert_case(store->cases, r, 0, 0);
487 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
488 psppire_data_store_clear_datum(model, r, c);
493 const gint index = psppire_variable_get_fv(pv);
497 d_in.e = text + strlen(text);
499 d_in.f1 = d_in.f2 = 0;
500 d_in.format = * psppire_variable_get_write_spec(pv);
504 if ( ! data_in(&d_in) )
506 g_warning("Cannot encode string");
511 psppire_case_file_set_value(store->case_file, row, index, &d_in) ;
519 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
521 g_return_if_fail (store);
522 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
524 store->font_desc = fd;
525 g_sheet_model_range_changed (G_SHEET_MODEL(store),
531 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
533 g_return_if_fail (store);
534 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
536 store->show_labels = show_labels;
538 g_sheet_model_range_changed (G_SHEET_MODEL(store),
545 write_case(const struct ccase *cc,
548 struct sfm_writer *writer = aux;
550 if ( ! sfm_write_case(writer, cc) )
558 psppire_data_store_create_system_file(PsppireDataStore *store,
559 struct file_handle *handle)
561 const struct sfm_write_options wo = {
562 true, /* writeable */
563 false, /* dont compress */
567 struct sfm_writer *writer ;
571 writer = sfm_open_writer(handle, store->dict->dict, wo);
577 psppire_case_array_iterate_case(store->cases, write_case, writer);
580 sfm_close_writer(writer);
586 psppire_data_store_clear(PsppireDataStore *data_store)
588 psppire_case_file_clear(data_store->case_file);
590 psppire_dict_clear(data_store->dict);
596 /* Column related funcs */
599 geometry_get_column_count(const GSheetColumn *geom, gpointer data)
601 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
603 return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
606 /* Return the width that an 'M' character would occupy when typeset at
609 M_width(const GtkSheet *sheet, gint row, gint col)
611 GtkSheetCellAttr attributes;
613 /* FIXME: make this a member of the data store */
614 static PangoLayout *layout = 0;
616 gtk_sheet_get_attributes(sheet, row, col, &attributes);
619 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
623 pango_layout_set_font_description (layout,
624 attributes.font_desc);
626 pango_layout_get_extents (layout, NULL, &rect);
629 g_object_unref(G_OBJECT(layout));
632 return PANGO_PIXELS(rect.width);
636 /* Return the number of pixels corresponding to a column of
639 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
641 return (M_width(sheet, 0, column) * width);
646 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
648 const struct PsppireVariable *pv ;
649 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
651 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
654 /* FIXME: We can optimise this by caching the widths until they're resized */
655 pv = psppire_dict_get_variable(ds->dict, unit);
657 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
664 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
666 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
668 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
670 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
675 static GtkJustification
676 geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
678 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
679 const struct PsppireVariable *pv ;
682 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
683 return GTK_JUSTIFY_LEFT;
685 pv = psppire_dict_get_variable(ds->dict, unit);
687 /* Kludge: Happily GtkJustification is defined similarly
688 to enum alignment from pspp/variable.h */
689 return psppire_variable_get_alignment(pv);
693 static const gchar null_var_name[]=N_("var");
696 geometry_get_column_button_label(const GSheetColumn *geom, gint unit,
700 struct PsppireVariable *pv ;
701 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
703 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
704 return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
706 pv = psppire_dict_get_variable(ds->dict, unit);
708 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
715 geometry_get_sensitivity(const GSheetColumn *geom, gint unit, gpointer data)
717 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
720 return (unit < psppire_dict_get_var_cnt(ds->dict));
725 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
727 iface->get_column_count = geometry_get_column_count;
728 iface->get_width = geometry_get_width;
729 iface->set_width = geometry_set_width;
730 iface->get_visibility = always_true;
731 iface->get_sensitivity = geometry_get_sensitivity;
732 iface->get_justification = geometry_get_justification;
734 iface->get_button_label = geometry_get_column_button_label;
738 /* Row related funcs */
741 geometry_get_row_count(const GSheetRow *geom, gpointer data)
743 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
745 return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
750 geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
757 geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
759 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
762 return (unit < psppire_case_file_get_case_count(ds->case_file));
767 geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
771 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
774 TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
777 s = g_strdup_printf(_("%d"), unit);
779 text = pspp_locale_to_utf8(s, -1, 0);
788 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
790 iface->get_row_count = geometry_get_row_count;
792 iface->get_height = geometry_get_height;
793 iface->set_height = 0;
794 iface->get_visibility = always_true;
795 iface->get_sensitivity = geometry_get_row_sensitivity;
797 iface->get_button_label = geometry_get_row_button_label;