annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/searchbase.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 '''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
jpayne@69 2
jpayne@69 3 from tkinter import Toplevel
jpayne@69 4 from tkinter.ttk import Frame, Entry, Label, Button, Checkbutton, Radiobutton
jpayne@69 5
jpayne@69 6
jpayne@69 7 class SearchDialogBase:
jpayne@69 8 '''Create most of a 3 or 4 row, 3 column search dialog.
jpayne@69 9
jpayne@69 10 The left and wide middle column contain:
jpayne@69 11 1 or 2 labeled text entry lines (make_entry, create_entries);
jpayne@69 12 a row of standard Checkbuttons (make_frame, create_option_buttons),
jpayne@69 13 each of which corresponds to a search engine Variable;
jpayne@69 14 a row of dialog-specific Check/Radiobuttons (create_other_buttons).
jpayne@69 15
jpayne@69 16 The narrow right column contains command buttons
jpayne@69 17 (make_button, create_command_buttons).
jpayne@69 18 These are bound to functions that execute the command.
jpayne@69 19
jpayne@69 20 Except for command buttons, this base class is not limited to items
jpayne@69 21 common to all three subclasses. Rather, it is the Find dialog minus
jpayne@69 22 the "Find Next" command, its execution function, and the
jpayne@69 23 default_command attribute needed in create_widgets. The other
jpayne@69 24 dialogs override attributes and methods, the latter to replace and
jpayne@69 25 add widgets.
jpayne@69 26 '''
jpayne@69 27
jpayne@69 28 title = "Search Dialog" # replace in subclasses
jpayne@69 29 icon = "Search"
jpayne@69 30 needwrapbutton = 1 # not in Find in Files
jpayne@69 31
jpayne@69 32 def __init__(self, root, engine):
jpayne@69 33 '''Initialize root, engine, and top attributes.
jpayne@69 34
jpayne@69 35 top (level widget): set in create_widgets() called from open().
jpayne@69 36 text (Text searched): set in open(), only used in subclasses().
jpayne@69 37 ent (ry): created in make_entry() called from create_entry().
jpayne@69 38 row (of grid): 0 in create_widgets(), +1 in make_entry/frame().
jpayne@69 39 default_command: set in subclasses, used in create_widgets().
jpayne@69 40
jpayne@69 41 title (of dialog): class attribute, override in subclasses.
jpayne@69 42 icon (of dialog): ditto, use unclear if cannot minimize dialog.
jpayne@69 43 '''
jpayne@69 44 self.root = root
jpayne@69 45 self.bell = root.bell
jpayne@69 46 self.engine = engine
jpayne@69 47 self.top = None
jpayne@69 48
jpayne@69 49 def open(self, text, searchphrase=None):
jpayne@69 50 "Make dialog visible on top of others and ready to use."
jpayne@69 51 self.text = text
jpayne@69 52 if not self.top:
jpayne@69 53 self.create_widgets()
jpayne@69 54 else:
jpayne@69 55 self.top.deiconify()
jpayne@69 56 self.top.tkraise()
jpayne@69 57 self.top.transient(text.winfo_toplevel())
jpayne@69 58 if searchphrase:
jpayne@69 59 self.ent.delete(0,"end")
jpayne@69 60 self.ent.insert("end",searchphrase)
jpayne@69 61 self.ent.focus_set()
jpayne@69 62 self.ent.selection_range(0, "end")
jpayne@69 63 self.ent.icursor(0)
jpayne@69 64 self.top.grab_set()
jpayne@69 65
jpayne@69 66 def close(self, event=None):
jpayne@69 67 "Put dialog away for later use."
jpayne@69 68 if self.top:
jpayne@69 69 self.top.grab_release()
jpayne@69 70 self.top.transient('')
jpayne@69 71 self.top.withdraw()
jpayne@69 72
jpayne@69 73 def create_widgets(self):
jpayne@69 74 '''Create basic 3 row x 3 col search (find) dialog.
jpayne@69 75
jpayne@69 76 Other dialogs override subsidiary create_x methods as needed.
jpayne@69 77 Replace and Find-in-Files add another entry row.
jpayne@69 78 '''
jpayne@69 79 top = Toplevel(self.root)
jpayne@69 80 top.bind("<Return>", self.default_command)
jpayne@69 81 top.bind("<Escape>", self.close)
jpayne@69 82 top.protocol("WM_DELETE_WINDOW", self.close)
jpayne@69 83 top.wm_title(self.title)
jpayne@69 84 top.wm_iconname(self.icon)
jpayne@69 85 self.top = top
jpayne@69 86
jpayne@69 87 self.row = 0
jpayne@69 88 self.top.grid_columnconfigure(0, pad=2, weight=0)
jpayne@69 89 self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
jpayne@69 90
jpayne@69 91 self.create_entries() # row 0 (and maybe 1), cols 0, 1
jpayne@69 92 self.create_option_buttons() # next row, cols 0, 1
jpayne@69 93 self.create_other_buttons() # next row, cols 0, 1
jpayne@69 94 self.create_command_buttons() # col 2, all rows
jpayne@69 95
jpayne@69 96 def make_entry(self, label_text, var):
jpayne@69 97 '''Return (entry, label), .
jpayne@69 98
jpayne@69 99 entry - gridded labeled Entry for text entry.
jpayne@69 100 label - Label widget, returned for testing.
jpayne@69 101 '''
jpayne@69 102 label = Label(self.top, text=label_text)
jpayne@69 103 label.grid(row=self.row, column=0, sticky="nw")
jpayne@69 104 entry = Entry(self.top, textvariable=var, exportselection=0)
jpayne@69 105 entry.grid(row=self.row, column=1, sticky="nwe")
jpayne@69 106 self.row = self.row + 1
jpayne@69 107 return entry, label
jpayne@69 108
jpayne@69 109 def create_entries(self):
jpayne@69 110 "Create one or more entry lines with make_entry."
jpayne@69 111 self.ent = self.make_entry("Find:", self.engine.patvar)[0]
jpayne@69 112
jpayne@69 113 def make_frame(self,labeltext=None):
jpayne@69 114 '''Return (frame, label).
jpayne@69 115
jpayne@69 116 frame - gridded labeled Frame for option or other buttons.
jpayne@69 117 label - Label widget, returned for testing.
jpayne@69 118 '''
jpayne@69 119 if labeltext:
jpayne@69 120 label = Label(self.top, text=labeltext)
jpayne@69 121 label.grid(row=self.row, column=0, sticky="nw")
jpayne@69 122 else:
jpayne@69 123 label = ''
jpayne@69 124 frame = Frame(self.top)
jpayne@69 125 frame.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
jpayne@69 126 self.row = self.row + 1
jpayne@69 127 return frame, label
jpayne@69 128
jpayne@69 129 def create_option_buttons(self):
jpayne@69 130 '''Return (filled frame, options) for testing.
jpayne@69 131
jpayne@69 132 Options is a list of searchengine booleanvar, label pairs.
jpayne@69 133 A gridded frame from make_frame is filled with a Checkbutton
jpayne@69 134 for each pair, bound to the var, with the corresponding label.
jpayne@69 135 '''
jpayne@69 136 frame = self.make_frame("Options")[0]
jpayne@69 137 engine = self.engine
jpayne@69 138 options = [(engine.revar, "Regular expression"),
jpayne@69 139 (engine.casevar, "Match case"),
jpayne@69 140 (engine.wordvar, "Whole word")]
jpayne@69 141 if self.needwrapbutton:
jpayne@69 142 options.append((engine.wrapvar, "Wrap around"))
jpayne@69 143 for var, label in options:
jpayne@69 144 btn = Checkbutton(frame, variable=var, text=label)
jpayne@69 145 btn.pack(side="left", fill="both")
jpayne@69 146 return frame, options
jpayne@69 147
jpayne@69 148 def create_other_buttons(self):
jpayne@69 149 '''Return (frame, others) for testing.
jpayne@69 150
jpayne@69 151 Others is a list of value, label pairs.
jpayne@69 152 A gridded frame from make_frame is filled with radio buttons.
jpayne@69 153 '''
jpayne@69 154 frame = self.make_frame("Direction")[0]
jpayne@69 155 var = self.engine.backvar
jpayne@69 156 others = [(1, 'Up'), (0, 'Down')]
jpayne@69 157 for val, label in others:
jpayne@69 158 btn = Radiobutton(frame, variable=var, value=val, text=label)
jpayne@69 159 btn.pack(side="left", fill="both")
jpayne@69 160 return frame, others
jpayne@69 161
jpayne@69 162 def make_button(self, label, command, isdef=0):
jpayne@69 163 "Return command button gridded in command frame."
jpayne@69 164 b = Button(self.buttonframe,
jpayne@69 165 text=label, command=command,
jpayne@69 166 default=isdef and "active" or "normal")
jpayne@69 167 cols,rows=self.buttonframe.grid_size()
jpayne@69 168 b.grid(pady=1,row=rows,column=0,sticky="ew")
jpayne@69 169 self.buttonframe.grid(rowspan=rows+1)
jpayne@69 170 return b
jpayne@69 171
jpayne@69 172 def create_command_buttons(self):
jpayne@69 173 "Place buttons in vertical command frame gridded on right."
jpayne@69 174 f = self.buttonframe = Frame(self.top)
jpayne@69 175 f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
jpayne@69 176
jpayne@69 177 b = self.make_button("Close", self.close)
jpayne@69 178 b.lower()
jpayne@69 179
jpayne@69 180
jpayne@69 181 class _searchbase(SearchDialogBase): # htest #
jpayne@69 182 "Create auto-opening dialog with no text connection."
jpayne@69 183
jpayne@69 184 def __init__(self, parent):
jpayne@69 185 import re
jpayne@69 186 from idlelib import searchengine
jpayne@69 187
jpayne@69 188 self.root = parent
jpayne@69 189 self.engine = searchengine.get(parent)
jpayne@69 190 self.create_widgets()
jpayne@69 191 print(parent.geometry())
jpayne@69 192 width,height, x,y = list(map(int, re.split('[x+]', parent.geometry())))
jpayne@69 193 self.top.geometry("+%d+%d" % (x + 40, y + 175))
jpayne@69 194
jpayne@69 195 def default_command(self, dummy): pass
jpayne@69 196
jpayne@69 197
jpayne@69 198 if __name__ == '__main__':
jpayne@69 199 from unittest import main
jpayne@69 200 main('idlelib.idle_test.test_searchbase', verbosity=2, exit=False)
jpayne@69 201
jpayne@69 202 from idlelib.idle_test.htest import run
jpayne@69 203 run(_searchbase)