var-type-dialog: Reduce redundancy further.
[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 static gint
300 get_index_from_treeview (GtkTreeView *treeview)
301 {
302   GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);
303   GtkTreeModel *model;
304   GtkTreePath *path;
305   GtkTreeIter iter;
306   gint index;
307
308   gtk_tree_selection_get_selected (selection, &model, &iter);
309   path = gtk_tree_model_get_path (model, &iter);
310   if (!path || gtk_tree_path_get_depth (path) < 1)
311     index = 0;
312   else
313     index = gtk_tree_path_get_indices (path)[0];
314   gtk_tree_path_free (path);
315
316   return index;
317 }
318
319 /* Callback for when a date treeview row is changed.
320    It sets the fmt_l_spec to reflect the selected format */
321 static void
322 set_date_format_from_treeview (GtkTreeView *treeview,
323                                struct var_type_dialog *dialog)
324 {
325   dialog->fmt_l = date_format[get_index_from_treeview (treeview)];
326 }
327
328 /* Callback for when a dollar treeview row is changed.
329    It sets the fmt_l_spec to reflect the selected format */
330 static void
331 set_dollar_format_from_treeview (GtkTreeView *treeview,
332                                struct var_type_dialog *dialog)
333 {
334   dialog->fmt_l = dollar_format[get_index_from_treeview (treeview)];
335 }
336
337 /* Callback for when a treeview row is changed.
338    It sets the type of the fmt_l to reflect the selected type */
339 static void
340 set_custom_format_from_treeview (GtkTreeView *treeview,
341                                  struct var_type_dialog *dialog)
342 {
343   dialog->fmt_l.type = cc_format[get_index_from_treeview (treeview)];
344 }
345
346 /* Create the structure */
347 struct var_type_dialog *
348 var_type_dialog_create (GtkWindow *toplevel)
349 {
350   gint i;
351   struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
352
353   GtkBuilder *xml = builder_new ("var-type-dialog.ui");
354
355   dialog->window = get_widget_assert (xml,"var_type_dialog");
356   dialog->active_button = -1;
357
358
359   g_signal_connect (dialog->window, "delete-event",
360                     G_CALLBACK (gtk_widget_hide_on_delete), NULL);
361
362   gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
363                                 toplevel);
364
365   dialog->radioButton[BUTTON_NUMERIC] =
366     get_widget_assert (xml,"radiobutton1");
367   dialog->radioButton[BUTTON_COMMA] =
368     get_widget_assert (xml,"radiobutton2");
369   dialog->radioButton[BUTTON_DOT] =
370     get_widget_assert (xml,"radiobutton3");
371   dialog->radioButton[BUTTON_SCIENTIFIC] =
372     get_widget_assert (xml,"radiobutton4");
373   dialog->radioButton[BUTTON_DATE] =
374     get_widget_assert (xml,"radiobutton5");
375   dialog->radioButton[BUTTON_DOLLAR] =
376     get_widget_assert (xml,"radiobutton6");
377   dialog->radioButton[BUTTON_CUSTOM] =
378     get_widget_assert (xml,"radiobutton7");
379   dialog->radioButton[BUTTON_STRING] =
380     get_widget_assert (xml,"radiobutton8");
381
382
383   dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
384   dialog->width_decimals = get_widget_assert (xml, "width_decimals");
385   dialog->label_decimals = get_widget_assert (xml, "decimals_label");
386   dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
387
388   dialog->label_psample = get_widget_assert (xml, "psample_label");
389   dialog->label_nsample = get_widget_assert (xml, "nsample_label");
390
391
392   dialog->entry_width = get_widget_assert (xml,"width_entry");
393
394   dialog->custom_currency_hbox = get_widget_assert (xml,
395                                                    "custom_currency_hbox");
396
397   dialog->dollar_window = get_widget_assert (xml, "dollar_window");
398   dialog->dollar_treeview =
399     GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
400
401   dialog->custom_treeview =
402     GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
403
404
405   dialog->ok = get_widget_assert (xml,"var_type_ok");
406
407
408   {
409   GtkTreeIter iter;
410   GtkListStore *list_store ;
411
412   GtkTreeViewColumn *column;
413   GtkCellRenderer *renderer ;
414
415   /* The "middle_box" is a vbox with serveral children.
416      However only one child is ever shown at a time.
417      We need to make sure that they all have the same width, to avoid
418      upleasant resizing effects */
419   GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
420
421   gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
422                         add_to_group, sizeGroup);
423
424
425   for (i = 0 ; i < num_BUTTONS; ++i )
426     g_signal_connect (dialog->radioButton[i], "toggled",
427                       G_CALLBACK (on_toggle), dialog);
428
429   /* Populate the date format tree view */
430   dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
431                                               "date_format_list_view"));
432
433   renderer = gtk_cell_renderer_text_new ();
434
435   column = gtk_tree_view_column_new_with_attributes ("Title",
436                                                      renderer,
437                                                      "text",
438                                                      0,
439                                                      NULL);
440
441   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
442                                column);
443
444
445   list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
446
447   for ( i = 0 ; i < sizeof (date_format) / sizeof (date_format[0]) ; ++i )
448     {
449       const struct fmt_spec *f = &date_format[i];
450       gtk_list_store_append (list_store, &iter);
451       gtk_list_store_set (list_store, &iter,
452                           0, fmt_date_template (f->type, f->w),
453                           1, f,
454                           -1);
455     }
456
457   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
458                           GTK_TREE_MODEL (list_store));
459
460   g_object_unref (list_store);
461
462   g_signal_connect (dialog->date_format_treeview, "cursor-changed",
463                    G_CALLBACK (set_date_format_from_treeview), dialog);
464
465
466   /* populate the dollar treeview */
467
468   renderer = gtk_cell_renderer_text_new ();
469
470   column = gtk_tree_view_column_new_with_attributes ("Title",
471                                                      renderer,
472                                                      "text",
473                                                      0,
474                                                      NULL);
475
476   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
477                                column);
478
479
480   list_store = gtk_list_store_new (2, G_TYPE_STRING,
481                                                  G_TYPE_POINTER);
482
483   for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
484     {
485       char *template = settings_dollar_template (&dollar_format[i]);
486       gtk_list_store_append (list_store, &iter);
487       gtk_list_store_set (list_store, &iter,
488                           0, template,
489                           1, &dollar_format[i],
490                           -1);
491       free (template);
492     }
493
494   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
495                           GTK_TREE_MODEL (list_store));
496
497   g_object_unref (list_store);
498
499   g_signal_connect (dialog->dollar_treeview,
500                    "cursor-changed",
501                    G_CALLBACK (set_dollar_format_from_treeview), dialog);
502
503   g_signal_connect_swapped (dialog->dollar_treeview,
504                    "cursor-changed",
505                    G_CALLBACK (update_width_decimals), dialog);
506
507
508   /* populate the custom treeview */
509
510   renderer = gtk_cell_renderer_text_new ();
511
512   column = gtk_tree_view_column_new_with_attributes ("Title",
513                                                      renderer,
514                                                      "text",
515                                                      0,
516                                                      NULL);
517
518   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
519                                column);
520
521
522   list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
523
524   for ( i = 0 ; i < 5 ; ++i )
525     {
526       enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
527       gtk_list_store_append (list_store, &iter);
528       gtk_list_store_set (list_store, &iter,
529                           0, fmt_name (cc_fmts[i]),
530                           1, cc_format[i],
531                           -1);
532     }
533
534   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
535                           GTK_TREE_MODEL (list_store));
536
537   g_object_unref (list_store);
538
539
540   g_signal_connect (dialog->custom_treeview,
541                    "cursor-changed",
542                    G_CALLBACK (set_custom_format_from_treeview), dialog);
543
544
545   g_signal_connect (dialog->custom_treeview,
546                    "cursor-changed",
547                    G_CALLBACK (preview_custom), dialog);
548
549
550   g_signal_connect (dialog->entry_width,
551                    "changed",
552                    G_CALLBACK (preview_custom), dialog);
553
554
555   g_signal_connect (dialog->entry_decimals,
556                    "changed",
557                    G_CALLBACK (preview_custom), dialog);
558
559
560   /* Connect to the OK button */
561   g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
562                    dialog);
563
564
565   /* And the cancel button */
566   g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
567                     G_CALLBACK (hide_dialog),
568                     dialog);
569   }
570
571   g_object_unref (xml);
572
573   return dialog;
574 }
575
576
577 /* Set a particular button to be active */
578 void
579 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
580 {
581   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
582                                TRUE);
583 }
584
585
586
587 static void
588 select_treeview_at_index (GtkTreeView *treeview, int index)
589 {
590   GtkTreePath *path;
591
592   path = gtk_tree_path_new_from_indices (index, -1);
593   gtk_tree_view_set_cursor (treeview, path, 0, 0);
594   gtk_tree_path_free (path);
595 }
596
597 static int
598 find_format (const struct fmt_spec *target,
599              const struct fmt_spec formats[], int n_formats)
600 {
601   int i;
602
603   for (i = 0; i < n_formats; i++)
604     if (fmt_equal (target, &formats[i]))
605       return i;
606
607   return 0;
608 }
609
610 static int
611 find_format_type (int target, const int types[], int n_types)
612 {
613   int i;
614
615   for (i = 0; i < n_types; i++)
616     if (target == types[i])
617       return i;
618
619   return 0;
620 }
621
622 /* Set up the state of the dialog box to match the variable VAR */
623 static void
624 var_type_dialog_set_state (struct var_type_dialog *dialog)
625 {
626   int button;
627
628   g_assert (dialog);
629   g_assert (dialog->pv);
630
631   /* Populate the radio button states */
632   switch (var_get_print_format (dialog->pv)->type)
633     {
634     default:
635     case FMT_F:
636       button = BUTTON_NUMERIC;
637       break;
638     case FMT_A:
639       button = BUTTON_STRING;
640       break;
641     case FMT_COMMA:
642       button = BUTTON_COMMA;
643       break;
644     case FMT_DOT:
645       button = BUTTON_DOT;
646       break;
647     case FMT_DOLLAR:
648       button = BUTTON_DOLLAR;
649       break;
650     case FMT_DATE:
651     case FMT_EDATE:
652     case FMT_SDATE:
653     case FMT_ADATE:
654     case FMT_JDATE:
655     case FMT_QYR:
656     case FMT_MOYR:
657     case FMT_WKYR:
658     case FMT_DATETIME:
659     case FMT_TIME:
660     case FMT_DTIME:
661     case FMT_WKDAY:
662     case FMT_MONTH:
663       button = BUTTON_DATE;
664       break;
665     case FMT_CCA:
666     case FMT_CCB:
667     case FMT_CCC:
668     case FMT_CCD:
669     case FMT_CCE:
670       button = BUTTON_CUSTOM;
671       break;
672     }
673
674   var_type_dialog_set_active_button (dialog, button);
675   refresh_active_button (dialog);
676   on_active_button_change (GTK_TOGGLE_BUTTON (dialog->radioButton[button]),
677                            dialog);
678 }
679
680
681 /* Popup the dialog box */
682 void
683 var_type_dialog_show (struct var_type_dialog *dialog)
684 {
685   var_type_dialog_set_state (dialog);
686
687   gtk_widget_show (dialog->window);
688 }
689
690 /* Fills F with an output format specification with type TYPE, width
691    W, and D decimals. Iff it's a valid format, then return true.
692 */
693 static bool
694 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
695 {
696   f->type = type;
697   f->w = w;
698   f->d = d;
699   return fmt_check_output (f);
700 }
701
702
703
704
705 /* Callbacks for the Variable Type Dialog Box */
706
707 /* Callback for when the var type dialog is closed using the OK button.
708    It sets the appropriate variable accordingly. */
709 static gint
710 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
711 {
712   struct var_type_dialog *dialog = data;
713
714   g_assert (dialog);
715   g_assert (dialog->pv);
716
717   {
718     gint width = atoi (gtk_entry_get_text
719                       (GTK_ENTRY (dialog->entry_width)));
720
721     gint decimals = atoi (gtk_entry_get_text
722                          (GTK_ENTRY (dialog->entry_decimals)));
723
724     gint new_width = 0;
725     bool result = false;
726     struct fmt_spec spec;
727     switch (dialog->active_button)
728       {
729       case BUTTON_STRING:
730         new_width = width;
731         result = make_output_format_try (&spec, FMT_A, width, 0);
732         break;
733       case BUTTON_NUMERIC:
734         result = make_output_format_try (&spec, FMT_F, width, decimals);
735         break;
736       case BUTTON_COMMA:
737         result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
738         break;
739       case BUTTON_DOT:
740         result = make_output_format_try (&spec, FMT_DOT, width, decimals);
741         break;
742       case BUTTON_SCIENTIFIC:
743         result = make_output_format_try (&spec, FMT_E, width, decimals);
744         break;
745       case BUTTON_DATE:
746       case BUTTON_CUSTOM:
747         if  (! fmt_check_output (&dialog->fmt_l))
748           g_critical ("Invalid variable format");
749         else
750           result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
751         break;
752       case BUTTON_DOLLAR:
753         result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
754         break;
755       default:
756         g_critical ("Unknown variable type: %d", dialog->active_button) ;
757         result = false;
758         break;
759       }
760
761     if ( result == true )
762       {
763         var_set_width (dialog->pv, new_width);
764         var_set_both_formats (dialog->pv, &spec);
765       }
766
767   }
768   gtk_widget_hide (dialog->window);
769
770   return FALSE;
771 }
772
773
774
775 static gint
776 hide_dialog (GtkWidget *w,  gpointer data)
777 {
778   struct var_type_dialog *dialog = data;
779
780   gtk_widget_hide (dialog->window);
781
782   return FALSE;
783 }
784