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