1 from Tkinter import * 2 3 class 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 14 def open(self, text, searchphrase=None): 15 self.text = text 16 if not self.top: 17 self.create_widgets() 18 else: 19 self.top.deiconify() 20 self.top.tkraise() 21 if searchphrase: 22 self.ent.delete(0,"end") 23 self.ent.insert("end",searchphrase) 24 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 44 self.top.grid_columnconfigure(0, pad=2, weight=0) 45 self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100) 46 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) 54 l.grid(row=self.row, column=0, sticky="nw") 55 e = Entry(self.top, textvariable=var, exportselection=0) 56 e.grid(row=self.row, column=1, sticky="nwe") 57 self.row = self.row + 1 58 return e 59 60 def make_frame(self,labeltext=None): 61 if labeltext: 62 l = Label(self.top, text=labeltext) 63 l.grid(row=self.row, column=0, sticky="nw") 64 f = Frame(self.top) 65 f.grid(row=self.row, column=1, columnspan=1, sticky="nwe") 66 self.row = self.row + 1 67 return f 68 69 def make_button(self, label, command, isdef=0): 70 b = Button(self.buttonframe, 71 text=label, command=command, 72 default=isdef and "active" or "normal") 73 cols,rows=self.buttonframe.grid_size() 74 b.grid(pady=1,row=rows,column=0,sticky="ew") 75 self.buttonframe.grid(rowspan=rows+1) 76 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): 82 f = self.make_frame("Options") 83 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): 114 f = self.make_frame("Direction") 115 116 #lbl = Label(f, text="Direction: ") 117 #lbl.pack(side="left") 118 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): 134 # 135 # place button frame on the right 136 f = self.buttonframe = Frame(self.top) 137 f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2) 138 139 b = self.make_button("close", self.close) 140 b.lower() 141