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