2 /* PSPPIRE - a graphical user interface for PSPP.
3 Copyright (C) 2008 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "psppire-var-sheet.h"
21 #include <glade/glade.h>
23 #include <gtksheet/gsheet-hetero-column.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 static const struct column_parameters column_def[] = {
103 { N_("Decimals"),91},
105 { N_("Values"), 103},
106 { N_("Missing"), 95},
107 { N_("Columns"), 80},
109 { N_("Measure"), 99},
113 #define n_ALIGNMENTS 3
115 const gchar *const alignments[n_ALIGNMENTS + 1]={
122 const gchar *const measures[n_MEASURES + 1]={
131 /* Create a list store from an array of strings */
132 static GtkListStore *
133 create_label_list (const gchar *const *labels)
139 GtkListStore *list_store;
140 list_store = gtk_list_store_new (1, G_TYPE_STRING);
143 while ( (s = labels[i++]))
145 gtk_list_store_append (list_store, &iter);
146 gtk_list_store_set (list_store, &iter,
156 psppire_var_sheet_set_property (GObject *object,
161 PsppireVarSheet *self = (PsppireVarSheet *) object;
165 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
166 self->may_create_vars = g_value_get_boolean (value);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
176 psppire_var_sheet_get_property (GObject *object,
181 PsppireVarSheet *self = (PsppireVarSheet *) object;
185 case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
186 g_value_set_boolean (value, self->may_create_vars);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
198 psppire_var_sheet_class_init (PsppireVarSheetClass *klass)
200 GObjectClass *object_class = G_OBJECT_CLASS (klass);
203 parent_class = g_type_class_peek_parent (klass);
205 object_class->dispose = psppire_var_sheet_dispose;
206 object_class->finalize = psppire_var_sheet_finalize;
207 object_class->set_property = psppire_var_sheet_set_property;
208 object_class->get_property = psppire_var_sheet_get_property;
210 pspec = g_param_spec_boolean ("may-create-vars",
211 "May create variables",
212 "Whether the user may create more variables",
215 g_object_class_install_property (object_class,
216 PSPPIRE_VAR_SHEET_MAY_CREATE_VARS,
219 klass->measure_list = create_label_list (measures);
220 klass->alignment_list = create_label_list (alignments);
225 /* Callback for when the alignment combo box
228 change_alignment (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_alignment (var, active_item);
240 /* Callback for when the measure combo box
243 change_measure (GtkComboBox *cb,
244 struct variable *var)
246 gint active_item = gtk_combo_box_get_active (cb);
248 if ( active_item < 0 ) return ;
250 var_set_measure (var, active_item);
256 traverse_cell_callback (GtkSheet *sheet,
257 gint row, gint column,
258 gint *new_row, gint *new_column
261 PsppireVarSheet *var_sheet = PSPPIRE_VAR_SHEET (sheet);
262 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
264 gint n_vars = psppire_var_store_get_var_cnt (var_store);
266 if (*new_row >= n_vars && !var_sheet->may_create_vars)
269 if ( row == n_vars && *new_row >= n_vars)
271 GtkEntry *entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
273 const gchar *name = gtk_entry_get_text (entry);
275 if (! psppire_dict_check_name (var_store->dict, name, TRUE))
278 psppire_dict_insert_variable (var_store->dict, row, name);
283 /* If the destination cell is outside the current variables, then
284 automatically create variables for the new rows.
286 if ( ((*new_row > n_vars) ||
287 (*new_row == n_vars && *new_column != PSPPIRE_VAR_STORE_COL_NAME)) )
290 for ( i = n_vars ; i <= *new_row; ++i )
291 psppire_dict_insert_variable (var_store->dict, i, NULL);
301 Callback whenever the pointer leaves a cell on the var sheet.
304 var_sheet_cell_entry_leave (GtkSheet * sheet, gint row, gint column,
307 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
313 Callback whenever the pointer enters a cell on the var sheet.
316 var_sheet_cell_entry_enter (PsppireVarSheet *vs, gint row, gint column,
319 GtkSheetCellAttr attributes;
320 PsppireVarStore *var_store ;
321 PsppireVarSheetClass *vs_class =
322 PSPPIRE_VAR_SHEET_CLASS(G_OBJECT_GET_CLASS (vs));
324 struct variable *var ;
325 GtkSheet *sheet = GTK_SHEET (vs);
327 g_return_val_if_fail (sheet != NULL, FALSE);
329 var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
331 g_assert (var_store);
333 if ( row >= psppire_var_store_get_var_cnt (var_store))
336 gtk_sheet_get_attributes (sheet, row, column, &attributes);
339 var = psppire_var_store_get_var (var_store, row);
343 case PSPPIRE_VAR_STORE_COL_ALIGN:
345 static GtkListStore *list_store = NULL;
346 GtkComboBoxEntry *cbe;
347 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
349 GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
352 if ( ! list_store) list_store = create_label_list (alignments);
354 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
355 GTK_TREE_MODEL (vs_class->alignment_list));
357 gtk_combo_box_entry_set_text_column (cbe, 0);
359 g_signal_connect (G_OBJECT (cbe),"changed",
360 G_CALLBACK (change_alignment), var);
364 case PSPPIRE_VAR_STORE_COL_MEASURE:
366 GtkComboBoxEntry *cbe;
367 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
369 GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
373 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
374 GTK_TREE_MODEL (vs_class->measure_list));
376 gtk_combo_box_entry_set_text_column (cbe, 0);
378 g_signal_connect (G_OBJECT (cbe),"changed",
379 G_CALLBACK (change_measure), var);
383 case PSPPIRE_VAR_STORE_COL_VALUES:
385 PsppireCustomEntry *customEntry;
387 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
390 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
392 if ( var_is_long_string (var))
393 g_object_set (customEntry,
397 val_labs_dialog_set_target_variable (vs->val_labs_dialog, var);
399 g_signal_connect_swapped (customEntry,
401 G_CALLBACK (val_labs_dialog_show),
402 vs->val_labs_dialog);
406 case PSPPIRE_VAR_STORE_COL_MISSING:
408 PsppireCustomEntry *customEntry;
410 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
413 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
415 if ( var_is_long_string (var))
416 g_object_set (customEntry,
421 vs->missing_val_dialog->pv =
422 psppire_var_store_get_var (var_store, row);
424 g_signal_connect_swapped (customEntry,
426 G_CALLBACK (missing_val_dialog_show),
427 vs->missing_val_dialog);
431 case PSPPIRE_VAR_STORE_COL_TYPE:
433 PsppireCustomEntry *customEntry;
435 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
438 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
441 /* Popup the Variable Type dialog box */
442 vs->var_type_dialog->pv = var;
444 g_signal_connect_swapped (customEntry,
446 G_CALLBACK (var_type_dialog_show),
447 vs->var_type_dialog);
451 case PSPPIRE_VAR_STORE_COL_WIDTH:
452 case PSPPIRE_VAR_STORE_COL_DECIMALS:
453 case PSPPIRE_VAR_STORE_COL_COLUMNS:
455 if ( attributes.is_editable)
459 const gchar *s = gtk_sheet_cell_get_text (sheet, row, column);
463 GtkSpinButton *spinButton ;
464 const gint current_value = g_strtod (s, NULL);
467 const struct fmt_spec *fmt = var_get_write_format (var);
470 case PSPPIRE_VAR_STORE_COL_WIDTH:
471 r_min = MAX (fmt->d + 1, fmt_min_output_width (fmt->type));
472 r_max = fmt_max_output_width (fmt->type);
474 case PSPPIRE_VAR_STORE_COL_DECIMALS:
476 r_max = fmt_max_output_decimals (fmt->type, fmt->w);
478 case PSPPIRE_VAR_STORE_COL_COLUMNS:
480 r_max = 255 ; /* Is this a sensible value ? */
483 g_assert_not_reached ();
486 adj = gtk_adjustment_new (current_value,
488 1.0, 1.0, 1.0 /* steps */
491 gtk_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
494 GTK_SPIN_BUTTON (gtk_sheet_get_entry (sheet));
496 gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
497 gtk_spin_button_set_digits (spinButton, 0);
504 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
516 psppire_var_sheet_init (PsppireVarSheet *vs)
519 GObject *geo = g_sheet_hetero_column_new (75, PSPPIRE_VAR_STORE_n_COLS);
520 GladeXML *xml = XML_NEW ("data-editor.glade");
522 vs->val_labs_dialog = val_labs_dialog_create (xml);
523 vs->missing_val_dialog = missing_val_dialog_create (xml);
524 vs->var_type_dialog = var_type_dialog_create (xml);
526 g_object_unref (xml);
528 vs->dispose_has_run = FALSE;
529 vs->may_create_vars = TRUE;
531 for (i = 0 ; i < PSPPIRE_VAR_STORE_n_COLS ; ++i )
533 g_sheet_hetero_column_set_button_label (G_SHEET_HETERO_COLUMN (geo), i,
534 gettext (column_def[i].label));
536 g_sheet_hetero_column_set_width (G_SHEET_HETERO_COLUMN (geo), i,
537 column_def[i].width);
540 g_object_set (vs, "column-geometry", geo, NULL);
543 g_signal_connect (vs, "activate",
544 G_CALLBACK (var_sheet_cell_entry_enter),
547 g_signal_connect (vs, "deactivate",
548 G_CALLBACK (var_sheet_cell_entry_leave),
551 g_signal_connect (vs, "traverse",
552 G_CALLBACK (traverse_cell_callback), NULL);
557 psppire_var_sheet_new (void)
559 return GTK_WIDGET (g_object_new (psppire_var_sheet_get_type (), NULL));