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"
35 #include <data/variable.h>
36 #include <data/settings.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);
222 gint int_part = fmt->w - fmt->d;
223 if ( fmt->d > 0 ) --int_part;
224 g_assert(int_part > 0);
226 g_strlcpy(buf, "$", LEN);
231 g_strlcat(buf, "#", LEN);
232 if(--c % 4 == 0 && c > 0 )
234 g_strlcat(buf, ",", LEN);
240 g_strlcat(buf, ".", LEN);
241 for ( c = 0 ; c < fmt->d ; ++c )
242 g_strlcat(buf, "#", LEN);
250 add_to_group(GtkWidget *w, gpointer data)
252 GtkSizeGroup *sg = data;
254 gtk_size_group_add_widget(sg, w);
257 /* Set the local width and decimals entry boxes to reflec the local format */
259 update_width_decimals(const struct var_type_dialog *dialog)
264 text = g_strdup_printf("%d", dialog->fmt_l.w);
265 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
268 text = g_strdup_printf("%d", dialog->fmt_l.d);
269 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
273 /* Callback for when the custom treeview row is changed.
274 It sets dialog box to reflect the selected format */
276 preview_custom(GtkWidget *w, gpointer data)
280 struct var_type_dialog *dialog = data;
282 if ( dialog->active_button != BUTTON_CUSTOM )
285 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
286 dialog->fmt_l.d = atoi(text);
288 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
289 dialog->fmt_l.w = atoi(text);
291 if ( ! check_output_specifier(&dialog->fmt_l, 0))
293 gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
294 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
302 sample_text = value_to_text(v, dialog->fmt_l);
303 gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
307 sample_text = value_to_text(v, dialog->fmt_l);
308 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
313 /* Callback for when a treeview row is changed.
314 It sets the fmt_l_spec to reflect the selected format */
316 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
318 struct var_type_dialog *dialog = data;
320 GValue the_value = {0};
322 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
324 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
326 gtk_tree_selection_get_selected (sel, &model, &iter);
328 gtk_tree_model_get_value(model, &iter, 1, &the_value);
330 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
334 /* Callback for when a treeview row is changed.
335 It sets the type of the fmt_l to reflect the selected type */
337 set_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
339 static struct fmt_spec custom_format = {0,0,0};
340 struct var_type_dialog *dialog = data;
342 GValue the_value = {0};
344 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
346 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
348 gtk_tree_selection_get_selected (sel, &model, &iter);
350 gtk_tree_model_get_value(model, &iter, 1, &the_value);
352 dialog->fmt_l = custom_format;
353 dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
360 /* Create the structure from the XML definitions */
361 struct var_type_dialog *
362 var_type_dialog_create(GladeXML *xml)
365 struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
369 dialog->window = get_widget_assert(xml,"var_type_dialog");
371 gtk_window_set_transient_for(GTK_WINDOW(dialog->window),
372 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
374 dialog->radioButton[BUTTON_NUMERIC] =
375 get_widget_assert(xml,"radiobutton1");
376 dialog->radioButton[BUTTON_COMMA] =
377 get_widget_assert(xml,"radiobutton2");
378 dialog->radioButton[BUTTON_DOT] =
379 get_widget_assert(xml,"radiobutton3");
380 dialog->radioButton[BUTTON_SCIENTIFIC] =
381 get_widget_assert(xml,"radiobutton4");
382 dialog->radioButton[BUTTON_DATE] =
383 get_widget_assert(xml,"radiobutton5");
384 dialog->radioButton[BUTTON_DOLLAR] =
385 get_widget_assert(xml,"radiobutton6");
386 dialog->radioButton[BUTTON_CUSTOM] =
387 get_widget_assert(xml,"radiobutton7");
388 dialog->radioButton[BUTTON_STRING] =
389 get_widget_assert(xml,"radiobutton8");
392 dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
393 dialog->width_decimals = get_widget_assert(xml, "width_decimals");
394 dialog->label_decimals = get_widget_assert(xml, "decimals_label");
395 dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
397 dialog->label_psample = get_widget_assert(xml, "psample_label");
398 dialog->label_nsample = get_widget_assert(xml, "nsample_label");
401 dialog->entry_width = get_widget_assert(xml,"width_entry");
403 dialog->custom_currency_hbox = get_widget_assert(xml,
404 "custom_currency_hbox");
406 dialog->dollar_window = get_widget_assert(xml, "dollar_window");
407 dialog->dollar_treeview =
408 GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
410 dialog->custom_treeview =
411 GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
414 dialog->ok = get_widget_assert(xml,"var_type_ok");
419 GtkListStore *list_store ;
421 GtkTreeViewColumn *column;
422 GtkCellRenderer *renderer ;
424 static struct tgs tgs[num_BUTTONS];
425 /* The "middle_box" is a vbox with serveral children.
426 However only one child is ever shown at a time.
427 We need to make sure that they all have the same width, to avoid
428 upleasant resizing effects */
429 GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
431 gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")),
432 add_to_group, sizeGroup);
435 for (i = 0 ; i < num_BUTTONS; ++i )
437 tgs[i].dialog = dialog;
439 g_signal_connect(dialog->radioButton[i], "toggled",
440 G_CALLBACK(on_toggle_1), &tgs[i]);
442 g_signal_connect(dialog->radioButton[i], "toggled",
443 G_CALLBACK(on_toggle_2), dialog);
446 /* Populate the date format tree view */
447 dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml,
448 "date_format_list_view"));
450 renderer = gtk_cell_renderer_text_new();
452 column = gtk_tree_view_column_new_with_attributes ("Title",
458 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
462 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
464 for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i )
466 gtk_list_store_append (list_store, &iter);
467 gtk_list_store_set (list_store, &iter,
468 0, format_option[i].desc,
469 1, &format_option[i].spec,
473 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview),
474 GTK_TREE_MODEL(list_store));
476 g_object_unref(list_store);
478 g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
479 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
482 /* populate the dollar treeview */
484 renderer = gtk_cell_renderer_text_new();
486 column = gtk_tree_view_column_new_with_attributes ("Title",
492 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
496 list_store = gtk_list_store_new (2, G_TYPE_STRING,
499 for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i )
501 gtk_list_store_append (list_store, &iter);
502 gtk_list_store_set (list_store, &iter,
503 0, dollar_format_template(&dollar_format[i]),
504 1, &dollar_format[i],
508 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),
509 GTK_TREE_MODEL(list_store));
511 g_object_unref(list_store);
513 g_signal_connect(GTK_OBJECT(dialog->dollar_treeview),
515 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
517 g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview),
519 GTK_SIGNAL_FUNC(update_width_decimals), dialog);
522 /* populate the custom treeview */
524 renderer = gtk_cell_renderer_text_new();
526 column = gtk_tree_view_column_new_with_attributes ("Title",
532 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
536 list_store = gtk_list_store_new (2, G_TYPE_STRING,
539 for ( i = 0 ; i < CC_CNT ; ++i )
542 g_snprintf(text, 4, "CC%c", 'A' + i);
543 gtk_list_store_append (list_store, &iter);
544 gtk_list_store_set (list_store, &iter,
550 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview),
551 GTK_TREE_MODEL(list_store));
553 g_object_unref(list_store);
556 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
558 GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
561 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
563 GTK_SIGNAL_FUNC(preview_custom), dialog);
566 g_signal_connect(GTK_OBJECT(dialog->entry_width),
568 GTK_SIGNAL_FUNC(preview_custom), dialog);
571 g_signal_connect(GTK_OBJECT(dialog->entry_decimals),
573 GTK_SIGNAL_FUNC(preview_custom), dialog);
576 /* Connect the OK button */
577 g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked),
587 /* Set a particular button to be active */
589 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
591 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
593 dialog->active_button = b;
598 /* Set the TREEVIEW list cursor to the item described by FMT */
600 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
605 We do this with a linear search through the model --- hardly
606 efficient, but the list is short ... */
609 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
612 for (success = gtk_tree_model_get_iter_first(model, &iter);
614 success = gtk_tree_model_iter_next(model, &iter))
616 const struct fmt_spec *spec;
620 gtk_tree_model_get_value(model, &iter, 1, &value);
622 spec = g_value_get_pointer(&value);
624 if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
630 path = gtk_tree_model_get_path(model, &iter);
633 gtk_tree_view_set_cursor(treeview, path, 0, 0);
634 gtk_tree_path_free(path);
637 g_warning("Unusual date format: %s\n", fmt_to_string(fmt));
642 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
644 select_treeview_from_format_type(GtkTreeView *treeview,
650 We do this with a linear search through the model --- hardly
651 efficient, but the list is short ... */
654 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
657 for (success = gtk_tree_model_get_iter_first(model, &iter);
659 success = gtk_tree_model_iter_next(model, &iter))
665 gtk_tree_model_get_value(model, &iter, 1, &value);
667 spec = * ((int *) g_value_get_pointer(&value));
669 if ( spec == fmt_type)
673 path = gtk_tree_model_get_path(model, &iter);
676 gtk_tree_view_set_cursor(treeview, path, 0, 0);
677 gtk_tree_path_free(path);
680 g_warning("Unknown custom type %d\n", fmt_type);
684 /* Set up the state of the dialog box to match the variable VAR */
686 var_type_dialog_set_state(struct var_type_dialog *dialog)
688 const struct fmt_spec *write_spec ;
689 GString *str = g_string_new("");
692 g_assert(dialog->pv);
694 /* Populate width and decimals */
695 write_spec = psppire_variable_get_write_spec(dialog->pv);
697 g_string_printf(str, "%d", write_spec->d);
699 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals),
702 g_string_printf(str, "%d", write_spec->w);
704 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width),
707 g_string_free(str, TRUE);
709 /* Populate the radio button states */
710 switch (write_spec->type)
713 var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
714 gtk_widget_show_all(dialog->width_decimals);
717 var_type_dialog_set_active_button(dialog, BUTTON_STRING);
718 gtk_widget_hide(dialog->label_decimals);
719 gtk_widget_hide(dialog->entry_decimals);
722 var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
723 gtk_widget_show_all(dialog->width_decimals);
726 var_type_dialog_set_active_button(dialog, BUTTON_DOT);
727 gtk_widget_show_all(dialog->width_decimals);
730 var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
731 gtk_widget_show_all(dialog->width_decimals);
733 select_treeview_from_format(dialog->dollar_treeview, write_spec);
748 var_type_dialog_set_active_button(dialog, BUTTON_DATE);
749 gtk_widget_hide(dialog->width_decimals);
750 gtk_widget_show(dialog->date_format_list);
751 select_treeview_from_format(dialog->date_format_treeview, write_spec);
758 var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
759 select_treeview_from_format_type(dialog->custom_treeview,
761 gtk_widget_show_all(dialog->width_decimals);
764 gtk_widget_show_all(dialog->width_decimals);
770 /* Popup the dialog box */
772 var_type_dialog_show(struct var_type_dialog *dialog)
774 var_type_dialog_set_state(dialog);
776 gtk_widget_show(dialog->window);
779 /* Fills F with an output format specification with type TYPE, width
780 W, and D decimals. Iff it's a valid format, then return true.
783 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
788 return check_output_specifier (f, true);
794 /* Callbacks for the Variable Type Dialog Box */
796 /* Callback for when the var type dialog is closed using the OK button.
797 It sets the appropriate variable accordingly. */
799 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
801 struct var_type_dialog *dialog = data;
804 g_assert(dialog->pv);
807 gint width = atoi(gtk_entry_get_text
808 (GTK_ENTRY(dialog->entry_width)));
810 gint decimals = atoi(gtk_entry_get_text
811 (GTK_ENTRY(dialog->entry_decimals)));
813 gint new_type = NUMERIC;
816 struct fmt_spec spec;
817 switch (dialog->active_button)
822 result = make_output_format_try(&spec, FMT_A, width, 0);
825 result = make_output_format_try(&spec, FMT_F, width, decimals);
828 result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
831 result = make_output_format_try(&spec, FMT_DOT, width, decimals);
833 case BUTTON_SCIENTIFIC:
834 result = make_output_format_try(&spec, FMT_E, width, decimals);
838 g_assert(check_output_specifier(&dialog->fmt_l, TRUE));
839 result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
842 result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
845 g_print("Unknown variable type: %d\n", dialog->active_button) ;
850 if ( result == true )
852 psppire_variable_set_type(dialog->pv, new_type);
853 psppire_variable_set_width(dialog->pv, new_width);
854 psppire_variable_set_write_spec(dialog->pv, spec);
855 psppire_variable_set_print_spec(dialog->pv, spec);
859 gtk_widget_hide(dialog->window);
867 on_var_type_cancel_clicked(GtkWidget *w, gpointer data)