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