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 ;
191 g_return_if_fail (data);
193 store = PSPPIRE_DATA_STORE(data);
195 g_sheet_model_range_changed (G_SHEET_MODEL(store),
197 psppire_case_array_get_n_cases(store->cases),
203 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
205 PsppireDataStore *store ;
206 g_return_if_fail (data);
208 store = PSPPIRE_DATA_STORE(data);
210 g_sheet_model_range_changed (G_SHEET_MODEL(store),
218 delete_variables_callback(GtkWidget *w, gint var_num, gint n_vars, gpointer data)
220 PsppireDataStore *store ;
222 g_return_if_fail (data);
224 store = PSPPIRE_DATA_STORE(data);
226 g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
232 insert_variable_callback(GtkWidget *w, gint var_num, gpointer data)
234 PsppireDataStore *store;
236 g_return_if_fail (data);
238 store = PSPPIRE_DATA_STORE(data);
241 g_sheet_model_range_changed (G_SHEET_MODEL(store),
243 psppire_case_array_get_n_cases(store->cases),
247 psppire_case_array_resize(store->cases,
248 dict_get_next_value_idx (store->dict->dict));
256 * psppire_data_store_new:
257 * @dict: The dictionary for this data_store.
260 * Return value: a new #PsppireDataStore
263 psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
265 PsppireDataStore *retval;
267 retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
269 retval->cases = cases;
270 g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback),
273 g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback),
277 g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback),
281 psppire_data_store_set_dictionary(retval, dict);
290 * psppire_data_store_replace_set_dictionary:
291 * @data_store: The variable store
292 * @dict: The dictionary to set
294 * If a dictionary is already associated with the data-store, then it will be
298 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
301 if ( data_store->dict ) g_object_unref(data_store->dict);
304 data_store->dict = dict;
306 psppire_case_array_resize(data_store->cases,
307 dict_get_next_value_idx (data_store->dict->dict));
310 g_signal_connect(dict, "variable-inserted",
311 G_CALLBACK(insert_variable_callback),
314 g_signal_connect(dict, "variables-deleted",
315 G_CALLBACK(delete_variables_callback),
319 /* The entire model has changed */
320 g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
324 psppire_data_store_finalize (GObject *object)
328 (* parent_class->finalize) (object);
333 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
336 const struct fmt_spec *fp ;
337 const struct PsppireVariable *pv ;
338 const union value *v ;
340 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
342 g_return_val_if_fail(store->dict, NULL);
343 g_return_val_if_fail(store->cases, NULL);
345 if (column >= psppire_dict_get_var_cnt(store->dict))
348 if ( row >= psppire_case_array_get_n_cases(store->cases))
352 pv = psppire_dict_get_variable(store->dict, column);
354 v = psppire_case_array_get_value(store->cases, row,
355 psppire_variable_get_index(pv));
357 if ( store->show_labels)
359 const struct val_labs * vl = psppire_variable_get_value_labels(pv);
362 if ( (label = val_labs_find(vl, *v)) )
364 return pspp_locale_to_utf8(label, -1, 0);
368 fp = psppire_variable_get_write_spec(pv);
370 s = g_string_sized_new (fp->w + 1);
371 g_string_set_size(s, fp->w);
373 memset(s->str, 0, fp->w);
375 g_assert(fp->w == s->len);
377 /* Converts binary value V into printable form in the exactly
378 FP->W character in buffer S according to format specification
379 FP. No null terminator is appended to the buffer. */
380 data_out (s->str, fp, v);
383 text = pspp_locale_to_utf8(s->str, fp->w, 0);
384 g_string_free(s, TRUE);
391 set_null_string_value(union value *val, gpointer data)
398 set_sysmis_value(union value *val, gpointer data)
406 psppire_data_store_clear_datum(GSheetModel *model,
410 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
412 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
414 const gint index = psppire_variable_get_index(pv) ;
416 if ( psppire_variable_get_type(pv) == NUMERIC)
417 psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
419 psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
425 fillit(union value *val, gpointer data)
427 struct data_in *d_in = data;
431 if ( ! data_in(d_in) )
433 g_warning("Cant encode string\n");
441 /* Attempts to update that part of the variable store which corresponds
442 to ROW, COL with the value TEXT.
443 Returns true if anything was updated, false otherwise.
446 psppire_data_store_set_string(GSheetModel *model,
447 const gchar *text, gint row, gint col)
450 PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
452 const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
454 for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r )
457 psppire_case_array_insert_case(store->cases, r, 0, 0);
459 for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c )
460 psppire_data_store_clear_datum(model, r, c);
464 const gint index = psppire_variable_get_index(pv);
468 d_in.e = text + strlen(text);
470 d_in.f1 = d_in.f2 = 0;
471 d_in.format = * psppire_variable_get_write_spec(pv);
474 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)
524 const struct sfm_write_options wo = {
525 true, /* writeable */
526 false, /* dont compress */
530 struct sfm_writer *writer ;
534 writer = sfm_open_writer(handle, store->dict->dict, wo);
539 psppire_case_array_iterate_case(store->cases, write_case, writer);
541 sfm_close_writer(writer);
546 /* Column related funcs */
549 geometry_get_column_count(const GSheetColumn *geom)
551 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
553 return max(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
556 /* Return the width that an 'M' character would occupy when typeset at
559 M_width(GtkSheet *sheet, gint row, gint col)
561 GtkSheetCellAttr attributes;
563 /* FIXME: make this a member of the data store */
564 static PangoLayout *layout = 0;
566 gtk_sheet_get_attributes(sheet, row, col, &attributes);
569 layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
573 pango_layout_set_font_description (layout,
574 attributes.font_desc);
576 pango_layout_get_extents (layout, NULL, &rect);
579 g_object_unref(G_OBJECT(layout));
582 return PANGO_PIXELS(rect.width);
586 /* Return the number of pixels corresponding to a column of
589 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
591 return (M_width(sheet, 0, column) * width);
596 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
598 const struct PsppireVariable *pv ;
599 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
601 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
604 /* FIXME: We can optimise this by caching the widths until they're resized */
605 pv = psppire_dict_get_variable(ds->dict, unit);
607 return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
614 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
616 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
618 struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
620 psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
625 static GtkJustification
626 geometry_get_justification(const GSheetColumn *geom, gint unit)
628 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
629 const struct PsppireVariable *pv ;
632 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
633 return GTK_JUSTIFY_LEFT;
635 pv = psppire_dict_get_variable(ds->dict, unit);
637 /* Kludge: Happily GtkJustification is defined similarly
638 to enum alignment from pspp/variable.h */
639 return psppire_variable_get_alignment(pv);
643 static const gchar null_var_name[]=_("var");
646 geometry_get_button_label(const GSheetColumn *geom, gint unit)
649 struct PsppireVariable *pv ;
650 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
652 if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
653 return pspp_locale_to_utf8(null_var_name, -1, 0);
655 pv = psppire_dict_get_variable(ds->dict, unit);
657 text = pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
664 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
666 PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
669 return (unit < psppire_dict_get_var_cnt(ds->dict));
674 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
676 iface->get_column_count = geometry_get_column_count;
677 iface->get_width = geometry_get_width;
678 iface->set_width = geometry_set_width;
679 iface->get_visibility = always_true;
680 iface->get_sensitivity = geometry_get_sensitivity;
681 iface->get_justification = geometry_get_justification;
683 iface->get_button_label = geometry_get_button_label;