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
24 #include <libpspp/i18n.h>
26 #include <gobject/gvaluecollector.h>
28 #include <ui/gui/sheet/psppire-sheetmodel.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"
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 (PsppireSheetModelIface *iface);
53 static void psppire_var_store_finalize (GObject *object);
56 static gchar *psppire_var_store_get_string (const PsppireSheetModel *sheet_model, glong row, glong column);
58 static gboolean psppire_var_store_clear (PsppireSheetModel *model, glong row, glong col);
61 static gboolean psppire_var_store_set_string (PsppireSheetModel *model,
62 const gchar *text, glong row, glong column);
64 static glong psppire_var_store_get_row_count (const PsppireSheetModel * model);
65 static glong psppire_var_store_get_column_count (const PsppireSheetModel * model);
67 static gchar *text_for_column (PsppireVarStore *vs, const struct variable *pv,
68 gint c, GError **err);
71 static GObjectClass *parent_class = NULL;
74 psppire_var_store_format_type_get_type (void)
76 static GType etype = 0;
79 static const GEnumValue values[] =
81 { PSPPIRE_VAR_STORE_INPUT_FORMATS,
82 "PSPPIRE_VAR_STORE_INPUT_FORMATS",
84 { PSPPIRE_VAR_STORE_OUTPUT_FORMATS,
85 "PSPPIRE_VAR_STORE_OUTPUT_FORMATS",
90 etype = g_enum_register_static
91 (g_intern_static_string ("PsppireVarStoreFormatType"), values);
98 psppire_var_store_get_type (void)
100 static GType var_store_type = 0;
104 static const GTypeInfo var_store_info =
106 sizeof (PsppireVarStoreClass),
107 NULL, /* base_init */
108 NULL, /* base_finalize */
109 (GClassInitFunc) psppire_var_store_class_init,
110 NULL, /* class_finalize */
111 NULL, /* class_data */
112 sizeof (PsppireVarStore),
114 (GInstanceInitFunc) psppire_var_store_init,
117 static const GInterfaceInfo sheet_model_info =
119 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
124 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
126 g_type_add_interface_static (var_store_type,
127 PSPPIRE_TYPE_SHEET_MODEL,
131 return var_store_type;
135 psppire_var_store_set_property (GObject *object,
140 PsppireVarStore *self = (PsppireVarStore *) object;
144 case PSPPIRE_VAR_STORE_FORMAT_TYPE:
145 self->format_type = g_value_get_enum (value);
149 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
155 psppire_var_store_get_property (GObject *object,
160 PsppireVarStore *self = (PsppireVarStore *) object;
164 case PSPPIRE_VAR_STORE_FORMAT_TYPE:
165 g_value_set_enum (value, self->format_type);
169 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
176 psppire_var_store_class_init (PsppireVarStoreClass *class)
178 GObjectClass *object_class;
181 parent_class = g_type_class_peek_parent (class);
182 object_class = (GObjectClass*) class;
184 object_class->finalize = psppire_var_store_finalize;
185 object_class->set_property = psppire_var_store_set_property;
186 object_class->get_property = psppire_var_store_get_property;
188 pspec = g_param_spec_enum ("format-type",
189 "Variable format type",
190 ("Whether variables have input or output "
192 PSPPIRE_TYPE_VAR_STORE_FORMAT_TYPE,
193 PSPPIRE_VAR_STORE_OUTPUT_FORMATS,
196 g_object_class_install_property (object_class,
197 PSPPIRE_VAR_STORE_FORMAT_TYPE,
201 #define DISABLED_COLOR "gray"
204 psppire_var_store_init (PsppireVarStore *var_store)
206 if ( ! gdk_color_parse (DISABLED_COLOR, &var_store->disabled))
207 g_critical ("Could not parse color \"%s\"", DISABLED_COLOR);
210 var_store->format_type = PSPPIRE_VAR_STORE_OUTPUT_FORMATS;
214 psppire_var_store_item_editable (PsppireVarStore *var_store, glong row, glong column)
216 const struct fmt_spec *write_spec ;
218 struct variable *pv = psppire_var_store_get_var (var_store, row);
223 if ( var_is_alpha (pv) && column == PSPPIRE_VAR_STORE_COL_DECIMALS )
226 write_spec = var_get_print_format (pv);
228 switch ( write_spec->type )
243 if ( column == PSPPIRE_VAR_STORE_COL_DECIMALS || column == PSPPIRE_VAR_STORE_COL_WIDTH)
255 psppire_var_store_get_var (PsppireVarStore *store, glong row)
257 return psppire_dict_get_variable (store->dict, row);
261 psppire_var_store_is_editable (const PsppireSheetModel *model, glong row, glong column)
263 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
264 return psppire_var_store_item_editable (store, row, column);
269 psppire_var_store_get_foreground (const PsppireSheetModel *model, glong row, glong column)
271 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
273 if ( ! psppire_var_store_item_editable (store, row, column) )
274 return &store->disabled;
280 static gchar *get_column_title (const PsppireSheetModel *model, gint col);
281 static gchar *get_row_title (const PsppireSheetModel *model, gint row);
282 static gboolean get_row_sensitivity (const PsppireSheetModel *model, gint row);
285 psppire_var_store_sheet_model_init (PsppireSheetModelIface *iface)
287 iface->get_row_count = psppire_var_store_get_row_count;
288 iface->get_column_count = psppire_var_store_get_column_count;
289 iface->free_strings = TRUE;
290 iface->get_string = psppire_var_store_get_string;
291 iface->set_string = psppire_var_store_set_string;
292 iface->clear_datum = psppire_var_store_clear;
293 iface->is_editable = psppire_var_store_is_editable;
294 iface->get_foreground = psppire_var_store_get_foreground;
295 iface->get_background = NULL;
296 iface->get_justification = NULL;
298 iface->get_column_title = get_column_title;
300 iface->get_row_title = get_row_title;
301 iface->get_row_sensitivity = get_row_sensitivity;
303 iface->get_row_overstrike = NULL;
307 * psppire_var_store_new:
308 * @dict: The dictionary for this var_store.
311 * Return value: a new #PsppireVarStore
314 psppire_var_store_new (PsppireDict *dict)
316 PsppireVarStore *retval;
318 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
320 psppire_var_store_set_dictionary (retval, dict);
326 var_change_callback (GtkWidget *w, gint n, gpointer data)
328 PsppireSheetModel *model = PSPPIRE_SHEET_MODEL (data);
330 psppire_sheet_model_range_changed (model,
331 n, 0, n, PSPPIRE_VAR_STORE_n_COLS);
336 var_delete_callback (GtkWidget *w, gint dict_idx, gint case_idx, gint val_cnt, gpointer data)
338 PsppireSheetModel *model = PSPPIRE_SHEET_MODEL (data);
340 psppire_sheet_model_rows_deleted (model, dict_idx, 1);
346 var_insert_callback (GtkWidget *w, glong row, gpointer data)
348 PsppireSheetModel *model = PSPPIRE_SHEET_MODEL (data);
350 psppire_sheet_model_rows_inserted (model, row, 1);
354 refresh (PsppireDict *d, gpointer data)
356 PsppireVarStore *vs = data;
358 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (vs), -1, -1, -1, -1);
362 * psppire_var_store_replace_set_dictionary:
363 * @var_store: The variable store
364 * @dict: The dictionary to set
366 * If a dictionary is already associated with the var-store, then it will be
370 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
372 if ( var_store->dict ) g_object_unref (var_store->dict);
374 var_store->dict = dict;
376 g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
379 g_signal_connect (dict, "variable-deleted", G_CALLBACK (var_delete_callback),
382 g_signal_connect (dict, "variable-inserted",
383 G_CALLBACK (var_insert_callback), var_store);
385 g_signal_connect (dict, "backend-changed", G_CALLBACK (refresh),
388 /* The entire model has changed */
389 psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (var_store), -1, -1, -1, -1);
393 psppire_var_store_finalize (GObject *object)
396 (* parent_class->finalize) (object);
400 psppire_var_store_get_string (const PsppireSheetModel *model,
401 glong row, glong column)
403 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
407 if ( row >= psppire_dict_get_var_cnt (store->dict))
410 pv = psppire_dict_get_variable (store->dict, row);
412 return text_for_column (store, pv, column, 0);
416 /* Clears that part of the variable store, if possible, which corresponds
418 Returns true if anything was updated, false otherwise.
421 psppire_var_store_clear (PsppireSheetModel *model, glong row, glong col)
423 struct variable *pv ;
425 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
427 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
430 pv = psppire_var_store_get_var (var_store, row);
437 case PSPPIRE_VAR_STORE_COL_LABEL:
438 var_set_label (pv, 0);
446 /* Attempts to update that part of the variable store which corresponds
447 to ROW, COL with the value TEXT.
448 Returns true if anything was updated, false otherwise.
451 psppire_var_store_set_string (PsppireSheetModel *model,
452 const gchar *text, glong row, glong col)
454 struct variable *pv ;
456 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
458 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
461 pv = psppire_var_store_get_var (var_store, row);
468 case PSPPIRE_VAR_STORE_COL_NAME:
471 char *s = recode_string (psppire_dict_encoding (var_store->dict),
475 ok = psppire_dict_rename_var (var_store->dict, pv, s);
480 case PSPPIRE_VAR_STORE_COL_COLUMNS:
481 if ( ! text) return FALSE;
482 var_set_display_width (pv, atoi (text));
485 case PSPPIRE_VAR_STORE_COL_WIDTH:
487 int width = atoi (text);
488 if ( ! text) return FALSE;
489 if ( var_is_alpha (pv))
490 var_set_width (pv, width);
494 = var_store->format_type == PSPPIRE_VAR_STORE_INPUT_FORMATS;
495 struct fmt_spec fmt ;
496 fmt = *var_get_write_format (pv);
497 if ( width < fmt_min_width (fmt.type, for_input)
499 width > fmt_max_width (fmt.type, for_input))
503 fmt.d = MIN (fmt_max_decimals (fmt.type, width, for_input), fmt.d);
505 var_set_both_formats (pv, &fmt);
511 case PSPPIRE_VAR_STORE_COL_DECIMALS:
514 = var_store->format_type == PSPPIRE_VAR_STORE_INPUT_FORMATS;
517 if ( ! text) return FALSE;
518 decimals = atoi (text);
519 fmt = *var_get_write_format (pv);
521 fmt_max_decimals (fmt.type,
528 var_set_both_formats (pv, &fmt);
532 case PSPPIRE_VAR_STORE_COL_LABEL:
534 gchar *s = recode_string (psppire_dict_encoding (var_store->dict),
537 var_set_label (pv, s);
542 case PSPPIRE_VAR_STORE_COL_TYPE:
543 case PSPPIRE_VAR_STORE_COL_VALUES:
544 case PSPPIRE_VAR_STORE_COL_MISSING:
545 case PSPPIRE_VAR_STORE_COL_ALIGN:
546 case PSPPIRE_VAR_STORE_COL_MEASURE:
547 /* These can be modified only by their respective dialog boxes */
551 g_assert_not_reached ();
559 static const gchar none[] = N_("None");
562 text_for_column (PsppireVarStore *vs,
563 const struct variable *pv, gint c, GError **err)
565 PsppireDict *dict = vs->dict;
566 static const gchar *const type_label[] =
577 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
578 VT_CUSTOM, VT_STRING};
580 const struct fmt_spec *write_spec = var_get_write_format (pv);
584 case PSPPIRE_VAR_STORE_COL_NAME:
585 return recode_string (UTF8, psppire_dict_encoding (dict),
586 var_get_name (pv), -1);
588 case PSPPIRE_VAR_STORE_COL_TYPE:
590 switch ( write_spec->type )
593 return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
596 return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
599 return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
602 return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
617 return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
620 return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
627 return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
630 return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
634 char str[FMT_STRING_LEN_MAX + 1];
635 g_warning ("Unknown format: \"%s\"\n",
636 fmt_to_string (write_spec, str));
642 case PSPPIRE_VAR_STORE_COL_WIDTH:
645 GString *gstr = g_string_sized_new (10);
646 g_string_printf (gstr, _("%d"), write_spec->w);
647 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
648 g_string_free (gstr, TRUE);
652 case PSPPIRE_VAR_STORE_COL_DECIMALS:
655 GString *gstr = g_string_sized_new (10);
656 g_string_printf (gstr, _("%d"), write_spec->d);
657 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
658 g_string_free (gstr, TRUE);
662 case PSPPIRE_VAR_STORE_COL_COLUMNS:
665 GString *gstr = g_string_sized_new (10);
666 g_string_printf (gstr, _("%d"), var_get_display_width (pv));
667 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
668 g_string_free (gstr, TRUE);
672 case PSPPIRE_VAR_STORE_COL_LABEL:
673 return recode_string (UTF8, psppire_dict_encoding (dict),
674 var_get_label (pv), -1);
677 case PSPPIRE_VAR_STORE_COL_MISSING:
679 return missing_values_to_string (dict, pv, err);
682 case PSPPIRE_VAR_STORE_COL_VALUES:
684 if ( ! var_has_value_labels (pv))
685 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
689 GString *gstr = g_string_sized_new (10);
690 const struct val_labs *vls = var_get_value_labels (pv);
691 struct val_labs_iterator *ip = 0;
692 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
697 gchar *const vstr = value_to_text (vl->value, *write_spec);
699 g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
705 ss = recode_string (UTF8, psppire_dict_encoding (dict),
706 gstr->str, gstr->len);
707 g_string_free (gstr, TRUE);
712 case PSPPIRE_VAR_STORE_COL_ALIGN:
714 const gint align = var_get_alignment (pv);
716 g_assert (align < n_ALIGNMENTS);
717 return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
720 case PSPPIRE_VAR_STORE_COL_MEASURE:
722 return measure_to_string (pv, err);
731 /* Return the number of variables */
733 psppire_var_store_get_var_cnt (PsppireVarStore *store)
735 return psppire_dict_get_var_cnt (store->dict);
740 psppire_var_store_get_row_count (const PsppireSheetModel * model)
743 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
746 rows = psppire_dict_get_var_cnt (vs->dict);
752 psppire_var_store_get_column_count (const PsppireSheetModel * model)
754 return PSPPIRE_VAR_STORE_n_COLS ;
759 /* Row related funcs */
763 get_row_sensitivity (const PsppireSheetModel *model, gint row)
765 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
770 return row < psppire_dict_get_var_cnt (vs->dict);
775 get_row_title (const PsppireSheetModel *model, gint unit)
777 return g_strdup_printf (_("%d"), unit + 1);
783 static const gchar *column_titles[] = {
798 get_column_title (const PsppireSheetModel *model, gint col)
802 return g_strdup (gettext (column_titles[col]));