2 * dialog-random-generator.c:
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>
28 #include "dialogs/tool-dialogs.h"
29 #include "tools/random-generator-cor.h"
32 #include <workbook-control.h>
34 #include <workbook-view.h>
36 #include <parse-util.h>
37 #include <gnm-format.h>
38 #include <dialogs/dao-gui-utils.h>
41 #include <number-match.h>
43 #include <selection.h>
47 #include <widgets/gnumeric-expr-entry.h>
48 #include <widgets/gnm-dao.h>
54 /**********************************************/
55 /* Generic guru items */
56 /**********************************************/
59 #define RANDOM_COR_KEY "analysistools-random-cor-dialog"
61 static char const * const matrix_group
[] = {
68 GnmGenericToolState base
;
69 GtkWidget
*count_entry
;
73 /**********************************************/
74 /* Begin of random tool code */
75 /**********************************************/
79 * random_cor_tool_update_sensitivity_cb:
83 * Update the dialog widgets sensitivity if the only items of interest
84 * are the standard input (one range) and output items.
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
);
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
);
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 "
117 gtk_widget_set_sensitive (state
->base
.ok_button
, FALSE
);
121 if (entry_to_int (GTK_ENTRY (state
->count_entry
), &count
, FALSE
) != 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
);
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:
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.
143 random_cor_tool_ok_clicked_cb (GtkWidget
*button
, RandomCorToolState
*state
)
145 data_analysis_output_t
*dao
;
146 tools_data_random_cor_t
*data
;
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
),
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
),
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:
176 * Create the dialog (guru).
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
),
188 G_CALLBACK (random_cor_tool_update_sensitivity_cb
), state
);
193 * dialog_random_cor_tool:
197 * Show the dialog (guru).
201 dialog_random_cor_tool (WBCGtk
*wbcg
, Sheet
*sheet
)
203 RandomCorToolState
*state
;
210 /* Only pop up one copy per workbook */
211 if (gnm_dialog_raise_if_exists (wbcg
, RANDOM_COR_KEY
)) {
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."),
222 G_CALLBACK (random_cor_tool_ok_clicked_cb
), NULL
,
223 G_CALLBACK (random_cor_tool_update_sensitivity_cb
),
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
);