GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / dialogs / dialog-analysis-tool-chi-squared.c
bloba9d53c43de969cc0ef11a72cde27c8f8f90744ae
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-analysis-tool-chi-squared.c:
5 * Authors:
6 * Andreas J. Guelzow <aguelzow@taliesin.ca>
8 * (C) Copyright 2008 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
26 #include <gnumeric.h>
27 #include "dialogs.h"
28 #include "analysis-chi-squared.h"
29 #include "analysis-tools.h"
31 #include <workbook.h>
32 #include <workbook-control.h>
33 #include <wbc-gtk.h>
34 #include <workbook-view.h>
35 #include <gui-util.h>
36 #include <parse-util.h>
37 #include <gnm-format.h>
38 #include <tool-dialogs.h>
39 #include <dao-gui-utils.h>
40 #include <sheet.h>
41 #include <expr.h>
42 #include <number-match.h>
43 #include <ranges.h>
44 #include <selection.h>
45 #include <value.h>
46 #include <commands.h>
47 #include "help.h"
49 #include <widgets/gnm-dao.h>
50 #include <widgets/gnumeric-expr-entry.h>
52 #include <string.h>
53 #include <gtk/gtk.h>
55 #define CHI_SQUARED_I_KEY "analysistools-chi-square-independence-dialog"
57 typedef struct {
58 GenericToolState base;
59 GtkWidget *alpha_entry;
60 GtkWidget *label;
61 } ChiSquaredIToolState;
63 /**
64 * chi_squared_tool_ok_clicked_cb:
65 * @button:
66 * @state:
68 * Retrieve the information from the dialog and call the fourier_tool.
69 * Note that we assume that the ok_button is only active if the entry fields
70 * contain sensible data.
71 **/
72 static void
73 chi_squared_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
74 ChiSquaredIToolState *state)
76 data_analysis_output_t *dao;
77 GtkWidget *w;
78 analysis_tools_data_chi_squared_t *data;
80 data = g_new0 (analysis_tools_data_chi_squared_t, 1);
81 dao = parse_output ((GenericToolState *)state, NULL);
83 data->input = gnm_expr_entry_parse_as_value
84 (GNM_EXPR_ENTRY (state->base.input_entry),
85 state->base.sheet);
87 data->wbc = GNM_WBC (state->base.wbcg);
89 data->labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->label));
91 data->alpha = gtk_spin_button_get_value
92 (GTK_SPIN_BUTTON (state->alpha_entry));
94 w = go_gtk_builder_get_widget (state->base.gui, "test-of-independence");
95 data->independence = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
97 data->n_c = (data->input->v_range.cell.b.col - data->input->v_range.cell.a.col + 1);
98 data->n_r = (data->input->v_range.cell.b.row - data->input->v_range.cell.a.row + 1);
100 if (data->labels)
101 data->n_c--, data->n_r--;
104 if (!cmd_analysis_tool (data->wbc, state->base.sheet,
105 dao, data, analysis_tool_chi_squared_engine,
106 TRUE))
107 gtk_widget_destroy (state->base.dialog);
109 return;
112 static int
113 calc_min_size (GnmValue *input_range)
115 int row = (input_range->v_range.cell.b.row - input_range->v_range.cell.a.row + 1);
116 int col = (input_range->v_range.cell.b.col - input_range->v_range.cell.a.col + 1);
118 return ((row < col) ? row : col);
122 * chi_squared_tool_update_sensitivity_cb:
123 * @state:
125 * Update the dialog widgets sensitivity.
126 * We cannot use tool_update_sensitivity_cb
127 * since we are also considering whether in fact
128 * an alpha is given.
130 static void
131 chi_squared_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
132 ChiSquaredIToolState *state)
134 gnm_float alpha;
135 GnmValue *input_range;
137 /* Checking Input Range */
138 input_range = gnm_expr_entry_parse_as_value
139 (GNM_EXPR_ENTRY (state->base.input_entry),
140 state->base.sheet);
141 if (input_range == NULL) {
142 gtk_label_set_text (GTK_LABEL (state->base.warning),
143 _("The input range is invalid."));
144 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
145 return;
146 } else {
147 int min_size = calc_min_size (input_range);
148 gboolean label = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->label));
149 value_release (input_range);
151 if (min_size < (label ? 3 : 2)) {
152 gtk_label_set_text (GTK_LABEL (state->base.warning),
153 _("The input range is too small."));
154 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
155 return;
159 /* Checking Alpha*/
160 alpha = gtk_spin_button_get_value
161 (GTK_SPIN_BUTTON (state->alpha_entry));
162 if (!(alpha > 0 && alpha < 1)) {
163 gtk_label_set_text (GTK_LABEL (state->base.warning),
164 _("The alpha value should "
165 "be a number between 0 and 1."));
166 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
167 return;
170 /* Checking Output Page */
171 if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
172 gtk_label_set_text (GTK_LABEL (state->base.warning),
173 _("The output specification "
174 "is invalid."));
175 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
176 return;
179 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
180 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
186 * dialog_chi_squared_tool:
187 * @wbcg:
188 * @sheet:
189 * @independence
191 * Show the dialog (guru).
195 dialog_chi_square_tool (WBCGtk *wbcg, Sheet *sheet, gboolean independence)
197 ChiSquaredIToolState *state;
198 char const *type;
199 char const * plugins[] = { "Gnumeric_fnstat",
200 "Gnumeric_fnlookup",
201 "Gnumeric_fnmath",
202 NULL};
204 if ((wbcg == NULL) ||
205 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
206 return 1;
208 /* Only pop up one copy per workbook */
209 if (gnm_dialog_raise_if_exists (wbcg, CHI_SQUARED_I_KEY))
210 return 0;
212 state = g_new0 (ChiSquaredIToolState, 1);
214 if (dialog_tool_init (&state->base, wbcg, sheet,
215 GNUMERIC_HELP_LINK_CHI_SQUARED,
216 "res:ui/chi-squared.ui", "Chi-Squared Tests",
217 _("Could not create the Chi Squared Tests "
218 "tool dialog."),
219 CHI_SQUARED_I_KEY,
220 G_CALLBACK (chi_squared_tool_ok_clicked_cb),
221 NULL,
222 G_CALLBACK (chi_squared_tool_update_sensitivity_cb),
223 GNM_EE_SINGLE_RANGE))
225 g_free(state);
226 return 0;
229 if (independence)
230 type ="test-of-independence";
231 else
232 type ="test-of-homogeneity";
233 gtk_toggle_button_set_active
234 (GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget (state->base.gui,
235 type)),
236 TRUE);
238 state->label = tool_setup_update
239 (&state->base, "labels_button",
240 G_CALLBACK (chi_squared_tool_update_sensitivity_cb),
241 state);
243 state->alpha_entry = tool_setup_update
244 (&state->base, "alpha-entry",
245 G_CALLBACK (chi_squared_tool_update_sensitivity_cb),
246 state);
247 float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
249 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
250 chi_squared_tool_update_sensitivity_cb (NULL, state);
251 tool_load_selection ((GenericToolState *)state, TRUE);
253 return 0;