1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2005, 2006 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 */
28 #include "var-type-dialog.h"
32 #include <data/variable.h>
33 #include <data/settings.h>
34 #include <libpspp/message.h>
39 struct var_type_dialog *dialog;
50 static const struct format_opt format_option[] =
52 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
53 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
54 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
55 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
56 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
57 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
58 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
59 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
60 { "yyddd", {FMT_JDATE, 5, 0} },
61 { "yyyyddd", {FMT_JDATE, 7, 0} },
62 { "q Q yyyy", {FMT_QYR, 8, 0} },
63 { "q Q yy", {FMT_QYR, 6, 0} },
64 { "mmm yyyy", {FMT_MOYR, 8, 0} },
65 { "mmm yy", {FMT_MOYR, 6, 0} },
66 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
67 { "dd WK yy", {FMT_WKYR, 8, 0} },
68 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}},
69 { "dd-mmm-yyyy HH:MM:SS", {FMT_DATETIME, 20, 0}}
73 static const struct fmt_spec dollar_format[] =
89 static const int cc_format[] =
99 static void select_treeview_from_format
100 (GtkTreeView *treeview, const struct fmt_spec *fmt);
102 static void select_treeview_from_format_type (GtkTreeView *treeview,
106 /* callback for when any of the radio buttons are toggled */
108 on_toggle_1 (GtkToggleButton *togglebutton, gpointer user_data)
110 struct tgs *tgs = user_data;
112 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
115 tgs->dialog->active_button = tgs->button;
118 static void update_width_decimals (const struct var_type_dialog *dialog);
120 #define force_max(x, val) if (x > val) x = val
123 Set the local format from the variable
124 and force them to have sensible values */
126 set_local_width_decimals (struct var_type_dialog *dialog)
128 dialog->fmt_l = * var_get_write_format (dialog->pv);
130 switch (dialog->active_button)
133 force_max ( dialog->fmt_l.w, 255);
136 force_max ( dialog->fmt_l.w, 40);
137 force_max ( dialog->fmt_l.d, 16);
143 /* callback for when any of the radio buttons are toggled */
145 on_toggle_2 (GtkToggleButton *togglebutton, gpointer user_data)
147 struct var_type_dialog *dialog = user_data;
148 if ( gtk_toggle_button_get_active (togglebutton) == FALSE)
150 switch (dialog->active_button)
153 gtk_widget_hide (dialog->date_format_list);
156 gtk_widget_hide (dialog->custom_currency_hbox);
159 gtk_widget_hide (dialog->dollar_window);
162 gtk_widget_show (dialog->label_decimals);
163 gtk_widget_show (dialog->entry_decimals);
169 set_local_width_decimals (dialog);
170 update_width_decimals (dialog);
172 switch (dialog->active_button)
175 gtk_widget_show (dialog->entry_width);
176 gtk_widget_show (dialog->width_decimals);
177 gtk_widget_hide (dialog->label_decimals);
178 gtk_widget_hide (dialog->entry_decimals);
181 select_treeview_from_format (dialog->date_format_treeview,
182 &format_option[0].spec);
183 gtk_widget_hide (dialog->width_decimals);
184 gtk_widget_show (dialog->date_format_list);
187 select_treeview_from_format (dialog->dollar_treeview,
189 gtk_widget_show (dialog->dollar_window);
190 gtk_widget_show_all (dialog->width_decimals);
193 select_treeview_from_format_type (dialog->custom_treeview,
196 gtk_widget_show (dialog->width_decimals);
197 gtk_widget_show (dialog->custom_currency_hbox);
200 gtk_widget_show_all (dialog->width_decimals);
207 static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data);
208 static gint hide_dialog (GtkWidget *w, gpointer data);
212 add_to_group (GtkWidget *w, gpointer data)
214 GtkSizeGroup *sg = data;
216 gtk_size_group_add_widget (sg, w);
219 /* Set the local width and decimals entry boxes to reflec the local format */
221 update_width_decimals (const struct var_type_dialog *dialog)
226 text = g_strdup_printf ("%d", dialog->fmt_l.w);
227 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text);
230 text = g_strdup_printf ("%d", dialog->fmt_l.d);
231 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text);
235 /* Callback for when the custom treeview row is changed.
236 It sets dialog box to reflect the selected format */
238 preview_custom (GtkWidget *w, gpointer data)
242 struct var_type_dialog *dialog = data;
244 if ( dialog->active_button != BUTTON_CUSTOM )
247 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals));
248 dialog->fmt_l.d = atoi (text);
250 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_width));
251 dialog->fmt_l.w = atoi (text);
254 if ( ! fmt_check_output (&dialog->fmt_l))
256 gtk_label_set_text (GTK_LABEL (dialog->label_psample), "---");
257 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), "---");
265 sample_text = value_to_text (v, NULL, dialog->fmt_l);
266 gtk_label_set_text (GTK_LABEL (dialog->label_psample), sample_text);
267 g_free (sample_text);
270 sample_text = value_to_text (v, NULL, dialog->fmt_l);
271 gtk_label_set_text (GTK_LABEL (dialog->label_nsample), sample_text);
272 g_free (sample_text);
277 /* Callback for when a treeview row is changed.
278 It sets the fmt_l_spec to reflect the selected format */
280 set_format_from_treeview (GtkTreeView *treeview, gpointer data)
282 struct var_type_dialog *dialog = data;
284 GValue the_value = {0};
286 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
288 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
290 gtk_tree_selection_get_selected (sel, &model, &iter);
292 gtk_tree_model_get_value (model, &iter, 1, &the_value);
294 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value);
298 /* Callback for when a treeview row is changed.
299 It sets the type of the fmt_l to reflect the selected type */
301 set_format_type_from_treeview (GtkTreeView *treeview, gpointer data)
303 static struct fmt_spec custom_format = {0,0,0};
304 struct var_type_dialog *dialog = data;
306 GValue the_value = {0};
308 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
310 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
312 gtk_tree_selection_get_selected (sel, &model, &iter);
314 gtk_tree_model_get_value (model, &iter, 1, &the_value);
316 dialog->fmt_l = custom_format;
317 dialog->fmt_l.type = *(int*) g_value_get_pointer (&the_value);
324 /* Create the structure */
325 struct var_type_dialog *
326 var_type_dialog_create (GtkWindow *toplevel)
329 struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog));
331 GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
333 dialog->window = get_widget_assert (xml,"var_type_dialog");
334 dialog->active_button = -1;
337 g_signal_connect (dialog->window, "delete-event",
338 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
340 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
343 dialog->radioButton[BUTTON_NUMERIC] =
344 get_widget_assert (xml,"radiobutton1");
345 dialog->radioButton[BUTTON_COMMA] =
346 get_widget_assert (xml,"radiobutton2");
347 dialog->radioButton[BUTTON_DOT] =
348 get_widget_assert (xml,"radiobutton3");
349 dialog->radioButton[BUTTON_SCIENTIFIC] =
350 get_widget_assert (xml,"radiobutton4");
351 dialog->radioButton[BUTTON_DATE] =
352 get_widget_assert (xml,"radiobutton5");
353 dialog->radioButton[BUTTON_DOLLAR] =
354 get_widget_assert (xml,"radiobutton6");
355 dialog->radioButton[BUTTON_CUSTOM] =
356 get_widget_assert (xml,"radiobutton7");
357 dialog->radioButton[BUTTON_STRING] =
358 get_widget_assert (xml,"radiobutton8");
361 dialog->date_format_list = get_widget_assert (xml, "scrolledwindow4");
362 dialog->width_decimals = get_widget_assert (xml, "width_decimals");
363 dialog->label_decimals = get_widget_assert (xml, "decimals_label");
364 dialog->entry_decimals = get_widget_assert (xml, "decimals_entry");
366 dialog->label_psample = get_widget_assert (xml, "psample_label");
367 dialog->label_nsample = get_widget_assert (xml, "nsample_label");
370 dialog->entry_width = get_widget_assert (xml,"width_entry");
372 dialog->custom_currency_hbox = get_widget_assert (xml,
373 "custom_currency_hbox");
375 dialog->dollar_window = get_widget_assert (xml, "dollar_window");
376 dialog->dollar_treeview =
377 GTK_TREE_VIEW (get_widget_assert (xml, "dollar_treeview"));
379 dialog->custom_treeview =
380 GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview"));
383 dialog->ok = get_widget_assert (xml,"var_type_ok");
388 GtkListStore *list_store ;
390 GtkTreeViewColumn *column;
391 GtkCellRenderer *renderer ;
393 static struct tgs tgs[num_BUTTONS];
394 /* The "middle_box" is a vbox with serveral children.
395 However only one child is ever shown at a time.
396 We need to make sure that they all have the same width, to avoid
397 upleasant resizing effects */
398 GtkSizeGroup *sizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
400 gtk_container_foreach (GTK_CONTAINER (get_widget_assert (xml, "middle_box")),
401 add_to_group, sizeGroup);
404 for (i = 0 ; i < num_BUTTONS; ++i )
406 tgs[i].dialog = dialog;
408 g_signal_connect (dialog->radioButton[i], "toggled",
409 G_CALLBACK (on_toggle_1), &tgs[i]);
411 g_signal_connect (dialog->radioButton[i], "toggled",
412 G_CALLBACK (on_toggle_2), dialog);
415 /* Populate the date format tree view */
416 dialog->date_format_treeview = GTK_TREE_VIEW (get_widget_assert (xml,
417 "date_format_list_view"));
419 renderer = gtk_cell_renderer_text_new ();
421 column = gtk_tree_view_column_new_with_attributes ("Title",
427 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
431 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
433 for ( i = 0 ; i < sizeof (format_option) / sizeof (format_option[0]) ; ++i )
435 gtk_list_store_append (list_store, &iter);
436 gtk_list_store_set (list_store, &iter,
437 0, format_option[i].desc,
438 1, &format_option[i].spec,
442 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->date_format_treeview),
443 GTK_TREE_MODEL (list_store));
445 g_object_unref (list_store);
447 g_signal_connect (dialog->date_format_treeview, "cursor-changed",
448 GTK_SIGNAL_FUNC (set_format_from_treeview), dialog);
451 /* populate the dollar treeview */
453 renderer = gtk_cell_renderer_text_new ();
455 column = gtk_tree_view_column_new_with_attributes ("Title",
461 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
465 list_store = gtk_list_store_new (2, G_TYPE_STRING,
468 for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i )
470 char *template = settings_dollar_template (&dollar_format[i]);
471 gtk_list_store_append (list_store, &iter);
472 gtk_list_store_set (list_store, &iter,
474 1, &dollar_format[i],
479 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->dollar_treeview),
480 GTK_TREE_MODEL (list_store));
482 g_object_unref (list_store);
484 g_signal_connect (dialog->dollar_treeview,
486 GTK_SIGNAL_FUNC (set_format_from_treeview), dialog);
488 g_signal_connect_swapped (dialog->dollar_treeview,
490 GTK_SIGNAL_FUNC (update_width_decimals), dialog);
493 /* populate the custom treeview */
495 renderer = gtk_cell_renderer_text_new ();
497 column = gtk_tree_view_column_new_with_attributes ("Title",
503 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
507 list_store = gtk_list_store_new (2, G_TYPE_STRING,
510 for ( i = 0 ; i < 5 ; ++i )
512 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
514 g_snprintf (text, 4, "%s", fmt_name (cc_fmts[i]));
515 gtk_list_store_append (list_store, &iter);
516 gtk_list_store_set (list_store, &iter,
522 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->custom_treeview),
523 GTK_TREE_MODEL (list_store));
525 g_object_unref (list_store);
528 g_signal_connect (dialog->custom_treeview,
530 GTK_SIGNAL_FUNC (set_format_type_from_treeview), dialog);
533 g_signal_connect (dialog->custom_treeview,
535 GTK_SIGNAL_FUNC (preview_custom), dialog);
538 g_signal_connect (dialog->entry_width,
540 GTK_SIGNAL_FUNC (preview_custom), dialog);
543 g_signal_connect (dialog->entry_decimals,
545 GTK_SIGNAL_FUNC (preview_custom), dialog);
548 /* Connect to the OK button */
549 g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked),
553 /* And the cancel button */
554 g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked",
555 G_CALLBACK (hide_dialog),
559 g_object_unref (xml);
565 /* Set a particular button to be active */
567 var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b)
569 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]),
571 dialog->active_button = b;
576 /* Set the TREEVIEW list cursor to the item described by FMT */
578 select_treeview_from_format (GtkTreeView *treeview, const struct fmt_spec *fmt)
583 We do this with a linear search through the model --- hardly
584 efficient, but the list is short ... */
587 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
590 for (success = gtk_tree_model_get_iter_first (model, &iter);
592 success = gtk_tree_model_iter_next (model, &iter))
594 const struct fmt_spec *spec;
598 gtk_tree_model_get_value (model, &iter, 1, &value);
600 spec = g_value_get_pointer (&value);
602 if ( 0 == memcmp (spec, fmt, sizeof (struct fmt_spec)))
608 path = gtk_tree_model_get_path (model, &iter);
611 gtk_tree_view_set_cursor (treeview, path, 0, 0);
612 gtk_tree_path_free (path);
616 char str[FMT_STRING_LEN_MAX + 1];
617 g_warning ("Unusual date format: %s\n", fmt_to_string (fmt, str));
622 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
624 select_treeview_from_format_type (GtkTreeView *treeview,
630 We do this with a linear search through the model --- hardly
631 efficient, but the list is short ... */
634 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
637 for (success = gtk_tree_model_get_iter_first (model, &iter);
639 success = gtk_tree_model_iter_next (model, &iter))
645 gtk_tree_model_get_value (model, &iter, 1, &value);
647 spec = * ((int *) g_value_get_pointer (&value));
649 if ( spec == fmt_type)
653 path = gtk_tree_model_get_path (model, &iter);
656 gtk_tree_view_set_cursor (treeview, path, 0, 0);
657 gtk_tree_path_free (path);
660 g_warning ("Unknown custom type %d\n", fmt_type);
664 /* Set up the state of the dialog box to match the variable VAR */
666 var_type_dialog_set_state (struct var_type_dialog *dialog)
668 const struct fmt_spec *write_spec ;
669 GString *str = g_string_new ("");
672 g_assert (dialog->pv);
674 /* Populate width and decimals */
675 write_spec = var_get_write_format (dialog->pv);
677 g_string_printf (str, "%d", write_spec->d);
679 gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals),
682 g_string_printf (str, "%d", write_spec->w);
684 gtk_entry_set_text (GTK_ENTRY (dialog->entry_width),
687 g_string_free (str, TRUE);
689 /* Populate the radio button states */
690 switch (write_spec->type)
693 var_type_dialog_set_active_button (dialog, BUTTON_NUMERIC);
694 gtk_widget_show_all (dialog->width_decimals);
697 var_type_dialog_set_active_button (dialog, BUTTON_STRING);
698 gtk_widget_hide (dialog->label_decimals);
699 gtk_widget_hide (dialog->entry_decimals);
702 var_type_dialog_set_active_button (dialog, BUTTON_COMMA);
703 gtk_widget_show_all (dialog->width_decimals);
706 var_type_dialog_set_active_button (dialog, BUTTON_DOT);
707 gtk_widget_show_all (dialog->width_decimals);
710 var_type_dialog_set_active_button (dialog, BUTTON_DOLLAR);
711 gtk_widget_show_all (dialog->width_decimals);
713 select_treeview_from_format (dialog->dollar_treeview, write_spec);
728 var_type_dialog_set_active_button (dialog, BUTTON_DATE);
729 gtk_widget_hide (dialog->width_decimals);
730 gtk_widget_show (dialog->date_format_list);
731 select_treeview_from_format (dialog->date_format_treeview, write_spec);
738 var_type_dialog_set_active_button (dialog, BUTTON_CUSTOM);
739 select_treeview_from_format_type (dialog->custom_treeview,
741 gtk_widget_show_all (dialog->width_decimals);
744 gtk_widget_show_all (dialog->width_decimals);
750 /* Popup the dialog box */
752 var_type_dialog_show (struct var_type_dialog *dialog)
754 var_type_dialog_set_state (dialog);
756 gtk_widget_show (dialog->window);
759 /* Fills F with an output format specification with type TYPE, width
760 W, and D decimals. Iff it's a valid format, then return true.
763 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
768 return fmt_check_output (f);
774 /* Callbacks for the Variable Type Dialog Box */
776 /* Callback for when the var type dialog is closed using the OK button.
777 It sets the appropriate variable accordingly. */
779 on_var_type_ok_clicked (GtkWidget *w, gpointer data)
781 struct var_type_dialog *dialog = data;
784 g_assert (dialog->pv);
787 gint width = atoi (gtk_entry_get_text
788 (GTK_ENTRY (dialog->entry_width)));
790 gint decimals = atoi (gtk_entry_get_text
791 (GTK_ENTRY (dialog->entry_decimals)));
793 gint new_type = VAL_NUMERIC;
796 struct fmt_spec spec;
797 switch (dialog->active_button)
800 new_type = VAL_STRING;
802 result = make_output_format_try (&spec, FMT_A, width, 0);
805 result = make_output_format_try (&spec, FMT_F, width, decimals);
808 result = make_output_format_try (&spec, FMT_COMMA, width, decimals);
811 result = make_output_format_try (&spec, FMT_DOT, width, decimals);
813 case BUTTON_SCIENTIFIC:
814 result = make_output_format_try (&spec, FMT_E, width, decimals);
818 g_assert (fmt_check_output (&dialog->fmt_l));
819 result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec));
822 result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals);
825 g_print ("Unknown variable type: %d\n", dialog->active_button) ;
830 if ( result == true )
832 var_set_width (dialog->pv, new_width);
833 var_set_both_formats (dialog->pv, &spec);
837 gtk_widget_hide (dialog->window);
845 hide_dialog (GtkWidget *w, gpointer data)
847 struct var_type_dialog *dialog = data;
849 gtk_widget_hide (dialog->window);