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 <pango/pango-context.h>
38 #include "psppire-variable.h"
39 #include "psppire-data-store.h"
42 #include <data/dictionary.h>
43 #include <data/missing-values.h>
44 #include <data/value-labels.h>
45 #include <data/data-in.h>
47 #include <data/file-handle-def.h>
48 #include <data/sys-file-writer.h>
52 static void psppire_data_store_init (PsppireDataStore *data_store);
53 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
54 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
55 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
56 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
58 static void psppire_data_store_finalize (GObject *object);
60 static gchar *psppire_data_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
62 static gboolean psppire_data_store_set_string(GSheetModel *model,
63 const gchar *text, gint row, gint column);
65 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
66 gint row, gint column);
69 #define MIN_COLUMNS 10
71 #define TRAILING_ROWS 10
73 static GObjectClass *parent_class = NULL;
79 static guint signal[n_SIGNALS];
83 psppire_data_store_get_type (void)
85 static GType data_store_type = 0;
89 static const GTypeInfo data_store_info =
91 sizeof (PsppireDataStoreClass),
93 NULL, /* base_finalize */
94 (GClassInitFunc) psppire_data_store_class_init,
95 NULL, /* class_finalize */
96 NULL, /* class_data */
97 sizeof (PsppireDataStore),
99 (GInstanceInitFunc) psppire_data_store_init,
102 static const GInterfaceInfo sheet_model_info =
104 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
109 static const GInterfaceInfo sheet_column_info =
111 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
116 static const GInterfaceInfo sheet_row_info =
118 (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
124 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
125 &data_store_info, 0);
127 g_type_add_interface_static (data_store_type,
131 g_type_add_interface_static (data_store_type,
135 g_type_add_interface_static (data_store_type,
140 return data_store_type;
145 psppire_data_store_class_init (PsppireDataStoreClass *class)
147 GObjectClass *object_class;
149 parent_class = g_type_class_peek_parent (class);
150 object_class = (GObjectClass*) class;
152 object_class->finalize = psppire_data_store_finalize;
154 signal[FONT_CHANGED] =
155 g_signal_new ("font_changed",
156 G_TYPE_FROM_CLASS(class),
160 g_cclosure_marshal_VOID__VOID,
168 psppire_data_store_get_var_count (const GSheetModel *model)
170 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
172 return psppire_dict_get_var_cnt(store->dict);
176 psppire_data_store_get_case_count (const GSheetModel *model)
178 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
180 return psppire_case_file_get_case_count(store->case_file);
185 psppire_data_store_init (PsppireDataStore *data_store)
187 data_store->dict = 0;
188 data_store->case_file = 0;
189 data_store->width_of_m = 10;
192 const PangoFontDescription *
193 psppire_data_store_get_font_desc(const GSheetModel *model,
194 gint row, gint column)
196 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
198 return store->font_desc;
203 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
205 iface->free_strings = TRUE;
206 iface->get_string = psppire_data_store_get_string;
207 iface->set_string = psppire_data_store_set_string;
208 iface->clear_datum = psppire_data_store_clear_datum;
209 iface->is_editable = NULL;
210 iface->is_visible = NULL;
211 iface->get_foreground = NULL;
212 iface->get_background = NULL;
213 iface->get_font_desc = psppire_data_store_get_font_desc;
214 iface->get_cell_border = NULL;
215 iface->get_column_count = psppire_data_store_get_var_count;
216 iface->get_row_count = psppire_data_store_get_case_count;
220 gboolean always_true()
227 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
229 PsppireDataStore *store ;
231 g_return_if_fail (data);
233 store = PSPPIRE_DATA_STORE(data);
235 g_assert(first >= 0);
237 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
242 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
244 PsppireDataStore *store ;
246 g_return_if_fail (data);
248 store = PSPPIRE_DATA_STORE(data);
250 g_sheet_model_range_changed (G_SHEET_MODEL(store),
252 psppire_case_file_get_case_count(store->case_file),
255 g_sheet_model_rows_inserted (G_SHEET_MODEL(store), casenum, 1);
260 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
262 PsppireDataStore *store ;
263 g_return_if_fail (data);
265 store = PSPPIRE_DATA_STORE(data);
267 g_sheet_model_range_changed (G_SHEET_MODEL(store),
274 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
276 PsppireDataStore *store ;
278 g_return_if_fail (data);
280 store = PSPPIRE_DATA_STORE(data);
282 g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
284 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
289 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
291 PsppireDataStore *store;
294 g_return_if_fail (data);
296 store = PSPPIRE_DATA_STORE(data);
300 struct PsppireVariable *variable;
301 variable = psppire_dict_get_variable(store->dict, var_num);
303 posn = psppire_variable_get_fv(variable);
310 psppire_case_file_insert_values(store->case_file, 1, posn);
312 g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
315 g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
320 dict_size_change_callback(GObject *obj,
321 gint posn, gint adjustment, gpointer data)
323 PsppireDataStore *store ;
325 g_return_if_fail (data);
327 store = PSPPIRE_DATA_STORE(data);
330 if ( adjustment > 0 )
332 psppire_case_file_insert_values (store->case_file, adjustment, posn);
338 * psppire_data_store_new:
339 * @dict: The dictionary for this data_store.
342 * Return value: a new #PsppireDataStore
345 psppire_data_store_new (PsppireDict *dict)
347 PsppireDataStore *retval;
349 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
351 psppire_data_store_set_dictionary(retval, dict);
360 * psppire_data_store_replace_set_dictionary:
361 * @data_store: The variable store
362 * @dict: The dictionary to set
364 * If a dictionary is already associated with the data-store, then it will be
368 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
370 gint var_cnt = psppire_dict_get_next_value_idx(dict);
372 if ( data_store->dict ) g_object_unref(data_store->dict);
375 data_store->dict = dict;
377 if ( data_store->case_file)
379 g_object_unref(data_store->case_file);
380 data_store->case_file = 0;
383 data_store->case_file = psppire_case_file_new(var_cnt);
385 g_signal_connect(data_store->case_file, "cases-deleted",
386 G_CALLBACK(delete_cases_callback),
389 g_signal_connect(data_store->case_file, "case-inserted",
390 G_CALLBACK(insert_case_callback),
394 g_signal_connect(data_store->case_file, "case-changed",
395 G_CALLBACK(changed_case_callback),
398 g_signal_connect(dict, "variable-inserted",
399 G_CALLBACK(insert_variable_callback),
402 g_signal_connect(dict, "variables-deleted",
403 G_CALLBACK(delete_variables_callback),
406 g_signal_connect (dict, "dict-size-changed",
407 G_CALLBACK(dict_size_change_callback),
410 /* The entire model has changed */
411 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
413 g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
417 psppire_data_store_finalize (GObject *object)
421 (* parent_class->finalize) (object);
426 psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
430 const struct fmt_spec *fp ;
431 const struct PsppireVariable *pv ;
432 const union value *v ;
434 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
436 g_return_val_if_fail(store->dict, NULL);
437 g_return_val_if_fail(store->case_file, NULL);
439 if (column >= psppire_dict_get_var_cnt(store->dict))
442 if ( row >= psppire_case_file_get_case_count(store->case_file))
445 pv = psppire_dict_get_variable(store->dict, column);
447 idx = psppire_variable_get_fv(pv);
449 v = psppire_case_file_get_value(store->case_file, row, idx);
451 g_return_val_if_fail(v, NULL);
453 if ( store->show_labels)
455 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
458 if ( (label = val_labs_find(vl, *v)) )
460 return pspp_locale_to_utf8(label, -1, 0);
464 fp = psppire_variable_get_write_spec(pv);
466 s = g_string_sized_new (fp->w + 1);
467 g_string_set_size(s, fp->w);
469 memset(s->str, 0, fp->w);
471 g_assert(fp->w == s->len);
473 /* Converts binary value V into printable form in the exactly
474 FP->W character in buffer S according to format specification
475 FP. No null terminator is appended to the buffer. */
476 data_out (s->str, fp, v);
478 text = pspp_locale_to_utf8(s->str, fp->w, 0);
479 g_string_free(s, TRUE);
486 psppire_data_store_clear_datum(GSheetModel *model,
490 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
493 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
495 const gint index = psppire_variable_get_fv(pv) ;
497 if ( psppire_variable_get_type(pv) == NUMERIC)
500 memcpy(v.s, "", MAX_SHORT_STRING);
502 psppire_case_file_set_value(store->case_file, row, index, &v,
503 psppire_variable_get_width(pv));
508 /* Attempts to update that part of the variable store which corresponds
509 to ROW, COL with the value TEXT.
510 Returns true if anything was updated, false otherwise.
513 psppire_data_store_set_string(GSheetModel *model,
514 const gchar *text, gint row, gint col)
516 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
518 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
519 g_return_val_if_fail(pv, FALSE);
522 /* Allow the user to insert a lot of blank cases, simply by skipping rows */
523 for(r = psppire_case_file_get_case_count(store->case_file); r <= row ; ++r)
528 psppire_case_array_insert_case(store->cases, r, 0, 0);
531 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
532 psppire_data_store_clear_datum(model, r, c);
537 const gint index = psppire_variable_get_fv(pv);
541 d_in.e = text + strlen(text);
543 d_in.f1 = d_in.f2 = 0;
544 d_in.format = * psppire_variable_get_write_spec(pv);
547 psppire_case_file_data_in(store->case_file, row, index, &d_in) ;
555 psppire_data_store_set_font(PsppireDataStore *store,
556 const PangoFontDescription *fd)
558 g_return_if_fail (store);
559 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
561 store->font_desc = fd;
563 store->width_of_m = calc_m_width(fd);
565 g_signal_emit(store, signal[FONT_CHANGED], 0);
568 g_sheet_model_range_changed (G_SHEET_MODEL(store),
574 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
576 g_return_if_fail (store);
577 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
579 store->show_labels = show_labels;
581 g_sheet_model_range_changed (G_SHEET_MODEL(store),
588 psppire_data_store_create_system_file(PsppireDataStore *store,
589 struct file_handle *handle)
591 const struct sfm_write_options wo = {
592 true, /* writeable */
593 false, /* dont compress */
597 struct sfm_writer *writer ;
601 writer = sfm_open_writer(handle, store->dict->dict, wo);
607 psppire_case_array_iterate_case(store->cases, write_case, writer);
610 sfm_close_writer(writer);
616 psppire_data_store_clear(PsppireDataStore *data_store)
618 psppire_case_file_clear(data_store->case_file);
620 psppire_dict_clear(data_store->dict);
626 /* Column related funcs */
629 geometry_get_column_count(const GSheetColumn *geom)
631 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
633 return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
639 geometry_get_width(const GSheetColumn *geom, gint unit)
641 const struct PsppireVariable *pv ;
642 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
644 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
645 return ds->width_of_m * 8 ;
647 pv = psppire_dict_get_variable(ds->dict, unit);
650 return ds->width_of_m * 8 ;
652 return ds->width_of_m * psppire_variable_get_columns(pv);
656 geometry_set_width(GSheetColumn *geom, gint unit, gint width)
658 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
660 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
662 psppire_variable_set_columns(pv, width / ds->width_of_m );
667 static GtkJustification
668 geometry_get_justification(const GSheetColumn *geom, gint unit)
670 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
671 const struct PsppireVariable *pv ;
674 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
675 return GTK_JUSTIFY_LEFT;
677 pv = psppire_dict_get_variable(ds->dict, unit);
679 /* Kludge: Happily GtkJustification is defined similarly
680 to enum alignment from pspp/variable.h */
681 return psppire_variable_get_alignment(pv);
685 static const gchar null_var_name[]=N_("var");
688 geometry_get_column_button_label(const GSheetColumn *geom, gint unit)
691 struct PsppireVariable *pv ;
692 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
694 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
695 return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
697 pv = psppire_dict_get_variable(ds->dict, unit);
699 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
706 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
708 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
711 return (unit < psppire_dict_get_var_cnt(ds->dict));
716 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
718 iface->get_column_count = geometry_get_column_count;
719 iface->get_width = geometry_get_width;
720 iface->set_width = geometry_set_width;
721 iface->get_visibility = always_true;
722 iface->get_sensitivity = geometry_get_sensitivity;
723 iface->get_justification = geometry_get_justification;
725 iface->get_button_label = geometry_get_column_button_label;
729 /* Row related funcs */
732 geometry_get_row_count(const GSheetRow *geom, gpointer data)
734 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
736 return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
741 geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
748 geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
750 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
753 return (unit < psppire_case_file_get_case_count(ds->case_file));
758 geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
762 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
765 TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
768 s = g_strdup_printf(_("%d"), unit);
770 text = pspp_locale_to_utf8(s, -1, 0);
779 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
781 iface->get_row_count = geometry_get_row_count;
783 iface->get_height = geometry_get_height;
784 iface->set_height = 0;
785 iface->get_visibility = always_true;
786 iface->get_sensitivity = geometry_get_row_sensitivity;
788 iface->get_button_label = geometry_get_row_button_label;