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