GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / dialogs / dialog-analysis-tool-sign-test.c
blob8b32f49881ee979346b45d658d26c523a9e0c62d
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-analysis-tool-sign-test.c:
5 * Authors:
6 * Andreas J. Guelzow <aguelzow@pyrshep.ca>
8 * (C) Copyright 2009-2010 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-sign-test.h"
29 #include "analysis-signed-rank-test.h"
30 #include "analysis-tools.h"
32 #include <workbook.h>
33 #include <workbook-control.h>
34 #include <wbc-gtk.h>
35 #include <workbook-view.h>
36 #include <gui-util.h>
37 #include <parse-util.h>
38 #include <gnm-format.h>
39 #include <tool-dialogs.h>
40 #include <dao-gui-utils.h>
41 #include <sheet.h>
42 #include <expr.h>
43 #include <number-match.h>
44 #include <ranges.h>
45 #include <selection.h>
46 #include <value.h>
47 #include <commands.h>
48 #include "help.h"
50 #include <widgets/gnm-dao.h>
51 #include <widgets/gnumeric-expr-entry.h>
53 #include <string.h>
54 #include <gtk/gtk.h>
56 #define SIGN_TEST_KEY_ONE "analysistools-sign-test-one-dialog"
57 #define SIGN_TEST_KEY_TWO "analysistools-sign-test-two-dialog"
59 static char const * const grouped_by_group[] = {
60 "grouped_by_row",
61 "grouped_by_col",
62 "grouped_by_area",
63 NULL
66 typedef struct {
67 GenericToolState base;
68 GtkWidget *alpha_entry;
69 GtkWidget *median_entry;
70 } SignTestToolState;
72 /**
73 * sign_test_tool_update_common_sensitivity_cb:
74 * @dummy:
75 * @state:
77 * Update the dialog widgets sensitivity
78 **/
79 static gboolean
80 sign_test_tool_update_common_sensitivity_cb (SignTestToolState *state)
82 gnm_float alpha;
83 gnm_float median;
84 gboolean err;
86 /* Checking Median*/
87 err = entry_to_float
88 (GTK_ENTRY (state->median_entry), &median, FALSE);
89 if (err) {
90 gtk_label_set_text (GTK_LABEL (state->base.warning),
91 _("The predicted median should be a number."));
92 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
93 return FALSE;
96 /* Checking Alpha*/
97 alpha = gtk_spin_button_get_value
98 (GTK_SPIN_BUTTON (state->alpha_entry));
99 if (!(alpha > 0 && alpha < 1)) {
100 gtk_label_set_text (GTK_LABEL (state->base.warning),
101 _("The alpha value should "
102 "be a number between 0 and 1."));
103 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
104 return FALSE;
107 /* Checking Output Page */
108 if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
109 gtk_label_set_text (GTK_LABEL (state->base.warning),
110 _("The output specification "
111 "is invalid."));
112 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
113 return FALSE;
116 return TRUE;
121 * sign_test_tool_update_sensitivity_cb:
122 * @dummy:
123 * @state:
125 * Update the dialog widgets sensitivity
127 static void
128 sign_test_two_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
129 SignTestToolState *state)
131 GnmValue *input_range;
132 gint w, h;
134 /* Checking first input range*/
135 input_range = gnm_expr_entry_parse_as_value
136 (GNM_EXPR_ENTRY (state->base.input_entry),
137 state->base.sheet);
138 if (input_range == NULL || !VALUE_IS_CELLRANGE (input_range)) {
139 gtk_label_set_text (GTK_LABEL (state->base.warning),
140 (state->base.input_entry_2 == NULL)
141 ? _("The input range is invalid.")
142 : _("The first input range is invalid."));
143 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
144 value_release (input_range);
145 return;
146 } else {
147 GnmRange r;
148 range_init_rangeref (&r, &(input_range->v_range.cell));
149 w = range_width (&r);
150 h = range_height (&r);
151 value_release (input_range);
154 /* Checking second input range*/
155 if (state->base.input_entry_2 != NULL) {
156 input_range = gnm_expr_entry_parse_as_value
157 (GNM_EXPR_ENTRY (state->base.input_entry_2),
158 state->base.sheet);
159 if (input_range == NULL || !VALUE_IS_CELLRANGE (input_range)) {
160 gtk_label_set_text (GTK_LABEL (state->base.warning),
161 _("The second input range is invalid."));
162 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
163 value_release (input_range);
164 return;
165 } else {
166 GnmRange r;
167 range_init_rangeref (&r, &(input_range->v_range.cell));
168 value_release (input_range);
169 if (w != range_width (&r) ||
170 h != range_height (&r)) {
171 gtk_label_set_text
172 (GTK_LABEL (state->base.warning),
173 _("The input ranges do not have the same shape."));
174 gtk_widget_set_sensitive
175 (state->base.ok_button, FALSE);
176 return;
182 if (sign_test_tool_update_common_sensitivity_cb (state)) {
183 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
184 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
188 static void
189 sign_test_two_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
190 SignTestToolState *state)
192 data_analysis_output_t *dao;
193 GtkWidget *w;
194 analysis_tools_data_sign_test_two_t *data;
195 analysis_tool_engine engine;
197 data = g_new0 (analysis_tools_data_sign_test_two_t, 1);
198 dao = parse_output ((GenericToolState *)state, NULL);
200 data->base.range_1 = gnm_expr_entry_parse_as_value
201 (GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
203 data->base.range_2 = gnm_expr_entry_parse_as_value
204 (GNM_EXPR_ENTRY (state->base.input_entry_2), state->base.sheet);
206 w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
207 data->base.labels = gtk_toggle_button_get_active
208 (GTK_TOGGLE_BUTTON (w));
210 entry_to_float
211 (GTK_ENTRY (state->median_entry), &data->median, FALSE);
213 data->base.alpha = gtk_spin_button_get_value
214 (GTK_SPIN_BUTTON (state->alpha_entry));
216 w = go_gtk_builder_get_widget (state->base.gui, "signtest");
217 engine = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w))
218 ? analysis_tool_sign_test_two_engine
219 : analysis_tool_signed_rank_test_two_engine;
221 if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
222 state->base.sheet,
223 dao, data, engine, TRUE))
224 gtk_widget_destroy (state->base.dialog);
226 return;
230 * dialog_sign_test_two_tool:
234 dialog_sign_test_two_tool (WBCGtk *wbcg, Sheet *sheet, signtest_type type)
236 char const * plugins[] = { "Gnumeric_fnstat",
237 "Gnumeric_fnlogical",
238 "Gnumeric_fnmath",
239 "Gnumeric_fninfo",
240 NULL};
241 SignTestToolState *state;
242 GtkWidget *w;
244 if ((wbcg == NULL) ||
245 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
246 return 1;
248 /* Only pop up one copy per workbook */
249 if (gnm_dialog_raise_if_exists (wbcg, SIGN_TEST_KEY_TWO))
250 return 0;
252 state = g_new0 (SignTestToolState, 1);
254 if (dialog_tool_init (&state->base, wbcg, sheet,
255 GNUMERIC_HELP_LINK_SIGN_TEST_TWO,
256 "res:ui/sign-test-two.ui", "Sign-Test",
257 _("Could not create the Sign Test Tool dialog."),
258 SIGN_TEST_KEY_TWO,
259 G_CALLBACK (sign_test_two_tool_ok_clicked_cb),
260 NULL,
261 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
262 GNM_EE_SINGLE_RANGE))
264 g_free(state);
265 return 0;
269 state->alpha_entry = tool_setup_update
270 (&state->base, "alpha-entry",
271 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
272 state);
273 float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
275 state->median_entry = tool_setup_update
276 (&state->base, "median-entry",
277 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
278 state);
279 int_to_entry (GTK_ENTRY (state->median_entry), 0);
281 w = go_gtk_builder_get_widget (state->base.gui,
282 (type == SIGNTEST) ? "signtest"
283 : "signedranktest");
284 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
286 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
287 sign_test_two_tool_update_sensitivity_cb (NULL, state);
288 tool_load_selection ((GenericToolState *)state, TRUE);
290 return 0;
294 /************************************************************************************/
297 * sign_test_tool_ok_clicked_cb:
298 * @button:
299 * @state:
301 * Retrieve the information from the dialog and call the sign_test_tool.
302 * Note that we assume that the ok_button is only active if the entry fields
303 * contain sensible data.
305 static void
306 sign_test_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
307 SignTestToolState *state)
309 data_analysis_output_t *dao;
310 GtkWidget *w;
311 analysis_tools_data_sign_test_t *data;
312 analysis_tool_engine engine;
314 data = g_new0 (analysis_tools_data_sign_test_t, 1);
315 dao = parse_output ((GenericToolState *)state, NULL);
317 data->base.input = gnm_expr_entry_parse_as_list (
318 GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
319 data->base.group_by = gnm_gui_group_value (state->base.gui, grouped_by_group);
321 w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
322 data->base.labels = gtk_toggle_button_get_active
323 (GTK_TOGGLE_BUTTON (w));
325 entry_to_float
326 (GTK_ENTRY (state->median_entry), &data->median, FALSE);
327 data->alpha = gtk_spin_button_get_value
328 (GTK_SPIN_BUTTON (state->alpha_entry));
330 w = go_gtk_builder_get_widget (state->base.gui, "signtest");
331 engine = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w))
332 ? analysis_tool_sign_test_engine
333 : analysis_tool_signed_rank_test_engine;
335 if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
336 state->base.sheet,
337 dao, data, engine, TRUE))
338 gtk_widget_destroy (state->base.dialog);
340 return;
344 * sign_test_tool_update_sensitivity_cb:
345 * @dummy:
346 * @state:
348 * Update the dialog widgets sensitivity
350 static void
351 sign_test_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
352 SignTestToolState *state)
354 GSList *input_range;
356 /* Checking first input range*/
357 input_range = gnm_expr_entry_parse_as_list
358 (GNM_EXPR_ENTRY (state->base.input_entry),
359 state->base.sheet);
360 if (input_range == NULL) {
361 gtk_label_set_text (GTK_LABEL (state->base.warning),
362 (state->base.input_entry_2 == NULL)
363 ? _("The input range is invalid.")
364 : _("The first input range is invalid."));
365 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
366 return;
367 } else
368 range_list_destroy (input_range);
370 if (sign_test_tool_update_common_sensitivity_cb (state)) {
371 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
372 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
378 * dialog_sign_test_tool:
382 dialog_sign_test_tool (WBCGtk *wbcg, Sheet *sheet, signtest_type type)
384 char const * plugins[] = { "Gnumeric_fnstat",
385 "Gnumeric_fnlogical",
386 "Gnumeric_fnmath",
387 "Gnumeric_fninfo",
388 NULL};
389 SignTestToolState *state;
390 GtkWidget *w;
392 if ((wbcg == NULL) ||
393 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
394 return 1;
396 /* Only pop up one copy per workbook */
397 if (gnm_dialog_raise_if_exists (wbcg, SIGN_TEST_KEY_ONE))
398 return 0;
400 state = g_new0 (SignTestToolState, 1);
402 if (dialog_tool_init (&state->base, wbcg, sheet,
403 GNUMERIC_HELP_LINK_SIGN_TEST,
404 "res:ui/sign-test.ui", "Sign-Test",
405 _("Could not create the Sign Test Tool dialog."),
406 SIGN_TEST_KEY_ONE,
407 G_CALLBACK (sign_test_tool_ok_clicked_cb),
408 NULL,
409 G_CALLBACK (sign_test_tool_update_sensitivity_cb),
412 g_free(state);
413 return 0;
417 state->alpha_entry = tool_setup_update
418 (&state->base, "alpha-entry",
419 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
420 state);
421 state->median_entry = tool_setup_update
422 (&state->base, "median-entry",
423 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
424 state);
426 int_to_entry (GTK_ENTRY (state->median_entry), 0);
427 float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
429 w = go_gtk_builder_get_widget (state->base.gui,
430 (type == SIGNTEST) ? "signtest"
431 : "signedranktest");
432 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
434 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
435 sign_test_tool_update_sensitivity_cb (NULL, state);
436 tool_load_selection ((GenericToolState *)state, TRUE);
438 return 0;