2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 2005, 2006 Free Software Foundation
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 2 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, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 /* This module creates the Variable Sheet used for inputing the
22 variables in the dictonary */
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
29 #include <data/value-labels.h>
31 #include <glade/glade.h>
38 #include <data/value.h>
40 #include <gtksheet/gtksheet.h>
41 #include <gtksheet/gsheet-hetero-column.h>
42 #include <gtksheet/gsheet-uniform-row.h>
44 #include "psppire-var-store.h"
46 #include "psppire-dict.h"
47 #include "var-type-dialog.h"
48 #include "var-sheet.h"
49 #include "customentry.h"
51 #include "val-labs-dialog.h"
52 #include "missing-val-dialog.h"
56 static const gint n_initial_rows = 40;
60 struct column_parameters
66 static const struct column_parameters column_def[] = {
81 const gchar *const alignments[n_ALIGNMENTS + 1]={
88 const gchar *const measures[n_MEASURES + 1]={
96 create_label_list (const gchar *const *labels)
102 GtkListStore *list_store;
103 list_store = gtk_list_store_new (1, G_TYPE_STRING);
106 while ( (s = labels[i++]))
108 gtk_list_store_append (list_store, &iter);
109 gtk_list_store_set (list_store, &iter,
117 /* Callback for when the alignment combo box
120 change_alignment (GtkComboBox *cb,
123 struct variable *pv = user_data;
124 gint active_item = gtk_combo_box_get_active (cb);
126 if ( active_item < 0 ) return ;
128 var_set_alignment (pv, active_item);
133 /* Callback for when the measure combo box
136 change_measure (GtkComboBox *cb,
139 struct variable *pv = user_data;
140 gint active_item = gtk_combo_box_get_active (cb);
142 if ( active_item < 0 ) return ;
144 var_set_measure (pv, active_item);
150 traverse_cell_callback (GtkSheet * sheet,
151 gint row, gint column,
152 gint *new_row, gint *new_column
155 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
157 gint n_vars = psppire_var_store_get_var_cnt (var_store);
159 if ( row == n_vars && *new_row >= n_vars)
161 GtkEntry *entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
163 const gchar *name = gtk_entry_get_text (entry);
165 if (! psppire_dict_check_name (var_store->dict, name, TRUE))
168 psppire_dict_insert_variable (var_store->dict, row, name);
173 /* If the destination cell is outside the current variables, then
174 automatically create variables for the new rows.
176 if ( (*new_row > n_vars) ||
177 (*new_row == n_vars && *new_column != COL_NAME) )
180 for ( i = n_vars ; i <= *new_row; ++i )
181 psppire_dict_insert_variable (var_store->dict, i, NULL);
191 Callback whenever the cell on the var sheet is left
194 var_sheet_cell_entry_leave (GtkSheet * sheet, gint row, gint column,
197 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
204 Callback whenever the cell on the var sheet is entered.
207 var_sheet_cell_entry_enter (GtkSheet * sheet, gint row, gint column,
210 GtkSheetCellAttr attributes;
211 PsppireVarStore *var_store ;
212 struct variable *pv ;
216 g_return_val_if_fail (sheet != NULL, FALSE);
218 var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
220 g_assert (var_store);
222 if ( row >= psppire_var_store_get_var_cnt (var_store))
225 xml = glade_xml_new (PKGDATADIR "/data-editor.glade", NULL, NULL);
227 gtk_sheet_get_attributes (sheet, row, column, &attributes);
229 pv = psppire_var_store_get_var (var_store, row);
235 static GtkListStore *list_store = 0;
236 GtkComboBoxEntry *cbe;
237 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
239 GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
242 if ( ! list_store) list_store = create_label_list (alignments);
244 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
245 GTK_TREE_MODEL (list_store));
247 gtk_combo_box_entry_set_text_column (cbe, 0);
250 g_signal_connect (G_OBJECT (cbe),"changed",
251 G_CALLBACK (change_alignment), pv);
256 static GtkListStore *list_store = 0;
257 GtkComboBoxEntry *cbe;
258 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
260 GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
263 if ( ! list_store) list_store = create_label_list (measures);
265 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
266 GTK_TREE_MODEL (list_store));
268 gtk_combo_box_entry_set_text_column (cbe, 0);
270 g_signal_connect (G_OBJECT (cbe),"changed",
271 G_CALLBACK (change_measure), pv);
277 static struct val_labs_dialog *val_labs_dialog = 0;
279 PsppireCustomEntry *customEntry;
281 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
284 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
287 if (!val_labs_dialog )
288 val_labs_dialog = val_labs_dialog_create (xml);
290 val_labs_dialog_set_target_variable (val_labs_dialog, pv);
292 g_signal_connect_swapped (GTK_OBJECT (customEntry),
294 GTK_SIGNAL_FUNC (val_labs_dialog_show),
300 static struct missing_val_dialog *missing_val_dialog = 0;
301 PsppireCustomEntry *customEntry;
303 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
306 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
308 if (!missing_val_dialog )
309 missing_val_dialog = missing_val_dialog_create (xml);
311 missing_val_dialog->pv = psppire_var_store_get_var (var_store, row);
313 g_signal_connect_swapped (GTK_OBJECT (customEntry),
315 GTK_SIGNAL_FUNC (missing_val_dialog_show),
322 static struct var_type_dialog *var_type_dialog = 0;
324 PsppireCustomEntry *customEntry;
326 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
329 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
332 /* Popup the Variable Type dialog box */
333 if (!var_type_dialog )
334 var_type_dialog = var_type_dialog_create (xml);
337 var_type_dialog->pv = pv;
339 g_signal_connect_swapped (GTK_OBJECT (customEntry),
341 GTK_SIGNAL_FUNC (var_type_dialog_show),
349 if ( attributes.is_editable)
353 const gchar *s = gtk_sheet_cell_get_text (sheet, row, column);
357 GtkSpinButton *spinButton ;
358 const gint current_value = atoi (s);
361 const struct fmt_spec *fmt = var_get_write_format (pv);
365 r_min = MAX (fmt->d + 1, fmt_min_output_width (fmt->type));
366 r_max = fmt_max_output_width (fmt->type);
370 r_max = fmt_max_output_decimals (fmt->type, fmt->w);
374 r_max = 255 ; /* Is this a sensible value ? */
377 g_assert_not_reached ();
380 adj = gtk_adjustment_new (current_value,
382 1.0, 1.0, 1.0 /* steps */
385 gtk_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
388 GTK_SPIN_BUTTON (gtk_sheet_get_entry (sheet));
390 gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
391 gtk_spin_button_set_digits (spinButton, 0);
398 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
403 g_object_unref (xml);
409 extern PsppireVarStore *the_var_store;
412 /* Create the var sheet */
413 G_MODULE_EXPORT GtkWidget*
414 psppire_variable_sheet_create (gchar *widget_name,
417 gint int1, gint int2)
423 GObject *geo = g_sheet_hetero_column_new (75, n_COLS);
425 g_assert (the_var_store);
427 sheet = gtk_sheet_new (G_SHEET_ROW (the_var_store),
428 G_SHEET_COLUMN (geo),
429 "variable sheet", 0);
432 g_signal_connect (GTK_OBJECT (sheet), "activate",
433 GTK_SIGNAL_FUNC (var_sheet_cell_entry_enter),
436 g_signal_connect (GTK_OBJECT (sheet), "deactivate",
437 GTK_SIGNAL_FUNC (var_sheet_cell_entry_leave),
440 g_signal_connect (GTK_OBJECT (sheet), "traverse",
441 GTK_SIGNAL_FUNC (traverse_cell_callback), 0);
444 gtk_sheet_set_model (GTK_SHEET (sheet), G_SHEET_MODEL (the_var_store));
447 /* Since this happens inside glade_xml_new, we must prevent strings from
448 * being re-encoded twice */
449 codeset = bind_textdomain_codeset (PACKAGE, 0);
450 bind_textdomain_codeset (PACKAGE, nl_langinfo (CODESET));
451 for (i = 0 ; i < n_COLS ; ++i )
453 g_sheet_hetero_column_set_button_label (G_SHEET_HETERO_COLUMN (geo), i,
454 gettext (column_def[i].label));
456 g_sheet_hetero_column_set_width (G_SHEET_HETERO_COLUMN (geo), i,
457 column_def[i].width);
459 bind_textdomain_codeset (PACKAGE, codeset);
461 gtk_widget_show (sheet);