blob: 77ef7b9a82c382f4c772f1e95162f1f60c2d2144 [file] [log] [blame]
Georg Brandl14fc4272008-05-17 18:39:55 +00001from tkinter import *
David Scherer7aced172000-08-15 01:13:23 +00002
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +00003from idlelib import SearchEngine
4from idlelib.SearchDialogBase import SearchDialogBase
David Scherer7aced172000-08-15 01:13:23 +00005
6def _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
13def find(text):
Chui Tey5a231c8f2002-11-06 02:18:45 +000014 pat = text.get("sel.first", "sel.last")
15 return _setup(text).open(text,pat)
David Scherer7aced172000-08-15 01:13:23 +000016
17def find_again(text):
18 return _setup(text).find_again(text)
19
20def find_selection(text):
21 return _setup(text).find_selection(text)
22
23class SearchDialog(SearchDialogBase):
24
25 def create_widgets(self):
Terry Jan Reedy27336182015-05-14 18:10:50 -040026 SearchDialogBase.create_widgets(self)
Roger Serwy391f4692013-06-10 23:01:20 -050027 self.make_button("Find Next", self.default_command, 1)
David Scherer7aced172000-08-15 01:13:23 +000028
29 def default_command(self, event=None):
30 if not self.engine.getprog():
31 return
Roger Serwy391f4692013-06-10 23:01:20 -050032 self.find_again(self.text)
David Scherer7aced172000-08-15 01:13:23 +000033
34 def find_again(self, text):
35 if not self.engine.getpat():
36 self.open(text)
Kurt B. Kaiser0f4402d2002-09-18 03:10:10 +000037 return False
David Scherer7aced172000-08-15 01:13:23 +000038 if not self.engine.getprog():
Kurt B. Kaiser0f4402d2002-09-18 03:10:10 +000039 return False
David Scherer7aced172000-08-15 01:13:23 +000040 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. Kaiser0f4402d2002-09-18 03:10:10 +000051 return False
David Scherer7aced172000-08-15 01:13:23 +000052 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. Kaiser0f4402d2002-09-18 03:10:10 +000058 return True
David Scherer7aced172000-08-15 01:13:23 +000059 else:
60 text.bell()
Kurt B. Kaiser0f4402d2002-09-18 03:10:10 +000061 return False
David Scherer7aced172000-08-15 01:13:23 +000062
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 Reedy0a4d13e2014-05-27 03:30:54 -040068
69def _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
87if __name__ == '__main__':
88 from idlelib.idle_test.htest import run
89 run(_search_dialog)