2013-08-18 19:22:43 -03:00
|
|
|
'''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
|
2014-06-26 02:40:51 -03:00
|
|
|
|
2019-01-02 23:04:06 -04:00
|
|
|
from tkinter import Toplevel
|
|
|
|
from tkinter.ttk import Frame, Entry, Label, Button, Checkbutton, Radiobutton
|
2021-04-25 07:07:58 -03:00
|
|
|
from tkinter.simpledialog import _setup_dialog
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
class SearchDialogBase:
|
2014-06-26 02:40:51 -03:00
|
|
|
'''Create most of a 3 or 4 row, 3 column search dialog.
|
2013-08-18 19:22:43 -03:00
|
|
|
|
2014-06-26 02:40:51 -03:00
|
|
|
The left and wide middle column contain:
|
|
|
|
1 or 2 labeled text entry lines (make_entry, create_entries);
|
|
|
|
a row of standard Checkbuttons (make_frame, create_option_buttons),
|
|
|
|
each of which corresponds to a search engine Variable;
|
|
|
|
a row of dialog-specific Check/Radiobuttons (create_other_buttons).
|
2013-08-18 19:27:02 -03:00
|
|
|
|
2013-08-18 19:22:43 -03:00
|
|
|
The narrow right column contains command buttons
|
2014-06-26 02:40:51 -03:00
|
|
|
(make_button, create_command_buttons).
|
2013-08-18 19:22:43 -03:00
|
|
|
These are bound to functions that execute the command.
|
|
|
|
|
2014-06-30 21:00:03 -03:00
|
|
|
Except for command buttons, this base class is not limited to items
|
|
|
|
common to all three subclasses. Rather, it is the Find dialog minus
|
|
|
|
the "Find Next" command, its execution function, and the
|
|
|
|
default_command attribute needed in create_widgets. The other
|
|
|
|
dialogs override attributes and methods, the latter to replace and
|
|
|
|
add widgets.
|
2013-08-18 19:22:43 -03:00
|
|
|
'''
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2014-06-26 02:40:51 -03:00
|
|
|
title = "Search Dialog" # replace in subclasses
|
2000-08-14 22:13:23 -03:00
|
|
|
icon = "Search"
|
2014-06-26 02:40:51 -03:00
|
|
|
needwrapbutton = 1 # not in Find in Files
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def __init__(self, root, engine):
|
2014-06-26 02:40:51 -03:00
|
|
|
'''Initialize root, engine, and top attributes.
|
|
|
|
|
|
|
|
top (level widget): set in create_widgets() called from open().
|
2020-10-25 00:14:02 -03:00
|
|
|
frame: container for all widgets in dialog.
|
2014-06-30 21:00:03 -03:00
|
|
|
text (Text searched): set in open(), only used in subclasses().
|
2014-06-26 02:40:51 -03:00
|
|
|
ent (ry): created in make_entry() called from create_entry().
|
|
|
|
row (of grid): 0 in create_widgets(), +1 in make_entry/frame().
|
2019-06-03 01:21:15 -03:00
|
|
|
default_command: set in subclasses, used in create_widgets().
|
2014-06-26 02:40:51 -03:00
|
|
|
|
|
|
|
title (of dialog): class attribute, override in subclasses.
|
|
|
|
icon (of dialog): ditto, use unclear if cannot minimize dialog.
|
|
|
|
'''
|
2000-08-14 22:13:23 -03:00
|
|
|
self.root = root
|
2018-12-28 03:41:35 -04:00
|
|
|
self.bell = root.bell
|
2000-08-14 22:13:23 -03:00
|
|
|
self.engine = engine
|
|
|
|
self.top = None
|
|
|
|
|
2002-11-05 22:18:45 -04:00
|
|
|
def open(self, text, searchphrase=None):
|
2014-06-26 02:40:51 -03:00
|
|
|
"Make dialog visible on top of others and ready to use."
|
2000-08-14 22:13:23 -03:00
|
|
|
self.text = text
|
|
|
|
if not self.top:
|
|
|
|
self.create_widgets()
|
|
|
|
else:
|
|
|
|
self.top.deiconify()
|
|
|
|
self.top.tkraise()
|
2019-06-07 02:54:40 -03:00
|
|
|
self.top.transient(text.winfo_toplevel())
|
2002-11-05 22:18:45 -04:00
|
|
|
if searchphrase:
|
|
|
|
self.ent.delete(0,"end")
|
|
|
|
self.ent.insert("end",searchphrase)
|
2000-08-14 22:13:23 -03:00
|
|
|
self.ent.focus_set()
|
|
|
|
self.ent.selection_range(0, "end")
|
|
|
|
self.ent.icursor(0)
|
|
|
|
self.top.grab_set()
|
|
|
|
|
|
|
|
def close(self, event=None):
|
2014-06-26 02:40:51 -03:00
|
|
|
"Put dialog away for later use."
|
2000-08-14 22:13:23 -03:00
|
|
|
if self.top:
|
|
|
|
self.top.grab_release()
|
2019-06-07 02:54:40 -03:00
|
|
|
self.top.transient('')
|
2000-08-14 22:13:23 -03:00
|
|
|
self.top.withdraw()
|
|
|
|
|
|
|
|
def create_widgets(self):
|
2014-06-26 02:40:51 -03:00
|
|
|
'''Create basic 3 row x 3 col search (find) dialog.
|
|
|
|
|
|
|
|
Other dialogs override subsidiary create_x methods as needed.
|
|
|
|
Replace and Find-in-Files add another entry row.
|
|
|
|
'''
|
2000-08-14 22:13:23 -03:00
|
|
|
top = Toplevel(self.root)
|
|
|
|
top.bind("<Return>", self.default_command)
|
|
|
|
top.bind("<Escape>", self.close)
|
|
|
|
top.protocol("WM_DELETE_WINDOW", self.close)
|
|
|
|
top.wm_title(self.title)
|
|
|
|
top.wm_iconname(self.icon)
|
2021-04-25 07:07:58 -03:00
|
|
|
_setup_dialog(top)
|
2000-08-14 22:13:23 -03:00
|
|
|
self.top = top
|
2024-08-06 13:45:53 -03:00
|
|
|
self.frame = Frame(top, padding=5)
|
2020-10-25 00:14:02 -03:00
|
|
|
self.frame.grid(sticky="nwes")
|
|
|
|
top.grid_columnconfigure(0, weight=100)
|
|
|
|
top.grid_rowconfigure(0, weight=100)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
self.row = 0
|
2020-10-25 00:14:02 -03:00
|
|
|
self.frame.grid_columnconfigure(0, pad=2, weight=0)
|
|
|
|
self.frame.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2014-06-26 02:40:51 -03:00
|
|
|
self.create_entries() # row 0 (and maybe 1), cols 0, 1
|
|
|
|
self.create_option_buttons() # next row, cols 0, 1
|
|
|
|
self.create_other_buttons() # next row, cols 0, 1
|
|
|
|
self.create_command_buttons() # col 2, all rows
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2014-07-13 18:27:26 -03:00
|
|
|
def make_entry(self, label_text, var):
|
|
|
|
'''Return (entry, label), .
|
|
|
|
|
|
|
|
entry - gridded labeled Entry for text entry.
|
|
|
|
label - Label widget, returned for testing.
|
|
|
|
'''
|
2020-10-25 00:14:02 -03:00
|
|
|
label = Label(self.frame, text=label_text)
|
2014-07-13 18:27:26 -03:00
|
|
|
label.grid(row=self.row, column=0, sticky="nw")
|
2020-10-25 00:14:02 -03:00
|
|
|
entry = Entry(self.frame, textvariable=var, exportselection=0)
|
2014-07-13 18:27:26 -03:00
|
|
|
entry.grid(row=self.row, column=1, sticky="nwe")
|
2000-08-14 22:13:23 -03:00
|
|
|
self.row = self.row + 1
|
2014-07-13 18:27:26 -03:00
|
|
|
return entry, label
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2014-06-26 02:40:51 -03:00
|
|
|
def create_entries(self):
|
|
|
|
"Create one or more entry lines with make_entry."
|
2014-07-13 18:27:26 -03:00
|
|
|
self.ent = self.make_entry("Find:", self.engine.patvar)[0]
|
2014-06-26 02:40:51 -03:00
|
|
|
|
2002-11-04 19:07:51 -04:00
|
|
|
def make_frame(self,labeltext=None):
|
2014-07-13 18:27:26 -03:00
|
|
|
'''Return (frame, label).
|
|
|
|
|
|
|
|
frame - gridded labeled Frame for option or other buttons.
|
|
|
|
label - Label widget, returned for testing.
|
|
|
|
'''
|
2002-11-04 19:07:51 -04:00
|
|
|
if labeltext:
|
2020-10-25 00:14:02 -03:00
|
|
|
label = Label(self.frame, text=labeltext)
|
2014-07-01 00:52:20 -03:00
|
|
|
label.grid(row=self.row, column=0, sticky="nw")
|
2014-06-30 21:00:03 -03:00
|
|
|
else:
|
2014-07-01 00:52:20 -03:00
|
|
|
label = ''
|
2020-10-25 00:14:02 -03:00
|
|
|
frame = Frame(self.frame)
|
2014-07-01 00:52:20 -03:00
|
|
|
frame.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
|
2000-08-14 22:13:23 -03:00
|
|
|
self.row = self.row + 1
|
2014-07-13 18:27:26 -03:00
|
|
|
return frame, label
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def create_option_buttons(self):
|
2014-07-13 18:27:26 -03:00
|
|
|
'''Return (filled frame, options) for testing.
|
|
|
|
|
2016-05-28 14:22:31 -03:00
|
|
|
Options is a list of searchengine booleanvar, label pairs.
|
2014-07-13 18:27:26 -03:00
|
|
|
A gridded frame from make_frame is filled with a Checkbutton
|
|
|
|
for each pair, bound to the var, with the corresponding label.
|
|
|
|
'''
|
2014-07-01 00:52:20 -03:00
|
|
|
frame = self.make_frame("Options")[0]
|
|
|
|
engine = self.engine
|
|
|
|
options = [(engine.revar, "Regular expression"),
|
|
|
|
(engine.casevar, "Match case"),
|
|
|
|
(engine.wordvar, "Whole word")]
|
2000-08-14 22:13:23 -03:00
|
|
|
if self.needwrapbutton:
|
2014-07-01 00:52:20 -03:00
|
|
|
options.append((engine.wrapvar, "Wrap around"))
|
|
|
|
for var, label in options:
|
2016-07-10 21:21:31 -03:00
|
|
|
btn = Checkbutton(frame, variable=var, text=label)
|
2000-08-14 22:13:23 -03:00
|
|
|
btn.pack(side="left", fill="both")
|
2014-07-13 18:27:26 -03:00
|
|
|
return frame, options
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def create_other_buttons(self):
|
2014-07-13 18:27:26 -03:00
|
|
|
'''Return (frame, others) for testing.
|
|
|
|
|
|
|
|
Others is a list of value, label pairs.
|
|
|
|
A gridded frame from make_frame is filled with radio buttons.
|
|
|
|
'''
|
2014-07-01 00:52:20 -03:00
|
|
|
frame = self.make_frame("Direction")[0]
|
|
|
|
var = self.engine.backvar
|
|
|
|
others = [(1, 'Up'), (0, 'Down')]
|
|
|
|
for val, label in others:
|
2016-07-10 21:21:31 -03:00
|
|
|
btn = Radiobutton(frame, variable=var, value=val, text=label)
|
2014-07-01 00:52:20 -03:00
|
|
|
btn.pack(side="left", fill="both")
|
2014-07-13 18:27:26 -03:00
|
|
|
return frame, others
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2014-06-26 02:40:51 -03:00
|
|
|
def make_button(self, label, command, isdef=0):
|
|
|
|
"Return command button gridded in command frame."
|
|
|
|
b = Button(self.buttonframe,
|
|
|
|
text=label, command=command,
|
|
|
|
default=isdef and "active" or "normal")
|
|
|
|
cols,rows=self.buttonframe.grid_size()
|
|
|
|
b.grid(pady=1,row=rows,column=0,sticky="ew")
|
|
|
|
self.buttonframe.grid(rowspan=rows+1)
|
|
|
|
return b
|
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
def create_command_buttons(self):
|
2014-06-26 02:40:51 -03:00
|
|
|
"Place buttons in vertical command frame gridded on right."
|
2020-10-25 00:14:02 -03:00
|
|
|
f = self.buttonframe = Frame(self.frame)
|
2002-11-20 23:02:17 -04:00
|
|
|
f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
|
2002-11-04 19:07:51 -04:00
|
|
|
|
2019-05-31 05:26:35 -03:00
|
|
|
b = self.make_button("Close", self.close)
|
2000-08-14 22:13:23 -03:00
|
|
|
b.lower()
|
2014-06-26 02:40:51 -03:00
|
|
|
|
2016-07-10 21:21:31 -03:00
|
|
|
|
|
|
|
class _searchbase(SearchDialogBase): # htest #
|
|
|
|
"Create auto-opening dialog with no text connection."
|
|
|
|
|
|
|
|
def __init__(self, parent):
|
|
|
|
import re
|
|
|
|
from idlelib import searchengine
|
|
|
|
|
|
|
|
self.root = parent
|
|
|
|
self.engine = searchengine.get(parent)
|
|
|
|
self.create_widgets()
|
|
|
|
print(parent.geometry())
|
|
|
|
width,height, x,y = list(map(int, re.split('[x+]', parent.geometry())))
|
|
|
|
self.top.geometry("+%d+%d" % (x + 40, y + 175))
|
|
|
|
|
2016-08-11 00:44:54 -03:00
|
|
|
def default_command(self, dummy): pass
|
2016-07-10 21:21:31 -03:00
|
|
|
|
2018-06-19 20:12:52 -03:00
|
|
|
|
2014-06-26 02:40:51 -03:00
|
|
|
if __name__ == '__main__':
|
2018-06-19 20:12:52 -03:00
|
|
|
from unittest import main
|
|
|
|
main('idlelib.idle_test.test_searchbase', verbosity=2, exit=False)
|
2016-07-10 21:21:31 -03:00
|
|
|
|
|
|
|
from idlelib.idle_test.htest import run
|
|
|
|
run(_searchbase)
|