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"
34 static const struct fmt_spec date_format[] =
52 {FMT_DATETIME, 17, 0},
57 static const struct fmt_spec dollar_format[] =
73 static const int cc_format[] =
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);
87 static void select_treeview_at_index (GtkTreeView *, int index);
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 *);
94 /* callback for when any of the radio buttons are toggled */
96 on_toggle (GtkToggleButton *togglebutton, gpointer dialog_)
98 struct var_type_dialog *dialog = dialog_;
100 if ( gtk_toggle_button_get_active (togglebutton) == TRUE)
101 refresh_active_button (dialog);
105 refresh_active_button (struct var_type_dialog *dialog)
109 for (i = 0; i < num_BUTTONS; i++)
111 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON (dialog->radioButton[i]);
113 if (gtk_toggle_button_get_active (toggle))
115 if (dialog->active_button != i)
117 dialog->active_button = i;
118 on_active_button_change (toggle, dialog);
124 g_return_if_reached ();
127 /* callback for when any of the radio buttons are toggled */
129 on_active_button_change (GtkToggleButton *togglebutton,
130 struct var_type_dialog *dialog)
135 W_DATE_FORMATS = 1 << 2,
136 W_DOLLAR_FORMATS = 1 << 3,
137 W_CC_FORMATS = 1 << 4,
140 enum widgets widgets;
143 switch (dialog->active_button)
148 case BUTTON_SCIENTIFIC:
149 widgets = W_WIDTH | W_DECIMALS;
157 widgets = W_DATE_FORMATS;
161 widgets = W_DOLLAR_FORMATS;
165 widgets = W_CC_FORMATS | W_WIDTH | W_DECIMALS;
169 /* No button active */
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);
184 dialog->fmt_l = *var_get_print_format (dialog->pv);
186 switch (dialog->active_button)
189 dialog->fmt_l.type = FMT_F;
192 dialog->fmt_l.type = FMT_COMMA;
195 dialog->fmt_l.type = FMT_DOT;
197 case BUTTON_SCIENTIFIC:
198 dialog->fmt_l.type = FMT_E;
201 dialog->fmt_l.type = FMT_A;
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];
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];
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];
223 fmt_fix_output (&dialog->fmt_l);
224 update_width_decimals (dialog);
229 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
230 static gint hide_dialog (GtkWidget *w, gpointer data);
234 add_to_group (GtkWidget *w, gpointer data)
236 GtkSizeGroup *sg = data;
238 gtk_size_group_add_widget (sg, w);
241 /* Set the local width and decimals entry boxes to reflec the local format */
243 update_width_decimals (const struct var_type_dialog *dialog)
248 text = g_strdup_printf ("%d", dialog->fmt_l.w);
249 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
252 text = g_strdup_printf ("%d", dialog->fmt_l.d);
253 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
257 /* Callback for when the custom treeview row is changed.
258 It sets dialog box to reflect the selected format */
260 preview_custom (GtkWidget *w, gpointer data)
264 struct var_type_dialog *dialog = data;
266 if ( dialog->active_button != BUTTON_CUSTOM )
269 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
270 dialog->fmt_l.d = atoi (text);
272 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
273 dialog->fmt_l.w = atoi (text);
276 if ( ! fmt_check_output (&dialog->fmt_l))
278 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
279 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
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);
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);
299 /* Callback for when a treeview row is changed.
300 It sets the fmt_l_spec to reflect the selected format */
302 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
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 = *(struct fmt_spec *) g_value_get_pointer (&the_value);
318 g_value_unset (&the_value);
322 /* Callback for when a treeview row is changed.
323 It sets the type of the fmt_l to reflect the selected type */
325 set_format_type_from_treeview (GtkTreeView *treeview, gpointer data)
327 static struct fmt_spec custom_format = {0,0,0};
328 struct var_type_dialog *dialog = data;
330 GValue the_value = {0};
332 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
334 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
336 gtk_tree_selection_get_selected (sel, &model, &iter);
338 gtk_tree_model_get_value (model, &iter, 1, &the_value);
340 dialog->fmt_l = custom_format;
341 dialog->fmt_l.type = g_value_get_int (&the_value);
343 g_value_unset (&the_value);
347 /* Create the structure */
348 struct var_type_dialog *
349 var_type_dialog_create (GtkWindow *toplevel)
352 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
354 GtkBuilder *xml = builder_new ("var-type-dialog.ui");
356 dialog->window = get_widget_assert (xml,"var_type_dialog");
357 dialog->active_button = -1;
360 g_signal_connect (dialog->window, "delete-event",
361 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
363 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
366 dialog->radioButton[BUTTON_NUMERIC] =
367 get_widget_assert (xml,"radiobutton1");
368 dialog->radioButton[BUTTON_COMMA] =
369 get_widget_assert (xml,"radiobutton2");
370 dialog->radioButton[BUTTON_DOT] =
371 get_widget_assert (xml,"radiobutton3");
372 dialog->radioButton[BUTTON_SCIENTIFIC] =
373 get_widget_assert (xml,"radiobutton4");
374 dialog->radioButton[BUTTON_DATE] =
375 get_widget_assert (xml,"radiobutton5");
376 dialog->radioButton[BUTTON_DOLLAR] =
377 get_widget_assert (xml,"radiobutton6");
378 dialog->radioButton[BUTTON_CUSTOM] =
379 get_widget_assert (xml,"radiobutton7");
380 dialog->radioButton[BUTTON_STRING] =
381 get_widget_assert (xml,"radiobutton8");
384 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
385 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
386 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
387 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
389 dialog->label_psample = get_widget_assert (xml, "psample_label");
390 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
393 dialog->entry_width = get_widget_assert (xml,"width_entry");
395 dialog->custom_currency_hbox = get_widget_assert (xml,
396 "custom_currency_hbox");
398 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
399 dialog->dollar_treeview =
400 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
402 dialog->custom_treeview =
403 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
406 dialog->ok = get_widget_assert (xml,"var_type_ok");
411 GtkListStore *list_store ;
413 GtkTreeViewColumn *column;
414 GtkCellRenderer *renderer ;
416 /* The "middle_box" is a vbox with serveral children.
417 However only one child is ever shown at a time.
418 We need to make sure that they all have the same width, to avoid
419 upleasant resizing effects */
420 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
422 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
423 add_to_group, sizeGroup);
426 for (i = 0 ; i < num_BUTTONS; ++i )
427 g_signal_connect (dialog->radioButton[i], "toggled",
428 G_CALLBACK (on_toggle), dialog);
430 /* Populate the date format tree view */
431 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
432 "date_format_list_view"));
434 renderer = gtk_cell_renderer_text_new ();
436 column = gtk_tree_view_column_new_with_attributes ("Title",
442 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
446 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
448 for ( i = 0 ; i < sizeof (date_format) / sizeof (date_format[0]) ; ++i )
450 const struct fmt_spec *f = &date_format[i];
451 gtk_list_store_append (list_store, &iter);
452 gtk_list_store_set (list_store, &iter,
453 0, fmt_date_template (f->type, f->w),
458 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
459 GTK_TREE_MODEL (list_store));
461 g_object_unref (list_store);
463 g_signal_connect (dialog->date_format_treeview, "cursor-changed",
464 G_CALLBACK (set_format_from_treeview), dialog);
467 /* populate the dollar treeview */
469 renderer = gtk_cell_renderer_text_new ();
471 column = gtk_tree_view_column_new_with_attributes ("Title",
477 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
481 list_store = gtk_list_store_new (2, G_TYPE_STRING,
484 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
486 char *template = settings_dollar_template (&dollar_format[i]);
487 gtk_list_store_append (list_store, &iter);
488 gtk_list_store_set (list_store, &iter,
490 1, &dollar_format[i],
495 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
496 GTK_TREE_MODEL (list_store));
498 g_object_unref (list_store);
500 g_signal_connect (dialog->dollar_treeview,
502 G_CALLBACK (set_format_from_treeview), dialog);
504 g_signal_connect_swapped (dialog->dollar_treeview,
506 G_CALLBACK (update_width_decimals), dialog);
509 /* populate the custom treeview */
511 renderer = gtk_cell_renderer_text_new ();
513 column = gtk_tree_view_column_new_with_attributes ("Title",
519 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
523 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
525 for ( i = 0 ; i < 5 ; ++i )
527 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
528 gtk_list_store_append (list_store, &iter);
529 gtk_list_store_set (list_store, &iter,
530 0, fmt_name (cc_fmts[i]),
535 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
536 GTK_TREE_MODEL (list_store));
538 g_object_unref (list_store);
541 g_signal_connect (dialog->custom_treeview,
543 G_CALLBACK (set_format_type_from_treeview), dialog);
546 g_signal_connect (dialog->custom_treeview,
548 G_CALLBACK (preview_custom), dialog);
551 g_signal_connect (dialog->entry_width,
553 G_CALLBACK (preview_custom), dialog);
556 g_signal_connect (dialog->entry_decimals,
558 G_CALLBACK (preview_custom), dialog);
561 /* Connect to the OK button */
562 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
566 /* And the cancel button */
567 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
568 G_CALLBACK (hide_dialog),
572 g_object_unref (xml);
578 /* Set a particular button to be active */
580 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
582 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
589 select_treeview_at_index (GtkTreeView *treeview, int index)
593 path = gtk_tree_path_new_from_indices (index, -1);
594 gtk_tree_view_set_cursor (treeview, path, 0, 0);
595 gtk_tree_path_free (path);
599 find_format (const struct fmt_spec *target,
600 const struct fmt_spec formats[], int n_formats)
604 for (i = 0; i < n_formats; i++)
605 if (fmt_equal (target, &formats[i]))
612 find_format_type (int target, const int types[], int n_types)
616 for (i = 0; i < n_types; i++)
617 if (target == types[i])
623 /* Set up the state of the dialog box to match the variable VAR */
625 var_type_dialog_set_state (struct var_type_dialog *dialog)
630 g_assert (dialog->pv);
632 /* Populate the radio button states */
633 switch (var_get_print_format (dialog->pv)->type)
637 button = BUTTON_NUMERIC;
640 button = BUTTON_STRING;
643 button = BUTTON_COMMA;
649 button = BUTTON_DOLLAR;
664 button = BUTTON_DATE;
671 button = BUTTON_CUSTOM;
675 var_type_dialog_set_active_button (dialog, button);
676 refresh_active_button (dialog);
677 on_active_button_change (GTK_TOGGLE_BUTTON (dialog->radioButton[button]),
682 /* Popup the dialog box */
684 var_type_dialog_show (struct var_type_dialog *dialog)
686 var_type_dialog_set_state (dialog);
688 gtk_widget_show (dialog->window);
691 /* Fills F with an output format specification with type TYPE, width
692 W, and D decimals. Iff it's a valid format, then return true.
695 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
700 return fmt_check_output (f);
706 /* Callbacks for the Variable Type Dialog Box */
708 /* Callback for when the var type dialog is closed using the OK button.
709 It sets the appropriate variable accordingly. */
711 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
713 struct var_type_dialog *dialog = data;
716 g_assert (dialog->pv);
719 gint width = atoi (gtk_entry_get_text
720 (GTK_ENTRY (dialog->entry_width)));
722 gint decimals = atoi (gtk_entry_get_text
723 (GTK_ENTRY (dialog->entry_decimals)));
727 struct fmt_spec spec;
728 switch (dialog->active_button)
732 result = make_output_format_try (&spec, FMT_A, width, 0);
735 result = make_output_format_try (&spec, FMT_F, width, decimals);
738 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
741 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
743 case BUTTON_SCIENTIFIC:
744 result = make_output_format_try (&spec, FMT_E, width, decimals);
748 if (! fmt_check_output (&dialog->fmt_l))
749 g_critical ("Invalid variable format");
751 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
754 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
757 g_critical ("Unknown variable type: %d", dialog->active_button) ;
762 if ( result == true )
764 var_set_width (dialog->pv, new_width);
765 var_set_both_formats (dialog->pv, &spec);
769 gtk_widget_hide (dialog->window);
777 hide_dialog (GtkWidget *w, gpointer data)
779 struct var_type_dialog *dialog = data;
781 gtk_widget_hide (dialog->window);