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