Compilation: fix up tools includes.
[gnumeric.git] / src / dialogs / dialog-random-generator-cor.c
blob27648bed0d2e4d413ea2dd118cdca4405432a531
1 /*
2 * dialog-random-generator.c:
4 * Authors:
5 * Andreas J. Guelzow <aguelzow@pyrshep.ca>
7 * (C) Copyright 2009 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.h"
27 #include "help.h"
28 #include "dialogs/tool-dialogs.h"
29 #include "tools/random-generator-cor.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/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>
47 #include <widgets/gnumeric-expr-entry.h>
48 #include <widgets/gnm-dao.h>
50 #include <gtk/gtk.h>
51 #include <string.h>
54 /**********************************************/
55 /* Generic guru items */
56 /**********************************************/
59 #define RANDOM_COR_KEY "analysistools-random-cor-dialog"
61 static char const * const matrix_group[] = {
62 "cov-button",
63 "cholesky-button",
64 NULL
67 typedef struct {
68 GnmGenericToolState base;
69 GtkWidget *count_entry;
70 } RandomCorToolState;
73 /**********************************************/
74 /* Begin of random tool code */
75 /**********************************************/
78 /**
79 * random_cor_tool_update_sensitivity_cb:
80 * @dummy:
81 * @state:
83 * Update the dialog widgets sensitivity if the only items of interest
84 * are the standard input (one range) and output items.
85 **/
86 static void
87 random_cor_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
88 RandomCorToolState *state)
90 GnmValue *input_range;
91 gint height, width, count;
93 input_range = gnm_expr_entry_parse_as_value
94 (GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
95 if (input_range == NULL) {
96 gtk_label_set_text (GTK_LABEL (state->base.warning),
97 _("The matrix range is not valid."));
98 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
99 return;
102 height = input_range->v_range.cell.b.row - input_range->v_range.cell.a.row;
103 width = input_range->v_range.cell.b.col - input_range->v_range.cell.a.col;
104 value_release (input_range);
106 if (height != width || height == 0) {
107 gtk_label_set_text (GTK_LABEL (state->base.warning),
108 _("The matrix must be symmetric positive-definite."));
109 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
110 return;
113 if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
114 gtk_label_set_text (GTK_LABEL (state->base.warning),
115 _("The output specification "
116 "is invalid."));
117 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
118 return;
121 if (entry_to_int (GTK_ENTRY (state->count_entry), &count, FALSE) != 0 ||
122 count <= 0) {
123 gtk_label_set_text (GTK_LABEL (state->base.warning),
124 _("The number of random numbers requested is invalid."));
125 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
126 return;
129 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
130 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
134 * random_cor_tool_ok_clicked_cb:
135 * @button:
136 * @state:
138 * Retrieve the information from the dialog and call the appropriate tool.
139 * Note that we assume that the ok_button is only active if the entry fields
140 * contain sensible data.
142 static void
143 random_cor_tool_ok_clicked_cb (GtkWidget *button, RandomCorToolState *state)
145 data_analysis_output_t *dao;
146 tools_data_random_cor_t *data;
147 gint err;
149 data = g_new0 (tools_data_random_cor_t, 1);
151 dao = parse_output ((GnmGenericToolState *)state, NULL);
152 err = entry_to_int (GTK_ENTRY (state->count_entry), &data->count, FALSE);
153 data->matrix = gnm_expr_entry_parse_as_value
154 (GNM_EXPR_ENTRY (state->base.input_entry),
155 state->base.sheet);
157 data->variables = data->matrix->v_range.cell.b.row -
158 data->matrix->v_range.cell.a.row + 1;
160 data->matrix_type = gnm_gui_group_value
161 (state->base.gui, matrix_group);
164 if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
165 state->base.sheet,
166 dao, data, tool_random_cor_engine, TRUE) &&
167 (button == state->base.ok_button))
168 gtk_widget_destroy (state->base.dialog);
173 * dialog_random_cor_tool_init:
174 * @state:
176 * Create the dialog (guru).
179 static void
180 dialog_random_cor_tool_init (RandomCorToolState *state)
182 state->count_entry = go_gtk_builder_get_widget (state->base.gui, "count_entry");
183 int_to_entry (GTK_ENTRY (state->count_entry), 2);
184 gnm_editable_enters (GTK_WINDOW (state->base.dialog),
185 GTK_WIDGET (state->count_entry));
186 g_signal_connect_after (G_OBJECT (state->count_entry),
187 "changed",
188 G_CALLBACK (random_cor_tool_update_sensitivity_cb), state);
193 * dialog_random_cor_tool:
194 * @wbcg:
195 * @sheet:
197 * Show the dialog (guru).
201 dialog_random_cor_tool (WBCGtk *wbcg, Sheet *sheet)
203 RandomCorToolState *state;
205 if (wbcg == NULL) {
206 return 1;
210 /* Only pop up one copy per workbook */
211 if (gnm_dialog_raise_if_exists (wbcg, RANDOM_COR_KEY)) {
212 return 0;
215 state = g_new (RandomCorToolState, 1);
217 if (dialog_tool_init ((GnmGenericToolState *)state, wbcg, sheet,
218 GNUMERIC_HELP_LINK_RANDOM_GENERATOR_COR,
219 "res:ui/random-generation-cor.ui", "CorRandom",
220 _("Could not create the Correlated Random Tool dialog."),
221 RANDOM_COR_KEY,
222 G_CALLBACK (random_cor_tool_ok_clicked_cb), NULL,
223 G_CALLBACK (random_cor_tool_update_sensitivity_cb),
225 return 0;
228 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
229 dialog_random_cor_tool_init (state);
231 tool_load_selection ((GnmGenericToolState *)state, TRUE);
233 gtk_widget_show_all (state->base.dialog);
235 return 0;