3 PSPPIRE --- A Graphical User Interface for PSPP
4 Copyright (C) 2006 Free Software Foundation
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #define _(msgid) gettext (msgid)
26 #define N_(msgid) msgid
30 #include <gobject/gvaluecollector.h>
32 #include <gtksheet/gsheetmodel.h>
34 #include "psppire-var-store.h"
35 #include "var-sheet.h"
38 #include <data/dictionary.h>
39 #include <data/variable.h>
40 #include <data/missing-values.h>
42 #include "val-labs-dialog.h"
43 #include "missing-val-dialog.h"
44 #include <data/value-labels.h>
47 #define TRAILING_ROWS 40
49 static void psppire_var_store_init (PsppireVarStore *var_store);
50 static void psppire_var_store_class_init (PsppireVarStoreClass *class);
51 static void psppire_var_store_sheet_model_init (GSheetModelIface *iface);
52 static void psppire_var_store_finalize (GObject *object);
54 static gchar *psppire_var_store_get_string (const GSheetModel *sheet_model, gint row, gint column);
56 static gboolean psppire_var_store_clear (GSheetModel *model, gint row, gint col);
59 static gboolean psppire_var_store_set_string (GSheetModel *model,
60 const gchar *text, gint row, gint column);
62 static gint psppire_var_store_get_row_count (const GSheetModel * model);
64 static gchar *text_for_column (const struct variable *pv, gint c, GError **err);
67 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
71 static GObjectClass *parent_class = NULL;
74 psppire_var_store_get_type (void)
76 static GType var_store_type = 0;
80 static const GTypeInfo var_store_info =
82 sizeof (PsppireVarStoreClass),
84 NULL, /* base_finalize */
85 (GClassInitFunc) psppire_var_store_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
88 sizeof (PsppireVarStore),
90 (GInstanceInitFunc) psppire_var_store_init,
93 static const GInterfaceInfo sheet_model_info =
95 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
100 static const GInterfaceInfo sheet_row_info =
102 (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
107 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
109 g_type_add_interface_static (var_store_type,
113 g_type_add_interface_static (var_store_type,
120 return var_store_type;
124 psppire_var_store_class_init (PsppireVarStoreClass *class)
126 GObjectClass *object_class;
128 parent_class = g_type_class_peek_parent (class);
129 object_class = (GObjectClass*) class;
131 object_class->finalize = psppire_var_store_finalize;
136 psppire_var_store_init (PsppireVarStore *var_store)
138 GdkColormap *colormap = gdk_colormap_get_system ();
140 g_assert (gdk_color_parse ("gray", &var_store->disabled));
142 gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
148 psppire_var_store_item_editable (PsppireVarStore *var_store, gint row, gint column)
150 const struct fmt_spec *write_spec ;
152 struct variable *pv = psppire_var_store_get_var (var_store, row);
157 if ( VAR_STRING == var_get_type (pv) && column == COL_DECIMALS )
160 write_spec =var_get_write_format (pv);
162 switch ( write_spec->type )
177 if ( column == COL_DECIMALS || column == COL_WIDTH)
189 psppire_var_store_get_var (PsppireVarStore *store, gint row)
191 return psppire_dict_get_variable (store->dict, row);
195 psppire_var_store_is_editable (const GSheetModel *model, gint row, gint column)
197 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
198 return psppire_var_store_item_editable (store, row, column);
202 static const GdkColor *
203 psppire_var_store_get_foreground (const GSheetModel *model, gint row, gint column)
205 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
207 if ( ! psppire_var_store_item_editable (store, row, column) )
208 return &store->disabled;
214 const PangoFontDescription *
215 psppire_var_store_get_font_desc (const GSheetModel *model,
216 gint row, gint column)
218 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
220 return store->font_desc;
226 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
228 iface->get_row_count = psppire_var_store_get_row_count;
229 iface->free_strings = TRUE;
230 iface->get_string = psppire_var_store_get_string;
231 iface->set_string = psppire_var_store_set_string;
232 iface->clear_datum = psppire_var_store_clear;
233 iface->is_editable = psppire_var_store_is_editable;
234 iface->is_visible = NULL;
235 iface->get_foreground = psppire_var_store_get_foreground;
236 iface->get_background = NULL;
237 iface->get_font_desc = psppire_var_store_get_font_desc;
238 iface->get_cell_border = NULL;
244 * psppire_var_store_new:
245 * @dict: The dictionary for this var_store.
248 * Return value: a new #PsppireVarStore
251 psppire_var_store_new (PsppireDict *dict)
253 PsppireVarStore *retval;
255 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
257 psppire_var_store_set_dictionary (retval, dict);
263 var_change_callback (GtkWidget *w, gint n, gpointer data)
265 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 return psppire_dict_rename_var (var_store->dict, pv, text);
400 if ( ! text) return FALSE;
401 var_set_display_width (pv, atoi (text));
406 int width = atoi (text);
407 if ( ! text) return FALSE;
408 if ( var_is_alpha (pv))
409 var_set_width (pv, width);
412 struct fmt_spec fmt ;
413 fmt = *var_get_write_format (pv);
414 if ( width < fmt_min_output_width (fmt.type)
416 width > fmt_max_output_width (fmt.type))
420 fmt.d = MIN (fmt_max_output_decimals (fmt.type, width), fmt.d);
422 var_set_both_formats (pv, &fmt);
432 if ( ! text) return FALSE;
433 decimals = atoi (text);
434 fmt = *var_get_write_format (pv);
436 fmt_max_output_decimals (fmt.type,
442 var_set_both_formats (pv, &fmt);
447 var_set_label (pv, text);
455 /* These can be modified only by their respective dialog boxes */
459 g_assert_not_reached ();
468 text_for_column (const struct variable *pv, gint c, GError **err)
470 static gchar none[] = N_("None");
472 static const gchar *const type_label[] =
483 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
484 VT_CUSTOM, VT_STRING};
486 const struct fmt_spec *write_spec = var_get_write_format (pv);
491 return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
495 switch ( write_spec->type )
498 return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
501 return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
504 return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
507 return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
522 return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
525 return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
532 return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
535 return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
539 char str[FMT_STRING_LEN_MAX + 1];
540 g_warning ("Unknown format: \"%s\"\n",
541 fmt_to_string (write_spec, str));
550 GString *gstr = g_string_sized_new (10);
551 g_string_printf (gstr, _("%d"), write_spec->w);
552 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
553 g_string_free (gstr, TRUE);
560 GString *gstr = g_string_sized_new (10);
561 g_string_printf (gstr, _("%d"), write_spec->d);
562 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
563 g_string_free (gstr, TRUE);
570 GString *gstr = g_string_sized_new (10);
571 g_string_printf (gstr, _("%d"), var_get_display_width (pv));
572 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
573 g_string_free (gstr, TRUE);
578 return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
584 const struct missing_values *miss = var_get_missing_values (pv);
585 if ( mv_is_empty (miss))
586 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
589 if ( ! mv_has_range (miss))
591 GString *gstr = g_string_sized_new (10);
592 const int n = mv_n_values (miss);
593 gchar *mv[4] = {0,0,0,0};
595 for (i = 0 ; i < n; ++i )
598 mv_peek_value (miss, &v, i);
599 mv[i] = value_to_text (v, *write_spec);
601 g_string_append (gstr, ", ");
602 g_string_append (gstr, mv[i]);
605 s = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
606 g_string_free (gstr, TRUE);
610 GString *gstr = g_string_sized_new (10);
612 union value low, high;
613 mv_peek_range (miss, &low.f, &high.f);
615 l = value_to_text (low, *write_spec);
616 h = value_to_text (high, *write_spec);
618 g_string_printf (gstr, "%s - %s", l, h);
622 if ( mv_has_value (miss))
626 mv_peek_value (miss, &v, 0);
628 ss = value_to_text (v, *write_spec);
630 g_string_append (gstr, ", ");
631 g_string_append (gstr, ss);
634 s = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
635 g_string_free (gstr, TRUE);
644 if ( ! var_has_value_labels (pv))
645 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
649 GString *gstr = g_string_sized_new (10);
650 const struct val_labs *vls = var_get_value_labels (pv);
651 struct val_labs_iterator *ip = 0;
652 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
657 gchar *const vstr = value_to_text (vl->value, *write_spec);
659 g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
665 ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
666 g_string_free (gstr, TRUE);
673 const gint align = var_get_alignment (pv);
675 g_assert (align < n_ALIGNMENTS);
676 return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
681 const gint measure = var_get_measure (pv);
683 g_assert (measure < n_MEASURES);
684 return g_locale_to_utf8 (gettext (measures[measure]),
694 /* Return the number of variables */
696 psppire_var_store_get_var_cnt (PsppireVarStore *store)
698 return psppire_dict_get_var_cnt (store->dict);
703 psppire_var_store_set_font (PsppireVarStore *store, const PangoFontDescription *fd)
705 g_return_if_fail (store);
706 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
708 store->font_desc = fd;
710 g_sheet_model_range_changed (G_SHEET_MODEL (store), -1, -1, -1, -1);
715 psppire_var_store_get_row_count (const GSheetModel * model)
718 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
721 rows = psppire_dict_get_var_cnt (vs->dict);
726 /* Row related funcs */
729 geometry_get_row_count (const GSheetRow *geom, gpointer data)
732 PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
735 rows = psppire_dict_get_var_cnt (vs->dict);
737 return rows + TRAILING_ROWS;
742 geometry_get_height (const GSheetRow *geom, gint row, gpointer data)
749 geometry_is_sensitive (const GSheetRow *geom, gint row, gpointer data)
751 PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
756 return row < psppire_dict_get_var_cnt (vs->dict);
760 gboolean always_true ()
767 geometry_get_button_label (const GSheetRow *geom, gint unit, gpointer data)
769 gchar *label = g_strdup_printf (_("%d"), unit);
776 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
778 iface->get_row_count = geometry_get_row_count;
779 iface->get_height = geometry_get_height;
780 iface->set_height = 0;
781 iface->get_visibility = always_true;
782 iface->get_sensitivity = geometry_is_sensitive;
784 iface->get_button_label = geometry_get_button_label;