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