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 <gtksheet/gsheetmodel.h>
30 #include "psppire-variable.h"
31 #include "psppire-var-store.h"
32 #include "var-sheet.h"
35 #include <data/dictionary.h>
36 #include <data/variable.h>
37 #include <data/missing-values.h>
39 #include "val-labs-dialog.h"
40 #include "missing-val-dialog.h"
41 #include <data/value-labels.h>
47 static void psppire_var_store_init (PsppireVarStore *var_store);
48 static void psppire_var_store_class_init (PsppireVarStoreClass *class);
49 static void psppire_var_store_sheet_model_init (GSheetModelIface *iface);
50 static void psppire_var_store_finalize (GObject *object);
52 static const gchar *psppire_var_store_get_string(GSheetModel *sheet_model, gint row, gint column);
54 static gboolean psppire_var_store_clear(GSheetModel *model, gint row, gint col);
57 static gboolean psppire_var_store_set_string(GSheetModel *model,
58 const gchar *text, gint row, gint column);
61 static const gchar *text_for_column(const struct PsppireVariable *pv, gint c);
64 static GObjectClass *parent_class = NULL;
67 psppire_var_store_get_type (void)
69 static GType var_store_type = 0;
73 static const GTypeInfo var_store_info =
75 sizeof (PsppireVarStoreClass),
77 NULL, /* base_finalize */
78 (GClassInitFunc) psppire_var_store_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (PsppireVarStore),
83 (GInstanceInitFunc) psppire_var_store_init,
86 static const GInterfaceInfo sheet_model_info =
88 (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
93 var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore",
96 g_type_add_interface_static (var_store_type,
101 return var_store_type;
105 psppire_var_store_class_init (PsppireVarStoreClass *class)
107 GObjectClass *object_class;
109 parent_class = g_type_class_peek_parent (class);
110 object_class = (GObjectClass*) class;
112 object_class->finalize = psppire_var_store_finalize;
117 psppire_var_store_init (PsppireVarStore *var_store)
119 GdkColormap *colormap = gdk_colormap_get_system();
121 g_assert(gdk_color_parse("gray", &var_store->disabled));
123 gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
129 psppire_var_store_item_editable(PsppireVarStore *var_store, gint row, gint column)
131 const struct fmt_spec *write_spec ;
133 struct PsppireVariable *pv = psppire_var_store_get_variable(var_store, row);
138 if ( ALPHA == psppire_variable_get_type(pv) && column == COL_DECIMALS )
141 write_spec = psppire_variable_get_write_spec(pv);
143 switch ( write_spec->type )
158 if ( column == COL_DECIMALS || column == COL_WIDTH)
169 psppire_var_store_is_editable(GSheetModel *model, gint row, gint column)
171 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
172 return psppire_var_store_item_editable(store, row, column);
176 static const GdkColor *
177 psppire_var_store_get_foreground(GSheetModel *model, gint row, gint column)
179 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
181 if ( ! psppire_var_store_item_editable(store, row, column) )
182 return &store->disabled;
188 const PangoFontDescription *
189 psppire_var_store_get_font_desc(GSheetModel *model,
190 gint row, gint column)
192 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
194 return store->font_desc;
200 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
202 iface->free_strings = TRUE;
203 iface->get_string = psppire_var_store_get_string;
204 iface->set_string = psppire_var_store_set_string;
205 iface->clear_datum = psppire_var_store_clear;
206 iface->is_editable = psppire_var_store_is_editable;
207 iface->is_visible = NULL;
208 iface->get_foreground = psppire_var_store_get_foreground;
209 iface->get_background = NULL;
210 iface->get_font_desc = psppire_var_store_get_font_desc;
211 iface->get_cell_border = NULL;
217 * psppire_var_store_new:
218 * @dict: The dictionary for this var_store.
221 * Return value: a new #PsppireVarStore
224 psppire_var_store_new (PsppireDict *dict)
226 PsppireVarStore *retval;
228 retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
230 psppire_var_store_set_dictionary(retval, dict);
236 var_change_callback(GtkWidget *w, gint n, gpointer data)
238 GSheetModel *model = G_SHEET_MODEL(data);
239 g_sheet_model_range_changed (model,
245 var_delete_callback(GtkWidget *w, gint first, gint n, gpointer data)
247 GSheetModel *model = G_SHEET_MODEL(data);
249 g_sheet_model_rows_deleted (model, first, n);
255 var_insert_callback(GtkWidget *w, gint row, gpointer data)
257 GSheetModel *model = G_SHEET_MODEL(data);
259 g_sheet_model_rows_inserted (model, row, 1);
265 * psppire_var_store_replace_set_dictionary:
266 * @var_store: The variable store
267 * @dict: The dictionary to set
269 * If a dictionary is already associated with the var-store, then it will be
273 psppire_var_store_set_dictionary(PsppireVarStore *var_store, PsppireDict *dict)
275 if ( var_store->dict ) g_object_unref(var_store->dict);
277 var_store->dict = dict;
279 g_signal_connect(dict, "variable-changed", G_CALLBACK(var_change_callback),
282 g_signal_connect(dict, "variables-deleted", G_CALLBACK(var_delete_callback),
285 g_signal_connect(dict, "variable-inserted", G_CALLBACK(var_insert_callback),
289 /* The entire model has changed */
290 g_sheet_model_range_changed (G_SHEET_MODEL(var_store), -1, -1, -1, -1);
294 psppire_var_store_finalize (GObject *object)
297 (* parent_class->finalize) (object);
301 psppire_var_store_get_string(GSheetModel *model, gint row, gint column)
305 PsppireVarStore *store = PSPPIRE_VAR_STORE(model);
307 struct PsppireVariable *pv;
309 if ( row >= psppire_dict_get_var_cnt(store->dict))
312 pv = psppire_dict_get_variable (store->dict, row);
314 s = text_for_column(pv, column);
316 return pspp_locale_to_utf8(s, -1, 0);
320 struct PsppireVariable *
321 psppire_var_store_get_variable(PsppireVarStore *store, gint row)
323 g_return_val_if_fail(store, NULL);
324 g_return_val_if_fail(store->dict, NULL);
326 if ( row >= psppire_dict_get_var_cnt(store->dict))
329 return psppire_dict_get_variable (store->dict, row);
332 /* Clears that part of the variable store, if possible, which corresponds
334 Returns true if anything was updated, false otherwise.
337 psppire_var_store_clear(GSheetModel *model, gint row, gint col)
339 struct PsppireVariable *pv ;
341 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
343 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
346 pv = psppire_var_store_get_variable(var_store, row);
354 psppire_variable_set_label(pv, 0);
362 /* Attempts to update that part of the variable store which corresponds
363 to ROW, COL with the value TEXT.
364 Returns true if anything was updated, false otherwise.
367 psppire_var_store_set_string(GSheetModel *model,
368 const gchar *text, gint row, gint col)
370 struct PsppireVariable *pv ;
372 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(model);
374 if ( row >= psppire_dict_get_var_cnt(var_store->dict))
377 pv = psppire_var_store_get_variable(var_store, row);
384 return psppire_variable_set_name(pv, text);
387 if ( ! text) return FALSE;
388 return psppire_variable_set_columns(pv, atoi(text));
391 if ( ! text) return FALSE;
392 return psppire_variable_set_width(pv, atoi(text));
395 if ( ! text) return FALSE;
396 return psppire_variable_set_decimals(pv, atoi(text));
399 psppire_variable_set_label(pv, text);
407 /* These can be modified only by their respective dialog boxes */
411 g_assert_not_reached();
419 #define MAX_CELL_TEXT_LEN 255
422 text_for_column(const struct PsppireVariable *pv, gint c)
424 static gchar buf[MAX_CELL_TEXT_LEN];
426 static gchar none[]=_("None");
428 static const gchar *const type_label[] =
439 enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
440 VT_CUSTOM, VT_STRING};
442 const struct fmt_spec *write_spec = psppire_variable_get_write_spec(pv);
447 return psppire_variable_get_name(pv);
451 switch ( write_spec->type )
454 return type_label[VT_NUMERIC];
457 return type_label[VT_COMMA];
460 return type_label[VT_DOT];
463 return type_label[VT_SCIENTIFIC];
478 return type_label[VT_DATE];
481 return type_label[VT_DOLLAR];
488 return type_label[VT_CUSTOM];
491 return type_label[VT_STRING];
494 g_warning("Unknown format: \"%s\"\n",
495 fmt_to_string(write_spec));
502 g_snprintf(buf, MAX_CELL_TEXT_LEN, "%d", write_spec->w);
508 g_snprintf(buf, MAX_CELL_TEXT_LEN, "%d", write_spec->d);
514 g_snprintf(buf, MAX_CELL_TEXT_LEN,
515 "%d", psppire_variable_get_columns(pv));
520 return psppire_variable_get_label(pv);
524 const struct missing_values *miss = psppire_variable_get_missing(pv);
525 if ( mv_is_empty(miss))
529 if ( ! mv_has_range (miss))
531 const int n = mv_n_values(miss);
532 gchar *mv[4] = {0,0,0,0};
534 for(i = 0 ; i < n; ++i )
537 mv_peek_value(miss, &v, i);
538 mv[i] = value_to_text(v, *write_spec);
541 for(i = 0 ; i < n; ++i )
544 g_strlcat(buf, ", ", MAX_CELL_TEXT_LEN);
545 g_strlcat(buf, mv[i], MAX_CELL_TEXT_LEN);
552 union value low, high;
553 mv_peek_range(miss, &low.f, &high.f);
555 l = value_to_text(low, *write_spec);
556 h = value_to_text(high, *write_spec);
558 g_snprintf(buf, MAX_CELL_TEXT_LEN, "%s - %s", l, h);
562 if ( mv_has_value(miss))
564 gchar buf2[MAX_CELL_TEXT_LEN];
567 mv_peek_value(miss, &v, 0);
569 s = value_to_text(v, *write_spec);
571 g_snprintf(buf2, MAX_CELL_TEXT_LEN, "%s, %s", buf, s);
583 const struct val_labs *vls = psppire_variable_get_value_labels(pv);
584 if ( ! vls || 0 == val_labs_count(vls))
588 struct val_labs_iterator *ip=0;
589 struct val_lab *vl = val_labs_first_sorted (vls, &ip);
594 gchar *const vstr = value_to_text(vl->value, *write_spec);
596 g_snprintf(buf, MAX_CELL_TEXT_LEN, "{%s,\"%s\"}_", vstr, vl->label);
607 return alignments[psppire_variable_get_alignment(pv)];
610 return measures[psppire_variable_get_measure(pv)];
618 /* Return the number of variables */
620 psppire_var_store_get_var_cnt(PsppireVarStore *store)
622 return psppire_dict_get_var_cnt(store->dict);
627 psppire_var_store_set_font(PsppireVarStore *store, PangoFontDescription *fd)
629 g_return_if_fail (store);
630 g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
632 store->font_desc = fd;
634 g_sheet_model_range_changed (G_SHEET_MODEL(store), -1, -1, -1, -1);