TextViewer.py 6.71 KB
Newer Older
Barry Warsaw's avatar
Barry Warsaw committed
1 2 3 4 5 6
"""TextViewer class.

The TextViewer allows you to see how the selected color would affect various
characteristics of a Tk text widget.  This is an output viewer only.

In the top part of the window is a standard text widget with some sample text
7
in it.  You are free to edit this text in any way you want (BAW: allow you to
Barry Warsaw's avatar
Barry Warsaw committed
8 9 10 11 12 13 14 15 16 17
change font characteristics).  If you want changes in other viewers to update
text characteristics, turn on Track color changes.

To select which characteristic tracks the change, select one of the radio
buttons in the window below.  Text foreground and background affect the text
in the window above.  The Selection is what you see when you click the middle
button and drag it through some text.  The Insertion is the insertion cursor
in the text window (which only has a background).
"""

Barry Warsaw's avatar
Barry Warsaw committed
18 19 20
from Tkinter import *
import ColorDB

21 22
ADDTOVIEW = 'Text Window...'

23 24


Barry Warsaw's avatar
Barry Warsaw committed
25
class TextViewer:
26
    def __init__(self, switchboard, master=None):
Barry Warsaw's avatar
Barry Warsaw committed
27
        self.__sb = switchboard
28
        optiondb = switchboard.optiondb()
29 30
        root = self.__root = Toplevel(master, class_='Pynche')
        root.protocol('WM_DELETE_WINDOW', self.withdraw)
Barry Warsaw's avatar
Barry Warsaw committed
31
        root.title('Pynche Text Window')
Barry Warsaw's avatar
Barry Warsaw committed
32 33 34
        root.iconname('Pynche Text Window')
        root.bind('<Alt-q>', self.__quit)
        root.bind('<Alt-Q>', self.__quit)
35 36
        root.bind('<Alt-w>', self.withdraw)
        root.bind('<Alt-W>', self.withdraw)
Barry Warsaw's avatar
Barry Warsaw committed
37 38 39 40
        #
        # create the text widget
        #
        self.__text = Text(root, relief=SUNKEN,
41 42
                           background=optiondb.get('TEXTBG', 'black'),
                           foreground=optiondb.get('TEXTFG', 'white'),
Barry Warsaw's avatar
Barry Warsaw committed
43
                           width=35, height=15)
44 45 46 47 48 49 50 51 52
        sfg = optiondb.get('TEXT_SFG')
        if sfg:
            self.__text.configure(selectforeground=sfg)
        sbg = optiondb.get('TEXT_SBG')
        if sbg:
            self.__text.configure(selectbackground=sbg)
        ibg = optiondb.get('TEXT_IBG')
        if ibg:
            self.__text.configure(insertbackground=ibg)
Barry Warsaw's avatar
Barry Warsaw committed
53
        self.__text.pack()
54
        self.__text.insert(0.0, optiondb.get('TEXT', '''\
Barry Warsaw's avatar
Barry Warsaw committed
55 56 57
Insert some stuff here and play
with the buttons below to see
how the colors interact in
58 59 60 61
textual displays.

See how the selection can also
be affected by tickling the buttons
62 63 64 65 66 67 68 69 70 71 72
and choosing a color.'''))
        insert = optiondb.get('TEXTINS')
        if insert:
            self.__text.mark_set(INSERT, insert)
        try:
            start, end = optiondb.get('TEXTSEL', (6.0, END))
            self.__text.tag_add(SEL, start, end)
        except ValueError:
            # selection wasn't set
            pass
        self.__text.focus_set()
Barry Warsaw's avatar
Barry Warsaw committed
73 74 75
        #
        # variables
        self.__trackp = BooleanVar()
76
        self.__trackp.set(optiondb.get('TRACKP', 0))
Barry Warsaw's avatar
Barry Warsaw committed
77
        self.__which = IntVar()
78
        self.__which.set(optiondb.get('WHICH', 0))
Barry Warsaw's avatar
Barry Warsaw committed
79 80 81 82
        #
        # track toggle
        self.__t = Checkbutton(root, text='Track color changes',
                               variable=self.__trackp,
83 84
                               relief=GROOVE,
                               command=self.__toggletrack)
