2016-07-10 21:21:31 -03:00
|
|
|
from tkinter import TclError
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib import searchengine
|
|
|
|
from idlelib.searchbase import SearchDialogBase
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def _setup(text):
|
2016-05-17 19:18:37 -03:00
|
|
|
"Create or find the singleton SearchDialog instance."
|
2000-08-14 22:13:23 -03:00
|
|
|
root = text._root()
|
2016-05-28 14:22:31 -03:00
|
|
|
engine = searchengine.get(root)
|
2000-08-14 22:13:23 -03:00
|
|
|
if not hasattr(engine, "_searchdialog"):
|
|
|
|
engine._searchdialog = SearchDialog(root, engine)
|
|
|
|
return engine._searchdialog
|
|
|
|
|
|
|
|
def find(text):
|
2016-05-17 19:18:37 -03:00
|
|
|
"Handle the editor edit menu item and corresponding event."
|
2002-11-05 22:18:45 -04:00
|
|
|
pat = text.get("sel.first", "sel.last")
|
2016-05-17 19:18:37 -03:00
|
|
|
return _setup(text).open(text, pat) # Open is inherited from SDBase.
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def find_again(text):
|
2016-05-17 19:18:37 -03:00
|
|
|
"Handle the editor edit menu item and corresponding event."
|
2000-08-14 22:13:23 -03:00
|
|
|
return _setup(text).find_again(text)
|
|
|
|
|
|
|
|
def find_selection(text):
|
2016-05-17 19:18:37 -03:00
|
|
|
"Handle the editor edit menu item and corresponding event."
|
2000-08-14 22:13:23 -03:00
|
|
|
return _setup(text).find_selection(text)
|
|
|
|
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
class SearchDialog(SearchDialogBase):
|
|
|
|
|
|
|
|
def create_widgets(self):
|
2015-05-14 19:10:50 -03:00
|
|
|
SearchDialogBase.create_widgets(self)
|
2013-06-11 01:01:20 -03:00
|
|
|
self.make_button("Find Next", self.default_command, 1)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def default_command(self, event=None):
|
|
|
|
if not self.engine.getprog():
|
|
|
|
return
|
2013-06-11 01:01:20 -03:00
|
|
|
self.find_again(self.text)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def find_again(self, text):
|
|
|
|
if not self.engine.getpat():
|
|
|
|
self.open(text)
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03:00
|
|
|
if not self.engine.getprog():
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03:00
|
|
|
res = self.engine.search_text(text)
|
|
|
|
if res:
|
|
|
|
line, m = res
|
|
|
|
i, j = m.span()
|
|
|
|
first = "%d.%d" % (line, i)
|
|
|
|
last = "%d.%d" % (line, j)
|
|
|
|
try:
|
|
|
|
selfirst = text.index("sel.first")
|
|
|
|
sellast = text.index("sel.last")
|
|
|
|
if selfirst == first and sellast == last:
|
2016-08-11 00:44:54 -03:00
|
|
|
self.bell()
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03:00
|
|
|
except TclError:
|
|
|
|
pass
|
|
|
|
text.tag_remove("sel", "1.0", "end")
|
|
|
|
text.tag_add("sel", first, last)
|
|
|
|
text.mark_set("insert", self.engine.isback() and first or last)
|
|
|
|
text.see("insert")
|
2002-09-18 00:10:10 -03:00
|
|
|
return True
|
2000-08-14 22:13:23 -03:00
|
|
|
else:
|
2016-08-11 00:44:54 -03:00
|
|
|
self.bell()
|
2002-09-18 00:10:10 -03:00
|
|
|
return False
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def find_selection(self, text):
|
|
|
|
pat = text.get("sel.first", "sel.last")
|
|
|
|
if pat:
|
|
|
|
self.engine.setcookedpat(pat)
|
|
|
|
return self.find_again(text)
|
2014-05-27 04:30:54 -03:00
|
|
|
|
2016-05-17 19:18:37 -03:00
|
|
|
|
|
|
|
def _search_dialog(parent): # htest #
|
2016-07-10 21:21:31 -03:00
|
|
|
"Display search test box."
|
|
|
|
from tkinter import Toplevel, Text
|
|
|
|
from tkinter.ttk import Button
|
|
|
|
|
2016-05-17 19:18:37 -03:00
|
|
|
box = Toplevel(parent)
|
|
|
|
box.title("Test SearchDialog")
|
2016-07-10 18:28:10 -03:00
|
|
|
x, y = map(int, parent.geometry().split('+')[1:])
|
|
|
|
box.geometry("+%d+%d" % (x, y + 175))
|
2016-05-17 19:18:37 -03:00
|
|
|
text = Text(box, inactiveselectbackground='gray')
|
2014-05-27 04:30:54 -03:00
|
|
|
text.pack()
|
2016-05-17 19:18:37 -03:00
|
|
|
text.insert("insert","This is a sample string.\n"*5)
|
2014-05-27 04:30:54 -03:00
|
|
|
|
|
|
|
def show_find():
|
2016-07-10 21:21:31 -03:00
|
|
|
text.tag_add('sel', '1.0', 'end')
|
2016-05-17 19:18:37 -03:00
|
|
|
_setup(text).open(text)
|
2016-07-10 21:21:31 -03:00
|
|
|
text.tag_remove('sel', '1.0', 'end')
|
2014-05-27 04:30:54 -03:00
|
|
|
|
2016-05-17 19:18:37 -03:00
|
|
|
button = Button(box, text="Search (selection ignored)", command=show_find)
|
2014-05-27 04:30:54 -03:00
|
|
|
button.pack()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-05-17 19:18:37 -03:00
|
|
|
import unittest
|
2016-07-10 18:28:10 -03:00
|
|
|
unittest.main('idlelib.idle_test.test_search',
|
2016-05-17 19:18:37 -03:00
|
|
|
verbosity=2, exit=False)
|
2016-07-10 18:28:10 -03:00
|
|
|
|
2014-05-27 04:30:54 -03:00
|
|
|
from idlelib.idle_test.htest import run
|
|
|
|
run(_search_dialog)
|