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"
33 #include <data/dictionary.h>
34 #include <data/variable.h>
35 #include <data/format.h>
36 #include <data/missing-values.h>
38 #include "val-labs-dialog.h"
39 #include "missing-val-dialog.h"
40 #include <data/value-labels.h>
42 #include "var-display.h"
46 PSPPIRE_VAR_STORE_TRAILING_ROWS = 1,
47 PSPPIRE_VAR_STORE_FORMAT_TYPE
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);
56 gchar * missing_values_to_string (const struct variable *pv, GError **err);
59 static gchar *psppire_var_store_get_string (const GSheetModel *sheet_model, glong row, glong column);
61 static gboolean psppire_var_store_clear (GSheetModel *model, glong row, glong col);
64 static gboolean psppire_var_store_set_string (GSheetModel *model,
65 const gchar *text, glong row, glong column);
67 static glong psppire_var_store_get_row_count (const GSheetModel * model);
68 static glong psppire_var_store_get_column_count (const GSheetModel * model);
70 static gchar *text_for_column (const struct variable *pv, gint c, GError **err);
73 static GObjectClass *parent_class = NULL;
76 psppire_var_store_format_type_get_type (void)
78 static GType etype = 0;
81 static const GEnumValue values[] =
83 { PSPPIRE_VAR_STORE_INPUT_FORMATS,
84 "PSPPIRE_VAR_STORE_INPUT_FORMATS",
86 { PSPPIRE_VAR_STORE_OUTPUT_FORMATS,
87 "PSPPIRE_VAR_STORE_OUTPUT_FORMATS",
92 etype = g_enum_register_static
93 (g_intern_static_string ("PsppireVarStoreFormatType"), values);
100 psppire_var_store_get_type (void)
102 static GType var_store_type = 0;
106 static const GTypeInfo var_store_info =
108 sizeof (PsppireVarStoreClass),
109 NULL, /* base_init */
110 NULL, /* base_finalize */
111 (GClassInitFunc) psppire_var_store_class_init,
112 NULL, /* class_finalize */
113 NULL, /* class_data */
114 sizeof (PsppireVarStore),
116 (GInstanceInitFunc) psppire_var_store_init,
119 static const GInterfaceInfo sheet_model_info =
121 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
126 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
128 g_type_add_interface_static (var_store_type,
133 return var_store_type;
137 psppire_var_store_set_property (GObject *object,
142 PsppireVarStore *self = (PsppireVarStore *) object;
146 case PSPPIRE_VAR_STORE_TRAILING_ROWS:
147 self->trailing_rows = g_value_get_int (value);
150 case PSPPIRE_VAR_STORE_FORMAT_TYPE:
151 self->format_type = g_value_get_enum (value);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
161 psppire_var_store_get_property (GObject *object,
166 PsppireVarStore *self = (PsppireVarStore *) object;
170 case PSPPIRE_VAR_STORE_TRAILING_ROWS:
171 g_value_set_int (value, self->trailing_rows);
174 case PSPPIRE_VAR_STORE_FORMAT_TYPE:
175 g_value_set_enum (value, self->format_type);
179 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
186 psppire_var_store_class_init (PsppireVarStoreClass *class)
188 GObjectClass *object_class;
191 parent_class = g_type_class_peek_parent (class);
192 object_class = (GObjectClass*) class;
194 object_class->finalize = psppire_var_store_finalize;
195 object_class->set_property = psppire_var_store_set_property;
196 object_class->get_property = psppire_var_store_get_property;
198 /* The minimum value for trailing-rows is 1 to prevent the
199 var-store from ever having 0 rows, which breaks invariants
201 pspec = g_param_spec_int ("trailing-rows",
203 "Number of rows displayed after last variable",
204 1 /* minimum value */,
205 100 /* maximum value */,
206 40 /* default value */,
208 g_object_class_install_property (object_class,
209 PSPPIRE_VAR_STORE_TRAILING_ROWS,
212 pspec = g_param_spec_enum ("format-type",
213 "Variable format type",
214 ("Whether variables have input or output "
216 G_TYPE_PSPPIRE_VAR_STORE_FORMAT_TYPE,
217 PSPPIRE_VAR_STORE_OUTPUT_FORMATS,
219 g_object_class_install_property (object_class,
220 PSPPIRE_VAR_STORE_FORMAT_TYPE,
224 #define DISABLED_COLOR "gray"
227 psppire_var_store_init (PsppireVarStore *var_store)
229 if ( ! gdk_color_parse (DISABLED_COLOR, &var_store->disabled))
230 g_critical ("Could not parse color \"%s\"", DISABLED_COLOR);
233 var_store->trailing_rows = 40;
234 var_store->format_type = PSPPIRE_VAR_STORE_OUTPUT_FORMATS;
238 psppire_var_store_item_editable (PsppireVarStore *var_store, glong row, glong column)
240 const struct fmt_spec *write_spec ;
242 struct variable *pv = psppire_var_store_get_var (var_store, row);
247 if ( var_is_alpha (pv) && column == PSPPIRE_VAR_STORE_COL_DECIMALS )
250 write_spec = var_get_print_format (pv);
252 switch ( write_spec->type )
267 if ( column == PSPPIRE_VAR_STORE_COL_DECIMALS || column == PSPPIRE_VAR_STORE_COL_WIDTH)
279 psppire_var_store_get_var (PsppireVarStore *store, glong row)
281 return psppire_dict_get_variable (store->dict, row);
285 psppire_var_store_is_editable (const GSheetModel *model, glong row, glong column)
287 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
288 return psppire_var_store_item_editable (store, row, column);
293 psppire_var_store_get_foreground (const GSheetModel *model, glong row, glong column)
295 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
297 if ( ! psppire_var_store_item_editable (store, row, column) )
298 return &store->disabled;
304 static gchar *get_column_title (const GSheetModel *model, gint col);
305 static gchar *get_row_title (const GSheetModel *model, gint row);
306 static gboolean get_row_sensitivity (const GSheetModel *model, gint row);
309 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
311 iface->get_row_count = psppire_var_store_get_row_count;
312 iface->get_column_count = psppire_var_store_get_column_count;
313 iface->free_strings = TRUE;
314 iface->get_string = psppire_var_store_get_string;
315 iface->set_string = psppire_var_store_set_string;
316 iface->clear_datum = psppire_var_store_clear;
317 iface->is_editable = psppire_var_store_is_editable;
318 iface->get_foreground = psppire_var_store_get_foreground;
319 iface->get_background = NULL;
320 iface->get_cell_border = NULL;
321 iface->get_justification = NULL;
323 iface->get_column_title = get_column_title;
325 iface->get_row_title = get_row_title;
326 iface->get_row_sensitivity = get_row_sensitivity;
330 * psppire_var_store_new:
331 * @dict: The dictionary for this var_store.
334 * Return value: a new #PsppireVarStore
337 psppire_var_store_new (PsppireDict *dict)
339 PsppireVarStore *retval;
341 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
343 psppire_var_store_set_dictionary (retval, dict);
349 var_change_callback (GtkWidget *w, gint n, gpointer data)
351 GSheetModel *model = G_SHEET_MODEL (data);
353 g_sheet_model_range_changed (model,
354 n, 0, n, PSPPIRE_VAR_STORE_n_COLS);
359 var_delete_callback (GtkWidget *w, gint dict_idx, gint case_idx, gint val_cnt, gpointer data)
361 GSheetModel *model = G_SHEET_MODEL (data);
363 g_sheet_model_rows_deleted (model, dict_idx, 1);
369 var_insert_callback (GtkWidget *w, glong row, gpointer data)
371 GSheetModel *model = G_SHEET_MODEL (data);
373 g_sheet_model_rows_inserted (model, row, 1);
377 refresh (PsppireDict *d, gpointer data)
379 PsppireVarStore *vs = data;
381 g_sheet_model_range_changed (G_SHEET_MODEL (vs), -1, -1, -1, -1);
385 * psppire_var_store_replace_set_dictionary:
386 * @var_store: The variable store
387 * @dict: The dictionary to set
389 * If a dictionary is already associated with the var-store, then it will be
393 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
395 if ( var_store->dict ) g_object_unref (var_store->dict);
397 var_store->dict = dict;
399 g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
402 g_signal_connect (dict, "variable-deleted", G_CALLBACK (var_delete_callback),
405 g_signal_connect (dict, "variable-inserted",
406 G_CALLBACK (var_insert_callback), var_store);
408 g_signal_connect (dict, "backend-changed", G_CALLBACK (refresh),
411 /* The entire model has changed */
412 g_sheet_model_range_changed (G_SHEET_MODEL (var_store), -1, -1, -1, -1);
416 psppire_var_store_finalize (GObject *object)
419 (* parent_class->finalize) (object);
423 psppire_var_store_get_string (const GSheetModel *model, glong row, glong column)
425 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
429 if ( row >= psppire_dict_get_var_cnt (store->dict))
432 pv = psppire_dict_get_variable (store->dict, row);
434 return text_for_column (pv, column, 0);
438 /* Clears that part of the variable store, if possible, which corresponds
440 Returns true if anything was updated, false otherwise.
443 psppire_var_store_clear (GSheetModel *model, glong row, glong col)
445 struct variable *pv ;
447 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
449 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
452 pv = psppire_var_store_get_var (var_store, row);
459 case PSPPIRE_VAR_STORE_COL_LABEL:
460 var_set_label (pv, 0);
468 /* Attempts to update that part of the variable store which corresponds
469 to ROW, COL with the value TEXT.
470 Returns true if anything was updated, false otherwise.
473 psppire_var_store_set_string (GSheetModel *model,
474 const gchar *text, glong row, glong col)
476 struct variable *pv ;
478 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
480 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
483 pv = psppire_var_store_get_var (var_store, row);
490 case PSPPIRE_VAR_STORE_COL_NAME:
491 return psppire_dict_rename_var (var_store->dict, pv, text);
493 case PSPPIRE_VAR_STORE_COL_COLUMNS:
494 if ( ! text) return FALSE;
495 var_set_display_width (pv, atoi (text));
498 case PSPPIRE_VAR_STORE_COL_WIDTH:
500 int width = atoi (text);
501 if ( ! text) return FALSE;
502 if ( var_is_alpha (pv))
503 var_set_width (pv, width);
507 = var_store->format_type == PSPPIRE_VAR_STORE_INPUT_FORMATS;
508 struct fmt_spec fmt ;
509 fmt = *var_get_write_format (pv);
510 if ( width < fmt_min_width (fmt.type, for_input)
512 width > fmt_max_width (fmt.type, for_input))
516 fmt.d = MIN (fmt_max_decimals (fmt.type, width, for_input), fmt.d);
518 var_set_both_formats (pv, &fmt);
524 case PSPPIRE_VAR_STORE_COL_DECIMALS:
527 = var_store->format_type == PSPPIRE_VAR_STORE_INPUT_FORMATS;
530 if ( ! text) return FALSE;
531 decimals = atoi (text);
532 fmt = *var_get_write_format (pv);
534 fmt_max_decimals (fmt.type,
541 var_set_both_formats (pv, &fmt);
545 case PSPPIRE_VAR_STORE_COL_LABEL:
546 var_set_label (pv, text);
549 case PSPPIRE_VAR_STORE_COL_TYPE:
550 case PSPPIRE_VAR_STORE_COL_VALUES:
551 case PSPPIRE_VAR_STORE_COL_MISSING:
552 case PSPPIRE_VAR_STORE_COL_ALIGN:
553 case PSPPIRE_VAR_STORE_COL_MEASURE:
554 /* These can be modified only by their respective dialog boxes */
558 g_assert_not_reached ();
566 const static gchar none[] = N_("None");
569 text_for_column (const struct variable *pv, gint c, GError **err)
571 static const gchar *const type_label[] =
582 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
583 VT_CUSTOM, VT_STRING};
585 const struct fmt_spec *write_spec = var_get_write_format (pv);
589 case PSPPIRE_VAR_STORE_COL_NAME:
590 return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
592 case PSPPIRE_VAR_STORE_COL_TYPE:
594 switch ( write_spec->type )
597 return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
600 return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
603 return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
606 return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
621 return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
624 return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
631 return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
634 return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
638 char str[FMT_STRING_LEN_MAX + 1];
639 g_warning ("Unknown format: \"%s\"\n",
640 fmt_to_string (write_spec, str));
646 case PSPPIRE_VAR_STORE_COL_WIDTH:
649 GString *gstr = g_string_sized_new (10);
650 g_string_printf (gstr, _("%d"), write_spec->w);
651 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
652 g_string_free (gstr, TRUE);
656 case PSPPIRE_VAR_STORE_COL_DECIMALS:
659 GString *gstr = g_string_sized_new (10);
660 g_string_printf (gstr, _("%d"), write_spec->d);
661 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
662 g_string_free (gstr, TRUE);
666 case PSPPIRE_VAR_STORE_COL_COLUMNS:
669 GString *gstr = g_string_sized_new (10);
670 g_string_printf (gstr, _("%d"), var_get_display_width (pv));
671 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
672 g_string_free (gstr, TRUE);
676 case PSPPIRE_VAR_STORE_COL_LABEL:
677 return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
680 case PSPPIRE_VAR_STORE_COL_MISSING:
682 return missing_values_to_string (pv, err);
685 case PSPPIRE_VAR_STORE_COL_VALUES:
687 if ( ! var_has_value_labels (pv))
688 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
692 GString *gstr = g_string_sized_new (10);
693 const struct val_labs *vls = var_get_value_labels (pv);
694 struct val_labs_iterator *ip = 0;
695 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
700 gchar *const vstr = value_to_text (vl->value, *write_spec);
702 g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
708 ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
709 g_string_free (gstr, TRUE);
714 case PSPPIRE_VAR_STORE_COL_ALIGN:
716 const gint align = var_get_alignment (pv);
718 g_assert (align < n_ALIGNMENTS);
719 return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
722 case PSPPIRE_VAR_STORE_COL_MEASURE:
724 return measure_to_string (pv, err);
733 /* Return the number of variables */
735 psppire_var_store_get_var_cnt (PsppireVarStore *store)
737 return psppire_dict_get_var_cnt (store->dict);
742 psppire_var_store_get_row_count (const GSheetModel * model)
745 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
748 rows = psppire_dict_get_var_cnt (vs->dict);
754 psppire_var_store_get_column_count (const GSheetModel * model)
756 return PSPPIRE_VAR_STORE_n_COLS ;
761 /* Row related funcs */
765 get_row_sensitivity (const GSheetModel *model, gint row)
767 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
772 return row < psppire_dict_get_var_cnt (vs->dict);
777 get_row_title (const GSheetModel *model, gint unit)
779 return g_strdup_printf (_("%d"), unit + 1);
785 static const gchar *column_titles[] = {
800 get_column_title (const GSheetModel *model, gint col)
804 return g_strdup (gettext (column_titles[col]));