Barry Warsaw's avatar
Barry Warsaw committed
85 86 87 88 89 90 91 92 93 94 95
        self.__t.pack(fill=X, expand=YES)
        frame = self.__frame = Frame(root)
        frame.pack()
        #
        # labels
        self.__labels = []
        row = 2
        for text in ('Text:', 'Selection:', 'Insertion:'):
            l = Label(frame, text=text)
            l.grid(row=row, column=0, sticky=E)
            self.__labels.append(l)
96
            row += 1
Barry Warsaw's avatar
Barry Warsaw committed
97 98 99 100 101
        col = 1
        for text in ('Foreground', 'Background'):
            l = Label(frame, text=text)
            l.grid(row=1, column=col)
            self.__labels.append(l)
102
            col += 1
Barry Warsaw's avatar
Barry Warsaw committed
103 104 105 106 107 108 109 110 111
        #
        # radios
        self.__radios = []
        for col in (1, 2):
            for row in (2, 3, 4):
                # there is no insertforeground option
                if row==4 and col==1:
                    continue
                r = Radiobutton(frame, variable=self.__which,
112 113
                                value=(row-2)*2 + col-1,
                                command=self.__set_color)
Barry Warsaw's avatar
Barry Warsaw committed
114 115
                r.grid(row=row, column=col)
                self.__radios.append(r)
116
        self.__toggletrack()
Barry Warsaw's avatar
Barry Warsaw committed
117 118

    def __quit(self, event=None):
Barry Warsaw's avatar
Barry Warsaw committed
119
        self.__root.quit()
Barry Warsaw's avatar
Barry Warsaw committed
120

121
    def withdraw(self, event=None):
Barry Warsaw's avatar
Barry Warsaw committed
122 123 124 125 126 127 128 129
        self.__root.withdraw()

    def deiconify(self, event=None):
        self.__root.deiconify()

    def __forceupdate(self, event=None):
        self.__sb.update_views_current()

130 131 132 133 134 135 136 137 138 139 140 141
    def __toggletrack(self, event=None):
        if self.__trackp.get():
            state = NORMAL
            fg = self.__radios[0]['foreground']
        else:
            state = DISABLED
            fg = self.__radios[0]['disabledforeground']
        for r in self.__radios:
            r.configure(state=state)
        for l in self.__labels:
            l.configure(foreground=fg)

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    def __set_color(self, event=None):
        which = self.__which.get()
        text = self.__text
        if which == 0:
            color = text['foreground']
        elif which == 1:
            color = text['background']
        elif which == 2:
            color = text['selectforeground']
        elif which == 3:
            color = text['selectbackground']
        elif which == 5:
            color = text['insertbackground']
        try:
            red, green, blue = ColorDB.rrggbb_to_triplet(color)
        except ColorDB.BadColor:
            # must have been a color name
            red, green, blue = self.__sb.colordb().find_byname(color)
        self.__sb.update_views(red, green, blue)

Barry Warsaw's avatar
Barry Warsaw committed
162
    def update_yourself(self, red, green, blue):
163 164 165
        if self.__trackp.get():
            colorname = ColorDB.triplet_to_rrggbb((red, green, blue))
            which = self.__which.get()
166
            text = self.__text
167
            if which == 0:
168
                text.configure(foreground=colorname)
169
            elif which == 1:
170
                text.configure(background=colorname)
171
            elif which == 2:
172
                text.configure(selectforeground=colorname)
173
            elif which == 3:
174
                text.configure(selectbackground=colorname)
175
            elif which == 5:
176
                text.configure(insertbackground=colorname)
177 178 179 180 181 182 183 184 185 186 187 188

    def save_options(self, optiondb):
        optiondb['TRACKP'] = self.__trackp.get()
        optiondb['WHICH'] = self.__which.get()
        optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c')
        optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2]
        optiondb['TEXTINS'] = self.__text.index(INSERT)
        optiondb['TEXTFG'] = self.__text['foreground']
        optiondb['TEXTBG'] = self.__text['background']
        optiondb['TEXT_SFG'] = self.__text['selectforeground']
        optiondb['TEXT_SBG'] = self.__text['selectbackground']
        optiondb['TEXT_IBG'] = self.__text['insertbackground']