1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009, 2011, 2012 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* This module implements the "Find" dialog; a dialog box to locate cases
19 which match particular strings */
27 #include <sys/types.h>
29 #include "data/data-in.h"
30 #include "data/datasheet.h"
31 #include "data/format.h"
32 #include "data/value.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/message.h"
35 #include "ui/gui/builder-wrapper.h"
36 #include "ui/gui/dict-display.h"
37 #include "ui/gui/find-dialog.h"
38 #include "ui/gui/helper.h"
39 #include "ui/gui/psppire-data-sheet.h"
40 #include "ui/gui/psppire-data-store.h"
41 #include "ui/gui/psppire-data-window.h"
42 #include "ui/gui/psppire-dialog.h"
43 #include "ui/gui/psppire-selector.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
56 struct datasheet *data;
57 PsppireDataWindow *de;
58 GtkWidget *variable_entry;
59 GtkWidget *value_entry;
60 GtkWidget *value_labels_checkbox;
61 GtkWidget *match_regexp_checkbox;
62 GtkWidget *match_substring_checkbox;
66 find_value (const struct find_dialog *fd, casenumber current_row,
67 casenumber *row, int *column);
70 /* A callback which occurs whenever the "Refresh" button is clicked,
71 and when the dialog pops up.
72 It restores the dialog to its default state.
75 refresh (GObject *obj, const struct find_dialog *fd)
77 gtk_toggle_button_set_active
78 (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-wrap")),
81 gtk_toggle_button_set_active
82 (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-backwards")),
85 gtk_entry_set_text (GTK_ENTRY (fd->variable_entry), "");
86 gtk_entry_set_text (GTK_ENTRY (fd->value_entry), "");
88 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->match_regexp_checkbox),
91 gtk_toggle_button_set_active
92 (GTK_TOGGLE_BUTTON (fd->match_substring_checkbox), FALSE);
95 gtk_toggle_button_set_active
96 (GTK_TOGGLE_BUTTON (fd->match_substring_checkbox), FALSE);
99 /* Callback on the "Find" button */
101 do_find (GObject *obj, const struct find_dialog *fd)
103 PsppireDataSheet *data_sheet;
108 data_sheet = psppire_data_editor_get_active_data_sheet (fd->de->data_editor);
109 row = psppire_data_sheet_get_selected_case (data_sheet);
113 find_value (fd, row, &x, &column);
118 gtk_notebook_set_current_page (GTK_NOTEBOOK (fd->de->data_editor),
119 PSPPIRE_DATA_EDITOR_DATA_VIEW);
121 psppire_data_sheet_goto_case (data_sheet, x);
122 psppire_data_sheet_goto_variable (data_sheet, column);
127 /* Callback on the selector.
128 It gets invoked whenever a variable is selected */
130 on_select (GtkEntry *entry, gpointer data)
132 struct find_dialog *fd = data;
133 const char *var_name = gtk_entry_get_text (GTK_ENTRY (fd->variable_entry));
134 struct variable *var = dict_lookup_var (fd->dict->dict, var_name);
135 gboolean search_labels ;
137 g_return_if_fail (var);
139 gtk_widget_set_sensitive (fd->value_labels_checkbox,
140 var_has_value_labels (var));
143 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->value_labels_checkbox));
145 gtk_widget_set_sensitive (fd->match_regexp_checkbox,
146 var_is_alpha (var) || search_labels);
149 gtk_widget_set_sensitive (fd->match_substring_checkbox,
150 var_is_alpha (var) || search_labels);
153 /* Callback on the selector.
154 It gets invoked whenever a variable is unselected */
156 on_deselect (GtkEntry *entry, gpointer data)
158 struct find_dialog *fd = data;
160 gtk_widget_set_sensitive (fd->value_labels_checkbox, FALSE);
161 gtk_widget_set_sensitive (fd->match_substring_checkbox, FALSE);
162 gtk_widget_set_sensitive (fd->match_regexp_checkbox, FALSE);
166 value_labels_toggled (GtkToggleButton *tb, gpointer data)
168 struct find_dialog *fd = data;
170 const char *var_name = gtk_entry_get_text (GTK_ENTRY (fd->variable_entry));
171 const struct variable *var = dict_lookup_var (fd->dict->dict, var_name);
173 gboolean active = gtk_toggle_button_get_active (tb) ;
175 gtk_widget_set_sensitive (fd->match_substring_checkbox,
176 active || (var && var_is_alpha (var)));
178 gtk_widget_set_sensitive (fd->match_regexp_checkbox,
179 active || (var && var_is_alpha (var)));
182 /* Pops up the Find dialog box
185 find_dialog (PsppireDataWindow *de)
187 struct find_dialog fd;
192 GtkWidget *find_button;
194 GtkWidget *buttonbox;
196 PsppireDataStore *ds ;
198 fd.xml = builder_new ("find.ui");
201 find_button = gtk_button_new_from_stock (GTK_STOCK_FIND);
202 gtk_widget_show (find_button);
204 buttonbox = get_widget_assert (fd.xml, "find-buttonbox");
206 psppire_box_pack_start_defaults (GTK_BOX (buttonbox), find_button);
207 gtk_box_reorder_child (GTK_BOX (buttonbox), find_button, 0);
209 dialog = get_widget_assert (fd.xml, "find-dialog");
210 source = get_widget_assert (fd.xml, "find-variable-treeview");
211 selector = get_widget_assert (fd.xml, "find-selector");
213 g_object_get (de->data_editor,
214 "dictionary", &fd.dict,
218 fd.data = ds->datasheet;
220 fd.variable_entry = get_widget_assert (fd.xml, "find-variable-entry");
221 fd.value_entry = get_widget_assert (fd.xml, "find-value-entry");
222 fd.value_labels_checkbox =
223 get_widget_assert (fd.xml,
224 "find-value-labels-checkbutton");
226 fd.match_regexp_checkbox =
227 get_widget_assert (fd.xml,
228 "find-match-regexp-checkbutton");
230 fd.match_substring_checkbox =
231 get_widget_assert (fd.xml,
232 "find-match-substring-checkbutton");
236 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
239 g_object_set (source, "model", fd.dict,
240 "selection-mode", GTK_SELECTION_SINGLE,
244 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
245 is_currently_in_entry);
247 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &fd);
249 g_signal_connect (find_button, "clicked", G_CALLBACK (do_find), &fd);
251 g_signal_connect (selector, "selected",
252 G_CALLBACK (on_select), &fd);
254 g_signal_connect (selector, "de-selected",
255 G_CALLBACK (on_deselect), &fd);
257 g_signal_connect (fd.value_labels_checkbox, "toggled",
258 G_CALLBACK (value_labels_toggled), &fd);
261 psppire_dialog_run (PSPPIRE_DIALOG (dialog));
263 g_object_unref (fd.xml);
270 forward (casenumber *i, struct datasheet *data UNUSED)
277 forward_wrap (casenumber *i, struct datasheet *data)
279 if ( ++*i >= datasheet_get_n_rows (data) ) *i = 0;
283 backward (casenumber *i, struct datasheet *data UNUSED)
290 backward_wrap (casenumber *i, struct datasheet *data)
293 *i = datasheet_get_n_rows (data) - 1;
297 /* Current plus one */
299 cp1 (casenumber current, struct datasheet *data)
304 /* Current plus one, circular */
306 cp1c (casenumber current, struct datasheet *data)
308 casenumber next = current;
310 forward_wrap (&next, data);
316 /* Current minus one */
318 cm1 (casenumber current, struct datasheet *data)
323 /* Current minus one, circular */
325 cm1c (casenumber current, struct datasheet *data)
327 casenumber next = current;
329 backward_wrap (&next, data);
336 last (casenumber current, struct datasheet *data)
338 return datasheet_get_n_rows (data) ;
342 minus1 (casenumber current, struct datasheet *data)
347 /* An type to facilitate iterating through casenumbers */
348 struct casenum_iterator
350 /* returns the first case to access */
351 casenumber (*start) (casenumber, struct datasheet *);
353 /* Returns one past the last case to access */
354 casenumber (*end) (casenumber, struct datasheet *);
356 /* Sets the first arg to the next case to access */
357 void (*next) (casenumber *, struct datasheet *);
368 static const struct casenum_iterator ip[n_iterators] =
370 {cp1, last, forward},
371 {cp1c, cm1, forward_wrap},
372 {cm1, minus1, backward},
373 {cm1c, cp1, backward_wrap}
378 /* A factory returning an iterator according to the dialog box's settings */
379 static const struct casenum_iterator *
380 get_iteration_params (const struct find_dialog *fd)
382 gboolean wrap = gtk_toggle_button_get_active
383 (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-wrap")));
385 gboolean reverse = gtk_toggle_button_get_active
386 (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-backwards")));
391 return &ip[REVERSE_WRAP];
393 return &ip[FORWARD_WRAP];
405 enum string_cmp_flags
407 STR_CMP_SUBSTR = 0x01, /* Find strings which are substrings of the
409 STR_CMP_REGEXP = 0x02, /* Match against a regular expression */
411 STR_CMP_LABELS = 0x04 /* Match against the values' labels instead
416 /* An abstract base type for comparing union values against a reference */
419 const struct variable *var;
420 enum string_cmp_flags flags;
422 bool (*compare) (const struct comparator *,
423 const union value *);
425 void (*destroy) (struct comparator *);
429 /* A comparator which operates on the unadulterated union values */
430 struct value_comparator
432 struct comparator parent;
436 /* A comparator which matches string values or parts thereof */
437 struct string_comparator
439 struct comparator parent;
443 /* A comparator to match string values against a POSIX.2 regular expression */
444 struct regexp_comparator
446 struct comparator parent;
452 value_compare (const struct comparator *cmptr,
453 const union value *v)
455 const struct value_comparator *vc = (const struct value_comparator *) cmptr;
456 return 0 == value_compare_3way (v, &vc->pattern, var_get_width (cmptr->var));
460 /* Return true if the label of VAL matches the reference string*/
462 string_label_compare (const struct comparator *cmptr,
463 const union value *val)
465 const struct string_comparator *ssc =
466 (const struct string_comparator *) cmptr;
470 const char *text = var_lookup_value_label (cmptr->var, val);
474 width = strlen (text);
476 assert ( cmptr->flags & STR_CMP_LABELS);
478 g_return_val_if_fail (width > 0, false);
480 if ( cmptr->flags & STR_CMP_SUBSTR)
481 return (NULL != g_strstr_len (text, width, ssc->pattern));
483 return (0 == strncmp (text, ssc->pattern, width));
486 /* Return true if VAL matches the reference string*/
488 string_value_compare (const struct comparator *cmptr,
489 const union value *val)
493 const struct string_comparator *ssc =
494 (const struct string_comparator *) cmptr;
496 int width = var_get_width (cmptr->var);
497 g_return_val_if_fail (width > 0, false);
498 assert ( ! (cmptr->flags & STR_CMP_LABELS));
500 text = value_to_text (*val, cmptr->var);
502 if ( cmptr->flags & STR_CMP_SUBSTR)
503 found = (NULL != g_strstr_len (text, width, ssc->pattern));
505 found = (0 == strncmp (text, ssc->pattern, width));
513 /* Return true if VAL matched the regexp */
515 regexp_value_compare (const struct comparator *cmptr,
516 const union value *val)
520 const struct regexp_comparator *rec =
521 (const struct regexp_comparator *) cmptr;
523 int width = var_get_width (cmptr->var);
525 assert ( ! (cmptr->flags & STR_CMP_LABELS) );
527 g_return_val_if_fail (width > 0, false);
529 text = value_to_text (*val, cmptr->var);
530 /* We must remove trailing whitespace, otherwise $ will not match where
534 retval = (0 == regexec (&rec->re, text, 0, 0, 0));
541 /* Return true if the label of VAL matched the regexp */
543 regexp_label_compare (const struct comparator *cmptr,
544 const union value *val)
547 const struct regexp_comparator *rec =
548 (const struct regexp_comparator *) cmptr;
552 assert ( cmptr->flags & STR_CMP_LABELS);
554 text = var_lookup_value_label (cmptr->var, val);
555 width = strlen (text);
557 g_return_val_if_fail (width > 0, false);
559 return (0 == regexec (&rec->re, text, 0, 0, 0));
565 regexp_destroy (struct comparator *cmptr)
567 struct regexp_comparator *rec
568 = UP_CAST (cmptr, struct regexp_comparator, parent);
574 cmptr_value_destroy (struct comparator *cmptr)
576 struct value_comparator *vc
577 = UP_CAST (cmptr, struct value_comparator, parent);
578 value_destroy (&vc->pattern, var_get_width (cmptr->var));
582 static struct comparator *
583 value_comparator_create (const struct variable *var, const char *target)
585 struct value_comparator *vc = xzalloc (sizeof (*vc));
586 struct comparator *cmptr = &vc->parent;
590 cmptr->compare = value_compare ;
591 cmptr->destroy = cmptr_value_destroy;
593 text_to_value (target, var, &vc->pattern);
598 static struct comparator *
599 string_comparator_create (const struct variable *var, const char *target,
600 enum string_cmp_flags flags)
602 struct string_comparator *ssc = xzalloc (sizeof (*ssc));
603 struct comparator *cmptr = &ssc->parent;
605 cmptr->flags = flags;
608 if ( flags & STR_CMP_LABELS)
609 cmptr->compare = string_label_compare;
611 cmptr->compare = string_value_compare;
613 ssc->pattern = target;
619 static struct comparator *
620 regexp_comparator_create (const struct variable *var, const char *target,
621 enum string_cmp_flags flags)
624 struct regexp_comparator *rec = xzalloc (sizeof (*rec));
625 struct comparator *cmptr = &rec->parent;
627 cmptr->flags = flags;
629 cmptr->compare = (flags & STR_CMP_LABELS)
630 ? regexp_label_compare : regexp_value_compare ;
632 cmptr->destroy = regexp_destroy;
634 code = regcomp (&rec->re, target, 0);
638 size_t errbuf_size = regerror (code, &rec->re, errbuf, 0);
640 errbuf = xmalloc (errbuf_size);
642 regerror (code, &rec->re, errbuf, errbuf_size);
644 msg (ME, _("Bad regular expression: %s"), errbuf);
655 /* Compare V against CMPTR's reference */
657 comparator_compare (const struct comparator *cmptr,
658 const union value *v)
660 return cmptr->compare (cmptr, v);
665 comparator_destroy (struct comparator *cmptr)
670 if ( cmptr->destroy )
671 cmptr->destroy (cmptr);
677 static struct comparator *
678 comparator_factory (const struct variable *var, const char *str,
679 enum string_cmp_flags flags)
681 if ( flags & STR_CMP_REGEXP )
682 return regexp_comparator_create (var, str, flags);
684 if ( flags & (STR_CMP_SUBSTR | STR_CMP_LABELS) )
685 return string_comparator_create (var, str, flags);
687 return value_comparator_create (var, str);
691 /* Find the row and column specified by the dialog FD, starting at CURRENT_ROW.
692 After the function returns, *ROW contains the row and *COLUMN the column.
693 If no such case is found, then *ROW will be set to -1
696 find_value (const struct find_dialog *fd, casenumber current_row,
697 casenumber *row, int *column)
700 const struct variable *var;
701 const char *var_name = gtk_entry_get_text (GTK_ENTRY (fd->variable_entry));
702 const char *target_string = gtk_entry_get_text (GTK_ENTRY (fd->value_entry));
704 enum string_cmp_flags flags = 0;
705 g_assert (current_row >= 0);
707 var = dict_lookup_var (fd->dict->dict, var_name);
711 width = var_get_width (var);
713 *column = var_get_dict_index (var);
716 if ( gtk_toggle_button_get_active
717 (GTK_TOGGLE_BUTTON (fd->match_substring_checkbox)))
718 flags |= STR_CMP_SUBSTR;
720 if ( gtk_toggle_button_get_active
721 (GTK_TOGGLE_BUTTON (fd->match_regexp_checkbox)))
722 flags |= STR_CMP_REGEXP;
724 if ( gtk_toggle_button_get_active
725 (GTK_TOGGLE_BUTTON (fd->value_labels_checkbox)))
726 flags |= STR_CMP_LABELS;
731 const struct casenum_iterator *ip = get_iteration_params (fd);
732 struct comparator *cmptr =
733 comparator_factory (var, target_string, flags);
735 value_init (&val, width);
739 for (i = ip->start (current_row, fd->data);
740 i != ip->end (current_row, fd->data);
741 ip->next (&i, fd->data))
743 datasheet_get_value (fd->data, i, var_get_case_index (var), &val);
745 if ( comparator_compare (cmptr, &val))
753 comparator_destroy (cmptr);
754 value_destroy (&val, width);