SearchDialogBase.py 6.84 KB
Newer Older
1
'''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
2 3 4

from tkinter import (Toplevel, Frame, Entry, Label, Button,
                     Checkbutton, Radiobutton)
David Scherer's avatar
David Scherer committed
5 6

class SearchDialogBase:
7
    '''Create most of a 3 or 4 row, 3 column search dialog.
8

9 10 11 12 13
    The left and wide middle column contain:
    1 or 2 labeled text entry lines (make_entry, create_entries);
    a row of standard Checkbuttons (make_frame, create_option_buttons),
    each of which corresponds to a search engine Variable;
    a row of dialog-specific Check/Radiobuttons (create_other_buttons).
14

15
    The narrow right column contains command buttons
16
    (make_button, create_command_buttons).
17 18
    These are bound to functions that execute the command.

19 20 21 22 23 24
    Except for command buttons, this base class is not limited to items
    common to all three subclasses.  Rather, it is the Find dialog minus
    the "Find Next" command, its execution function, and the
    default_command attribute needed in create_widgets. The other
    dialogs override attributes and methods, the latter to replace and
    add widgets.
25
    '''
David Scherer's avatar
David Scherer committed
26

27
    title = "Search Dialog"  # replace in subclasses
David Scherer's avatar
David Scherer committed
28
    icon = "Search"
29
    needwrapbutton = 1  # not in Find in Files
David Scherer's avatar
David Scherer committed
30 31

    def __init__(self, root, engine):
32 33 34
        '''Initialize root, engine, and top attributes.

        top (level widget): set in create_widgets() called from open().
35
        text (Text searched): set in open(), only used in subclasses().
36 37
        ent (ry): created in make_entry() called from create_entry().
        row (of grid): 0 in create_widgets(), +1 in make_entry/frame().
38
        default_command: set in subclasses, used in create_widgers().
