| 1 | n/a | from tkinter import TclError |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from idlelib import searchengine |
|---|
| 4 | n/a | from idlelib.searchbase import SearchDialogBase |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | def _setup(text): |
|---|
| 7 | n/a | "Create or find the singleton SearchDialog instance." |
|---|
| 8 | n/a | root = text._root() |
|---|
| 9 | n/a | engine = searchengine.get(root) |
|---|
| 10 | n/a | if not hasattr(engine, "_searchdialog"): |
|---|
| 11 | n/a | engine._searchdialog = SearchDialog(root, engine) |
|---|
| 12 | n/a | return engine._searchdialog |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | def find(text): |
|---|
| 15 | n/a | "Handle the editor edit menu item and corresponding event." |
|---|
| 16 | n/a | pat = text.get("sel.first", "sel.last") |
|---|
| 17 | n/a | return _setup(text).open(text, pat) # Open is inherited from SDBase. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def find_again(text): |
|---|
| 20 | n/a | "Handle the editor edit menu item and corresponding event." |
|---|
| 21 | n/a | return _setup(text).find_again(text) |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | def find_selection(text): |
|---|
| 24 | n/a | "Handle the editor edit menu item and corresponding event." |
|---|
| 25 | n/a | return _setup(text).find_selection(text) |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | class SearchDialog(SearchDialogBase): |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def create_widgets(self): |
|---|
| 31 | n/a | SearchDialogBase.create_widgets(self) |
|---|
| 32 | n/a | self.make_button("Find Next", self.default_command, 1) |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | def default_command(self, event=None): |
|---|
| 35 | n/a | if not self.engine.getprog(): |
|---|
| 36 | n/a | return |
|---|
| 37 | n/a | self.find_again(self.text) |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def find_again(self, text): |
|---|
| 40 | n/a | if not self.engine.getpat(): |
|---|
| 41 | n/a | self.open(text) |
|---|
| 42 | n/a | return False |
|---|
| 43 | n/a | if not self.engine.getprog(): |
|---|
| 44 | n/a | return False |
|---|
| 45 | n/a | res = self.engine.search_text(text) |
|---|
| 46 | n/a | if res: |
|---|
| 47 | n/a | line, m = res |
|---|
| 48 | n/a | i, j = m.span() |
|---|
| 49 | n/a | first = "%d.%d" % (line, i) |
|---|
| 50 | n/a | last = "%d.%d" % (line, j) |
|---|
| 51 | n/a | try: |
|---|
| 52 | n/a | selfirst = text.index("sel.first") |
|---|
| 53 | n/a | sellast = text.index("sel.last") |
|---|
| 54 | n/a | if selfirst == first and sellast == last: |
|---|
| 55 | n/a | self.bell() |
|---|
| 56 | n/a | return False |
|---|
| 57 | n/a | except TclError: |
|---|
| 58 | n/a | pass |
|---|
| 59 | n/a | text.tag_remove("sel", "1.0", "end") |
|---|
| 60 | n/a | text.tag_add("sel", first, last) |
|---|
| 61 | n/a | text.mark_set("insert", self.engine.isback() and first or last) |
|---|
| 62 | n/a | text.see("insert") |
|---|
| 63 | n/a | return True |
|---|
| 64 | n/a | else: |
|---|
| 65 | n/a | self.bell() |
|---|
| 66 | n/a | return False |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def find_selection(self, text): |
|---|
| 69 | n/a | pat = text.get("sel.first", "sel.last") |
|---|
| 70 | n/a | if pat: |
|---|
| 71 | n/a | self.engine.setcookedpat(pat) |
|---|
| 72 | n/a | return self.find_again(text) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | def _search_dialog(parent): # htest # |
|---|
| 76 | n/a | "Display search test box." |
|---|
| 77 | n/a | from tkinter import Toplevel, Text |
|---|
| 78 | n/a | from tkinter.ttk import Button |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | box = Toplevel(parent) |
|---|
| 81 | n/a | box.title("Test SearchDialog") |
|---|
| 82 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
|---|
| 83 | n/a | box.geometry("+%d+%d" % (x, y + 175)) |
|---|
| 84 | n/a | text = Text(box, inactiveselectbackground='gray') |
|---|
| 85 | n/a | text.pack() |
|---|
| 86 | n/a | text.insert("insert","This is a sample string.\n"*5) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | def show_find(): |
|---|
| 89 | n/a | text.tag_add('sel', '1.0', 'end') |
|---|
| 90 | n/a | _setup(text).open(text) |
|---|
| 91 | n/a | text.tag_remove('sel', '1.0', 'end') |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | button = Button(box, text="Search (selection ignored)", command=show_find) |
|---|
| 94 | n/a | button.pack() |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | if __name__ == '__main__': |
|---|
| 97 | n/a | import unittest |
|---|
| 98 | n/a | unittest.main('idlelib.idle_test.test_search', |
|---|
| 99 | n/a | verbosity=2, exit=False) |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | from idlelib.idle_test.htest import run |
|---|
| 102 | n/a | run(_search_dialog) |
|---|