2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 2005, 2006 Free Software Foundation
4 Written by John Darrington
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* This module creates the Variable Sheet used for inputing the
23 variables in the dictonary */
25 #include <data/value-labels.h>
27 #include <glade/glade.h>
33 #define min(A,B) ((A < B)?A:B)
35 #include <gtksheet/gtksheet.h>
36 #include <gtksheet/gsheet-hetero-column.h>
37 #include <gtksheet/gsheet-uniform-row.h>
39 #include "psppire-var-store.h"
41 #include "menu-actions.h"
42 #include "psppire-dict.h"
43 #include "psppire-variable.h"
44 #include "var-type-dialog.h"
45 #include "var-sheet.h"
46 #include "customentry.h"
48 #include "val-labs-dialog.h"
49 #include "missing-val-dialog.h"
55 static const gint n_initial_rows = 40;
60 struct column_parameters
66 static const struct column_parameters column_def[] = {
81 click2row(GtkWidget *w, gint row, gpointer data)
83 gint current_row, current_column;
84 GtkWidget *data_sheet = get_widget_assert(xml, "data_sheet");
86 select_sheet(PAGE_DATA_SHEET);
88 gtk_sheet_get_active_cell(GTK_SHEET(data_sheet),
89 ¤t_row, ¤t_column);
91 gtk_sheet_set_active_cell(GTK_SHEET(data_sheet), current_row, row);
98 const gchar *alignments[]={
105 const gchar *measures[]={
112 static GtkListStore *
113 create_label_list(const gchar **labels)
119 GtkListStore *list_store;
120 list_store = gtk_list_store_new (1, G_TYPE_STRING);
123 while ( (s = labels[i++]))
125 gtk_list_store_append (list_store, &iter);
126 gtk_list_store_set (list_store, &iter,
134 /* Callback for when the alignment combo box
137 change_alignment(GtkComboBox *cb,
140 struct PsppireVariable *pv = user_data;
141 gint active_item = gtk_combo_box_get_active(cb);
143 if ( active_item < 0 ) return ;
145 psppire_variable_set_alignment(pv, active_item);
150 /* Callback for when the measure combo box
153 change_measure(GtkComboBox *cb,
156 struct PsppireVariable *pv = user_data;
157 gint active_item = gtk_combo_box_get_active(cb);
159 if ( active_item < 0 ) return ;
161 psppire_variable_set_measure(pv, active_item);
167 traverse_cell_callback (GtkSheet * sheet,
168 gint row, gint column,
169 gint *new_row, gint *new_column
172 PsppireVarStore *var_store = PSPPIRE_VAR_STORE(gtk_sheet_get_model(sheet));
174 gint n_vars = psppire_var_store_get_var_cnt(var_store);
176 if ( row == n_vars && *new_row >= n_vars)
178 GtkEntry *entry = GTK_ENTRY(gtk_sheet_get_entry(sheet));
180 const gchar *name = gtk_entry_get_text(entry);
182 if (! psppire_dict_check_name(var_store->dict, name, TRUE))
185 psppire_dict_insert_variable(var_store->dict, row, name);
190 /* If the destination cell is outside the current variables, then
191 accept the destination only as the name column of the first blank row
193 if ( *new_row > n_vars)
196 if ( *new_row >= n_vars && *new_column != COL_NAME)
203 /* Callback whenever the cell on the var sheet is entered or left.
204 It sets the entry box type appropriately.
207 var_sheet_cell_change_entry (GtkSheet * sheet, gint row, gint column,
210 GtkSheetCellAttr attributes;
211 PsppireVarStore *var_store ;
212 struct PsppireVariable *pv ;
214 g_return_val_if_fail(sheet != NULL, FALSE);
216 var_store = PSPPIRE_VAR_STORE(gtk_sheet_get_model(sheet));
218 if ( row >= psppire_var_store_get_var_cnt(var_store))
223 gtk_sheet_change_entry(sheet, GTK_TYPE_ENTRY);
228 gtk_sheet_get_attributes(sheet, row, column, &attributes);
230 pv = psppire_var_store_get_variable(var_store, row);
236 static GtkListStore *list_store = 0;
237 GtkComboBoxEntry *cbe;
238 gtk_sheet_change_entry(sheet, GTK_TYPE_COMBO_BOX_ENTRY);
240 GTK_COMBO_BOX_ENTRY(gtk_sheet_get_entry(sheet)->parent);
243 if ( ! list_store) list_store = create_label_list(alignments);
245 gtk_combo_box_set_model(GTK_COMBO_BOX(cbe),
246 GTK_TREE_MODEL(list_store));
248 gtk_combo_box_entry_set_text_column (cbe, 0);
251 g_signal_connect(G_OBJECT(cbe),"changed",
252 G_CALLBACK(change_alignment), pv);
257 static GtkListStore *list_store = 0;
258 GtkComboBoxEntry *cbe;
259 gtk_sheet_change_entry(sheet, GTK_TYPE_COMBO_BOX_ENTRY);
261 GTK_COMBO_BOX_ENTRY(gtk_sheet_get_entry(sheet)->parent);
264 if ( ! list_store) list_store = create_label_list(measures);
266 gtk_combo_box_set_model(GTK_COMBO_BOX(cbe),
267 GTK_TREE_MODEL(list_store));
269 gtk_combo_box_entry_set_text_column (cbe, 0);
271 g_signal_connect(G_OBJECT(cbe),"changed",
272 G_CALLBACK(change_measure), pv);
278 static struct val_labs_dialog *val_labs_dialog = 0;
280 PsppireCustomEntry *customEntry;
282 gtk_sheet_change_entry(sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
285 PSPPIRE_CUSTOM_ENTRY(gtk_sheet_get_entry(sheet));
288 if (!val_labs_dialog )
289 val_labs_dialog = val_labs_dialog_create(xml);
291 val_labs_dialog->pv = pv;
293 g_signal_connect_swapped(GTK_OBJECT(customEntry),
295 GTK_SIGNAL_FUNC(val_labs_dialog_show),
301 static struct missing_val_dialog *missing_val_dialog = 0;
302 PsppireCustomEntry *customEntry;
304 gtk_sheet_change_entry(sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
307 PSPPIRE_CUSTOM_ENTRY(gtk_sheet_get_entry(sheet));
309 if (!missing_val_dialog )
310 missing_val_dialog = missing_val_dialog_create(xml);
312 missing_val_dialog->pv = psppire_var_store_get_variable(var_store, row);
314 g_signal_connect_swapped(GTK_OBJECT(customEntry),
316 GTK_SIGNAL_FUNC(missing_val_dialog_show),
323 static struct var_type_dialog *var_type_dialog = 0;
325 PsppireCustomEntry *customEntry;
327 gtk_sheet_change_entry(sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
330 PSPPIRE_CUSTOM_ENTRY(gtk_sheet_get_entry(sheet));
333 /* Popup the Variable Type dialog box */
334 if (!var_type_dialog )
335 var_type_dialog = var_type_dialog_create(xml);
338 var_type_dialog->pv = pv;
340 g_signal_connect_swapped(GTK_OBJECT(customEntry),
342 GTK_SIGNAL_FUNC(var_type_dialog_show),
350 if ( attributes.is_editable)
354 const gchar *s = gtk_sheet_cell_get_text(sheet, row, column);
360 GtkSpinButton *spinButton ;
361 const gint current_value = atoi(s);
364 const struct fmt_spec *fmt = psppire_variable_get_write_spec(pv);
369 r_max = (psppire_variable_get_type(pv) == ALPHA) ? 255 : 40;
373 r_max = min(fmt->w - 1, 16);
377 r_max = 255 ; /* Is this a sensible value ? */
380 g_assert_not_reached();
383 adj = gtk_adjustment_new(current_value,
385 1.0, 1.0, 1.0 /* steps */
388 gtk_sheet_change_entry(sheet, GTK_TYPE_SPIN_BUTTON);
391 GTK_SPIN_BUTTON(gtk_sheet_get_entry(sheet));
393 gtk_spin_button_set_adjustment(spinButton, GTK_ADJUSTMENT(adj));
394 gtk_spin_button_set_digits(spinButton, 0);
401 gtk_sheet_change_entry(sheet, GTK_TYPE_ENTRY);
410 /* Create the var sheet */
412 psppire_variable_sheet_create (gchar *widget_name,
415 gint int1, gint int2)
420 GObject *geo = g_sheet_hetero_column_new(75, n_COLS);
421 GObject *row_geometry = g_sheet_uniform_row_new(25, n_initial_rows);
425 sheet = gtk_sheet_new(G_SHEET_ROW(row_geometry),
427 "variable sheet", 0);
429 g_signal_connect (GTK_OBJECT (sheet), "activate",
430 GTK_SIGNAL_FUNC (var_sheet_cell_change_entry),
433 g_signal_connect (GTK_OBJECT (sheet), "deactivate",
434 GTK_SIGNAL_FUNC (var_sheet_cell_change_entry),
437 g_signal_connect (GTK_OBJECT (sheet), "traverse",
438 GTK_SIGNAL_FUNC (traverse_cell_callback), 0);
441 g_signal_connect (GTK_OBJECT (sheet), "double-click-row",
442 GTK_SIGNAL_FUNC (click2row),
445 for (i = 0 ; i < n_COLS ; ++i )
447 g_sheet_hetero_column_set_button_label(G_SHEET_HETERO_COLUMN(geo), i,
448 column_def[i].label);
450 g_sheet_hetero_column_set_width(G_SHEET_HETERO_COLUMN(geo), i,
451 column_def[i].width);
454 gtk_widget_show(sheet);