1 /*
2  * dialog-shuffle.c:
3  *
4  * Authors:
5  *        Jukka-Pekka Iivonen <iivonen@iki.fi>
6  *        Andreas J. Guelzow <aguelzow@taliesin.ca>
7  *
8  * (C) Copyright 2000-2003 by Jukka-Pekka Iivonen <jiivonen@hutcs.cs.hut.fi>
9  * (C) Copyright 2002 by Andreas J. Guelzow <aguelzow@taliesin.ca>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, see <https://www.gnu.org/licenses/>.
23  **/
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
26 #include <gnumeric.h>
27 #include <dialogs/dialogs.h>
28 #include <dialogs/help.h>
29 
30 #include <sheet.h>
31 #include <cell.h>
32 #include <ranges.h>
33 #include <gui-util.h>
34 #include <dialogs/tool-dialogs.h>
35 #include <dialogs/dao-gui-utils.h>
36 #include <value.h>
37 #include <wbc-gtk.h>
38 #include <commands.h>
39 #include <selection.h>
40 
41 #include <widgets/gnm-expr-entry.h>
42 #include <tools/data-shuffling.h>
43 
44 
45 #define SHUFFLE_KEY         "shuffle-dialog"
46 
47 typedef GnmGenericToolState ShuffleState;
48 
49 static char const * const shuffle_by[] = {
50 	"shuffle_cols",
51 	"shuffle_rows",
52 	"shuffle_area",
53 	NULL
54 };
55 
56 /**
57  * shuffle_update_sensitivity_cb:
58  * @dummy:
59  * @state:
60  *
61  * Update the dialog widgets sensitivity
62  **/
63 static void
shuffle_update_sensitivity_cb(G_GNUC_UNUSED GtkWidget * dummy,ShuffleState * state)64 shuffle_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
65 			       ShuffleState *state)
66 {
67         GnmValue *input_range = gnm_expr_entry_parse_as_value (
68 		GNM_EXPR_ENTRY (state->input_entry), state->sheet);
69 	if (input_range == NULL) {
70 		gtk_label_set_text (GTK_LABEL (state->warning),
71 				    _("The input range is invalid."));
72 		gtk_widget_set_sensitive (state->ok_button, FALSE);
73 	} else {
74 		value_release (input_range);
75 		gtk_label_set_text (GTK_LABEL (state->warning), "");
76 		gtk_widget_set_sensitive (state->ok_button, TRUE);
77 	}
78 }
79 
80 /**
81  * shuffle_ok_clicked_cb:
82  * @button:
83  * @state:
84  *
85  * Retrieve the information from the dialog and call the data_shuffling.
86  * Note that we assume that the ok_button is only active if the entry fields
87  * contain sensible data.
88  **/
89 static void
shuffle_ok_clicked_cb(G_GNUC_UNUSED GtkWidget * button,ShuffleState * state)90 shuffle_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button, ShuffleState *state)
91 {
92 	data_analysis_output_t  *dao;
93 	data_shuffling_t        *ds;
94 	WorkbookControl         *wbc;
95 	GnmValue                *input;
96 	int                     type;
97 
98 	/* This is free'ed by data_shuffling_free. */
99 	/* We later want to extend this to shuffle to other locations */
100 	dao = dao_init (NULL, InPlaceOutput);
101 
102 	input = gnm_expr_entry_parse_as_value (
103 		GNM_EXPR_ENTRY (state->input_entry), state->sheet);
104 
105 	if (dao->type == InPlaceOutput)
106 		dao_load_from_value (dao, input);
107 
108 	type = gnm_gui_group_value (state->gui, shuffle_by);
109 
110 	ds = data_shuffling (GNM_WBC (state->wbcg), dao,
111 			     state->sheet, input, type);
112 
113 	wbc = GNM_WBC (state->wbcg);
114 	cmd_data_shuffle (wbc, ds, state->sheet);
115 
116 	value_release (input);
117 	gtk_widget_destroy (state->dialog);
118 }
119 
120 /**
121  * dialog_shuffle:
122  * @wbcg:
123  *
124  * Show the dialog (guru).
125  **/
126 void
dialog_shuffle(WBCGtk * wbcg)127 dialog_shuffle (WBCGtk *wbcg)
128 {
129         ShuffleState    *state;
130 	WorkbookControl *wbc;
131 	GtkWidget *w;
132 	char const *type;
133 	GnmRange const *r;
134 
135 	g_return_if_fail (wbcg != NULL);
136 
137 	wbc = GNM_WBC (wbcg);
138 
139 	/* Only pop up one copy per workbook */
140 	if (gnm_dialog_raise_if_exists (wbcg, SHUFFLE_KEY))
141 		return;
142 
143 	state = g_new (ShuffleState, 1);
144 
145 	if (dialog_tool_init (state, wbcg, wb_control_cur_sheet (wbc),
146 			      GNUMERIC_HELP_LINK_DATA_SHUFFLE,
147 			      "res:ui/shuffle.ui", "Shuffling",
148 			      _("Could not create the Data Shuffling dialog."),
149 			      SHUFFLE_KEY,
150 			      G_CALLBACK (shuffle_ok_clicked_cb), NULL,
151 			      G_CALLBACK (shuffle_update_sensitivity_cb),
152 			      0))
153 		return;
154 
155 	shuffle_update_sensitivity_cb (NULL, state);
156 	state->gdao = NULL;
157 	tool_load_selection ((GnmGenericToolState *)state, FALSE);
158 
159 	r = selection_first_range (state->sv, NULL, NULL);
160 	if (range_width (r) == 1)
161 		type = "shuffle_cols";
162 	else if (range_height (r) == 1)
163 		type = "shuffle_rows";
164 	else
165 		type = "shuffle_area";
166 	w = go_gtk_builder_get_widget (state->gui, type);
167 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
168 
169 	gtk_widget_show (state->dialog);
170 
171         return;
172 }
173