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.
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 (TBD: allow you to
8 change font characteristics). If you want changes in other viewers to update
9 text characteristics, turn on Track color changes.
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).
21 ADDTOVIEW
= 'Text Window...'
24 def __init__(self
, switchboard
, master
=None):
25 self
.__sb
= switchboard
26 optiondb
= switchboard
.optiondb()
27 root
= self
.__root
= Toplevel(master
, class_
='Pynche')
28 root
.protocol('WM_DELETE_WINDOW', self
.withdraw
)
29 root
.title('Pynche Text Window')
30 root
.iconname('Pynche Text Window')
31 root
.bind('<Alt-q>', self
.__quit
)
32 root
.bind('<Alt-Q>', self
.__quit
)
33 root
.bind('<Alt-w>', self
.withdraw
)
34 root
.bind('<Alt-W>', self
.withdraw
)
36 # create the text widget
38 self
.__text
= Text(root
, relief
=SUNKEN
,
39 background
=optiondb
.get('TEXTBG', 'black'),
40 foreground
=optiondb
.get('TEXTFG', 'white'),
42 sfg
= optiondb
.get('TEXT_SFG')
44 self
.__text
.configure(selectforeground
=sfg
)
45 sbg
= optiondb
.get('TEXT_SBG')
47 self
.__text
.configure(selectbackground
=sbg
)
48 ibg
= optiondb
.get('TEXT_IBG')
50 self
.__text
.configure(insertbackground
=ibg
)
52 self
.__text
.insert(0.0, optiondb
.get('TEXT', '''\
53 Insert some stuff here and play
54 with the buttons below to see
55 how the colors interact in
58 See how the selection can also
59 be affected by tickling the buttons
60 and choosing a color.'''))
61 insert
= optiondb
.get('TEXTINS')
63 self
.__text
.mark_set(INSERT
, insert
)
65 start
, end
= optiondb
.get('TEXTSEL', (6.0, END
))
66 self
.__text
.tag_add(SEL
, start
, end
)
68 # selection wasn't set
70 self
.__text
.focus_set()
73 self
.__trackp
= BooleanVar()
74 self
.__trackp
.set(optiondb
.get('TRACKP', 0))
75 self
.__which
= IntVar()
76 self
.__which
.set(optiondb
.get('WHICH', 0))
79 self
.__t
= Checkbutton(root
, text
='Track color changes',
80 variable
=self
.__trackp
,
82 command
=self
.__toggletrack
)
83 self
.__t
.pack(fill
=X
, expand
=YES
)
84 frame
= self
.__frame
= Frame(root
)
90 for text
in ('Text:', 'Selection:', 'Insertion:'):
91 l
= Label(frame
, text
=text
)
92 l
.grid(row
=row
, column
=0, sticky
=E
)
93 self
.__labels
.append(l
)
96 for text
in ('Foreground', 'Background'):
97 l
= Label(frame
, text
=text
)
98 l
.grid(row
=1, column
=col
)
99 self
.__labels
.append(l
)
106 for row
in (2, 3, 4):
107 # there is no insertforeground option
108 if row
==4 and col
==1:
110 r
= Radiobutton(frame
, variable
=self
.__which
,
111 value
=(row
-2)*2 + col
-1,
112 command
=self
.__set
_color
)
113 r
.grid(row
=row
, column
=col
)
114 self
.__radios
.append(r
)
117 def __quit(self
, event
=None):
120 def withdraw(self
, event
=None):
121 self
.__root
.withdraw()
123 def deiconify(self
, event
=None):
124 self
.__root
.deiconify()
126 def __forceupdate(self
, event
=None):
127 self
.__sb
.update_views_current()
129 def __toggletrack(self
, event
=None):
130 if self
.__trackp
.get():
132 fg
= self
.__radios
[0]['foreground']
135 fg
= self
.__radios
[0]['disabledforeground']
136 for r
in self
.__radios
:
137 r
.configure(state
=state
)
138 for l
in self
.__labels
:
139 l
.configure(foreground
=fg
)
141 def __set_color(self
, event
=None):
142 which
= self
.__which
.get()
145 color
= text
['foreground']
147 color
= text
['background']
149 color
= text
['selectforeground']
151 color
= text
['selectbackground']
153 color
= text
['insertbackground']
155 red
, green
, blue
= ColorDB
.rrggbb_to_triplet(color
)
156 except ColorDB
.BadColor
:
157 # must have been a color name
158 red
, green
, blue
= self
.__sb
.colordb().find_byname(color
)
159 self
.__sb
.update_views(red
, green
, blue
)
161 def update_yourself(self
, red
, green
, blue
):
162 if self
.__trackp
.get():
163 colorname
= ColorDB
.triplet_to_rrggbb((red
, green
, blue
))
164 which
= self
.__which
.get()
167 text
.configure(foreground
=colorname
)
169 text
.configure(background
=colorname
)
171 text
.configure(selectforeground
=colorname
)
173 text
.configure(selectbackground
=colorname
)
175 text
.configure(insertbackground
=colorname
)
177 def save_options(self
, optiondb
):
178 optiondb
['TRACKP'] = self
.__trackp
.get()
179 optiondb
['WHICH'] = self
.__which
.get()
180 optiondb
['TEXT'] = self
.__text
.get(0.0, 'end - 1c')
181 optiondb
['TEXTSEL'] = self
.__text
.tag_ranges(SEL
)[0:2]
182 optiondb
['TEXTINS'] = self
.__text
.index(INSERT
)
183 optiondb
['TEXTFG'] = self
.__text
['foreground']
184 optiondb
['TEXTBG'] = self
.__text
['background']
185 optiondb
['TEXT_SFG'] = self
.__text
['selectforeground']
186 optiondb
['TEXT_SBG'] = self
.__text
['selectbackground']
187 optiondb
['TEXT_IBG'] = self
.__text
['insertbackground']