blob: bd1717a5af52c54cf4a422c26616703cbfb09294 [file] [log] [blame]
Terry Jan Reedy9946a282013-08-18 18:22:34 -04001'''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
Georg Brandl6634bf22008-05-20 07:13:37 +00002from Tkinter import *
David Scherer7aced172000-08-15 01:13:23 +00003
4class SearchDialogBase:
Terry Jan Reedy9946a282013-08-18 18:22:34 -04005 '''Create most of a modal search dialog (make_frame, create_widgets).
6
7 The wide left column contains:
8 1 or 2 text entry lines (create_entries, make_entry);
9 a row of standard radiobuttons (create_option_buttons);
10 a row of dialog specific radiobuttons (create_other_buttons).
11
12 The narrow right column contains command buttons
13 (create_command_buttons, make_button).
14 These are bound to functions that execute the command.
15
16 Except for command buttons, this base class is not limited to
17 items common to all three subclasses. Rather, it is the Find dialog
18 minus the "Find Next" command and its execution function.
19 The other dialogs override methods to replace and add widgets.
20 '''
David Scherer7aced172000-08-15 01:13:23 +000021
22 title = "Search Dialog"
23 icon = "Search"
24 needwrapbutton = 1
25
26 def __init__(self, root, engine):
27 self.root = root
28 self.engine = engine
29 self.top = None
30
Chui Tey5a231c82002-11-06 02:18:45 +000031 def open(self, text, searchphrase=None):
David Scherer7aced172000-08-15 01:13:23 +000032 self.text = text
33 if not self.top:
34 self.create_widgets()
35 else:
36 self.top.deiconify()
37 self.top.tkraise()
Chui Tey5a231c82002-11-06 02:18:45 +000038 if searchphrase:
39 self.ent.delete(0,"end")
40 self.ent.insert("end",searchphrase)
David Scherer7aced172000-08-15 01:13:23 +000041 self.ent.focus_set()
42 self.ent.selection_range(0, "end")
43 self.ent.icursor(0)
44 self.top.grab_set()
45
46 def close(self, event=None):
47 if self.top:
48 self.top.grab_release()
49 self.top.withdraw()
50
51 def create_widgets(self):
52 top = Toplevel(self.root)
53 top.bind("<Return>", self.default_command)
54 top.bind("<Escape>", self.close)
55 top.protocol("WM_DELETE_WINDOW", self.close)
56 top.wm_title(self.title)
57 top.wm_iconname(self.icon)
58 self.top = top
59
60 self.row = 0
Chui Tey72a8a3b2002-11-04 23:07:51 +000061 self.top.grid_columnconfigure(0, pad=2, weight=0)
62 self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
David Scherer7aced172000-08-15 01:13:23 +000063
64 self.create_entries()
65 self.create_option_buttons()
66 self.create_other_buttons()
67 return self.create_command_buttons()
68
69 def make_entry(self, label, var):
70 l = Label(self.top, text=label)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000071 l.grid(row=self.row, column=0, sticky="nw")
David Scherer7aced172000-08-15 01:13:23 +000072 e = Entry(self.top, textvariable=var, exportselection=0)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000073 e.grid(row=self.row, column=1, sticky="nwe")
David Scherer7aced172000-08-15 01:13:23 +000074 self.row = self.row + 1
75 return e
76
Chui Tey72a8a3b2002-11-04 23:07:51 +000077 def make_frame(self,labeltext=None):
78 if labeltext:
79 l = Label(self.top, text=labeltext)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000080 l.grid(row=self.row, column=0, sticky="nw")
David Scherer7aced172000-08-15 01:13:23 +000081 f = Frame(self.top)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000082 f.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
David Scherer7aced172000-08-15 01:13:23 +000083 self.row = self.row + 1
84 return f
85
Chui Tey72a8a3b2002-11-04 23:07:51 +000086 def make_button(self, label, command, isdef=0):
David Scherer7aced172000-08-15 01:13:23 +000087 b = Button(self.buttonframe,
88 text=label, command=command,
89 default=isdef and "active" or "normal")
Chui Tey72a8a3b2002-11-04 23:07:51 +000090 cols,rows=self.buttonframe.grid_size()
91 b.grid(pady=1,row=rows,column=0,sticky="ew")
92 self.buttonframe.grid(rowspan=rows+1)
David Scherer7aced172000-08-15 01:13:23 +000093 return b
94
95 def create_entries(self):
96 self.ent = self.make_entry("Find:", self.engine.patvar)
97
98 def create_option_buttons(self):
Chui Tey72a8a3b2002-11-04 23:07:51 +000099 f = self.make_frame("Options")
David Scherer7aced172000-08-15 01:13:23 +0000100
101 btn = Checkbutton(f, anchor="w",
102 variable=self.engine.revar,
103 text="Regular expression")
104 btn.pack(side="left", fill="both")
105 if self.engine.isre():
106 btn.select()
107
108 btn = Checkbutton(f, anchor="w",
109 variable=self.engine.casevar,
110 text="Match case")
111 btn.pack(side="left", fill="both")
112 if self.engine.iscase():
113 btn.select()
114
115 btn = Checkbutton(f, anchor="w",
116 variable=self.engine.wordvar,
117 text="Whole word")
118 btn.pack(side="left", fill="both")
119 if self.engine.isword():
120 btn.select()
121
122 if self.needwrapbutton:
123 btn = Checkbutton(f, anchor="w",
124 variable=self.engine.wrapvar,
125 text="Wrap around")
126 btn.pack(side="left", fill="both")
127 if self.engine.iswrap():
128 btn.select()
129
130 def create_other_buttons(self):
Chui Tey72a8a3b2002-11-04 23:07:51 +0000131 f = self.make_frame("Direction")
David Scherer7aced172000-08-15 01:13:23 +0000132
Chui Tey72a8a3b2002-11-04 23:07:51 +0000133 #lbl = Label(f, text="Direction: ")
134 #lbl.pack(side="left")
David Scherer7aced172000-08-15 01:13:23 +0000135
136 btn = Radiobutton(f, anchor="w",
137 variable=self.engine.backvar, value=1,
138 text="Up")
139 btn.pack(side="left", fill="both")
140 if self.engine.isback():
141 btn.select()
142
143 btn = Radiobutton(f, anchor="w",
144 variable=self.engine.backvar, value=0,
145 text="Down")
146 btn.pack(side="left", fill="both")
147 if not self.engine.isback():
148 btn.select()
149
150 def create_command_buttons(self):
Chui Tey72a8a3b2002-11-04 23:07:51 +0000151 #
152 # place button frame on the right
153 f = self.buttonframe = Frame(self.top)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +0000154 f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
Chui Tey72a8a3b2002-11-04 23:07:51 +0000155
156 b = self.make_button("close", self.close)
David Scherer7aced172000-08-15 01:13:23 +0000157 b.lower()