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}},
73 { "dd-mmm-yyyy HH:MM:SS", {FMT_DATETIME, 20, 0}}
77 static const struct fmt_spec dollar_format[] =
93 static const int cc_format[] =
103 static void select_treeview_from_format
104 (GtkTreeView *treeview, const struct fmt_spec *fmt);
106 static void select_treeview_from_format_type (GtkTreeView *treeview,
110 /* callback for when any of the radio buttons are toggled */
112 on_toggle_1 (GtkToggleButton *togglebutton, gpointer user_data)
114 struct tgs *tgs = user_data;
116 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
119 tgs->dialog->active_button = tgs->button;
122 static void update_width_decimals (const struct var_type_dialog *dialog);
124 #define force_max(x, val) if (x > val) x = val
127 Set the local format from the variable
128 and force them to have sensible values */
130 set_local_width_decimals (struct var_type_dialog *dialog)
132 dialog->fmt_l = * var_get_write_format (dialog->pv);
134 switch (dialog->active_button)
137 force_max ( dialog->fmt_l.w, 255);
140 force_max ( dialog->fmt_l.w, 40);
141 force_max ( dialog->fmt_l.d, 16);
147 /* callback for when any of the radio buttons are toggled */
149 on_toggle_2 (GtkToggleButton *togglebutton, gpointer user_data)
151 struct var_type_dialog *dialog = user_data;
152 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
154 switch (dialog->active_button)
157 gtk_widget_hide (dialog->date_format_list);
160 gtk_widget_hide (dialog->custom_currency_hbox);
163 gtk_widget_hide (dialog->dollar_window);
166 gtk_widget_show (dialog->label_decimals);
167 gtk_widget_show (dialog->entry_decimals);
173 set_local_width_decimals (dialog);
174 update_width_decimals (dialog);
176 switch (dialog->active_button)
179 gtk_widget_show (dialog->width_decimals);
180 gtk_widget_hide (dialog->label_decimals);
181 gtk_widget_hide (dialog->entry_decimals);
184 select_treeview_from_format (dialog->date_format_treeview,
185 &format_option[0].spec);
186 gtk_widget_hide (dialog->width_decimals);
187 gtk_widget_show (dialog->date_format_list);
190 select_treeview_from_format (dialog->dollar_treeview,
192 gtk_widget_show (dialog->dollar_window);
193 gtk_widget_show_all (dialog->width_decimals);
196 select_treeview_from_format_type (dialog->custom_treeview,
199 gtk_widget_show (dialog->width_decimals);
200 gtk_widget_show (dialog->custom_currency_hbox);
203 gtk_widget_show_all (dialog->width_decimals);
210 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
211 static gint hide_dialog (GtkWidget *w, gpointer data);
215 add_to_group (GtkWidget *w, gpointer data)
217 GtkSizeGroup *sg = data;
219 gtk_size_group_add_widget (sg, w);
222 /* Set the local width and decimals entry boxes to reflec the local format */
224 update_width_decimals (const struct var_type_dialog *dialog)
229 text = g_strdup_printf ("%d", dialog->fmt_l.w);
230 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
233 text = g_strdup_printf ("%d", dialog->fmt_l.d);
234 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
238 /* Callback for when the custom treeview row is changed.
239 It sets dialog box to reflect the selected format */
241 preview_custom (GtkWidget *w, gpointer data)
245 struct var_type_dialog *dialog = data;
247 if ( dialog->active_button != BUTTON_CUSTOM )
250 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
251 dialog->fmt_l.d = atoi (text);
253 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
254 dialog->fmt_l.w = atoi (text);
257 if ( ! fmt_check_output (&dialog->fmt_l))
259 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
260 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
268 sample_text = value_to_text (v, dialog->fmt_l);
269 gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
270 g_free (sample_text);
273 sample_text = value_to_text (v, dialog->fmt_l);
274 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
275 g_free (sample_text);
280 /* Callback for when a treeview row is changed.
281 It sets the fmt_l_spec to reflect the selected format */
283 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
285 struct var_type_dialog *dialog = data;
287 GValue the_value = {0};
289 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
291 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
293 gtk_tree_selection_get_selected (sel, &model, &iter);
295 gtk_tree_model_get_value (model, &iter, 1, &the_value);
297 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value);
301 /* Callback for when a treeview row is changed.
302 It sets the type of the fmt_l to reflect the selected type */
304 set_format_type_from_treeview (GtkTreeView *treeview, gpointer data)
306 static struct fmt_spec custom_format = {0,0,0};
307 struct var_type_dialog *dialog = data;
309 GValue the_value = {0};
311 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
313 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
315 gtk_tree_selection_get_selected (sel, &model, &iter);
317 gtk_tree_model_get_value (model, &iter, 1, &the_value);
319 dialog->fmt_l = custom_format;
320 dialog->fmt_l.type = *(int*) g_value_get_pointer (&the_value);
327 /* Create the structure from the XML definitions */
328 struct var_type_dialog *
329 var_type_dialog_create (GladeXML *xml)
332 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
336 dialog->window = get_widget_assert (xml,"var_type_dialog");
339 g_signal_connect (dialog->window, "delete-event",
340 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
342 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
343 GTK_WINDOW (get_widget_assert (xml, "data_editor")));
345 dialog->radioButton[BUTTON_NUMERIC] =
346 get_widget_assert (xml,"radiobutton1");
347 dialog->radioButton[BUTTON_COMMA] =
348 get_widget_assert (xml,"radiobutton2");
349 dialog->radioButton[BUTTON_DOT] =
350 get_widget_assert (xml,"radiobutton3");
351 dialog->radioButton[BUTTON_SCIENTIFIC] =
352 get_widget_assert (xml,"radiobutton4");
353 dialog->radioButton[BUTTON_DATE] =
354 get_widget_assert (xml,"radiobutton5");
355 dialog->radioButton[BUTTON_DOLLAR] =
356 get_widget_assert (xml,"radiobutton6");
357 dialog->radioButton[BUTTON_CUSTOM] =
358 get_widget_assert (xml,"radiobutton7");
359 dialog->radioButton[BUTTON_STRING] =
360 get_widget_assert (xml,"radiobutton8");
363 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
364 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
365 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
366 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
368 dialog->label_psample = get_widget_assert (xml, "psample_label");
369 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
372 dialog->entry_width = get_widget_assert (xml,"width_entry");
374 dialog->custom_currency_hbox = get_widget_assert (xml,
375 "custom_currency_hbox");
377 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
378 dialog->dollar_treeview =
379 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
381 dialog->custom_treeview =
382 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
385 dialog->ok = get_widget_assert (xml,"var_type_ok");
390 GtkListStore *list_store ;
392 GtkTreeViewColumn *column;
393 GtkCellRenderer *renderer ;
395 static struct tgs tgs[num_BUTTONS];
396 /* The "middle_box" is a vbox with serveral children.
397 However only one child is ever shown at a time.
398 We need to make sure that they all have the same width, to avoid
399 upleasant resizing effects */
400 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
402 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
403 add_to_group, sizeGroup);
406 for (i = 0 ; i < num_BUTTONS; ++i )
408 tgs[i].dialog = dialog;
410 g_signal_connect (dialog->radioButton[i], "toggled",
411 G_CALLBACK (on_toggle_1), &tgs[i]);
413 g_signal_connect (dialog->radioButton[i], "toggled",
414 G_CALLBACK (on_toggle_2), dialog);
417 /* Populate the date format tree view */
418 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
419 "date_format_list_view"));
421 renderer = gtk_cell_renderer_text_new ();
423 column = gtk_tree_view_column_new_with_attributes ("Title",
429 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
433 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
435 for ( i = 0 ; i < sizeof (format_option) / sizeof (format_option[0]) ; ++i )
437 gtk_list_store_append (list_store, &iter);
438 gtk_list_store_set (list_store, &iter,
439 0, format_option[i].desc,
440 1, &format_option[i].spec,
444 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
445 GTK_TREE_MODEL (list_store));
447 g_object_unref (list_store);
449 g_signal_connect (GTK_OBJECT (dialog->date_format_treeview), "cursor-changed",
450 GTK_SIGNAL_FUNC (set_format_from_treeview), dialog);
453 /* populate the dollar treeview */
455 renderer = gtk_cell_renderer_text_new ();
457 column = gtk_tree_view_column_new_with_attributes ("Title",
463 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
467 list_store = gtk_list_store_new (2, G_TYPE_STRING,
470 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
472 char *template = fmt_dollar_template (&dollar_format[i]);
473 gtk_list_store_append (list_store, &iter);
474 gtk_list_store_set (list_store, &iter,
476 1, &dollar_format[i],
481 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
482 GTK_TREE_MODEL (list_store));
484 g_object_unref (list_store);
486 g_signal_connect (GTK_OBJECT (dialog->dollar_treeview),
488 GTK_SIGNAL_FUNC (set_format_from_treeview), dialog);
490 g_signal_connect_swapped (GTK_OBJECT (dialog->dollar_treeview),
492 GTK_SIGNAL_FUNC (update_width_decimals), dialog);
495 /* populate the custom treeview */
497 renderer = gtk_cell_renderer_text_new ();
499 column = gtk_tree_view_column_new_with_attributes ("Title",
505 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
509 list_store = gtk_list_store_new (2, G_TYPE_STRING,
512 for ( i = 0 ; i < 5 ; ++i )
514 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
516 g_snprintf (text, 4, "%s", fmt_name (cc_fmts[i]));
517 gtk_list_store_append (list_store, &iter);
518 gtk_list_store_set (list_store, &iter,
524 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
525 GTK_TREE_MODEL (list_store));
527 g_object_unref (list_store);
530 g_signal_connect (GTK_OBJECT (dialog->custom_treeview),
532 GTK_SIGNAL_FUNC (set_format_type_from_treeview), dialog);
535 g_signal_connect (GTK_OBJECT (dialog->custom_treeview),
537 GTK_SIGNAL_FUNC (preview_custom), dialog);
540 g_signal_connect (GTK_OBJECT (dialog->entry_width),
542 GTK_SIGNAL_FUNC (preview_custom), dialog);
545 g_signal_connect (GTK_OBJECT (dialog->entry_decimals),
547 GTK_SIGNAL_FUNC (preview_custom), dialog);
550 /* Connect to the OK button */
551 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
555 /* And the cancel button */
556 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
557 G_CALLBACK (hide_dialog),
567 /* Set a particular button to be active */
569 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
571 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
573 dialog->active_button = b;
578 /* Set the TREEVIEW list cursor to the item described by FMT */
580 select_treeview_from_format (GtkTreeView *treeview, const struct fmt_spec *fmt)
585 We do this with a linear search through the model --- hardly
586 efficient, but the list is short ... */
589 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
592 for (success = gtk_tree_model_get_iter_first (model, &iter);
594 success = gtk_tree_model_iter_next (model, &iter))
596 const struct fmt_spec *spec;
600 gtk_tree_model_get_value (model, &iter, 1, &value);
602 spec = g_value_get_pointer (&value);
604 if ( 0 == memcmp (spec, fmt, sizeof (struct fmt_spec)))
610 path = gtk_tree_model_get_path (model, &iter);
613 gtk_tree_view_set_cursor (treeview, path, 0, 0);
614 gtk_tree_path_free (path);
618 char str[FMT_STRING_LEN_MAX + 1];
619 g_warning ("Unusual date format: %s\n", fmt_to_string (fmt, str));
624 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
626 select_treeview_from_format_type (GtkTreeView *treeview,
632 We do this with a linear search through the model --- hardly
633 efficient, but the list is short ... */
636 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
639 for (success = gtk_tree_model_get_iter_first (model, &iter);
641 success = gtk_tree_model_iter_next (model, &iter))
647 gtk_tree_model_get_value (model, &iter, 1, &value);
649 spec = * ((int *) g_value_get_pointer (&value));
651 if ( spec == fmt_type)
655 path = gtk_tree_model_get_path (model, &iter);
658 gtk_tree_view_set_cursor (treeview, path, 0, 0);
659 gtk_tree_path_free (path);
662 g_warning ("Unknown custom type %d\n", fmt_type);
666 /* Set up the state of the dialog box to match the variable VAR */
668 var_type_dialog_set_state (struct var_type_dialog *dialog)
670 const struct fmt_spec *write_spec ;
671 GString *str = g_string_new ("");
674 g_assert (dialog->pv);
676 /* Populate width and decimals */
677 write_spec = var_get_write_format (dialog->pv);
679 g_string_printf (str, "%d", write_spec->d);
681 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals),
684 g_string_printf (str, "%d", write_spec->w);
686 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width),
689 g_string_free (str, TRUE);
691 /* Populate the radio button states */
692 switch (write_spec->type)
695 var_type_dialog_set_active_button (dialog, BUTTON_NUMERIC);
696 gtk_widget_show_all (dialog->width_decimals);
699 var_type_dialog_set_active_button (dialog, BUTTON_STRING);
700 gtk_widget_hide (dialog->label_decimals);
701 gtk_widget_hide (dialog->entry_decimals);
704 var_type_dialog_set_active_button (dialog, BUTTON_COMMA);
705 gtk_widget_show_all (dialog->width_decimals);
708 var_type_dialog_set_active_button (dialog, BUTTON_DOT);
709 gtk_widget_show_all (dialog->width_decimals);
712 var_type_dialog_set_active_button (dialog, BUTTON_DOLLAR);
713 gtk_widget_show_all (dialog->width_decimals);
715 select_treeview_from_format (dialog->dollar_treeview, write_spec);
730 var_type_dialog_set_active_button (dialog, BUTTON_DATE);
731 gtk_widget_hide (dialog->width_decimals);
732 gtk_widget_show (dialog->date_format_list);
733 select_treeview_from_format (dialog->date_format_treeview, write_spec);
740 var_type_dialog_set_active_button (dialog, BUTTON_CUSTOM);
741 select_treeview_from_format_type (dialog->custom_treeview,
743 gtk_widget_show_all (dialog->width_decimals);
746 gtk_widget_show_all (dialog->width_decimals);
752 /* Popup the dialog box */
754 var_type_dialog_show (struct var_type_dialog *dialog)
756 var_type_dialog_set_state (dialog);
758 gtk_widget_show (dialog->window);
761 /* Fills F with an output format specification with type TYPE, width
762 W, and D decimals. Iff it's a valid format, then return true.
765 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
770 return fmt_check_output (f);
776 /* Callbacks for the Variable Type Dialog Box */
778 /* Callback for when the var type dialog is closed using the OK button.
779 It sets the appropriate variable accordingly. */
781 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
783 struct var_type_dialog *dialog = data;
786 g_assert (dialog->pv);
789 gint width = atoi (gtk_entry_get_text
790 (GTK_ENTRY (dialog->entry_width)));
792 gint decimals = atoi (gtk_entry_get_text
793 (GTK_ENTRY (dialog->entry_decimals)));
795 gint new_type = VAR_NUMERIC;
798 struct fmt_spec spec;
799 switch (dialog->active_button)
802 new_type = VAR_STRING;
804 result = make_output_format_try (&spec, FMT_A, width, 0);
807 result = make_output_format_try (&spec, FMT_F, width, decimals);
810 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
813 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
815 case BUTTON_SCIENTIFIC:
816 result = make_output_format_try (&spec, FMT_E, width, decimals);
820 g_assert (fmt_check_output (&dialog->fmt_l));
821 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
824 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
827 g_print ("Unknown variable type: %d\n", dialog->active_button) ;
832 if ( result == true )
834 var_set_width (dialog->pv, new_width);
835 var_set_both_formats (dialog->pv, &spec);
839 gtk_widget_hide (dialog->window);
847 hide_dialog (GtkWidget *w, gpointer data)
849 struct var_type_dialog *dialog = data;
851 gtk_widget_hide (dialog->window);