1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2006 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #define _(msgid) gettext (msgid)
22 #define N_(msgid) msgid
26 #include <gobject/gvaluecollector.h>
28 #include <gtksheet/gsheetmodel.h>
30 #include "psppire-var-store.h"
31 #include <gtksheet/gsheet-row-iface.h>
34 #include <data/dictionary.h>
35 #include <data/variable.h>
36 #include <data/format.h>
37 #include <data/missing-values.h>
39 #include "val-labs-dialog.h"
40 #include "missing-val-dialog.h"
41 #include <data/value-labels.h>
43 #include "var-display.h"
47 PSPPIRE_VAR_STORE_TRAILING_ROWS = 1,
48 PSPPIRE_VAR_STORE_FORMAT_TYPE
51 static void psppire_var_store_init (PsppireVarStore *var_store);
52 static void psppire_var_store_class_init (PsppireVarStoreClass *class);
53 static void psppire_var_store_sheet_model_init (GSheetModelIface *iface);
54 static void psppire_var_store_finalize (GObject *object);
57 gchar * missing_values_to_string (const struct variable *pv, GError **err);
60 static gchar *psppire_var_store_get_string (const GSheetModel *sheet_model, glong row, glong column);
62 static gboolean psppire_var_store_clear (GSheetModel *model, glong row, glong col);
65 static gboolean psppire_var_store_set_string (GSheetModel *model,
66 const gchar *text, glong row, glong column);
68 static glong psppire_var_store_get_row_count (const GSheetModel * model);
69 static glong psppire_var_store_get_column_count (const GSheetModel * model);
71 static gchar *text_for_column (const struct variable *pv, gint c, GError **err);
74 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
78 static GObjectClass *parent_class = NULL;
81 psppire_var_store_format_type_get_type (void)
83 static GType etype = 0;
86 static const GEnumValue values[] =
88 { PSPPIRE_VAR_STORE_INPUT_FORMATS,
89 "PSPPIRE_VAR_STORE_INPUT_FORMATS",
91 { PSPPIRE_VAR_STORE_OUTPUT_FORMATS,
92 "PSPPIRE_VAR_STORE_OUTPUT_FORMATS",
97 etype = g_enum_register_static
98 (g_intern_static_string ("PsppireVarStoreFormatType"), values);
105 psppire_var_store_get_type (void)
107 static GType var_store_type = 0;
111 static const GTypeInfo var_store_info =
113 sizeof (PsppireVarStoreClass),
114 NULL, /* base_init */
115 NULL, /* base_finalize */
116 (GClassInitFunc) psppire_var_store_class_init,
117 NULL, /* class_finalize */
118 NULL, /* class_data */
119 sizeof (PsppireVarStore),
121 (GInstanceInitFunc) psppire_var_store_init,
124 static const GInterfaceInfo sheet_model_info =
126 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
131 static const GInterfaceInfo sheet_row_info =
133 (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
138 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
140 g_type_add_interface_static (var_store_type,
144 g_type_add_interface_static (var_store_type,
151 return var_store_type;
155 psppire_var_store_set_property (GObject *object,
160 PsppireVarStore *self = (PsppireVarStore *) object;
164 case PSPPIRE_VAR_STORE_TRAILING_ROWS:
165 self->trailing_rows = g_value_get_int (value);
168 case PSPPIRE_VAR_STORE_FORMAT_TYPE:
169 self->format_type = g_value_get_enum (value);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
179 psppire_var_store_get_property (GObject *object,
184 PsppireVarStore *self = (PsppireVarStore *) object;
188 case PSPPIRE_VAR_STORE_TRAILING_ROWS:
189 g_value_set_int (value, self->trailing_rows);
192 case PSPPIRE_VAR_STORE_FORMAT_TYPE:
193 g_value_set_enum (value, self->format_type);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
204 psppire_var_store_class_init (PsppireVarStoreClass *class)
206 GObjectClass *object_class;
209 parent_class = g_type_class_peek_parent (class);
210 object_class = (GObjectClass*) class;
212 object_class->finalize = psppire_var_store_finalize;
213 object_class->set_property = psppire_var_store_set_property;
214 object_class->get_property = psppire_var_store_get_property;
216 /* The minimum value for trailing-rows is 1 to prevent the
217 var-store from ever having 0 rows, which breaks invariants
219 pspec = g_param_spec_int ("trailing-rows",
221 "Number of rows displayed after last variable",
222 1 /* minimum value */,
223 100 /* maximum value */,
224 40 /* default value */,
226 g_object_class_install_property (object_class,
227 PSPPIRE_VAR_STORE_TRAILING_ROWS,
230 pspec = g_param_spec_enum ("format-type",
231 "Variable format type",
232 ("Whether variables have input or output "
234 G_TYPE_PSPPIRE_VAR_STORE_FORMAT_TYPE,
235 PSPPIRE_VAR_STORE_OUTPUT_FORMATS,
237 g_object_class_install_property (object_class,
238 PSPPIRE_VAR_STORE_FORMAT_TYPE,
242 #define DISABLED_COLOR "pink"
244 psppire_var_store_init (PsppireVarStore *var_store)
246 if ( ! gdk_color_parse (DISABLED_COLOR, &var_store->disabled))
247 g_critical ("Could not parse color \"%s\"", DISABLED_COLOR);
250 var_store->trailing_rows = 40;
251 var_store->format_type = PSPPIRE_VAR_STORE_OUTPUT_FORMATS;
255 psppire_var_store_item_editable (PsppireVarStore *var_store, glong row, glong column)
257 const struct fmt_spec *write_spec ;
259 struct variable *pv = psppire_var_store_get_var (var_store, row);
264 if ( var_is_alpha (pv) && column == PSPPIRE_VAR_STORE_COL_DECIMALS )
267 write_spec = var_get_print_format (pv);
269 switch ( write_spec->type )
284 if ( column == PSPPIRE_VAR_STORE_COL_DECIMALS || column == PSPPIRE_VAR_STORE_COL_WIDTH)
296 psppire_var_store_get_var (PsppireVarStore *store, glong row)
298 return psppire_dict_get_variable (store->dict, row);
302 psppire_var_store_is_editable (const GSheetModel *model, glong row, glong column)
304 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
305 return psppire_var_store_item_editable (store, row, column);
309 static const GdkColor *
310 psppire_var_store_get_foreground (const GSheetModel *model, glong row, glong column)
312 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
314 if ( ! psppire_var_store_item_editable (store, row, column) )
315 return &store->disabled;
321 const PangoFontDescription *
322 psppire_var_store_get_font_desc (const GSheetModel *model,
323 glong row, glong column)
325 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
327 return store->font_desc;
334 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
336 iface->get_row_count = psppire_var_store_get_row_count;
337 iface->get_column_count = psppire_var_store_get_column_count;
338 iface->free_strings = TRUE;
339 iface->get_string = psppire_var_store_get_string;
340 iface->set_string = psppire_var_store_set_string;
341 iface->clear_datum = psppire_var_store_clear;
342 iface->is_editable = psppire_var_store_is_editable;
343 iface->is_visible = NULL;
344 iface->get_foreground = psppire_var_store_get_foreground;
345 iface->get_background = NULL;
346 iface->get_font_desc = psppire_var_store_get_font_desc;
347 iface->get_cell_border = NULL;
353 * psppire_var_store_new:
354 * @dict: The dictionary for this var_store.
357 * Return value: a new #PsppireVarStore
360 psppire_var_store_new (PsppireDict *dict)
362 PsppireVarStore *retval;
364 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
366 psppire_var_store_set_dictionary (retval, dict);
372 var_change_callback (GtkWidget *w, gint n, gpointer data)
374 GSheetModel *model = G_SHEET_MODEL (data);
376 g_sheet_model_range_changed (model,
377 n, 0, n, PSPPIRE_VAR_STORE_n_COLS);
382 var_delete_callback (GtkWidget *w, gint dict_idx, gint case_idx, gint val_cnt, gpointer data)
384 GSheetModel *model = G_SHEET_MODEL (data);
386 g_sheet_model_rows_deleted (model, dict_idx, 1);
392 var_insert_callback (GtkWidget *w, glong row, gpointer data)
394 GSheetModel *model = G_SHEET_MODEL (data);
396 g_sheet_model_rows_inserted (model, row, 1);
400 refresh (PsppireDict *d, gpointer data)
402 PsppireVarStore *vs = data;
404 g_sheet_model_range_changed (G_SHEET_MODEL (vs), -1, -1, -1, -1);
408 * psppire_var_store_replace_set_dictionary:
409 * @var_store: The variable store
410 * @dict: The dictionary to set
412 * If a dictionary is already associated with the var-store, then it will be
416 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
418 if ( var_store->dict ) g_object_unref (var_store->dict);
420 var_store->dict = dict;
422 g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
425 g_signal_connect (dict, "variable-deleted", G_CALLBACK (var_delete_callback),
428 g_signal_connect (dict, "variable-inserted",
429 G_CALLBACK (var_insert_callback), var_store);
431 g_signal_connect (dict, "backend-changed", G_CALLBACK (refresh),
434 /* The entire model has changed */
435 g_sheet_model_range_changed (G_SHEET_MODEL (var_store), -1, -1, -1, -1);
439 psppire_var_store_finalize (GObject *object)
442 (* parent_class->finalize) (object);
446 psppire_var_store_get_string (const GSheetModel *model, glong row, glong column)
448 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
452 if ( row >= psppire_dict_get_var_cnt (store->dict))
455 pv = psppire_dict_get_variable (store->dict, row);
457 return text_for_column (pv, column, 0);
461 /* Clears that part of the variable store, if possible, which corresponds
463 Returns true if anything was updated, false otherwise.
466 psppire_var_store_clear (GSheetModel *model, glong row, glong col)
468 struct variable *pv ;
470 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
472 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
475 pv = psppire_var_store_get_var (var_store, row);
482 case PSPPIRE_VAR_STORE_COL_LABEL:
483 var_set_label (pv, 0);
491 /* Attempts to update that part of the variable store which corresponds
492 to ROW, COL with the value TEXT.
493 Returns true if anything was updated, false otherwise.
496 psppire_var_store_set_string (GSheetModel *model,
497 const gchar *text, glong row, glong col)
499 struct variable *pv ;
501 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
503 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
506 pv = psppire_var_store_get_var (var_store, row);
513 case PSPPIRE_VAR_STORE_COL_NAME:
514 return psppire_dict_rename_var (var_store->dict, pv, text);
516 case PSPPIRE_VAR_STORE_COL_COLUMNS:
517 if ( ! text) return FALSE;
518 var_set_display_width (pv, atoi (text));
521 case PSPPIRE_VAR_STORE_COL_WIDTH:
523 int width = atoi (text);
524 if ( ! text) return FALSE;
525 if ( var_is_alpha (pv))
526 var_set_width (pv, width);
530 = var_store->format_type == PSPPIRE_VAR_STORE_INPUT_FORMATS;
531 struct fmt_spec fmt ;
532 fmt = *var_get_write_format (pv);
533 if ( width < fmt_min_width (fmt.type, for_input)
535 width > fmt_max_width (fmt.type, for_input))
539 fmt.d = MIN (fmt_max_decimals (fmt.type, width, for_input), fmt.d);
541 var_set_both_formats (pv, &fmt);
547 case PSPPIRE_VAR_STORE_COL_DECIMALS:
550 = var_store->format_type == PSPPIRE_VAR_STORE_INPUT_FORMATS;
553 if ( ! text) return FALSE;
554 decimals = atoi (text);
555 fmt = *var_get_write_format (pv);
557 fmt_max_decimals (fmt.type,
564 var_set_both_formats (pv, &fmt);
568 case PSPPIRE_VAR_STORE_COL_LABEL:
569 var_set_label (pv, text);
572 case PSPPIRE_VAR_STORE_COL_TYPE:
573 case PSPPIRE_VAR_STORE_COL_VALUES:
574 case PSPPIRE_VAR_STORE_COL_MISSING:
575 case PSPPIRE_VAR_STORE_COL_ALIGN:
576 case PSPPIRE_VAR_STORE_COL_MEASURE:
577 /* These can be modified only by their respective dialog boxes */
581 g_assert_not_reached ();
589 const static gchar none[] = N_("None");
592 text_for_column (const struct variable *pv, gint c, GError **err)
594 static const gchar *const type_label[] =
605 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
606 VT_CUSTOM, VT_STRING};
608 const struct fmt_spec *write_spec = var_get_write_format (pv);
612 case PSPPIRE_VAR_STORE_COL_NAME:
613 return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
615 case PSPPIRE_VAR_STORE_COL_TYPE:
617 switch ( write_spec->type )
620 return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
623 return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
626 return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
629 return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
644 return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
647 return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
654 return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
657 return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
661 char str[FMT_STRING_LEN_MAX + 1];
662 g_warning ("Unknown format: \"%s\"\n",
663 fmt_to_string (write_spec, str));
669 case PSPPIRE_VAR_STORE_COL_WIDTH:
672 GString *gstr = g_string_sized_new (10);
673 g_string_printf (gstr, _("%d"), write_spec->w);
674 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
675 g_string_free (gstr, TRUE);
679 case PSPPIRE_VAR_STORE_COL_DECIMALS:
682 GString *gstr = g_string_sized_new (10);
683 g_string_printf (gstr, _("%d"), write_spec->d);
684 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
685 g_string_free (gstr, TRUE);
689 case PSPPIRE_VAR_STORE_COL_COLUMNS:
692 GString *gstr = g_string_sized_new (10);
693 g_string_printf (gstr, _("%d"), var_get_display_width (pv));
694 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
695 g_string_free (gstr, TRUE);
699 case PSPPIRE_VAR_STORE_COL_LABEL:
700 return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
703 case PSPPIRE_VAR_STORE_COL_MISSING:
705 return missing_values_to_string (pv, err);
708 case PSPPIRE_VAR_STORE_COL_VALUES:
710 if ( ! var_has_value_labels (pv))
711 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
715 GString *gstr = g_string_sized_new (10);
716 const struct val_labs *vls = var_get_value_labels (pv);
717 struct val_labs_iterator *ip = 0;
718 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
723 gchar *const vstr = value_to_text (vl->value, *write_spec);
725 g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
731 ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
732 g_string_free (gstr, TRUE);
737 case PSPPIRE_VAR_STORE_COL_ALIGN:
739 const gint align = var_get_alignment (pv);
741 g_assert (align < n_ALIGNMENTS);
742 return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
745 case PSPPIRE_VAR_STORE_COL_MEASURE:
747 return measure_to_string (pv, err);
756 /* Return the number of variables */
758 psppire_var_store_get_var_cnt (PsppireVarStore *store)
760 return psppire_dict_get_var_cnt (store->dict);
765 psppire_var_store_set_font (PsppireVarStore *store, const PangoFontDescription *fd)
767 g_return_if_fail (store);
768 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
770 store->font_desc = fd;
772 g_sheet_model_range_changed (G_SHEET_MODEL (store), -1, -1, -1, -1);
777 psppire_var_store_get_row_count (const GSheetModel * model)
780 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
783 rows = psppire_dict_get_var_cnt (vs->dict);
789 psppire_var_store_get_column_count (const GSheetModel * model)
791 return PSPPIRE_VAR_STORE_n_COLS ;
795 /* Row related funcs */
798 geometry_get_row_count (const GSheetRow *geom)
801 PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
804 rows = psppire_dict_get_var_cnt (vs->dict);
806 return rows + vs->trailing_rows;
811 geometry_get_height (const GSheetRow *geom, glong row)
818 geometry_is_sensitive (const GSheetRow *geom, glong row)
820 PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
825 return row < psppire_dict_get_var_cnt (vs->dict);
829 geometry_get_button_label (const GSheetRow *geom, glong unit)
831 gchar *label = g_strdup_printf (_("%ld"), unit + 1);
837 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
839 iface->get_row_count = geometry_get_row_count;
840 iface->get_height = geometry_get_height;
841 iface->set_height = NULL;
842 iface->get_sensitivity = geometry_is_sensitive;
844 iface->get_button_label = geometry_get_button_label;