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 */
26 #include <glade/glade.h>
31 #include "var-type-dialog.h"
33 #include "psppire-variable.h"
36 #include <data/variable.h>
37 #include <data/settings.h>
42 struct var_type_dialog *dialog;
53 static const struct format_opt format_option[] =
55 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
56 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
57 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
58 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
59 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
60 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
61 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
62 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
63 { "yyddd", {FMT_JDATE, 5, 0} },
64 { "yyyyddd", {FMT_JDATE, 7, 0} },
65 { "q Q yyyy", {FMT_QYR, 8, 0} },
66 { "q Q yy", {FMT_QYR, 6, 0} },
67 { "mmm yyyy", {FMT_MOYR, 8, 0} },
68 { "mmm yy", {FMT_MOYR, 6, 0} },
69 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
70 { "dd WK yy", {FMT_WKYR, 8, 0} },
71 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}}
75 static const struct fmt_spec dollar_format[] =
91 static const int cc_format[] =
101 static void select_treeview_from_format
102 (GtkTreeView *treeview, const struct fmt_spec *fmt);
104 static void select_treeview_from_format_type(GtkTreeView *treeview,
108 /* callback for when any of the radio buttons are toggled */
110 on_toggle_1(GtkToggleButton *togglebutton, gpointer user_data)
112 struct tgs *tgs = user_data;
114 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
117 tgs->dialog->active_button = tgs->button;
120 static void update_width_decimals(const struct var_type_dialog *dialog);
122 #define force_max(x, val) if (x > val) x = val
125 Set the local format from the variable
126 and force them to have sensible values */
128 set_local_width_decimals(struct var_type_dialog *dialog)
130 dialog->fmt_l = * psppire_variable_get_write_spec(dialog->pv);
132 switch (dialog->active_button)
135 force_max( dialog->fmt_l.w, 255);
138 force_max( dialog->fmt_l.w, 40);
139 force_max( dialog->fmt_l.d, 16);
145 /* callback for when any of the radio buttons are toggled */
147 on_toggle_2(GtkToggleButton *togglebutton, gpointer user_data)
149 struct var_type_dialog *dialog = user_data;
150 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
152 switch (dialog->active_button)
155 gtk_widget_hide(dialog->date_format_list);
158 gtk_widget_hide(dialog->custom_currency_hbox);
161 gtk_widget_hide(dialog->dollar_window);
164 gtk_widget_show(dialog->label_decimals);
165 gtk_widget_show(dialog->entry_decimals);
171 set_local_width_decimals(dialog);
172 update_width_decimals(dialog);
174 switch (dialog->active_button)
177 gtk_widget_show(dialog->width_decimals);
178 gtk_widget_hide(dialog->label_decimals);
179 gtk_widget_hide(dialog->entry_decimals);
182 select_treeview_from_format(dialog->date_format_treeview,
183 &format_option[0].spec);
184 gtk_widget_hide(dialog->width_decimals);
185 gtk_widget_show(dialog->date_format_list);
188 select_treeview_from_format(dialog->dollar_treeview,
190 gtk_widget_show(dialog->dollar_window);
191 gtk_widget_show_all(dialog->width_decimals);
194 select_treeview_from_format_type(dialog->custom_treeview,
197 gtk_widget_show(dialog->width_decimals);
198 gtk_widget_show(dialog->custom_currency_hbox);
201 gtk_widget_show_all(dialog->width_decimals);
208 static gint on_var_type_ok_clicked(GtkWidget *w, gpointer data);
212 /* return a string of the form "$#,###.##" according to FMT.
213 FMT must be of type FMT_DOLLAR
216 dollar_format_template(const struct fmt_spec *fmt)
218 static gchar buf[LEN];
219 g_assert( fmt->type == FMT_DOLLAR);
223 gint int_part = fmt->w - fmt->d;
224 if ( fmt->d > 0 ) --int_part;
225 g_assert(int_part > 0);
227 g_strlcpy(buf, "$", LEN);
232 g_strlcat(buf, "#", LEN);
233 if(--c % 4 == 0 && c > 0 )
235 g_strlcat(buf, ",", LEN);
241 g_strlcat(buf, ".", LEN);
242 for ( c = 0 ; c < fmt->d ; ++c )
243 g_strlcat(buf, "#", LEN);
251 add_to_group(GtkWidget *w, gpointer data)
253 GtkSizeGroup *sg = data;
255 gtk_size_group_add_widget(sg, w);
258 /* Set the local width and decimals entry boxes to reflec the local format */
260 update_width_decimals(const struct var_type_dialog *dialog)
265 text = g_strdup_printf("%d", dialog->fmt_l.w);
266 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
269 text = g_strdup_printf("%d", dialog->fmt_l.d);
270 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
274 /* Callback for when the custom treeview row is changed.
275 It sets dialog box to reflect the selected format */
277 preview_custom(GtkWidget *w, gpointer data)
281 struct var_type_dialog *dialog = data;
283 if ( dialog->active_button != BUTTON_CUSTOM )
286 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
287 dialog->fmt_l.d = atoi(text);
289 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
290 dialog->fmt_l.w = atoi(text);
292 if ( ! check_output_specifier(&dialog->fmt_l, 0))
294 gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
295 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
303 sample_text = value_to_text(v, dialog->fmt_l);
304 gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
308 sample_text = value_to_text(v, dialog->fmt_l);
309 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
314 /* Callback for when a treeview row is changed.
315 It sets the fmt_l_spec to reflect the selected format */
317 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
319 struct var_type_dialog *dialog = data;
321 GValue the_value = {0};
323 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
325 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
327 gtk_tree_selection_get_selected (sel, &model, &iter);
329 gtk_tree_model_get_value(model, &iter, 1, &the_value);
331 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
335 /* Callback for when a treeview row is changed.
336 It sets the type of the fmt_l to reflect the selected type */
338 set_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
340 static struct fmt_spec custom_format = {0,0,0};
341 struct var_type_dialog *dialog = data;
343 GValue the_value = {0};
345 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
347 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
349 gtk_tree_selection_get_selected (sel, &model, &iter);
351 gtk_tree_model_get_value(model, &iter, 1, &the_value);
353 dialog->fmt_l = custom_format;
354 dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
361 /* Create the structure from the XML definitions */
362 struct var_type_dialog *
363 var_type_dialog_create(GladeXML *xml)
366 struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
370 dialog->window = get_widget_assert(xml,"var_type_dialog");
372 gtk_window_set_transient_for(GTK_WINDOW(dialog->window),
373 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
375 dialog->radioButton[BUTTON_NUMERIC] =
376 get_widget_assert(xml,"radiobutton1");
377 dialog->radioButton[BUTTON_COMMA] =
378 get_widget_assert(xml,"radiobutton2");
379 dialog->radioButton[BUTTON_DOT] =
380 get_widget_assert(xml,"radiobutton3");
381 dialog->radioButton[BUTTON_SCIENTIFIC] =
382 get_widget_assert(xml,"radiobutton4");
383 dialog->radioButton[BUTTON_DATE] =
384 get_widget_assert(xml,"radiobutton5");
385 dialog->radioButton[BUTTON_DOLLAR] =
386 get_widget_assert(xml,"radiobutton6");
387 dialog->radioButton[BUTTON_CUSTOM] =
388 get_widget_assert(xml,"radiobutton7");
389 dialog->radioButton[BUTTON_STRING] =
390 get_widget_assert(xml,"radiobutton8");
393 dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
394 dialog->width_decimals = get_widget_assert(xml, "width_decimals");
395 dialog->label_decimals = get_widget_assert(xml, "decimals_label");
396 dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
398 dialog->label_psample = get_widget_assert(xml, "psample_label");
399 dialog->label_nsample = get_widget_assert(xml, "nsample_label");
402 dialog->entry_width = get_widget_assert(xml,"width_entry");
404 dialog->custom_currency_hbox = get_widget_assert(xml,
405 "custom_currency_hbox");
407 dialog->dollar_window = get_widget_assert(xml, "dollar_window");
408 dialog->dollar_treeview =
409 GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
411 dialog->custom_treeview =
412 GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
415 dialog->ok = get_widget_assert(xml,"var_type_ok");
420 GtkListStore *list_store ;
422 GtkTreeViewColumn *column;
423 GtkCellRenderer *renderer ;
425 static struct tgs tgs[num_BUTTONS];
426 /* The "middle_box" is a vbox with serveral children.
427 However only one child is ever shown at a time.
428 We need to make sure that they all have the same width, to avoid
429 upleasant resizing effects */
430 GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
432 gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")),
433 add_to_group, sizeGroup);
436 for (i = 0 ; i < num_BUTTONS; ++i )
438 tgs[i].dialog = dialog;
440 g_signal_connect(dialog->radioButton[i], "toggled",
441 G_CALLBACK(on_toggle_1), &tgs[i]);
443 g_signal_connect(dialog->radioButton[i], "toggled",
444 G_CALLBACK(on_toggle_2), dialog);
447 /* Populate the date format tree view */
448 dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml,
449 "date_format_list_view"));
451 renderer = gtk_cell_renderer_text_new();
453 column = gtk_tree_view_column_new_with_attributes ("Title",
459 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
463 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
465 for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i )
467 gtk_list_store_append (list_store, &iter);
468 gtk_list_store_set (list_store, &iter,
469 0, format_option[i].desc,
470 1, &format_option[i].spec,
474 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview),
475 GTK_TREE_MODEL(list_store));
477 g_object_unref(list_store);
479 g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
480 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
483 /* populate the dollar treeview */
485 renderer = gtk_cell_renderer_text_new();
487 column = gtk_tree_view_column_new_with_attributes ("Title",
493 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
497 list_store = gtk_list_store_new (2, G_TYPE_STRING,
500 for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i )
502 gtk_list_store_append (list_store, &iter);
503 gtk_list_store_set (list_store, &iter,
504 0, dollar_format_template(&dollar_format[i]),
505 1, &dollar_format[i],
509 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),
510 GTK_TREE_MODEL(list_store));
512 g_object_unref(list_store);
514 g_signal_connect(GTK_OBJECT(dialog->dollar_treeview),
516 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
518 g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview),
520 GTK_SIGNAL_FUNC(update_width_decimals), dialog);
523 /* populate the custom treeview */
525 renderer = gtk_cell_renderer_text_new();
527 column = gtk_tree_view_column_new_with_attributes ("Title",
533 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
537 list_store = gtk_list_store_new (2, G_TYPE_STRING,
540 for ( i = 0 ; i < CC_CNT ; ++i )
543 g_snprintf(text, 4, "CC%c", 'A' + i);
544 gtk_list_store_append (list_store, &iter);
545 gtk_list_store_set (list_store, &iter,
551 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview),
552 GTK_TREE_MODEL(list_store));
554 g_object_unref(list_store);
557 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
559 GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
562 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
564 GTK_SIGNAL_FUNC(preview_custom), dialog);
567 g_signal_connect(GTK_OBJECT(dialog->entry_width),
569 GTK_SIGNAL_FUNC(preview_custom), dialog);
572 g_signal_connect(GTK_OBJECT(dialog->entry_decimals),
574 GTK_SIGNAL_FUNC(preview_custom), dialog);
577 /* Connect the OK button */
578 g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked),
588 /* Set a particular button to be active */
590 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
592 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
594 dialog->active_button = b;
599 /* Set the TREEVIEW list cursor to the item described by FMT */
601 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
606 We do this with a linear search through the model --- hardly
607 efficient, but the list is short ... */
610 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
613 for (success = gtk_tree_model_get_iter_first(model, &iter);
615 success = gtk_tree_model_iter_next(model, &iter))
617 const struct fmt_spec *spec;
621 gtk_tree_model_get_value(model, &iter, 1, &value);
623 spec = g_value_get_pointer(&value);
625 if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
631 path = gtk_tree_model_get_path(model, &iter);
634 gtk_tree_view_set_cursor(treeview, path, 0, 0);
635 gtk_tree_path_free(path);
638 g_warning("Unusual date format: %s\n", fmt_to_string(fmt));
643 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
645 select_treeview_from_format_type(GtkTreeView *treeview,
651 We do this with a linear search through the model --- hardly
652 efficient, but the list is short ... */
655 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
658 for (success = gtk_tree_model_get_iter_first(model, &iter);
660 success = gtk_tree_model_iter_next(model, &iter))
666 gtk_tree_model_get_value(model, &iter, 1, &value);
668 spec = * ((int *) g_value_get_pointer(&value));
670 if ( spec == fmt_type)
674 path = gtk_tree_model_get_path(model, &iter);
677 gtk_tree_view_set_cursor(treeview, path, 0, 0);
678 gtk_tree_path_free(path);
681 g_warning("Unknown custom type %d\n", fmt_type);
685 /* Set up the state of the dialog box to match the variable VAR */
687 var_type_dialog_set_state(struct var_type_dialog *dialog)
689 const struct fmt_spec *write_spec ;
690 GString *str = g_string_new("");
693 g_assert(dialog->pv);
695 /* Populate width and decimals */
696 write_spec = psppire_variable_get_write_spec(dialog->pv);
698 g_string_printf(str, "%d", write_spec->d);
700 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals),
703 g_string_printf(str, "%d", write_spec->w);
705 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width),
708 g_string_free(str, TRUE);
710 /* Populate the radio button states */
711 switch (write_spec->type)
714 var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
715 gtk_widget_show_all(dialog->width_decimals);
718 var_type_dialog_set_active_button(dialog, BUTTON_STRING);
719 gtk_widget_hide(dialog->label_decimals);
720 gtk_widget_hide(dialog->entry_decimals);
723 var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
724 gtk_widget_show_all(dialog->width_decimals);
727 var_type_dialog_set_active_button(dialog, BUTTON_DOT);
728 gtk_widget_show_all(dialog->width_decimals);
731 var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
732 gtk_widget_show_all(dialog->width_decimals);
734 select_treeview_from_format(dialog->dollar_treeview, write_spec);
749 var_type_dialog_set_active_button(dialog, BUTTON_DATE);
750 gtk_widget_hide(dialog->width_decimals);
751 gtk_widget_show(dialog->date_format_list);
752 select_treeview_from_format(dialog->date_format_treeview, write_spec);
759 var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
760 select_treeview_from_format_type(dialog->custom_treeview,
762 gtk_widget_show_all(dialog->width_decimals);
765 gtk_widget_show_all(dialog->width_decimals);
771 /* Popup the dialog box */
773 var_type_dialog_show(struct var_type_dialog *dialog)
775 var_type_dialog_set_state(dialog);
777 gtk_widget_show(dialog->window);
780 /* Fills F with an output format specification with type TYPE, width
781 W, and D decimals. Iff it's a valid format, then return true.
784 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
789 return check_output_specifier (f, true);
795 /* Callbacks for the Variable Type Dialog Box */
797 /* Callback for when the var type dialog is closed using the OK button.
798 It sets the appropriate variable accordingly. */
800 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
802 struct var_type_dialog *dialog = data;
805 g_assert(dialog->pv);
808 gint width = atoi(gtk_entry_get_text
809 (GTK_ENTRY(dialog->entry_width)));
811 gint decimals = atoi(gtk_entry_get_text
812 (GTK_ENTRY(dialog->entry_decimals)));
814 gint new_type = NUMERIC;
817 struct fmt_spec spec;
818 switch (dialog->active_button)
823 result = make_output_format_try(&spec, FMT_A, width, 0);
826 result = make_output_format_try(&spec, FMT_F, width, decimals);
829 result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
832 result = make_output_format_try(&spec, FMT_DOT, width, decimals);
834 case BUTTON_SCIENTIFIC:
835 result = make_output_format_try(&spec, FMT_E, width, decimals);
839 g_assert(check_output_specifier(&dialog->fmt_l, TRUE));
840 result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
843 result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
846 g_print("Unknown variable type: %d\n", dialog->active_button) ;
851 if ( result == true )
853 psppire_variable_set_type(dialog->pv, new_type);
854 psppire_variable_set_width(dialog->pv, new_width);
855 psppire_variable_set_write_spec(dialog->pv, spec);
856 psppire_variable_set_print_spec(dialog->pv, spec);
860 gtk_widget_hide(dialog->window);
868 on_var_type_cancel_clicked(GtkWidget *w, gpointer data)