8a2ba9855c6206a7494c8b893f90e8b845fa9986
[pspp-builds.git] / src / ui / gui / var-sheet.c
1 /* 
2    PSPPIRE --- A Graphical User Interface for PSPP
3    Copyright (C) 2004, 2005, 2006  Free Software Foundation
4    Written by John Darrington
5
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.
10
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.
15
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
19    02110-1301, USA. */
20
21
22 /* This module creates the Variable Sheet used for inputing the
23    variables in the  dictonary */
24
25 #include <value-labels.h>
26
27 #include <glade/glade.h>
28 #include <gtk/gtk.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define min(A,B) ((A < B)?A:B)
34
35 #include "gtksheet.h"
36 #include "gsheet-hetero-column.h"
37 #include "gsheet-uniform-row.h"
38 #include "psppire-var-store.h"
39
40 #include "helper.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"
47
48 #include "val-labs-dialog.h"
49 #include "missing-val-dialog.h"
50
51 #define _(A) A
52 #define N_(A) A
53
54
55 static const gint n_initial_rows = 40;
56
57
58 extern GladeXML *xml;
59
60 struct column_parameters
61 {
62   gchar label[20];  
63   gint width ;
64 };
65
66 static const struct column_parameters column_def[] = {
67   {N_("Name"),    80},
68   {N_("Type"),    100},
69   {N_("Width"),   57}, 
70   {N_("Decimals"),91}, 
71   {N_("Label"),   95}, 
72   {N_("Values"),  103},
73   {N_("Missing"), 95}, 
74   {N_("Columns"), 80}, 
75   {N_("Align"),   69}, 
76   {N_("Measure"), 99}, 
77 };
78
79
80 static gboolean
81 click2row(GtkWidget *w, gint row, gpointer data)
82 {
83   gint current_row, current_column;
84
85   select_sheet(PAGE_DATA_SHEET);
86   GtkWidget *data_sheet  = get_widget_assert(xml, "data_sheet");
87
88   gtk_sheet_get_active_cell(GTK_SHEET(data_sheet), 
89                             &current_row, &current_column);
90
91   gtk_sheet_set_active_cell(GTK_SHEET(data_sheet), current_row, row);
92
93   return FALSE;
94 }
95
96
97
98 const gchar *alignments[]={
99   _("Left"),
100   _("Right"),
101   _("Centre"),
102   0
103 };
104
105 const gchar *measures[]={
106   _("Nominal"),
107   _("Ordinal"),
108   _("Scale"),
109   0
110 };
111
112 static GtkListStore *
113 create_label_list(const gchar **labels)
114 {
115   gint i = 0;
116   GtkTreeIter iter;
117
118   GtkListStore *list_store;
119   list_store = gtk_list_store_new (1, G_TYPE_STRING);
120
121   const gchar *s;
122   while ( (s = labels[i++]))
123     {
124       gtk_list_store_append (list_store, &iter);
125       gtk_list_store_set (list_store, &iter,
126                           0, s,
127                           -1);
128     }
129         
130   return list_store;
131 }
132
133 /* Callback for when the alignment combo box 
134    item is selected */
135 static void        
136 change_alignment(GtkComboBox *cb,
137     gpointer user_data)
138 {
139   struct PsppireVariable *pv = user_data;
140   gint active_item = gtk_combo_box_get_active(cb);
141
142   if ( active_item < 0 ) return ;
143
144   psppire_variable_set_alignment(pv, active_item);
145 }
146
147
148
149 /* Callback for when the measure combo box 
150    item is selected */
151 static void        
152 change_measure(GtkComboBox *cb,
153     gpointer user_data)
154 {
155   struct PsppireVariable *pv = user_data;
156   gint active_item = gtk_combo_box_get_active(cb);
157
158   if ( active_item < 0 ) return ;
159
160   psppire_variable_set_measure(pv, active_item);
161 }
162
163
164
165 static gboolean 
166 traverse_cell_callback (GtkSheet * sheet, 
167                         gint row, gint column, 
168                         gint *new_row, gint *new_column
169                         )
170 {
171   PsppireVarStore *var_store = PSPPIRE_VAR_STORE(gtk_sheet_get_model(sheet));
172
173   gint n_vars = psppire_var_store_get_var_cnt(var_store);
174
175   if ( row == n_vars && *new_row >= n_vars)
176     {
177       GtkEntry *entry = GTK_ENTRY(gtk_sheet_get_entry(sheet));
178
179       const gchar *name = gtk_entry_get_text(entry);
180
181       if (! psppire_dict_check_name(var_store->dict, name, TRUE))
182         return FALSE;
183       
184       psppire_dict_insert_variable(var_store->dict, row, name);
185
186       return TRUE;
187     }
188
189   /* If the destination cell is outside the current  variables, then
190      accept the destination only as the name column of the first blank row
191   */
192   if ( *new_row > n_vars) 
193     return FALSE;
194   
195   if ( *new_row >= n_vars && *new_column != COL_NAME) 
196     return FALSE;
197
198   return TRUE;
199 }
200
201
202 /* Callback whenever the cell on the var sheet is entered or left.
203    It sets the entry box type appropriately.
204 */
205 static gboolean 
206 var_sheet_cell_change_entry (GtkSheet * sheet, gint row, gint column, 
207                              gpointer leaving)
208 {
209   g_return_val_if_fail(sheet != NULL, FALSE);
210
211   PsppireVarStore *var_store = PSPPIRE_VAR_STORE(gtk_sheet_get_model(sheet));
212
213   if ( row >= psppire_var_store_get_var_cnt(var_store))
214     return TRUE;
215
216   if ( leaving ) 
217     {
218       gtk_sheet_change_entry(sheet, GTK_TYPE_ENTRY);
219       return TRUE;
220     }
221
222   GtkSheetCellAttr attributes;
223   gtk_sheet_get_attributes(sheet, row, column, &attributes);
224
225   struct PsppireVariable *pv = psppire_var_store_get_variable(var_store, row);
226
227   switch (column)
228     {
229     case COL_ALIGN:
230       {
231         static GtkListStore *list_store = 0;
232         GtkComboBoxEntry *cbe;
233         gtk_sheet_change_entry(sheet, GTK_TYPE_COMBO_BOX_ENTRY);
234         cbe = 
235           GTK_COMBO_BOX_ENTRY(gtk_sheet_get_entry(sheet)->parent);
236
237
238         if ( ! list_store) list_store = create_label_list(alignments);
239
240         gtk_combo_box_set_model(GTK_COMBO_BOX(cbe), 
241                                 GTK_TREE_MODEL(list_store));
242
243         gtk_combo_box_entry_set_text_column (cbe, 0);
244
245
246         g_signal_connect(G_OBJECT(cbe),"changed", 
247                          G_CALLBACK(change_alignment), pv);
248       }
249       break;
250     case COL_MEASURE:
251       {
252         static GtkListStore *list_store = 0;
253         GtkComboBoxEntry *cbe;
254         gtk_sheet_change_entry(sheet, GTK_TYPE_COMBO_BOX_ENTRY);
255         cbe = 
256           GTK_COMBO_BOX_ENTRY(gtk_sheet_get_entry(sheet)->parent);
257
258
259         if ( ! list_store) list_store = create_label_list(measures);
260
261         gtk_combo_box_set_model(GTK_COMBO_BOX(cbe), 
262                                 GTK_TREE_MODEL(list_store));
263
264         gtk_combo_box_entry_set_text_column (cbe, 0);
265
266         g_signal_connect(G_OBJECT(cbe),"changed", 
267                          G_CALLBACK(change_measure), pv);
268       }
269       break;
270
271     case COL_VALUES:
272       {
273         static struct val_labs_dialog *val_labs_dialog = 0;
274
275         PsppireCustomEntry *customEntry;
276
277         gtk_sheet_change_entry(sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
278
279         customEntry = 
280           PSPPIRE_CUSTOM_ENTRY(gtk_sheet_get_entry(sheet));
281
282
283         if (!val_labs_dialog ) 
284             val_labs_dialog = val_labs_dialog_create(xml);
285
286         val_labs_dialog->pv = pv;
287
288         g_signal_connect_swapped(GTK_OBJECT(customEntry),
289                                  "clicked",
290                                  GTK_SIGNAL_FUNC(val_labs_dialog_show),
291                                  val_labs_dialog);
292       }
293       break;
294     case COL_MISSING:
295       {
296         static struct missing_val_dialog *missing_val_dialog = 0;
297         PsppireCustomEntry *customEntry;
298         
299         gtk_sheet_change_entry(sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
300
301         customEntry = 
302           PSPPIRE_CUSTOM_ENTRY(gtk_sheet_get_entry(sheet));
303
304         if (!missing_val_dialog ) 
305             missing_val_dialog = missing_val_dialog_create(xml);
306
307         missing_val_dialog->pv = psppire_var_store_get_variable(var_store, row);
308
309         g_signal_connect_swapped(GTK_OBJECT(customEntry),
310                                  "clicked",
311                                  GTK_SIGNAL_FUNC(missing_val_dialog_show),
312                                  missing_val_dialog);
313       }
314       break;
315
316     case COL_TYPE:
317       {
318         static struct var_type_dialog *var_type_dialog = 0;
319
320         PsppireCustomEntry *customEntry;
321
322         gtk_sheet_change_entry(sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
323
324         customEntry = 
325           PSPPIRE_CUSTOM_ENTRY(gtk_sheet_get_entry(sheet));
326
327
328         /* Popup the Variable Type dialog box */
329         if (!var_type_dialog ) 
330             var_type_dialog = var_type_dialog_create(xml);
331
332
333         var_type_dialog->pv = pv;
334
335         g_signal_connect_swapped(GTK_OBJECT(customEntry),
336                                  "clicked",
337                                  GTK_SIGNAL_FUNC(var_type_dialog_show),
338                                  var_type_dialog);
339       }
340       break;
341     case COL_WIDTH:
342     case COL_DECIMALS:
343     case COL_COLUMNS:
344       {
345         if ( attributes.is_editable) 
346           {
347             gint r_min, r_max;
348
349             const gchar *s = gtk_sheet_cell_get_text(sheet, row, column);
350
351             if (!s) 
352               return FALSE;
353
354             const gint current_value  = atoi(s);
355
356             const struct fmt_spec *fmt = psppire_variable_get_write_spec(pv);
357             switch (column) 
358               {
359               case COL_WIDTH:
360                 r_min = fmt->d + 1;
361                 r_max = (psppire_variable_get_type(pv) == ALPHA) ? 255 : 40;
362                 break;
363               case COL_DECIMALS:
364                 r_min = 0 ; 
365                 r_max = min(fmt->w - 1, 16);
366                 break;
367               case COL_COLUMNS:
368                 r_min = 1;
369                 r_max = 255 ; /* Is this a sensible value ? */
370                 break;
371               default:
372                 g_assert_not_reached();
373               }
374
375             GtkObject *adj =  
376               gtk_adjustment_new(current_value,
377                                  r_min, r_max,
378                                  1.0, 1.0, 1.0 /* steps */
379                                  );
380
381             gtk_sheet_change_entry(sheet, GTK_TYPE_SPIN_BUTTON);
382
383             GtkSpinButton *spinButton = 
384               GTK_SPIN_BUTTON(gtk_sheet_get_entry(sheet));
385
386             gtk_spin_button_set_adjustment(spinButton, GTK_ADJUSTMENT(adj));
387             gtk_spin_button_set_digits(spinButton, 0);
388           }
389       }
390       break; 
391
392     default:
393       gtk_sheet_change_entry(sheet, GTK_TYPE_ENTRY);
394       break;
395     }
396
397   return TRUE;
398 }
399
400
401
402 /* Create the var sheet */
403 GtkWidget*
404 psppire_variable_sheet_create (gchar *widget_name, 
405                                gchar *string1, 
406                                gchar *string2,
407                                gint int1, gint int2)
408 {
409   gint i;
410   GtkWidget *sheet;
411
412   GObject *geo = g_sheet_hetero_column_new(75, n_COLS);
413   GObject *row_geometry = g_sheet_uniform_row_new(25, n_initial_rows); 
414
415
416
417   sheet = gtk_sheet_new(G_SHEET_ROW(row_geometry),
418                         G_SHEET_COLUMN(geo), 
419                         "variable sheet", 0); 
420
421   g_signal_connect (GTK_OBJECT (sheet), "activate",
422                     GTK_SIGNAL_FUNC (var_sheet_cell_change_entry),
423                     0);
424
425   g_signal_connect (GTK_OBJECT (sheet), "deactivate",
426                     GTK_SIGNAL_FUNC (var_sheet_cell_change_entry),
427                     (void *) 1);
428
429   g_signal_connect (GTK_OBJECT (sheet), "traverse",
430                     GTK_SIGNAL_FUNC (traverse_cell_callback), 0);
431
432
433   g_signal_connect (GTK_OBJECT (sheet), "double-click-row",
434                     GTK_SIGNAL_FUNC (click2row),
435                     sheet);
436
437   for (i = 0 ; i < n_COLS ; ++i ) 
438     {
439       g_sheet_hetero_column_set_button_label(G_SHEET_HETERO_COLUMN(geo), i, 
440                                                column_def[i].label);
441
442       g_sheet_hetero_column_set_width(G_SHEET_HETERO_COLUMN(geo), i, 
443                                                column_def[i].width);
444     }
445
446   gtk_widget_show(sheet);
447
448   return sheet;
449 }
450
451