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