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 | |
Barry Warsaw | f3ebf90 | 1999-04-27 18:54:12 +0000 | [diff] [blame] | 21 | ADDTOVIEW = 'Text Window...' |
| 22 | |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 23 | class TextViewer: |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 24 | def __init__(self, switchboard, master=None): |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 25 | self.__sb = switchboard |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 26 | optiondb = switchboard.optiondb() |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 27 | root = self.__root = Toplevel(master, class_='Pynche') |
| 28 | root.protocol('WM_DELETE_WINDOW', self.withdraw) |
Barry Warsaw | 7c51a9a | 1998-10-05 21:31:37 +0000 | [diff] [blame] | 29 | root.title('Pynche Text Window') |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 30 | root.iconname('Pynche Text Window') |
| 31 | root.bind('<Alt-q>', self.__quit) |
| 32 | root.bind('<Alt-Q>', self.__quit) |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 33 | root.bind('<Alt-w>', self.withdraw) |
| 34 | root.bind('<Alt-W>', self.withdraw) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 35 | # |
| 36 | # create the text widget |
| 37 | # |
| 38 | self.__text = Text(root, relief=SUNKEN, |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 39 | background=optiondb.get('TEXTBG', 'black'), |
| 40 | foreground=optiondb.get('TEXTFG', 'white'), |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 41 | width=35, height=15) |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 42 | sfg = optiondb.get('TEXT_SFG') |
| 43 | if sfg: |
| 44 | self.__text.configure(selectforeground=sfg) |
| 45 | sbg = optiondb.get('TEXT_SBG') |
| 46 | if sbg: |
| 47 | self.__text.configure(selectbackground=sbg) |
| 48 | ibg = optiondb.get('TEXT_IBG') |
| 49 | if ibg: |
| 50 | self.__text.configure(insertbackground=ibg) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 51 | self.__text.pack() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 52 | self.__text.insert(0.0, optiondb.get('TEXT', '''\ |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 53 | Insert some stuff here and play |
| 54 | with the buttons below to see |
| 55 | how the colors interact in |
Barry Warsaw | da7502e | 1998-10-06 21:40:22 +0000 | [diff] [blame] | 56 | textual displays. |
| 57 | |
| 58 | See how the selection can also |
| 59 | be affected by tickling the buttons |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 60 | and choosing a color.''')) |
| 61 | insert = optiondb.get('TEXTINS') |
| 62 | if insert: |
| 63 | self.__text.mark_set(INSERT, insert) |
| 64 | try: |
| 65 | start, end = optiondb.get('TEXTSEL', (6.0, END)) |
| 66 | self.__text.tag_add(SEL, start, end) |
| 67 | except ValueError: |
| 68 | # selection wasn't set |
| 69 | pass |
| 70 | self.__text.focus_set() |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 71 | # |
| 72 | # variables |
| 73 | self.__trackp = BooleanVar() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 74 | self.__trackp.set(optiondb.get('TRACKP', 0)) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 75 | self.__which = IntVar() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 76 | self.__which.set(optiondb.get('WHICH', 0)) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 77 | # |
| 78 | # track toggle |
| 79 | self.__t = Checkbutton(root, text='Track color changes', |
| 80 | variable=self.__trackp, |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 81 | relief=GROOVE, |
| 82 | command=self.__toggletrack) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 83 | self.__t.pack(fill=X, expand=YES) |
| 84 | frame = self.__frame = Frame(root) |
| 85 | frame.pack() |
| 86 | # |
| 87 | # labels |
| 88 | self.__labels = [] |
| 89 | row = 2 |
| 90 | for text in ('Text:', 'Selection:', 'Insertion:'): |
| 91 | l = Label(frame, text=text) |
| 92 | l.grid(row=row, column=0, sticky=E) |
| 93 | self.__labels.append(l) |
| 94 | row = row + 1 |
| 95 | col = 1 |
| 96 | for text in ('Foreground', 'Background'): |
| 97 | l = Label(frame, text=text) |
| 98 | l.grid(row=1, column=col) |
| 99 | self.__labels.append(l) |
| 100 | col = col + 1 |
| 101 | # |
| 102 | # radios |
| 103 | self.__radios = [] |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 104 | for col in (1, 2): |
| 105 | for row in (2, 3, 4): |
| 106 | # there is no insertforeground option |
| 107 | if row==4 and col==1: |
| 108 | continue |
| 109 | r = Radiobutton(frame, variable=self.__which, |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 110 | value=(row-2)*2 + col-1, |
| 111 | command=self.__set_color) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 112 | r.grid(row=row, column=col) |
| 113 | self.__radios.append(r) |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 114 | self.__toggletrack() |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 115 | |
| 116 | def __quit(self, event=None): |
Barry Warsaw | 45c8d34 | 1998-10-06 19:48:35 +0000 | [diff] [blame] | 117 | self.__root.quit() |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 118 | |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 119 | def withdraw(self, event=None): |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 120 | self.__root.withdraw() |
| 121 | |
| 122 | def deiconify(self, event=None): |
| 123 | self.__root.deiconify() |
| 124 | |
| 125 | def __forceupdate(self, event=None): |
| 126 | self.__sb.update_views_current() |
| 127 | |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 128 | def __toggletrack(self, event=None): |
| 129 | if self.__trackp.get(): |
| 130 | state = NORMAL |
| 131 | fg = self.__radios[0]['foreground'] |
| 132 | else: |
| 133 | state = DISABLED |
| 134 | fg = self.__radios[0]['disabledforeground'] |
| 135 | for r in self.__radios: |
| 136 | r.configure(state=state) |
| 137 | for l in self.__labels: |
| 138 | l.configure(foreground=fg) |
| 139 | |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 140 | def __set_color(self, event=None): |
| 141 | which = self.__which.get() |
| 142 | text = self.__text |
| 143 | if which == 0: |
| 144 | color = text['foreground'] |
| 145 | elif which == 1: |
| 146 | color = text['background'] |
| 147 | elif which == 2: |
| 148 | color = text['selectforeground'] |
| 149 | elif which == 3: |
| 150 | color = text['selectbackground'] |
| 151 | elif which == 5: |
| 152 | color = text['insertbackground'] |
| 153 | try: |
| 154 | red, green, blue = ColorDB.rrggbb_to_triplet(color) |
| 155 | except ColorDB.BadColor: |
| 156 | # must have been a color name |
| 157 | red, green, blue = self.__sb.colordb().find_byname(color) |
| 158 | self.__sb.update_views(red, green, blue) |
| 159 | |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 160 | def update_yourself(self, red, green, blue): |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 161 | if self.__trackp.get(): |
| 162 | colorname = ColorDB.triplet_to_rrggbb((red, green, blue)) |
| 163 | which = self.__which.get() |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 164 | text = self.__text |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 165 | if which == 0: |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 166 | text.configure(foreground=colorname) |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 167 | elif which == 1: |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 168 | text.configure(background=colorname) |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 169 | elif which == 2: |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 170 | text.configure(selectforeground=colorname) |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 171 | elif which == 3: |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 172 | text.configure(selectbackground=colorname) |
Barry Warsaw | 3a476e9 | 1998-10-06 18:46:57 +0000 | [diff] [blame] | 173 | elif which == 5: |
Barry Warsaw | 7b28122 | 1999-04-27 15:56:53 +0000 | [diff] [blame] | 174 | text.configure(insertbackground=colorname) |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 175 | |
| 176 | def save_options(self, optiondb): |
| 177 | optiondb['TRACKP'] = self.__trackp.get() |
| 178 | optiondb['WHICH'] = self.__which.get() |
| 179 | optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c') |
| 180 | optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2] |
| 181 | optiondb['TEXTINS'] = self.__text.index(INSERT) |
| 182 | optiondb['TEXTFG'] = self.__text['foreground'] |
| 183 | optiondb['TEXTBG'] = self.__text['background'] |
| 184 | optiondb['TEXT_SFG'] = self.__text['selectforeground'] |
| 185 | optiondb['TEXT_SBG'] = self.__text['selectbackground'] |
| 186 | optiondb['TEXT_IBG'] = self.__text['insertbackground'] |