Update Spanish translation
[gnumeric.git] / src / dialogs / dialog-analysis-tool-frequency.c
blob5045a9065aeaf379dcfeeda20b724608f5276f8c
1 /*
2 * dialog-analysis-tool-frequency.c:
4 * Authors:
5 * Andreas J. Guelzow <aguelzow@taliesin.ca>
7 * (C) Copyright 2008 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
25 #include <gnumeric.h>
26 #include <dialogs/dialogs.h>
27 #include <tools/analysis-frequency.h>
28 #include <tools/analysis-tools.h>
30 #include <workbook.h>
31 #include <workbook-control.h>
32 #include <wbc-gtk.h>
33 #include <workbook-view.h>
34 #include <gui-util.h>
35 #include <parse-util.h>
36 #include <gnm-format.h>
37 #include <dialogs/tool-dialogs.h>
38 #include <dialogs/dao-gui-utils.h>
39 #include <sheet.h>
40 #include <expr.h>
41 #include <number-match.h>
42 #include <ranges.h>
43 #include <selection.h>
44 #include <value.h>
45 #include <commands.h>
46 #include <dialogs/help.h>
48 #include <widgets/gnm-dao.h>
49 #include <widgets/gnm-expr-entry.h>
51 #include <string.h>
53 #define FREQUENCY_KEY "analysistools-frequency-dialog"
55 static char const * const grouped_by_group[] = {
56 "grouped_by_row",
57 "grouped_by_col",
58 NULL
61 static char const * const chart_group[] = {
62 "nochart-button",
63 "barchart-button",
64 "columnchart-button",
65 NULL
68 typedef struct {
69 GnmGenericToolState base;
70 GtkWidget *predetermined_button;
71 GtkWidget *calculated_button;
72 GtkEntry *n_entry;
73 } FrequencyToolState;
76 /**
77 * frequency_tool_update_sensitivity_cb:
78 * @dummy:
79 * @state:
81 * Update the dialog widgets sensitivity
82 **/
83 static void
84 frequency_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
85 FrequencyToolState *state)
87 int the_n;
88 gboolean predetermined_bins;
89 GSList *input_range;
90 GnmValue *input_range_2 = NULL;
92 input_range = gnm_expr_entry_parse_as_list
93 (GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
94 if (input_range == NULL) {
95 gtk_label_set_text (GTK_LABEL (state->base.warning),
96 _("The input range is invalid."));
97 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
98 return;
101 range_list_destroy (input_range);
103 predetermined_bins = gtk_toggle_button_get_active (
104 GTK_TOGGLE_BUTTON (state->predetermined_button));
105 if (predetermined_bins) {
106 input_range_2 = gnm_expr_entry_parse_as_value
107 (GNM_EXPR_ENTRY (state->base.input_entry_2), state->base.sheet);
108 if (input_range_2 == NULL) {
109 gtk_label_set_text (GTK_LABEL (state->base.warning),
110 _("The categories range is not valid."));
111 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
112 return;
114 value_release (input_range_2);
115 } else if (entry_to_int(state->n_entry, &the_n,FALSE) != 0 || the_n <= 0) {
116 gtk_label_set_text (GTK_LABEL (state->base.warning),
117 _("The number of categories is invalid."));
118 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
119 return;
122 if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
123 gtk_label_set_text (GTK_LABEL (state->base.warning),
124 _("The output specification "
125 "is invalid."));
126 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
127 return;
130 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
131 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
133 return;
137 * frequency_tool_ok_clicked_cb:
138 * @button:
139 * @state:
141 * Retrieve the information from the dialog and call the frequency_tool.
142 * Note that we assume that the ok_button is only active if the entry fields
143 * contain sensible data.
145 static void
146 frequency_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
147 FrequencyToolState *state)
149 data_analysis_output_t *dao;
150 analysis_tools_data_frequency_t *data;
152 GtkWidget *w;
154 data = g_new0 (analysis_tools_data_frequency_t, 1);
155 dao = parse_output ((GnmGenericToolState *)state, NULL);
157 data->base.input = gnm_expr_entry_parse_as_list (
158 GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
159 data->base.group_by = gnm_gui_group_value (state->base.gui, grouped_by_group);
161 data->predetermined = gtk_toggle_button_get_active (
162 GTK_TOGGLE_BUTTON (state->predetermined_button));
163 if (data->predetermined) {
164 w = go_gtk_builder_get_widget (state->base.gui, "labels_2_button");
165 data->bin = gnm_expr_entry_parse_as_value
166 (GNM_EXPR_ENTRY (state->base.input_entry_2),
167 state->base.sheet);
168 } else {
169 entry_to_int(state->n_entry, &data->n,TRUE);
170 data->bin = NULL;
173 data->chart = gnm_gui_group_value (state->base.gui, chart_group);
175 w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
176 data->base.labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
177 w = go_gtk_builder_get_widget (state->base.gui, "percentage-button");
178 data->percentage = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
179 w = go_gtk_builder_get_widget (state->base.gui, "exact-button");
180 data->exact = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
182 if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
183 state->base.sheet,
184 dao, data, analysis_tool_frequency_engine,
185 TRUE))
186 gtk_widget_destroy (state->base.dialog);
188 return;
192 * frequency_tool_set_predetermined:
193 * @widget:
194 * @focus_widget:
195 * @state:
197 * Output range entry was focused. Switch to output range.
200 static gboolean
201 frequency_tool_set_predetermined (G_GNUC_UNUSED GtkWidget *widget,
202 G_GNUC_UNUSED GdkEventFocus *event,
203 FrequencyToolState *state)
205 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->predetermined_button), TRUE);
206 return FALSE;
210 * frequency_tool_set_calculated:
211 * @widget:
212 * @event:
213 * @state:
216 static gboolean
217 frequency_tool_set_calculated (G_GNUC_UNUSED GtkWidget *widget,
218 G_GNUC_UNUSED GdkEventFocus *event,
219 FrequencyToolState *state)
221 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->calculated_button), TRUE);
222 return FALSE;
226 * dialog_frequency_tool:
227 * @wbcg:
228 * @sheet:
230 * Show the dialog (guru).
234 dialog_frequency_tool (WBCGtk *wbcg, Sheet *sheet)
236 FrequencyToolState *state;
237 char const * plugins[] = { "Gnumeric_fnlookup",
238 "Gnumeric_fninfo",
239 "Gnumeric_fnstring",
240 "Gnumeric_fnlogical",
241 NULL};
243 if ((wbcg == NULL) ||
244 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
245 return 1;
247 /* Only pop up one copy per workbook */
248 if (gnm_dialog_raise_if_exists (wbcg, FREQUENCY_KEY))
249 return 0;
251 state = g_new0 (FrequencyToolState, 1);
253 if (dialog_tool_init (&state->base, wbcg, sheet,
254 GNUMERIC_HELP_LINK_FREQUENCY,
255 "res:ui/frequency.ui", "Frequency",
256 _("Could not create the Frequency Tool dialog."),
257 FREQUENCY_KEY,
258 G_CALLBACK (frequency_tool_ok_clicked_cb), NULL,
259 G_CALLBACK (frequency_tool_update_sensitivity_cb),
262 g_free(state);
263 return 0;
266 state->predetermined_button = tool_setup_update
267 (&state->base, "pre_determined_button",
268 G_CALLBACK (frequency_tool_update_sensitivity_cb),
269 state);
271 state->calculated_button = tool_setup_update
272 (&state->base, "calculated_button",
273 G_CALLBACK (frequency_tool_update_sensitivity_cb),
274 state);
276 state->n_entry =
277 GTK_ENTRY(tool_setup_update
278 (&state->base, "n_entry",
279 G_CALLBACK (frequency_tool_update_sensitivity_cb),
280 state));
281 g_signal_connect (G_OBJECT (state->n_entry),
282 "key-press-event",
283 G_CALLBACK (frequency_tool_set_calculated), state);
284 g_signal_connect (G_OBJECT
285 (gnm_expr_entry_get_entry (
286 GNM_EXPR_ENTRY (state->base.input_entry_2))),
287 "focus-in-event",
288 G_CALLBACK (frequency_tool_set_predetermined), state);
290 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
291 frequency_tool_update_sensitivity_cb (NULL, state);
292 tool_load_selection ((GnmGenericToolState *)state, TRUE);
294 gtk_widget_set_sensitive (GTK_WIDGET (state->n_entry), FALSE);
295 gtk_widget_set_sensitive (state->calculated_button, FALSE);
297 return 0;