[391] | 1 | '''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
|
---|
[2] | 2 | from Tkinter import *
|
---|
| 3 |
|
---|
| 4 | class SearchDialogBase:
|
---|
[391] | 5 | '''Create most of a modal search dialog (make_frame, create_widgets).
|
---|
[2] | 6 |
|
---|
[391] | 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 | '''
|
---|
| 21 |
|
---|
[2] | 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 |
|
---|
| 31 | def open(self, text, searchphrase=None):
|
---|
| 32 | self.text = text
|
---|
| 33 | if not self.top:
|
---|
| 34 | self.create_widgets()
|
---|
| 35 | else:
|
---|
| 36 | self.top.deiconify()
|
---|
| 37 | self.top.tkraise()
|
---|
| 38 | if searchphrase:
|
---|
| 39 | self.ent.delete(0,"end")
|
---|
| 40 | self.ent.insert("end",searchphrase)
|
---|
| 41 | 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
|
---|
| 61 | self.top.grid_columnconfigure(0, pad=2, weight=0)
|
---|
| 62 | self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
|
---|
| 63 |
|
---|
| 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)
|
---|
| 71 | l.grid(row=self.row, column=0, sticky="nw")
|
---|
| 72 | e = Entry(self.top, textvariable=var, exportselection=0)
|
---|
| 73 | e.grid(row=self.row, column=1, sticky="nwe")
|
---|
| 74 | self.row = self.row + 1
|
---|
| 75 | return e
|
---|
| 76 |
|
---|
| 77 | def make_frame(self,labeltext=None):
|
---|
| 78 | if labeltext:
|
---|
| 79 | l = Label(self.top, text=labeltext)
|
---|
| 80 | l.grid(row=self.row, column=0, sticky="nw")
|
---|
| 81 | f = Frame(self.top)
|
---|
| 82 | f.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
|
---|
| 83 | self.row = self.row + 1
|
---|
| 84 | return f
|
---|
| 85 |
|
---|
| 86 | def make_button(self, label, command, isdef=0):
|
---|
| 87 | b = Button(self.buttonframe,
|
---|
| 88 | text=label, command=command,
|
---|
| 89 | default=isdef and "active" or "normal")
|
---|
| 90 | cols,rows=self.buttonframe.grid_size()
|
---|
| 91 | b.grid(pady=1,row=rows,column=0,sticky="ew")
|
---|
| 92 | self.buttonframe.grid(rowspan=rows+1)
|
---|
| 93 | 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):
|
---|
| 99 | f = self.make_frame("Options")
|
---|
| 100 |
|
---|
| 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):
|
---|
| 131 | f = self.make_frame("Direction")
|
---|
| 132 |
|
---|
| 133 | #lbl = Label(f, text="Direction: ")
|
---|
| 134 | #lbl.pack(side="left")
|
---|
| 135 |
|
---|
| 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):
|
---|
| 151 | #
|
---|
| 152 | # place button frame on the right
|
---|
| 153 | f = self.buttonframe = Frame(self.top)
|
---|
| 154 | f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
|
---|
| 155 |
|
---|
| 156 | b = self.make_button("close", self.close)
|
---|
| 157 | b.lower()
|
---|