1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009 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 */
23 #include "find-dialog.h"
24 #include "psppire-selector.h"
25 #include "psppire-dialog.h"
27 #include "psppire-data-window.h"
28 #include "dict-display.h"
29 #include <data/value.h>
30 #include <data/format.h>
31 #include <data/datasheet.h>
32 #include <data/data-in.h>
33 #include "psppire-data-store.h"
35 #include <sys/types.h>
37 #include <libpspp/message.h>
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
49 /* FIXME: These shouldn't be here */
50 #include "psppire-var-store.h"
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)
107 g_object_get (fd->de->data_editor, "current-case", &row, NULL);
112 find_value (fd, row, &x, &column);
117 gtk_notebook_set_current_page (GTK_NOTEBOOK (fd->de->data_editor),
118 PSPPIRE_DATA_EDITOR_DATA_VIEW);
120 g_object_set (fd->de->data_editor,
122 "current-variable", column,
128 /* Callback on the selector.
129 It gets invoked whenever a variable is selected */
131 on_select (GtkEntry *entry, gpointer data)
133 struct find_dialog *fd = data;
134 const char *var_name = gtk_entry_get_text (GTK_ENTRY (fd->variable_entry));
135 struct variable *var = dict_lookup_var (fd->dict->dict, var_name);
136 gboolean search_labels ;
138 g_return_if_fail (var);
140 gtk_widget_set_sensitive (fd->value_labels_checkbox,
141 var_has_value_labels (var));
144 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->value_labels_checkbox));
146 gtk_widget_set_sensitive (fd->match_regexp_checkbox,
147 var_is_alpha (var) || search_labels);
150 gtk_widget_set_sensitive (fd->match_substring_checkbox,
151 var_is_alpha (var) || search_labels);
154 /* Callback on the selector.
155 It gets invoked whenever a variable is unselected */
157 on_deselect (GtkEntry *entry, gpointer data)
159 struct find_dialog *fd = data;
161 gtk_widget_set_sensitive (fd->value_labels_checkbox, FALSE);
162 gtk_widget_set_sensitive (fd->match_substring_checkbox, FALSE);
163 gtk_widget_set_sensitive (fd->match_regexp_checkbox, FALSE);
167 value_labels_toggled (GtkToggleButton *tb, gpointer data)
169 struct find_dialog *fd = data;
171 const char *var_name = gtk_entry_get_text (GTK_ENTRY (fd->variable_entry));
172 const struct variable *var = dict_lookup_var (fd->dict->dict, var_name);
174 gboolean active = gtk_toggle_button_get_active (tb) ;
176 gtk_widget_set_sensitive (fd->match_substring_checkbox,
177 active || (var && var_is_alpha (var)));
179 gtk_widget_set_sensitive (fd->match_regexp_checkbox,
180 active || (var && var_is_alpha (var)));
183 /* Pops up the Find dialog box
186 find_dialog (GObject *o, gpointer data)
188 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
190 struct find_dialog fd;
195 GtkWidget *find_button;
197 GtkWidget *buttonbox;
199 PsppireVarStore *vs ;
200 PsppireDataStore *ds ;
202 fd.xml = builder_new ("find.ui");
205 find_button = gtk_button_new_from_stock (GTK_STOCK_FIND);
206 gtk_widget_show (find_button);
208 buttonbox = get_widget_assert (fd.xml, "find-buttonbox");
210 gtk_box_pack_start_defaults (GTK_BOX (buttonbox), find_button);
211 gtk_box_reorder_child (GTK_BOX (buttonbox), find_button, 0);
213 dialog = get_widget_assert (fd.xml, "find-dialog");
214 source = get_widget_assert (fd.xml, "find-variable-treeview");
215 selector = get_widget_assert (fd.xml, "find-selector");
217 g_object_get (de->data_editor,
222 g_object_get (vs, "dictionary", &fd.dict, NULL);
224 fd.data = ds->datasheet;
226 fd.variable_entry = get_widget_assert (fd.xml, "find-variable-entry");
227 fd.value_entry = get_widget_assert (fd.xml, "find-value-entry");
228 fd.value_labels_checkbox =
229 get_widget_assert (fd.xml,
230 "find-value-labels-checkbutton");
232 fd.match_regexp_checkbox =
233 get_widget_assert (fd.xml,
234 "find-match-regexp-checkbutton");
236 fd.match_substring_checkbox =
237 get_widget_assert (fd.xml,
238 "find-match-substring-checkbutton");
242 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
245 g_object_set (source, "dictionary", fd.dict,
246 "selection-mode", GTK_SELECTION_SINGLE,
249 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
252 insert_source_row_into_entry,
253 is_currently_in_entry,
257 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &fd);
259 g_signal_connect (find_button, "clicked", G_CALLBACK (do_find), &fd);
261 g_signal_connect (selector, "selected",
262 G_CALLBACK (on_select), &fd);
264 g_signal_connect (selector, "de-selected",
265 G_CALLBACK (on_deselect), &fd);
267 g_signal_connect (fd.value_labels_checkbox, "toggled",
268 G_CALLBACK (value_labels_toggled), &fd);
271 psppire_dialog_run (PSPPIRE_DIALOG (dialog));
273 g_object_unref (fd.xml);
280 forward (casenumber *i, struct datasheet *data UNUSED)
287 forward_wrap (casenumber *i, struct datasheet *data)
289 if ( ++*i >= datasheet_get_n_rows (data) ) *i = 0;
293 backward (casenumber *i, struct datasheet *data UNUSED)
300 backward_wrap (casenumber *i, struct datasheet *data)
303 *i = datasheet_get_n_rows (data) - 1;
307 /* Current plus one */
309 cp1 (casenumber current, struct datasheet *data)
314 /* Current plus one, circular */
316 cp1c (casenumber current, struct datasheet *data)
318 casenumber next = current;
320 forward_wrap (&next, data);
326 /* Current minus one */
328 cm1 (casenumber current, struct datasheet *data)
333 /* Current minus one, circular */
335 cm1c (casenumber current, struct datasheet *data)
337 casenumber next = current;
339 backward_wrap (&next, data);
346 last (casenumber current, struct datasheet *data)
348 return datasheet_get_n_rows (data) ;
352 minus1 (casenumber current, struct datasheet *data)
357 /* An type to facilitate iterating through casenumbers */
358 struct casenum_iterator
360 /* returns the first case to access */
361 casenumber (*start) (casenumber, struct datasheet *);
363 /* Returns one past the last case to access */
364 casenumber (*end) (casenumber, struct datasheet *);
366 /* Sets the first arg to the next case to access */
367 void (*next) (casenumber *, struct datasheet *);
378 static const struct casenum_iterator ip[n_iterators] =
380 {cp1, last, forward},
381 {cp1c, cm1, forward_wrap},
382 {cm1, minus1, backward},
383 {cm1c, cp1, backward_wrap}
388 /* A factory returning an iterator according to the dialog box's settings */
389 static const struct casenum_iterator *
390 get_iteration_params (const struct find_dialog *fd)
392 gboolean wrap = gtk_toggle_button_get_active
393 (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-wrap")));
395 gboolean reverse = gtk_toggle_button_get_active
396 (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-backwards")));
401 return &ip[REVERSE_WRAP];
403 return &ip[FORWARD_WRAP];
415 enum string_cmp_flags
417 STR_CMP_SUBSTR = 0x01, /* Find strings which are substrings of the
419 STR_CMP_REGEXP = 0x02, /* Match against a regular expression */
421 STR_CMP_LABELS = 0x04 /* Match against the values' labels instead
426 /* An abstract base type for comparing union values against a reference */
429 const struct variable *var;
430 enum string_cmp_flags flags;
431 const PsppireDict *dict;
433 bool (*compare) (const struct comparator *,
434 const union value *);
436 void (*destroy) (struct comparator *);
440 /* A comparator which operates on the unadulterated union values */
441 struct value_comparator
443 struct comparator parent;
447 /* A comparator which matches string values or parts thereof */
448 struct string_comparator
450 struct comparator parent;
454 /* A comparator to match string values against a POSIX.2 regular expression */
455 struct regexp_comparator
457 struct comparator parent;
463 value_compare (const struct comparator *cmptr,
464 const union value *v)
466 const struct value_comparator *vc = (const struct value_comparator *) cmptr;
467 return 0 == value_compare_3way (v, &vc->pattern, var_get_width (cmptr->var));
471 /* Return true if the label of VAL matches the reference string*/
473 string_label_compare (const struct comparator *cmptr,
474 const union value *val)
476 const struct string_comparator *ssc =
477 (const struct string_comparator *) cmptr;
479 const char *text = var_lookup_value_label (cmptr->var, val);
480 int width = strlen (text);
482 assert ( cmptr->flags & STR_CMP_LABELS);
484 g_return_val_if_fail (width > 0, false);
486 if ( cmptr->flags & STR_CMP_SUBSTR)
487 return (NULL != g_strstr_len (text, width, ssc->pattern));
489 return (0 == strncmp (text, ssc->pattern, width));
492 /* Return true if VAL matches the reference string*/
494 string_value_compare (const struct comparator *cmptr,
495 const union value *val)
499 const struct string_comparator *ssc =
500 (const struct string_comparator *) cmptr;
502 int width = var_get_width (cmptr->var);
503 g_return_val_if_fail (width > 0, false);
504 assert ( ! (cmptr->flags & STR_CMP_LABELS));
506 text = value_to_text (*val, cmptr->dict, *var_get_write_format (cmptr->var));
508 if ( cmptr->flags & STR_CMP_SUBSTR)
509 found = (NULL != g_strstr_len (text, width, ssc->pattern));
511 found = (0 == strncmp (text, ssc->pattern, width));
519 /* Return true if VAL matched the regexp */
521 regexp_value_compare (const struct comparator *cmptr,
522 const union value *val)
526 const struct regexp_comparator *rec =
527 (const struct regexp_comparator *) cmptr;
529 int width = var_get_width (cmptr->var);
531 assert ( ! (cmptr->flags & STR_CMP_LABELS) );
533 g_return_val_if_fail (width > 0, false);
535 text = value_to_text (*val, cmptr->dict, *var_get_write_format (cmptr->var));
536 /* We must remove trailing whitespace, otherwise $ will not match where
540 retval = (0 == regexec (&rec->re, text, 0, 0, 0));
547 /* Return true if the label of VAL matched the regexp */
549 regexp_label_compare (const struct comparator *cmptr,
550 const union value *val)
553 const struct regexp_comparator *rec =
554 (const struct regexp_comparator *) cmptr;
558 assert ( cmptr->flags & STR_CMP_LABELS);
560 text = var_lookup_value_label (cmptr->var, val);
561 width = strlen (text);
563 g_return_val_if_fail (width > 0, false);
565 return (0 == regexec (&rec->re, text, 0, 0, 0));
571 regexp_destroy (struct comparator *cmptr)
573 struct regexp_comparator *rec = (struct regexp_comparator *) cmptr;
579 cmptr_value_destroy (struct comparator *cmptr)
581 struct value_comparator *vc = (struct value_comparator *) cmptr;
582 value_destroy (&vc->pattern, var_get_width (cmptr->var));
586 static struct comparator *
587 value_comparator_create (const struct variable *var, const PsppireDict *dict, const char *target)
589 struct value_comparator *vc = xzalloc (sizeof (*vc));
590 struct comparator *cmptr = (struct comparator *) vc;
594 cmptr->compare = value_compare ;
595 cmptr->destroy = cmptr_value_destroy;
598 text_to_value (target, dict, var, &vc->pattern);
603 static struct comparator *
604 string_comparator_create (const struct variable *var, const PsppireDict *dict,
606 enum string_cmp_flags flags)
608 struct string_comparator *ssc = xzalloc (sizeof (*ssc));
609 struct comparator *cmptr = (struct comparator *) ssc;
611 cmptr->flags = flags;
615 if ( flags & STR_CMP_LABELS)
616 cmptr->compare = string_label_compare;
618 cmptr->compare = string_value_compare;
620 ssc->pattern = target;
626 static struct comparator *
627 regexp_comparator_create (const struct variable *var, const PsppireDict *dict, const char *target,
628 enum string_cmp_flags flags)
631 struct regexp_comparator *rec = xzalloc (sizeof (*rec));
632 struct comparator *cmptr = (struct comparator *) rec;
634 cmptr->flags = flags;
637 cmptr->compare = (flags & STR_CMP_LABELS)
638 ? regexp_label_compare : regexp_value_compare ;
640 cmptr->destroy = regexp_destroy;
642 code = regcomp (&rec->re, target, 0);
646 size_t errbuf_size = regerror (code, &rec->re, errbuf, 0);
648 errbuf = xmalloc (errbuf_size);
650 regerror (code, &rec->re, errbuf, errbuf_size);
652 msg (ME, _("Bad regular expression: %s"), errbuf);
663 /* Compare V against CMPTR's reference */
665 comparator_compare (const struct comparator *cmptr,
666 const union value *v)
668 return cmptr->compare (cmptr, v);
673 comparator_destroy (struct comparator *cmptr)
678 if ( cmptr->destroy )
679 cmptr->destroy (cmptr);
685 static struct comparator *
686 comparator_factory (const struct variable *var, const PsppireDict *dict, const char *str,
687 enum string_cmp_flags flags)
689 if ( flags & STR_CMP_REGEXP )
690 return regexp_comparator_create (var, dict, str, flags);
692 if ( flags & (STR_CMP_SUBSTR | STR_CMP_LABELS) )
693 return string_comparator_create (var, dict, str, flags);
695 return value_comparator_create (var, dict, str);
699 /* Find the row and column specified by the dialog FD, starting at CURRENT_ROW.
700 After the function returns, *ROW contains the row and *COLUMN the column.
701 If no such case is found, then *ROW will be set to -1
704 find_value (const struct find_dialog *fd, casenumber current_row,
705 casenumber *row, int *column)
708 const struct variable *var;
709 const char *var_name = gtk_entry_get_text (GTK_ENTRY (fd->variable_entry));
710 const char *target_string = gtk_entry_get_text (GTK_ENTRY (fd->value_entry));
712 enum string_cmp_flags flags = 0;
713 g_assert (current_row >= 0);
715 var = dict_lookup_var (fd->dict->dict, var_name);
719 width = var_get_width (var);
721 *column = var_get_dict_index (var);
724 if ( gtk_toggle_button_get_active
725 (GTK_TOGGLE_BUTTON (fd->match_substring_checkbox)))
726 flags |= STR_CMP_SUBSTR;
728 if ( gtk_toggle_button_get_active
729 (GTK_TOGGLE_BUTTON (fd->match_regexp_checkbox)))
730 flags |= STR_CMP_REGEXP;
732 if ( gtk_toggle_button_get_active
733 (GTK_TOGGLE_BUTTON (fd->value_labels_checkbox)))
734 flags |= STR_CMP_LABELS;
739 const struct casenum_iterator *ip = get_iteration_params (fd);
740 struct comparator *cmptr =
741 comparator_factory (var, fd->dict, target_string, flags);
743 value_init (&val, width);
747 for (i = ip->start (current_row, fd->data);
748 i != ip->end (current_row, fd->data);
749 ip->next (&i, fd->data))
751 datasheet_get_value (fd->data, i, var_get_case_index (var), &val);
753 if ( comparator_compare (cmptr, &val))
761 comparator_destroy (cmptr);
762 value_destroy (&val, width);