Cleaned up GUI, by objectifying the data editor. Removed a number of global variables.
[pspp-builds.git] / src / ui / gui / var-type-dialog.c
1 /* 
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2005, 2006  Free Software Foundation
4
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.
9
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.
14
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
18     02110-1301, USA. */
19
20
21 /*  This module describes the behaviour of the Variable Type dialog box used
22     for inputing the variable type in the var sheet */
23
24 #include <config.h>
25
26 #include <gtk/gtk.h>
27 #include <glade/glade.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "var-type-dialog.h"
33
34 #include "helper.h"
35
36 #include <data/variable.h>
37 #include <data/settings.h>
38 #include <libpspp/message.h>
39
40
41 struct tgs
42 {
43   struct var_type_dialog *dialog;
44   gint button;
45 };
46
47
48 struct format_opt {
49   gchar desc[18];
50   struct fmt_spec spec;
51 };
52
53
54 static const struct format_opt format_option[] =
55   {
56     { "dd-mmm-yyyy", {FMT_DATE,  11, 0} },
57     { "dd-mmm-yy",   {FMT_DATE,   9, 0} },
58     { "mm/dd/yyyy",  {FMT_ADATE, 10, 0} },
59     { "mm/dd/yy",    {FMT_ADATE, 8, 0} },
60     { "dd.mm.yyyy",  {FMT_EDATE, 10, 0} },
61     { "dd.mm.yy",    {FMT_EDATE, 8, 0} },
62     { "yyyy/mm/dd",  {FMT_SDATE, 10, 0} },
63     { "yy/mm/dd",    {FMT_SDATE, 8, 0} },
64     { "yyddd",       {FMT_JDATE, 5, 0} },
65     { "yyyyddd",     {FMT_JDATE, 7, 0} },
66     { "q Q yyyy",    {FMT_QYR, 8, 0} },
67     { "q Q yy",      {FMT_QYR, 6, 0} },
68     { "mmm yyyy",    {FMT_MOYR, 8, 0} },
69     { "mmm yy",      {FMT_MOYR, 6, 0} },
70     { "dd WK yyyy",  {FMT_WKYR, 10, 0} },
71     { "dd WK yy",    {FMT_WKYR, 8, 0} },
72     { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}}
73   };
74
75
76 static const struct fmt_spec dollar_format[] = 
77   {
78     {FMT_DOLLAR, 2, 0},
79     {FMT_DOLLAR, 3, 0},
80     {FMT_DOLLAR, 4, 0},
81     {FMT_DOLLAR, 7, 2},
82     {FMT_DOLLAR, 6, 0},
83     {FMT_DOLLAR, 9, 2},
84     {FMT_DOLLAR, 8, 0},
85     {FMT_DOLLAR, 11, 2},
86     {FMT_DOLLAR, 12, 0},
87     {FMT_DOLLAR, 15, 2},
88     {FMT_DOLLAR, 16, 0},
89     {FMT_DOLLAR, 19, 2}
90   };
91
92 static const int cc_format[] = 
93   {
94     FMT_CCA, 
95     FMT_CCB, 
96     FMT_CCC, 
97     FMT_CCD, 
98     FMT_CCE, 
99   };
100
101
102 static void select_treeview_from_format
103  (GtkTreeView *treeview, const struct fmt_spec *fmt);
104
105 static void select_treeview_from_format_type(GtkTreeView *treeview, 
106                                              const int fmt_type);
107
108
109 /* callback for when any of the radio buttons are toggled */
110 static void        
111 on_toggle_1(GtkToggleButton *togglebutton, gpointer user_data)
112 {
113   struct tgs *tgs = user_data;
114
115   if ( gtk_toggle_button_get_active(togglebutton) == FALSE) 
116     return ;
117
118   tgs->dialog->active_button = tgs->button;
119 }
120
121 static void update_width_decimals(const struct var_type_dialog *dialog);
122
123 #define force_max(x, val) if (x > val) x = val
124
125 /* 
126    Set the local format from the variable
127    and force them to have sensible values */
128 static void
129 set_local_width_decimals(struct var_type_dialog *dialog)
130 {
131   dialog->fmt_l = * var_get_write_format (dialog->pv);
132
133   switch (dialog->active_button) 
134     {
135     case BUTTON_STRING:
136       force_max( dialog->fmt_l.w, 255);
137       break;
138     default:
139       force_max( dialog->fmt_l.w, 40);
140       force_max( dialog->fmt_l.d, 16);
141       break;
142     }
143 }
144
145
146 /* callback for when any of the radio buttons are toggled */
147 static void        
148 on_toggle_2(GtkToggleButton *togglebutton, gpointer user_data)
149 {
150   struct var_type_dialog *dialog = user_data;
151   if ( gtk_toggle_button_get_active(togglebutton) == FALSE) 
152     {
153       switch (dialog->active_button) 
154         {
155         case BUTTON_DATE:
156           gtk_widget_hide(dialog->date_format_list);
157           break;
158         case BUTTON_CUSTOM:
159           gtk_widget_hide(dialog->custom_currency_hbox);
160           break;
161         case BUTTON_DOLLAR:
162           gtk_widget_hide(dialog->dollar_window);
163           break;
164         case BUTTON_STRING:
165           gtk_widget_show(dialog->label_decimals);
166           gtk_widget_show(dialog->entry_decimals);
167           break;
168         }
169       return ;
170     }
171
172   set_local_width_decimals(dialog);
173   update_width_decimals(dialog);
174
175   switch (dialog->active_button) 
176     {
177     case BUTTON_STRING:
178       gtk_widget_show(dialog->width_decimals);
179       gtk_widget_hide(dialog->label_decimals);
180       gtk_widget_hide(dialog->entry_decimals);
181       break;
182     case BUTTON_DATE:
183       select_treeview_from_format(dialog->date_format_treeview,
184                                   &format_option[0].spec);
185       gtk_widget_hide(dialog->width_decimals);
186       gtk_widget_show(dialog->date_format_list);
187       break;
188     case BUTTON_DOLLAR:
189       select_treeview_from_format(dialog->dollar_treeview,
190                                   &dollar_format[0]);
191       gtk_widget_show(dialog->dollar_window);
192       gtk_widget_show_all(dialog->width_decimals);
193       break;
194     case BUTTON_CUSTOM:
195       select_treeview_from_format_type(dialog->custom_treeview,
196                                   cc_format[0]);
197
198       gtk_widget_show(dialog->width_decimals);
199       gtk_widget_show(dialog->custom_currency_hbox);
200       break;
201     default:
202       gtk_widget_show_all(dialog->width_decimals);
203       break;
204     }
205 }
206
207
208
209 static gint on_var_type_ok_clicked(GtkWidget *w, gpointer data);
210
211 #define LEN 20
212
213 static void
214 add_to_group(GtkWidget *w, gpointer data)
215 {
216   GtkSizeGroup *sg = data;
217   
218   gtk_size_group_add_widget(sg, w);
219 }
220
221 /* Set the local width and decimals entry boxes to reflec the local format */
222 static void
223 update_width_decimals(const struct var_type_dialog *dialog)
224 {
225   gchar *text;
226   g_assert(dialog);
227
228   text = g_strdup_printf("%d", dialog->fmt_l.w);
229   gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
230   g_free(text);
231
232   text = g_strdup_printf("%d", dialog->fmt_l.d);
233   gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
234   g_free(text);
235 }
236
237 /* Callback for when the custom treeview row is changed.
238    It sets dialog box to reflect the selected format */
239 static void
240 preview_custom(GtkWidget *w, gpointer data)
241 {
242   const gchar *text ;
243
244   struct var_type_dialog *dialog = data;
245
246   if ( dialog->active_button != BUTTON_CUSTOM ) 
247     return;
248
249   text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
250   dialog->fmt_l.d = atoi(text);
251
252   text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
253   dialog->fmt_l.w = atoi(text);
254
255   msg_disable ();
256   if ( ! fmt_check_output(&dialog->fmt_l))
257     {
258       gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
259       gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
260     }
261   else
262     {
263       gchar *sample_text;
264       union value v;
265       v.f = 1234.56;
266
267       sample_text = value_to_text(v, dialog->fmt_l);
268       gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
269       g_free(sample_text);
270
271       v.f = -v.f;
272       sample_text = value_to_text(v, dialog->fmt_l);
273       gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
274       g_free(sample_text);
275     }
276   msg_enable ();
277 }
278
279 /* Callback for when a treeview row is changed.
280    It sets the fmt_l_spec to reflect the selected format */
281 static void
282 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
283 {
284   struct var_type_dialog *dialog = data;
285   GtkTreeIter iter ;
286   GValue the_value = {0}; 
287
288   GtkTreeSelection* sel =  gtk_tree_view_get_selection(treeview);
289
290   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
291
292   gtk_tree_selection_get_selected (sel, &model, &iter);
293
294   gtk_tree_model_get_value(model, &iter, 1, &the_value);
295
296   dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
297 }
298
299
300 /* Callback for when a treeview row is changed.
301    It sets the type of the fmt_l to reflect the selected type */
302 static void
303 set_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
304 {
305   static struct fmt_spec custom_format = {0,0,0};
306   struct var_type_dialog *dialog = data;
307   GtkTreeIter iter ;
308   GValue the_value = {0}; 
309
310   GtkTreeSelection* sel =  gtk_tree_view_get_selection(treeview);
311
312   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
313
314   gtk_tree_selection_get_selected (sel, &model, &iter);
315
316   gtk_tree_model_get_value(model, &iter, 1, &the_value);
317
318   dialog->fmt_l = custom_format;
319   dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
320
321 }
322
323
324
325
326 /* Create the structure from the XML definitions */
327 struct var_type_dialog *
328 var_type_dialog_create (GladeXML *xml)
329 {
330   gint i;
331   struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
332
333   g_assert(xml);
334
335   dialog->window = get_widget_assert(xml,"var_type_dialog");
336
337   gtk_window_set_transient_for(GTK_WINDOW(dialog->window), 
338                                GTK_WINDOW(get_widget_assert(xml, "data_editor")));
339
340   dialog->radioButton[BUTTON_NUMERIC] = 
341     get_widget_assert(xml,"radiobutton1");
342   dialog->radioButton[BUTTON_COMMA] =   
343     get_widget_assert(xml,"radiobutton2");
344   dialog->radioButton[BUTTON_DOT] =     
345     get_widget_assert(xml,"radiobutton3");
346   dialog->radioButton[BUTTON_SCIENTIFIC] = 
347     get_widget_assert(xml,"radiobutton4");
348   dialog->radioButton[BUTTON_DATE] =   
349     get_widget_assert(xml,"radiobutton5");
350   dialog->radioButton[BUTTON_DOLLAR] = 
351     get_widget_assert(xml,"radiobutton6");
352   dialog->radioButton[BUTTON_CUSTOM] = 
353     get_widget_assert(xml,"radiobutton7");
354   dialog->radioButton[BUTTON_STRING] = 
355     get_widget_assert(xml,"radiobutton8");
356
357
358   dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
359   dialog->width_decimals = get_widget_assert(xml, "width_decimals");
360   dialog->label_decimals = get_widget_assert(xml, "decimals_label");
361   dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
362
363   dialog->label_psample = get_widget_assert(xml, "psample_label");
364   dialog->label_nsample = get_widget_assert(xml, "nsample_label");
365
366
367   dialog->entry_width = get_widget_assert(xml,"width_entry");
368
369   dialog->custom_currency_hbox = get_widget_assert(xml,
370                                                    "custom_currency_hbox");
371
372   dialog->dollar_window = get_widget_assert(xml, "dollar_window");
373   dialog->dollar_treeview = 
374     GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
375
376   dialog->custom_treeview = 
377     GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
378
379
380   dialog->ok = get_widget_assert(xml,"var_type_ok");
381
382
383   {
384   GtkTreeIter iter;
385   GtkListStore *list_store ;
386
387   GtkTreeViewColumn *column;
388   GtkCellRenderer *renderer ;
389
390   static struct tgs tgs[num_BUTTONS];
391   /* The "middle_box" is a vbox with serveral children.
392      However only one child is ever shown at a time.
393      We need to make sure that they all have the same width, to avoid
394      upleasant resizing effects */
395   GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
396
397   gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")), 
398                         add_to_group, sizeGroup);
399
400
401   for (i = 0 ; i < num_BUTTONS; ++i ) 
402     {
403       tgs[i].dialog = dialog;
404       tgs[i].button = i;
405       g_signal_connect(dialog->radioButton[i], "toggled", 
406                        G_CALLBACK(on_toggle_1), &tgs[i]);
407
408       g_signal_connect(dialog->radioButton[i], "toggled", 
409                        G_CALLBACK(on_toggle_2), dialog);
410     }
411
412   /* Populate the date format tree view */
413   dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml, 
414                                               "date_format_list_view"));
415
416   renderer = gtk_cell_renderer_text_new();
417   
418   column = gtk_tree_view_column_new_with_attributes ("Title",
419                                                      renderer,
420                                                      "text",
421                                                      0,
422                                                      NULL);
423
424   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview), 
425                                column);
426
427
428   list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
429
430   for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i ) 
431     {
432       gtk_list_store_append (list_store, &iter);
433       gtk_list_store_set (list_store, &iter,
434                           0, format_option[i].desc,
435                           1, &format_option[i].spec,
436                           -1);
437     }
438
439   gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview), 
440                           GTK_TREE_MODEL(list_store));
441
442   g_object_unref(list_store);
443
444   g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
445                    GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
446
447
448   /* populate the dollar treeview */
449
450   renderer = gtk_cell_renderer_text_new();
451   
452   column = gtk_tree_view_column_new_with_attributes ("Title",
453                                                      renderer,
454                                                      "text",
455                                                      0,
456                                                      NULL);
457
458   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview), 
459                                column);
460
461
462   list_store = gtk_list_store_new (2, G_TYPE_STRING, 
463                                                  G_TYPE_POINTER);
464
465   for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i ) 
466     {
467       char *template = fmt_dollar_template (&dollar_format[i]);
468       gtk_list_store_append (list_store, &iter);
469       gtk_list_store_set (list_store, &iter,
470                           0, template,
471                           1, &dollar_format[i],
472                           -1);
473       free (template);
474     }
475
476   gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview), 
477                           GTK_TREE_MODEL(list_store));
478
479   g_object_unref(list_store);
480
481   g_signal_connect(GTK_OBJECT(dialog->dollar_treeview), 
482                    "cursor-changed",
483                    GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
484
485   g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview), 
486                    "cursor-changed",
487                    GTK_SIGNAL_FUNC(update_width_decimals), dialog);
488
489
490   /* populate the custom treeview */
491
492   renderer = gtk_cell_renderer_text_new();
493   
494   column = gtk_tree_view_column_new_with_attributes ("Title",
495                                                      renderer,
496                                                      "text",
497                                                      0,
498                                                      NULL);
499
500   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview), 
501                                column);
502
503
504   list_store = gtk_list_store_new (2, G_TYPE_STRING, 
505                                                  G_TYPE_POINTER);
506
507   for ( i = 0 ; i < 5 ; ++i ) 
508     {
509       enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
510       gchar text[4];
511       g_snprintf(text, 4, "%s", fmt_name (cc_fmts[i]));
512       gtk_list_store_append (list_store, &iter);
513       gtk_list_store_set (list_store, &iter,
514                           0, text,
515                           1, &cc_format[i],
516                           -1);
517     }
518
519   gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview), 
520                           GTK_TREE_MODEL(list_store));
521
522   g_object_unref(list_store);
523
524
525   g_signal_connect(GTK_OBJECT(dialog->custom_treeview), 
526                    "cursor-changed",
527                    GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
528
529
530   g_signal_connect(GTK_OBJECT(dialog->custom_treeview), 
531                    "cursor-changed",
532                    GTK_SIGNAL_FUNC(preview_custom), dialog);
533
534
535   g_signal_connect(GTK_OBJECT(dialog->entry_width), 
536                    "changed",
537                    GTK_SIGNAL_FUNC(preview_custom), dialog);
538
539
540   g_signal_connect(GTK_OBJECT(dialog->entry_decimals), 
541                    "changed",
542                    GTK_SIGNAL_FUNC(preview_custom), dialog);
543
544
545   /* Connect the OK button */
546   g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked), 
547                    dialog);
548
549
550   }
551
552   return dialog;
553 }
554
555
556 /* Set a particular button to be active */
557 void
558 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
559 {
560   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
561                                TRUE);
562   dialog->active_button = b;
563 }
564
565
566
567 /* Set the TREEVIEW list cursor to the item described by FMT */
568 static void
569 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
570 {
571   GtkTreePath *path ;
572
573   /*
574     We do this with a linear search through the model --- hardly 
575     efficient, but the list is short ... */
576   GtkTreeIter iter;
577
578   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
579
580   gboolean success;
581   for (success = gtk_tree_model_get_iter_first(model, &iter);
582        success;
583        success = gtk_tree_model_iter_next(model, &iter))
584     {
585       const struct fmt_spec *spec;
586
587       GValue value = {0};
588
589       gtk_tree_model_get_value(model, &iter, 1, &value);
590           
591       spec = g_value_get_pointer(&value);
592
593       if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
594         {
595           break;
596         }
597     }
598         
599   path = gtk_tree_model_get_path(model, &iter);
600   if ( path ) 
601     {
602       gtk_tree_view_set_cursor(treeview, path, 0, 0);
603       gtk_tree_path_free(path);
604     }
605   else 
606     {
607       char str[FMT_STRING_LEN_MAX + 1];
608       g_warning("Unusual date format: %s\n", fmt_to_string(fmt, str));
609     }
610 }
611
612
613 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
614 static void
615 select_treeview_from_format_type(GtkTreeView *treeview, 
616                                  const int fmt_type)
617 {
618   GtkTreePath *path ;
619  
620  /*
621     We do this with a linear search through the model --- hardly 
622     efficient, but the list is short ... */
623   GtkTreeIter iter;
624
625   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
626
627   gboolean success;
628   for (success = gtk_tree_model_get_iter_first(model, &iter);
629        success;
630        success = gtk_tree_model_iter_next(model, &iter))
631     {
632       int spec ;
633       
634       GValue value = {0};
635
636       gtk_tree_model_get_value(model, &iter, 1, &value);
637           
638       spec = * ((int *) g_value_get_pointer(&value));
639
640       if ( spec == fmt_type)
641         break;
642     }
643         
644   path = gtk_tree_model_get_path(model, &iter);
645   if ( path ) 
646     {
647       gtk_tree_view_set_cursor(treeview, path, 0, 0);
648       gtk_tree_path_free(path);
649     }
650   else
651     g_warning("Unknown custom type  %d\n", fmt_type);
652
653 }
654
655 /* Set up the state of the dialog box to match the variable VAR */
656 static void
657 var_type_dialog_set_state(struct var_type_dialog *dialog)
658 {
659   const struct fmt_spec *write_spec ;
660   GString *str = g_string_new("");
661
662   g_assert(dialog);
663   g_assert(dialog->pv);
664
665   /* Populate width and decimals */
666   write_spec = var_get_write_format (dialog->pv);
667
668   g_string_printf(str, "%d", write_spec->d);
669
670   gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), 
671                      str->str);
672
673   g_string_printf(str, "%d", write_spec->w);
674
675   gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), 
676                      str->str);
677
678   g_string_free(str, TRUE);
679
680   /* Populate the radio button states */
681   switch (write_spec->type)
682     {
683     case FMT_F:
684       var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
685       gtk_widget_show_all(dialog->width_decimals);
686       break;
687     case FMT_A:
688       var_type_dialog_set_active_button(dialog, BUTTON_STRING);
689       gtk_widget_hide(dialog->label_decimals);
690       gtk_widget_hide(dialog->entry_decimals);
691       break;
692     case FMT_COMMA:
693       var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
694       gtk_widget_show_all(dialog->width_decimals);
695       break;
696     case FMT_DOT:
697       var_type_dialog_set_active_button(dialog, BUTTON_DOT);
698       gtk_widget_show_all(dialog->width_decimals);
699       break;
700     case FMT_DOLLAR:
701       var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
702       gtk_widget_show_all(dialog->width_decimals);
703       
704       select_treeview_from_format(dialog->dollar_treeview, write_spec);
705       break;
706     case FMT_DATE:      
707     case FMT_EDATE:     
708     case FMT_SDATE:     
709     case FMT_ADATE:     
710     case FMT_JDATE:     
711     case FMT_QYR:       
712     case FMT_MOYR:      
713     case FMT_WKYR:      
714     case FMT_DATETIME:  
715     case FMT_TIME:      
716     case FMT_DTIME:     
717     case FMT_WKDAY:     
718     case FMT_MONTH:     
719       var_type_dialog_set_active_button(dialog, BUTTON_DATE);
720       gtk_widget_hide(dialog->width_decimals);
721       gtk_widget_show(dialog->date_format_list);
722       select_treeview_from_format(dialog->date_format_treeview, write_spec);
723       break;
724     case FMT_CCA:
725     case FMT_CCB:
726     case FMT_CCC:
727     case FMT_CCD:
728     case FMT_CCE:
729       var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
730       select_treeview_from_format_type(dialog->custom_treeview, 
731                                        write_spec->type);
732       gtk_widget_show_all(dialog->width_decimals);
733       break;
734     default:
735       gtk_widget_show_all(dialog->width_decimals);
736       break;
737     }
738 }
739
740
741 /* Popup the dialog box */
742 void 
743 var_type_dialog_show(struct var_type_dialog *dialog)
744 {
745   var_type_dialog_set_state(dialog);
746
747   gtk_widget_show(dialog->window);
748 }
749
750 /* Fills F with an output format specification with type TYPE, width
751    W, and D decimals. Iff it's a valid format, then return true.
752 */
753 static bool
754 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
755 {
756   f->type = type;
757   f->w = w;
758   f->d = d;
759   return fmt_check_output (f);
760 }
761
762
763
764
765 /* Callbacks for the Variable Type Dialog Box */
766
767 /* Callback for when the var type dialog is closed using the OK button. 
768    It sets the appropriate variable accordingly. */
769 static gint
770 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
771 {
772   struct var_type_dialog *dialog = data;
773
774   g_assert(dialog);
775   g_assert(dialog->pv);
776
777   {
778     gint width = atoi(gtk_entry_get_text
779                       (GTK_ENTRY(dialog->entry_width)));
780
781     gint decimals = atoi(gtk_entry_get_text
782                          (GTK_ENTRY(dialog->entry_decimals)));
783
784     gint new_type = VAR_NUMERIC;
785     gint new_width = 0;
786     bool result = false;
787     struct fmt_spec spec;
788     switch (dialog->active_button) 
789       {
790       case BUTTON_STRING:
791         new_type = VAR_STRING;
792         new_width = width;
793         result = make_output_format_try(&spec, FMT_A, width, 0);
794         break;
795       case BUTTON_NUMERIC:
796         result = make_output_format_try(&spec, FMT_F, width, decimals);
797         break;
798       case BUTTON_COMMA:
799         result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
800         break;
801       case BUTTON_DOT:
802         result = make_output_format_try(&spec, FMT_DOT, width, decimals);
803         break;
804       case BUTTON_SCIENTIFIC:
805         result = make_output_format_try(&spec, FMT_E, width, decimals);
806         break;
807       case BUTTON_DATE:
808       case BUTTON_CUSTOM:
809         g_assert(fmt_check_output(&dialog->fmt_l));
810         result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
811         break;
812       case BUTTON_DOLLAR:
813         result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
814         break;
815       default:
816         g_print("Unknown variable type: %d\n", dialog->active_button) ;
817         result = false;
818         break;
819       }
820
821     if ( result == true )
822       {
823         var_set_width (dialog->pv, new_width);
824         var_set_both_formats (dialog->pv, &spec);
825       }
826
827   }
828   gtk_widget_hide(dialog->window);
829
830   return FALSE;
831 }
832
833
834
835 gint
836 on_var_type_cancel_clicked(GtkWidget *w,  gpointer data)
837 {
838   gtk_widget_hide(w);
839
840   return FALSE;
841 }
842