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 *);
93 static void on_width_changed (GtkEntry *, struct var_type_dialog *);
94 static void on_decimals_changed (GtkEntry *, struct var_type_dialog *);
96 /* callback for when any of the radio buttons are toggled */
98 on_toggle (GtkToggleButton *togglebutton, gpointer dialog_)
100 struct var_type_dialog *dialog = dialog_;
102 if ( gtk_toggle_button_get_active (togglebutton) == TRUE)
103 refresh_active_button (dialog);
107 refresh_active_button (struct var_type_dialog *dialog)
111 for (i = 0; i < num_BUTTONS; i++)
113 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON (dialog->radioButton[i]);
115 if (gtk_toggle_button_get_active (toggle))
117 if (dialog->active_button != i)
119 dialog->active_button = i;
120 on_active_button_change (toggle, dialog);
126 g_return_if_reached ();
130 update_adj_ranges (struct var_type_dialog *dialog)
132 enum fmt_type type = dialog->fmt_l.type;
133 const enum fmt_use use = FMT_FOR_OUTPUT;
134 int min_w = fmt_min_width (type, use);
135 int max_w = fmt_max_width (type, use);
136 int max_d = fmt_max_decimals (type, max_w, use);
138 g_object_set (dialog->adj_width,
139 "lower", (double) min_w,
140 "upper", (double) max_w,
143 g_object_set (dialog->adj_decimals,
145 "upper", (double) max_d,
149 /* callback for when any of the radio buttons are toggled */
151 on_active_button_change (GtkToggleButton *togglebutton,
152 struct var_type_dialog *dialog)
157 W_DATE_FORMATS = 1 << 2,
158 W_DOLLAR_FORMATS = 1 << 3,
159 W_CC_FORMATS = 1 << 4,
162 enum widgets widgets;
165 switch (dialog->active_button)
170 case BUTTON_SCIENTIFIC:
171 widgets = W_WIDTH | W_DECIMALS;
179 widgets = W_DATE_FORMATS;
183 widgets = W_DOLLAR_FORMATS;
187 widgets = W_CC_FORMATS | W_WIDTH | W_DECIMALS;
191 /* No button active */
195 gtk_widget_set_visible (dialog->width_decimals, (widgets & W_WIDTH) != 0);
196 gtk_widget_set_visible (dialog->entry_width, (widgets & W_WIDTH) != 0);
197 gtk_widget_set_visible (dialog->entry_decimals, (widgets & W_DECIMALS) != 0);
198 gtk_widget_set_visible (dialog->label_decimals, (widgets & W_DECIMALS) != 0);
199 gtk_widget_set_visible (dialog->date_format_list,
200 (widgets & W_DATE_FORMATS) != 0);
201 gtk_widget_set_visible (dialog->custom_currency_hbox,
202 (widgets & W_CC_FORMATS) != 0);
203 gtk_widget_set_visible (dialog->dollar_window,
204 (widgets & W_DOLLAR_FORMATS) != 0);
206 dialog->fmt_l = *var_get_print_format (dialog->pv);
208 switch (dialog->active_button)
211 dialog->fmt_l.type = FMT_F;
214 dialog->fmt_l.type = FMT_COMMA;
217 dialog->fmt_l.type = FMT_DOT;
219 case BUTTON_SCIENTIFIC:
220 dialog->fmt_l.type = FMT_E;
223 dialog->fmt_l.type = FMT_A;
226 indx = find_format (&dialog->fmt_l, date_format,
227 sizeof date_format / sizeof *date_format);
228 select_treeview_at_index (dialog->date_format_treeview, indx);
229 dialog->fmt_l = date_format[indx];
232 indx = find_format (&dialog->fmt_l, dollar_format,
233 sizeof dollar_format / sizeof *dollar_format);
234 select_treeview_at_index (dialog->dollar_treeview, indx);
235 dialog->fmt_l = dollar_format[indx];
238 indx = find_format_type (dialog->fmt_l.type, cc_format,
239 sizeof cc_format / sizeof *cc_format);
240 select_treeview_at_index (dialog->custom_treeview, indx);
241 dialog->fmt_l.type = cc_format[indx];
245 fmt_fix_output (&dialog->fmt_l);
246 update_adj_ranges (dialog);
247 update_width_decimals (dialog);
252 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
253 static gint hide_dialog (GtkWidget *w, gpointer data);
257 add_to_group (GtkWidget *w, gpointer data)
259 GtkSizeGroup *sg = data;
261 gtk_size_group_add_widget (sg, w);
264 /* Set the local width and decimals entry boxes to reflec the local format */
266 update_width_decimals (const struct var_type_dialog *dialog)
268 gtk_adjustment_set_value (dialog->adj_width, dialog->fmt_l.w);
269 gtk_adjustment_set_value (dialog->adj_decimals, dialog->fmt_l.d);
273 on_width_changed (GtkEntry *entry, struct var_type_dialog *dialog)
275 int w = atoi (gtk_entry_get_text (GTK_ENTRY (dialog->entry_width)));
276 fmt_change_width (&dialog->fmt_l, w, FMT_FOR_OUTPUT);
277 update_width_decimals (dialog);
281 on_decimals_changed (GtkEntry *entry, struct var_type_dialog *dialog)
283 int d = atoi (gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals)));
284 fmt_change_decimals (&dialog->fmt_l, d, FMT_FOR_OUTPUT);
285 update_width_decimals (dialog);
288 /* Callback for when the custom treeview row is changed.
289 It sets dialog box to reflect the selected format */
291 preview_custom (GtkWidget *w, gpointer data)
295 struct var_type_dialog *dialog = data;
297 if ( dialog->active_button != BUTTON_CUSTOM )
300 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
301 dialog->fmt_l.d = atoi (text);
303 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
304 dialog->fmt_l.w = atoi (text);
307 if ( ! fmt_check_output (&dialog->fmt_l))
309 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
310 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
318 sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
319 gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
320 g_free (sample_text);
323 sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
324 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
325 g_free (sample_text);
331 get_index_from_treeview (GtkTreeView *treeview)
333 GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);
339 gtk_tree_selection_get_selected (selection, &model, &iter);
340 path = gtk_tree_model_get_path (model, &iter);
341 if (!path || gtk_tree_path_get_depth (path) < 1)
344 index = gtk_tree_path_get_indices (path)[0];
345 gtk_tree_path_free (path);
350 /* Callback for when a date treeview row is changed.
351 It sets the fmt_l_spec to reflect the selected format */
353 set_date_format_from_treeview (GtkTreeView *treeview,
354 struct var_type_dialog *dialog)
356 dialog->fmt_l = date_format[get_index_from_treeview (treeview)];
359 /* Callback for when a dollar treeview row is changed.
360 It sets the fmt_l_spec to reflect the selected format */
362 set_dollar_format_from_treeview (GtkTreeView *treeview,
363 struct var_type_dialog *dialog)
365 dialog->fmt_l = dollar_format[get_index_from_treeview (treeview)];
368 /* Callback for when a treeview row is changed.
369 It sets the type of the fmt_l to reflect the selected type */
371 set_custom_format_from_treeview (GtkTreeView *treeview,
372 struct var_type_dialog *dialog)
374 dialog->fmt_l.type = cc_format[get_index_from_treeview (treeview)];
375 update_adj_ranges (dialog);
376 fmt_fix_output (&dialog->fmt_l);
377 update_width_decimals (dialog);
380 /* Create the structure */
381 struct var_type_dialog *
382 var_type_dialog_create (GtkWindow *toplevel)
385 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
387 GtkBuilder *xml = builder_new ("var-type-dialog.ui");
389 dialog->window = get_widget_assert (xml,"var_type_dialog");
390 dialog->active_button = -1;
393 g_signal_connect (dialog->window, "delete-event",
394 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
396 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
399 dialog->radioButton[BUTTON_NUMERIC] =
400 get_widget_assert (xml,"radiobutton1");
401 dialog->radioButton[BUTTON_COMMA] =
402 get_widget_assert (xml,"radiobutton2");
403 dialog->radioButton[BUTTON_DOT] =
404 get_widget_assert (xml,"radiobutton3");
405 dialog->radioButton[BUTTON_SCIENTIFIC] =
406 get_widget_assert (xml,"radiobutton4");
407 dialog->radioButton[BUTTON_DATE] =
408 get_widget_assert (xml,"radiobutton5");
409 dialog->radioButton[BUTTON_DOLLAR] =
410 get_widget_assert (xml,"radiobutton6");
411 dialog->radioButton[BUTTON_CUSTOM] =
412 get_widget_assert (xml,"radiobutton7");
413 dialog->radioButton[BUTTON_STRING] =
414 get_widget_assert (xml,"radiobutton8");
417 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
418 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
419 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
420 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
421 dialog->adj_decimals = gtk_spin_button_get_adjustment (
422 GTK_SPIN_BUTTON (dialog->entry_decimals));
424 dialog->label_psample = get_widget_assert (xml, "psample_label");
425 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
428 dialog->entry_width = get_widget_assert (xml,"width_entry");
429 dialog->adj_width = gtk_spin_button_get_adjustment (
430 GTK_SPIN_BUTTON (dialog->entry_width));
431 dialog->custom_currency_hbox = get_widget_assert (xml,
432 "custom_currency_hbox");
434 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
435 dialog->dollar_treeview =
436 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
438 dialog->custom_treeview =
439 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
442 dialog->ok = get_widget_assert (xml,"var_type_ok");
447 GtkListStore *list_store ;
449 GtkTreeViewColumn *column;
450 GtkCellRenderer *renderer ;
452 /* The "middle_box" is a vbox with serveral children.
453 However only one child is ever shown at a time.
454 We need to make sure that they all have the same width, to avoid
455 upleasant resizing effects */
456 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
458 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
459 add_to_group, sizeGroup);
462 for (i = 0 ; i < num_BUTTONS; ++i )
463 g_signal_connect (dialog->radioButton[i], "toggled",
464 G_CALLBACK (on_toggle), dialog);
466 /* Populate the date format tree view */
467 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
468 "date_format_list_view"));
470 renderer = gtk_cell_renderer_text_new ();
472 column = gtk_tree_view_column_new_with_attributes ("Title",
478 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
482 list_store = gtk_list_store_new (1, G_TYPE_STRING);
484 for ( i = 0 ; i < sizeof (date_format) / sizeof (date_format[0]) ; ++i )
486 const struct fmt_spec *f = &date_format[i];
487 gtk_list_store_append (list_store, &iter);
488 gtk_list_store_set (list_store, &iter,
489 0, fmt_date_template (f->type, f->w),
493 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
494 GTK_TREE_MODEL (list_store));
496 g_object_unref (list_store);
498 g_signal_connect (dialog->date_format_treeview, "cursor-changed",
499 G_CALLBACK (set_date_format_from_treeview), dialog);
502 /* populate the dollar treeview */
504 renderer = gtk_cell_renderer_text_new ();
506 column = gtk_tree_view_column_new_with_attributes ("Title",
512 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
516 list_store = gtk_list_store_new (1, G_TYPE_STRING);
518 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
520 char *template = settings_dollar_template (&dollar_format[i]);
521 gtk_list_store_append (list_store, &iter);
522 gtk_list_store_set (list_store, &iter,
528 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
529 GTK_TREE_MODEL (list_store));
531 g_object_unref (list_store);
533 g_signal_connect (dialog->dollar_treeview,
535 G_CALLBACK (set_dollar_format_from_treeview), dialog);
537 g_signal_connect_swapped (dialog->dollar_treeview,
539 G_CALLBACK (update_width_decimals), dialog);
542 /* populate the custom treeview */
544 renderer = gtk_cell_renderer_text_new ();
546 column = gtk_tree_view_column_new_with_attributes ("Title",
552 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
556 list_store = gtk_list_store_new (1, G_TYPE_STRING);
558 for ( i = 0 ; i < 5 ; ++i )
560 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
561 gtk_list_store_append (list_store, &iter);
562 gtk_list_store_set (list_store, &iter,
563 0, fmt_name (cc_fmts[i]),
567 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
568 GTK_TREE_MODEL (list_store));
570 g_object_unref (list_store);
573 g_signal_connect (dialog->custom_treeview,
575 G_CALLBACK (set_custom_format_from_treeview), dialog);
578 g_signal_connect (dialog->custom_treeview,
580 G_CALLBACK (preview_custom), dialog);
583 g_signal_connect (dialog->entry_width, "changed",
584 G_CALLBACK (on_width_changed), dialog);
585 g_signal_connect (dialog->entry_decimals, "changed",
586 G_CALLBACK (on_decimals_changed), dialog);
588 g_signal_connect (dialog->entry_width,
590 G_CALLBACK (preview_custom), dialog);
593 g_signal_connect (dialog->entry_decimals,
595 G_CALLBACK (preview_custom), dialog);
598 /* Connect to the OK button */
599 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
603 /* And the cancel button */
604 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
605 G_CALLBACK (hide_dialog),
609 g_object_unref (xml);
615 /* Set a particular button to be active */
617 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
619 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
626 select_treeview_at_index (GtkTreeView *treeview, int index)
630 path = gtk_tree_path_new_from_indices (index, -1);
631 gtk_tree_view_set_cursor (treeview, path, 0, 0);
632 gtk_tree_path_free (path);
636 find_format (const struct fmt_spec *target,
637 const struct fmt_spec formats[], int n_formats)
641 for (i = 0; i < n_formats; i++)
642 if (fmt_equal (target, &formats[i]))
649 find_format_type (int target, const int types[], int n_types)
653 for (i = 0; i < n_types; i++)
654 if (target == types[i])
660 /* Set up the state of the dialog box to match the variable VAR */
662 var_type_dialog_set_state (struct var_type_dialog *dialog)
667 g_assert (dialog->pv);
669 /* Populate the radio button states */
670 switch (var_get_print_format (dialog->pv)->type)
674 button = BUTTON_NUMERIC;
677 button = BUTTON_STRING;
680 button = BUTTON_COMMA;
686 button = BUTTON_DOLLAR;
701 button = BUTTON_DATE;
708 button = BUTTON_CUSTOM;
712 var_type_dialog_set_active_button (dialog, button);
713 refresh_active_button (dialog);
714 on_active_button_change (GTK_TOGGLE_BUTTON (dialog->radioButton[button]),
719 /* Popup the dialog box */
721 var_type_dialog_show (struct var_type_dialog *dialog)
723 var_type_dialog_set_state (dialog);
725 gtk_widget_show (dialog->window);
728 /* Callbacks for the Variable Type Dialog Box */
730 /* Callback for when the var type dialog is closed using the OK button.
731 It sets the appropriate variable accordingly. */
733 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
735 struct var_type_dialog *dialog = data;
737 var_set_width (dialog->pv, fmt_var_width (&dialog->fmt_l));
738 var_set_both_formats (dialog->pv, &dialog->fmt_l);
740 gtk_widget_hide (dialog->window);
748 hide_dialog (GtkWidget *w, gpointer data)
750 struct var_type_dialog *dialog = data;
752 gtk_widget_hide (dialog->window);