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 "gsheetmodel.h"
28 #include "gsheet-column-iface.h"
30 #include "psppire-variable.h"
31 #include "psppire-data-store.h"
33 #include "dictionary.h"
34 #include "missing-values.h"
35 #include "value-labels.h"
38 #include "file-handle-def.h"
39 #include "sys-file-writer.h"
45 static void psppire_data_store_init (PsppireDataStore *data_store);
46 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
47 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
48 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
49 static void psppire_data_store_finalize (GObject *object);
51 static const gchar *const psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
53 static gboolean psppire_data_store_set_string(GSheetModel *model,
54 const gchar *text, gint row, gint column);
56 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
57 gint row, gint column);
60 #define MIN_COLUMNS 10
62 #define max(A,B) ((A>B)?A:B)
64 static GObjectClass *parent_class = NULL;
67 psppire_data_store_get_type (void)
69 static GType data_store_type = 0;
73 static const GTypeInfo data_store_info =
75 sizeof (PsppireDataStoreClass),
77 NULL, /* base_finalize */
78 (GClassInitFunc) psppire_data_store_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (PsppireDataStore),
83 (GInstanceInitFunc) psppire_data_store_init,
86 static const GInterfaceInfo sheet_model_info =
88 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
93 static const GInterfaceInfo sheet_column_info =
95 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
102 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
103 &data_store_info, 0);
105 g_type_add_interface_static (data_store_type,
109 g_type_add_interface_static (data_store_type,
115 return data_store_type;
119 psppire_data_store_class_init (PsppireDataStoreClass *class)
121 GObjectClass *object_class;
123 parent_class = g_type_class_peek_parent (class);
124 object_class = (GObjectClass*) class;
126 object_class->finalize = psppire_data_store_finalize;
131 psppire_data_store_init (PsppireDataStore *data_store)
133 data_store->dict = 0;
134 data_store->cases = 0;
137 const PangoFontDescription *
138 psppire_data_store_get_font_desc(GSheetModel *model,
139 gint row, gint column)
141 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
143 return store->font_desc;
148 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
150 iface->get_string = psppire_data_store_get_string;
151 iface->set_string = psppire_data_store_set_string;
152 iface->clear_datum = psppire_data_store_clear_datum;
153 iface->is_editable = NULL;
154 iface->is_visible = NULL;
155 iface->get_foreground = NULL;
156 iface->get_background = NULL;
157 iface->get_font_desc = psppire_data_store_get_font_desc;
158 iface->get_cell_border = NULL;
162 gboolean always_true()
170 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
172 g_return_if_fail (data);
174 PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
176 g_assert(first >= 0);
178 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
183 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
185 g_return_if_fail (data);
187 PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
189 g_sheet_model_range_changed (G_SHEET_MODEL(store),
191 psppire_case_array_get_n_cases(store->cases),
197 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
199 g_return_if_fail (data);
201 PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
204 g_sheet_model_range_changed (G_SHEET_MODEL(store),
212 delete_variables_callback(GtkWidget *w, gint var_num, gint n_vars, gpointer data)
214 g_return_if_fail (data);
216 PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
218 g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
224 insert_variable_callback(GtkWidget *w, gint var_num, gpointer data)
226 g_return_if_fail (data);
228 PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
231 g_sheet_model_range_changed (G_SHEET_MODEL(store),
233 psppire_case_array_get_n_cases(store->cases),
237 psppire_case_array_resize(store->cases,
238 dict_get_next_value_idx (store->dict->dict));
246 * psppire_data_store_new:
247 * @dict: The dictionary for this data_store.
250 * Return value: a new #PsppireDataStore
253 psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
255 PsppireDataStore *retval;
257 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
259 retval->cases = cases;
260 g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback),
263 g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback),
267 g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback),
271 psppire_data_store_set_dictionary(retval, dict);
280 * psppire_data_store_replace_set_dictionary:
281 * @data_store: The variable store
282 * @dict: The dictionary to set
284 * If a dictionary is already associated with the data-store, then it will be
288 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
291 if ( data_store->dict ) g_object_unref(data_store->dict);
294 data_store->dict = dict;
296 psppire_case_array_resize(data_store->cases,
297 dict_get_next_value_idx (data_store->dict->dict));
300 g_signal_connect(dict, "variable-inserted",
301 G_CALLBACK(insert_variable_callback),
304 g_signal_connect(dict, "variables-deleted",
305 G_CALLBACK(delete_variables_callback),
309 /* The entire model has changed */
310 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
314 psppire_data_store_finalize (GObject *object)
318 (* parent_class->finalize) (object);
322 static const gchar *const
323 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
326 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
328 g_return_val_if_fail(store->dict, NULL);
329 g_return_val_if_fail(store->cases, NULL);
331 if (column >= psppire_dict_get_var_cnt(store->dict))
334 if ( row >= psppire_case_array_get_n_cases(store->cases))
337 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, column);
340 const union value *v =
341 psppire_case_array_get_value(store->cases, row,
342 psppire_variable_get_index(pv));
345 if ( store->show_labels)
347 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
350 if ( (label = val_labs_find(vl, *v)) )
356 const struct fmt_spec *fp = psppire_variable_get_write_spec(pv);
358 if ( psppire_variable_get_type(pv) == NUMERIC )
360 /* Converts binary value V into printable form in the exactly
361 FP->W character in buffer S according to format specification
362 FP. No null terminator is appended to the buffer. */
368 const gint len = psppire_variable_get_width(pv);
369 memcpy(s, v->s, len);
373 static gchar buf[255];
375 gchar *text = g_locale_to_utf8(s, fp->w, 0, 0, &err);
378 g_snprintf(buf, 255, text);
383 g_warning("Cannot convert string \"%s\" to utf-8: %s\n", s, err->message);
393 set_null_string_value(union value *val, gpointer data)
400 set_sysmis_value(union value *val, gpointer data)
408 psppire_data_store_clear_datum(GSheetModel *model,
412 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
414 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
416 const gint index = psppire_variable_get_index(pv) ;
418 if ( psppire_variable_get_type(pv) == NUMERIC)
419 psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
421 psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
427 fillit(union value *val, gpointer data)
429 struct data_in *d_in = data;
433 if ( ! data_in(d_in) )
435 g_warning("Cant encode string\n");
443 /* Attempts to update that part of the variable store which corresponds
444 to ROW, COL with the value TEXT.
445 Returns true if anything was updated, false otherwise.
448 psppire_data_store_set_string(GSheetModel *model,
449 const gchar *text, gint row, gint col)
452 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
454 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
456 for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r )
459 psppire_case_array_insert_case(store->cases, r);
461 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
462 psppire_data_store_clear_datum(model, r, c);
465 const gint index = psppire_variable_get_index(pv);
469 d_in.e = text + strlen(text);
471 d_in.f1 = d_in.f2 = 0;
472 d_in.format = * psppire_variable_get_write_spec(pv);
475 psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
482 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
484 g_return_if_fail (store);
485 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
487 store->font_desc = fd;
488 g_sheet_model_range_changed (G_SHEET_MODEL(store),
494 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
496 g_return_if_fail (store);
497 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
499 store->show_labels = show_labels;
501 g_sheet_model_range_changed (G_SHEET_MODEL(store),
508 write_case(const struct ccase *cc,
511 struct sfm_writer *writer = aux;
513 if ( ! sfm_write_case(writer, cc) )
521 psppire_data_store_create_system_file(PsppireDataStore *store,
522 struct file_handle *handle)
526 const struct sfm_write_options wo = {
527 true, /* writeable */
528 false, /* dont compress */
532 struct sfm_writer *writer = sfm_open_writer(handle, store->dict->dict, wo);
537 psppire_case_array_iterate_case(store->cases, write_case, writer);
539 sfm_close_writer(writer);
544 /* Column related funcs */
547 geometry_get_column_count(const GSheetColumn *geom)
549 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
551 return max(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
554 /* Return the width that an 'M' character would occupy when typeset at
557 M_width(GtkSheet *sheet, gint row, gint col)
559 GtkSheetCellAttr attributes;
561 gtk_sheet_get_attributes(sheet, row, col, &attributes);
564 /* FIXME: make this a member of the data store */
565 static PangoLayout *layout = 0;
568 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
570 pango_layout_set_font_description (layout,
571 attributes.font_desc);
573 pango_layout_get_extents (layout, NULL, &rect);
576 g_object_unref(G_OBJECT(layout));
579 return PANGO_PIXELS(rect.width);
583 /* Return the number of pixels corresponding to a column of
586 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
588 return (M_width(sheet, 0, column) * width);
593 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
595 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
597 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
600 /* FIXME: We can optimise this by caching the widths until they're resized */
601 const struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
603 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
610 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
612 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
614 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
616 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
621 static GtkJustification
622 geometry_get_justification(const GSheetColumn *geom, gint unit)
624 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
627 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
628 return GTK_JUSTIFY_LEFT;
630 const struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
632 /* Kludge: Happily GtkJustification is defined similarly
633 to enum alignment from pspp/variable.h */
634 return psppire_variable_get_alignment(pv);
638 static const gchar null_var_name[]=_("var");
641 geometry_get_button_label(const GSheetColumn *geom, gint unit)
643 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
645 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
646 return null_var_name;
648 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
650 return psppire_variable_get_name(pv);
655 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
657 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
660 return (unit < psppire_dict_get_var_cnt(ds->dict));
665 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
667 iface->get_column_count = geometry_get_column_count;
668 iface->get_width = geometry_get_width;
669 iface->set_width = geometry_set_width;
670 iface->get_visibility = always_true;
671 iface->get_sensitivity = geometry_get_sensitivity;
672 iface->get_justification = geometry_get_justification;
674 iface->get_button_label = geometry_get_button_label;