blob: baa1e62ca760309f3891a4aa5b4eb95fdf0100b3 [file] [log] [blame]
Barry Warsaw4638c5b1998-10-02 16:20:14 +00001"""TextViewer class.
2
3The TextViewer allows you to see how the selected color would affect various
4characteristics of a Tk text widget. This is an output viewer only.
5
6In the top part of the window is a standard text widget with some sample text
Barry Warsaw179048f2001-07-10 21:44:59 +00007in it. You are free to edit this text in any way you want (BAW: allow you to
Barry Warsaw4638c5b1998-10-02 16:20:14 +00008change font characteristics). If you want changes in other viewers to update
9text characteristics, turn on Track color changes.
10
11To select which characteristic tracks the change, select one of the radio
12buttons in the window below. Text foreground and background affect the text
13in the window above. The Selection is what you see when you click the middle
14button and drag it through some text. The Insertion is the insertion cursor
15in the text window (which only has a background).
16"""
17
Georg Brandlbf76ce12010-08-02 22:08:58 +000018from tkinter import *
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000019import ColorDB
20
Barry Warsawf3ebf901999-04-27 18:54:12 +000021ADDTOVIEW = 'Text Window...'
22
Barry Warsaw179048f2001-07-10 21:44:59 +000023
24
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000025class TextViewer:
Barry Warsawca07ba01998-10-22 03:25:59 +000026 def __init__(self, switchboard, master=None):
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000027 self.__sb = switchboard
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000028 optiondb = switchboard.optiondb()
Barry Warsawca07ba01998-10-22 03:25:59 +000029 root = self.__root = Toplevel(master, class_='Pynche')
30 root.protocol('WM_DELETE_WINDOW', self.withdraw)
Barry Warsaw7c51a9a1998-10-05 21:31:37 +000031 root.title('Pynche Text Window')
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000032 root.iconname('Pynche Text Window')
33 root.bind('<Alt-q>', self.__quit)
34 root.bind('<Alt-Q>', self.__quit)
Barry Warsawca07ba01998-10-22 03:25:59 +000035 root.bind('<Alt-w>', self.withdraw)
36 root.bind('<Alt-W>', self.withdraw)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000037 #
38 # create the text widget
39 #
40 self.__text = Text(root, relief=SUNKEN,
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000041 background=optiondb.get('TEXTBG', 'black'),
42 foreground=optiondb.get('TEXTFG', 'white'),
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000043 width=35, height=15)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000044 sfg = optiondb.get('TEXT_SFG')
45 if sfg:
46 self.__text.configure(selectforeground=sfg)
47 sbg = optiondb.get('TEXT_SBG')
48 if sbg:
49 self.__text.configure(selectbackground=sbg)
50 ibg = optiondb.get('TEXT_IBG')
51 if ibg:
52 self.__text.configure(insertbackground=ibg)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000053 self.__text.pack()
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000054 self.__text.insert(0.0, optiondb.get('TEXT', '''\
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000055Insert some stuff here and play
56with the buttons below to see
57how the colors interact in
Barry Warsawda7502e1998-10-06 21:40:22 +000058textual displays.
59
60See how the selection can also
61be affected by tickling the buttons
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000062and choosing a color.'''))
63 insert = optiondb.get('TEXTINS')
64 if insert:
65 self.__text.mark_set(INSERT, insert)
66 try:
67 start, end = optiondb.get('TEXTSEL', (6.0, END))
68 self.__text.tag_add(SEL, start, end)
69 except ValueError:
70 # selection wasn't set
71 pass
72 self.__text.focus_set()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000073 #
74 # variables
75 self.__trackp = BooleanVar()
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000076 self.__trackp.set(optiondb.get('TRACKP', 0))
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000077 self.__which = IntVar()
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000078 self.__which.set(optiondb.get('WHICH', 0))
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000079 #
80 # track toggle
81 self.__t = Checkbutton(root, text='Track color changes',
82 variable=self.__trackp,
Barry Warsaw3a476e91998-10-06 18:46:57 +000083 relief=GROOVE,
84 command=self.__toggletrack)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000085 self.__t.pack(fill=X, expand=YES)
86 frame = self.__frame = Frame(root)
87 frame.pack()
88 #
89 # labels
90 self.__labels = []
91 row = 2
92 for text in ('Text:', 'Selection:', 'Insertion:'):
93 l = Label(frame, text=text)
94 l.grid(row=row, column=0, sticky=E)
95 self.__labels.append(l)
Barry Warsaw179048f2001-07-10 21:44:59 +000096 row += 1
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000097 col = 1
98 for text in ('Foreground', 'Background'):
99 l = Label(frame, text=text)
100 l.grid(row=1, column=col)
101 self.__labels.append(l)
Barry Warsaw179048f2001-07-10 21:44:59 +0000102 col += 1
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000103 #
104 # radios
105 self.__radios = []
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000106 for col in (1, 2):
107 for row in (2, 3, 4):
108 # there is no insertforeground option
109 if row==4 and col==1:
110 continue
111 r = Radiobutton(frame, variable=self.__which,
Barry Warsaw7b281221999-04-27 15:56:53 +0000112 value=(row-2)*2 + col-1,
113 command=self.__set_color)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000114 r.grid(row=row, column=col)
115 self.__radios.append(r)
Barry Warsaw3a476e91998-10-06 18:46:57 +0000116 self.__toggletrack()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000117
118 def __quit(self, event=None):
Barry Warsaw45c8d341998-10-06 19:48:35 +0000119 self.__root.quit()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000120
Barry Warsawca07ba01998-10-22 03:25:59 +0000121 def withdraw(self, event=None):
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000122 self.__root.withdraw()
123
124 def deiconify(self, event=None):
125 self.__root.deiconify()
126
127 def __forceupdate(self, event=None):
128 self.__sb.update_views_current()
129
Barry Warsaw3a476e91998-10-06 18:46:57 +0000130 def __toggletrack(self, event=None):
131 if self.__trackp.get():
132 state = NORMAL
133 fg = self.__radios[0]['foreground']
134 else:
135 state = DISABLED
136 fg = self.__radios[0]['disabledforeground']
137 for r in self.__radios:
138 r.configure(state=state)
139 for l in self.__labels:
140 l.configure(foreground=fg)
141
Barry Warsaw7b281221999-04-27 15:56:53 +0000142 def __set_color(self, event=None):
143 which = self.__which.get()
144 text = self.__text
145 if which == 0:
146 color = text['foreground']
147 elif which == 1:
148 color = text['background']
149 elif which == 2:
150 color = text['selectforeground']
151 elif which == 3:
152 color = text['selectbackground']
153 elif which == 5:
154 color = text['insertbackground']
155 try:
156 red, green, blue = ColorDB.rrggbb_to_triplet(color)
157 except ColorDB.BadColor:
158 # must have been a color name
159 red, green, blue = self.__sb.colordb().find_byname(color)
160 self.__sb.update_views(red, green, blue)
161
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000162 def update_yourself(self, red, green, blue):
Barry Warsaw3a476e91998-10-06 18:46:57 +0000163 if self.__trackp.get():
164 colorname = ColorDB.triplet_to_rrggbb((red, green, blue))
165 which = self.__which.get()
Barry Warsaw7b281221999-04-27 15:56:53 +0000166 text = self.__text
Barry Warsaw3a476e91998-10-06 18:46:57 +0000167 if which == 0:
Barry Warsaw7b281221999-04-27 15:56:53 +0000168 text.configure(foreground=colorname)
Barry Warsaw3a476e91998-10-06 18:46:57 +0000169 elif which == 1:
Barry Warsaw7b281221999-04-27 15:56:53 +0000170 text.configure(background=colorname)
Barry Warsaw3a476e91998-10-06 18:46:57 +0000171 elif which == 2:
Barry Warsaw7b281221999-04-27 15:56:53 +0000172 text.configure(selectforeground=colorname)
Barry Warsaw3a476e91998-10-06 18:46:57 +0000173 elif which == 3:
Barry Warsaw7b281221999-04-27 15:56:53 +0000174 text.configure(selectbackground=colorname)
Barry Warsaw3a476e91998-10-06 18:46:57 +0000175 elif which == 5:
Barry Warsaw7b281221999-04-27 15:56:53 +0000176 text.configure(insertbackground=colorname)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000177
178 def save_options(self, optiondb):
179 optiondb['TRACKP'] = self.__trackp.get()
180 optiondb['WHICH'] = self.__which.get()
181 optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c')
182 optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2]
183 optiondb['TEXTINS'] = self.__text.index(INSERT)
184 optiondb['TEXTFG'] = self.__text['foreground']
185 optiondb['TEXTBG'] = self.__text['background']
186 optiondb['TEXT_SFG'] = self.__text['selectforeground']
187 optiondb['TEXT_SBG'] = self.__text['selectbackground']
188 optiondb['TEXT_IBG'] = self.__text['insertbackground']