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;
273 g_return_if_fail (data);
275 store = PSPPIRE_DATA_STORE(data);
279 struct PsppireVariable *variable;
280 variable = psppire_dict_get_variable(store->dict, var_num);
282 posn = psppire_variable_get_fv(variable);
289 psppire_case_file_insert_values(store->case_file, 1, posn);
291 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
294 g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
299 dict_size_change_callback(GObject *obj,
300 gint posn, gint adjustment, gpointer data)
302 PsppireDataStore *store ;
304 g_return_if_fail (data);
306 store = PSPPIRE_DATA_STORE(data);
309 if ( adjustment > 0 )
311 psppire_case_file_insert_values (store->case_file, adjustment, posn);
317 * psppire_data_store_new:
318 * @dict: The dictionary for this data_store.
321 * Return value: a new #PsppireDataStore
324 psppire_data_store_new (PsppireDict *dict)
326 PsppireDataStore *retval;
328 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
330 psppire_data_store_set_dictionary(retval, dict);
339 * psppire_data_store_replace_set_dictionary:
340 * @data_store: The variable store
341 * @dict: The dictionary to set
343 * If a dictionary is already associated with the data-store, then it will be
347 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
349 gint var_cnt = psppire_dict_get_next_value_idx(dict);
351 if ( data_store->dict ) g_object_unref(data_store->dict);
354 data_store->dict = dict;
356 if ( data_store->case_file)
358 g_object_unref(data_store->case_file);
359 data_store->case_file = 0;
362 data_store->case_file = psppire_case_file_new(var_cnt);
364 g_signal_connect(data_store->case_file, "cases-deleted",
365 G_CALLBACK(delete_cases_callback),
368 g_signal_connect(data_store->case_file, "case-inserted",
369 G_CALLBACK(insert_case_callback),
373 g_signal_connect(data_store->case_file, "case-changed",
374 G_CALLBACK(changed_case_callback),
377 g_signal_connect(dict, "variable-inserted",
378 G_CALLBACK(insert_variable_callback),
381 g_signal_connect(dict, "variables-deleted",
382 G_CALLBACK(delete_variables_callback),
385 g_signal_connect (dict, "dict-size-changed",
386 G_CALLBACK(dict_size_change_callback),
389 /* The entire model has changed */
390 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
392 g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
396 psppire_data_store_finalize (GObject *object)
400 (* parent_class->finalize) (object);
405 psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
409 const struct fmt_spec *fp ;
410 const struct PsppireVariable *pv ;
411 const union value *v ;
413 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
415 g_return_val_if_fail(store->dict, NULL);
416 g_return_val_if_fail(store->case_file, NULL);
418 if (column >= psppire_dict_get_var_cnt(store->dict))
421 if ( row >= psppire_case_file_get_case_count(store->case_file))
424 pv = psppire_dict_get_variable(store->dict, column);
426 idx = psppire_variable_get_fv(pv);
428 v = psppire_case_file_get_value(store->case_file, row, idx);
430 g_return_val_if_fail(v, NULL);
432 if ( store->show_labels)
434 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
437 if ( (label = val_labs_find(vl, *v)) )
439 return pspp_locale_to_utf8(label, -1, 0);
443 fp = psppire_variable_get_write_spec(pv);
445 s = g_string_sized_new (fp->w + 1);
446 g_string_set_size(s, fp->w);
448 memset(s->str, 0, fp->w);
450 g_assert(fp->w == s->len);
452 /* Converts binary value V into printable form in the exactly
453 FP->W character in buffer S according to format specification
454 FP. No null terminator is appended to the buffer. */
455 data_out (s->str, fp, v);
457 text = pspp_locale_to_utf8(s->str, fp->w, 0);
458 g_string_free(s, TRUE);
465 psppire_data_store_clear_datum(GSheetModel *model,
469 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
472 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
474 const gint index = psppire_variable_get_fv(pv) ;
476 if ( psppire_variable_get_type(pv) == NUMERIC)
479 memcpy(v.s, "", MAX_SHORT_STRING);
481 psppire_case_file_set_value(store->case_file, row, index, &v,
482 psppire_variable_get_width(pv));
487 /* Attempts to update that part of the variable store which corresponds
488 to ROW, COL with the value TEXT.
489 Returns true if anything was updated, false otherwise.
492 psppire_data_store_set_string(GSheetModel *model,
493 const gchar *text, gint row, gint col)
495 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
497 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
498 g_return_val_if_fail(pv, FALSE);
501 /* Allow the user to insert a lot of blank cases, simply by skipping rows */
502 for(r = psppire_case_file_get_case_count(store->case_file); r <= row ; ++r)
507 psppire_case_array_insert_case(store->cases, r, 0, 0);
510 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
511 psppire_data_store_clear_datum(model, r, c);
516 const gint index = psppire_variable_get_fv(pv);
520 d_in.e = text + strlen(text);
522 d_in.f1 = d_in.f2 = 0;
523 d_in.format = * psppire_variable_get_write_spec(pv);
526 psppire_case_file_data_in(store->case_file, row, index, &d_in) ;
534 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
536 g_return_if_fail (store);
537 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
539 store->font_desc = fd;
540 g_sheet_model_range_changed (G_SHEET_MODEL(store),
546 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
548 g_return_if_fail (store);
549 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
551 store->show_labels = show_labels;
553 g_sheet_model_range_changed (G_SHEET_MODEL(store),
560 psppire_data_store_create_system_file(PsppireDataStore *store,
561 struct file_handle *handle)
563 const struct sfm_write_options wo = {
564 true, /* writeable */
565 false, /* dont compress */
569 struct sfm_writer *writer ;
573 writer = sfm_open_writer(handle, store->dict->dict, wo);
579 psppire_case_array_iterate_case(store->cases, write_case, writer);
582 sfm_close_writer(writer);
588 psppire_data_store_clear(PsppireDataStore *data_store)
590 psppire_case_file_clear(data_store->case_file);
592 psppire_dict_clear(data_store->dict);
598 /* Column related funcs */
601 geometry_get_column_count(const GSheetColumn *geom, gpointer data)
603 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
605 return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
608 /* Return the width that an 'M' character would occupy when typeset at
611 M_width(const GtkSheet *sheet, gint row, gint col)
613 GtkSheetCellAttr attributes;
615 /* FIXME: make this a member of the data store */
616 static PangoLayout *layout = 0;
618 gtk_sheet_get_attributes(sheet, row, col, &attributes);
621 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
625 pango_layout_set_font_description (layout,
626 attributes.font_desc);
628 pango_layout_get_extents (layout, NULL, &rect);
631 g_object_unref(G_OBJECT(layout));
634 return PANGO_PIXELS(rect.width);
638 /* Return the number of pixels corresponding to a column of
641 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
643 return (M_width(sheet, 0, column) * width);
648 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
650 const struct PsppireVariable *pv ;
651 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
653 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
656 /* FIXME: We can optimise this by caching the widths until they're resized */
657 pv = psppire_dict_get_variable(ds->dict, unit);
659 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
666 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
668 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
670 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
672 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
677 static GtkJustification
678 geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
680 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
681 const struct PsppireVariable *pv ;
684 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
685 return GTK_JUSTIFY_LEFT;
687 pv = psppire_dict_get_variable(ds->dict, unit);
689 /* Kludge: Happily GtkJustification is defined similarly
690 to enum alignment from pspp/variable.h */
691 return psppire_variable_get_alignment(pv);
695 static const gchar null_var_name[]=N_("var");
698 geometry_get_column_button_label(const GSheetColumn *geom, gint unit,
702 struct PsppireVariable *pv ;
703 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
705 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
706 return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
708 pv = psppire_dict_get_variable(ds->dict, unit);
710 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
717 geometry_get_sensitivity(const GSheetColumn *geom, gint unit, gpointer data)
719 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
722 return (unit < psppire_dict_get_var_cnt(ds->dict));
727 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
729 iface->get_column_count = geometry_get_column_count;
730 iface->get_width = geometry_get_width;
731 iface->set_width = geometry_set_width;
732 iface->get_visibility = always_true;
733 iface->get_sensitivity = geometry_get_sensitivity;
734 iface->get_justification = geometry_get_justification;
736 iface->get_button_label = geometry_get_column_button_label;
740 /* Row related funcs */
743 geometry_get_row_count(const GSheetRow *geom, gpointer data)
745 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
747 return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
752 geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
759 geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
761 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
764 return (unit < psppire_case_file_get_case_count(ds->case_file));
769 geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
773 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
776 TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
779 s = g_strdup_printf(_("%d"), unit);
781 text = pspp_locale_to_utf8(s, -1, 0);
790 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
792 iface->get_row_count = geometry_get_row_count;
794 iface->get_height = geometry_get_height;
795 iface->set_height = 0;
796 iface->get_visibility = always_true;
797 iface->get_sensitivity = geometry_get_row_sensitivity;
799 iface->get_button_label = geometry_get_row_button_label;