Debian Codegraph @

cpython27/Lib/idlelib/SearchDialogBase.py

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