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