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 void select_treeview_at_index (GtkTreeView *, int index);
84 static void select_treeview_from_format
85 (GtkTreeView *treeview, const struct fmt_spec *fmt);
87 static void select_treeview_from_format_type (GtkTreeView *treeview,
91 /* callback for when any of the radio buttons are toggled */
93 on_toggle_1 (GtkToggleButton *togglebutton, gpointer dialog_)
95 GtkWidget *widget = GTK_WIDGET (togglebutton);
96 struct var_type_dialog *dialog = dialog_;
99 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
102 for (i = 0; i < num_BUTTONS; i++)
103 if (widget == dialog->radioButton[i])
105 dialog->active_button = i;
108 g_return_if_reached ();
111 static void update_width_decimals (const struct var_type_dialog *dialog);
113 /* callback for when any of the radio buttons are toggled */
115 on_toggle_2 (GtkToggleButton *togglebutton, gpointer user_data)
117 struct var_type_dialog *dialog = user_data;
118 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
120 switch (dialog->active_button)
123 gtk_widget_hide (dialog->date_format_list);
126 gtk_widget_hide (dialog->custom_currency_hbox);
129 gtk_widget_hide (dialog->dollar_window);
132 gtk_widget_show (dialog->label_decimals);
133 gtk_widget_show (dialog->entry_decimals);
139 dialog->fmt_l = *var_get_print_format (dialog->pv);
141 switch (dialog->active_button)
144 gtk_widget_show_all (dialog->width_decimals);
145 dialog->fmt_l.type = FMT_F;
148 gtk_widget_show_all (dialog->width_decimals);
149 dialog->fmt_l.type = FMT_COMMA;
152 gtk_widget_show_all (dialog->width_decimals);
153 dialog->fmt_l.type = FMT_DOT;
155 case BUTTON_SCIENTIFIC:
156 gtk_widget_show_all (dialog->width_decimals);
157 dialog->fmt_l.type = FMT_E;
160 dialog->fmt_l.type = FMT_A;
161 gtk_widget_show (dialog->entry_width);
162 gtk_widget_show (dialog->width_decimals);
163 gtk_widget_hide (dialog->label_decimals);
164 gtk_widget_hide (dialog->entry_decimals);
167 select_treeview_at_index (dialog->date_format_treeview, 0);
168 dialog->fmt_l = date_format[0];
169 gtk_widget_hide (dialog->width_decimals);
170 gtk_widget_show (dialog->date_format_list);
173 select_treeview_at_index (dialog->dollar_treeview, 0);
174 dialog->fmt_l = dollar_format[0];
175 gtk_widget_show (dialog->dollar_window);
176 gtk_widget_show_all (dialog->width_decimals);
179 select_treeview_at_index (dialog->custom_treeview, 0);
180 dialog->fmt_l.type = cc_format[0];
182 gtk_widget_show (dialog->width_decimals);
183 gtk_widget_show (dialog->custom_currency_hbox);
187 fmt_fix_output (&dialog->fmt_l);
188 update_width_decimals (dialog);
193 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
194 static gint hide_dialog (GtkWidget *w, gpointer data);
198 add_to_group (GtkWidget *w, gpointer data)
200 GtkSizeGroup *sg = data;
202 gtk_size_group_add_widget (sg, w);
205 /* Set the local width and decimals entry boxes to reflec the local format */
207 update_width_decimals (const struct var_type_dialog *dialog)
212 text = g_strdup_printf ("%d", dialog->fmt_l.w);
213 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
216 text = g_strdup_printf ("%d", dialog->fmt_l.d);
217 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
221 /* Callback for when the custom treeview row is changed.
222 It sets dialog box to reflect the selected format */
224 preview_custom (GtkWidget *w, gpointer data)
228 struct var_type_dialog *dialog = data;
230 if ( dialog->active_button != BUTTON_CUSTOM )
233 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
234 dialog->fmt_l.d = atoi (text);
236 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
237 dialog->fmt_l.w = atoi (text);
240 if ( ! fmt_check_output (&dialog->fmt_l))
242 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
243 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
251 sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
252 gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
253 g_free (sample_text);
256 sample_text = g_strchug (data_out (&v, NULL, &dialog->fmt_l));
257 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
258 g_free (sample_text);
263 /* Callback for when a treeview row is changed.
264 It sets the fmt_l_spec to reflect the selected format */
266 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
268 struct var_type_dialog *dialog = data;
270 GValue the_value = {0};
272 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
274 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
276 gtk_tree_selection_get_selected (sel, &model, &iter);
278 gtk_tree_model_get_value (model, &iter, 1, &the_value);
280 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value);
282 g_value_unset (&the_value);
286 /* Callback for when a treeview row is changed.
287 It sets the type of the fmt_l to reflect the selected type */
289 set_format_type_from_treeview (GtkTreeView *treeview, gpointer data)
291 static struct fmt_spec custom_format = {0,0,0};
292 struct var_type_dialog *dialog = data;
294 GValue the_value = {0};
296 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
298 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
300 gtk_tree_selection_get_selected (sel, &model, &iter);
302 gtk_tree_model_get_value (model, &iter, 1, &the_value);
304 dialog->fmt_l = custom_format;
305 dialog->fmt_l.type = g_value_get_int (&the_value);
307 g_value_unset (&the_value);
311 /* Create the structure */
312 struct var_type_dialog *
313 var_type_dialog_create (GtkWindow *toplevel)
316 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
318 GtkBuilder *xml = builder_new ("var-type-dialog.ui");
320 dialog->window = get_widget_assert (xml,"var_type_dialog");
321 dialog->active_button = -1;
324 g_signal_connect (dialog->window, "delete-event",
325 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
327 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
330 dialog->radioButton[BUTTON_NUMERIC] =
331 get_widget_assert (xml,"radiobutton1");
332 dialog->radioButton[BUTTON_COMMA] =
333 get_widget_assert (xml,"radiobutton2");
334 dialog->radioButton[BUTTON_DOT] =
335 get_widget_assert (xml,"radiobutton3");
336 dialog->radioButton[BUTTON_SCIENTIFIC] =
337 get_widget_assert (xml,"radiobutton4");
338 dialog->radioButton[BUTTON_DATE] =
339 get_widget_assert (xml,"radiobutton5");
340 dialog->radioButton[BUTTON_DOLLAR] =
341 get_widget_assert (xml,"radiobutton6");
342 dialog->radioButton[BUTTON_CUSTOM] =
343 get_widget_assert (xml,"radiobutton7");
344 dialog->radioButton[BUTTON_STRING] =
345 get_widget_assert (xml,"radiobutton8");
348 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
349 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
350 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
351 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
353 dialog->label_psample = get_widget_assert (xml, "psample_label");
354 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
357 dialog->entry_width = get_widget_assert (xml,"width_entry");
359 dialog->custom_currency_hbox = get_widget_assert (xml,
360 "custom_currency_hbox");
362 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
363 dialog->dollar_treeview =
364 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
366 dialog->custom_treeview =
367 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
370 dialog->ok = get_widget_assert (xml,"var_type_ok");
375 GtkListStore *list_store ;
377 GtkTreeViewColumn *column;
378 GtkCellRenderer *renderer ;
380 /* The "middle_box" is a vbox with serveral children.
381 However only one child is ever shown at a time.
382 We need to make sure that they all have the same width, to avoid
383 upleasant resizing effects */
384 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
386 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
387 add_to_group, sizeGroup);
390 for (i = 0 ; i < num_BUTTONS; ++i )
392 g_signal_connect (dialog->radioButton[i], "toggled",
393 G_CALLBACK (on_toggle_1), dialog);
395 g_signal_connect (dialog->radioButton[i], "toggled",
396 G_CALLBACK (on_toggle_2), dialog);
399 /* Populate the date format tree view */
400 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
401 "date_format_list_view"));
403 renderer = gtk_cell_renderer_text_new ();
405 column = gtk_tree_view_column_new_with_attributes ("Title",
411 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
415 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
417 for ( i = 0 ; i < sizeof (date_format) / sizeof (date_format[0]) ; ++i )
419 const struct fmt_spec *f = &date_format[i];
420 gtk_list_store_append (list_store, &iter);
421 gtk_list_store_set (list_store, &iter,
422 0, fmt_date_template (f->type, f->w),
427 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
428 GTK_TREE_MODEL (list_store));
430 g_object_unref (list_store);
432 g_signal_connect (dialog->date_format_treeview, "cursor-changed",
433 G_CALLBACK (set_format_from_treeview), dialog);
436 /* populate the dollar treeview */
438 renderer = gtk_cell_renderer_text_new ();
440 column = gtk_tree_view_column_new_with_attributes ("Title",
446 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
450 list_store = gtk_list_store_new (2, G_TYPE_STRING,
453 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
455 char *template = settings_dollar_template (&dollar_format[i]);
456 gtk_list_store_append (list_store, &iter);
457 gtk_list_store_set (list_store, &iter,
459 1, &dollar_format[i],
464 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
465 GTK_TREE_MODEL (list_store));
467 g_object_unref (list_store);
469 g_signal_connect (dialog->dollar_treeview,
471 G_CALLBACK (set_format_from_treeview), dialog);
473 g_signal_connect_swapped (dialog->dollar_treeview,
475 G_CALLBACK (update_width_decimals), dialog);
478 /* populate the custom treeview */
480 renderer = gtk_cell_renderer_text_new ();
482 column = gtk_tree_view_column_new_with_attributes ("Title",
488 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
492 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
494 for ( i = 0 ; i < 5 ; ++i )
496 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
497 gtk_list_store_append (list_store, &iter);
498 gtk_list_store_set (list_store, &iter,
499 0, fmt_name (cc_fmts[i]),
504 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
505 GTK_TREE_MODEL (list_store));
507 g_object_unref (list_store);
510 g_signal_connect (dialog->custom_treeview,
512 G_CALLBACK (set_format_type_from_treeview), dialog);
515 g_signal_connect (dialog->custom_treeview,
517 G_CALLBACK (preview_custom), dialog);
520 g_signal_connect (dialog->entry_width,
522 G_CALLBACK (preview_custom), dialog);
525 g_signal_connect (dialog->entry_decimals,
527 G_CALLBACK (preview_custom), dialog);
530 /* Connect to the OK button */
531 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
535 /* And the cancel button */
536 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
537 G_CALLBACK (hide_dialog),
541 g_object_unref (xml);
547 /* Set a particular button to be active */
549 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
551 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
553 dialog->active_button = b;
559 select_treeview_at_index (GtkTreeView *treeview, int index)
563 path = gtk_tree_path_new_from_indices (index, -1);
564 gtk_tree_view_set_cursor (treeview, path, 0, 0);
565 gtk_tree_path_free (path);
568 /* Set the TREEVIEW list cursor to the item described by FMT */
570 select_treeview_from_format (GtkTreeView *treeview, const struct fmt_spec *fmt)
575 We do this with a linear search through the model --- hardly
576 efficient, but the list is short ... */
579 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
582 for (success = gtk_tree_model_get_iter_first (model, &iter);
584 success = gtk_tree_model_iter_next (model, &iter))
586 const struct fmt_spec *spec;
590 gtk_tree_model_get_value (model, &iter, 1, &value);
592 spec = g_value_get_pointer (&value);
594 g_value_unset (&value);
596 if ( 0 == memcmp (spec, fmt, sizeof (struct fmt_spec)))
602 path = gtk_tree_model_get_path (model, &iter);
605 gtk_tree_view_set_cursor (treeview, path, 0, 0);
606 gtk_tree_path_free (path);
610 char str[FMT_STRING_LEN_MAX + 1];
611 g_warning ("Unusual date format: %s\n", fmt_to_string (fmt, str));
616 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
618 select_treeview_from_format_type (GtkTreeView *treeview,
624 We do this with a linear search through the model --- hardly
625 efficient, but the list is short ... */
628 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
631 for (success = gtk_tree_model_get_iter_first (model, &iter);
633 success = gtk_tree_model_iter_next (model, &iter))
639 gtk_tree_model_get_value (model, &iter, 1, &value);
641 spec = g_value_get_int (&value);
643 g_value_unset (&value);
645 if ( spec == fmt_type)
649 path = gtk_tree_model_get_path (model, &iter);
652 gtk_tree_view_set_cursor (treeview, path, 0, 0);
653 gtk_tree_path_free (path);
656 g_warning ("Unknown custom type %d\n", fmt_type);
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)
664 const struct fmt_spec *format ;
665 GString *str = g_string_new ("");
668 g_assert (dialog->pv);
670 /* Populate width and decimals */
671 format = var_get_print_format (dialog->pv);
673 g_string_printf (str, "%d", format->d);
675 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals),
678 g_string_printf (str, "%d", format->w);
680 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width),
683 g_string_free (str, TRUE);
685 /* Populate the radio button states */
686 switch (format->type)
689 var_type_dialog_set_active_button (dialog, BUTTON_NUMERIC);
690 gtk_widget_show_all (dialog->width_decimals);
693 var_type_dialog_set_active_button (dialog, BUTTON_STRING);
694 gtk_widget_hide (dialog->label_decimals);
695 gtk_widget_hide (dialog->entry_decimals);
698 var_type_dialog_set_active_button (dialog, BUTTON_COMMA);
699 gtk_widget_show_all (dialog->width_decimals);
702 var_type_dialog_set_active_button (dialog, BUTTON_DOT);
703 gtk_widget_show_all (dialog->width_decimals);
706 var_type_dialog_set_active_button (dialog, BUTTON_DOLLAR);
707 gtk_widget_show_all (dialog->width_decimals);
709 select_treeview_from_format (dialog->dollar_treeview, format);
724 var_type_dialog_set_active_button (dialog, BUTTON_DATE);
725 gtk_widget_hide (dialog->width_decimals);
726 gtk_widget_show (dialog->date_format_list);
727 select_treeview_from_format (dialog->date_format_treeview, format);
734 var_type_dialog_set_active_button (dialog, BUTTON_CUSTOM);
735 select_treeview_from_format_type (dialog->custom_treeview,
737 gtk_widget_show_all (dialog->width_decimals);
740 gtk_widget_show_all (dialog->width_decimals);
746 /* Popup the dialog box */
748 var_type_dialog_show (struct var_type_dialog *dialog)
750 var_type_dialog_set_state (dialog);
752 gtk_widget_show (dialog->window);
755 /* Fills F with an output format specification with type TYPE, width
756 W, and D decimals. Iff it's a valid format, then return true.
759 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
764 return fmt_check_output (f);
770 /* Callbacks for the Variable Type Dialog Box */
772 /* Callback for when the var type dialog is closed using the OK button.
773 It sets the appropriate variable accordingly. */
775 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
777 struct var_type_dialog *dialog = data;
780 g_assert (dialog->pv);
783 gint width = atoi (gtk_entry_get_text
784 (GTK_ENTRY (dialog->entry_width)));
786 gint decimals = atoi (gtk_entry_get_text
787 (GTK_ENTRY (dialog->entry_decimals)));
791 struct fmt_spec spec;
792 switch (dialog->active_button)
796 result = make_output_format_try (&spec, FMT_A, width, 0);
799 result = make_output_format_try (&spec, FMT_F, width, decimals);
802 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
805 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
807 case BUTTON_SCIENTIFIC:
808 result = make_output_format_try (&spec, FMT_E, width, decimals);
812 if (! fmt_check_output (&dialog->fmt_l))
813 g_critical ("Invalid variable format");
815 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
818 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
821 g_critical ("Unknown variable type: %d", dialog->active_button) ;
826 if ( result == true )
828 var_set_width (dialog->pv, new_width);
829 var_set_both_formats (dialog->pv, &spec);
833 gtk_widget_hide (dialog->window);
841 hide_dialog (GtkWidget *w, gpointer data)
843 struct var_type_dialog *dialog = data;
845 gtk_widget_hide (dialog->window);