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