1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation
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 /* This module creates the Variable Sheet used for inputing the
19 variables in the dictonary */
23 #define _(msgid) gettext (msgid)
24 #define N_(msgid) msgid
26 #include <data/value-labels.h>
28 #include <glade/glade.h>
34 #include <data/value.h>
36 #include <gtksheet/gtksheet.h>
37 #include <gtksheet/gsheet-hetero-column.h>
38 #include <gtksheet/gsheet-uniform-row.h>
40 #include "localcharset.h"
42 #include "psppire-var-store.h"
44 #include "psppire-dict.h"
45 #include "var-type-dialog.h"
46 #include "var-sheet.h"
47 #include "customentry.h"
49 #include "val-labs-dialog.h"
50 #include "missing-val-dialog.h"
54 static const gint n_initial_rows = 40;
58 struct column_parameters
64 static const struct column_parameters column_def[] = {
79 const gchar *const alignments[n_ALIGNMENTS + 1]={
86 const gchar *const measures[n_MEASURES + 1]={
94 create_label_list (const gchar *const *labels)
100 GtkListStore *list_store;
101 list_store = gtk_list_store_new (1, G_TYPE_STRING);
104 while ( (s = labels[i++]))
106 gtk_list_store_append (list_store, &iter);
107 gtk_list_store_set (list_store, &iter,
115 /* Callback for when the alignment combo box
118 change_alignment (GtkComboBox *cb,
121 struct variable *pv = user_data;
122 gint active_item = gtk_combo_box_get_active (cb);
124 if ( active_item < 0 ) return ;
126 var_set_alignment (pv, active_item);
131 /* Callback for when the measure combo box
134 change_measure (GtkComboBox *cb,
137 struct variable *pv = user_data;
138 gint active_item = gtk_combo_box_get_active (cb);
140 if ( active_item < 0 ) return ;
142 var_set_measure (pv, active_item);
148 traverse_cell_callback (GtkSheet * sheet,
149 gint row, gint column,
150 gint *new_row, gint *new_column
153 PsppireVarStore *var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
155 gint n_vars = psppire_var_store_get_var_cnt (var_store);
157 if ( row == n_vars && *new_row >= n_vars)
159 GtkEntry *entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
161 const gchar *name = gtk_entry_get_text (entry);
163 if (! psppire_dict_check_name (var_store->dict, name, TRUE))
166 psppire_dict_insert_variable (var_store->dict, row, name);
171 /* If the destination cell is outside the current variables, then
172 automatically create variables for the new rows.
174 if ( (*new_row > n_vars) ||
175 (*new_row == n_vars && *new_column != COL_NAME) )
178 for ( i = n_vars ; i <= *new_row; ++i )
179 psppire_dict_insert_variable (var_store->dict, i, NULL);
189 Callback whenever the cell on the var sheet is left
192 var_sheet_cell_entry_leave (GtkSheet * sheet, gint row, gint column,
195 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
202 Callback whenever the cell on the var sheet is entered.
205 var_sheet_cell_entry_enter (GtkSheet * sheet, gint row, gint column,
208 GtkSheetCellAttr attributes;
209 PsppireVarStore *var_store ;
210 struct variable *var ;
214 g_return_val_if_fail (sheet != NULL, FALSE);
216 var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
218 g_assert (var_store);
220 if ( row >= psppire_var_store_get_var_cnt (var_store))
223 xml = XML_NEW ("data-editor.glade");
225 gtk_sheet_get_attributes (sheet, row, column, &attributes);
227 var = psppire_var_store_get_var (var_store, row);
233 static GtkListStore *list_store = NULL;
234 GtkComboBoxEntry *cbe;
235 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
237 GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
240 if ( ! list_store) list_store = create_label_list (alignments);
242 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
243 GTK_TREE_MODEL (list_store));
245 gtk_combo_box_entry_set_text_column (cbe, 0);
248 g_signal_connect (G_OBJECT (cbe),"changed",
249 G_CALLBACK (change_alignment), var);
255 static GtkListStore *list_store = 0;
256 GtkComboBoxEntry *cbe;
257 gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
259 GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
262 if ( ! list_store) list_store = create_label_list (measures);
264 gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
265 GTK_TREE_MODEL (list_store));
267 gtk_combo_box_entry_set_text_column (cbe, 0);
269 g_signal_connect (G_OBJECT (cbe),"changed",
270 G_CALLBACK (change_measure), var);
276 static struct val_labs_dialog *val_labs_dialog = NULL;
278 PsppireCustomEntry *customEntry;
280 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
283 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
285 if ( var_is_long_string (var))
286 g_object_set (customEntry,
290 if (!val_labs_dialog )
291 val_labs_dialog = val_labs_dialog_create (xml);
293 val_labs_dialog_set_target_variable (val_labs_dialog, var);
295 g_signal_connect_swapped (GTK_OBJECT (customEntry),
297 GTK_SIGNAL_FUNC (val_labs_dialog_show),
304 static struct missing_val_dialog *missing_val_dialog = 0;
305 PsppireCustomEntry *customEntry;
307 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
310 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
312 if ( var_is_long_string (var))
313 g_object_set (customEntry,
317 if (!missing_val_dialog )
318 missing_val_dialog = missing_val_dialog_create (xml);
320 missing_val_dialog->pv = psppire_var_store_get_var (var_store, row);
322 g_signal_connect_swapped (GTK_OBJECT (customEntry),
324 GTK_SIGNAL_FUNC (missing_val_dialog_show),
331 static struct var_type_dialog *var_type_dialog = 0;
333 PsppireCustomEntry *customEntry;
335 gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
338 PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
341 /* Popup the Variable Type dialog box */
342 if (!var_type_dialog )
343 var_type_dialog = var_type_dialog_create (xml);
346 var_type_dialog->pv = var;
348 g_signal_connect_swapped (GTK_OBJECT (customEntry),
350 GTK_SIGNAL_FUNC (var_type_dialog_show),
359 if ( attributes.is_editable)
363 const gchar *s = gtk_sheet_cell_get_text (sheet, row, column);
367 GtkSpinButton *spinButton ;
368 const gint current_value = atoi (s);
371 const struct fmt_spec *fmt = var_get_write_format (var);
375 r_min = MAX (fmt->d + 1, fmt_min_output_width (fmt->type));
376 r_max = fmt_max_output_width (fmt->type);
380 r_max = fmt_max_output_decimals (fmt->type, fmt->w);
384 r_max = 255 ; /* Is this a sensible value ? */
387 g_assert_not_reached ();
390 adj = gtk_adjustment_new (current_value,
392 1.0, 1.0, 1.0 /* steps */
395 gtk_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
398 GTK_SPIN_BUTTON (gtk_sheet_get_entry (sheet));
400 gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
401 gtk_spin_button_set_digits (spinButton, 0);
408 gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
413 g_object_unref (xml);
419 extern PsppireVarStore *the_var_store;
422 /* Create the var sheet */
423 G_MODULE_EXPORT GtkWidget*
424 psppire_variable_sheet_create (gchar *widget_name,
427 gint int1, gint int2)
433 GObject *geo = g_sheet_hetero_column_new (75, n_COLS);
435 g_assert (the_var_store);
437 sheet = gtk_sheet_new (G_SHEET_ROW (the_var_store),
438 G_SHEET_COLUMN (geo),
439 "variable sheet", 0);
442 g_signal_connect (GTK_OBJECT (sheet), "activate",
443 GTK_SIGNAL_FUNC (var_sheet_cell_entry_enter),
446 g_signal_connect (GTK_OBJECT (sheet), "deactivate",
447 GTK_SIGNAL_FUNC (var_sheet_cell_entry_leave),
450 g_signal_connect (GTK_OBJECT (sheet), "traverse",
451 GTK_SIGNAL_FUNC (traverse_cell_callback), 0);
454 gtk_sheet_set_model (GTK_SHEET (sheet), G_SHEET_MODEL (the_var_store));
457 /* Since this happens inside glade_xml_new, we must prevent strings from
458 * being re-encoded twice */
459 codeset = xstrdup (bind_textdomain_codeset (PACKAGE, 0));
460 bind_textdomain_codeset (PACKAGE, locale_charset ());
462 for (i = 0 ; i < n_COLS ; ++i )
464 g_sheet_hetero_column_set_button_label (G_SHEET_HETERO_COLUMN (geo), i,
465 gettext (column_def[i].label));
467 g_sheet_hetero_column_set_width (G_SHEET_HETERO_COLUMN (geo), i,
468 column_def[i].width);
471 bind_textdomain_codeset (PACKAGE, codeset);
474 gtk_widget_show (sheet);