ssdiff: move comparison engine into its own file.
[gnumeric.git] / src / dialogs / dialog-advanced-filter.c
blobd3b53ba709a8d140d0d4ab65a7d30c8c630785e0
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-advanced-filter.c:
5 * Authors:
6 * Jukka-Pekka Iivonen <iivonen@iki.fi>
7 * Andreas J. Guelzow <aguelzow@taliesin.ca>
9 * (C) Copyright 2000, 2001 by Jukka-Pekka Iivonen <jiivonen@hutcs.cs.hut.fi>
10 * (C) Copyright 2002 by Andreas J. Guelzow <aguelzow@taliesin.ca>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 **/
25 #include <gnumeric-config.h>
26 #include <glib/gi18n-lib.h>
27 #include <gnumeric.h>
28 #include "dialogs.h"
29 #include "help.h"
31 #include <sheet.h>
32 #include <cell.h>
33 #include <ranges.h>
34 #include <gui-util.h>
35 #include <tool-dialogs.h>
36 #include <dao-gui-utils.h>
37 #include <value.h>
38 #include <wbc-gtk.h>
40 #include <gtk/gtk.h>
41 #include <widgets/gnumeric-expr-entry.h>
42 #include <widgets/gnm-dao.h>
43 #include "filter.h"
44 #include "analysis-tools.h"
45 #include "commands.h"
48 #define ADVANCED_FILTER_KEY "advanced-filter-dialog"
50 typedef GenericToolState AdvancedFilterState;
52 /**
53 * advanced_filter_update_sensitivity_cb:
54 * @dummy:
55 * @state:
57 * Update the dialog widgets sensitivity
58 **/
59 static void
60 advanced_filter_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
61 AdvancedFilterState *state)
63 GnmValue *input_range = NULL;
64 GnmValue *criteria_range = NULL;
66 input_range = gnm_expr_entry_parse_as_value (
67 GNM_EXPR_ENTRY (state->input_entry), state->sheet);
68 if (input_range == NULL) {
69 gtk_label_set_text (GTK_LABEL (state->warning),
70 _("The list range is invalid."));
71 gtk_widget_set_sensitive (state->ok_button, FALSE);
72 return;
73 } else
74 value_release (input_range);
76 criteria_range = gnm_expr_entry_parse_as_value
77 (state->input_entry_2, state->sheet);
78 if (criteria_range == NULL) {
79 gtk_label_set_text (GTK_LABEL (state->warning),
80 _("The criteria range is invalid."));
81 gtk_widget_set_sensitive (state->ok_button, FALSE);
82 return;
83 } else
84 value_release (criteria_range);
86 if (!gnm_dao_is_ready (GNM_DAO (state->gdao))) {
87 gtk_label_set_text (GTK_LABEL (state->warning),
88 _("The output range is invalid."));
89 gtk_widget_set_sensitive (state->ok_button, FALSE);
90 return;
93 gtk_label_set_text (GTK_LABEL (state->warning), "");
94 gtk_widget_set_sensitive (state->ok_button, TRUE);
95 return;
98 /**
99 * advanced_filter_ok_clicked_cb:
100 * @button:
101 * @state:
103 * Retrieve the information from the dialog and call the advanced_filter.
104 * Note that we assume that the ok_button is only active if the entry fields
105 * contain sensible data.
107 static void
108 advanced_filter_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
109 AdvancedFilterState *state)
111 data_analysis_output_t *dao;
112 GnmValue *input;
113 GnmValue *criteria;
114 char *text;
115 GtkWidget *w;
116 int err = 0;
117 gboolean unique;
119 input = gnm_expr_entry_parse_as_value (
120 GNM_EXPR_ENTRY (state->input_entry), state->sheet);
122 criteria = gnm_expr_entry_parse_as_value
123 (state->input_entry_2, state->sheet);
125 dao = parse_output ((GenericToolState *) state, NULL);
127 w = go_gtk_builder_get_widget (state->gui, "unique-button");
128 unique = (1 == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)));
130 if (dao->type == InPlaceOutput)
131 err = advanced_filter (GNM_WBC (state->wbcg),
132 dao, input, criteria, unique);
133 else {
134 analysis_tools_data_advanced_filter_t *
135 data = g_new0 (analysis_tools_data_advanced_filter_t, 1);
136 data->base.wbc = GNM_WBC (state->wbcg);
137 data->base.range_1 = input;
138 data->base.range_2 = criteria;
139 data->unique_only_flag = unique;
141 if (cmd_analysis_tool (GNM_WBC (state->wbcg), state->sheet,
142 dao, data, analysis_tool_advanced_filter_engine, FALSE)) {
143 err = data->base.err;
144 g_free (data);
145 } else
146 err = analysis_tools_noerr;
150 if (dao->type == InPlaceOutput || err != analysis_tools_noerr) {
151 value_release (input);
152 value_release (criteria);
153 g_free (dao);
156 switch (err) {
157 case analysis_tools_noerr:
158 gtk_widget_destroy (state->dialog);
159 break;
160 case analysis_tools_invalid_field:
161 error_in_entry ((GenericToolState *) state,
162 GTK_WIDGET (state->input_entry_2),
163 _("The given criteria are invalid."));
164 break;
165 case analysis_tools_no_records_found:
166 go_gtk_notice_nonmodal_dialog ((GtkWindow *) state->dialog,
167 &(state->warning_dialog),
168 GTK_MESSAGE_INFO,
169 _("No matching records were found."));
170 break;
171 default:
172 text = g_strdup_printf (_("An unexpected error has occurred: "
173 "%d."), err);
174 error_in_entry ((GenericToolState *) state,
175 GTK_WIDGET (state->input_entry), text);
176 g_free (text);
177 break;
179 return;
183 * dialog_advanced_filter:
184 * @wbcg:
185 * @sheet:
187 * Show the dialog (guru).
190 void
191 dialog_advanced_filter (WBCGtk *wbcg)
193 AdvancedFilterState *state;
194 WorkbookControl *wbc;
196 g_return_if_fail (wbcg != NULL);
198 wbc = GNM_WBC (wbcg);
200 /* Only pop up one copy per workbook */
201 if (gnm_dialog_raise_if_exists (wbcg, ADVANCED_FILTER_KEY))
202 return;
204 state = g_new (AdvancedFilterState, 1);
206 if (dialog_tool_init (state, wbcg, wb_control_cur_sheet (wbc),
207 GNUMERIC_HELP_LINK_ADVANCED_FILTER,
208 "advanced-filter.ui", "Filter",
209 _("Could not create the Advanced Filter dialog."),
210 ADVANCED_FILTER_KEY,
211 G_CALLBACK (advanced_filter_ok_clicked_cb), NULL,
212 G_CALLBACK (advanced_filter_update_sensitivity_cb),
214 return;
216 gnm_dao_set_inplace (GNM_DAO (state->gdao), _("Filter _in-place"));
217 gnm_dao_set_put (GNM_DAO (state->gdao), FALSE, FALSE);
218 advanced_filter_update_sensitivity_cb (NULL, state);
219 tool_load_selection ((GenericToolState *)state, TRUE);
221 return;