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);
300 get_index_from_treeview (GtkTreeView *treeview)
302 GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);
308 gtk_tree_selection_get_selected (selection, &model, &iter);
309 path = gtk_tree_model_get_path (model, &iter);
310 if (!path || gtk_tree_path_get_depth (path) < 1)
313 index = gtk_tree_path_get_indices (path)[0];
314 gtk_tree_path_free (path);
319 /* Callback for when a date treeview row is changed.
320 It sets the fmt_l_spec to reflect the selected format */
322 set_date_format_from_treeview (GtkTreeView *treeview,
323 struct var_type_dialog *dialog)
325 dialog->fmt_l = date_format[get_index_from_treeview (treeview)];
328 /* Callback for when a dollar treeview row is changed.
329 It sets the fmt_l_spec to reflect the selected format */
331 set_dollar_format_from_treeview (GtkTreeView *treeview,
332 struct var_type_dialog *dialog)
334 dialog->fmt_l = dollar_format[get_index_from_treeview (treeview)];
337 /* Callback for when a treeview row is changed.
338 It sets the type of the fmt_l to reflect the selected type */
340 set_custom_format_from_treeview (GtkTreeView *treeview,
341 struct var_type_dialog *dialog)
343 dialog->fmt_l.type = cc_format[get_index_from_treeview (treeview)];
346 /* Create the structure */
347 struct var_type_dialog *
348 var_type_dialog_create (GtkWindow *toplevel)
351 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
353 GtkBuilder *xml = builder_new ("var-type-dialog.ui");
355 dialog->window = get_widget_assert (xml,"var_type_dialog");
356 dialog->active_button = -1;
359 g_signal_connect (dialog->window, "delete-event",
360 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
362 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
365 dialog->radioButton[BUTTON_NUMERIC] =
366 get_widget_assert (xml,"radiobutton1");
367 dialog->radioButton[BUTTON_COMMA] =
368 get_widget_assert (xml,"radiobutton2");
369 dialog->radioButton[BUTTON_DOT] =
370 get_widget_assert (xml,"radiobutton3");
371 dialog->radioButton[BUTTON_SCIENTIFIC] =
372 get_widget_assert (xml,"radiobutton4");
373 dialog->radioButton[BUTTON_DATE] =
374 get_widget_assert (xml,"radiobutton5");
375 dialog->radioButton[BUTTON_DOLLAR] =
376 get_widget_assert (xml,"radiobutton6");
377 dialog->radioButton[BUTTON_CUSTOM] =
378 get_widget_assert (xml,"radiobutton7");
379 dialog->radioButton[BUTTON_STRING] =
380 get_widget_assert (xml,"radiobutton8");
383 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
384 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
385 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
386 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
388 dialog->label_psample = get_widget_assert (xml, "psample_label");
389 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
392 dialog->entry_width = get_widget_assert (xml,"width_entry");
394 dialog->custom_currency_hbox = get_widget_assert (xml,
395 "custom_currency_hbox");
397 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
398 dialog->dollar_treeview =
399 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
401 dialog->custom_treeview =
402 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
405 dialog->ok = get_widget_assert (xml,"var_type_ok");
410 GtkListStore *list_store ;
412 GtkTreeViewColumn *column;
413 GtkCellRenderer *renderer ;
415 /* The "middle_box" is a vbox with serveral children.
416 However only one child is ever shown at a time.
417 We need to make sure that they all have the same width, to avoid
418 upleasant resizing effects */
419 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
421 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
422 add_to_group, sizeGroup);
425 for (i = 0 ; i < num_BUTTONS; ++i )
426 g_signal_connect (dialog->radioButton[i], "toggled",
427 G_CALLBACK (on_toggle), dialog);
429 /* Populate the date format tree view */
430 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
431 "date_format_list_view"));
433 renderer = gtk_cell_renderer_text_new ();
435 column = gtk_tree_view_column_new_with_attributes ("Title",
441 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
445 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
447 for ( i = 0 ; i < sizeof (date_format) / sizeof (date_format[0]) ; ++i )
449 const struct fmt_spec *f = &date_format[i];
450 gtk_list_store_append (list_store, &iter);
451 gtk_list_store_set (list_store, &iter,
452 0, fmt_date_template (f->type, f->w),
457 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
458 GTK_TREE_MODEL (list_store));
460 g_object_unref (list_store);
462 g_signal_connect (dialog->date_format_treeview, "cursor-changed",
463 G_CALLBACK (set_date_format_from_treeview), dialog);
466 /* populate the dollar treeview */
468 renderer = gtk_cell_renderer_text_new ();
470 column = gtk_tree_view_column_new_with_attributes ("Title",
476 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
480 list_store = gtk_list_store_new (2, G_TYPE_STRING,
483 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
485 char *template = settings_dollar_template (&dollar_format[i]);
486 gtk_list_store_append (list_store, &iter);
487 gtk_list_store_set (list_store, &iter,
489 1, &dollar_format[i],
494 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
495 GTK_TREE_MODEL (list_store));
497 g_object_unref (list_store);
499 g_signal_connect (dialog->dollar_treeview,
501 G_CALLBACK (set_dollar_format_from_treeview), dialog);
503 g_signal_connect_swapped (dialog->dollar_treeview,
505 G_CALLBACK (update_width_decimals), dialog);
508 /* populate the custom treeview */
510 renderer = gtk_cell_renderer_text_new ();
512 column = gtk_tree_view_column_new_with_attributes ("Title",
518 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
522 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
524 for ( i = 0 ; i < 5 ; ++i )
526 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
527 gtk_list_store_append (list_store, &iter);
528 gtk_list_store_set (list_store, &iter,
529 0, fmt_name (cc_fmts[i]),
534 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
535 GTK_TREE_MODEL (list_store));
537 g_object_unref (list_store);
540 g_signal_connect (dialog->custom_treeview,
542 G_CALLBACK (set_custom_format_from_treeview), dialog);
545 g_signal_connect (dialog->custom_treeview,
547 G_CALLBACK (preview_custom), dialog);
550 g_signal_connect (dialog->entry_width,
552 G_CALLBACK (preview_custom), dialog);
555 g_signal_connect (dialog->entry_decimals,
557 G_CALLBACK (preview_custom), dialog);
560 /* Connect to the OK button */
561 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
565 /* And the cancel button */
566 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
567 G_CALLBACK (hide_dialog),
571 g_object_unref (xml);
577 /* Set a particular button to be active */
579 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
581 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
588 select_treeview_at_index (GtkTreeView *treeview, int index)
592 path = gtk_tree_path_new_from_indices (index, -1);
593 gtk_tree_view_set_cursor (treeview, path, 0, 0);
594 gtk_tree_path_free (path);
598 find_format (const struct fmt_spec *target,
599 const struct fmt_spec formats[], int n_formats)
603 for (i = 0; i < n_formats; i++)
604 if (fmt_equal (target, &formats[i]))
611 find_format_type (int target, const int types[], int n_types)
615 for (i = 0; i < n_types; i++)
616 if (target == types[i])
622 /* Set up the state of the dialog box to match the variable VAR */
624 var_type_dialog_set_state (struct var_type_dialog *dialog)
629 g_assert (dialog->pv);
631 /* Populate the radio button states */
632 switch (var_get_print_format (dialog->pv)->type)
636 button = BUTTON_NUMERIC;
639 button = BUTTON_STRING;
642 button = BUTTON_COMMA;
648 button = BUTTON_DOLLAR;
663 button = BUTTON_DATE;
670 button = BUTTON_CUSTOM;
674 var_type_dialog_set_active_button (dialog, button);
675 refresh_active_button (dialog);
676 on_active_button_change (GTK_TOGGLE_BUTTON (dialog->radioButton[button]),
681 /* Popup the dialog box */
683 var_type_dialog_show (struct var_type_dialog *dialog)
685 var_type_dialog_set_state (dialog);
687 gtk_widget_show (dialog->window);
690 /* Fills F with an output format specification with type TYPE, width
691 W, and D decimals. Iff it's a valid format, then return true.
694 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
699 return fmt_check_output (f);
705 /* Callbacks for the Variable Type Dialog Box */
707 /* Callback for when the var type dialog is closed using the OK button.
708 It sets the appropriate variable accordingly. */
710 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
712 struct var_type_dialog *dialog = data;
715 g_assert (dialog->pv);
718 gint width = atoi (gtk_entry_get_text
719 (GTK_ENTRY (dialog->entry_width)));
721 gint decimals = atoi (gtk_entry_get_text
722 (GTK_ENTRY (dialog->entry_decimals)));
726 struct fmt_spec spec;
727 switch (dialog->active_button)
731 result = make_output_format_try (&spec, FMT_A, width, 0);
734 result = make_output_format_try (&spec, FMT_F, width, decimals);
737 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
740 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
742 case BUTTON_SCIENTIFIC:
743 result = make_output_format_try (&spec, FMT_E, width, decimals);
747 if (! fmt_check_output (&dialog->fmt_l))
748 g_critical ("Invalid variable format");
750 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
753 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
756 g_critical ("Unknown variable type: %d", dialog->active_button) ;
761 if ( result == true )
763 var_set_width (dialog->pv, new_width);
764 var_set_both_formats (dialog->pv, &spec);
768 gtk_widget_hide (dialog->window);
776 hide_dialog (GtkWidget *w, gpointer data)
778 struct var_type_dialog *dialog = data;
780 gtk_widget_hide (dialog->window);