blob: 316bd1537fca3c34d31082a3e876b6faffde48d2 [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
7in it. You are free to edit this text in any way you want (TBD: allow you to
8change 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
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000018from Tkinter import *
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000019import ColorDB
20
21class TextViewer:
22 def __init__(self, switchboard, parent=None):
23 self.__sb = switchboard
24 root = self.__root = Toplevel(parent, class_='Pynche')
25 root.protocol('WM_DELETE_WINDOW', self.__withdraw)
Barry Warsaw7c51a9a1998-10-05 21:31:37 +000026 root.title('Pynche Text Window')
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000027 root.iconname('Pynche Text Window')
28 root.bind('<Alt-q>', self.__quit)
29 root.bind('<Alt-Q>', self.__quit)
Barry Warsaw964fbbb1998-10-05 21:15:08 +000030 root.bind('<Alt-w>', self.__withdraw)
31 root.bind('<Alt-W>', self.__withdraw)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000032 #
33 # create the text widget
34 #
35 self.__text = Text(root, relief=SUNKEN,
36 background='black',
37 foreground='white',
38 width=35, height=15)
39 self.__text.pack()
40 self.__text.insert(0.0, '''\
41Insert some stuff here and play
42with the buttons below to see
43how the colors interact in
44textual displays.''')
45 #
46 # variables
47 self.__trackp = BooleanVar()
48 self.__trackp.set(0)
49 self.__which = IntVar()
Barry Warsaw4638c5b1998-10-02 16:20:14 +000050 self.__which.set(0)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000051 #
52 # track toggle
53 self.__t = Checkbutton(root, text='Track color changes',
54 variable=self.__trackp,
Barry Warsaw3a476e91998-10-06 18:46:57 +000055 relief=GROOVE,
56 command=self.__toggletrack)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000057 self.__t.pack(fill=X, expand=YES)
58 frame = self.__frame = Frame(root)
59 frame.pack()
60 #
61 # labels
62 self.__labels = []
63 row = 2
64 for text in ('Text:', 'Selection:', 'Insertion:'):
65 l = Label(frame, text=text)
66 l.grid(row=row, column=0, sticky=E)
67 self.__labels.append(l)
68 row = row + 1
69 col = 1
70 for text in ('Foreground', 'Background'):
71 l = Label(frame, text=text)
72 l.grid(row=1, column=col)
73 self.__labels.append(l)
74 col = col + 1
75 #
76 # radios
77 self.__radios = []
78 val = 0
79 for col in (1, 2):
80 for row in (2, 3, 4):
81 # there is no insertforeground option
82 if row==4 and col==1:
83 continue
84 r = Radiobutton(frame, variable=self.__which,
85 value=(row-2)*2 + col-1)
86 r.grid(row=row, column=col)
87 self.__radios.append(r)
Barry Warsaw3a476e91998-10-06 18:46:57 +000088 self.__toggletrack()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000089
90 def __quit(self, event=None):
Barry Warsaw45c8d341998-10-06 19:48:35 +000091 self.__root.quit()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000092
93 def __withdraw(self, event=None):
94 self.__root.withdraw()
95
96 def deiconify(self, event=None):
97 self.__root.deiconify()
98
99 def __forceupdate(self, event=None):
100 self.__sb.update_views_current()
101
Barry Warsaw3a476e91998-10-06 18:46:57 +0000102 def __toggletrack(self, event=None):
103 if self.__trackp.get():
104 state = NORMAL
105 fg = self.__radios[0]['foreground']
106 else:
107 state = DISABLED
108 fg = self.__radios[0]['disabledforeground']
109 for r in self.__radios:
110 r.configure(state=state)
111 for l in self.__labels:
112 l.configure(foreground=fg)
113
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000114 def update_yourself(self, red, green, blue):
Barry Warsaw3a476e91998-10-06 18:46:57 +0000115 if self.__trackp.get():
116 colorname = ColorDB.triplet_to_rrggbb((red, green, blue))
117 which = self.__which.get()
118 if which == 0:
119 self.__text.configure(foreground=colorname)
120 elif which == 1:
121 self.__text.configure(background=colorname)
122 elif which == 2:
123 self.__text.configure(selectforeground=colorname)
124 elif which == 3:
125 self.__text.configure(selectbackground=colorname)
126 elif which == 5:
127 self.__text.configure(insertbackground=colorname)