Compilation: clean up dialog including.
[gnumeric.git] / src / dialogs / dialog-analysis-tool-one-mean.c
blobfcdd72937309146698e35a1eeed430206d6623b2
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-analysis-tool-one-mean.c:
5 * Authors:
6 * Andreas J. Guelzow <aguelzow@pyrshep.ca>
8 * (C) Copyright 2012 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-one-mean-test.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 <dialogs/tool-dialogs.h>
39 #include <dialogs/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 ONE_MEAN_TEST_KEY "analysistools-one-mean-test-dialog"
57 static char const * const grouped_by_group[] = {
58 "grouped_by_row",
59 "grouped_by_col",
60 "grouped_by_area",
61 NULL
64 typedef struct {
65 GnmGenericToolState base;
66 GtkWidget *alpha_entry;
67 GtkWidget *mean_entry;
68 } OneeMeanTestToolState;
70 /**
71 * one_mean_test_tool_update_common_sensitivity_cb:
72 * @dummy:
73 * @state:
75 * Update the dialog widgets sensitivity
76 **/
77 static gboolean
78 one_mean_test_tool_update_common_sensitivity_cb (OneeMeanTestToolState *state)
80 gnm_float alpha;
81 gnm_float mean;
82 gboolean err;
84 /* Checking Mean*/
85 err = entry_to_float
86 (GTK_ENTRY (state->mean_entry), &mean, FALSE);
87 if (err) {
88 gtk_label_set_text (GTK_LABEL (state->base.warning),
89 _("The predicted mean should be a number."));
90 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
91 return FALSE;
94 /* Checking Alpha*/
95 alpha = gtk_spin_button_get_value
96 (GTK_SPIN_BUTTON (state->alpha_entry));
97 if (!(alpha > 0 && alpha < 1)) {
98 gtk_label_set_text (GTK_LABEL (state->base.warning),
99 _("The alpha value should "
100 "be a number between 0 and 1."));
101 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
102 return FALSE;
105 /* Checking Output Page */
106 if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
107 gtk_label_set_text (GTK_LABEL (state->base.warning),
108 _("The output specification "
109 "is invalid."));
110 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
111 return FALSE;
114 return TRUE;
120 /************************************************************************************/
123 * one_mean_test_tool_ok_clicked_cb:
124 * @button:
125 * @state:
127 * Retrieve the information from the dialog and call the one_mean_test_tool.
128 * Note that we assume that the ok_button is only active if the entry fields
129 * contain sensible data.
131 static void
132 one_mean_test_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
133 OneeMeanTestToolState *state)
135 data_analysis_output_t *dao;
136 GtkWidget *w;
137 analysis_tools_data_one_mean_test_t *data;
139 data = g_new0 (analysis_tools_data_one_mean_test_t, 1);
140 dao = parse_output ((GnmGenericToolState *)state, NULL);
142 data->base.input = gnm_expr_entry_parse_as_list (
143 GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
144 data->base.group_by = gnm_gui_group_value (state->base.gui, grouped_by_group);
146 w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
147 data->base.labels = gtk_toggle_button_get_active
148 (GTK_TOGGLE_BUTTON (w));
150 entry_to_float
151 (GTK_ENTRY (state->mean_entry), &data->mean, FALSE);
152 data->alpha = gtk_spin_button_get_value
153 (GTK_SPIN_BUTTON (state->alpha_entry));
155 if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
156 state->base.sheet,
157 dao, data, analysis_tool_one_mean_test_engine,
158 TRUE))
159 gtk_widget_destroy (state->base.dialog);
161 return;
165 * one_mean_test_tool_update_sensitivity_cb:
166 * @dummy:
167 * @state:
169 * Update the dialog widgets sensitivity
171 static void
172 one_mean_test_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
173 OneeMeanTestToolState *state)
175 GSList *input_range;
177 /* Checking first input range*/
178 input_range = gnm_expr_entry_parse_as_list
179 (GNM_EXPR_ENTRY (state->base.input_entry),
180 state->base.sheet);
181 if (input_range == NULL) {
182 gtk_label_set_text (GTK_LABEL (state->base.warning),
183 (state->base.input_entry_2 == NULL)
184 ? _("The input range is invalid.")
185 : _("The first input range is invalid."));
186 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
187 return;
188 } else
189 range_list_destroy (input_range);
191 if (one_mean_test_tool_update_common_sensitivity_cb (state)) {
192 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
193 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
199 * dialog_one_mean_test_tool:
203 dialog_one_mean_test_tool (WBCGtk *wbcg, Sheet *sheet)
205 char const * plugins[] = { "Gnumeric_fnstat",
206 "Gnumeric_fnlogical",
207 "Gnumeric_fnmath",
208 NULL};
209 OneeMeanTestToolState *state;
211 if ((wbcg == NULL) ||
212 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
213 return 1;
215 /* Only pop up one copy per workbook */
216 if (gnm_dialog_raise_if_exists (wbcg, ONE_MEAN_TEST_KEY))
217 return 0;
219 state = g_new0 (OneeMeanTestToolState, 1);
221 if (dialog_tool_init (&state->base, wbcg, sheet,
222 GNUMERIC_HELP_LINK_ONE_MEAN,
223 "res:ui/one-mean-test.ui", "One-Mean-Test",
224 _("Could not create the Student-t Test Tool dialog."),
225 ONE_MEAN_TEST_KEY,
226 G_CALLBACK (one_mean_test_tool_ok_clicked_cb),
227 NULL,
228 G_CALLBACK (one_mean_test_tool_update_sensitivity_cb),
231 g_free(state);
232 return 0;
236 state->alpha_entry = tool_setup_update
237 (&state->base, "alpha-entry",
238 G_CALLBACK (one_mean_test_tool_update_sensitivity_cb),
239 state);
240 state->mean_entry = tool_setup_update
241 (&state->base, "mean-entry",
242 G_CALLBACK (one_mean_test_tool_update_sensitivity_cb),
243 state);
245 int_to_entry (GTK_ENTRY (state->mean_entry), 0);
246 float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
248 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
249 one_mean_test_tool_update_sensitivity_cb (NULL, state);
250 tool_load_selection ((GnmGenericToolState *)state, TRUE);
252 return 0;