Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 1 | from tkinter import * |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 3 | from idlelib import SearchEngine |
| 4 | from idlelib.SearchDialogBase import SearchDialogBase |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 5 | |
| 6 | def _setup(text): |
| 7 | root = text._root() |
| 8 | engine = SearchEngine.get(root) |
| 9 | if not hasattr(engine, "_searchdialog"): |
| 10 | engine._searchdialog = SearchDialog(root, engine) |
| 11 | return engine._searchdialog |
| 12 | |
| 13 | def find(text): |
Chui Tey | 5a231c8f | 2002-11-06 02:18:45 +0000 | [diff] [blame] | 14 | pat = text.get("sel.first", "sel.last") |
| 15 | return _setup(text).open(text,pat) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 16 | |
| 17 | def find_again(text): |
| 18 | return _setup(text).find_again(text) |
| 19 | |
| 20 | def find_selection(text): |
| 21 | return _setup(text).find_selection(text) |
| 22 | |
| 23 | class SearchDialog(SearchDialogBase): |
| 24 | |
| 25 | def create_widgets(self): |
Terry Jan Reedy | 2733618 | 2015-05-14 18:10:50 -0400 | [diff] [blame] | 26 | SearchDialogBase.create_widgets(self) |
Roger Serwy | 391f469 | 2013-06-10 23:01:20 -0500 | [diff] [blame] | 27 | self.make_button("Find Next", self.default_command, 1) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | |
| 29 | def default_command(self, event=None): |
| 30 | if not self.engine.getprog(): |
| 31 | return |
Roger Serwy | 391f469 | 2013-06-10 23:01:20 -0500 | [diff] [blame] | 32 | self.find_again(self.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 33 | |
| 34 | def find_again(self, text): |
| 35 | if not self.engine.getpat(): |
| 36 | self.open(text) |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 37 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 38 | if not self.engine.getprog(): |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 39 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 40 | res = self.engine.search_text(text) |
| 41 | if res: |
| 42 | line, m = res |
| 43 | i, j = m.span() |
| 44 | first = "%d.%d" % (line, i) |
| 45 | last = "%d.%d" % (line, j) |
| 46 | try: |
| 47 | selfirst = text.index("sel.first") |
| 48 | sellast = text.index("sel.last") |
| 49 | if selfirst == first and sellast == last: |
| 50 | text.bell() |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 51 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 52 | except TclError: |
| 53 | pass |
| 54 | text.tag_remove("sel", "1.0", "end") |
| 55 | text.tag_add("sel", first, last) |
| 56 | text.mark_set("insert", self.engine.isback() and first or last) |
| 57 | text.see("insert") |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 58 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 59 | else: |
| 60 | text.bell() |
Kurt B. Kaiser | 0f4402d | 2002-09-18 03:10:10 +0000 | [diff] [blame] | 61 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 62 | |
| 63 | def find_selection(self, text): |
| 64 | pat = text.get("sel.first", "sel.last") |
| 65 | if pat: |
| 66 | self.engine.setcookedpat(pat) |
| 67 | return self.find_again(text) |
Terry Jan Reedy | 0a4d13e | 2014-05-27 03:30:54 -0400 | [diff] [blame] | 68 | |
| 69 | def _search_dialog(parent): |
| 70 | root = Tk() |
| 71 | root.title("Test SearchDialog") |
| 72 | width, height, x, y = list(map(int, re.split('[x+]', parent.geometry()))) |
| 73 | root.geometry("+%d+%d"%(x, y + 150)) |
| 74 | text = Text(root) |
| 75 | text.pack() |
| 76 | text.insert("insert","This is a sample string.\n"*10) |
| 77 | |
| 78 | def show_find(): |
| 79 | text.tag_add(SEL, "1.0", END) |
| 80 | s = _setup(text) |
| 81 | s.open(text) |
| 82 | text.tag_remove(SEL, "1.0", END) |
| 83 | |
| 84 | button = Button(root, text="Search", command=show_find) |
| 85 | button.pack() |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | from idlelib.idle_test.htest import run |
| 89 | run(_search_dialog) |