ยปCore Development>Code coverage>Tools/pynche/TextViewer.py

Python code coverage for Tools/pynche/TextViewer.py

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