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