1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * dialog-advanced-filter.c:
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/>.
25 #include <gnumeric-config.h>
26 #include <glib/gi18n-lib.h>
35 #include <dialogs/tool-dialogs.h>
36 #include <dialogs/dao-gui-utils.h>
41 #include <widgets/gnumeric-expr-entry.h>
42 #include <widgets/gnm-dao.h>
44 #include "analysis-tools.h"
48 #define ADVANCED_FILTER_KEY "advanced-filter-dialog"
50 typedef GnmGenericToolState AdvancedFilterState
;
53 * advanced_filter_update_sensitivity_cb:
57 * Update the dialog widgets sensitivity
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
);
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
);
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
);
93 gtk_label_set_text (GTK_LABEL (state
->warning
), "");
94 gtk_widget_set_sensitive (state
->ok_button
, TRUE
);
99 * advanced_filter_ok_clicked_cb:
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.
108 advanced_filter_ok_clicked_cb (G_GNUC_UNUSED GtkWidget
*button
,
109 AdvancedFilterState
*state
)
111 data_analysis_output_t
*dao
;
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 ((GnmGenericToolState
*) 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
);
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
;
146 err
= analysis_tools_noerr
;
150 if (dao
->type
== InPlaceOutput
|| err
!= analysis_tools_noerr
) {
151 value_release (input
);
152 value_release (criteria
);
157 case analysis_tools_noerr
:
158 gtk_widget_destroy (state
->dialog
);
160 case analysis_tools_invalid_field
:
161 error_in_entry ((GnmGenericToolState
*) state
,
162 GTK_WIDGET (state
->input_entry_2
),
163 _("The given criteria are invalid."));
165 case analysis_tools_no_records_found
:
166 go_gtk_notice_nonmodal_dialog ((GtkWindow
*) state
->dialog
,
167 &(state
->warning_dialog
),
169 _("No matching records were found."));
172 text
= g_strdup_printf (_("An unexpected error has occurred: "
174 error_in_entry ((GnmGenericToolState
*) state
,
175 GTK_WIDGET (state
->input_entry
), text
);
183 * dialog_advanced_filter:
186 * Show the dialog (guru).
189 dialog_advanced_filter (WBCGtk
*wbcg
)
191 AdvancedFilterState
*state
;
192 WorkbookControl
*wbc
;
194 g_return_if_fail (wbcg
!= NULL
);
196 wbc
= GNM_WBC (wbcg
);
198 /* Only pop up one copy per workbook */
199 if (gnm_dialog_raise_if_exists (wbcg
, ADVANCED_FILTER_KEY
))
202 state
= g_new (AdvancedFilterState
, 1);
204 if (dialog_tool_init (state
, wbcg
, wb_control_cur_sheet (wbc
),
205 GNUMERIC_HELP_LINK_ADVANCED_FILTER
,
206 "res:ui/advanced-filter.ui", "Filter",
207 _("Could not create the Advanced Filter dialog."),
209 G_CALLBACK (advanced_filter_ok_clicked_cb
), NULL
,
210 G_CALLBACK (advanced_filter_update_sensitivity_cb
),
214 gnm_dao_set_inplace (GNM_DAO (state
->gdao
), _("Filter _in-place"));
215 gnm_dao_set_put (GNM_DAO (state
->gdao
), FALSE
, FALSE
);
216 advanced_filter_update_sensitivity_cb (NULL
, state
);
217 tool_load_selection ((GnmGenericToolState
*)state
, TRUE
);