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 <gobject/gvaluecollector.h>
33 #include <gtksheet/gsheetmodel.h>
35 #include "psppire-var-store.h"
36 #include "var-sheet.h"
39 #include <data/dictionary.h>
40 #include <data/variable.h>
41 #include <data/missing-values.h>
43 #include "val-labs-dialog.h"
44 #include "missing-val-dialog.h"
45 #include <data/value-labels.h>
48 #define TRAILING_ROWS 40
50 static void psppire_var_store_init (PsppireVarStore *var_store);
51 static void psppire_var_store_class_init (PsppireVarStoreClass *class);
52 static void psppire_var_store_sheet_model_init (GSheetModelIface *iface);
53 static void psppire_var_store_finalize (GObject *object);
55 static gchar *psppire_var_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
57 static gboolean psppire_var_store_clear(GSheetModel *model, gint row, gint col);
60 static gboolean psppire_var_store_set_string(GSheetModel *model,
61 const gchar *text, gint row, gint column);
63 static gint psppire_var_store_get_row_count(const GSheetModel * model);
65 static gchar *text_for_column(const struct variable *pv, gint c, GError **err);
68 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
72 static GObjectClass *parent_class = NULL;
75 psppire_var_store_get_type (void)
77 static GType var_store_type = 0;
81 static const GTypeInfo var_store_info =
83 sizeof (PsppireVarStoreClass),
85 NULL, /* base_finalize */
86 (GClassInitFunc) psppire_var_store_class_init,
87 NULL, /* class_finalize */
88 NULL, /* class_data */
89 sizeof (PsppireVarStore),
91 (GInstanceInitFunc) psppire_var_store_init,
94 static const GInterfaceInfo sheet_model_info =
96 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
101 static const GInterfaceInfo sheet_row_info =
103 (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
108 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
110 g_type_add_interface_static (var_store_type,
114 g_type_add_interface_static (var_store_type,
121 return var_store_type;
125 psppire_var_store_class_init (PsppireVarStoreClass *class)
127 GObjectClass *object_class;
129 parent_class = g_type_class_peek_parent (class);
130 object_class = (GObjectClass*) class;
132 object_class->finalize = psppire_var_store_finalize;
137 psppire_var_store_init (PsppireVarStore *var_store)
139 GdkColormap *colormap = gdk_colormap_get_system();
141 g_assert(gdk_color_parse("gray", &var_store->disabled));
143 gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
149 psppire_var_store_item_editable(PsppireVarStore *var_store, gint row, gint column)
151 const struct fmt_spec *write_spec ;
153 struct variable *pv = psppire_var_store_get_var (var_store, row);
158 if ( VAR_STRING == var_get_type (pv) && column == COL_DECIMALS )
161 write_spec =var_get_write_format (pv);
163 switch ( write_spec->type )
178 if ( column == COL_DECIMALS || column == COL_WIDTH)
190 psppire_var_store_get_var (PsppireVarStore *store, gint row)
192 return psppire_dict_get_variable (store->dict, row);
196 psppire_var_store_is_editable(const GSheetModel *model, gint row, gint column)
198 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
199 return psppire_var_store_item_editable(store, row, column);
203 static const GdkColor *
204 psppire_var_store_get_foreground(const GSheetModel *model, gint row, gint column)
206 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
208 if ( ! psppire_var_store_item_editable(store, row, column) )
209 return &store->disabled;
215 const PangoFontDescription *
216 psppire_var_store_get_font_desc(const GSheetModel *model,
217 gint row, gint column)
219 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
221 return store->font_desc;
227 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
229 iface->get_row_count = psppire_var_store_get_row_count;
230 iface->free_strings = TRUE;
231 iface->get_string = psppire_var_store_get_string;
232 iface->set_string = psppire_var_store_set_string;
233 iface->clear_datum = psppire_var_store_clear;
234 iface->is_editable = psppire_var_store_is_editable;
235 iface->is_visible = NULL;
236 iface->get_foreground = psppire_var_store_get_foreground;
237 iface->get_background = NULL;
238 iface->get_font_desc = psppire_var_store_get_font_desc;
239 iface->get_cell_border = NULL;
245 * psppire_var_store_new:
246 * @dict: The dictionary for this var_store.
249 * Return value: a new #PsppireVarStore
252 psppire_var_store_new (PsppireDict *dict)
254 PsppireVarStore *retval;
256 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
258 psppire_var_store_set_dictionary(retval, dict);
264 var_change_callback(GtkWidget *w, gint n, gpointer data)
266 GSheetModel *model = G_SHEET_MODEL(data);
267 g_sheet_model_range_changed (model,
273 var_delete_callback(GtkWidget *w, gint first, gint n, gpointer data)
275 GSheetModel *model = G_SHEET_MODEL(data);
277 g_sheet_model_rows_deleted (model, first, n);
283 var_insert_callback(GtkWidget *w, gint row, gpointer data)
285 GSheetModel *model = G_SHEET_MODEL(data);
287 g_sheet_model_rows_inserted (model, row, 1);
293 * psppire_var_store_replace_set_dictionary:
294 * @var_store: The variable store
295 * @dict: The dictionary to set
297 * If a dictionary is already associated with the var-store, then it will be
301 psppire_var_store_set_dictionary(PsppireVarStore *var_store, PsppireDict *dict)
303 if ( var_store->dict ) g_object_unref(var_store->dict);
305 var_store->dict = dict;
307 g_signal_connect(dict, "variable-changed", G_CALLBACK(var_change_callback),
310 g_signal_connect(dict, "variables-deleted", G_CALLBACK(var_delete_callback),
313 g_signal_connect(dict, "variable-inserted", G_CALLBACK(var_insert_callback),
317 /* The entire model has changed */
318 g_sheet_model_range_changed (G_SHEET_MODEL(var_store), -1, -1, -1, -1);
322 psppire_var_store_finalize (GObject *object)
325 (* parent_class->finalize) (object);
329 psppire_var_store_get_string(const GSheetModel *model, gint row, gint column)
331 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
335 if ( row >= psppire_dict_get_var_cnt(store->dict))
338 pv = psppire_dict_get_variable (store->dict, row);
340 return text_for_column (pv, column, 0);
344 /* Clears that part of the variable store, if possible, which corresponds
346 Returns true if anything was updated, false otherwise.
349 psppire_var_store_clear(GSheetModel *model, gint row, gint col)
351 struct variable *pv ;
353 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
355 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
358 pv = psppire_var_store_get_var (var_store, row);
366 var_set_label (pv, 0);
374 /* Attempts to update that part of the variable store which corresponds
375 to ROW, COL with the value TEXT.
376 Returns true if anything was updated, false otherwise.
379 psppire_var_store_set_string(GSheetModel *model,
380 const gchar *text, gint row, gint col)
382 struct variable *pv ;
384 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
386 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
389 pv = psppire_var_store_get_var (var_store, row);
397 psppire_dict_rename_var (var_store->dict, pv, text);
401 if ( ! text) return FALSE;
402 var_set_display_width (pv, atoi(text));
407 int width = atoi (text);
408 if ( ! text) return FALSE;
409 if ( var_is_alpha (pv))
410 var_set_width (pv, width);
413 struct fmt_spec fmt ;
414 fmt = *var_get_write_format (pv);
415 if ( width < fmt_min_output_width (fmt.type)
417 width > fmt_max_output_width (fmt.type))
421 fmt.d = MIN (fmt_max_output_decimals (fmt.type, width), fmt.d);
423 var_set_both_formats (pv, &fmt);
433 if ( ! text) return FALSE;
434 decimals = atoi (text);
435 fmt = *var_get_write_format (pv);
437 fmt_max_output_decimals (fmt.type,
443 var_set_both_formats (pv, &fmt);
448 var_set_label(pv, text);
456 /* These can be modified only by their respective dialog boxes */
460 g_assert_not_reached();
469 text_for_column(const struct variable *pv, gint c, GError **err)
471 static gchar none[] = N_("None");
473 static const gchar *const type_label[] =
484 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
485 VT_CUSTOM, VT_STRING};
487 const struct fmt_spec *write_spec = var_get_write_format (pv);
492 return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
496 switch ( write_spec->type )
499 return g_locale_to_utf8(gettext(type_label[VT_NUMERIC]), -1, 0, 0, err);
502 return g_locale_to_utf8(gettext(type_label[VT_COMMA]), -1, 0, 0, err);
505 return g_locale_to_utf8(gettext(type_label[VT_DOT]), -1, 0, 0, err);
508 return g_locale_to_utf8(gettext(type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
523 return g_locale_to_utf8(type_label[VT_DATE], -1, 0, 0, err);
526 return g_locale_to_utf8(type_label[VT_DOLLAR], -1, 0, 0, err);
533 return g_locale_to_utf8(gettext(type_label[VT_CUSTOM]), -1, 0, 0, err);
536 return g_locale_to_utf8(gettext(type_label[VT_STRING]), -1, 0, 0, err);
540 char str[FMT_STRING_LEN_MAX + 1];
541 g_warning("Unknown format: \"%s\"\n",
542 fmt_to_string(write_spec, str));
551 GString *gstr = g_string_sized_new(10);
552 g_string_printf(gstr, _("%d"), write_spec->w);
553 s = g_locale_to_utf8(gstr->str, gstr->len, 0, 0, err);
554 g_string_free(gstr, TRUE);
561 GString *gstr = g_string_sized_new(10);
562 g_string_printf(gstr, _("%d"), write_spec->d);
563 s = g_locale_to_utf8(gstr->str, gstr->len, 0, 0, err);
564 g_string_free(gstr, TRUE);
571 GString *gstr = g_string_sized_new(10);
572 g_string_printf(gstr, _("%d"), var_get_display_width (pv));
573 s = g_locale_to_utf8(gstr->str, gstr->len, 0, 0, err);
574 g_string_free(gstr, TRUE);
579 return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
585 const struct missing_values *miss = var_get_missing_values (pv);
586 if ( mv_is_empty(miss))
587 return g_locale_to_utf8(gettext(none), -1, 0, 0, err);
590 if ( ! mv_has_range (miss))
592 GString *gstr = g_string_sized_new(10);
593 const int n = mv_n_values(miss);
594 gchar *mv[4] = {0,0,0,0};
596 for(i = 0 ; i < n; ++i )
599 mv_peek_value(miss, &v, i);
600 mv[i] = value_to_text(v, *write_spec);
602 g_string_append(gstr, ", ");
603 g_string_append(gstr, mv[i]);
606 s = pspp_locale_to_utf8(gstr->str, gstr->len, err);
607 g_string_free(gstr, TRUE);
611 GString *gstr = g_string_sized_new(10);
613 union value low, high;
614 mv_peek_range(miss, &low.f, &high.f);
616 l = value_to_text(low, *write_spec);
617 h = value_to_text(high, *write_spec);
619 g_string_printf(gstr, "%s - %s", l, h);
623 if ( mv_has_value(miss))
627 mv_peek_value(miss, &v, 0);
629 ss = value_to_text(v, *write_spec);
631 g_string_append(gstr, ", ");
632 g_string_append(gstr, ss);
635 s = pspp_locale_to_utf8(gstr->str, gstr->len, err);
636 g_string_free(gstr, TRUE);
645 if ( ! var_has_value_labels (pv))
646 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
650 GString *gstr = g_string_sized_new (10);
651 const struct val_labs *vls = var_get_value_labels (pv);
652 struct val_labs_iterator *ip = 0;
653 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
658 gchar *const vstr = value_to_text (vl->value, *write_spec);
660 g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
666 ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
667 g_string_free (gstr, TRUE);
674 const gint align = var_get_alignment(pv);
676 g_assert (align < n_ALIGNMENTS);
677 return g_locale_to_utf8(gettext(alignments[align]), -1, 0, 0, err);
682 const gint measure = var_get_measure (pv);
684 g_assert (measure - 1 < n_MEASURES);
685 return g_locale_to_utf8 (gettext (measures[measure - 1]),
695 /* Return the number of variables */
697 psppire_var_store_get_var_cnt(PsppireVarStore *store)
699 return psppire_dict_get_var_cnt(store->dict);
704 psppire_var_store_set_font(PsppireVarStore *store, const PangoFontDescription *fd)
706 g_return_if_fail (store);
707 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
709 store->font_desc = fd;
711 g_sheet_model_range_changed (G_SHEET_MODEL(store), -1, -1, -1, -1);
716 psppire_var_store_get_row_count(const GSheetModel * model)
719 PsppireVarStore *vs = PSPPIRE_VAR_STORE(model);
722 rows = psppire_dict_get_var_cnt(vs->dict);
727 /* Row related funcs */
730 geometry_get_row_count(const GSheetRow *geom, gpointer data)
733 PsppireVarStore *vs = PSPPIRE_VAR_STORE(geom);
736 rows = psppire_dict_get_var_cnt(vs->dict);
738 return rows + TRAILING_ROWS;
743 geometry_get_height(const GSheetRow *geom, gint row, gpointer data)
750 geometry_is_sensitive(const GSheetRow *geom, gint row, gpointer data)
752 PsppireVarStore *vs = PSPPIRE_VAR_STORE(geom);
757 return row < psppire_dict_get_var_cnt(vs->dict);
761 gboolean always_true()
768 geometry_get_button_label(const GSheetRow *geom, gint unit, gpointer data)
770 gchar *label = g_strdup_printf(_("%d"), unit);
777 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
779 iface->get_row_count = geometry_get_row_count;
780 iface->get_height = geometry_get_height;
781 iface->set_height = 0;
782 iface->get_visibility = always_true;
783 iface->get_sensitivity = geometry_is_sensitive;
785 iface->get_button_label = geometry_get_button_label;