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 <ui/gui/sheet/psppire-axis-impl.h>
23 #include "customentry.h"
24 #include <data/variable.h>
25 #include "psppire-var-store.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
32 static void psppire_var_sheet_class_init (PsppireVarSheetClass *klass);
33 static void psppire_var_sheet_init (PsppireVarSheet *vs);
37 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS = 1
41 psppire_var_sheet_get_type (void)
43 static GType vs_type = 0;
47 static const GTypeInfo vs_info =
49 sizeof (PsppireVarSheetClass),
51 NULL, /* base_finalize */
52 (GClassInitFunc) psppire_var_sheet_class_init,
53 NULL, /* class_finalize */
54 NULL, /* class_data */
55 sizeof (PsppireVarSheet),
57 (GInstanceInitFunc) psppire_var_sheet_init,
60 vs_type = g_type_register_static (PSPPIRE_TYPE_SHEET, "PsppireVarSheet",
67 static GObjectClass * parent_class = NULL;
70 psppire_var_sheet_dispose (GObject *obj)
72 PsppireVarSheet *vs = (PsppireVarSheet *)obj;
74 if (vs->dispose_has_run)
77 /* Make sure dispose does not run twice. */
78 vs->dispose_has_run = TRUE;
80 /* Chain up to the parent class */
81 G_OBJECT_CLASS (parent_class)->dispose (obj);
85 psppire_var_sheet_finalize (GObject *obj)
87 /* Chain up to the parent class */
88 G_OBJECT_CLASS (parent_class)->finalize (obj);
92 struct column_parameters
98 #define n_ALIGNMENTS 3
100 const gchar *const alignments[n_ALIGNMENTS + 1]={
107 const gchar *const measures[n_MEASURES + 1]={
116 /* Create a list store from an array of strings */
117 static GtkListStore *
118 create_label_list (const gchar *const *labels)
124 GtkListStore *list_store;
125 list_store = gtk_list_store_new (1, G_TYPE_STRING);
128 while ( (s = labels[i++]))
130 gtk_list_store_append (list_store, &iter);
131 gtk_list_store_set (list_store, &iter,
141 psppire_var_sheet_set_property (GObject *object,
146 PsppireVarSheet *self = (PsppireVarSheet *) object;
150 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
151 self->may_create_vars = g_value_get_boolean (value);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
161 psppire_var_sheet_get_property (GObject *object,
166 PsppireVarSheet *self = (PsppireVarSheet *) object;
170 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
171 g_value_set_boolean (value, self->may_create_vars);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
183 psppire_var_sheet_class_init (PsppireVarSheetClass *klass)
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
188 parent_class = g_type_class_peek_parent (klass);
190 object_class->dispose = psppire_var_sheet_dispose;
191 object_class->finalize = psppire_var_sheet_finalize;
192 object_class->set_property = psppire_var_sheet_set_property;
193 object_class->get_property = psppire_var_sheet_get_property;
195 pspec = g_param_spec_boolean ("may-create-vars",
196 "May create variables",
197 "Whether the user may create more variables",
200 g_object_class_install_property (object_class,
201 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS,
204 klass->measure_list = create_label_list (measures);
205 klass->alignment_list = create_label_list (alignments);
210 /* Callback for when the alignment combo box
213 change_alignment (GtkComboBox *cb,
214 struct variable *var)
216 gint active_item = gtk_combo_box_get_active (cb);
218 if ( active_item < 0 ) return ;
220 var_set_alignment (var, active_item);
225 /* Callback for when the measure combo box
228 change_measure (GtkComboBox *cb,
229 struct variable *var)
231 gint active_item = gtk_combo_box_get_active (cb);
233 if ( active_item < 0 ) return ;
235 var_set_measure (var, active_item);
239 /* Moves the focus to a new cell.
240 Returns TRUE iff the move should be disallowed */
242 traverse_cell_callback (PsppireSheet *sheet,
243 const PsppireSheetCell *existing_cell,
244 PsppireSheetCell *new_cell)
246 PsppireVarSheet *var_sheet = PSPPIRE_VAR_SHEET (sheet);
247 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (psppire_sheet_get_model (sheet));
249 gint n_vars = psppire_var_store_get_var_cnt (var_store);
251 if (new_cell->row >= n_vars && !var_sheet->may_create_vars)
254 if ( existing_cell->row == n_vars && new_cell->row >= n_vars)
256 GtkEntry *entry = psppire_sheet_get_entry (sheet);
258 const gchar *name = gtk_entry_get_text (entry);
260 if (! psppire_dict_check_name (var_store->dict, name, TRUE))
263 psppire_dict_insert_variable (var_store->dict, existing_cell->row, name);
269 /* If the destination cell is outside the current variables, then
270 automatically create variables for the new rows.
272 if ( ((new_cell->row > n_vars) ||
273 (new_cell->row == n_vars &&
274 new_cell->col != PSPPIRE_VAR_STORE_COL_NAME)) )
277 for ( i = n_vars ; i <= new_cell->row; ++i )
278 psppire_dict_insert_variable (var_store->dict, i, NULL);
288 Callback whenever the active cell changes on the var sheet.
291 var_sheet_change_active_cell (PsppireVarSheet *vs,
292 gint row, gint column,
293 gint oldrow, gint oldcolumn,
296 PsppireSheetCellAttr attributes;
297 PsppireVarStore *var_store;
298 PsppireVarSheetClass *vs_class =
299 PSPPIRE_VAR_SHEET_CLASS(G_OBJECT_GET_CLASS (vs));
301 struct variable *var ;
302 PsppireSheet *sheet = PSPPIRE_SHEET (vs);
304 g_return_if_fail (sheet != NULL);
306 var_store = PSPPIRE_VAR_STORE (psppire_sheet_get_model (sheet));
308 g_assert (var_store);
310 g_return_if_fail (oldcolumn == PSPPIRE_VAR_STORE_COL_NAME ||
311 row < psppire_var_store_get_var_cnt (var_store));
313 psppire_sheet_get_attributes (sheet, row, column, &attributes);
315 var = psppire_var_store_get_var (var_store, row);
319 case PSPPIRE_VAR_STORE_COL_ALIGN:
322 static GtkListStore *list_store = NULL;
323 GtkComboBoxEntry *cbe;
324 psppire_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
325 entry = psppire_sheet_get_entry (sheet);
326 cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
328 if ( ! list_store) list_store = create_label_list (alignments);
330 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
331 GTK_TREE_MODEL (vs_class->alignment_list));
333 gtk_combo_box_entry_set_text_column (cbe, 0);
335 g_signal_connect (G_OBJECT (cbe),"changed",
336 G_CALLBACK (change_alignment), var);
340 case PSPPIRE_VAR_STORE_COL_MEASURE:
343 GtkComboBoxEntry *cbe;
344 psppire_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
345 entry = psppire_sheet_get_entry (sheet);
346 cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
348 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
349 GTK_TREE_MODEL (vs_class->measure_list));
351 gtk_combo_box_entry_set_text_column (cbe, 0);
353 g_signal_connect (G_OBJECT (cbe),"changed",
354 G_CALLBACK (change_measure), var);
358 case PSPPIRE_VAR_STORE_COL_VALUES:
360 PsppireCustomEntry *customEntry;
362 psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
365 PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
367 if ( var_is_long_string (var))
368 g_object_set (customEntry,
372 val_labs_dialog_set_target_variable (vs->val_labs_dialog, var);
374 g_signal_connect_swapped (customEntry,
376 G_CALLBACK (val_labs_dialog_show),
377 vs->val_labs_dialog);
381 case PSPPIRE_VAR_STORE_COL_MISSING:
383 PsppireCustomEntry *customEntry;
385 psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
388 PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
390 if ( var_is_long_string (var))
391 g_object_set (customEntry,
396 vs->missing_val_dialog->pv =
397 psppire_var_store_get_var (var_store, row);
399 g_signal_connect_swapped (customEntry,
401 G_CALLBACK (missing_val_dialog_show),
402 vs->missing_val_dialog);
406 case PSPPIRE_VAR_STORE_COL_TYPE:
408 PsppireCustomEntry *customEntry;
410 psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
413 PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
416 /* Popup the Variable Type dialog box */
417 vs->var_type_dialog->pv = var;
419 g_signal_connect_swapped (customEntry,
421 G_CALLBACK (var_type_dialog_show),
422 vs->var_type_dialog);
426 case PSPPIRE_VAR_STORE_COL_WIDTH:
427 case PSPPIRE_VAR_STORE_COL_DECIMALS:
428 case PSPPIRE_VAR_STORE_COL_COLUMNS:
430 if ( attributes.is_editable)
434 const gchar *s = psppire_sheet_cell_get_text (sheet, row, column);
438 GtkSpinButton *spinButton ;
439 const gint current_value = g_strtod (s, NULL);
442 const struct fmt_spec *fmt = var_get_write_format (var);
445 case PSPPIRE_VAR_STORE_COL_WIDTH:
446 r_min = MAX (fmt->d + 1, fmt_min_output_width (fmt->type));
447 r_max = fmt_max_output_width (fmt->type);
449 case PSPPIRE_VAR_STORE_COL_DECIMALS:
451 r_max = fmt_max_output_decimals (fmt->type, fmt->w);
453 case PSPPIRE_VAR_STORE_COL_COLUMNS:
455 r_max = 255 ; /* Is this a sensible value ? */
458 g_assert_not_reached ();
461 adj = gtk_adjustment_new (current_value,
463 1.0, 1.0, 1.0 /* steps */
466 psppire_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
469 GTK_SPIN_BUTTON (psppire_sheet_get_entry (sheet));
471 gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
472 gtk_spin_button_set_digits (spinButton, 0);
479 psppire_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
486 psppire_var_sheet_init (PsppireVarSheet *vs)
488 GtkBuilder *builder = builder_new ("data-editor.ui");
490 vs->val_labs_dialog = val_labs_dialog_create (builder);
491 vs->missing_val_dialog = missing_val_dialog_create (builder);
492 vs->var_type_dialog = var_type_dialog_create (builder);
494 connect_help (builder);
496 g_object_unref (builder);
498 vs->dispose_has_run = FALSE;
499 vs->may_create_vars = TRUE;
501 g_signal_connect (vs, "activate",
502 G_CALLBACK (var_sheet_change_active_cell),
505 g_signal_connect (vs, "traverse",
506 G_CALLBACK (traverse_cell_callback), NULL);
510 static const struct column_parameters column_def[] = {
514 { N_("Decimals"),91},
516 { N_("Values"), 103},
517 { N_("Missing"), 95},
518 { N_("Columns"), 80},
520 { N_("Measure"), 99},
524 psppire_var_sheet_new (void)
527 PsppireAxisImpl *ha = psppire_axis_impl_new ();
528 PsppireAxisImpl *va = psppire_axis_impl_new ();
530 GtkWidget *w = g_object_new (psppire_var_sheet_get_type (), NULL);
532 for (i = 0 ; i < 10 ; ++i)
533 psppire_axis_impl_append (ha, column_def[i].width);
539 g_object_set (ha, "minimum-extent", 0,
543 "horizontal-axis", ha,