1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "psppire-var-sheet.h"
19 #include <gtksheet/psppire-axis-hetero.h>
21 #include <glade/glade.h>
24 #include "customentry.h"
25 #include <data/variable.h>
26 #include "psppire-var-store.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
33 static void psppire_var_sheet_class_init (PsppireVarSheetClass *klass);
34 static void psppire_var_sheet_init (PsppireVarSheet *vs);
38 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS = 1
42 psppire_var_sheet_get_type (void)
44 static GType vs_type = 0;
48 static const GTypeInfo vs_info =
50 sizeof (PsppireVarSheetClass),
52 NULL, /* base_finalize */
53 (GClassInitFunc) psppire_var_sheet_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
56 sizeof (PsppireVarSheet),
58 (GInstanceInitFunc) psppire_var_sheet_init,
61 vs_type = g_type_register_static (GTK_TYPE_SHEET, "PsppireVarSheet",
68 static GObjectClass * parent_class = NULL;
71 psppire_var_sheet_dispose (GObject *obj)
73 PsppireVarSheet *vs = (PsppireVarSheet *)obj;
75 if (vs->dispose_has_run)
78 /* Make sure dispose does not run twice. */
79 vs->dispose_has_run = TRUE;
81 /* Chain up to the parent class */
82 G_OBJECT_CLASS (parent_class)->dispose (obj);
86 psppire_var_sheet_finalize (GObject *obj)
88 /* Chain up to the parent class */
89 G_OBJECT_CLASS (parent_class)->finalize (obj);
93 struct column_parameters
99 #define n_ALIGNMENTS 3
101 const gchar *const alignments[n_ALIGNMENTS + 1]={
108 const gchar *const measures[n_MEASURES + 1]={
117 /* Create a list store from an array of strings */
118 static GtkListStore *
119 create_label_list (const gchar *const *labels)
125 GtkListStore *list_store;
126 list_store = gtk_list_store_new (1, G_TYPE_STRING);
129 while ( (s = labels[i++]))
131 gtk_list_store_append (list_store, &iter);
132 gtk_list_store_set (list_store, &iter,
142 psppire_var_sheet_set_property (GObject *object,
147 PsppireVarSheet *self = (PsppireVarSheet *) object;
151 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
152 self->may_create_vars = g_value_get_boolean (value);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
162 psppire_var_sheet_get_property (GObject *object,
167 PsppireVarSheet *self = (PsppireVarSheet *) object;
171 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
172 g_value_set_boolean (value, self->may_create_vars);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
184 psppire_var_sheet_class_init (PsppireVarSheetClass *klass)
186 GObjectClass *object_class = G_OBJECT_CLASS (klass);
189 parent_class = g_type_class_peek_parent (klass);
191 object_class->dispose = psppire_var_sheet_dispose;
192 object_class->finalize = psppire_var_sheet_finalize;
193 object_class->set_property = psppire_var_sheet_set_property;
194 object_class->get_property = psppire_var_sheet_get_property;
196 pspec = g_param_spec_boolean ("may-create-vars",
197 "May create variables",
198 "Whether the user may create more variables",
201 g_object_class_install_property (object_class,
202 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS,
205 klass->measure_list = create_label_list (measures);
206 klass->alignment_list = create_label_list (alignments);
211 /* Callback for when the alignment combo box
214 change_alignment (GtkComboBox *cb,
215 struct variable *var)
217 gint active_item = gtk_combo_box_get_active (cb);
219 if ( active_item < 0 ) return ;
221 var_set_alignment (var, active_item);
226 /* Callback for when the measure combo box
229 change_measure (GtkComboBox *cb,
230 struct variable *var)
232 gint active_item = gtk_combo_box_get_active (cb);
234 if ( active_item < 0 ) return ;
236 var_set_measure (var, active_item);
240 /* Moves the focus to a new cell.
241 Returns TRUE iff the move should be disallowed */
243 traverse_cell_callback (GtkSheet *sheet,
244 const GtkSheetCell *existing_cell,
245 GtkSheetCell *new_cell)
247 PsppireVarSheet *var_sheet = PSPPIRE_VAR_SHEET (sheet);
248 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
250 gint n_vars = psppire_var_store_get_var_cnt (var_store);
252 if (new_cell->row >= n_vars && !var_sheet->may_create_vars)
255 if ( existing_cell->row == n_vars && new_cell->row >= n_vars)
257 GtkEntry *entry = gtk_sheet_get_entry (sheet);
259 const gchar *name = gtk_entry_get_text (entry);
261 if (! psppire_dict_check_name (var_store->dict, name, TRUE))
264 psppire_dict_insert_variable (var_store->dict, existing_cell->row, name);
270 /* If the destination cell is outside the current variables, then
271 automatically create variables for the new rows.
273 if ( ((new_cell->row > n_vars) ||
274 (new_cell->row == n_vars &&
275 new_cell->col != PSPPIRE_VAR_STORE_COL_NAME)) )
278 for ( i = n_vars ; i <= new_cell->row; ++i )
279 psppire_dict_insert_variable (var_store->dict, i, NULL);
289 Callback whenever the active cell changes on the var sheet.
292 var_sheet_change_active_cell (PsppireVarSheet *vs,
293 gint row, gint column,
294 gint oldrow, gint oldcolumn,
297 GtkSheetCellAttr attributes;
298 PsppireVarStore *var_store;
299 PsppireVarSheetClass *vs_class =
300 PSPPIRE_VAR_SHEET_CLASS(G_OBJECT_GET_CLASS (vs));
302 struct variable *var ;
303 GtkSheet *sheet = GTK_SHEET (vs);
305 g_return_if_fail (sheet != NULL);
307 var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
309 g_assert (var_store);
311 g_return_if_fail (oldcolumn == PSPPIRE_VAR_STORE_COL_NAME ||
312 row < psppire_var_store_get_var_cnt (var_store));
314 gtk_sheet_get_attributes (sheet, row, column, &attributes);
316 var = psppire_var_store_get_var (var_store, row);
320 case PSPPIRE_VAR_STORE_COL_ALIGN:
323 static GtkListStore *list_store = NULL;
324 GtkComboBoxEntry *cbe;
325 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
326 entry = gtk_sheet_get_entry (sheet);
327 cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
329 if ( ! list_store) list_store = create_label_list (alignments);
331 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
332 GTK_TREE_MODEL (vs_class->alignment_list));
334 gtk_combo_box_entry_set_text_column (cbe, 0);
336 g_signal_connect (G_OBJECT (cbe),"changed",
337 G_CALLBACK (change_alignment), var);
341 case PSPPIRE_VAR_STORE_COL_MEASURE:
344 GtkComboBoxEntry *cbe;
345 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
346 entry = gtk_sheet_get_entry (sheet);
347 cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
349 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
350 GTK_TREE_MODEL (vs_class->measure_list));
352 gtk_combo_box_entry_set_text_column (cbe, 0);
354 g_signal_connect (G_OBJECT (cbe),"changed",
355 G_CALLBACK (change_measure), var);
359 case PSPPIRE_VAR_STORE_COL_VALUES:
361 PsppireCustomEntry *customEntry;
363 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
366 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
368 if ( var_is_long_string (var))
369 g_object_set (customEntry,
373 val_labs_dialog_set_target_variable (vs->val_labs_dialog, var);
375 g_signal_connect_swapped (customEntry,
377 G_CALLBACK (val_labs_dialog_show),
378 vs->val_labs_dialog);
382 case PSPPIRE_VAR_STORE_COL_MISSING:
384 PsppireCustomEntry *customEntry;
386 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
389 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
391 if ( var_is_long_string (var))
392 g_object_set (customEntry,
397 vs->missing_val_dialog->pv =
398 psppire_var_store_get_var (var_store, row);
400 g_signal_connect_swapped (customEntry,
402 G_CALLBACK (missing_val_dialog_show),
403 vs->missing_val_dialog);
407 case PSPPIRE_VAR_STORE_COL_TYPE:
409 PsppireCustomEntry *customEntry;
411 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
414 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
417 /* Popup the Variable Type dialog box */
418 vs->var_type_dialog->pv = var;
420 g_signal_connect_swapped (customEntry,
422 G_CALLBACK (var_type_dialog_show),
423 vs->var_type_dialog);
427 case PSPPIRE_VAR_STORE_COL_WIDTH:
428 case PSPPIRE_VAR_STORE_COL_DECIMALS:
429 case PSPPIRE_VAR_STORE_COL_COLUMNS:
431 if ( attributes.is_editable)
435 const gchar *s = gtk_sheet_cell_get_text (sheet, row, column);
439 GtkSpinButton *spinButton ;
440 const gint current_value = g_strtod (s, NULL);
443 const struct fmt_spec *fmt = var_get_write_format (var);
446 case PSPPIRE_VAR_STORE_COL_WIDTH:
447 r_min = MAX (fmt->d + 1, fmt_min_output_width (fmt->type));
448 r_max = fmt_max_output_width (fmt->type);
450 case PSPPIRE_VAR_STORE_COL_DECIMALS:
452 r_max = fmt_max_output_decimals (fmt->type, fmt->w);
454 case PSPPIRE_VAR_STORE_COL_COLUMNS:
456 r_max = 255 ; /* Is this a sensible value ? */
459 g_assert_not_reached ();
462 adj = gtk_adjustment_new (current_value,
464 1.0, 1.0, 1.0 /* steps */
467 gtk_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
470 GTK_SPIN_BUTTON (gtk_sheet_get_entry (sheet));
472 gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
473 gtk_spin_button_set_digits (spinButton, 0);
480 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
487 psppire_var_sheet_init (PsppireVarSheet *vs)
489 GladeXML *xml = XML_NEW ("data-editor.glade");
491 vs->val_labs_dialog = val_labs_dialog_create (xml);
492 vs->missing_val_dialog = missing_val_dialog_create (xml);
493 vs->var_type_dialog = var_type_dialog_create (xml);
495 g_object_unref (xml);
497 vs->dispose_has_run = FALSE;
498 vs->may_create_vars = TRUE;
500 g_signal_connect (vs, "activate",
501 G_CALLBACK (var_sheet_change_active_cell),
504 g_signal_connect (vs, "traverse",
505 G_CALLBACK (traverse_cell_callback), NULL);
509 static const struct column_parameters column_def[] = {
513 { N_("Decimals"),91},
515 { N_("Values"), 103},
516 { N_("Missing"), 95},
517 { N_("Columns"), 80},
519 { N_("Measure"), 99},
523 psppire_var_sheet_new (void)
526 PsppireAxisHetero *ha = psppire_axis_hetero_new ();
527 PsppireAxisHetero *va = psppire_axis_hetero_new ();
529 GtkWidget *w = g_object_new (psppire_var_sheet_get_type (), NULL);
531 for (i = 0 ; i < 10 ; ++i)
532 psppire_axis_hetero_append (ha, column_def[i].width);
539 g_object_set (ha, "minimum-extent", 0,
543 "horizontal-axis", ha,