1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * dialog-analysis-tool-chi-squared.c:
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>
28 #include "analysis-chi-squared.h"
29 #include "analysis-tools.h"
32 #include <workbook-control.h>
34 #include <workbook-view.h>
36 #include <parse-util.h>
37 #include <gnm-format.h>
38 #include <dialogs/tool-dialogs.h>
39 #include <dialogs/dao-gui-utils.h>
42 #include <number-match.h>
44 #include <selection.h>
49 #include <widgets/gnm-dao.h>
50 #include <widgets/gnumeric-expr-entry.h>
55 #define CHI_SQUARED_I_KEY "analysistools-chi-square-independence-dialog"
58 GnmGenericToolState base
;
59 GtkWidget
*alpha_entry
;
61 } ChiSquaredIToolState
;
64 * chi_squared_tool_ok_clicked_cb:
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.
73 chi_squared_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget
*button
,
74 ChiSquaredIToolState
*state
)
76 data_analysis_output_t
*dao
;
78 analysis_tools_data_chi_squared_t
*data
;
80 data
= g_new0 (analysis_tools_data_chi_squared_t
, 1);
81 dao
= parse_output ((GnmGenericToolState
*)state
, NULL
);
83 data
->input
= gnm_expr_entry_parse_as_value
84 (GNM_EXPR_ENTRY (state
->base
.input_entry
),
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);
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
,
107 gtk_widget_destroy (state
->base
.dialog
);
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:
125 * Update the dialog widgets sensitivity.
126 * We cannot use tool_update_sensitivity_cb
127 * since we are also considering whether in fact
131 chi_squared_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget
*dummy
,
132 ChiSquaredIToolState
*state
)
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
),
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
);
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
);
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
);
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 "
175 gtk_widget_set_sensitive (state
->base
.ok_button
, FALSE
);
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:
191 * Show the dialog (guru).
195 dialog_chi_square_tool (WBCGtk
*wbcg
, Sheet
*sheet
, gboolean independence
)
197 ChiSquaredIToolState
*state
;
199 char const * plugins
[] = { "Gnumeric_fnstat",
204 if ((wbcg
== NULL
) ||
205 gnm_check_for_plugins_missing (plugins
, wbcg_toplevel (wbcg
)))
208 /* Only pop up one copy per workbook */
209 if (gnm_dialog_raise_if_exists (wbcg
, CHI_SQUARED_I_KEY
))
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 "
220 G_CALLBACK (chi_squared_tool_ok_clicked_cb
),
222 G_CALLBACK (chi_squared_tool_update_sensitivity_cb
),
223 GNM_EE_SINGLE_RANGE
))
230 type
="test-of-independence";
232 type
="test-of-homogeneity";
233 gtk_toggle_button_set_active
234 (GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget (state
->base
.gui
,
238 state
->label
= tool_setup_update
239 (&state
->base
, "labels_button",
240 G_CALLBACK (chi_squared_tool_update_sensitivity_cb
),
243 state
->alpha_entry
= tool_setup_update
244 (&state
->base
, "alpha-entry",
245 G_CALLBACK (chi_squared_tool_update_sensitivity_cb
),
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 ((GnmGenericToolState
*)state
, TRUE
);