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