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 #include <gobject/gvaluecollector.h>
28 #include "gsheetmodel.h"
30 #include "psppire-variable.h"
31 #include "psppire-var-store.h"
32 #include "var-sheet.h"
34 #include "dictionary.h"
36 #include "missing-values.h"
38 #include "val-labs-dialog.h"
39 #include "missing-val-dialog.h"
40 #include "value-labels.h"
46 static void psppire_var_store_init (PsppireVarStore *var_store);
47 static void psppire_var_store_class_init (PsppireVarStoreClass *class);
48 static void psppire_var_store_sheet_model_init (GSheetModelIface *iface);
49 static void psppire_var_store_finalize (GObject *object);
51 static const gchar *const psppire_var_store_get_string(GSheetModel *sheet_model, gint row, gint column);
53 static gboolean psppire_var_store_clear(GSheetModel *model, gint row, gint col);
56 static gboolean psppire_var_store_set_string(GSheetModel *model,
57 const gchar *text, gint row, gint column);
60 static const gchar *const text_for_column(const struct PsppireVariable *pv, gint c);
63 static GObjectClass *parent_class = NULL;
66 psppire_var_store_get_type (void)
68 static GType var_store_type = 0;
72 static const GTypeInfo var_store_info =
74 sizeof (PsppireVarStoreClass),
76 NULL, /* base_finalize */
77 (GClassInitFunc) psppire_var_store_class_init,
78 NULL, /* class_finalize */
79 NULL, /* class_data */
80 sizeof (PsppireVarStore),
82 (GInstanceInitFunc) psppire_var_store_init,
85 static const GInterfaceInfo sheet_model_info =
87 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
92 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore",
95 g_type_add_interface_static (var_store_type,
100 return var_store_type;
104 psppire_var_store_class_init (PsppireVarStoreClass *class)
106 GObjectClass *object_class;
108 parent_class = g_type_class_peek_parent (class);
109 object_class = (GObjectClass*) class;
111 object_class->finalize = psppire_var_store_finalize;
116 psppire_var_store_init (PsppireVarStore *var_store)
119 g_assert(gdk_color_parse("gray", &var_store->disabled));
121 GdkColormap *colormap = gdk_colormap_get_system();
123 gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
129 psppire_var_store_item_editable(PsppireVarStore *var_store, gint row, gint column)
132 struct PsppireVariable *pv = psppire_var_store_get_variable(var_store, row);
137 if ( ALPHA == psppire_variable_get_type(pv) && column == COL_DECIMALS )
140 const struct fmt_spec *write_spec = psppire_variable_get_write_spec(pv);
142 switch ( write_spec->type )
157 if ( column == COL_DECIMALS || column == COL_WIDTH)
168 psppire_var_store_is_editable(GSheetModel *model, gint row, gint column)
170 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
171 return psppire_var_store_item_editable(store, row, column);
175 static const GdkColor *
176 psppire_var_store_get_foreground(GSheetModel *model, gint row, gint column)
178 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
180 if ( ! psppire_var_store_item_editable(store, row, column) )
181 return &store->disabled;
187 const PangoFontDescription *
188 psppire_var_store_get_font_desc(GSheetModel *model,
189 gint row, gint column)
191 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
193 return store->font_desc;
199 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
201 iface->get_string = psppire_var_store_get_string;
202 iface->set_string = psppire_var_store_set_string;
203 iface->clear_datum = psppire_var_store_clear;
204 iface->is_editable = psppire_var_store_is_editable;
205 iface->is_visible = NULL;
206 iface->get_foreground = psppire_var_store_get_foreground;
207 iface->get_background = NULL;
208 iface->get_font_desc = psppire_var_store_get_font_desc;
209 iface->get_cell_border = NULL;
215 * psppire_var_store_new:
216 * @dict: The dictionary for this var_store.
219 * Return value: a new #PsppireVarStore
222 psppire_var_store_new (PsppireDict *dict)
224 PsppireVarStore *retval;
226 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
228 psppire_var_store_set_dictionary(retval, dict);
234 var_change_callback(GtkWidget *w, gint n, gpointer data)
236 GSheetModel *model = G_SHEET_MODEL(data);
237 g_sheet_model_range_changed (model,
243 var_delete_callback(GtkWidget *w, gint first, gint n, gpointer data)
245 GSheetModel *model = G_SHEET_MODEL(data);
247 g_sheet_model_rows_deleted (model, first, n);
253 var_insert_callback(GtkWidget *w, gint row, gpointer data)
255 GSheetModel *model = G_SHEET_MODEL(data);
257 g_sheet_model_rows_inserted (model, row, 1);
263 * psppire_var_store_replace_set_dictionary:
264 * @var_store: The variable store
265 * @dict: The dictionary to set
267 * If a dictionary is already associated with the var-store, then it will be
271 psppire_var_store_set_dictionary(PsppireVarStore *var_store, PsppireDict *dict)
273 if ( var_store->dict ) g_object_unref(var_store->dict);
275 var_store->dict = dict;
277 g_signal_connect(dict, "variable-changed", G_CALLBACK(var_change_callback),
280 g_signal_connect(dict, "variables-deleted", G_CALLBACK(var_delete_callback),
283 g_signal_connect(dict, "variable-inserted", G_CALLBACK(var_insert_callback),
287 /* The entire model has changed */
288 g_sheet_model_range_changed (G_SHEET_MODEL(var_store), -1, -1, -1, -1);
292 psppire_var_store_finalize (GObject *object)
295 (* parent_class->finalize) (object);
298 static const gchar *const
299 psppire_var_store_get_string(GSheetModel *model, gint row, gint column)
301 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
303 struct PsppireVariable *pv;
305 if ( row >= psppire_dict_get_var_cnt(store->dict))
308 pv = psppire_dict_get_variable (store->dict, row);
310 const gchar *s = text_for_column(pv, column);
316 struct PsppireVariable *
317 psppire_var_store_get_variable(PsppireVarStore *store, gint row)
319 g_return_val_if_fail(store, NULL);
320 g_return_val_if_fail(store->dict, NULL);
322 if ( row >= psppire_dict_get_var_cnt(store->dict))
325 return psppire_dict_get_variable (store->dict, row);
328 /* Clears that part of the variable store, if possible, which corresponds
330 Returns true if anything was updated, false otherwise.
333 psppire_var_store_clear(GSheetModel *model, gint row, gint col)
335 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
337 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
340 struct PsppireVariable *pv = psppire_var_store_get_variable(var_store, row);
348 psppire_variable_set_label(pv, 0);
356 /* Attempts to update that part of the variable store which corresponds
357 to ROW, COL with the value TEXT.
358 Returns true if anything was updated, false otherwise.
361 psppire_var_store_set_string(GSheetModel *model,
362 const gchar *text, gint row, gint col)
364 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
366 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
369 struct PsppireVariable *pv = psppire_var_store_get_variable(var_store, row);
377 return psppire_variable_set_name(pv, text);
380 if ( ! text) return FALSE;
381 return psppire_variable_set_columns(pv, atoi(text));
384 if ( ! text) return FALSE;
385 return psppire_variable_set_width(pv, atoi(text));
388 if ( ! text) return FALSE;
389 return psppire_variable_set_decimals(pv, atoi(text));
392 psppire_variable_set_label(pv, text);
400 /* These can be modified only by their respective dialog boxes */
404 g_assert_not_reached();
412 #define MAX_CELL_TEXT_LEN 255
414 static const gchar *const
415 text_for_column(const struct PsppireVariable *pv, gint c)
417 static gchar buf[MAX_CELL_TEXT_LEN];
419 static gchar none[]=_("None");
421 static const gchar *const type_label[] =
432 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
433 VT_CUSTOM, VT_STRING};
435 const struct fmt_spec *write_spec = psppire_variable_get_write_spec(pv);
440 return psppire_variable_get_name(pv);
444 switch ( write_spec->type )
447 return type_label[VT_NUMERIC];
450 return type_label[VT_COMMA];
453 return type_label[VT_DOT];
456 return type_label[VT_SCIENTIFIC];
471 return type_label[VT_DATE];
474 return type_label[VT_DOLLAR];
481 return type_label[VT_CUSTOM];
484 return type_label[VT_STRING];
487 g_warning("Unknown format: \"%s\"\n",
488 fmt_to_string(write_spec));
495 g_snprintf(buf, MAX_CELL_TEXT_LEN, "%d", write_spec->w);
501 g_snprintf(buf, MAX_CELL_TEXT_LEN, "%d", write_spec->d);
507 g_snprintf(buf, MAX_CELL_TEXT_LEN,
508 "%d", psppire_variable_get_columns(pv));
513 return psppire_variable_get_label(pv);
517 const struct missing_values *miss = psppire_variable_get_missing(pv);
518 if ( mv_is_empty(miss))
522 if ( ! mv_has_range (miss))
524 const int n = mv_n_values(miss);
525 gchar *mv[4] = {0,0,0,0};
527 for(i = 0 ; i < n; ++i )
530 mv_peek_value(miss, &v, i);
531 mv[i] = value_to_text(v, *write_spec);
534 for(i = 0 ; i < n; ++i )
537 g_strlcat(buf, ", ", MAX_CELL_TEXT_LEN);
538 g_strlcat(buf, mv[i], MAX_CELL_TEXT_LEN);
545 union value low, high;
546 mv_peek_range(miss, &low.f, &high.f);
548 l = value_to_text(low, *write_spec);
549 h = value_to_text(high, *write_spec);
551 g_snprintf(buf, MAX_CELL_TEXT_LEN, "%s - %s", l, h);
555 if ( mv_has_value(miss))
557 gchar buf2[MAX_CELL_TEXT_LEN];
560 mv_peek_value(miss, &v, 0);
562 s = value_to_text(v, *write_spec);
564 g_snprintf(buf2, MAX_CELL_TEXT_LEN, "%s, %s", buf, s);
576 const struct val_labs *vls = psppire_variable_get_value_labels(pv);
577 if ( ! vls || 0 == val_labs_count(vls))
581 struct val_labs_iterator *ip=0;
582 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
586 gchar *const vstr = value_to_text(vl->value, *write_spec);
588 g_snprintf(buf, MAX_CELL_TEXT_LEN, "{%s,\"%s\"}_", vstr, vl->label);
599 return alignments[psppire_variable_get_alignment(pv)];
602 return measures[psppire_variable_get_measure(pv)];
610 /* Return the number of variables */
612 psppire_var_store_get_var_cnt(PsppireVarStore *store)
614 return psppire_dict_get_var_cnt(store->dict);
619 psppire_var_store_set_font(PsppireVarStore *store, PangoFontDescription *fd)
621 g_return_if_fail (store);
622 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
624 store->font_desc = fd;
626 g_sheet_model_range_changed (G_SHEET_MODEL(store), -1, -1, -1, -1);