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 #include <gtksheet/gtksheet.h>
27 #include <gtksheet/gsheetmodel.h>
28 #include <gtksheet/gsheet-column-iface.h>
30 #include "psppire-variable.h"
31 #include "psppire-data-store.h"
34 #include <data/dictionary.h>
35 #include <data/missing-values.h>
36 #include <data/value-labels.h>
37 #include <data/data-in.h>
39 #include <data/file-handle-def.h>
40 #include <data/sys-file-writer.h>
46 static void psppire_data_store_init (PsppireDataStore *data_store);
47 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
48 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
49 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
50 static void psppire_data_store_finalize (GObject *object);
52 static const gchar *psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
54 static gboolean psppire_data_store_set_string(GSheetModel *model,
55 const gchar *text, gint row, gint column);
57 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
58 gint row, gint column);
61 #define MIN_COLUMNS 10
63 #define max(A,B) ((A>B)?A:B)
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;
132 psppire_data_store_init (PsppireDataStore *data_store)
134 data_store->dict = 0;
135 data_store->cases = 0;
138 const PangoFontDescription *
139 psppire_data_store_get_font_desc(GSheetModel *model,
140 gint row, gint column)
142 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
144 return store->font_desc;
149 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
151 iface->free_strings = TRUE;
152 iface->get_string = psppire_data_store_get_string;
153 iface->set_string = psppire_data_store_set_string;
154 iface->clear_datum = psppire_data_store_clear_datum;
155 iface->is_editable = NULL;
156 iface->is_visible = NULL;
157 iface->get_foreground = NULL;
158 iface->get_background = NULL;
159 iface->get_font_desc = psppire_data_store_get_font_desc;
160 iface->get_cell_border = NULL;
164 gboolean always_true()
172 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
174 PsppireDataStore *store ;
176 g_return_if_fail (data);
178 store = PSPPIRE_DATA_STORE(data);
180 g_assert(first >= 0);
182 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
187 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
189 PsppireDataStore *store ;
190 g_return_if_fail (data);
192 store = PSPPIRE_DATA_STORE(data);
194 g_sheet_model_range_changed (G_SHEET_MODEL(store),
196 psppire_case_array_get_n_cases(store->cases),
202 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
204 PsppireDataStore *store ;
205 g_return_if_fail (data);
207 store = PSPPIRE_DATA_STORE(data);
209 g_sheet_model_range_changed (G_SHEET_MODEL(store),
217 delete_variables_callback(GtkWidget *w, gint var_num, gint n_vars, gpointer data)
219 PsppireDataStore *store ;
221 g_return_if_fail (data);
223 store = PSPPIRE_DATA_STORE(data);
225 g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
231 insert_variable_callback(GtkWidget *w, gint var_num, gpointer data)
233 PsppireDataStore *store;
235 g_return_if_fail (data);
237 store = PSPPIRE_DATA_STORE(data);
240 g_sheet_model_range_changed (G_SHEET_MODEL(store),
242 psppire_case_array_get_n_cases(store->cases),
246 psppire_case_array_resize(store->cases,
247 dict_get_next_value_idx (store->dict->dict));
255 * psppire_data_store_new:
256 * @dict: The dictionary for this data_store.
259 * Return value: a new #PsppireDataStore
262 psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
264 PsppireDataStore *retval;
266 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
268 retval->cases = cases;
269 g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback),
272 g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback),
276 g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback),
280 psppire_data_store_set_dictionary(retval, dict);
289 * psppire_data_store_replace_set_dictionary:
290 * @data_store: The variable store
291 * @dict: The dictionary to set
293 * If a dictionary is already associated with the data-store, then it will be
297 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
300 if ( data_store->dict ) g_object_unref(data_store->dict);
303 data_store->dict = dict;
305 psppire_case_array_resize(data_store->cases,
306 dict_get_next_value_idx (data_store->dict->dict));
309 g_signal_connect(dict, "variable-inserted",
310 G_CALLBACK(insert_variable_callback),
313 g_signal_connect(dict, "variables-deleted",
314 G_CALLBACK(delete_variables_callback),
318 /* The entire model has changed */
319 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
323 psppire_data_store_finalize (GObject *object)
327 (* parent_class->finalize) (object);
332 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
335 const struct fmt_spec *fp ;
336 const struct PsppireVariable *pv ;
337 const union value *v ;
339 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
341 g_return_val_if_fail(store->dict, NULL);
342 g_return_val_if_fail(store->cases, NULL);
344 if (column >= psppire_dict_get_var_cnt(store->dict))
347 if ( row >= psppire_case_array_get_n_cases(store->cases))
351 pv = psppire_dict_get_variable(store->dict, column);
353 v = psppire_case_array_get_value(store->cases, row,
354 psppire_variable_get_index(pv));
356 if ( store->show_labels)
358 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
361 if ( (label = val_labs_find(vl, *v)) )
363 return pspp_locale_to_utf8(label, -1, 0);
367 fp = psppire_variable_get_write_spec(pv);
369 s = g_string_sized_new (fp->w + 1);
370 g_string_set_size(s, fp->w);
372 memset(s->str, 0, fp->w);
374 g_assert(fp->w == s->len);
376 /* Converts binary value V into printable form in the exactly
377 FP->W character in buffer S according to format specification
378 FP. No null terminator is appended to the buffer. */
379 data_out (s->str, fp, v);
382 text = pspp_locale_to_utf8(s->str, fp->w, 0);
383 g_string_free(s, TRUE);
390 set_null_string_value(union value *val, gpointer data)
397 set_sysmis_value(union value *val, gpointer data)
405 psppire_data_store_clear_datum(GSheetModel *model,
409 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
411 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
413 const gint index = psppire_variable_get_index(pv) ;
415 if ( psppire_variable_get_type(pv) == NUMERIC)
416 psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
418 psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
424 fillit(union value *val, gpointer data)
426 struct data_in *d_in = data;
430 if ( ! data_in(d_in) )
432 g_warning("Cant encode string\n");
440 /* Attempts to update that part of the variable store which corresponds
441 to ROW, COL with the value TEXT.
442 Returns true if anything was updated, false otherwise.
445 psppire_data_store_set_string(GSheetModel *model,
446 const gchar *text, gint row, gint col)
449 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
451 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
453 for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r )
456 psppire_case_array_insert_case(store->cases, r);
458 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
459 psppire_data_store_clear_datum(model, r, c);
463 const gint index = psppire_variable_get_index(pv);
467 d_in.e = text + strlen(text);
469 d_in.f1 = d_in.f2 = 0;
470 d_in.format = * psppire_variable_get_write_spec(pv);
473 psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
481 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
483 g_return_if_fail (store);
484 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
486 store->font_desc = fd;
487 g_sheet_model_range_changed (G_SHEET_MODEL(store),
493 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
495 g_return_if_fail (store);
496 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
498 store->show_labels = show_labels;
500 g_sheet_model_range_changed (G_SHEET_MODEL(store),
507 write_case(const struct ccase *cc,
510 struct sfm_writer *writer = aux;
512 if ( ! sfm_write_case(writer, cc) )
520 psppire_data_store_create_system_file(PsppireDataStore *store,
521 struct file_handle *handle)
523 const struct sfm_write_options wo = {
524 true, /* writeable */
525 false, /* dont compress */
529 struct sfm_writer *writer ;
533 writer = sfm_open_writer(handle, store->dict->dict, wo);
538 psppire_case_array_iterate_case(store->cases, write_case, writer);
540 sfm_close_writer(writer);
545 /* Column related funcs */
548 geometry_get_column_count(const GSheetColumn *geom)
550 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
552 return max(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
555 /* Return the width that an 'M' character would occupy when typeset at
558 M_width(GtkSheet *sheet, gint row, gint col)
560 GtkSheetCellAttr attributes;
562 /* FIXME: make this a member of the data store */
563 static PangoLayout *layout = 0;
565 gtk_sheet_get_attributes(sheet, row, col, &attributes);
568 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
572 pango_layout_set_font_description (layout,
573 attributes.font_desc);
575 pango_layout_get_extents (layout, NULL, &rect);
578 g_object_unref(G_OBJECT(layout));
581 return PANGO_PIXELS(rect.width);
585 /* Return the number of pixels corresponding to a column of
588 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
590 return (M_width(sheet, 0, column) * width);
595 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
597 const struct PsppireVariable *pv ;
598 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
600 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
603 /* FIXME: We can optimise this by caching the widths until they're resized */
604 pv = psppire_dict_get_variable(ds->dict, unit);
606 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
613 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
615 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
617 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
619 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
624 static GtkJustification
625 geometry_get_justification(const GSheetColumn *geom, gint unit)
627 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
628 const struct PsppireVariable *pv ;
631 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
632 return GTK_JUSTIFY_LEFT;
634 pv = psppire_dict_get_variable(ds->dict, unit);
636 /* Kludge: Happily GtkJustification is defined similarly
637 to enum alignment from pspp/variable.h */
638 return psppire_variable_get_alignment(pv);
642 static const gchar null_var_name[]=_("var");
645 geometry_get_button_label(const GSheetColumn *geom, gint unit)
648 struct PsppireVariable *pv ;
649 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
651 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
652 return pspp_locale_to_utf8(null_var_name, -1, 0);
654 pv = psppire_dict_get_variable(ds->dict, unit);
656 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
663 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
665 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
668 return (unit < psppire_dict_get_var_cnt(ds->dict));
673 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
675 iface->get_column_count = geometry_get_column_count;
676 iface->get_width = geometry_get_width;
677 iface->set_width = geometry_set_width;
678 iface->get_visibility = always_true;
679 iface->get_sensitivity = geometry_get_sensitivity;
680 iface->get_justification = geometry_get_justification;
682 iface->get_button_label = geometry_get_button_label;