2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005, 2006 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>
40 #include <libpspp/message.h>
45 struct var_type_dialog *dialog;
56 static const struct format_opt format_option[] =
58 { "dd-mmm-yyyy", {FMT_DATE, 11, 0} },
59 { "dd-mmm-yy", {FMT_DATE, 9, 0} },
60 { "mm/dd/yyyy", {FMT_ADATE, 10, 0} },
61 { "mm/dd/yy", {FMT_ADATE, 8, 0} },
62 { "dd.mm.yyyy", {FMT_EDATE, 10, 0} },
63 { "dd.mm.yy", {FMT_EDATE, 8, 0} },
64 { "yyyy/mm/dd", {FMT_SDATE, 10, 0} },
65 { "yy/mm/dd", {FMT_SDATE, 8, 0} },
66 { "yyddd", {FMT_JDATE, 5, 0} },
67 { "yyyyddd", {FMT_JDATE, 7, 0} },
68 { "q Q yyyy", {FMT_QYR, 8, 0} },
69 { "q Q yy", {FMT_QYR, 6, 0} },
70 { "mmm yyyy", {FMT_MOYR, 8, 0} },
71 { "mmm yy", {FMT_MOYR, 6, 0} },
72 { "dd WK yyyy", {FMT_WKYR, 10, 0} },
73 { "dd WK yy", {FMT_WKYR, 8, 0} },
74 { "dd-mmm-yyyy HH:MM", {FMT_DATETIME, 17, 0}}
78 static const struct fmt_spec dollar_format[] =
94 static const int cc_format[] =
104 static void select_treeview_from_format
105 (GtkTreeView *treeview, const struct fmt_spec *fmt);
107 static void select_treeview_from_format_type(GtkTreeView *treeview,
111 /* callback for when any of the radio buttons are toggled */
113 on_toggle_1(GtkToggleButton *togglebutton, gpointer user_data)
115 struct tgs *tgs = user_data;
117 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
120 tgs->dialog->active_button = tgs->button;
123 static void update_width_decimals(const struct var_type_dialog *dialog);
125 #define force_max(x, val) if (x > val) x = val
128 Set the local format from the variable
129 and force them to have sensible values */
131 set_local_width_decimals(struct var_type_dialog *dialog)
133 dialog->fmt_l = * psppire_variable_get_write_spec(dialog->pv);
135 switch (dialog->active_button)
138 force_max( dialog->fmt_l.w, 255);
141 force_max( dialog->fmt_l.w, 40);
142 force_max( dialog->fmt_l.d, 16);
148 /* callback for when any of the radio buttons are toggled */
150 on_toggle_2(GtkToggleButton *togglebutton, gpointer user_data)
152 struct var_type_dialog *dialog = user_data;
153 if ( gtk_toggle_button_get_active(togglebutton) == FALSE)
155 switch (dialog->active_button)
158 gtk_widget_hide(dialog->date_format_list);
161 gtk_widget_hide(dialog->custom_currency_hbox);
164 gtk_widget_hide(dialog->dollar_window);
167 gtk_widget_show(dialog->label_decimals);
168 gtk_widget_show(dialog->entry_decimals);
174 set_local_width_decimals(dialog);
175 update_width_decimals(dialog);
177 switch (dialog->active_button)
180 gtk_widget_show(dialog->width_decimals);
181 gtk_widget_hide(dialog->label_decimals);
182 gtk_widget_hide(dialog->entry_decimals);
185 select_treeview_from_format(dialog->date_format_treeview,
186 &format_option[0].spec);
187 gtk_widget_hide(dialog->width_decimals);
188 gtk_widget_show(dialog->date_format_list);
191 select_treeview_from_format(dialog->dollar_treeview,
193 gtk_widget_show(dialog->dollar_window);
194 gtk_widget_show_all(dialog->width_decimals);
197 select_treeview_from_format_type(dialog->custom_treeview,
200 gtk_widget_show(dialog->width_decimals);
201 gtk_widget_show(dialog->custom_currency_hbox);
204 gtk_widget_show_all(dialog->width_decimals);
211 static gint on_var_type_ok_clicked(GtkWidget *w, gpointer data);
216 add_to_group(GtkWidget *w, gpointer data)
218 GtkSizeGroup *sg = data;
220 gtk_size_group_add_widget(sg, w);
223 /* Set the local width and decimals entry boxes to reflec the local format */
225 update_width_decimals(const struct var_type_dialog *dialog)
230 text = g_strdup_printf("%d", dialog->fmt_l.w);
231 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
234 text = g_strdup_printf("%d", dialog->fmt_l.d);
235 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), text);
239 /* Callback for when the custom treeview row is changed.
240 It sets dialog box to reflect the selected format */
242 preview_custom(GtkWidget *w, gpointer data)
246 struct var_type_dialog *dialog = data;
248 if ( dialog->active_button != BUTTON_CUSTOM )
251 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
252 dialog->fmt_l.d = atoi(text);
254 text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
255 dialog->fmt_l.w = atoi(text);
258 if ( ! fmt_check_output(&dialog->fmt_l))
260 gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
261 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
269 sample_text = value_to_text(v, dialog->fmt_l);
270 gtk_label_set_text(GTK_LABEL(dialog->label_psample), sample_text);
274 sample_text = value_to_text(v, dialog->fmt_l);
275 gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
281 /* Callback for when a treeview row is changed.
282 It sets the fmt_l_spec to reflect the selected format */
284 set_format_from_treeview(GtkTreeView *treeview, gpointer data)
286 struct var_type_dialog *dialog = data;
288 GValue the_value = {0};
290 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
292 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
294 gtk_tree_selection_get_selected (sel, &model, &iter);
296 gtk_tree_model_get_value(model, &iter, 1, &the_value);
298 dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer(&the_value);
302 /* Callback for when a treeview row is changed.
303 It sets the type of the fmt_l to reflect the selected type */
305 set_format_type_from_treeview(GtkTreeView *treeview, gpointer data)
307 static struct fmt_spec custom_format = {0,0,0};
308 struct var_type_dialog *dialog = data;
310 GValue the_value = {0};
312 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
314 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
316 gtk_tree_selection_get_selected (sel, &model, &iter);
318 gtk_tree_model_get_value(model, &iter, 1, &the_value);
320 dialog->fmt_l = custom_format;
321 dialog->fmt_l.type = *(int*) g_value_get_pointer(&the_value);
328 /* Create the structure from the XML definitions */
329 struct var_type_dialog *
330 var_type_dialog_create(GladeXML *xml)
333 struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
337 dialog->window = get_widget_assert(xml,"var_type_dialog");
339 gtk_window_set_transient_for(GTK_WINDOW(dialog->window),
340 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
342 dialog->radioButton[BUTTON_NUMERIC] =
343 get_widget_assert(xml,"radiobutton1");
344 dialog->radioButton[BUTTON_COMMA] =
345 get_widget_assert(xml,"radiobutton2");
346 dialog->radioButton[BUTTON_DOT] =
347 get_widget_assert(xml,"radiobutton3");
348 dialog->radioButton[BUTTON_SCIENTIFIC] =
349 get_widget_assert(xml,"radiobutton4");
350 dialog->radioButton[BUTTON_DATE] =
351 get_widget_assert(xml,"radiobutton5");
352 dialog->radioButton[BUTTON_DOLLAR] =
353 get_widget_assert(xml,"radiobutton6");
354 dialog->radioButton[BUTTON_CUSTOM] =
355 get_widget_assert(xml,"radiobutton7");
356 dialog->radioButton[BUTTON_STRING] =
357 get_widget_assert(xml,"radiobutton8");
360 dialog->date_format_list = get_widget_assert(xml, "scrolledwindow4");
361 dialog->width_decimals = get_widget_assert(xml, "width_decimals");
362 dialog->label_decimals = get_widget_assert(xml, "decimals_label");
363 dialog->entry_decimals = get_widget_assert(xml, "decimals_entry");
365 dialog->label_psample = get_widget_assert(xml, "psample_label");
366 dialog->label_nsample = get_widget_assert(xml, "nsample_label");
369 dialog->entry_width = get_widget_assert(xml,"width_entry");
371 dialog->custom_currency_hbox = get_widget_assert(xml,
372 "custom_currency_hbox");
374 dialog->dollar_window = get_widget_assert(xml, "dollar_window");
375 dialog->dollar_treeview =
376 GTK_TREE_VIEW(get_widget_assert(xml, "dollar_treeview"));
378 dialog->custom_treeview =
379 GTK_TREE_VIEW(get_widget_assert(xml, "custom_treeview"));
382 dialog->ok = get_widget_assert(xml,"var_type_ok");
387 GtkListStore *list_store ;
389 GtkTreeViewColumn *column;
390 GtkCellRenderer *renderer ;
392 static struct tgs tgs[num_BUTTONS];
393 /* The "middle_box" is a vbox with serveral children.
394 However only one child is ever shown at a time.
395 We need to make sure that they all have the same width, to avoid
396 upleasant resizing effects */
397 GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
399 gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")),
400 add_to_group, sizeGroup);
403 for (i = 0 ; i < num_BUTTONS; ++i )
405 tgs[i].dialog = dialog;
407 g_signal_connect(dialog->radioButton[i], "toggled",
408 G_CALLBACK(on_toggle_1), &tgs[i]);
410 g_signal_connect(dialog->radioButton[i], "toggled",
411 G_CALLBACK(on_toggle_2), dialog);
414 /* Populate the date format tree view */
415 dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml,
416 "date_format_list_view"));
418 renderer = gtk_cell_renderer_text_new();
420 column = gtk_tree_view_column_new_with_attributes ("Title",
426 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->date_format_treeview),
430 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
432 for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i )
434 gtk_list_store_append (list_store, &iter);
435 gtk_list_store_set (list_store, &iter,
436 0, format_option[i].desc,
437 1, &format_option[i].spec,
441 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->date_format_treeview),
442 GTK_TREE_MODEL(list_store));
444 g_object_unref(list_store);
446 g_signal_connect(GTK_OBJECT(dialog->date_format_treeview), "cursor-changed",
447 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
450 /* populate the dollar treeview */
452 renderer = gtk_cell_renderer_text_new();
454 column = gtk_tree_view_column_new_with_attributes ("Title",
460 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->dollar_treeview),
464 list_store = gtk_list_store_new (2, G_TYPE_STRING,
467 for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i )
469 char *template = fmt_dollar_template (&dollar_format[i]);
470 gtk_list_store_append (list_store, &iter);
471 gtk_list_store_set (list_store, &iter,
473 1, &dollar_format[i],
478 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview),
479 GTK_TREE_MODEL(list_store));
481 g_object_unref(list_store);
483 g_signal_connect(GTK_OBJECT(dialog->dollar_treeview),
485 GTK_SIGNAL_FUNC(set_format_from_treeview), dialog);
487 g_signal_connect_swapped(GTK_OBJECT(dialog->dollar_treeview),
489 GTK_SIGNAL_FUNC(update_width_decimals), dialog);
492 /* populate the custom treeview */
494 renderer = gtk_cell_renderer_text_new();
496 column = gtk_tree_view_column_new_with_attributes ("Title",
502 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->custom_treeview),
506 list_store = gtk_list_store_new (2, G_TYPE_STRING,
509 for ( i = 0 ; i < 5 ; ++i )
511 enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
513 g_snprintf(text, 4, "%s", fmt_name (cc_fmts[i]));
514 gtk_list_store_append (list_store, &iter);
515 gtk_list_store_set (list_store, &iter,
521 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->custom_treeview),
522 GTK_TREE_MODEL(list_store));
524 g_object_unref(list_store);
527 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
529 GTK_SIGNAL_FUNC(set_format_type_from_treeview), dialog);
532 g_signal_connect(GTK_OBJECT(dialog->custom_treeview),
534 GTK_SIGNAL_FUNC(preview_custom), dialog);
537 g_signal_connect(GTK_OBJECT(dialog->entry_width),
539 GTK_SIGNAL_FUNC(preview_custom), dialog);
542 g_signal_connect(GTK_OBJECT(dialog->entry_decimals),
544 GTK_SIGNAL_FUNC(preview_custom), dialog);
547 /* Connect the OK button */
548 g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked),
558 /* Set a particular button to be active */
560 var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
562 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->radioButton[b]),
564 dialog->active_button = b;
569 /* Set the TREEVIEW list cursor to the item described by FMT */
571 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
576 We do this with a linear search through the model --- hardly
577 efficient, but the list is short ... */
580 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
583 for (success = gtk_tree_model_get_iter_first(model, &iter);
585 success = gtk_tree_model_iter_next(model, &iter))
587 const struct fmt_spec *spec;
591 gtk_tree_model_get_value(model, &iter, 1, &value);
593 spec = g_value_get_pointer(&value);
595 if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
601 path = gtk_tree_model_get_path(model, &iter);
604 gtk_tree_view_set_cursor(treeview, path, 0, 0);
605 gtk_tree_path_free(path);
609 char str[FMT_STRING_LEN_MAX + 1];
610 g_warning("Unusual date format: %s\n", fmt_to_string(fmt, str));
615 /* Set the TREEVIEW list cursor to the item described by FMT_TYPE */
617 select_treeview_from_format_type(GtkTreeView *treeview,
623 We do this with a linear search through the model --- hardly
624 efficient, but the list is short ... */
627 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
630 for (success = gtk_tree_model_get_iter_first(model, &iter);
632 success = gtk_tree_model_iter_next(model, &iter))
638 gtk_tree_model_get_value(model, &iter, 1, &value);
640 spec = * ((int *) g_value_get_pointer(&value));
642 if ( spec == fmt_type)
646 path = gtk_tree_model_get_path(model, &iter);
649 gtk_tree_view_set_cursor(treeview, path, 0, 0);
650 gtk_tree_path_free(path);
653 g_warning("Unknown custom type %d\n", fmt_type);
657 /* Set up the state of the dialog box to match the variable VAR */
659 var_type_dialog_set_state(struct var_type_dialog *dialog)
661 const struct fmt_spec *write_spec ;
662 GString *str = g_string_new("");
665 g_assert(dialog->pv);
667 /* Populate width and decimals */
668 write_spec = psppire_variable_get_write_spec(dialog->pv);
670 g_string_printf(str, "%d", write_spec->d);
672 gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals),
675 g_string_printf(str, "%d", write_spec->w);
677 gtk_entry_set_text(GTK_ENTRY(dialog->entry_width),
680 g_string_free(str, TRUE);
682 /* Populate the radio button states */
683 switch (write_spec->type)
686 var_type_dialog_set_active_button(dialog, BUTTON_NUMERIC);
687 gtk_widget_show_all(dialog->width_decimals);
690 var_type_dialog_set_active_button(dialog, BUTTON_STRING);
691 gtk_widget_hide(dialog->label_decimals);
692 gtk_widget_hide(dialog->entry_decimals);
695 var_type_dialog_set_active_button(dialog, BUTTON_COMMA);
696 gtk_widget_show_all(dialog->width_decimals);
699 var_type_dialog_set_active_button(dialog, BUTTON_DOT);
700 gtk_widget_show_all(dialog->width_decimals);
703 var_type_dialog_set_active_button(dialog, BUTTON_DOLLAR);
704 gtk_widget_show_all(dialog->width_decimals);
706 select_treeview_from_format(dialog->dollar_treeview, write_spec);
721 var_type_dialog_set_active_button(dialog, BUTTON_DATE);
722 gtk_widget_hide(dialog->width_decimals);
723 gtk_widget_show(dialog->date_format_list);
724 select_treeview_from_format(dialog->date_format_treeview, write_spec);
731 var_type_dialog_set_active_button(dialog, BUTTON_CUSTOM);
732 select_treeview_from_format_type(dialog->custom_treeview,
734 gtk_widget_show_all(dialog->width_decimals);
737 gtk_widget_show_all(dialog->width_decimals);
743 /* Popup the dialog box */
745 var_type_dialog_show(struct var_type_dialog *dialog)
747 var_type_dialog_set_state(dialog);
749 gtk_widget_show(dialog->window);
752 /* Fills F with an output format specification with type TYPE, width
753 W, and D decimals. Iff it's a valid format, then return true.
756 make_output_format_try (struct fmt_spec *f, int type, int w, int d)
761 return fmt_check_output (f);
767 /* Callbacks for the Variable Type Dialog Box */
769 /* Callback for when the var type dialog is closed using the OK button.
770 It sets the appropriate variable accordingly. */
772 on_var_type_ok_clicked(GtkWidget *w, gpointer data)
774 struct var_type_dialog *dialog = data;
777 g_assert(dialog->pv);
780 gint width = atoi(gtk_entry_get_text
781 (GTK_ENTRY(dialog->entry_width)));
783 gint decimals = atoi(gtk_entry_get_text
784 (GTK_ENTRY(dialog->entry_decimals)));
786 gint new_type = VAR_NUMERIC;
789 struct fmt_spec spec;
790 switch (dialog->active_button)
793 new_type = VAR_STRING;
795 result = make_output_format_try(&spec, FMT_A, width, 0);
798 result = make_output_format_try(&spec, FMT_F, width, decimals);
801 result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
804 result = make_output_format_try(&spec, FMT_DOT, width, decimals);
806 case BUTTON_SCIENTIFIC:
807 result = make_output_format_try(&spec, FMT_E, width, decimals);
811 g_assert(fmt_check_output(&dialog->fmt_l));
812 result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
815 result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
818 g_print("Unknown variable type: %d\n", dialog->active_button) ;
823 if ( result == true )
825 psppire_variable_set_type(dialog->pv, new_type);
826 psppire_variable_set_width(dialog->pv, new_width);
827 psppire_variable_set_write_spec(dialog->pv, spec);
828 psppire_variable_set_print_spec(dialog->pv, spec);
832 gtk_widget_hide(dialog->window);
840 on_var_type_cancel_clicked(GtkWidget *w, gpointer data)