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