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