blob: 65914ac24ec27eb706b632c96dc3ae734ac20269 [file] [log] [blame]
Georg Brandl14fc4272008-05-17 18:39:55 +00001from tkinter import *
David Scherer7aced172000-08-15 01:13:23 +00002
3class SearchDialogBase:
4
5 title = "Search Dialog"
6 icon = "Search"
7 needwrapbutton = 1
8
9 def __init__(self, root, engine):
10 self.root = root
11 self.engine = engine
12 self.top = None
13
Chui Tey5a231c8f2002-11-06 02:18:45 +000014 def open(self, text, searchphrase=None):
David Scherer7aced172000-08-15 01:13:23 +000015 self.text = text
16 if not self.top:
17 self.create_widgets()
18 else:
19 self.top.deiconify()
20 self.top.tkraise()
Chui Tey5a231c8f2002-11-06 02:18:45 +000021 if searchphrase:
22 self.ent.delete(0,"end")
23 self.ent.insert("end",searchphrase)
David Scherer7aced172000-08-15 01:13:23 +000024 self.ent.focus_set()
25 self.ent.selection_range(0, "end")
26 self.ent.icursor(0)
27 self.top.grab_set()
28
29 def close(self, event=None):
30 if self.top:
31 self.top.grab_release()
32 self.top.withdraw()
33
34 def create_widgets(self):
35 top = Toplevel(self.root)
36 top.bind("<Return>", self.default_command)
37 top.bind("<Escape>", self.close)
38 top.protocol("WM_DELETE_WINDOW", self.close)
39 top.wm_title(self.title)
40 top.wm_iconname(self.icon)
41 self.top = top
42
43 self.row = 0
Chui Tey72a8a3b2002-11-04 23:07:51 +000044 self.top.grid_columnconfigure(0, pad=2, weight=0)
45 self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
David Scherer7aced172000-08-15 01:13:23 +000046
47 self.create_entries()
48 self.create_option_buttons()
49 self.create_other_buttons()
50 return self.create_command_buttons()
51
52 def make_entry(self, label, var):
53 l = Label(self.top, text=label)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000054 l.grid(row=self.row, column=0, sticky="nw")
David Scherer7aced172000-08-15 01:13:23 +000055 e = Entry(self.top, textvariable=var, exportselection=0)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000056 e.grid(row=self.row, column=1, sticky="nwe")
David Scherer7aced172000-08-15 01:13:23 +000057 self.row = self.row + 1
58 return e
59
Chui Tey72a8a3b2002-11-04 23:07:51 +000060 def make_frame(self,labeltext=None):
61 if labeltext:
62 l = Label(self.top, text=labeltext)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000063 l.grid(row=self.row, column=0, sticky="nw")
David Scherer7aced172000-08-15 01:13:23 +000064 f = Frame(self.top)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +000065 f.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
David Scherer7aced172000-08-15 01:13:23 +000066 self.row = self.row + 1
67 return f
68
Chui Tey72a8a3b2002-11-04 23:07:51 +000069 def make_button(self, label, command, isdef=0):
David Scherer7aced172000-08-15 01:13:23 +000070 b = Button(self.buttonframe,
71 text=label, command=command,
72 default=isdef and "active" or "normal")
Chui Tey72a8a3b2002-11-04 23:07:51 +000073 cols,rows=self.buttonframe.grid_size()
74 b.grid(pady=1,row=rows,column=0,sticky="ew")
75 self.buttonframe.grid(rowspan=rows+1)
David Scherer7aced172000-08-15 01:13:23 +000076 return b
77
78 def create_entries(self):
79 self.ent = self.make_entry("Find:", self.engine.patvar)
80
81 def create_option_buttons(self):
Chui Tey72a8a3b2002-11-04 23:07:51 +000082 f = self.make_frame("Options")
David Scherer7aced172000-08-15 01:13:23 +000083
84 btn = Checkbutton(f, anchor="w",
85 variable=self.engine.revar,
86 text="Regular expression")
87 btn.pack(side="left", fill="both")
88 if self.engine.isre():
89 btn.select()
90
91 btn = Checkbutton(f, anchor="w",
92 variable=self.engine.casevar,
93 text="Match case")
94 btn.pack(side="left", fill="both")
95 if self.engine.iscase():
96 btn.select()
97
98 btn = Checkbutton(f, anchor="w",
99 variable=self.engine.wordvar,
100 text="Whole word")
101 btn.pack(side="left", fill="both")
102 if self.engine.isword():
103 btn.select()
104
105 if self.needwrapbutton:
106 btn = Checkbutton(f, anchor="w",
107 variable=self.engine.wrapvar,
108 text="Wrap around")
109 btn.pack(side="left", fill="both")
110 if self.engine.iswrap():
111 btn.select()
112
113 def create_other_buttons(self):
Chui Tey72a8a3b2002-11-04 23:07:51 +0000114 f = self.make_frame("Direction")
David Scherer7aced172000-08-15 01:13:23 +0000115
Chui Tey72a8a3b2002-11-04 23:07:51 +0000116 #lbl = Label(f, text="Direction: ")
117 #lbl.pack(side="left")
David Scherer7aced172000-08-15 01:13:23 +0000118
119 btn = Radiobutton(f, anchor="w",
120 variable=self.engine.backvar, value=1,
121 text="Up")
122 btn.pack(side="left", fill="both")
123 if self.engine.isback():
124 btn.select()
125
126 btn = Radiobutton(f, anchor="w",
127 variable=self.engine.backvar, value=0,
128 text="Down")
129 btn.pack(side="left", fill="both")
130 if not self.engine.isback():
131 btn.select()
132
133 def create_command_buttons(self):
Chui Tey72a8a3b2002-11-04 23:07:51 +0000134 #
135 # place button frame on the right
136 f = self.buttonframe = Frame(self.top)
Kurt B. Kaiser4fc90472002-11-21 03:02:17 +0000137 f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
Chui Tey72a8a3b2002-11-04 23:07:51 +0000138
139 b = self.make_button("close", self.close)
David Scherer7aced172000-08-15 01:13:23 +0000140 b.lower()