var-type-dialog: Fix possible memory leaks.
[pspp] / 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 format_opt {
35   gchar desc[21];
36   struct fmt_spec spec;
37 };
38
39
40 static const struct format_opt format_option[] =
41   {
42     { "dd-mmm-yyyy", {FMT_DATE,  11, 0} },
43     { "dd-mmm-yy",   {FMT_DATE,   9, 0} },
44     { "mm/dd/yyyy",  {FMT_ADATE, 10, 0} },
45     { "mm/dd/yy",    {FMT_ADATE, 8, 0} },
46     { "dd.mm.yyyy",  {FMT_EDATE, 10, 0} },
47     { "dd.mm.yy",    {FMT_EDATE, 8, 0} },
48     { "yyyy/mm/dd",  {FMT_SDATE, 10, 0} },
49     { "yy/mm/dd",    {FMT_SDATE, 8, 0} },
50     { "yyddd",       {FMT_JDATE, 5, 0} },
51     { "yyyyddd",     {FMT_JDATE, 7, 0} },
52     { "q Q yyyy",    {FMT_QYR, 8, 0} },
53     { "q Q yy",      {FMT_QYR, 6, 0} },
54     { "mmm yyyy",    {FMT_MOYR, 8, 0} },
55     { "mmm yy",      {FMT_MOYR, 6, 0} },
56     { "dd WK yyyy",  {FMT_WKYR, 10, 0} },
57     { "dd WK yy",    {FMT_WKYR, 8, 0} },
58     { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}},
59     { "dd-mmm-yyyy HH:MM:SS", {FMT_DATETIME, 20, 0}}
60   };
61
62
63 static const struct fmt_spec dollar_format[] =
64   {
65     {FMT_DOLLAR, 2, 0},
66     {FMT_DOLLAR, 3, 0},
67     {FMT_DOLLAR, 4, 0},
68     {FMT_DOLLAR, 7, 2},
69     {FMT_DOLLAR, 6, 0},
70     {FMT_DOLLAR, 9, 2},
71     {FMT_DOLLAR, 8, 0},
72     {FMT_DOLLAR, 11, 2},
73     {FMT_DOLLAR, 12, 0},
74     {FMT_DOLLAR, 15, 2},
75     {FMT_DOLLAR, 16, 0},
76     {FMT_DOLLAR, 19, 2}
77   };
78
79 static const int cc_format[] =
80   {
81     FMT_CCA,
82     FMT_CCB,
83     FMT_CCC,
84     FMT_CCD,
85     FMT_CCE,
86   };
87
88
89 static void select_treeview_from_format
90  (GtkTreeView *treeview, const struct fmt_spec *fmt);
91
92 static void select_treeview_from_format_type (GtkTreeView *treeview,
93                                              const int fmt_type);
94
95
96 /* callback for when any of the radio buttons are toggled */
97 static void
98 on_toggle_1 (GtkToggleButton *togglebutton, gpointer dialog_)
99 {
100   GtkWidget *widget = GTK_WIDGET (togglebutton);
101   struct var_type_dialog *dialog = dialog_;
102   int i;
103
104   if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
105     return ;
106
107   for (i = 0; i < num_BUTTONS; i++)
108     if (widget == dialog->radioButton[i])
109       {
110         dialog->active_button = i;
111         return;
112       }
113   g_return_if_reached ();
114 }
115
116 static void update_width_decimals (const struct var_type_dialog *dialog);
117
118 #define force_max(x, val) if (x > val) x = val
119
120 /*
121    Set the local format from the variable
122    and force them to have sensible values */
123 static void
124 set_local_width_decimals (struct var_type_dialog *dialog)
125 {
126   dialog->fmt_l = * var_get_print_format (dialog->pv);
127
128   switch (dialog->active_button)
129     {
130     case BUTTON_STRING:
131       force_max ( dialog->fmt_l.w, 255);
132       break;
133     default:
134       force_max ( dialog->fmt_l.w, 40);
135       force_max ( dialog->fmt_l.d, 16);
136       break;
137     }
138 }
139
140
141 /* callback for when any of the radio buttons are toggled */
142 static void
143 on_toggle_2 (GtkToggleButton *togglebutton, gpointer user_data)
144 {
145   struct var_type_dialog *dialog = user_data;
146   if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
147     {
148       switch (dialog->active_button)
149         {
150         case BUTTON_DATE:
151           gtk_widget_hide (dialog->date_format_list);
152           break;
153         case BUTTON_CUSTOM:
154           gtk_widget_hide (dialog->custom_currency_hbox);
155           break;
156         case BUTTON_DOLLAR:
157           gtk_widget_hide (dialog->dollar_window);
158           break;
159         case BUTTON_STRING:
160           gtk_widget_show (dialog->label_decimals);
161           gtk_widget_show (dialog->entry_decimals);
162           break;
163         }
164       return ;
165     }
166
167   set_local_width_decimals (dialog);
168   update_width_decimals (dialog);
169
170   switch (dialog->active_button)
171     {
172     case BUTTON_STRING:
173       gtk_widget_show (dialog->entry_width);
174       gtk_widget_show (dialog->width_decimals);
175       gtk_widget_hide (dialog->label_decimals);
176       gtk_widget_hide (dialog->entry_decimals);
177       break;
178     case BUTTON_DATE:
179       select_treeview_from_format (dialog->date_format_treeview,
180                                   &format_option[0].spec);
181       gtk_widget_hide (dialog->width_decimals);
182       gtk_widget_show (dialog->date_format_list);
183       break;
184     case BUTTON_DOLLAR:
185       select_treeview_from_format (dialog->dollar_treeview,
186                                   &dollar_format[0]);
187       gtk_widget_show (dialog->dollar_window);
188       gtk_widget_show_all (dialog->width_decimals);
189       break;
190     case BUTTON_CUSTOM:
191       select_treeview_from_format_type (dialog->custom_treeview,
192                                   cc_format[0]);
193
194       gtk_widget_show (dialog->width_decimals);
195       gtk_widget_show (dialog->custom_currency_hbox);
196       break;
197     default:
198       gtk_widget_show_all (dialog->width_decimals);
199       break;
200     }
201 }
202
203
204
205 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
206 static gint hide_dialog (GtkWidget *w,  gpointer data);
207
208
209 static void
210 add_to_group (GtkWidget *w, gpointer data)
211 {
212   GtkSizeGroup *sg = data;
213
214   gtk_size_group_add_widget (sg, w);
215 }
216
217 /* Set the local width and decimals entry boxes to reflec the local format */
218 static void
219 update_width_decimals (const struct var_type_dialog *dialog)
220 {
221   gchar *text;
222   g_assert (dialog);
223
224   text = g_strdup_printf ("%d", dialog->fmt_l.w);
225   gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
226   g_free (text);
227
228   text = g_strdup_printf ("%d", dialog->fmt_l.d);
229   gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
230   g_free (text);
231 }
232
233 /* Callback for when the custom treeview row is changed.
234    It sets dialog box to reflect the selected format */
235 static void
236 preview_custom (GtkWidget *w, gpointer data)
237 {
238   const gchar *text ;
239
240   struct var_type_dialog *dialog = data;
241
242   if ( dialog->active_button != BUTTON_CUSTOM )
243     return;
244
245   text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
246   dialog->fmt_l.d = atoi (text);
247
248   text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
249   dialog->fmt_l.w = atoi (text);
250
251   msg_disable ();
252   if ( ! fmt_check_output (&dialog->fmt_l))
253     {
254       gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
255       gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
256     }
257   else
258     {
259       gchar *sample_text;
260       union value v;
261       v.f = 1234.56;
262
263       sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
264       gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
265       g_free (sample_text);
266
267       v.f = -v.f;
268       sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
269       gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
270       g_free (sample_text);
271     }
272   msg_enable ();
273 }
274
275 /* Callback for when a treeview row is changed.
276    It sets the fmt_l_spec to reflect the selected format */
277 static void
278 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
279 {
280   struct var_type_dialog *dialog = data;
281   GtkTreeIter iter ;
282   GValue the_value = {0};
283
284   GtkTreeSelection* sel =  gtk_tree_view_get_selection (treeview);
285
286   GtkTreeModel * model  = gtk_tree_view_get_model (treeview);
287
288   gtk_tree_selection_get_selected (sel, &model, &iter);
289
290   gtk_tree_model_get_value (model, &iter, 1, &the_value);
291
292   dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value);
293
294   g_value_unset (&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   g_value_unset (&the_value);
320 }
321
322
323 /* Create the structure */
324 struct var_type_dialog *
325 var_type_dialog_create (GtkWindow *toplevel)
326 {
327   gint i;
328   struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
329
330   GtkBuilder *xml = builder_new ("var-type-dialog.ui");
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   /* The "middle_box" is a vbox with serveral children.
393      However only one child is ever shown at a time.
394      We need to make sure that they all have the same width, to avoid
395      upleasant resizing effects */
396   GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
397
398   gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
399                         add_to_group, sizeGroup);
400
401
402   for (i = 0 ; i < num_BUTTONS; ++i )
403     {
404       g_signal_connect (dialog->radioButton[i], "toggled",
405                        G_CALLBACK (on_toggle_1), dialog);
406
407       g_signal_connect (dialog->radioButton[i], "toggled",
408                        G_CALLBACK (on_toggle_2), dialog);
409     }
410
411   /* Populate the date format tree view */
412   dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
413                                               "date_format_list_view"));
414
415   renderer = gtk_cell_renderer_text_new ();
416
417   column = gtk_tree_view_column_new_with_attributes ("Title",
418                                                      renderer,
419                                                      "text",
420                                                      0,
421                                                      NULL);
422
423   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
424                                column);
425
426
427   list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
428
429   for ( i = 0 ; i < sizeof (format_option) / sizeof (format_option[0]) ; ++i )
430     {
431       gtk_list_store_append (list_store, &iter);
432       gtk_list_store_set (list_store, &iter,
433                           0, format_option[i].desc,
434                           1, &format_option[i].spec,
435                           -1);
436     }
437
438   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
439                           GTK_TREE_MODEL (list_store));
440
441   g_object_unref (list_store);
442
443   g_signal_connect (dialog->date_format_treeview, "cursor-changed",
444                    G_CALLBACK (set_format_from_treeview), dialog);
445
446
447   /* populate the dollar treeview */
448
449   renderer = gtk_cell_renderer_text_new ();
450
451   column = gtk_tree_view_column_new_with_attributes ("Title",
452                                                      renderer,
453                                                      "text",
454                                                      0,
455                                                      NULL);
456
457   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
458                                column);
459
460
461   list_store = gtk_list_store_new (2, G_TYPE_STRING,
462                                                  G_TYPE_POINTER);
463
464   for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
465     {
466       char *template = settings_dollar_template (&dollar_format[i]);
467       gtk_list_store_append (list_store, &iter);
468       gtk_list_store_set (list_store, &iter,
469                           0, template,
470                           1, &dollar_format[i],
471                           -1);
472       free (template);
473     }
474
475   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
476                           GTK_TREE_MODEL (list_store));
477
478   g_object_unref (list_store);
479
480   g_signal_connect (dialog->dollar_treeview,
481                    "cursor-changed",
482                    G_CALLBACK (set_format_from_treeview), dialog);
483
484   g_signal_connect_swapped (dialog->dollar_treeview,
485                    "cursor-changed",
486                    G_CALLBACK (update_width_decimals), dialog);
487
488
489   /* populate the custom treeview */
490
491   renderer = gtk_cell_renderer_text_new ();
492
493   column = gtk_tree_view_column_new_with_attributes ("Title",
494                                                      renderer,
495                                                      "text",
496                                                      0,
497                                                      NULL);
498
499   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
500                                column);
501
502
503   list_store = gtk_list_store_new (2, G_TYPE_STRING,
504                                                  G_TYPE_POINTER);
505
506   for ( i = 0 ; i < 5 ; ++i )
507     {
508       enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
509       gtk_list_store_append (list_store, &iter);
510       gtk_list_store_set (list_store, &iter,
511                           0, fmt_name (cc_fmts[i]),
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       g_value_unset (&value);
597
598       if ( 0 == memcmp (spec, fmt, sizeof (struct fmt_spec)))
599         {
600           break;
601         }
602     }
603
604   path = gtk_tree_model_get_path (model, &iter);
605   if ( path )
606     {
607       gtk_tree_view_set_cursor (treeview, path, 0, 0);
608       gtk_tree_path_free (path);
609     }
610   else
611     {
612       char str[FMT_STRING_LEN_MAX + 1];
613       g_warning ("Unusual date format: %s\n", fmt_to_string (fmt, str));
614     }
615 }
616
617
618 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
619 static void
620 select_treeview_from_format_type (GtkTreeView *treeview,
621                                  const int fmt_type)
622 {
623   GtkTreePath *path ;
624
625  /*
626     We do this with a linear search through the model --- hardly
627     efficient, but the list is short ... */
628   GtkTreeIter iter;
629
630   GtkTreeModel * model  = gtk_tree_view_get_model (treeview);
631
632   gboolean success;
633   for (success = gtk_tree_model_get_iter_first (model, &iter);
634        success;
635        success = gtk_tree_model_iter_next (model, &iter))
636     {
637       int spec ;
638
639       GValue value = {0};
640
641       gtk_tree_model_get_value (model, &iter, 1, &value);
642
643       spec = * ((int *) g_value_get_pointer (&value));
644
645       g_value_unset (&value);
646
647       if ( spec == fmt_type)
648         break;
649     }
650
651   path = gtk_tree_model_get_path (model, &iter);
652   if ( path )
653     {
654       gtk_tree_view_set_cursor (treeview, path, 0, 0);
655       gtk_tree_path_free (path);
656     }
657   else
658     g_warning ("Unknown custom type  %d\n", fmt_type);
659
660 }
661
662 /* Set up the state of the dialog box to match the variable VAR */
663 static void
664 var_type_dialog_set_state (struct var_type_dialog *dialog)
665 {
666   const struct fmt_spec *format ;
667   GString *str = g_string_new ("");
668
669   g_assert (dialog);
670   g_assert (dialog->pv);
671
672   /* Populate width and decimals */
673   format = var_get_print_format (dialog->pv);
674
675   g_string_printf (str, "%d", format->d);
676
677   gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals),
678                      str->str);
679
680   g_string_printf (str, "%d", format->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 (format->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, format);
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, format);
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                                        format->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 fmt_check_output (f);
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   {
785     gint width = atoi (gtk_entry_get_text
786                       (GTK_ENTRY (dialog->entry_width)));
787
788     gint decimals = atoi (gtk_entry_get_text
789                          (GTK_ENTRY (dialog->entry_decimals)));
790
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_width = width;
798         result = make_output_format_try (&spec, FMT_A, width, 0);
799         break;
800       case BUTTON_NUMERIC:
801         result = make_output_format_try (&spec, FMT_F, width, decimals);
802         break;
803       case BUTTON_COMMA:
804         result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
805         break;
806       case BUTTON_DOT:
807         result = make_output_format_try (&spec, FMT_DOT, width, decimals);
808         break;
809       case BUTTON_SCIENTIFIC:
810         result = make_output_format_try (&spec, FMT_E, width, decimals);
811         break;
812       case BUTTON_DATE:
813       case BUTTON_CUSTOM:
814         if  (! fmt_check_output (&dialog->fmt_l))
815           g_critical ("Invalid variable format");
816         else
817           result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
818         break;
819       case BUTTON_DOLLAR:
820         result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
821         break;
822       default:
823         g_critical ("Unknown variable type: %d", dialog->active_button) ;
824         result = false;
825         break;
826       }
827
828     if ( result == true )
829       {
830         var_set_width (dialog->pv, new_width);
831         var_set_both_formats (dialog->pv, &spec);
832       }
833
834   }
835   gtk_widget_hide (dialog->window);
836
837   return FALSE;
838 }
839
840
841
842 static gint
843 hide_dialog (GtkWidget *w,  gpointer data)
844 {
845   struct var_type_dialog *dialog = data;
846
847   gtk_widget_hide (dialog->window);
848
849   return FALSE;
850 }
851