1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2005, 2006, 2010, 2011, 2012 Free Software Foundation
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.
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.
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/>. */
18 /* This module describes the behaviour of the Variable Type dialog box used
19 for inputing the variable type in the var sheet */
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"
40 static const struct format_opt format_option[] =
42 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
43 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
44 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
45 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
46 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
47 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
48 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
49 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
50 { "yyddd", {FMT_JDATE, 5, 0} },
51 { "yyyyddd", {FMT_JDATE, 7, 0} },
52 { "q Q yyyy", {FMT_QYR, 8, 0} },
53 { "q Q yy", {FMT_QYR, 6, 0} },
54 { "mmm yyyy", {FMT_MOYR, 8, 0} },
55 { "mmm yy", {FMT_MOYR, 6, 0} },
56 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
57 { "dd WK yy", {FMT_WKYR, 8, 0} },
58 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}},
59 { "dd-mmm-yyyy HH:MM:SS", {FMT_DATETIME, 20, 0}}
63 static const struct fmt_spec dollar_format[] =
79 static const int cc_format[] =
89 static void select_treeview_from_format
90 (GtkTreeView *treeview, const struct fmt_spec *fmt);
92 static void select_treeview_from_format_type (GtkTreeView *treeview,
96 /* callback for when any of the radio buttons are toggled */
98 on_toggle_1 (GtkToggleButton *togglebutton, gpointer dialog_)
100 GtkWidget *widget = GTK_WIDGET (togglebutton);
101 struct var_type_dialog *dialog = dialog_;
104 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
107 for (i = 0; i < num_BUTTONS; i++)
108 if (widget == dialog->radioButton[i])
110 dialog->active_button = i;
113 g_return_if_reached ();
116 static void update_width_decimals (const struct var_type_dialog *dialog);
118 #define force_max(x, val) if (x > val) x = val
121 Set the local format from the variable
122 and force them to have sensible values */
124 set_local_width_decimals (struct var_type_dialog *dialog)
126 dialog->fmt_l = * var_get_print_format (dialog->pv);
128 switch (dialog->active_button)
131 force_max ( dialog->fmt_l.w, 255);
134 force_max ( dialog->fmt_l.w, 40);
135 force_max ( dialog->fmt_l.d, 16);
141 /* callback for when any of the radio buttons are toggled */
143 on_toggle_2 (GtkToggleButton *togglebutton, gpointer user_data)
145 struct var_type_dialog *dialog = user_data;
146 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
148 switch (dialog->active_button)
151 gtk_widget_hide (dialog->date_format_list);
154 gtk_widget_hide (dialog->custom_currency_hbox);
157 gtk_widget_hide (dialog->dollar_window);
160 gtk_widget_show (dialog->label_decimals);
161 gtk_widget_show (dialog->entry_decimals);
167 set_local_width_decimals (dialog);
168 update_width_decimals (dialog);
170 switch (dialog->active_button)
173 gtk_widget_show (dialog->entry_width);
174 gtk_widget_show (dialog->width_decimals);
175 gtk_widget_hide (dialog->label_decimals);
176 gtk_widget_hide (dialog->entry_decimals);
179 select_treeview_from_format (dialog->date_format_treeview,
180 &format_option[0].spec);
181 gtk_widget_hide (dialog->width_decimals);
182 gtk_widget_show (dialog->date_format_list);
185 select_treeview_from_format (dialog->dollar_treeview,
187 gtk_widget_show (dialog->dollar_window);
188 gtk_widget_show_all (dialog->width_decimals);
191 select_treeview_from_format_type (dialog->custom_treeview,
194 gtk_widget_show (dialog->width_decimals);
195 gtk_widget_show (dialog->custom_currency_hbox);
198 gtk_widget_show_all (dialog->width_decimals);
205 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
206 static gint hide_dialog (GtkWidget *w, gpointer data);
210 add_to_group (GtkWidget *w, gpointer data)
212 GtkSizeGroup *sg = data;
214 gtk_size_group_add_widget (sg, w);
217 /* Set the local width and decimals entry boxes to reflec the local format */
219 update_width_decimals (const struct var_type_dialog *dialog)
224 text = g_strdup_printf ("%d", dialog->fmt_l.w);
225 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
228 text = g_strdup_printf ("%d", dialog->fmt_l.d);
229 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
233 /* Callback for when the custom treeview row is changed.
234 It sets dialog box to reflect the selected format */
236 preview_custom (GtkWidget *w, gpointer data)
240 struct var_type_dialog *dialog = data;
242 if ( dialog->active_button != BUTTON_CUSTOM )
245 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
246 dialog->fmt_l.d = atoi (text);
248 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
249 dialog->fmt_l.w = atoi (text);
252 if ( ! fmt_check_output (&dialog->fmt_l))
254 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
255 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
263 sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
264 gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
265 g_free (sample_text);
268 sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
269 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
270 g_free (sample_text);
275 /* Callback for when a treeview row is changed.
276 It sets the fmt_l_spec to reflect the selected format */
278 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
280 struct var_type_dialog *dialog = data;
282 GValue the_value = {0};
284 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
286 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
288 gtk_tree_selection_get_selected (sel, &model, &iter);
290 gtk_tree_model_get_value (model, &iter, 1, &the_value);
292 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value);
294 g_value_unset (&the_value);
298 /* Callback for when a treeview row is changed.
299 It sets the type of the fmt_l to reflect the selected type */
301 set_format_type_from_treeview (GtkTreeView *treeview, gpointer data)
303 static struct fmt_spec custom_format = {0,0,0};
304 struct var_type_dialog *dialog = data;
306 GValue the_value = {0};
308 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
310 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
312 gtk_tree_selection_get_selected (sel, &model, &iter);
314 gtk_tree_model_get_value (model, &iter, 1, &the_value);
316 dialog->fmt_l = custom_format;
317 dialog->fmt_l.type = g_value_get_int (&the_value);
319 g_value_unset (&the_value);
323 /* Create the structure */
324 struct var_type_dialog *
325 var_type_dialog_create (GtkWindow *toplevel)
328 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
330 GtkBuilder *xml = builder_new ("var-type-dialog.ui");
332 dialog->window = get_widget_assert (xml,"var_type_dialog");
333 dialog->active_button = -1;
336 g_signal_connect (dialog->window, "delete-event",
337 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
339 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
342 dialog->radioButton[BUTTON_NUMERIC] =
343 get_widget_assert (xml,"radiobutton1");
344 dialog->radioButton[BUTTON_COMMA] =
345 get_widget_assert (xml,"radiobutton2");
346 dialog->radioButton[BUTTON_DOT] =
347 get_widget_assert (xml,"radiobutton3");
348 dialog->radioButton[BUTTON_SCIENTIFIC] =
349 get_widget_assert (xml,"radiobutton4");
350 dialog->radioButton[BUTTON_DATE] =
351 get_widget_assert (xml,"radiobutton5");
352 dialog->radioButton[BUTTON_DOLLAR] =
353 get_widget_assert (xml,"radiobutton6");
354 dialog->radioButton[BUTTON_CUSTOM] =
355 get_widget_assert (xml,"radiobutton7");
356 dialog->radioButton[BUTTON_STRING] =
357 get_widget_assert (xml,"radiobutton8");
360 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
361 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
362 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
363 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
365 dialog->label_psample = get_widget_assert (xml, "psample_label");
366 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
369 dialog->entry_width = get_widget_assert (xml,"width_entry");
371 dialog->custom_currency_hbox = get_widget_assert (xml,
372 "custom_currency_hbox");
374 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
375 dialog->dollar_treeview =
376 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
378 dialog->custom_treeview =
379 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
382 dialog->ok = get_widget_assert (xml,"var_type_ok");
387 GtkListStore *list_store ;
389 GtkTreeViewColumn *column;
390 GtkCellRenderer *renderer ;
392 /* The "middle_box" is a vbox with serveral children.
393 However only one child is ever shown at a time.
394 We need to make sure that they all have the same width, to avoid
395 upleasant resizing effects */
396 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
398 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
399 add_to_group, sizeGroup);
402 for (i = 0 ; i < num_BUTTONS; ++i )
404 g_signal_connect (dialog->radioButton[i], "toggled",
405 G_CALLBACK (on_toggle_1), dialog);
407 g_signal_connect (dialog->radioButton[i], "toggled",
408 G_CALLBACK (on_toggle_2), dialog);
411 /* Populate the date format tree view */
412 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
413 "date_format_list_view"));
415 renderer = gtk_cell_renderer_text_new ();
417 column = gtk_tree_view_column_new_with_attributes ("Title",
423 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
427 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
429 for ( i = 0 ; i < sizeof (format_option) / sizeof (format_option[0]) ; ++i )
431 gtk_list_store_append (list_store, &iter);
432 gtk_list_store_set (list_store, &iter,
433 0, format_option[i].desc,
434 1, &format_option[i].spec,
438 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
439 GTK_TREE_MODEL (list_store));
441 g_object_unref (list_store);
443 g_signal_connect (dialog->date_format_treeview, "cursor-changed",
444 G_CALLBACK (set_format_from_treeview), dialog);
447 /* populate the dollar treeview */
449 renderer = gtk_cell_renderer_text_new ();
451 column = gtk_tree_view_column_new_with_attributes ("Title",
457 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
461 list_store = gtk_list_store_new (2, G_TYPE_STRING,
464 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
466 char *template = settings_dollar_template (&dollar_format[i]);
467 gtk_list_store_append (list_store, &iter);
468 gtk_list_store_set (list_store, &iter,
470 1, &dollar_format[i],
475 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
476 GTK_TREE_MODEL (list_store));
478 g_object_unref (list_store);
480 g_signal_connect (dialog->dollar_treeview,
482 G_CALLBACK (set_format_from_treeview), dialog);
484 g_signal_connect_swapped (dialog->dollar_treeview,
486 G_CALLBACK (update_width_decimals), dialog);
489 /* populate the custom treeview */
491 renderer = gtk_cell_renderer_text_new ();
493 column = gtk_tree_view_column_new_with_attributes ("Title",
499 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
503 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
505 for ( i = 0 ; i < 5 ; ++i )
507 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
508 gtk_list_store_append (list_store, &iter);
509 gtk_list_store_set (list_store, &iter,
510 0, fmt_name (cc_fmts[i]),
515 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
516 GTK_TREE_MODEL (list_store));
518 g_object_unref (list_store);
521 g_signal_connect (dialog->custom_treeview,
523 G_CALLBACK (set_format_type_from_treeview), dialog);
526 g_signal_connect (dialog->custom_treeview,
528 G_CALLBACK (preview_custom), dialog);
531 g_signal_connect (dialog->entry_width,
533 G_CALLBACK (preview_custom), dialog);
536 g_signal_connect (dialog->entry_decimals,
538 G_CALLBACK (preview_custom), dialog);
541 /* Connect to the OK button */
542 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
546 /* And the cancel button */
547 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
548 G_CALLBACK (hide_dialog),
552 g_object_unref (xml);
558 /* Set a particular button to be active */
560 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
562 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
564 dialog->active_button = b;
569 /* Set the TREEVIEW list cursor to the item described by FMT */
571 select_treeview_from_format (GtkTreeView *treeview, const struct fmt_spec *fmt)
576 We do this with a linear search through the model --- hardly
577 efficient, but the list is short ... */
580 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
583 for (success = gtk_tree_model_get_iter_first (model, &iter);
585 success = gtk_tree_model_iter_next (model, &iter))
587 const struct fmt_spec *spec;
591 gtk_tree_model_get_value (model, &iter, 1, &value);
593 spec = g_value_get_pointer (&value);
595 g_value_unset (&value);
597 if ( 0 == memcmp (spec, fmt, sizeof (struct fmt_spec)))
603 path = gtk_tree_model_get_path (model, &iter);
606 gtk_tree_view_set_cursor (treeview, path, 0, 0);
607 gtk_tree_path_free (path);
611 char str[FMT_STRING_LEN_MAX + 1];
612 g_warning ("Unusual date format: %s\n", fmt_to_string (fmt, str));
617 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
619 select_treeview_from_format_type (GtkTreeView *treeview,
625 We do this with a linear search through the model --- hardly
626 efficient, but the list is short ... */
629 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
632 for (success = gtk_tree_model_get_iter_first (model, &iter);
634 success = gtk_tree_model_iter_next (model, &iter))
640 gtk_tree_model_get_value (model, &iter, 1, &value);
642 spec = g_value_get_int (&value);
644 g_value_unset (&value);
646 if ( spec == fmt_type)
650 path = gtk_tree_model_get_path (model, &iter);
653 gtk_tree_view_set_cursor (treeview, path, 0, 0);
654 gtk_tree_path_free (path);
657 g_warning ("Unknown custom type %d\n", fmt_type);
661 /* Set up the state of the dialog box to match the variable VAR */
663 var_type_dialog_set_state (struct var_type_dialog *dialog)
665 const struct fmt_spec *format ;
666 GString *str = g_string_new ("");
669 g_assert (dialog->pv);
671 /* Populate width and decimals */
672 format = var_get_print_format (dialog->pv);
674 g_string_printf (str, "%d", format->d);
676 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals),
679 g_string_printf (str, "%d", format->w);
681 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width),
684 g_string_free (str, TRUE);
686 /* Populate the radio button states */
687 switch (format->type)
690 var_type_dialog_set_active_button (dialog, BUTTON_NUMERIC);
691 gtk_widget_show_all (dialog->width_decimals);
694 var_type_dialog_set_active_button (dialog, BUTTON_STRING);
695 gtk_widget_hide (dialog->label_decimals);
696 gtk_widget_hide (dialog->entry_decimals);
699 var_type_dialog_set_active_button (dialog, BUTTON_COMMA);
700 gtk_widget_show_all (dialog->width_decimals);
703 var_type_dialog_set_active_button (dialog, BUTTON_DOT);
704 gtk_widget_show_all (dialog->width_decimals);
707 var_type_dialog_set_active_button (dialog, BUTTON_DOLLAR);
708 gtk_widget_show_all (dialog->width_decimals);
710 select_treeview_from_format (dialog->dollar_treeview, format);
725 var_type_dialog_set_active_button (dialog, BUTTON_DATE);
726 gtk_widget_hide (dialog->width_decimals);
727 gtk_widget_show (dialog->date_format_list);
728 select_treeview_from_format (dialog->date_format_treeview, format);
735 var_type_dialog_set_active_button (dialog, BUTTON_CUSTOM);
736 select_treeview_from_format_type (dialog->custom_treeview,
738 gtk_widget_show_all (dialog->width_decimals);
741 gtk_widget_show_all (dialog->width_decimals);
747 /* Popup the dialog box */
749 var_type_dialog_show (struct var_type_dialog *dialog)
751 var_type_dialog_set_state (dialog);
753 gtk_widget_show (dialog->window);
756 /* Fills F with an output format specification with type TYPE, width
757 W, and D decimals. Iff it's a valid format, then return true.
760 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
765 return fmt_check_output (f);
771 /* Callbacks for the Variable Type Dialog Box */
773 /* Callback for when the var type dialog is closed using the OK button.
774 It sets the appropriate variable accordingly. */
776 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
778 struct var_type_dialog *dialog = data;
781 g_assert (dialog->pv);
784 gint width = atoi (gtk_entry_get_text
785 (GTK_ENTRY (dialog->entry_width)));
787 gint decimals = atoi (gtk_entry_get_text
788 (GTK_ENTRY (dialog->entry_decimals)));
792 struct fmt_spec spec;
793 switch (dialog->active_button)
797 result = make_output_format_try (&spec, FMT_A, width, 0);
800 result = make_output_format_try (&spec, FMT_F, width, decimals);
803 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
806 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
808 case BUTTON_SCIENTIFIC:
809 result = make_output_format_try (&spec, FMT_E, width, decimals);
813 if (! fmt_check_output (&dialog->fmt_l))
814 g_critical ("Invalid variable format");
816 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
819 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
822 g_critical ("Unknown variable type: %d", dialog->active_button) ;
827 if ( result == true )
829 var_set_width (dialog->pv, new_width);
830 var_set_both_formats (dialog->pv, &spec);
834 gtk_widget_hide (dialog->window);
842 hide_dialog (GtkWidget *w, gpointer data)
844 struct var_type_dialog *dialog = data;
846 gtk_widget_hide (dialog->window);