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>
46 #include "var-display.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);
56 gchar * missing_values_to_string (const struct variable *pv, GError **err);
59 static gchar *psppire_var_store_get_string (const GSheetModel *sheet_model, gint row, gint column);
61 static gboolean psppire_var_store_clear (GSheetModel *model, gint row, gint col);
64 static gboolean psppire_var_store_set_string (GSheetModel *model,
65 const gchar *text, gint row, gint column);
67 static gint psppire_var_store_get_row_count (const GSheetModel * model);
69 static gchar *text_for_column (const struct variable *pv, gint c, GError **err);
72 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
76 static GObjectClass *parent_class = NULL;
79 psppire_var_store_get_type (void)
81 static GType var_store_type = 0;
85 static const GTypeInfo var_store_info =
87 sizeof (PsppireVarStoreClass),
89 NULL, /* base_finalize */
90 (GClassInitFunc) psppire_var_store_class_init,
91 NULL, /* class_finalize */
92 NULL, /* class_data */
93 sizeof (PsppireVarStore),
95 (GInstanceInitFunc) psppire_var_store_init,
98 static const GInterfaceInfo sheet_model_info =
100 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
105 static const GInterfaceInfo sheet_row_info =
107 (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
112 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
114 g_type_add_interface_static (var_store_type,
118 g_type_add_interface_static (var_store_type,
125 return var_store_type;
129 psppire_var_store_class_init (PsppireVarStoreClass *class)
131 GObjectClass *object_class;
133 parent_class = g_type_class_peek_parent (class);
134 object_class = (GObjectClass*) class;
136 object_class->finalize = psppire_var_store_finalize;
141 psppire_var_store_init (PsppireVarStore *var_store)
143 GdkColormap *colormap = gdk_colormap_get_system ();
145 g_assert (gdk_color_parse ("gray", &var_store->disabled));
147 gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
153 psppire_var_store_item_editable (PsppireVarStore *var_store, gint row, gint column)
155 const struct fmt_spec *write_spec ;
157 struct variable *pv = psppire_var_store_get_var (var_store, row);
162 if ( var_is_alpha (pv) && column == COL_DECIMALS )
165 write_spec = var_get_print_format (pv);
167 switch ( write_spec->type )
182 if ( column == COL_DECIMALS || column == COL_WIDTH)
194 psppire_var_store_get_var (PsppireVarStore *store, gint row)
196 return psppire_dict_get_variable (store->dict, row);
200 psppire_var_store_is_editable (const GSheetModel *model, gint row, gint column)
202 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
203 return psppire_var_store_item_editable (store, row, column);
207 static const GdkColor *
208 psppire_var_store_get_foreground (const GSheetModel *model, gint row, gint column)
210 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
212 if ( ! psppire_var_store_item_editable (store, row, column) )
213 return &store->disabled;
219 const PangoFontDescription *
220 psppire_var_store_get_font_desc (const GSheetModel *model,
221 gint row, gint column)
223 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
225 return store->font_desc;
231 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
233 iface->get_row_count = psppire_var_store_get_row_count;
234 iface->free_strings = TRUE;
235 iface->get_string = psppire_var_store_get_string;
236 iface->set_string = psppire_var_store_set_string;
237 iface->clear_datum = psppire_var_store_clear;
238 iface->is_editable = psppire_var_store_is_editable;
239 iface->is_visible = NULL;
240 iface->get_foreground = psppire_var_store_get_foreground;
241 iface->get_background = NULL;
242 iface->get_font_desc = psppire_var_store_get_font_desc;
243 iface->get_cell_border = NULL;
249 * psppire_var_store_new:
250 * @dict: The dictionary for this var_store.
253 * Return value: a new #PsppireVarStore
256 psppire_var_store_new (PsppireDict *dict)
258 PsppireVarStore *retval;
260 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
262 psppire_var_store_set_dictionary (retval, dict);
268 var_change_callback (GtkWidget *w, gint n, gpointer data)
270 GSheetModel *model = G_SHEET_MODEL (data);
272 g_sheet_model_range_changed (model,
278 var_delete_callback (GtkWidget *w, gint first, gint n, gpointer data)
280 GSheetModel *model = G_SHEET_MODEL (data);
282 g_sheet_model_rows_deleted (model, first, n);
288 var_insert_callback (GtkWidget *w, gint row, gpointer data)
290 GSheetModel *model = G_SHEET_MODEL (data);
292 g_sheet_model_rows_inserted (model, row, 1);
298 * psppire_var_store_replace_set_dictionary:
299 * @var_store: The variable store
300 * @dict: The dictionary to set
302 * If a dictionary is already associated with the var-store, then it will be
306 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
308 if ( var_store->dict ) g_object_unref (var_store->dict);
310 var_store->dict = dict;
312 g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
315 g_signal_connect (dict, "variables-deleted", G_CALLBACK (var_delete_callback),
318 g_signal_connect (dict, "variable-inserted", G_CALLBACK (var_insert_callback),
322 /* The entire model has changed */
323 g_sheet_model_range_changed (G_SHEET_MODEL (var_store), -1, -1, -1, -1);
327 psppire_var_store_finalize (GObject *object)
330 (* parent_class->finalize) (object);
334 psppire_var_store_get_string (const GSheetModel *model, gint row, gint column)
336 PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
340 if ( row >= psppire_dict_get_var_cnt (store->dict))
343 pv = psppire_dict_get_variable (store->dict, row);
345 return text_for_column (pv, column, 0);
349 /* Clears that part of the variable store, if possible, which corresponds
351 Returns true if anything was updated, false otherwise.
354 psppire_var_store_clear (GSheetModel *model, gint row, gint col)
356 struct variable *pv ;
358 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
360 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
363 pv = psppire_var_store_get_var (var_store, row);
371 var_set_label (pv, 0);
379 /* Attempts to update that part of the variable store which corresponds
380 to ROW, COL with the value TEXT.
381 Returns true if anything was updated, false otherwise.
384 psppire_var_store_set_string (GSheetModel *model,
385 const gchar *text, gint row, gint col)
387 struct variable *pv ;
389 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
391 if ( row >= psppire_dict_get_var_cnt (var_store->dict))
394 pv = psppire_var_store_get_var (var_store, row);
402 return psppire_dict_rename_var (var_store->dict, pv, text);
405 if ( ! text) return FALSE;
406 var_set_display_width (pv, atoi (text));
411 int width = atoi (text);
412 if ( ! text) return FALSE;
413 if ( var_is_alpha (pv))
414 var_set_width (pv, width);
417 struct fmt_spec fmt ;
418 fmt = *var_get_write_format (pv);
419 if ( width < fmt_min_output_width (fmt.type)
421 width > fmt_max_output_width (fmt.type))
425 fmt.d = MIN (fmt_max_output_decimals (fmt.type, width), fmt.d);
427 var_set_both_formats (pv, &fmt);
437 if ( ! text) return FALSE;
438 decimals = atoi (text);
439 fmt = *var_get_write_format (pv);
441 fmt_max_output_decimals (fmt.type,
447 var_set_both_formats (pv, &fmt);
452 var_set_label (pv, text);
460 /* These can be modified only by their respective dialog boxes */
464 g_assert_not_reached ();
472 const static gchar none[] = N_("None");
475 text_for_column (const struct variable *pv, gint c, GError **err)
477 static const gchar *const type_label[] =
488 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
489 VT_CUSTOM, VT_STRING};
491 const struct fmt_spec *write_spec = var_get_write_format (pv);
496 return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
500 switch ( write_spec->type )
503 return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
506 return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
509 return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
512 return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
527 return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
530 return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
537 return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
540 return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
544 char str[FMT_STRING_LEN_MAX + 1];
545 g_warning ("Unknown format: \"%s\"\n",
546 fmt_to_string (write_spec, str));
555 GString *gstr = g_string_sized_new (10);
556 g_string_printf (gstr, _("%d"), write_spec->w);
557 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
558 g_string_free (gstr, TRUE);
565 GString *gstr = g_string_sized_new (10);
566 g_string_printf (gstr, _("%d"), write_spec->d);
567 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
568 g_string_free (gstr, TRUE);
575 GString *gstr = g_string_sized_new (10);
576 g_string_printf (gstr, _("%d"), var_get_display_width (pv));
577 s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
578 g_string_free (gstr, TRUE);
583 return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
588 return missing_values_to_string (pv, err);
593 if ( ! var_has_value_labels (pv))
594 return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
598 GString *gstr = g_string_sized_new (10);
599 const struct val_labs *vls = var_get_value_labels (pv);
600 struct val_labs_iterator *ip = 0;
601 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
606 gchar *const vstr = value_to_text (vl->value, *write_spec);
608 g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
614 ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
615 g_string_free (gstr, TRUE);
622 const gint align = var_get_alignment (pv);
624 g_assert (align < n_ALIGNMENTS);
625 return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
630 return measure_to_string (pv, err);
639 /* Return the number of variables */
641 psppire_var_store_get_var_cnt (PsppireVarStore *store)
643 return psppire_dict_get_var_cnt (store->dict);
648 psppire_var_store_set_font (PsppireVarStore *store, const PangoFontDescription *fd)
650 g_return_if_fail (store);
651 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
653 store->font_desc = fd;
655 g_sheet_model_range_changed (G_SHEET_MODEL (store), -1, -1, -1, -1);
660 psppire_var_store_get_row_count (const GSheetModel * model)
663 PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
666 rows = psppire_dict_get_var_cnt (vs->dict);
671 /* Row related funcs */
674 geometry_get_row_count (const GSheetRow *geom, gpointer data)
677 PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
680 rows = psppire_dict_get_var_cnt (vs->dict);
682 return rows + TRAILING_ROWS;
687 geometry_get_height (const GSheetRow *geom, gint row, gpointer data)
694 geometry_is_sensitive (const GSheetRow *geom, gint row, gpointer data)
696 PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
701 return row < psppire_dict_get_var_cnt (vs->dict);
705 gboolean always_true ()
712 geometry_get_button_label (const GSheetRow *geom, gint unit, gpointer data)
714 gchar *label = g_strdup_printf (_("%d"), unit);
721 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
723 iface->get_row_count = geometry_get_row_count;
724 iface->get_height = geometry_get_height;
725 iface->set_height = 0;
726 iface->get_visibility = always_true;
727 iface->get_sensitivity = geometry_is_sensitive;
729 iface->get_button_label = geometry_get_button_label;