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