2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005, 2006 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 /* This module describes the behaviour of the Variable Type dialog box used
22 for inputing the variable type in the var sheet */
27 #include <glade/glade.h>
32 #include "var-type-dialog.h"
36 #include <data/variable.h>
37 #include <data/settings.h>
38 #include <libpspp/message.h>
43 struct var_type_dialog *dialog;
54 static const struct format_opt format_option[] =
56 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
57 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
58 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
59 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
60 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
61 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
62 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
63 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
64 { "yyddd", {FMT_JDATE, 5, 0} },
65 { "yyyyddd", {FMT_JDATE, 7, 0} },
66 { "q Q yyyy", {FMT_QYR, 8, 0} },
67 { "q Q yy", {FMT_QYR, 6, 0} },
68 { "mmm yyyy", {FMT_MOYR, 8, 0} },
69 { "mmm yy", {FMT_MOYR, 6, 0} },
70 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
71 { "dd WK yy", {FMT_WKYR, 8, 0} },
72 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}}
76 static const struct fmt_spec dollar_format[] =
92 static const int cc_format[] =
102 static void select_treeview_from_format
103 (GtkTreeView *treeview, const struct fmt_spec *fmt);
105 static void select_treeview_from_format_type (GtkTreeView *treeview,
109 /* callback for when any of the radio buttons are toggled */
111 on_toggle_1 (GtkToggleButton *togglebutton, gpointer user_data)
113 struct tgs *tgs = user_data;
115 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
118 tgs->dialog->active_button = tgs->button;
121 static void update_width_decimals (const struct var_type_dialog *dialog);
123 #define force_max(x, val) if (x > val) x = val
126 Set the local format from the variable
127 and force them to have sensible values */
129 set_local_width_decimals (struct var_type_dialog *dialog)
131 dialog->fmt_l = * var_get_write_format (dialog->pv);
133 switch (dialog->active_button)
136 force_max ( dialog->fmt_l.w, 255);
139 force_max ( dialog->fmt_l.w, 40);
140 force_max ( dialog->fmt_l.d, 16);
146 /* callback for when any of the radio buttons are toggled */
148 on_toggle_2 (GtkToggleButton *togglebutton, gpointer user_data)
150 struct var_type_dialog *dialog = user_data;
151 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
153 switch (dialog->active_button)
156 gtk_widget_hide (dialog->date_format_list);
159 gtk_widget_hide (dialog->custom_currency_hbox);
162 gtk_widget_hide (dialog->dollar_window);
165 gtk_widget_show (dialog->label_decimals);
166 gtk_widget_show (dialog->entry_decimals);
172 set_local_width_decimals (dialog);
173 update_width_decimals (dialog);
175 switch (dialog->active_button)
178 gtk_widget_show (dialog->width_decimals);
179 gtk_widget_hide (dialog->label_decimals);
180 gtk_widget_hide (dialog->entry_decimals);
183 select_treeview_from_format (dialog->date_format_treeview,
184 &format_option[0].spec);
185 gtk_widget_hide (dialog->width_decimals);
186 gtk_widget_show (dialog->date_format_list);
189 select_treeview_from_format (dialog->dollar_treeview,
191 gtk_widget_show (dialog->dollar_window);
192 gtk_widget_show_all (dialog->width_decimals);
195 select_treeview_from_format_type (dialog->custom_treeview,
198 gtk_widget_show (dialog->width_decimals);
199 gtk_widget_show (dialog->custom_currency_hbox);
202 gtk_widget_show_all (dialog->width_decimals);
209 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
210 static gint hide_dialog (GtkWidget *w, gpointer data);
214 add_to_group (GtkWidget *w, gpointer data)
216 GtkSizeGroup *sg = data;
218 gtk_size_group_add_widget (sg, w);
221 /* Set the local width and decimals entry boxes to reflec the local format */
223 update_width_decimals (const struct var_type_dialog *dialog)
228 text = g_strdup_printf ("%d", dialog->fmt_l.w);
229 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
232 text = g_strdup_printf ("%d", dialog->fmt_l.d);
233 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
237 /* Callback for when the custom treeview row is changed.
238 It sets dialog box to reflect the selected format */
240 preview_custom (GtkWidget *w, gpointer data)
244 struct var_type_dialog *dialog = data;
246 if ( dialog->active_button != BUTTON_CUSTOM )
249 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
250 dialog->fmt_l.d = atoi (text);
252 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
253 dialog->fmt_l.w = atoi (text);
256 if ( ! fmt_check_output (&dialog->fmt_l))
258 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
259 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
267 sample_text = value_to_text (v, dialog->fmt_l);
268 gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
269 g_free (sample_text);
272 sample_text = value_to_text (v, dialog->fmt_l);
273 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
274 g_free (sample_text);
279 /* Callback for when a treeview row is changed.
280 It sets the fmt_l_spec to reflect the selected format */
282 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
284 struct var_type_dialog *dialog = data;
286 GValue the_value = {0};
288 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
290 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
292 gtk_tree_selection_get_selected (sel, &model, &iter);
294 gtk_tree_model_get_value (model, &iter, 1, &the_value);
296 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value);
300 /* Callback for when a treeview row is changed.
301 It sets the type of the fmt_l to reflect the selected type */
303 set_format_type_from_treeview (GtkTreeView *treeview, gpointer data)
305 static struct fmt_spec custom_format = {0,0,0};
306 struct var_type_dialog *dialog = data;
308 GValue the_value = {0};
310 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
312 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
314 gtk_tree_selection_get_selected (sel, &model, &iter);
316 gtk_tree_model_get_value (model, &iter, 1, &the_value);
318 dialog->fmt_l = custom_format;
319 dialog->fmt_l.type = *(int*) g_value_get_pointer (&the_value);
326 /* Create the structure from the XML definitions */
327 struct var_type_dialog *
328 var_type_dialog_create (GladeXML *xml)
331 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
335 dialog->window = get_widget_assert (xml,"var_type_dialog");
338 g_signal_connect (dialog->window, "delete-event",
339 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
341 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
342 GTK_WINDOW (get_widget_assert (xml, "data_editor")));
344 dialog->radioButton[BUTTON_NUMERIC] =
345 get_widget_assert (xml,"radiobutton1");
346 dialog->radioButton[BUTTON_COMMA] =
347 get_widget_assert (xml,"radiobutton2");
348 dialog->radioButton[BUTTON_DOT] =
349 get_widget_assert (xml,"radiobutton3");
350 dialog->radioButton[BUTTON_SCIENTIFIC] =
351 get_widget_assert (xml,"radiobutton4");
352 dialog->radioButton[BUTTON_DATE] =
353 get_widget_assert (xml,"radiobutton5");
354 dialog->radioButton[BUTTON_DOLLAR] =
355 get_widget_assert (xml,"radiobutton6");
356 dialog->radioButton[BUTTON_CUSTOM] =
357 get_widget_assert (xml,"radiobutton7");
358 dialog->radioButton[BUTTON_STRING] =
359 get_widget_assert (xml,"radiobutton8");
362 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
363 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
364 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
365 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
367 dialog->label_psample = get_widget_assert (xml, "psample_label");
368 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
371 dialog->entry_width = get_widget_assert (xml,"width_entry");
373 dialog->custom_currency_hbox = get_widget_assert (xml,
374 "custom_currency_hbox");
376 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
377 dialog->dollar_treeview =
378 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
380 dialog->custom_treeview =
381 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
384 dialog->ok = get_widget_assert (xml,"var_type_ok");
389 GtkListStore *list_store ;
391 GtkTreeViewColumn *column;
392 GtkCellRenderer *renderer ;
394 static struct tgs tgs[num_BUTTONS];
395 /* The "middle_box" is a vbox with serveral children.
396 However only one child is ever shown at a time.
397 We need to make sure that they all have the same width, to avoid
398 upleasant resizing effects */
399 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
401 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
402 add_to_group, sizeGroup);
405 for (i = 0 ; i < num_BUTTONS; ++i )
407 tgs[i].dialog = dialog;
409 g_signal_connect (dialog->radioButton[i], "toggled",
410 G_CALLBACK (on_toggle_1), &tgs[i]);
412 g_signal_connect (dialog->radioButton[i], "toggled",
413 G_CALLBACK (on_toggle_2), dialog);
416 /* Populate the date format tree view */
417 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
418 "date_format_list_view"));
420 renderer = gtk_cell_renderer_text_new ();
422 column = gtk_tree_view_column_new_with_attributes ("Title",
428 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
432 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
434 for ( i = 0 ; i < sizeof (format_option) / sizeof (format_option[0]) ; ++i )
436 gtk_list_store_append (list_store, &iter);
437 gtk_list_store_set (list_store, &iter,
438 0, format_option[i].desc,
439 1, &format_option[i].spec,
443 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
444 GTK_TREE_MODEL (list_store));
446 g_object_unref (list_store);
448 g_signal_connect (GTK_OBJECT (dialog->date_format_treeview), "cursor-changed",
449 GTK_SIGNAL_FUNC (set_format_from_treeview), dialog);
452 /* populate the dollar treeview */
454 renderer = gtk_cell_renderer_text_new ();
456 column = gtk_tree_view_column_new_with_attributes ("Title",
462 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
466 list_store = gtk_list_store_new (2, G_TYPE_STRING,
469 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
471 char *template = fmt_dollar_template (&dollar_format[i]);
472 gtk_list_store_append (list_store, &iter);
473 gtk_list_store_set (list_store, &iter,
475 1, &dollar_format[i],
480 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
481 GTK_TREE_MODEL (list_store));
483 g_object_unref (list_store);
485 g_signal_connect (GTK_OBJECT (dialog->dollar_treeview),
487 GTK_SIGNAL_FUNC (set_format_from_treeview), dialog);
489 g_signal_connect_swapped (GTK_OBJECT (dialog->dollar_treeview),
491 GTK_SIGNAL_FUNC (update_width_decimals), dialog);
494 /* populate the custom treeview */
496 renderer = gtk_cell_renderer_text_new ();
498 column = gtk_tree_view_column_new_with_attributes ("Title",
504 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
508 list_store = gtk_list_store_new (2, G_TYPE_STRING,
511 for ( i = 0 ; i < 5 ; ++i )
513 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
515 g_snprintf (text, 4, "%s", fmt_name (cc_fmts[i]));
516 gtk_list_store_append (list_store, &iter);
517 gtk_list_store_set (list_store, &iter,
523 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
524 GTK_TREE_MODEL (list_store));
526 g_object_unref (list_store);
529 g_signal_connect (GTK_OBJECT (dialog->custom_treeview),
531 GTK_SIGNAL_FUNC (set_format_type_from_treeview), dialog);
534 g_signal_connect (GTK_OBJECT (dialog->custom_treeview),
536 GTK_SIGNAL_FUNC (preview_custom), dialog);
539 g_signal_connect (GTK_OBJECT (dialog->entry_width),
541 GTK_SIGNAL_FUNC (preview_custom), dialog);
544 g_signal_connect (GTK_OBJECT (dialog->entry_decimals),
546 GTK_SIGNAL_FUNC (preview_custom), dialog);
549 /* Connect to the OK button */
550 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
554 /* And the cancel button */
555 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
556 G_CALLBACK (hide_dialog),
566 /* Set a particular button to be active */
568 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
570 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
572 dialog->active_button = b;
577 /* Set the TREEVIEW list cursor to the item described by FMT */
579 select_treeview_from_format (GtkTreeView *treeview, const struct fmt_spec *fmt)
584 We do this with a linear search through the model --- hardly
585 efficient, but the list is short ... */
588 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
591 for (success = gtk_tree_model_get_iter_first (model, &iter);
593 success = gtk_tree_model_iter_next (model, &iter))
595 const struct fmt_spec *spec;
599 gtk_tree_model_get_value (model, &iter, 1, &value);
601 spec = g_value_get_pointer (&value);
603 if ( 0 == memcmp (spec, fmt, sizeof (struct fmt_spec)))
609 path = gtk_tree_model_get_path (model, &iter);
612 gtk_tree_view_set_cursor (treeview, path, 0, 0);
613 gtk_tree_path_free (path);
617 char str[FMT_STRING_LEN_MAX + 1];
618 g_warning ("Unusual date format: %s\n", fmt_to_string (fmt, str));
623 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
625 select_treeview_from_format_type (GtkTreeView *treeview,
631 We do this with a linear search through the model --- hardly
632 efficient, but the list is short ... */
635 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
638 for (success = gtk_tree_model_get_iter_first (model, &iter);
640 success = gtk_tree_model_iter_next (model, &iter))
646 gtk_tree_model_get_value (model, &iter, 1, &value);
648 spec = * ((int *) g_value_get_pointer (&value));
650 if ( spec == fmt_type)
654 path = gtk_tree_model_get_path (model, &iter);
657 gtk_tree_view_set_cursor (treeview, path, 0, 0);
658 gtk_tree_path_free (path);
661 g_warning ("Unknown custom type %d\n", fmt_type);
665 /* Set up the state of the dialog box to match the variable VAR */
667 var_type_dialog_set_state (struct var_type_dialog *dialog)
669 const struct fmt_spec *write_spec ;
670 GString *str = g_string_new ("");
673 g_assert (dialog->pv);
675 /* Populate width and decimals */
676 write_spec = var_get_write_format (dialog->pv);
678 g_string_printf (str, "%d", write_spec->d);
680 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals),
683 g_string_printf (str, "%d", write_spec->w);
685 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width),
688 g_string_free (str, TRUE);
690 /* Populate the radio button states */
691 switch (write_spec->type)
694 var_type_dialog_set_active_button (dialog, BUTTON_NUMERIC);
695 gtk_widget_show_all (dialog->width_decimals);
698 var_type_dialog_set_active_button (dialog, BUTTON_STRING);
699 gtk_widget_hide (dialog->label_decimals);
700 gtk_widget_hide (dialog->entry_decimals);
703 var_type_dialog_set_active_button (dialog, BUTTON_COMMA);
704 gtk_widget_show_all (dialog->width_decimals);
707 var_type_dialog_set_active_button (dialog, BUTTON_DOT);
708 gtk_widget_show_all (dialog->width_decimals);
711 var_type_dialog_set_active_button (dialog, BUTTON_DOLLAR);
712 gtk_widget_show_all (dialog->width_decimals);
714 select_treeview_from_format (dialog->dollar_treeview, write_spec);
729 var_type_dialog_set_active_button (dialog, BUTTON_DATE);
730 gtk_widget_hide (dialog->width_decimals);
731 gtk_widget_show (dialog->date_format_list);
732 select_treeview_from_format (dialog->date_format_treeview, write_spec);
739 var_type_dialog_set_active_button (dialog, BUTTON_CUSTOM);
740 select_treeview_from_format_type (dialog->custom_treeview,
742 gtk_widget_show_all (dialog->width_decimals);
745 gtk_widget_show_all (dialog->width_decimals);
751 /* Popup the dialog box */
753 var_type_dialog_show (struct var_type_dialog *dialog)
755 var_type_dialog_set_state (dialog);
757 gtk_widget_show (dialog->window);
760 /* Fills F with an output format specification with type TYPE, width
761 W, and D decimals. Iff it's a valid format, then return true.
764 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
769 return fmt_check_output (f);
775 /* Callbacks for the Variable Type Dialog Box */
777 /* Callback for when the var type dialog is closed using the OK button.
778 It sets the appropriate variable accordingly. */
780 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
782 struct var_type_dialog *dialog = data;
785 g_assert (dialog->pv);
788 gint width = atoi (gtk_entry_get_text
789 (GTK_ENTRY (dialog->entry_width)));
791 gint decimals = atoi (gtk_entry_get_text
792 (GTK_ENTRY (dialog->entry_decimals)));
794 gint new_type = VAR_NUMERIC;
797 struct fmt_spec spec;
798 switch (dialog->active_button)
801 new_type = VAR_STRING;
803 result = make_output_format_try (&spec, FMT_A, width, 0);
806 result = make_output_format_try (&spec, FMT_F, width, decimals);
809 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
812 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
814 case BUTTON_SCIENTIFIC:
815 result = make_output_format_try (&spec, FMT_E, width, decimals);
819 g_assert (fmt_check_output (&dialog->fmt_l));
820 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
823 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
826 g_print ("Unknown variable type: %d\n", dialog->active_button) ;
831 if ( result == true )
833 var_set_width (dialog->pv, new_width);
834 var_set_both_formats (dialog->pv, &spec);
838 gtk_widget_hide (dialog->window);
846 hide_dialog (GtkWidget *w, gpointer data)
848 struct var_type_dialog *dialog = data;
850 gtk_widget_hide (dialog->window);