2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005 Free Software Foundation
4 Written by John Darrington
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* This module describes the behaviour of the Variable Type dialog box used
23 for inputing the variable type in the var sheet */
28 #include <glade/glade.h>
33 #include "var-type-dialog.h"
35 #include "psppire-variable.h"
38 #include <data/variable.h>
39 #include <data/settings.h>
44 struct var_type_dialog *dialog;
55 static const struct format_opt format_option[] =
57 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
58 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
59 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
60 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
61 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
62 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
63 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
64 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
65 { "yyddd", {FMT_JDATE, 5, 0} },
66 { "yyyyddd", {FMT_JDATE, 7, 0} },
67 { "q Q yyyy", {FMT_QYR, 8, 0} },
68 { "q Q yy", {FMT_QYR, 6, 0} },
69 { "mmm yyyy", {FMT_MOYR, 8, 0} },
70 { "mmm yy", {FMT_MOYR, 6, 0} },
71 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
72 { "dd WK yy", {FMT_WKYR, 8, 0} },
73 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 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 = * psppire_variable_get_write_spec(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);
214 /* return a string of the form "$#,###.##" according to FMT.
215 FMT must be of type FMT_DOLLAR
218 dollar_format_template(const struct fmt_spec *fmt)
220 static gchar buf[LEN];
221 g_assert( fmt->type == FMT_DOLLAR);
225 gint int_part = fmt->w - fmt->d;
226 if ( fmt->d > 0 ) --int_part;
227 g_assert(int_part > 0);
229 g_strlcpy(buf, "$", LEN);
234 g_strlcat(buf, "#", LEN);
235 if(--c % 4 == 0 && c > 0 )
237 g_strlcat(buf, ",", LEN);
243 g_strlcat(buf, ".", LEN);
244 for ( c = 0 ; c < fmt->d ; ++c )
245 g_strlcat(buf, "#", LEN);
253 add_to_group(GtkWidget *w, gpointer data)
255 GtkSizeGroup *sg = data;
257 gtk_size_group_add_widget(sg, w);
260 /* Set the local width and decimals entry boxes to reflec the local format */
262 update_width_decimals(const struct var_type_dialog *dialog)
267 text = g_strdup_printf("%d", dialog->fmt_l.w);
268 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
271 text = g_strdup_printf("%d", dialog->fmt_l.d);
272 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
276 /* Callback for when the custom treeview row is changed.
277 It sets dialog box to reflect the selected format */
279 preview_custom(GtkWidget *w, gpointer data)
283 struct var_type_dialog *dialog = data;
285 if ( dialog->active_button != BUTTON_CUSTOM )
288 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
289 dialog->fmt_l.d = atoi(text);
291 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
292 dialog->fmt_l.w = atoi(text);
294 if ( ! check_output_specifier(&dialog->fmt_l, 0))
296 gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
297 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
305 sample_text = value_to_text(v, dialog->fmt_l);
306 gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
310 sample_text = value_to_text(v, dialog->fmt_l);
311 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
316 /* Callback for when a treeview row is changed.
317 It sets the fmt_l_spec to reflect the selected format */
319 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
321 struct var_type_dialog *dialog = data;
323 GValue the_value = {0};
325 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
327 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
329 gtk_tree_selection_get_selected (sel, &model, &iter);
331 gtk_tree_model_get_value(model, &iter, 1, &the_value);
333 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
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_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
342 static struct fmt_spec custom_format = {0,0,0};
343 struct var_type_dialog *dialog = data;
345 GValue the_value = {0};
347 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
349 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
351 gtk_tree_selection_get_selected (sel, &model, &iter);
353 gtk_tree_model_get_value(model, &iter, 1, &the_value);
355 dialog->fmt_l = custom_format;
356 dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
363 /* Create the structure from the XML definitions */
364 struct var_type_dialog *
365 var_type_dialog_create(GladeXML *xml)
368 struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
372 dialog->window = get_widget_assert(xml,"var_type_dialog");
374 gtk_window_set_transient_for(GTK_WINDOW(dialog->window),
375 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
377 dialog->radioButton[BUTTON_NUMERIC] =
378 get_widget_assert(xml,"radiobutton1");
379 dialog->radioButton[BUTTON_COMMA] =
380 get_widget_assert(xml,"radiobutton2");
381 dialog->radioButton[BUTTON_DOT] =
382 get_widget_assert(xml,"radiobutton3");
383 dialog->radioButton[BUTTON_SCIENTIFIC] =
384 get_widget_assert(xml,"radiobutton4");
385 dialog->radioButton[BUTTON_DATE] =
386 get_widget_assert(xml,"radiobutton5");
387 dialog->radioButton[BUTTON_DOLLAR] =
388 get_widget_assert(xml,"radiobutton6");
389 dialog->radioButton[BUTTON_CUSTOM] =
390 get_widget_assert(xml,"radiobutton7");
391 dialog->radioButton[BUTTON_STRING] =
392 get_widget_assert(xml,"radiobutton8");
395 dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
396 dialog->width_decimals = get_widget_assert(xml, "width_decimals");
397 dialog->label_decimals = get_widget_assert(xml, "decimals_label");
398 dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
400 dialog->label_psample = get_widget_assert(xml, "psample_label");
401 dialog->label_nsample = get_widget_assert(xml, "nsample_label");
404 dialog->entry_width = get_widget_assert(xml,"width_entry");
406 dialog->custom_currency_hbox = get_widget_assert(xml,
407 "custom_currency_hbox");
409 dialog->dollar_window = get_widget_assert(xml, "dollar_window");
410 dialog->dollar_treeview =
411 GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
413 dialog->custom_treeview =
414 GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
417 dialog->ok = get_widget_assert(xml,"var_type_ok");
422 GtkListStore *list_store ;
424 GtkTreeViewColumn *column;
425 GtkCellRenderer *renderer ;
427 static struct tgs tgs[num_BUTTONS];
428 /* The "middle_box" is a vbox with serveral children.
429 However only one child is ever shown at a time.
430 We need to make sure that they all have the same width, to avoid
431 upleasant resizing effects */
432 GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
434 gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")),
435 add_to_group, sizeGroup);
438 for (i = 0 ; i < num_BUTTONS; ++i )
440 tgs[i].dialog = dialog;
442 g_signal_connect(dialog->radioButton[i], "toggled",
443 G_CALLBACK(on_toggle_1), &tgs[i]);
445 g_signal_connect(dialog->radioButton[i], "toggled",
446 G_CALLBACK(on_toggle_2), dialog);
449 /* Populate the date format tree view */
450 dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml,
451 "date_format_list_view"));
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->date_format_treeview),
465 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
467 for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i )
469 gtk_list_store_append (list_store, &iter);
470 gtk_list_store_set (list_store, &iter,
471 0, format_option[i].desc,
472 1, &format_option[i].spec,
476 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview),
477 GTK_TREE_MODEL(list_store));
479 g_object_unref(list_store);
481 g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
482 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
485 /* populate the dollar treeview */
487 renderer = gtk_cell_renderer_text_new();
489 column = gtk_tree_view_column_new_with_attributes ("Title",
495 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
499 list_store = gtk_list_store_new (2, G_TYPE_STRING,
502 for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i )
504 gtk_list_store_append (list_store, &iter);
505 gtk_list_store_set (list_store, &iter,
506 0, dollar_format_template(&dollar_format[i]),
507 1, &dollar_format[i],
511 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),
512 GTK_TREE_MODEL(list_store));
514 g_object_unref(list_store);
516 g_signal_connect(GTK_OBJECT(dialog->dollar_treeview),
518 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
520 g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview),
522 GTK_SIGNAL_FUNC(update_width_decimals), dialog);
525 /* populate the custom treeview */
527 renderer = gtk_cell_renderer_text_new();
529 column = gtk_tree_view_column_new_with_attributes ("Title",
535 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
539 list_store = gtk_list_store_new (2, G_TYPE_STRING,
542 for ( i = 0 ; i < CC_CNT ; ++i )
545 g_snprintf(text, 4, "CC%c", 'A' + i);
546 gtk_list_store_append (list_store, &iter);
547 gtk_list_store_set (list_store, &iter,
553 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview),
554 GTK_TREE_MODEL(list_store));
556 g_object_unref(list_store);
559 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
561 GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
564 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
566 GTK_SIGNAL_FUNC(preview_custom), dialog);
569 g_signal_connect(GTK_OBJECT(dialog->entry_width),
571 GTK_SIGNAL_FUNC(preview_custom), dialog);
574 g_signal_connect(GTK_OBJECT(dialog->entry_decimals),
576 GTK_SIGNAL_FUNC(preview_custom), dialog);
579 /* Connect the OK button */
580 g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked),
590 /* Set a particular button to be active */
592 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
594 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
596 dialog->active_button = b;
601 /* Set the TREEVIEW list cursor to the item described by FMT */
603 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
608 We do this with a linear search through the model --- hardly
609 efficient, but the list is short ... */
612 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
615 for (success = gtk_tree_model_get_iter_first(model, &iter);
617 success = gtk_tree_model_iter_next(model, &iter))
619 const struct fmt_spec *spec;
623 gtk_tree_model_get_value(model, &iter, 1, &value);
625 spec = g_value_get_pointer(&value);
627 if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
633 path = gtk_tree_model_get_path(model, &iter);
636 gtk_tree_view_set_cursor(treeview, path, 0, 0);
637 gtk_tree_path_free(path);
640 g_warning("Unusual date format: %s\n", fmt_to_string(fmt));
645 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
647 select_treeview_from_format_type(GtkTreeView *treeview,
653 We do this with a linear search through the model --- hardly
654 efficient, but the list is short ... */
657 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
660 for (success = gtk_tree_model_get_iter_first(model, &iter);
662 success = gtk_tree_model_iter_next(model, &iter))
668 gtk_tree_model_get_value(model, &iter, 1, &value);
670 spec = * ((int *) g_value_get_pointer(&value));
672 if ( spec == fmt_type)
676 path = gtk_tree_model_get_path(model, &iter);
679 gtk_tree_view_set_cursor(treeview, path, 0, 0);
680 gtk_tree_path_free(path);
683 g_warning("Unknown custom type %d\n", fmt_type);
687 /* Set up the state of the dialog box to match the variable VAR */
689 var_type_dialog_set_state(struct var_type_dialog *dialog)
691 const struct fmt_spec *write_spec ;
692 GString *str = g_string_new("");
695 g_assert(dialog->pv);
697 /* Populate width and decimals */
698 write_spec = psppire_variable_get_write_spec(dialog->pv);
700 g_string_printf(str, "%d", write_spec->d);
702 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals),
705 g_string_printf(str, "%d", write_spec->w);
707 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width),
710 g_string_free(str, TRUE);
712 /* Populate the radio button states */
713 switch (write_spec->type)
716 var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
717 gtk_widget_show_all(dialog->width_decimals);
720 var_type_dialog_set_active_button(dialog, BUTTON_STRING);
721 gtk_widget_hide(dialog->label_decimals);
722 gtk_widget_hide(dialog->entry_decimals);
725 var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
726 gtk_widget_show_all(dialog->width_decimals);
729 var_type_dialog_set_active_button(dialog, BUTTON_DOT);
730 gtk_widget_show_all(dialog->width_decimals);
733 var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
734 gtk_widget_show_all(dialog->width_decimals);
736 select_treeview_from_format(dialog->dollar_treeview, write_spec);
751 var_type_dialog_set_active_button(dialog, BUTTON_DATE);
752 gtk_widget_hide(dialog->width_decimals);
753 gtk_widget_show(dialog->date_format_list);
754 select_treeview_from_format(dialog->date_format_treeview, write_spec);
761 var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
762 select_treeview_from_format_type(dialog->custom_treeview,
764 gtk_widget_show_all(dialog->width_decimals);
767 gtk_widget_show_all(dialog->width_decimals);
773 /* Popup the dialog box */
775 var_type_dialog_show(struct var_type_dialog *dialog)
777 var_type_dialog_set_state(dialog);
779 gtk_widget_show(dialog->window);
782 /* Fills F with an output format specification with type TYPE, width
783 W, and D decimals. Iff it's a valid format, then return true.
786 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
791 return check_output_specifier (f, true);
797 /* Callbacks for the Variable Type Dialog Box */
799 /* Callback for when the var type dialog is closed using the OK button.
800 It sets the appropriate variable accordingly. */
802 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
804 struct var_type_dialog *dialog = data;
807 g_assert(dialog->pv);
810 gint width = atoi(gtk_entry_get_text
811 (GTK_ENTRY(dialog->entry_width)));
813 gint decimals = atoi(gtk_entry_get_text
814 (GTK_ENTRY(dialog->entry_decimals)));
816 gint new_type = NUMERIC;
819 struct fmt_spec spec;
820 switch (dialog->active_button)
825 result = make_output_format_try(&spec, FMT_A, width, 0);
828 result = make_output_format_try(&spec, FMT_F, width, decimals);
831 result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
834 result = make_output_format_try(&spec, FMT_DOT, width, decimals);
836 case BUTTON_SCIENTIFIC:
837 result = make_output_format_try(&spec, FMT_E, width, decimals);
841 g_assert(check_output_specifier(&dialog->fmt_l, TRUE));
842 result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
845 result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
848 g_print("Unknown variable type: %d\n", dialog->active_button) ;
853 if ( result == true )
855 psppire_variable_set_type(dialog->pv, new_type);
856 psppire_variable_set_width(dialog->pv, new_width);
857 psppire_variable_set_write_spec(dialog->pv, spec);
858 psppire_variable_set_print_spec(dialog->pv, spec);
862 gtk_widget_hide(dialog->window);
870 on_var_type_cancel_clicked(GtkWidget *w, gpointer data)