3 PSPPIRE --- A Graphical User Interface for PSPP
4 Copyright (C) 2006 Free Software Foundation
5 Written by John Darrington
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
31 #include <gobject/gvaluecollector.h>
33 #include <gtksheet/gsheetmodel.h>
35 #include "psppire-variable.h"
36 #include "psppire-var-store.h"
37 #include "var-sheet.h"
40 #include <data/dictionary.h>
41 #include <data/variable.h>
42 #include <data/missing-values.h>
44 #include "val-labs-dialog.h"
45 #include "missing-val-dialog.h"
46 #include <data/value-labels.h>
49 #define TRAILING_ROWS 40
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);
56 static gchar *psppire_var_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
58 static gboolean psppire_var_store_clear(GSheetModel *model, gint row, gint col);
61 static gboolean psppire_var_store_set_string(GSheetModel *model,
62 const gchar *text, gint row, gint column);
64 static gint psppire_var_store_get_row_count(const GSheetModel * model);
66 static gchar *text_for_column(const struct PsppireVariable *pv, gint c, GError **err);
69 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
73 static GObjectClass *parent_class = NULL;
76 psppire_var_store_get_type (void)
78 static GType var_store_type = 0;
82 static const GTypeInfo var_store_info =
84 sizeof (PsppireVarStoreClass),
86 NULL, /* base_finalize */
87 (GClassInitFunc) psppire_var_store_class_init,
88 NULL, /* class_finalize */
89 NULL, /* class_data */
90 sizeof (PsppireVarStore),
92 (GInstanceInitFunc) psppire_var_store_init,
95 static const GInterfaceInfo sheet_model_info =
97 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
102 static const GInterfaceInfo sheet_row_info =
104 (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
109 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
111 g_type_add_interface_static (var_store_type,
115 g_type_add_interface_static (var_store_type,
122 return var_store_type;
126 psppire_var_store_class_init (PsppireVarStoreClass *class)
128 GObjectClass *object_class;
130 parent_class = g_type_class_peek_parent (class);
131 object_class = (GObjectClass*) class;
133 object_class->finalize = psppire_var_store_finalize;
138 psppire_var_store_init (PsppireVarStore *var_store)
140 GdkColormap *colormap = gdk_colormap_get_system();
142 g_assert(gdk_color_parse("gray", &var_store->disabled));
144 gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
150 psppire_var_store_item_editable(PsppireVarStore *var_store, gint row, gint column)
152 const struct fmt_spec *write_spec ;
154 struct PsppireVariable *pv = psppire_var_store_get_variable(var_store, row);
159 if ( ALPHA == psppire_variable_get_type(pv) && column == COL_DECIMALS )
162 write_spec = psppire_variable_get_write_spec(pv);
164 switch ( write_spec->type )
179 if ( column == COL_DECIMALS || column == COL_WIDTH)
190 psppire_var_store_is_editable(const GSheetModel *model, gint row, gint column)
192 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
193 return psppire_var_store_item_editable(store, row, column);
197 static const GdkColor *
198 psppire_var_store_get_foreground(const GSheetModel *model, gint row, gint column)
200 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
202 if ( ! psppire_var_store_item_editable(store, row, column) )
203 return &store->disabled;
209 const PangoFontDescription *
210 psppire_var_store_get_font_desc(const GSheetModel *model,
211 gint row, gint column)
213 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
215 return store->font_desc;
221 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
223 iface->get_row_count = psppire_var_store_get_row_count;
224 iface->free_strings = TRUE;
225 iface->get_string = psppire_var_store_get_string;
226 iface->set_string = psppire_var_store_set_string;
227 iface->clear_datum = psppire_var_store_clear;
228 iface->is_editable = psppire_var_store_is_editable;
229 iface->is_visible = NULL;
230 iface->get_foreground = psppire_var_store_get_foreground;
231 iface->get_background = NULL;
232 iface->get_font_desc = psppire_var_store_get_font_desc;
233 iface->get_cell_border = NULL;
239 * psppire_var_store_new:
240 * @dict: The dictionary for this var_store.
243 * Return value: a new #PsppireVarStore
246 psppire_var_store_new (PsppireDict *dict)
248 PsppireVarStore *retval;
250 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
252 psppire_var_store_set_dictionary(retval, dict);
258 var_change_callback(GtkWidget *w, gint n, gpointer data)
260 GSheetModel *model = G_SHEET_MODEL(data);
261 g_sheet_model_range_changed (model,
267 var_delete_callback(GtkWidget *w, gint first, gint n, gpointer data)
269 GSheetModel *model = G_SHEET_MODEL(data);
271 g_sheet_model_rows_deleted (model, first, n);
277 var_insert_callback(GtkWidget *w, gint row, gpointer data)
279 GSheetModel *model = G_SHEET_MODEL(data);
281 g_sheet_model_rows_inserted (model, row, 1);
287 * psppire_var_store_replace_set_dictionary:
288 * @var_store: The variable store
289 * @dict: The dictionary to set
291 * If a dictionary is already associated with the var-store, then it will be
295 psppire_var_store_set_dictionary(PsppireVarStore *var_store, PsppireDict *dict)
297 if ( var_store->dict ) g_object_unref(var_store->dict);
299 var_store->dict = dict;
301 g_signal_connect(dict, "variable-changed", G_CALLBACK(var_change_callback),
304 g_signal_connect(dict, "variables-deleted", G_CALLBACK(var_delete_callback),
307 g_signal_connect(dict, "variable-inserted", G_CALLBACK(var_insert_callback),
311 /* The entire model has changed */
312 g_sheet_model_range_changed (G_SHEET_MODEL(var_store), -1, -1, -1, -1);
316 psppire_var_store_finalize (GObject *object)
319 (* parent_class->finalize) (object);
323 psppire_var_store_get_string(const GSheetModel *model, gint row, gint column)
325 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
327 struct PsppireVariable *pv;
329 if ( row >= psppire_dict_get_var_cnt(store->dict))
332 pv = psppire_dict_get_variable (store->dict, row);
334 return text_for_column(pv, column, 0);
338 struct PsppireVariable *
339 psppire_var_store_get_variable(PsppireVarStore *store, gint row)
341 g_return_val_if_fail(store, NULL);
342 g_return_val_if_fail(store->dict, NULL);
344 if ( row >= psppire_dict_get_var_cnt(store->dict))
347 return psppire_dict_get_variable (store->dict, row);
350 /* Clears that part of the variable store, if possible, which corresponds
352 Returns true if anything was updated, false otherwise.
355 psppire_var_store_clear(GSheetModel *model, gint row, gint col)
357 struct PsppireVariable *pv ;
359 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
361 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
364 pv = psppire_var_store_get_variable(var_store, row);
372 psppire_variable_set_label(pv, 0);
380 /* Attempts to update that part of the variable store which corresponds
381 to ROW, COL with the value TEXT.
382 Returns true if anything was updated, false otherwise.
385 psppire_var_store_set_string(GSheetModel *model,
386 const gchar *text, gint row, gint col)
388 struct PsppireVariable *pv ;
390 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
392 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
395 pv = psppire_var_store_get_variable(var_store, row);
402 return psppire_variable_set_name(pv, text);
405 if ( ! text) return FALSE;
406 return psppire_variable_set_columns(pv, atoi(text));
409 if ( ! text) return FALSE;
410 return psppire_variable_set_width(pv, atoi(text));
413 if ( ! text) return FALSE;
414 return psppire_variable_set_decimals(pv, atoi(text));
417 psppire_variable_set_label(pv, text);
425 /* These can be modified only by their respective dialog boxes */
429 g_assert_not_reached();
438 text_for_column(const struct PsppireVariable *pv, gint c, GError **err)
440 static gchar none[] = N_("None");
442 static const gchar *const type_label[] =
453 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
454 VT_CUSTOM, VT_STRING};
456 const struct fmt_spec *write_spec = psppire_variable_get_write_spec(pv);
461 return pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, err);
465 switch ( write_spec->type )
468 return g_locale_to_utf8(gettext(type_label[VT_NUMERIC]), -1, 0, 0, err);
471 return g_locale_to_utf8(gettext(type_label[VT_COMMA]), -1, 0, 0, err);
474 return g_locale_to_utf8(gettext(type_label[VT_DOT]), -1, 0, 0, err);
477 return g_locale_to_utf8(gettext(type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
492 return g_locale_to_utf8(type_label[VT_DATE], -1, 0, 0, err);
495 return g_locale_to_utf8(type_label[VT_DOLLAR], -1, 0, 0, err);
502 return g_locale_to_utf8(gettext(type_label[VT_CUSTOM]), -1, 0, 0, err);
505 return g_locale_to_utf8(gettext(type_label[VT_STRING]), -1, 0, 0, err);
509 char str[FMT_STRING_LEN_MAX + 1];
510 g_warning("Unknown format: \"%s\"\n",
511 fmt_to_string(write_spec, str));
520 GString *gstr = g_string_sized_new(10);
521 g_string_printf(gstr, _("%d"), write_spec->w);
522 s = g_locale_to_utf8(gstr->str, gstr->len, 0, 0, err);
523 g_string_free(gstr, TRUE);
530 GString *gstr = g_string_sized_new(10);
531 g_string_printf(gstr, _("%d"), write_spec->d);
532 s = g_locale_to_utf8(gstr->str, gstr->len, 0, 0, err);
533 g_string_free(gstr, TRUE);
540 GString *gstr = g_string_sized_new(10);
541 g_string_printf(gstr, _("%d"), psppire_variable_get_columns(pv));
542 s = g_locale_to_utf8(gstr->str, gstr->len, 0, 0, err);
543 g_string_free(gstr, TRUE);
548 return pspp_locale_to_utf8(psppire_variable_get_label(pv), -1, err);
554 const struct missing_values *miss = psppire_variable_get_missing(pv);
555 if ( mv_is_empty(miss))
556 return g_locale_to_utf8(gettext(none), -1, 0, 0, err);
559 if ( ! mv_has_range (miss))
561 GString *gstr = g_string_sized_new(10);
562 const int n = mv_n_values(miss);
563 gchar *mv[4] = {0,0,0,0};
565 for(i = 0 ; i < n; ++i )
568 mv_peek_value(miss, &v, i);
569 mv[i] = value_to_text(v, *write_spec);
571 g_string_append(gstr, ", ");
572 g_string_append(gstr, mv[i]);
575 s = pspp_locale_to_utf8(gstr->str, gstr->len, err);
576 g_string_free(gstr, TRUE);
580 GString *gstr = g_string_sized_new(10);
582 union value low, high;
583 mv_peek_range(miss, &low.f, &high.f);
585 l = value_to_text(low, *write_spec);
586 h = value_to_text(high, *write_spec);
588 g_string_printf(gstr, "%s - %s", l, h);
592 if ( mv_has_value(miss))
596 mv_peek_value(miss, &v, 0);
598 ss = value_to_text(v, *write_spec);
600 g_string_append(gstr, ", ");
601 g_string_append(gstr, ss);
604 s = pspp_locale_to_utf8(gstr->str, gstr->len, err);
605 g_string_free(gstr, TRUE);
614 const struct val_labs *vls = psppire_variable_get_value_labels(pv);
615 if ( ! vls || 0 == val_labs_count(vls) )
616 return g_locale_to_utf8(gettext(none), -1, 0, 0, err);
620 GString *gstr = g_string_sized_new(10);
621 struct val_labs_iterator *ip = 0;
622 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
627 gchar *const vstr = value_to_text(vl->value, *write_spec);
629 g_string_printf(gstr, "{%s,\"%s\"}_", vstr, vl->label);
635 ss = pspp_locale_to_utf8(gstr->str, gstr->len, err);
636 g_string_free(gstr, TRUE);
643 const gint align = psppire_variable_get_alignment(pv);
645 g_assert(align < n_ALIGNMENTS);
646 return g_locale_to_utf8(gettext(alignments[align]), -1, 0, 0, err);
651 const gint measure = psppire_variable_get_measure(pv);
653 g_assert(measure < n_MEASURES);
654 return g_locale_to_utf8(gettext(measures[measure]), -1, 0, 0, err);
663 /* Return the number of variables */
665 psppire_var_store_get_var_cnt(PsppireVarStore *store)
667 return psppire_dict_get_var_cnt(store->dict);
672 psppire_var_store_set_font(PsppireVarStore *store, const PangoFontDescription *fd)
674 g_return_if_fail (store);
675 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
677 store->font_desc = fd;
679 g_sheet_model_range_changed (G_SHEET_MODEL(store), -1, -1, -1, -1);
684 psppire_var_store_get_row_count(const GSheetModel * model)
687 PsppireVarStore *vs = PSPPIRE_VAR_STORE(model);
690 rows = psppire_dict_get_var_cnt(vs->dict);
695 /* Row related funcs */
698 geometry_get_row_count(const GSheetRow *geom, gpointer data)
701 PsppireVarStore *vs = PSPPIRE_VAR_STORE(geom);
704 rows = psppire_dict_get_var_cnt(vs->dict);
706 return rows + TRAILING_ROWS;
711 geometry_get_height(const GSheetRow *geom, gint row, gpointer data)
718 geometry_is_sensitive(const GSheetRow *geom, gint row, gpointer data)
720 PsppireVarStore *vs = PSPPIRE_VAR_STORE(geom);
725 return row < psppire_dict_get_var_cnt(vs->dict);
729 gboolean always_true()
736 geometry_get_button_label(const GSheetRow *geom, gint unit, gpointer data)
738 gchar *label = g_strdup_printf(_("%d"), unit);
745 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
747 iface->get_row_count = geometry_get_row_count;
748 iface->get_height = geometry_get_height;
749 iface->set_height = 0;
750 iface->get_visibility = always_true;
751 iface->get_sensitivity = geometry_is_sensitive;
753 iface->get_button_label = geometry_get_button_label;