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
27 #include <gtksheet/gtksheet.h>
28 #include <gtksheet/gsheetmodel.h>
29 #include <gtksheet/gsheet-column-iface.h>
31 #include "psppire-variable.h"
32 #include "psppire-data-store.h"
35 #include <data/dictionary.h>
36 #include <data/missing-values.h>
37 #include <data/value-labels.h>
38 #include <data/data-in.h>
40 #include <data/file-handle-def.h>
41 #include <data/sys-file-writer.h>
47 static void psppire_data_store_init (PsppireDataStore *data_store);
48 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
49 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
50 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
51 static void psppire_data_store_finalize (GObject *object);
53 static const gchar *psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
55 static gboolean psppire_data_store_set_string(GSheetModel *model,
56 const gchar *text, gint row, gint column);
58 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
59 gint row, gint column);
62 #define MIN_COLUMNS 10
65 static GObjectClass *parent_class = NULL;
68 psppire_data_store_get_type (void)
70 static GType data_store_type = 0;
74 static const GTypeInfo data_store_info =
76 sizeof (PsppireDataStoreClass),
78 NULL, /* base_finalize */
79 (GClassInitFunc) psppire_data_store_class_init,
80 NULL, /* class_finalize */
81 NULL, /* class_data */
82 sizeof (PsppireDataStore),
84 (GInstanceInitFunc) psppire_data_store_init,
87 static const GInterfaceInfo sheet_model_info =
89 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
94 static const GInterfaceInfo sheet_column_info =
96 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
103 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
104 &data_store_info, 0);
106 g_type_add_interface_static (data_store_type,
110 g_type_add_interface_static (data_store_type,
116 return data_store_type;
120 psppire_data_store_class_init (PsppireDataStoreClass *class)
122 GObjectClass *object_class;
124 parent_class = g_type_class_peek_parent (class);
125 object_class = (GObjectClass*) class;
127 object_class->finalize = psppire_data_store_finalize;
133 psppire_data_store_get_var_count (const GSheetModel *model)
135 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
137 return psppire_dict_get_var_cnt(store->dict);
141 psppire_data_store_get_case_count (const GSheetModel *model)
143 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
145 return psppire_case_array_get_n_cases(store->cases);
150 psppire_data_store_init (PsppireDataStore *data_store)
152 data_store->dict = 0;
153 data_store->cases = 0;
156 const PangoFontDescription *
157 psppire_data_store_get_font_desc(GSheetModel *model,
158 gint row, gint column)
160 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
162 return store->font_desc;
167 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
169 iface->free_strings = TRUE;
170 iface->get_string = psppire_data_store_get_string;
171 iface->set_string = psppire_data_store_set_string;
172 iface->clear_datum = psppire_data_store_clear_datum;
173 iface->is_editable = NULL;
174 iface->is_visible = NULL;
175 iface->get_foreground = NULL;
176 iface->get_background = NULL;
177 iface->get_font_desc = psppire_data_store_get_font_desc;
178 iface->get_cell_border = NULL;
179 iface->get_column_count = psppire_data_store_get_var_count;
180 iface->get_row_count = psppire_data_store_get_case_count;
184 gboolean always_true()
191 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
193 PsppireDataStore *store ;
195 g_return_if_fail (data);
197 store = PSPPIRE_DATA_STORE(data);
199 g_assert(first >= 0);
201 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
206 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
208 PsppireDataStore *store ;
210 g_return_if_fail (data);
212 store = PSPPIRE_DATA_STORE(data);
214 g_sheet_model_range_changed (G_SHEET_MODEL(store),
216 psppire_case_array_get_n_cases(store->cases),
222 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
224 PsppireDataStore *store ;
225 g_return_if_fail (data);
227 store = PSPPIRE_DATA_STORE(data);
229 g_sheet_model_range_changed (G_SHEET_MODEL(store),
237 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
239 PsppireDataStore *store ;
241 g_return_if_fail (data);
243 store = PSPPIRE_DATA_STORE(data);
245 g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
248 g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
253 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
255 PsppireDataStore *store;
257 g_return_if_fail (data);
259 store = PSPPIRE_DATA_STORE(data);
262 g_sheet_model_range_changed (G_SHEET_MODEL(store),
264 psppire_case_array_get_n_cases(store->cases),
268 psppire_case_array_resize(store->cases,
269 dict_get_next_value_idx (store->dict->dict));
271 g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
278 * psppire_data_store_new:
279 * @dict: The dictionary for this data_store.
282 * Return value: a new #PsppireDataStore
285 psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
287 PsppireDataStore *retval;
289 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
291 retval->cases = cases;
292 g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback),
295 g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback),
299 g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback),
302 psppire_data_store_set_dictionary(retval, dict);
311 * psppire_data_store_replace_set_dictionary:
312 * @data_store: The variable store
313 * @dict: The dictionary to set
315 * If a dictionary is already associated with the data-store, then it will be
319 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
322 if ( data_store->dict ) g_object_unref(data_store->dict);
325 data_store->dict = dict;
327 psppire_case_array_resize(data_store->cases,
328 dict_get_next_value_idx (data_store->dict->dict));
331 g_signal_connect(dict, "variable-inserted",
332 G_CALLBACK(insert_variable_callback),
335 g_signal_connect(dict, "variables-deleted",
336 G_CALLBACK(delete_variables_callback),
339 /* The entire model has changed */
340 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
344 psppire_data_store_finalize (GObject *object)
348 (* parent_class->finalize) (object);
353 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
356 const struct fmt_spec *fp ;
357 const struct PsppireVariable *pv ;
358 const union value *v ;
360 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
362 g_return_val_if_fail(store->dict, NULL);
363 g_return_val_if_fail(store->cases, NULL);
365 if (column >= psppire_dict_get_var_cnt(store->dict))
368 if ( row >= psppire_case_array_get_n_cases(store->cases))
372 pv = psppire_dict_get_variable(store->dict, column);
374 v = psppire_case_array_get_value(store->cases, row,
375 psppire_variable_get_index(pv));
377 if ( store->show_labels)
379 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
382 if ( (label = val_labs_find(vl, *v)) )
384 return pspp_locale_to_utf8(label, -1, 0);
388 fp = psppire_variable_get_write_spec(pv);
390 s = g_string_sized_new (fp->w + 1);
391 g_string_set_size(s, fp->w);
393 memset(s->str, 0, fp->w);
395 g_assert(fp->w == s->len);
397 /* Converts binary value V into printable form in the exactly
398 FP->W character in buffer S according to format specification
399 FP. No null terminator is appended to the buffer. */
400 data_out (s->str, fp, v);
403 text = pspp_locale_to_utf8(s->str, fp->w, 0);
404 g_string_free(s, TRUE);
411 set_null_string_value(union value *val, gpointer data)
418 set_sysmis_value(union value *val, gpointer data)
426 psppire_data_store_clear_datum(GSheetModel *model,
430 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
432 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
434 const gint index = psppire_variable_get_index(pv) ;
436 if ( psppire_variable_get_type(pv) == NUMERIC)
437 psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
439 psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
445 fillit(union value *val, gpointer data)
447 struct data_in *d_in = data;
451 if ( ! data_in(d_in) )
453 g_warning("Cant encode string\n");
461 /* Attempts to update that part of the variable store which corresponds
462 to ROW, COL with the value TEXT.
463 Returns true if anything was updated, false otherwise.
466 psppire_data_store_set_string(GSheetModel *model,
467 const gchar *text, gint row, gint col)
470 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
472 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
473 g_return_val_if_fail(pv, FALSE);
475 for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r )
478 psppire_case_array_insert_case(store->cases, r, 0, 0);
480 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
481 psppire_data_store_clear_datum(model, r, c);
485 const gint index = psppire_variable_get_index(pv);
489 d_in.e = text + strlen(text);
491 d_in.f1 = d_in.f2 = 0;
492 d_in.format = * psppire_variable_get_write_spec(pv);
495 psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
503 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
505 g_return_if_fail (store);
506 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
508 store->font_desc = fd;
509 g_sheet_model_range_changed (G_SHEET_MODEL(store),
515 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
517 g_return_if_fail (store);
518 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
520 store->show_labels = show_labels;
522 g_sheet_model_range_changed (G_SHEET_MODEL(store),
529 write_case(const struct ccase *cc,
532 struct sfm_writer *writer = aux;
534 if ( ! sfm_write_case(writer, cc) )
542 psppire_data_store_create_system_file(PsppireDataStore *store,
543 struct file_handle *handle)
545 const struct sfm_write_options wo = {
546 true, /* writeable */
547 false, /* dont compress */
551 struct sfm_writer *writer ;
555 writer = sfm_open_writer(handle, store->dict->dict, wo);
560 psppire_case_array_iterate_case(store->cases, write_case, writer);
562 sfm_close_writer(writer);
567 /* Column related funcs */
570 geometry_get_column_count(const GSheetColumn *geom)
572 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
574 return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
577 /* Return the width that an 'M' character would occupy when typeset at
580 M_width(GtkSheet *sheet, gint row, gint col)
582 GtkSheetCellAttr attributes;
584 /* FIXME: make this a member of the data store */
585 static PangoLayout *layout = 0;
587 gtk_sheet_get_attributes(sheet, row, col, &attributes);
590 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
594 pango_layout_set_font_description (layout,
595 attributes.font_desc);
597 pango_layout_get_extents (layout, NULL, &rect);
600 g_object_unref(G_OBJECT(layout));
603 return PANGO_PIXELS(rect.width);
607 /* Return the number of pixels corresponding to a column of
610 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
612 return (M_width(sheet, 0, column) * width);
617 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
619 const struct PsppireVariable *pv ;
620 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
622 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
625 /* FIXME: We can optimise this by caching the widths until they're resized */
626 pv = psppire_dict_get_variable(ds->dict, unit);
628 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
635 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
637 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
639 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
641 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
646 static GtkJustification
647 geometry_get_justification(const GSheetColumn *geom, gint unit)
649 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
650 const struct PsppireVariable *pv ;
653 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
654 return GTK_JUSTIFY_LEFT;
656 pv = psppire_dict_get_variable(ds->dict, unit);
658 /* Kludge: Happily GtkJustification is defined similarly
659 to enum alignment from pspp/variable.h */
660 return psppire_variable_get_alignment(pv);
664 static const gchar null_var_name[]=_("var");
667 geometry_get_button_label(const GSheetColumn *geom, gint unit)
670 struct PsppireVariable *pv ;
671 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
673 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
674 return pspp_locale_to_utf8(null_var_name, -1, 0);
676 pv = psppire_dict_get_variable(ds->dict, unit);
678 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
685 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
687 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
690 return (unit < psppire_dict_get_var_cnt(ds->dict));
695 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
697 iface->get_column_count = geometry_get_column_count;
698 iface->get_width = geometry_get_width;
699 iface->set_width = geometry_set_width;
700 iface->get_visibility = always_true;
701 iface->get_sensitivity = geometry_get_sensitivity;
702 iface->get_justification = geometry_get_justification;
704 iface->get_button_label = geometry_get_button_label;