39 40 41 42

        title (of dialog): class attribute, override in subclasses.
        icon (of dialog): ditto, use unclear if cannot minimize dialog.
        '''
David Scherer's avatar
David Scherer committed
43 44 45 46
        self.root = root
        self.engine = engine
        self.top = None

47
    def open(self, text, searchphrase=None):
48
        "Make dialog visible on top of others and ready to use."
David Scherer's avatar
David Scherer committed
49 50 51 52 53 54
        self.text = text
        if not self.top:
            self.create_widgets()
        else:
            self.top.deiconify()
            self.top.tkraise()
55 56 57
        if searchphrase:
            self.ent.delete(0,"end")
            self.ent.insert("end",searchphrase)
David Scherer's avatar
David Scherer committed
58 59 60 61 62 63
        self.ent.focus_set()
        self.ent.selection_range(0, "end")
        self.ent.icursor(0)
        self.top.grab_set()

    def close(self, event=None):
64
        "Put dialog away for later use."
David Scherer's avatar
David Scherer committed
65 66 67 68 69
        if self.top:
            self.top.grab_release()
            self.top.withdraw()

    def create_widgets(self):
70 71 72 73 74
        '''Create basic 3 row x 3 col search (find) dialog.

        Other dialogs override subsidiary create_x methods as needed.
        Replace and Find-in-Files add another entry row.
        '''
David Scherer's avatar
David Scherer committed
75 76 77 78 79 80 81 82 83
        top = Toplevel(self.root)
        top.bind("<Return>", self.default_command)
        top.bind("<Escape>", self.close)
        top.protocol("WM_DELETE_WINDOW", self.close)
        top.wm_title(self.title)
        top.wm_iconname(self.icon)
        self.top = top

        self.row = 0
Chui Tey's avatar
Chui Tey committed
84 85
        self.top.grid_columnconfigure(0, pad=2, weight=0)
        self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
David Scherer's avatar
David Scherer committed
86

87 88 89 90
        self.create_entries()  # row 0 (and maybe 1), cols 0, 1
        self.create_option_buttons()  # next row, cols 0, 1
        self.create_other_buttons()  # next row, cols 0, 1
        self.create_command_buttons()  # col 2, all rows
David Scherer's avatar
David Scherer committed
91

92 93 94 95 96 97 98 99 100 101
    def make_entry(self, label_text, var):
        '''Return (entry, label), .

        entry - gridded labeled Entry for text entry.
        label - Label widget, returned for testing.
        '''
        label = Label(self.top, text=label_text)
        label.grid(row=self.row, column=0, sticky="nw")
        entry = Entry(self.top, textvariable=var, exportselection=0)
        entry.grid(row=self.row, column=1, sticky="nwe")
David Scherer's avatar
David Scherer committed
102
        self.row = self.row + 1
103
        return entry, label
David Scherer's avatar
David Scherer committed
104

105 106
    def create_entries(self):
        "Create one or more entry lines with make_entry."
107
        self.ent = self.make_entry("Find:", self.engine.patvar)[0]
108

Chui Tey's avatar
Chui Tey committed
109
    def make_frame(self,labeltext=None):
110 111 112 113 114
        '''Return (frame, label).

        frame - gridded labeled Frame for option or other buttons.
        label - Label widget, returned for testing.
        '''
Chui Tey's avatar
Chui Tey committed
115
        if labeltext:
116 117
            label = Label(self.top, text=labeltext)
            label.grid(row=self.row, column=0, sticky="nw")
118
        else:
119 120 121
            label = ''
        frame = Frame(self.top)
        frame.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
David Scherer's avatar
David Scherer committed
122
        self.row = self.row + 1
123
        return frame, label
David Scherer's avatar
David Scherer committed
124 125

    def create_option_buttons(self):
126 127 128 129 130 131
        '''Return (filled frame, options) for testing.

        Options is a list of SearchEngine booleanvar, label pairs.
        A gridded frame from make_frame is filled with a Checkbutton
        for each pair, bound to the var, with the corresponding label.
        '''
132 133 134 135 136
        frame = self.make_frame("Options")[0]
        engine = self.engine
        options = [(engine.revar, "Regular expression"),
                   (engine.casevar, "Match case"),
                   (engine.wordvar, "Whole word")]
David Scherer's avatar
David Scherer committed
137
        if self.needwrapbutton:
138 139 140
            options.append((engine.wrapvar, "Wrap around"))
        for var, label in options:
            btn = Checkbutton(frame, anchor="w", variable=var, text=label)
David Scherer's avatar
David Scherer committed
141
            btn.pack(side="left", fill="both")
142
            if var.get():
David Scherer's avatar
David Scherer committed
143
                btn.select()
144
        return frame, options
David Scherer's avatar
David Scherer committed
145 146

    def create_other_buttons(self):
147 148 149 150 151
        '''Return (frame, others) for testing.

        Others is a list of value, label pairs.
        A gridded frame from make_frame is filled with radio buttons.
        '''
152 153 154 155 156 157 158 159 160
        frame = self.make_frame("Direction")[0]
        var = self.engine.backvar
        others = [(1, 'Up'), (0, 'Down')]
        for val, label in others:
            btn = Radiobutton(frame, anchor="w",
                              variable=var, value=val, text=label)
            btn.pack(side="left", fill="both")
            if var.get() == val:
                btn.select()
161
        return frame, others
David Scherer's avatar
David Scherer committed
162

163 164 165 166 167 168 169 170 171 172
    def make_button(self, label, command, isdef=0):
        "Return command button gridded in command frame."
        b = Button(self.buttonframe,
                   text=label, command=command,
                   default=isdef and "active" or "normal")
        cols,rows=self.buttonframe.grid_size()
        b.grid(pady=1,row=rows,column=0,sticky="ew")
        self.buttonframe.grid(rowspan=rows+1)
        return b

David Scherer's avatar
David Scherer committed
173
    def create_command_buttons(self):
174
        "Place buttons in vertical command frame gridded on right."
Chui Tey's avatar
Chui Tey committed
175
        f = self.buttonframe = Frame(self.top)
176
        f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
Chui Tey's avatar
Chui Tey committed
177 178

        b = self.make_button("close", self.close)
David Scherer's avatar
David Scherer committed
179
        b.lower()
180 181 182 183 184

if __name__ == '__main__':
    import unittest
    unittest.main(
        'idlelib.idle_test.test_searchdialogbase', verbosity=2)