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"
41 struct var_type_dialog *dialog;
52 static const struct format_opt format_option[] =
54 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
55 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
56 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
57 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
58 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
59 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
60 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
61 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
62 { "yyddd", {FMT_JDATE, 5, 0} },
63 { "yyyyddd", {FMT_JDATE, 7, 0} },
64 { "q Q yyyy", {FMT_QYR, 8, 0} },
65 { "q Q yy", {FMT_QYR, 6, 0} },
66 { "mmm yyyy", {FMT_MOYR, 8, 0} },
67 { "mmm yy", {FMT_MOYR, 6, 0} },
68 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
69 { "dd WK yy", {FMT_WKYR, 8, 0} },
70 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}}
74 static const struct fmt_spec dollar_format[] =
90 static const int cc_format[] =
100 static void select_treeview_from_format
101 (GtkTreeView *treeview, const struct fmt_spec *fmt);
103 static void select_treeview_from_format_type(GtkTreeView *treeview,
107 /* callback for when any of the radio buttons are toggled */
109 on_toggle_1(GtkToggleButton *togglebutton, gpointer user_data)
111 struct tgs *tgs = user_data;
113 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
116 tgs->dialog->active_button = tgs->button;
119 static void update_width_decimals(const struct var_type_dialog *dialog);
121 #define force_max(x, val) if (x > val) x = val
124 Set the local format from the variable
125 and force them to have sensible values */
127 set_local_width_decimals(struct var_type_dialog *dialog)
129 dialog->fmt_l = * psppire_variable_get_write_spec(dialog->pv);
131 switch (dialog->active_button)
134 force_max( dialog->fmt_l.w, 255);
137 force_max( dialog->fmt_l.w, 40);
138 force_max( dialog->fmt_l.d, 16);
144 /* callback for when any of the radio buttons are toggled */
146 on_toggle_2(GtkToggleButton *togglebutton, gpointer user_data)
148 struct var_type_dialog *dialog = user_data;
149 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
151 switch (dialog->active_button)
154 gtk_widget_hide(dialog->date_format_list);
157 gtk_widget_hide(dialog->custom_currency_hbox);
160 gtk_widget_hide(dialog->dollar_window);
163 gtk_widget_show(dialog->label_decimals);
164 gtk_widget_show(dialog->entry_decimals);
170 set_local_width_decimals(dialog);
171 update_width_decimals(dialog);
173 switch (dialog->active_button)
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);
211 /* return a string of the form "$#,###.##" according to FMT.
212 FMT must be of type FMT_DOLLAR
215 dollar_format_template(const struct fmt_spec *fmt)
217 static gchar buf[LEN];
218 g_assert( fmt->type == FMT_DOLLAR);
220 gint int_part = fmt->w - fmt->d;
221 if ( fmt->d > 0 ) --int_part;
222 g_assert(int_part > 0);
224 g_strlcpy(buf, "$", LEN);
226 gint c = int_part - 1;
229 g_strlcat(buf, "#", LEN);
230 if(--c % 4 == 0 && c > 0 )
232 g_strlcat(buf, ",", LEN);
238 g_strlcat(buf, ".", LEN);
239 for ( c = 0 ; c < fmt->d ; ++c )
240 g_strlcat(buf, "#", LEN);
247 add_to_group(GtkWidget *w, gpointer data)
249 GtkSizeGroup *sg = data;
251 gtk_size_group_add_widget(sg, w);
254 /* Set the local width and decimals entry boxes to reflec the local format */
256 update_width_decimals(const struct var_type_dialog *dialog)
260 gchar *text = g_strdup_printf("%d", dialog->fmt_l.w);
261 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
264 text = g_strdup_printf("%d", dialog->fmt_l.d);
265 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
269 /* Callback for when the custom treeview row is changed.
270 It sets dialog box to reflect the selected format */
272 preview_custom(GtkWidget *w, gpointer data)
274 struct var_type_dialog *dialog = data;
276 if ( dialog->active_button != BUTTON_CUSTOM )
279 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
280 dialog->fmt_l.d = atoi(text);
282 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
283 dialog->fmt_l.w = atoi(text);
285 if ( ! check_output_specifier(&dialog->fmt_l, 0))
287 gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
288 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
296 sample_text = value_to_text(v, dialog->fmt_l);
297 gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
301 sample_text = value_to_text(v, dialog->fmt_l);
302 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
307 /* Callback for when a treeview row is changed.
308 It sets the fmt_l_spec to reflect the selected format */
310 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
312 struct var_type_dialog *dialog = data;
314 GValue the_value = {0};
316 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
318 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
320 gtk_tree_selection_get_selected (sel, &model, &iter);
322 gtk_tree_model_get_value(model, &iter, 1, &the_value);
324 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
328 /* Callback for when a treeview row is changed.
329 It sets the type of the fmt_l to reflect the selected type */
331 set_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
333 static struct fmt_spec custom_format = {0,0,0};
334 struct var_type_dialog *dialog = data;
336 GValue the_value = {0};
338 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
340 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
342 gtk_tree_selection_get_selected (sel, &model, &iter);
344 gtk_tree_model_get_value(model, &iter, 1, &the_value);
346 dialog->fmt_l = custom_format;
347 dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
354 /* Create the structure from the XML definitions */
355 struct var_type_dialog *
356 var_type_dialog_create(GladeXML *xml)
361 struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
363 dialog->window = get_widget_assert(xml,"var_type_dialog");
365 gtk_window_set_transient_for(GTK_WINDOW(dialog->window),
366 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
368 dialog->radioButton[BUTTON_NUMERIC] =
369 get_widget_assert(xml,"radiobutton1");
370 dialog->radioButton[BUTTON_COMMA] =
371 get_widget_assert(xml,"radiobutton2");
372 dialog->radioButton[BUTTON_DOT] =
373 get_widget_assert(xml,"radiobutton3");
374 dialog->radioButton[BUTTON_SCIENTIFIC] =
375 get_widget_assert(xml,"radiobutton4");
376 dialog->radioButton[BUTTON_DATE] =
377 get_widget_assert(xml,"radiobutton5");
378 dialog->radioButton[BUTTON_DOLLAR] =
379 get_widget_assert(xml,"radiobutton6");
380 dialog->radioButton[BUTTON_CUSTOM] =
381 get_widget_assert(xml,"radiobutton7");
382 dialog->radioButton[BUTTON_STRING] =
383 get_widget_assert(xml,"radiobutton8");
386 dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
387 dialog->width_decimals = get_widget_assert(xml, "width_decimals");
388 dialog->label_decimals = get_widget_assert(xml, "decimals_label");
389 dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
391 dialog->label_psample = get_widget_assert(xml, "psample_label");
392 dialog->label_nsample = get_widget_assert(xml, "nsample_label");
395 dialog->entry_width = get_widget_assert(xml,"width_entry");
397 dialog->custom_currency_hbox = get_widget_assert(xml,
398 "custom_currency_hbox");
400 dialog->dollar_window = get_widget_assert(xml, "dollar_window");
401 dialog->dollar_treeview =
402 GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
404 dialog->custom_treeview =
405 GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
408 dialog->ok = get_widget_assert(xml,"var_type_ok");
411 /* The "middle_box" is a vbox with serveral children.
412 However only one child is ever shown at a time.
413 We need to make sure that they all have the same width, to avoid
414 upleasant resizing effects */
415 GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
416 gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")),
417 add_to_group, sizeGroup);
420 static struct tgs tgs[num_BUTTONS];
421 for (i = 0 ; i < num_BUTTONS; ++i )
423 tgs[i].dialog = dialog;
425 g_signal_connect(dialog->radioButton[i], "toggled",
426 G_CALLBACK(on_toggle_1), &tgs[i]);
428 g_signal_connect(dialog->radioButton[i], "toggled",
429 G_CALLBACK(on_toggle_2), dialog);
432 /* Populate the date format tree view */
433 dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml,
434 "date_format_list_view"));
436 GtkTreeViewColumn *column;
438 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
440 column = gtk_tree_view_column_new_with_attributes ("Title",
446 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
451 GtkListStore *list_store = gtk_list_store_new (2, G_TYPE_STRING,
454 for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i )
456 gtk_list_store_append (list_store, &iter);
457 gtk_list_store_set (list_store, &iter,
458 0, format_option[i].desc,
459 1, &format_option[i].spec,
463 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview),
464 GTK_TREE_MODEL(list_store));
466 g_object_unref(list_store);
468 g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
469 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
472 /* populate the dollar treeview */
474 renderer = gtk_cell_renderer_text_new();
476 column = gtk_tree_view_column_new_with_attributes ("Title",
482 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
486 list_store = gtk_list_store_new (2, G_TYPE_STRING,
489 for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i )
491 gtk_list_store_append (list_store, &iter);
492 gtk_list_store_set (list_store, &iter,
493 0, dollar_format_template(&dollar_format[i]),
494 1, &dollar_format[i],
498 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),
499 GTK_TREE_MODEL(list_store));
501 g_object_unref(list_store);
503 g_signal_connect(GTK_OBJECT(dialog->dollar_treeview),
505 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
507 g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview),
509 GTK_SIGNAL_FUNC(update_width_decimals), dialog);
512 /* populate the custom treeview */
514 renderer = gtk_cell_renderer_text_new();
516 column = gtk_tree_view_column_new_with_attributes ("Title",
522 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
526 list_store = gtk_list_store_new (2, G_TYPE_STRING,
529 for ( i = 0 ; i < CC_CNT ; ++i )
532 g_snprintf(text, 4, "CC%c", 'A' + i);
533 gtk_list_store_append (list_store, &iter);
534 gtk_list_store_set (list_store, &iter,
540 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview),
541 GTK_TREE_MODEL(list_store));
543 g_object_unref(list_store);
546 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
548 GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
551 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
553 GTK_SIGNAL_FUNC(preview_custom), dialog);
556 g_signal_connect(GTK_OBJECT(dialog->entry_width),
558 GTK_SIGNAL_FUNC(preview_custom), dialog);
561 g_signal_connect(GTK_OBJECT(dialog->entry_decimals),
563 GTK_SIGNAL_FUNC(preview_custom), dialog);
566 /* Connect the OK button */
567 g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked),
574 /* Set a particular button to be active */
576 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
578 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
580 dialog->active_button = b;
585 /* Set the TREEVIEW list cursor to the item described by FMT */
587 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
590 We do this with a linear search through the model --- hardly
591 efficient, but the list is short ... */
594 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
597 for (success = gtk_tree_model_get_iter_first(model, &iter);
599 success = gtk_tree_model_iter_next(model, &iter))
603 gtk_tree_model_get_value(model, &iter, 1, &value);
605 const struct fmt_spec *spec = g_value_get_pointer(&value);
607 if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
613 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
616 gtk_tree_view_set_cursor(treeview, path, 0, 0);
617 gtk_tree_path_free(path);
620 g_warning("Unusual date format: %s\n", fmt_to_string(fmt));
625 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
627 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))
644 gtk_tree_model_get_value(model, &iter, 1, &value);
646 const int spec = * ((int *) g_value_get_pointer(&value));
648 if ( spec == fmt_type)
652 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
655 gtk_tree_view_set_cursor(treeview, path, 0, 0);
656 gtk_tree_path_free(path);
659 g_warning("Unknown custom type %d\n", fmt_type);
663 /* Set up the state of the dialog box to match the variable VAR */
665 var_type_dialog_set_state(struct var_type_dialog *dialog)
668 g_assert(dialog->pv);
670 /* Populate width and decimals */
671 const struct fmt_spec *write_spec = psppire_variable_get_write_spec(dialog->pv);
673 GString *str = g_string_new("");
675 g_string_printf(str, "%d", write_spec->d);
677 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals),
680 g_string_printf(str, "%d", write_spec->w);
682 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width),
685 g_string_free(str, TRUE);
687 /* Populate the radio button states */
688 switch (write_spec->type)
691 var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
692 gtk_widget_show_all(dialog->width_decimals);
695 var_type_dialog_set_active_button(dialog, BUTTON_STRING);
696 gtk_widget_hide(dialog->label_decimals);
697 gtk_widget_hide(dialog->entry_decimals);
700 var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
701 gtk_widget_show_all(dialog->width_decimals);
704 var_type_dialog_set_active_button(dialog, BUTTON_DOT);
705 gtk_widget_show_all(dialog->width_decimals);
708 var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
709 gtk_widget_show_all(dialog->width_decimals);
711 select_treeview_from_format(dialog->dollar_treeview, write_spec);
726 var_type_dialog_set_active_button(dialog, BUTTON_DATE);
727 gtk_widget_hide(dialog->width_decimals);
728 gtk_widget_show(dialog->date_format_list);
729 select_treeview_from_format(dialog->date_format_treeview, write_spec);
736 var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
737 select_treeview_from_format_type(dialog->custom_treeview,
739 gtk_widget_show_all(dialog->width_decimals);
742 gtk_widget_show_all(dialog->width_decimals);
748 /* Popup the dialog box */
750 var_type_dialog_show(struct var_type_dialog *dialog)
752 var_type_dialog_set_state(dialog);
754 gtk_widget_show(dialog->window);
757 /* Fills F with an output format specification with type TYPE, width
758 W, and D decimals. Iff it's a valid format, then return true.
761 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
766 return check_output_specifier (f, true);
772 /* Callbacks for the Variable Type Dialog Box */
774 /* Callback for when the var type dialog is closed using the OK button.
775 It sets the appropriate variable accordingly. */
777 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
779 struct var_type_dialog *dialog = data;
782 g_assert(dialog->pv);
784 gint width = atoi(gtk_entry_get_text
785 (GTK_ENTRY(dialog->entry_width)));
787 gint decimals = atoi(gtk_entry_get_text
788 (GTK_ENTRY(dialog->entry_decimals)));
790 gint new_type = NUMERIC;
793 struct fmt_spec spec;
794 switch (dialog->active_button)
799 result = make_output_format_try(&spec, FMT_A, width, 0);
802 result = make_output_format_try(&spec, FMT_F, width, decimals);
805 result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
808 result = make_output_format_try(&spec, FMT_DOT, width, decimals);
810 case BUTTON_SCIENTIFIC:
811 result = make_output_format_try(&spec, FMT_E, width, decimals);
815 g_assert(check_output_specifier(&dialog->fmt_l, TRUE));
816 result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
819 result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
822 g_print("Unknown variable type: %d\n", dialog->active_button) ;
827 if ( result == true )
829 psppire_variable_set_type(dialog->pv, new_type);
830 psppire_variable_set_width(dialog->pv, new_width);
831 psppire_variable_set_write_spec(dialog->pv, spec);
832 psppire_variable_set_print_spec(dialog->pv, spec);
835 gtk_widget_hide(dialog->window);
843 on_var_type_cancel_clicked(GtkWidget *w, gpointer data)