1 | """TextViewer class.
|
---|
2 |
|
---|
3 | The TextViewer allows you to see how the selected color would affect various
|
---|
4 | characteristics of a Tk text widget. This is an output viewer only.
|
---|
5 |
|
---|
6 | 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
|
---|
8 | change font characteristics). If you want changes in other viewers to update
|
---|
9 | text characteristics, turn on Track color changes.
|
---|
10 |
|
---|
11 | To select which characteristic tracks the change, select one of the radio
|
---|
12 | buttons in the window below. Text foreground and background affect the text
|
---|
13 | in the window above. The Selection is what you see when you click the middle
|
---|
14 | button and drag it through some text. The Insertion is the insertion cursor
|
---|
15 | in the text window (which only has a background).
|
---|
16 | """
|
---|
17 |
|
---|
18 | from Tkinter import *
|
---|
19 | import ColorDB
|
---|
20 |
|
---|
21 | ADDTOVIEW = 'Text Window...'
|
---|
22 |
|
---|
23 |
|
---|
24 | |
---|
25 |
|
---|
26 | class TextViewer:
|
---|
27 | def __init__(self, switchboard, master=None):
|
---|
28 | self.__sb = switchboard
|
---|
29 | optiondb = switchboard.optiondb()
|
---|
30 | root = self.__root = Toplevel(master, class_='Pynche')
|
---|
31 | root.protocol('WM_DELETE_WINDOW', self.withdraw)
|
---|
32 | root.title('Pynche Text Window')
|
---|
33 | root.iconname('Pynche Text Window')
|
---|
34 | root.bind('<Alt-q>', self.__quit)
|
---|
35 | root.bind('<Alt-Q>', self.__quit)
|
---|
36 | root.bind('<Alt-w>', self.withdraw)
|
---|
37 | root.bind('<Alt-W>', self.withdraw)
|
---|
38 | #
|
---|
39 | # create the text widget
|
---|
40 | #
|
---|
41 | self.__text = Text(root, relief=SUNKEN,
|
---|
42 | background=optiondb.get('TEXTBG', 'black'),
|
---|
43 | foreground=optiondb.get('TEXTFG', 'white'),
|
---|
44 | width=35, height=15)
|
---|
45 | sfg = optiondb.get('TEXT_SFG')
|
---|
46 | if sfg:
|
---|
47 | self.__text.configure(selectforeground=sfg)
|
---|
48 | sbg = optiondb.get('TEXT_SBG')
|
---|
49 | if sbg:
|
---|
50 | self.__text.configure(selectbackground=sbg)
|
---|
51 | ibg = optiondb.get('TEXT_IBG')
|
---|
52 | if ibg:
|
---|
53 | self.__text.configure(insertbackground=ibg)
|
---|
54 | self.__text.pack()
|
---|
55 | self.__text.insert(0.0, optiondb.get('TEXT', '''\
|
---|
56 | Insert some stuff here and play
|
---|
57 | with the buttons below to see
|
---|
58 | how the colors interact in
|
---|
59 | textual displays.
|
---|
60 |
|
---|
61 | See how the selection can also
|
---|
62 | be affected by tickling the buttons
|
---|
63 | and choosing a color.'''))
|
---|
64 | insert = optiondb.get('TEXTINS')
|
---|
65 | if insert:
|
---|
66 | self.__text.mark_set(INSERT, insert)
|
---|
67 | try:
|
---|
68 | start, end = optiondb.get('TEXTSEL', (6.0, END))
|
---|
69 | self.__text.tag_add(SEL, start, end)
|
---|
70 | except ValueError:
|
---|
71 | # selection wasn't set
|
---|
72 | pass
|
---|
73 | self.__text.focus_set()
|
---|
74 | #
|
---|
75 | # variables
|
---|
76 | self.__trackp = BooleanVar()
|
---|
77 | self.__trackp.set(optiondb.get('TRACKP', 0))
|
---|
78 | self.__which = IntVar()
|
---|
79 | self.__which.set(optiondb.get('WHICH', 0))
|
---|
80 | #
|
---|
81 | # track toggle
|
---|
82 | self.__t = Checkbutton(root, text='Track color changes',
|
---|
83 | variable=self.__trackp,
|
---|
84 | relief=GROOVE,
|
---|
85 | command=self.__toggletrack)
|
---|
86 | self.__t.pack(fill=X, expand=YES)
|
---|
87 | frame = self.__frame = Frame(root)
|
---|
88 | frame.pack()
|
---|
89 | #
|
---|
90 | # labels
|
---|
91 | self.__labels = []
|
---|
92 | row = 2
|
---|
93 | for text in ('Text:', 'Selection:', 'Insertion:'):
|
---|
94 | l = Label(frame, text=text)
|
---|
95 | l.grid(row=row, column=0, sticky=E)
|
---|
96 | self.__labels.append(l)
|
---|
97 | row += 1
|
---|
98 | col = 1
|
---|
99 | for text in ('Foreground', 'Background'):
|
---|
100 | l = Label(frame, text=text)
|
---|
101 | l.grid(row=1, column=col)
|
---|
102 | self.__labels.append(l)
|
---|
103 | col += 1
|
---|
104 | #
|
---|
105 | # radios
|
---|
106 | self.__radios = []
|
---|
107 | for col in (1, 2):
|
---|
108 | for row in (2, 3, 4):
|
---|
109 | # there is no insertforeground option
|
---|
110 | if row==4 and col==1:
|
---|
111 | continue
|
---|
112 | r = Radiobutton(frame, variable=self.__which,
|
---|
113 | value=(row-2)*2 + col-1,
|
---|
114 | command=self.__set_color)
|
---|
115 | r.grid(row=row, column=col)
|
---|
116 | self.__radios.append(r)
|
---|
117 | self.__toggletrack()
|
---|
118 |
|
---|
119 | def __quit(self, event=None):
|
---|
120 | self.__root.quit()
|
---|
121 |
|
---|
122 | def withdraw(self, event=None):
|
---|
123 | self.__root.withdraw()
|
---|
124 |
|
---|
125 | def deiconify(self, event=None):
|
---|
126 | self.__root.deiconify()
|
---|
127 |
|
---|
128 | def __forceupdate(self, event=None):
|
---|
129 | self.__sb.update_views_current()
|
---|
130 |
|
---|
131 | def __toggletrack(self, event=None):
|
---|
132 | if self.__trackp.get():
|
---|
133 | state = NORMAL
|
---|
134 | fg = self.__radios[0]['foreground']
|
---|
135 | else:
|
---|
136 | state = DISABLED
|
---|
137 | fg = self.__radios[0]['disabledforeground']
|
---|
138 | for r in self.__radios:
|
---|
139 | r.configure(state=state)
|
---|
140 | for l in self.__labels:
|
---|
141 | l.configure(foreground=fg)
|
---|
142 |
|
---|
143 | def __set_color(self, event=None):
|
---|
144 | which = self.__which.get()
|
---|
145 | text = self.__text
|
---|
146 | if which == 0:
|
---|
147 | color = text['foreground']
|
---|
148 | elif which == 1:
|
---|
149 | color = text['background']
|
---|
150 | elif which == 2:
|
---|
151 | color = text['selectforeground']
|
---|
152 | elif which == 3:
|
---|
153 | color = text['selectbackground']
|
---|
154 | elif which == 5:
|
---|
155 | color = text['insertbackground']
|
---|
156 | try:
|
---|
157 | red, green, blue = ColorDB.rrggbb_to_triplet(color)
|
---|
158 | except ColorDB.BadColor:
|
---|
159 | # must have been a color name
|
---|
160 | red, green, blue = self.__sb.colordb().find_byname(color)
|
---|
161 | self.__sb.update_views(red, green, blue)
|
---|
162 |
|
---|
163 | def update_yourself(self, red, green, blue):
|
---|
164 | if self.__trackp.get():
|
---|
165 | colorname = ColorDB.triplet_to_rrggbb((red, green, blue))
|
---|
166 | which = self.__which.get()
|
---|
167 | text = self.__text
|
---|
168 | if which == 0:
|
---|
169 | text.configure(foreground=colorname)
|
---|
170 | elif which == 1:
|
---|
171 | text.configure(background=colorname)
|
---|
172 | elif which == 2:
|
---|
173 | text.configure(selectforeground=colorname)
|
---|
174 | elif which == 3:
|
---|
175 | text.configure(selectbackground=colorname)
|
---|
176 | elif which == 5:
|
---|
177 | text.configure(insertbackground=colorname)
|
---|
178 |
|
---|
179 | def save_options(self, optiondb):
|
---|
180 | optiondb['TRACKP'] = self.__trackp.get()
|
---|
181 | optiondb['WHICH'] = self.__which.get()
|
---|
182 | optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c')
|
---|
183 | optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2]
|
---|
184 | optiondb['TEXTINS'] = self.__text.index(INSERT)
|
---|
185 | optiondb['TEXTFG'] = self.__text['foreground']
|
---|
186 | optiondb['TEXTBG'] = self.__text['background']
|
---|
187 | optiondb['TEXT_SFG'] = self.__text['selectforeground']
|
---|
188 | optiondb['TEXT_SBG'] = self.__text['selectbackground']
|
---|
189 | optiondb['TEXT_IBG'] = self.__text['insertbackground']
|
---|