Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | import SearchEngine |
| 3 | from SearchDialogBase import SearchDialogBase |
| 4 | |
| 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): |
| 14 | return _setup(text).open(text) |
| 15 | |
| 16 | def find_again(text): |
| 17 | return _setup(text).find_again(text) |
| 18 | |
| 19 | def find_selection(text): |
| 20 | return _setup(text).find_selection(text) |
| 21 | |
| 22 | class SearchDialog(SearchDialogBase): |
| 23 | |
| 24 | def create_widgets(self): |
| 25 | f = SearchDialogBase.create_widgets(self) |
| 26 | self.make_button("Find", self.default_command, 1) |
| 27 | |
| 28 | def default_command(self, event=None): |
| 29 | if not self.engine.getprog(): |
| 30 | return |
| 31 | if self.find_again(self.text): |
| 32 | self.close() |
| 33 | |
| 34 | def find_again(self, text): |
| 35 | if not self.engine.getpat(): |
| 36 | self.open(text) |
| 37 | return 0 |
| 38 | if not self.engine.getprog(): |
| 39 | return 0 |
| 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) |
Guido van Rossum | 959c937 | 1999-01-28 19:04:01 +0000 | [diff] [blame^] | 46 | try: |
| 47 | selfirst = text.index("sel.first") |
| 48 | sellast = text.index("sel.last") |
| 49 | if selfirst == first and sellast == last: |
| 50 | text.bell() |
| 51 | return 0 |
| 52 | except TclError: |
| 53 | pass |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 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") |
| 58 | return 1 |
| 59 | else: |
| 60 | text.bell() |
| 61 | return 0 |
| 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) |