2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005, 2006 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 /* This module describes the behaviour of the Variable Type dialog box used
22 for inputing the variable type in the var sheet */
27 #include <glade/glade.h>
32 #include "var-type-dialog.h"
36 #include <data/variable.h>
37 #include <data/settings.h>
38 #include <libpspp/message.h>
43 struct var_type_dialog *dialog;
54 static const struct format_opt format_option[] =
56 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
57 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
58 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
59 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
60 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
61 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
62 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
63 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
64 { "yyddd", {FMT_JDATE, 5, 0} },
65 { "yyyyddd", {FMT_JDATE, 7, 0} },
66 { "q Q yyyy", {FMT_QYR, 8, 0} },
67 { "q Q yy", {FMT_QYR, 6, 0} },
68 { "mmm yyyy", {FMT_MOYR, 8, 0} },
69 { "mmm yy", {FMT_MOYR, 6, 0} },
70 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
71 { "dd WK yy", {FMT_WKYR, 8, 0} },
72 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}}
76 static const struct fmt_spec dollar_format[] =
92 static const int cc_format[] =
102 static void select_treeview_from_format
103 (GtkTreeView *treeview, const struct fmt_spec *fmt);
105 static void select_treeview_from_format_type(GtkTreeView *treeview,
109 /* callback for when any of the radio buttons are toggled */
111 on_toggle_1(GtkToggleButton *togglebutton, gpointer user_data)
113 struct tgs *tgs = user_data;
115 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
118 tgs->dialog->active_button = tgs->button;
121 static void update_width_decimals(const struct var_type_dialog *dialog);
123 #define force_max(x, val) if (x > val) x = val
126 Set the local format from the variable
127 and force them to have sensible values */
129 set_local_width_decimals(struct var_type_dialog *dialog)
131 dialog->fmt_l = * var_get_write_format (dialog->pv);
133 switch (dialog->active_button)
136 force_max( dialog->fmt_l.w, 255);
139 force_max( dialog->fmt_l.w, 40);
140 force_max( dialog->fmt_l.d, 16);
146 /* callback for when any of the radio buttons are toggled */
148 on_toggle_2(GtkToggleButton *togglebutton, gpointer user_data)
150 struct var_type_dialog *dialog = user_data;
151 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
153 switch (dialog->active_button)
156 gtk_widget_hide(dialog->date_format_list);
159 gtk_widget_hide(dialog->custom_currency_hbox);
162 gtk_widget_hide(dialog->dollar_window);
165 gtk_widget_show(dialog->label_decimals);
166 gtk_widget_show(dialog->entry_decimals);
172 set_local_width_decimals(dialog);
173 update_width_decimals(dialog);
175 switch (dialog->active_button)
178 gtk_widget_show(dialog->width_decimals);
179 gtk_widget_hide(dialog->label_decimals);
180 gtk_widget_hide(dialog->entry_decimals);
183 select_treeview_from_format(dialog->date_format_treeview,
184 &format_option[0].spec);
185 gtk_widget_hide(dialog->width_decimals);
186 gtk_widget_show(dialog->date_format_list);
189 select_treeview_from_format(dialog->dollar_treeview,
191 gtk_widget_show(dialog->dollar_window);
192 gtk_widget_show_all(dialog->width_decimals);
195 select_treeview_from_format_type(dialog->custom_treeview,
198 gtk_widget_show(dialog->width_decimals);
199 gtk_widget_show(dialog->custom_currency_hbox);
202 gtk_widget_show_all(dialog->width_decimals);
209 static gint on_var_type_ok_clicked(GtkWidget *w, gpointer data);
214 add_to_group(GtkWidget *w, gpointer data)
216 GtkSizeGroup *sg = data;
218 gtk_size_group_add_widget(sg, w);
221 /* Set the local width and decimals entry boxes to reflec the local format */
223 update_width_decimals(const struct var_type_dialog *dialog)
228 text = g_strdup_printf("%d", dialog->fmt_l.w);
229 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
232 text = g_strdup_printf("%d", dialog->fmt_l.d);
233 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
237 /* Callback for when the custom treeview row is changed.
238 It sets dialog box to reflect the selected format */
240 preview_custom(GtkWidget *w, gpointer data)
244 struct var_type_dialog *dialog = data;
246 if ( dialog->active_button != BUTTON_CUSTOM )
249 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
250 dialog->fmt_l.d = atoi(text);
252 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
253 dialog->fmt_l.w = atoi(text);
256 if ( ! fmt_check_output(&dialog->fmt_l))
258 gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
259 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
267 sample_text = value_to_text(v, dialog->fmt_l);
268 gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
272 sample_text = value_to_text(v, dialog->fmt_l);
273 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
279 /* Callback for when a treeview row is changed.
280 It sets the fmt_l_spec to reflect the selected format */
282 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
284 struct var_type_dialog *dialog = data;
286 GValue the_value = {0};
288 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
290 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
292 gtk_tree_selection_get_selected (sel, &model, &iter);
294 gtk_tree_model_get_value(model, &iter, 1, &the_value);
296 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
300 /* Callback for when a treeview row is changed.
301 It sets the type of the fmt_l to reflect the selected type */
303 set_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
305 static struct fmt_spec custom_format = {0,0,0};
306 struct var_type_dialog *dialog = data;
308 GValue the_value = {0};
310 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
312 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
314 gtk_tree_selection_get_selected (sel, &model, &iter);
316 gtk_tree_model_get_value(model, &iter, 1, &the_value);
318 dialog->fmt_l = custom_format;
319 dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
326 /* Create the structure from the XML definitions */
327 struct var_type_dialog *
328 var_type_dialog_create(GladeXML *xml)
331 struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
335 dialog->window = get_widget_assert(xml,"var_type_dialog");
337 gtk_window_set_transient_for(GTK_WINDOW(dialog->window),
338 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
340 dialog->radioButton[BUTTON_NUMERIC] =
341 get_widget_assert(xml,"radiobutton1");
342 dialog->radioButton[BUTTON_COMMA] =
343 get_widget_assert(xml,"radiobutton2");
344 dialog->radioButton[BUTTON_DOT] =
345 get_widget_assert(xml,"radiobutton3");
346 dialog->radioButton[BUTTON_SCIENTIFIC] =
347 get_widget_assert(xml,"radiobutton4");
348 dialog->radioButton[BUTTON_DATE] =
349 get_widget_assert(xml,"radiobutton5");
350 dialog->radioButton[BUTTON_DOLLAR] =
351 get_widget_assert(xml,"radiobutton6");
352 dialog->radioButton[BUTTON_CUSTOM] =
353 get_widget_assert(xml,"radiobutton7");
354 dialog->radioButton[BUTTON_STRING] =
355 get_widget_assert(xml,"radiobutton8");
358 dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
359 dialog->width_decimals = get_widget_assert(xml, "width_decimals");
360 dialog->label_decimals = get_widget_assert(xml, "decimals_label");
361 dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
363 dialog->label_psample = get_widget_assert(xml, "psample_label");
364 dialog->label_nsample = get_widget_assert(xml, "nsample_label");
367 dialog->entry_width = get_widget_assert(xml,"width_entry");
369 dialog->custom_currency_hbox = get_widget_assert(xml,
370 "custom_currency_hbox");
372 dialog->dollar_window = get_widget_assert(xml, "dollar_window");
373 dialog->dollar_treeview =
374 GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
376 dialog->custom_treeview =
377 GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
380 dialog->ok = get_widget_assert(xml,"var_type_ok");
385 GtkListStore *list_store ;
387 GtkTreeViewColumn *column;
388 GtkCellRenderer *renderer ;
390 static struct tgs tgs[num_BUTTONS];
391 /* The "middle_box" is a vbox with serveral children.
392 However only one child is ever shown at a time.
393 We need to make sure that they all have the same width, to avoid
394 upleasant resizing effects */
395 GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
397 gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")),
398 add_to_group, sizeGroup);
401 for (i = 0 ; i < num_BUTTONS; ++i )
403 tgs[i].dialog = dialog;
405 g_signal_connect(dialog->radioButton[i], "toggled",
406 G_CALLBACK(on_toggle_1), &tgs[i]);
408 g_signal_connect(dialog->radioButton[i], "toggled",
409 G_CALLBACK(on_toggle_2), dialog);
412 /* Populate the date format tree view */
413 dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml,
414 "date_format_list_view"));
416 renderer = gtk_cell_renderer_text_new();
418 column = gtk_tree_view_column_new_with_attributes ("Title",
424 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
428 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
430 for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i )
432 gtk_list_store_append (list_store, &iter);
433 gtk_list_store_set (list_store, &iter,
434 0, format_option[i].desc,
435 1, &format_option[i].spec,
439 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview),
440 GTK_TREE_MODEL(list_store));
442 g_object_unref(list_store);
444 g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
445 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
448 /* populate the dollar treeview */
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->dollar_treeview),
462 list_store = gtk_list_store_new (2, G_TYPE_STRING,
465 for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i )
467 char *template = fmt_dollar_template (&dollar_format[i]);
468 gtk_list_store_append (list_store, &iter);
469 gtk_list_store_set (list_store, &iter,
471 1, &dollar_format[i],
476 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),
477 GTK_TREE_MODEL(list_store));
479 g_object_unref(list_store);
481 g_signal_connect(GTK_OBJECT(dialog->dollar_treeview),
483 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
485 g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview),
487 GTK_SIGNAL_FUNC(update_width_decimals), dialog);
490 /* populate the custom treeview */
492 renderer = gtk_cell_renderer_text_new();
494 column = gtk_tree_view_column_new_with_attributes ("Title",
500 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
504 list_store = gtk_list_store_new (2, G_TYPE_STRING,
507 for ( i = 0 ; i < 5 ; ++i )
509 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
511 g_snprintf(text, 4, "%s", fmt_name (cc_fmts[i]));
512 gtk_list_store_append (list_store, &iter);
513 gtk_list_store_set (list_store, &iter,
519 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview),
520 GTK_TREE_MODEL(list_store));
522 g_object_unref(list_store);
525 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
527 GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
530 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
532 GTK_SIGNAL_FUNC(preview_custom), dialog);
535 g_signal_connect(GTK_OBJECT(dialog->entry_width),
537 GTK_SIGNAL_FUNC(preview_custom), dialog);
540 g_signal_connect(GTK_OBJECT(dialog->entry_decimals),
542 GTK_SIGNAL_FUNC(preview_custom), dialog);
545 /* Connect the OK button */
546 g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked),
556 /* Set a particular button to be active */
558 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
560 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
562 dialog->active_button = b;
567 /* Set the TREEVIEW list cursor to the item described by FMT */
569 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
574 We do this with a linear search through the model --- hardly
575 efficient, but the list is short ... */
578 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
581 for (success = gtk_tree_model_get_iter_first(model, &iter);
583 success = gtk_tree_model_iter_next(model, &iter))
585 const struct fmt_spec *spec;
589 gtk_tree_model_get_value(model, &iter, 1, &value);
591 spec = g_value_get_pointer(&value);
593 if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
599 path = gtk_tree_model_get_path(model, &iter);
602 gtk_tree_view_set_cursor(treeview, path, 0, 0);
603 gtk_tree_path_free(path);
607 char str[FMT_STRING_LEN_MAX + 1];
608 g_warning("Unusual date format: %s\n", fmt_to_string(fmt, str));
613 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
615 select_treeview_from_format_type(GtkTreeView *treeview,
621 We do this with a linear search through the model --- hardly
622 efficient, but the list is short ... */
625 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
628 for (success = gtk_tree_model_get_iter_first(model, &iter);
630 success = gtk_tree_model_iter_next(model, &iter))
636 gtk_tree_model_get_value(model, &iter, 1, &value);
638 spec = * ((int *) g_value_get_pointer(&value));
640 if ( spec == fmt_type)
644 path = gtk_tree_model_get_path(model, &iter);
647 gtk_tree_view_set_cursor(treeview, path, 0, 0);
648 gtk_tree_path_free(path);
651 g_warning("Unknown custom type %d\n", fmt_type);
655 /* Set up the state of the dialog box to match the variable VAR */
657 var_type_dialog_set_state(struct var_type_dialog *dialog)
659 const struct fmt_spec *write_spec ;
660 GString *str = g_string_new("");
663 g_assert(dialog->pv);
665 /* Populate width and decimals */
666 write_spec = var_get_write_format (dialog->pv);
668 g_string_printf(str, "%d", write_spec->d);
670 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals),
673 g_string_printf(str, "%d", write_spec->w);
675 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width),
678 g_string_free(str, TRUE);
680 /* Populate the radio button states */
681 switch (write_spec->type)
684 var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
685 gtk_widget_show_all(dialog->width_decimals);
688 var_type_dialog_set_active_button(dialog, BUTTON_STRING);
689 gtk_widget_hide(dialog->label_decimals);
690 gtk_widget_hide(dialog->entry_decimals);
693 var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
694 gtk_widget_show_all(dialog->width_decimals);
697 var_type_dialog_set_active_button(dialog, BUTTON_DOT);
698 gtk_widget_show_all(dialog->width_decimals);
701 var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
702 gtk_widget_show_all(dialog->width_decimals);
704 select_treeview_from_format(dialog->dollar_treeview, write_spec);
719 var_type_dialog_set_active_button(dialog, BUTTON_DATE);
720 gtk_widget_hide(dialog->width_decimals);
721 gtk_widget_show(dialog->date_format_list);
722 select_treeview_from_format(dialog->date_format_treeview, write_spec);
729 var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
730 select_treeview_from_format_type(dialog->custom_treeview,
732 gtk_widget_show_all(dialog->width_decimals);
735 gtk_widget_show_all(dialog->width_decimals);
741 /* Popup the dialog box */
743 var_type_dialog_show(struct var_type_dialog *dialog)
745 var_type_dialog_set_state(dialog);
747 gtk_widget_show(dialog->window);
750 /* Fills F with an output format specification with type TYPE, width
751 W, and D decimals. Iff it's a valid format, then return true.
754 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
759 return fmt_check_output (f);
765 /* Callbacks for the Variable Type Dialog Box */
767 /* Callback for when the var type dialog is closed using the OK button.
768 It sets the appropriate variable accordingly. */
770 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
772 struct var_type_dialog *dialog = data;
775 g_assert(dialog->pv);
778 gint width = atoi(gtk_entry_get_text
779 (GTK_ENTRY(dialog->entry_width)));
781 gint decimals = atoi(gtk_entry_get_text
782 (GTK_ENTRY(dialog->entry_decimals)));
784 gint new_type = VAR_NUMERIC;
787 struct fmt_spec spec;
788 switch (dialog->active_button)
791 new_type = VAR_STRING;
793 result = make_output_format_try(&spec, FMT_A, width, 0);
796 result = make_output_format_try(&spec, FMT_F, width, decimals);
799 result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
802 result = make_output_format_try(&spec, FMT_DOT, width, decimals);
804 case BUTTON_SCIENTIFIC:
805 result = make_output_format_try(&spec, FMT_E, width, decimals);
809 g_assert(fmt_check_output(&dialog->fmt_l));
810 result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
813 result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
816 g_print("Unknown variable type: %d\n", dialog->active_button) ;
821 if ( result == true )
823 var_set_width (dialog->pv, new_width);
824 var_set_both_formats (dialog->pv, &spec);
828 gtk_widget_hide(dialog->window);
836 on_var_type_cancel_clicked(GtkWidget *w, gpointer data)