blob: 254e7e0478e492ce75fc5d9f0230c3552f3b24d6 [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
Barry Warsawda7502e1998-10-06 21:40:22 +000044textual displays.
45
46See how the selection can also
47be affected by tickling the buttons
48and choosing a color.''')
49 self.__text.tag_add(SEL, 6.0, END)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000050 #
51 # variables
52 self.__trackp = BooleanVar()
53 self.__trackp.set(0)
54 self.__which = IntVar()
Barry Warsaw4638c5b1998-10-02 16:20:14 +000055 self.__which.set(0)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000056 #
57 # track toggle
58 self.__t = Checkbutton(root, text='Track color changes',
59 variable=self.__trackp,
Barry Warsaw3a476e91998-10-06 18:46:57 +000060 relief=GROOVE,
61 command=self.__toggletrack)
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000062 self.__t.pack(fill=X, expand=YES)
63 frame = self.__frame = Frame(root)
64 frame.pack()
65 #
66 # labels
67 self.__labels = []
68 row = 2
69 for text in ('Text:', 'Selection:', 'Insertion:'):
70 l = Label(frame, text=text)
71 l.grid(row=row, column=0, sticky=E)
72 self.__labels.append(l)
73 row = row + 1
74 col = 1
75 for text in ('Foreground', 'Background'):
76 l = Label(frame, text=text)
77 l.grid(row=1, column=col)
78 self.__labels.append(l)
79 col = col + 1
80 #
81 # radios
82 self.__radios = []
83 val = 0
84 for col in (1, 2):
85 for row in (2, 3, 4):
86 # there is no insertforeground option
87 if row==4 and col==1:
88 continue
89 r = Radiobutton(frame, variable=self.__which,
90 value=(row-2)*2 + col-1)
91 r.grid(row=row, column=col)
92 self.__radios.append(r)
Barry Warsaw3a476e91998-10-06 18:46:57 +000093 self.__toggletrack()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000094
95 def __quit(self, event=None):
Barry Warsaw45c8d341998-10-06 19:48:35 +000096 self.__root.quit()
Barry Warsawf9c6ddc1998-10-01 16:45:32 +000097
98 def __withdraw(self, event=None):
99 self.__root.withdraw()
100
101 def deiconify(self, event=None):
102 self.__root.deiconify()
103
104 def __forceupdate(self, event=None):
105 self.__sb.update_views_current()
106
Barry Warsaw3a476e91998-10-06 18:46:57 +0000107 def __toggletrack(self, event=None):
108 if self.__trackp.get():
109 state = NORMAL
110 fg = self.__radios[0]['foreground']
111 else:
112 state = DISABLED
113 fg = self.__radios[0]['disabledforeground']
114 for r in self.__radios:
115 r.configure(state=state)
116 for l in self.__labels:
117 l.configure(foreground=fg)
118
Barry Warsawf9c6ddc1998-10-01 16:45:32 +0000119 def update_yourself(self, red, green, blue):
Barry Warsaw3a476e91998-10-06 18:46:57 +0000120 if self.__trackp.get():
121 colorname = ColorDB.triplet_to_rrggbb((red, green, blue))
122 which = self.__which.get()
123 if which == 0:
124 self.__text.configure(foreground=colorname)
125 elif which == 1:
126 self.__text.configure(background=colorname)
127 elif which == 2:
128 self.__text.configure(selectforeground=colorname)
129 elif which == 3:
130 self.__text.configure(selectbackground=colorname)
131 elif which == 5:
132 self.__text.configure(insertbackground=colorname)