Barry Warsaw | 4638c5b | 1998-10-02 16:20:14 +0000 | [diff] [blame] | 1 | """TextViewer class. |
| 2 | |
| 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. |
| 5 | |
| 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. |
| 10 | |
| 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). |
| 16 | """ |
| 17 | |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 18 | from Tkinter import * |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 19 | import ColorDB |
| 20 | |
| 21 | class 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 Warsaw | 7c51a9a | 1998-10-05 21:31:37 +0000 | [diff] [blame] | 26 | root.title('Pynche Text Window') |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 27 | root.iconname('Pynche Text Window') |
| 28 | root.bind('<Alt-q>', self.__quit) |
| 29 | root.bind('<Alt-Q>', self.__quit) |
Barry Warsaw | 964fbbb | 1998-10-05 21:15:08 +0000 | [diff] [blame] | 30 | root.bind('<Alt-w>', self.__withdraw) |
| 31 | root.bind('<Alt-W>', self.__withdraw) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 32 | # |
| 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, '''\ |
| 41 | Insert some stuff here and play |
| 42 | with the buttons below to see |
| 43 | how the colors interact in |
Barry Warsaw | da7502e | 1998-10-06 21:40:22 +0000 | [diff] [blame] | 44 | textual displays. |
| 45 | |
| 46 | See how the selection can also |
| 47 | be affected by tickling the buttons |
| 48 | and choosing a color.''') |
| 49 | self.__text.tag_add(SEL, 6.0, END) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 50 | # |
| 51 | # variables |
| 52 | self.__trackp = BooleanVar() |
| 53 | self.__trackp.set(0) |
| 54 | self.__which = IntVar() |
Barry Warsaw | 4638c5b | 1998-10-02 16:20:14 +0000 | [diff] [blame] | 55 | self.__which.set(0) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 56 | # |
| 57 | # track toggle |
| 58 | self.__t = Checkbutton(root, text='Track color changes', |
| 59 | variable=self.__trackp, |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 60 | relief=GROOVE, |
| 61 | command=self.__toggletrack) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 62 | 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 Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 93 | self.__toggletrack() |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 94 | |
| 95 | def __quit(self, event=None): |
Barry Warsaw | 45c8d34 | 1998-10-06 19:48:35 +0000 | [diff] [blame] | 96 | self.__root.quit() |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 97 | |
| 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 Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 107 | 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 Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 119 | def update_yourself(self, red, green, blue): |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 120 | 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) |