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
31 #include <gtksheet/gtksheet.h>
32 #include <gtksheet/gsheetmodel.h>
33 #include <gtksheet/gsheet-column-iface.h>
35 #include "psppire-variable.h"
36 #include "psppire-data-store.h"
39 #include <data/dictionary.h>
40 #include <data/missing-values.h>
41 #include <data/value-labels.h>
42 #include <data/data-in.h>
44 #include <data/file-handle-def.h>
45 #include <data/sys-file-writer.h>
49 static void psppire_data_store_init (PsppireDataStore *data_store);
50 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
51 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
52 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
53 static void psppire_data_store_finalize (GObject *object);
55 static const gchar *psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
57 static gboolean psppire_data_store_set_string(GSheetModel *model,
58 const gchar *text, gint row, gint column);
60 static gboolean psppire_data_store_clear_datum(GSheetModel *model,
61 gint row, gint column);
64 #define MIN_COLUMNS 10
67 static GObjectClass *parent_class = NULL;
70 psppire_data_store_get_type (void)
72 static GType data_store_type = 0;
76 static const GTypeInfo data_store_info =
78 sizeof (PsppireDataStoreClass),
80 NULL, /* base_finalize */
81 (GClassInitFunc) psppire_data_store_class_init,
82 NULL, /* class_finalize */
83 NULL, /* class_data */
84 sizeof (PsppireDataStore),
86 (GInstanceInitFunc) psppire_data_store_init,
89 static const GInterfaceInfo sheet_model_info =
91 (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
96 static const GInterfaceInfo sheet_column_info =
98 (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
105 data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
106 &data_store_info, 0);
108 g_type_add_interface_static (data_store_type,
112 g_type_add_interface_static (data_store_type,
118 return data_store_type;
122 psppire_data_store_class_init (PsppireDataStoreClass *class)
124 GObjectClass *object_class;
126 parent_class = g_type_class_peek_parent (class);
127 object_class = (GObjectClass*) class;
129 object_class->finalize = psppire_data_store_finalize;
135 psppire_data_store_get_var_count (const GSheetModel *model)
137 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
139 return psppire_dict_get_var_cnt(store->dict);
143 psppire_data_store_get_case_count (const GSheetModel *model)
145 const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
147 return psppire_case_array_get_n_cases(store->cases);
152 psppire_data_store_init (PsppireDataStore *data_store)
154 data_store->dict = 0;
155 data_store->cases = 0;
158 const PangoFontDescription *
159 psppire_data_store_get_font_desc(GSheetModel *model,
160 gint row, gint column)
162 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
164 return store->font_desc;
169 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
171 iface->free_strings = TRUE;
172 iface->get_string = psppire_data_store_get_string;
173 iface->set_string = psppire_data_store_set_string;
174 iface->clear_datum = psppire_data_store_clear_datum;
175 iface->is_editable = NULL;
176 iface->is_visible = NULL;
177 iface->get_foreground = NULL;
178 iface->get_background = NULL;
179 iface->get_font_desc = psppire_data_store_get_font_desc;
180 iface->get_cell_border = NULL;
181 iface->get_column_count = psppire_data_store_get_var_count;
182 iface->get_row_count = psppire_data_store_get_case_count;
186 gboolean always_true()
193 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
195 PsppireDataStore *store ;
197 g_return_if_fail (data);
199 store = PSPPIRE_DATA_STORE(data);
201 g_assert(first >= 0);
203 g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
208 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
210 PsppireDataStore *store ;
212 g_return_if_fail (data);
214 store = PSPPIRE_DATA_STORE(data);
216 g_sheet_model_range_changed (G_SHEET_MODEL(store),
218 psppire_case_array_get_n_cases(store->cases),
224 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
226 PsppireDataStore *store ;
227 g_return_if_fail (data);
229 store = PSPPIRE_DATA_STORE(data);
231 g_sheet_model_range_changed (G_SHEET_MODEL(store),
239 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
241 PsppireDataStore *store ;
243 g_return_if_fail (data);
245 store = PSPPIRE_DATA_STORE(data);
247 g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
250 g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
255 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
257 PsppireDataStore *store;
259 g_return_if_fail (data);
261 store = PSPPIRE_DATA_STORE(data);
264 g_sheet_model_range_changed (G_SHEET_MODEL(store),
266 psppire_case_array_get_n_cases(store->cases),
270 psppire_case_array_resize(store->cases,
271 dict_get_next_value_idx (store->dict->dict));
273 g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
280 * psppire_data_store_new:
281 * @dict: The dictionary for this data_store.
284 * Return value: a new #PsppireDataStore
287 psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
289 PsppireDataStore *retval;
291 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
293 retval->cases = cases;
294 g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback),
297 g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback),
301 g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback),
304 psppire_data_store_set_dictionary(retval, dict);
313 * psppire_data_store_replace_set_dictionary:
314 * @data_store: The variable store
315 * @dict: The dictionary to set
317 * If a dictionary is already associated with the data-store, then it will be
321 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
324 if ( data_store->dict ) g_object_unref(data_store->dict);
327 data_store->dict = dict;
329 psppire_case_array_resize(data_store->cases,
330 dict_get_next_value_idx (data_store->dict->dict));
333 g_signal_connect(dict, "variable-inserted",
334 G_CALLBACK(insert_variable_callback),
337 g_signal_connect(dict, "variables-deleted",
338 G_CALLBACK(delete_variables_callback),
341 /* The entire model has changed */
342 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
346 psppire_data_store_finalize (GObject *object)
350 (* parent_class->finalize) (object);
355 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
358 const struct fmt_spec *fp ;
359 const struct PsppireVariable *pv ;
360 const union value *v ;
362 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
364 g_return_val_if_fail(store->dict, NULL);
365 g_return_val_if_fail(store->cases, NULL);
367 if (column >= psppire_dict_get_var_cnt(store->dict))
370 if ( row >= psppire_case_array_get_n_cases(store->cases))
374 pv = psppire_dict_get_variable(store->dict, column);
376 v = psppire_case_array_get_value(store->cases, row,
377 psppire_variable_get_index(pv));
379 if ( store->show_labels)
381 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
384 if ( (label = val_labs_find(vl, *v)) )
386 return pspp_locale_to_utf8(label, -1, 0);
390 fp = psppire_variable_get_write_spec(pv);
392 s = g_string_sized_new (fp->w + 1);
393 g_string_set_size(s, fp->w);
395 memset(s->str, 0, fp->w);
397 g_assert(fp->w == s->len);
399 /* Converts binary value V into printable form in the exactly
400 FP->W character in buffer S according to format specification
401 FP. No null terminator is appended to the buffer. */
402 data_out (s->str, fp, v);
404 text = pspp_locale_to_utf8(s->str, fp->w, 0);
405 g_string_free(s, TRUE);
412 set_null_string_value(union value *val, gpointer data)
419 set_sysmis_value(union value *val, gpointer data)
427 psppire_data_store_clear_datum(GSheetModel *model,
431 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
433 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
435 const gint index = psppire_variable_get_index(pv) ;
437 if ( psppire_variable_get_type(pv) == NUMERIC)
438 psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
440 psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
446 fillit(union value *val, gpointer data)
448 struct data_in *d_in = data;
452 if ( ! data_in(d_in) )
454 g_warning("Cant encode string\n");
462 /* Attempts to update that part of the variable store which corresponds
463 to ROW, COL with the value TEXT.
464 Returns true if anything was updated, false otherwise.
467 psppire_data_store_set_string(GSheetModel *model,
468 const gchar *text, gint row, gint col)
471 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
473 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
474 g_return_val_if_fail(pv, FALSE);
476 for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r )
479 psppire_case_array_insert_case(store->cases, r, 0, 0);
481 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
482 psppire_data_store_clear_datum(model, r, c);
486 const gint index = psppire_variable_get_index(pv);
490 d_in.e = text + strlen(text);
492 d_in.f1 = d_in.f2 = 0;
493 d_in.format = * psppire_variable_get_write_spec(pv);
496 psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
504 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
506 g_return_if_fail (store);
507 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
509 store->font_desc = fd;
510 g_sheet_model_range_changed (G_SHEET_MODEL(store),
516 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
518 g_return_if_fail (store);
519 g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
521 store->show_labels = show_labels;
523 g_sheet_model_range_changed (G_SHEET_MODEL(store),
530 write_case(const struct ccase *cc,
533 struct sfm_writer *writer = aux;
535 if ( ! sfm_write_case(writer, cc) )
543 psppire_data_store_create_system_file(PsppireDataStore *store,
544 struct file_handle *handle)
546 const struct sfm_write_options wo = {
547 true, /* writeable */
548 false, /* dont compress */
552 struct sfm_writer *writer ;
556 writer = sfm_open_writer(handle, store->dict->dict, wo);
561 psppire_case_array_iterate_case(store->cases, write_case, writer);
563 sfm_close_writer(writer);
568 /* Column related funcs */
571 geometry_get_column_count(const GSheetColumn *geom)
573 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
575 return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
578 /* Return the width that an 'M' character would occupy when typeset at
581 M_width(const GtkSheet *sheet, gint row, gint col)
583 GtkSheetCellAttr attributes;
585 /* FIXME: make this a member of the data store */
586 static PangoLayout *layout = 0;
588 gtk_sheet_get_attributes(sheet, row, col, &attributes);
591 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
595 pango_layout_set_font_description (layout,
596 attributes.font_desc);
598 pango_layout_get_extents (layout, NULL, &rect);
601 g_object_unref(G_OBJECT(layout));
604 return PANGO_PIXELS(rect.width);
608 /* Return the number of pixels corresponding to a column of
611 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
613 return (M_width(sheet, 0, column) * width);
618 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
620 const struct PsppireVariable *pv ;
621 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
623 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
626 /* FIXME: We can optimise this by caching the widths until they're resized */
627 pv = psppire_dict_get_variable(ds->dict, unit);
629 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
636 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
638 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
640 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
642 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
647 static GtkJustification
648 geometry_get_justification(const GSheetColumn *geom, gint unit)
650 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
651 const struct PsppireVariable *pv ;
654 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
655 return GTK_JUSTIFY_LEFT;
657 pv = psppire_dict_get_variable(ds->dict, unit);
659 /* Kludge: Happily GtkJustification is defined similarly
660 to enum alignment from pspp/variable.h */
661 return psppire_variable_get_alignment(pv);
665 static const gchar null_var_name[]=N_("var");
668 geometry_get_button_label(const GSheetColumn *geom, gint unit)
671 struct PsppireVariable *pv ;
672 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
674 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
675 return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
677 pv = psppire_dict_get_variable(ds->dict, unit);
679 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
686 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
688 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
691 return (unit < psppire_dict_get_var_cnt(ds->dict));
696 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
698 iface->get_column_count = geometry_get_column_count;
699 iface->get_width = geometry_get_width;
700 iface->set_width = geometry_set_width;
701 iface->get_visibility = always_true;
702 iface->get_sensitivity = geometry_get_sensitivity;
703 iface->get_justification = geometry_get_justification;
705 iface->get_button_label = geometry_get_button_label;