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