1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2011 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 <ui/gui/sheet/psppire-axis.h>
21 #include "builder-wrapper.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);
35 static void psppire_var_sheet_realize (GtkWidget *w);
36 static void psppire_var_sheet_unrealize (GtkWidget *w);
41 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS = 1
45 psppire_var_sheet_get_type (void)
47 static GType vs_type = 0;
51 static const GTypeInfo vs_info =
53 sizeof (PsppireVarSheetClass),
55 NULL, /* base_finalize */
56 (GClassInitFunc) psppire_var_sheet_class_init,
57 NULL, /* class_finalize */
58 NULL, /* class_data */
59 sizeof (PsppireVarSheet),
61 (GInstanceInitFunc) psppire_var_sheet_init,
64 vs_type = g_type_register_static (PSPPIRE_TYPE_SHEET, "PsppireVarSheet",
71 static GObjectClass * parent_class = NULL;
74 psppire_var_sheet_dispose (GObject *obj)
76 PsppireVarSheet *vs = (PsppireVarSheet *)obj;
78 if (vs->dispose_has_run)
81 /* Make sure dispose does not run twice. */
82 vs->dispose_has_run = TRUE;
84 /* Chain up to the parent class */
85 G_OBJECT_CLASS (parent_class)->dispose (obj);
89 psppire_var_sheet_finalize (GObject *obj)
91 /* Chain up to the parent class */
92 G_OBJECT_CLASS (parent_class)->finalize (obj);
96 struct column_parameters
102 #define n_ALIGNMENTS 3
104 const gchar *const alignments[n_ALIGNMENTS + 1]={
111 const gchar *const measures[n_MEASURES + 1]={
120 /* Create a list store from an array of strings */
121 static GtkListStore *
122 create_label_list (const gchar *const *labels)
128 GtkListStore *list_store = gtk_list_store_new (1, G_TYPE_STRING);
130 while ( (s = labels[i++]))
132 gtk_list_store_append (list_store, &iter);
133 gtk_list_store_set (list_store, &iter,
143 psppire_var_sheet_set_property (GObject *object,
148 PsppireVarSheet *self = (PsppireVarSheet *) object;
152 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
153 self->may_create_vars = g_value_get_boolean (value);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
163 psppire_var_sheet_get_property (GObject *object,
168 PsppireVarSheet *self = (PsppireVarSheet *) object;
172 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
173 g_value_set_boolean (value, self->may_create_vars);
177 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);
187 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
190 parent_class = g_type_class_peek_parent (klass);
192 object_class->dispose = psppire_var_sheet_dispose;
193 object_class->finalize = psppire_var_sheet_finalize;
194 widget_class->realize = psppire_var_sheet_realize;
195 widget_class->unrealize = psppire_var_sheet_unrealize;
196 object_class->set_property = psppire_var_sheet_set_property;
197 object_class->get_property = psppire_var_sheet_get_property;
199 pspec = g_param_spec_boolean ("may-create-vars",
200 "May create variables",
201 "Whether the user may create more variables",
204 g_object_class_install_property (object_class,
205 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS,
208 klass->measure_list = create_label_list (measures);
209 klass->alignment_list = create_label_list (alignments);
214 /* Callback for when the alignment combo box
217 change_alignment (GtkComboBox *cb,
218 struct variable *var)
220 gint active_item = gtk_combo_box_get_active (cb);
222 if ( active_item < 0 ) return ;
224 var_set_alignment (var, active_item);
229 /* Callback for when the measure combo box
232 change_measure (GtkComboBox *cb,
233 struct variable *var)
235 gint active_item = gtk_combo_box_get_active (cb);
237 if ( active_item < 0 ) return ;
239 var_set_measure (var, active_item);
243 /* Moves the focus to a new cell.
244 Returns TRUE iff the move should be disallowed */
246 traverse_cell_callback (PsppireSheet *sheet,
247 const PsppireSheetCell *existing_cell,
248 PsppireSheetCell *new_cell)
250 PsppireVarSheet *var_sheet = PSPPIRE_VAR_SHEET (sheet);
251 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (psppire_sheet_get_model (sheet));
253 gint n_vars = psppire_var_store_get_var_cnt (var_store);
255 if (new_cell->col >= PSPPIRE_VAR_STORE_n_COLS)
258 if (new_cell->row >= n_vars && !var_sheet->may_create_vars)
261 if ( existing_cell->row == n_vars && new_cell->row >= n_vars)
263 GtkEntry *entry = psppire_sheet_get_entry (sheet);
265 const gchar *name = gtk_entry_get_text (entry);
267 if (! psppire_dict_check_name (var_store->dictionary, name, TRUE))
270 psppire_dict_insert_variable (var_store->dictionary, existing_cell->row, name);
276 /* If the destination cell is outside the current variables, then
277 automatically create variables for the new rows.
279 if ( ((new_cell->row > n_vars) ||
280 (new_cell->row == n_vars &&
281 new_cell->col != PSPPIRE_VAR_STORE_COL_NAME)) )
284 for ( i = n_vars ; i <= new_cell->row; ++i )
285 psppire_dict_insert_variable (var_store->dictionary, i, NULL);
294 Callback whenever the active cell changes on the var sheet.
297 var_sheet_change_active_cell (PsppireVarSheet *vs,
298 gint row, gint column,
299 gint oldrow, gint oldcolumn,
302 PsppireVarStore *var_store;
303 PsppireVarSheetClass *vs_class =
304 PSPPIRE_VAR_SHEET_CLASS(G_OBJECT_GET_CLASS (vs));
306 struct variable *var ;
307 PsppireSheet *sheet = PSPPIRE_SHEET (vs);
309 g_return_if_fail (sheet != NULL);
311 var_store = PSPPIRE_VAR_STORE (psppire_sheet_get_model (sheet));
313 g_assert (var_store);
315 g_return_if_fail (oldcolumn == PSPPIRE_VAR_STORE_COL_NAME ||
316 row < psppire_var_store_get_var_cnt (var_store));
318 var = psppire_var_store_get_var (var_store, row);
322 case PSPPIRE_VAR_STORE_COL_ALIGN:
325 static GtkListStore *list_store = NULL;
326 GtkComboBoxEntry *cbe;
327 psppire_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
328 entry = psppire_sheet_get_entry (sheet);
329 cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
331 if ( ! list_store) list_store = create_label_list (alignments);
333 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
334 GTK_TREE_MODEL (vs_class->alignment_list));
336 gtk_combo_box_entry_set_text_column (cbe, 0);
338 g_signal_connect (cbe, "changed",
339 G_CALLBACK (change_alignment), var);
343 case PSPPIRE_VAR_STORE_COL_MEASURE:
346 GtkComboBoxEntry *cbe;
347 psppire_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
348 entry = psppire_sheet_get_entry (sheet);
349 cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
351 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
352 GTK_TREE_MODEL (vs_class->measure_list));
354 gtk_combo_box_entry_set_text_column (cbe, 0);
356 g_signal_connect (cbe, "changed",
357 G_CALLBACK (change_measure), var);
361 case PSPPIRE_VAR_STORE_COL_VALUES:
363 PsppireCustomEntry *customEntry;
365 psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
368 PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
370 val_labs_dialog_set_target_variable (vs->val_labs_dialog, var);
372 g_signal_connect_swapped (customEntry,
374 G_CALLBACK (val_labs_dialog_show),
375 vs->val_labs_dialog);
379 case PSPPIRE_VAR_STORE_COL_MISSING:
381 PsppireCustomEntry *customEntry;
383 psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
386 PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
388 vs->missing_val_dialog->pv =
389 psppire_var_store_get_var (var_store, row);
391 g_signal_connect_swapped (customEntry,
393 G_CALLBACK (missing_val_dialog_show),
394 vs->missing_val_dialog);
398 case PSPPIRE_VAR_STORE_COL_TYPE:
400 PsppireCustomEntry *customEntry;
402 psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
405 PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
408 /* Popup the Variable Type dialog box */
409 vs->var_type_dialog->pv = var;
411 g_signal_connect_swapped (customEntry,
413 G_CALLBACK (var_type_dialog_show),
414 vs->var_type_dialog);
418 case PSPPIRE_VAR_STORE_COL_WIDTH:
419 case PSPPIRE_VAR_STORE_COL_DECIMALS:
420 case PSPPIRE_VAR_STORE_COL_COLUMNS:
422 if ( psppire_sheet_model_is_editable (PSPPIRE_SHEET_MODEL(var_store),
427 const gchar *s = psppire_sheet_cell_get_text (sheet, row, column);
431 GtkSpinButton *spinButton ;
432 const gint current_value = g_strtod (s, NULL);
435 const struct fmt_spec *fmt = var_get_print_format (var);
438 case PSPPIRE_VAR_STORE_COL_WIDTH:
439 r_min = MAX (fmt->d + 1, fmt_min_output_width (fmt->type));
440 r_max = fmt_max_output_width (fmt->type);
442 case PSPPIRE_VAR_STORE_COL_DECIMALS:
444 r_max = fmt_max_output_decimals (fmt->type, fmt->w);
446 case PSPPIRE_VAR_STORE_COL_COLUMNS:
448 r_max = 255 ; /* Is this a sensible value ? */
451 g_assert_not_reached ();
454 adj = gtk_adjustment_new (current_value,
456 1.0, 1.0, /* steps */
459 psppire_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
462 GTK_SPIN_BUTTON (psppire_sheet_get_entry (sheet));
464 gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
465 gtk_spin_button_set_digits (spinButton, 0);
472 psppire_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
479 psppire_var_sheet_realize (GtkWidget *w)
481 PsppireVarSheet *vs = PSPPIRE_VAR_SHEET (w);
483 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (vs));
485 vs->val_labs_dialog = val_labs_dialog_create (GTK_WINDOW (toplevel));
487 vs->missing_val_dialog = missing_val_dialog_create (GTK_WINDOW (toplevel));
488 vs->var_type_dialog = var_type_dialog_create (GTK_WINDOW (toplevel));
490 /* Chain up to the parent class */
491 GTK_WIDGET_CLASS (parent_class)->realize (w);
495 psppire_var_sheet_unrealize (GtkWidget *w)
497 PsppireVarSheet *vs = PSPPIRE_VAR_SHEET (w);
499 g_free (vs->val_labs_dialog);
500 g_free (vs->missing_val_dialog);
501 g_free (vs->var_type_dialog);
503 /* Chain up to the parent class */
504 GTK_WIDGET_CLASS (parent_class)->unrealize (w);
510 psppire_var_sheet_init (PsppireVarSheet *vs)
512 GtkBuilder *builder = builder_new ("data-editor.ui");
514 connect_help (builder);
516 g_object_unref (builder);
518 vs->dispose_has_run = FALSE;
519 vs->may_create_vars = TRUE;
521 g_signal_connect (vs, "activate",
522 G_CALLBACK (var_sheet_change_active_cell),
525 g_signal_connect (vs, "traverse",
526 G_CALLBACK (traverse_cell_callback), NULL);
530 static const struct column_parameters column_def[] = {
534 { N_("Decimals"),91},
536 { N_("Values"), 103},
537 { N_("Missing"), 95},
538 { N_("Columns"), 80},
540 { N_("Measure"), 99},
544 psppire_var_sheet_new (void)
547 PsppireAxis *ha = psppire_axis_new ();
548 PsppireAxis *va = psppire_axis_new ();
550 GtkWidget *w = g_object_new (psppire_var_sheet_get_type (), NULL);
552 for (i = 0 ; i < 10 ; ++i)
553 psppire_axis_append (ha, column_def[i].width);
559 g_object_set (ha, "minimum-extent", 0,
563 "horizontal-axis", ha,