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