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