1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2015 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "psppire-dialog-action-barchart.h"
21 #include "psppire-value-entry.h"
23 #include "dialog-common.h"
24 #include <ui/syntax-gen.h>
25 #include "psppire-var-view.h"
27 #include "psppire-dialog.h"
28 #include "builder-wrapper.h"
30 #include "psppire-dict.h"
31 #include "libpspp/str.h"
33 #include "language/stats/chart-category.h"
36 psppire_dialog_action_barchart_class_init (PsppireDialogActionBarchartClass *class);
38 G_DEFINE_TYPE (PsppireDialogActionBarchart, psppire_dialog_action_barchart, PSPPIRE_TYPE_DIALOG_ACTION);
41 dialog_state_valid (gpointer rd_)
43 PsppireDialogActionBarchart *rd = PSPPIRE_DIALOG_ACTION_BARCHART (rd_);
45 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->button_summary_func)) )
47 if (0 == g_strcmp0 ("", gtk_entry_get_text (GTK_ENTRY (rd->var))))
51 if (0 == g_strcmp0 ("", gtk_entry_get_text (GTK_ENTRY (rd->variable_xaxis))))
58 refresh (PsppireDialogAction *rd_)
60 PsppireDialogActionBarchart *rd = PSPPIRE_DIALOG_ACTION_BARCHART (rd_);
62 gtk_entry_set_text (GTK_ENTRY (rd->var), "");
63 gtk_entry_set_text (GTK_ENTRY (rd->variable_xaxis), "");
64 gtk_entry_set_text (GTK_ENTRY (rd->variable_cluster), "");
66 /* Set summary_func to true, then let it get unset again.
67 This ensures that the signal handler gets called. */
68 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->button_summary_func), TRUE);
69 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->button_freq_func[0]), TRUE);
71 gtk_widget_set_sensitive (rd->combobox, FALSE);
73 gtk_combo_box_set_active (GTK_COMBO_BOX (rd->combobox), 0);
77 on_summary_toggle (PsppireDialogActionBarchart *act)
79 gboolean status = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (act->button_summary_func));
81 gtk_widget_set_sensitive (act->summary_variables, status);
82 gtk_widget_set_sensitive (act->combobox, status);
86 populate_combo_model (GtkComboBox *cb)
89 GtkListStore *list = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
91 GtkCellRenderer *renderer ;
93 for (i = 0; i < N_AG_FUNCS; ++i)
95 const struct ag_func *af = ag_func + i;
100 gtk_list_store_append (list, &iter);
101 gtk_list_store_set (list, &iter,
107 renderer = gtk_cell_renderer_text_new ();
108 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cb), renderer, FALSE);
110 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cb), renderer, "text", 0);
112 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
113 g_object_unref (list);
118 psppire_dialog_action_barchart_activate (GtkAction *a)
120 PsppireDialogActionBarchart *act = PSPPIRE_DIALOG_ACTION_BARCHART (a);
121 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
123 GHashTable *thing = psppire_dialog_action_get_pointer (pda);
124 GtkBuilder *xml = g_hash_table_lookup (thing, a);
127 xml = builder_new ("barchart.ui");
128 g_hash_table_insert (thing, a, xml);
131 pda->dialog = get_widget_assert (xml, "barchart-dialog");
132 pda->source = get_widget_assert (xml, "dict-view");
134 act->variable_xaxis = get_widget_assert (xml, "entry1");
135 act->variable_cluster = get_widget_assert (xml, "entry3");
136 act->var = get_widget_assert (xml, "entry2");
137 act->button_freq_func[0] = get_widget_assert (xml, "radiobutton-count");
138 act->button_freq_func[1] = get_widget_assert (xml, "radiobutton-percent");
139 act->button_freq_func[2] = get_widget_assert (xml, "radiobutton-cum-count");
140 act->button_freq_func[3] = get_widget_assert (xml, "radiobutton-cum-percent");
142 act->button_summary_func = get_widget_assert (xml, "radiobutton3");
143 act->summary_variables = get_widget_assert (xml, "hbox1");
144 act->combobox = get_widget_assert (xml, "combobox1");
146 populate_combo_model (GTK_COMBO_BOX(act->combobox));
148 g_signal_connect_swapped (act->button_summary_func, "toggled",
149 G_CALLBACK (on_summary_toggle), act);
151 psppire_dialog_action_set_refresh (pda, refresh);
153 psppire_dialog_action_set_valid_predicate (pda,
156 if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_barchart_parent_class)->activate)
157 PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_barchart_parent_class)->activate (pda);
161 generate_syntax (PsppireDialogAction *a)
163 PsppireDialogActionBarchart *rd = PSPPIRE_DIALOG_ACTION_BARCHART (a);
165 const gchar *var_name_xaxis = gtk_entry_get_text (GTK_ENTRY (rd->variable_xaxis));
166 const gchar *var_name_cluster = gtk_entry_get_text (GTK_ENTRY (rd->variable_cluster));
168 GString *string = g_string_new ("GRAPH /BAR = ");
170 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->button_summary_func)))
173 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (rd->combobox), &iter))
176 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (rd->combobox));
177 gtk_tree_model_get_value (model, &iter, 1, &value);
178 g_string_append (string, g_value_get_string (&value));
179 g_value_unset (&value);
181 g_string_append (string, " (");
182 g_string_append (string, gtk_entry_get_text (GTK_ENTRY (rd->var)));
183 g_string_append (string, ")");
188 for (b = 0; b < 4; ++b)
190 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->button_freq_func[b])))
196 g_string_append (string, "COUNT");
199 g_string_append (string, "PCT");
202 g_string_append (string, "CUFREQ");
205 g_string_append (string, "CUPCT");
208 g_assert_not_reached ();
213 g_string_append (string, " BY ");
214 g_string_append (string, var_name_xaxis);
216 if (g_strcmp0 (var_name_cluster, ""))
218 g_string_append (string, " BY ");
219 g_string_append (string, var_name_cluster);
222 g_string_append (string, ".\n");
226 g_string_free (string, FALSE);
232 psppire_dialog_action_barchart_class_init (PsppireDialogActionBarchartClass *class)
234 psppire_dialog_action_set_activation (class, psppire_dialog_action_barchart_activate);
236 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
241 psppire_dialog_action_barchart_init (PsppireDialogActionBarchart *act)