| 1 | n/a | """TypeinViewer class. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | The TypeinViewer is what you see at the lower right of the main Pynche |
|---|
| 4 | n/a | widget. It contains three text entry fields, one each for red, green, blue. |
|---|
| 5 | n/a | Input into these windows is highly constrained; it only allows you to enter |
|---|
| 6 | n/a | values that are legal for a color axis. This usually means 0-255 for decimal |
|---|
| 7 | n/a | input and 0x0 - 0xff for hex input. |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | You can toggle whether you want to view and input the values in either decimal |
|---|
| 10 | n/a | or hex by clicking on Hexadecimal. By clicking on Update while typing, the |
|---|
| 11 | n/a | color selection will be made on every change to the text field. Otherwise, |
|---|
| 12 | n/a | you must hit Return or Tab to select the color. |
|---|
| 13 | n/a | """ |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | from tkinter import * |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | class TypeinViewer: |
|---|
| 20 | n/a | def __init__(self, switchboard, master=None): |
|---|
| 21 | n/a | # non-gui ivars |
|---|
| 22 | n/a | self.__sb = switchboard |
|---|
| 23 | n/a | optiondb = switchboard.optiondb() |
|---|
| 24 | n/a | self.__hexp = BooleanVar() |
|---|
| 25 | n/a | self.__hexp.set(optiondb.get('HEXTYPE', 0)) |
|---|
| 26 | n/a | self.__uwtyping = BooleanVar() |
|---|
| 27 | n/a | self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0)) |
|---|
| 28 | n/a | # create the gui |
|---|
| 29 | n/a | self.__frame = Frame(master, relief=RAISED, borderwidth=1) |
|---|
| 30 | n/a | self.__frame.grid(row=3, column=1, sticky='NSEW') |
|---|
| 31 | n/a | # Red |
|---|
| 32 | n/a | self.__xl = Label(self.__frame, text='Red:') |
|---|
| 33 | n/a | self.__xl.grid(row=0, column=0, sticky=E) |
|---|
| 34 | n/a | subframe = Frame(self.__frame) |
|---|
| 35 | n/a | subframe.grid(row=0, column=1) |
|---|
| 36 | n/a | self.__xox = Label(subframe, text='0x') |
|---|
| 37 | n/a | self.__xox.grid(row=0, column=0, sticky=E) |
|---|
| 38 | n/a | self.__xox['font'] = 'courier' |
|---|
| 39 | n/a | self.__x = Entry(subframe, width=3) |
|---|
| 40 | n/a | self.__x.grid(row=0, column=1) |
|---|
| 41 | n/a | self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update')) |
|---|
| 42 | n/a | self.__x.bind_class('Normalize', '<Key>', self.__normalize) |
|---|
| 43 | n/a | self.__x.bind_class('Update' , '<Key>', self.__maybeupdate) |
|---|
| 44 | n/a | # Green |
|---|
| 45 | n/a | self.__yl = Label(self.__frame, text='Green:') |
|---|
| 46 | n/a | self.__yl.grid(row=1, column=0, sticky=E) |
|---|
| 47 | n/a | subframe = Frame(self.__frame) |
|---|
| 48 | n/a | subframe.grid(row=1, column=1) |
|---|
| 49 | n/a | self.__yox = Label(subframe, text='0x') |
|---|
| 50 | n/a | self.__yox.grid(row=0, column=0, sticky=E) |
|---|
| 51 | n/a | self.__yox['font'] = 'courier' |
|---|
| 52 | n/a | self.__y = Entry(subframe, width=3) |
|---|
| 53 | n/a | self.__y.grid(row=0, column=1) |
|---|
| 54 | n/a | self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update')) |
|---|
| 55 | n/a | # Blue |
|---|
| 56 | n/a | self.__zl = Label(self.__frame, text='Blue:') |
|---|
| 57 | n/a | self.__zl.grid(row=2, column=0, sticky=E) |
|---|
| 58 | n/a | subframe = Frame(self.__frame) |
|---|
| 59 | n/a | subframe.grid(row=2, column=1) |
|---|
| 60 | n/a | self.__zox = Label(subframe, text='0x') |
|---|
| 61 | n/a | self.__zox.grid(row=0, column=0, sticky=E) |
|---|
| 62 | n/a | self.__zox['font'] = 'courier' |
|---|
| 63 | n/a | self.__z = Entry(subframe, width=3) |
|---|
| 64 | n/a | self.__z.grid(row=0, column=1) |
|---|
| 65 | n/a | self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update')) |
|---|
| 66 | n/a | # Update while typing? |
|---|
| 67 | n/a | self.__uwt = Checkbutton(self.__frame, |
|---|
| 68 | n/a | text='Update while typing', |
|---|
| 69 | n/a | variable=self.__uwtyping) |
|---|
| 70 | n/a | self.__uwt.grid(row=3, column=0, columnspan=2, sticky=W) |
|---|
| 71 | n/a | # Hex/Dec |
|---|
| 72 | n/a | self.__hex = Checkbutton(self.__frame, |
|---|
| 73 | n/a | text='Hexadecimal', |
|---|
| 74 | n/a | variable=self.__hexp, |
|---|
| 75 | n/a | command=self.__togglehex) |
|---|
| 76 | n/a | self.__hex.grid(row=4, column=0, columnspan=2, sticky=W) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def __togglehex(self, event=None): |
|---|
| 79 | n/a | red, green, blue = self.__sb.current_rgb() |
|---|
| 80 | n/a | if self.__hexp.get(): |
|---|
| 81 | n/a | label = '0x' |
|---|
| 82 | n/a | else: |
|---|
| 83 | n/a | label = ' ' |
|---|
| 84 | n/a | self.__xox['text'] = label |
|---|
| 85 | n/a | self.__yox['text'] = label |
|---|
| 86 | n/a | self.__zox['text'] = label |
|---|
| 87 | n/a | self.update_yourself(red, green, blue) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | def __normalize(self, event=None): |
|---|
| 90 | n/a | ew = event.widget |
|---|
| 91 | n/a | contents = ew.get() |
|---|
| 92 | n/a | icursor = ew.index(INSERT) |
|---|
| 93 | n/a | if contents and contents[0] in 'xX' and self.__hexp.get(): |
|---|
| 94 | n/a | contents = '0' + contents |
|---|
| 95 | n/a | # Figure out the contents in the current base. |
|---|
| 96 | n/a | try: |
|---|
| 97 | n/a | if self.__hexp.get(): |
|---|
| 98 | n/a | v = int(contents, 16) |
|---|
| 99 | n/a | else: |
|---|
| 100 | n/a | v = int(contents) |
|---|
| 101 | n/a | except ValueError: |
|---|
| 102 | n/a | v = None |
|---|
| 103 | n/a | # If value is not legal, or empty, delete the last character inserted |
|---|
| 104 | n/a | # and ring the bell. Don't ring the bell if the field is empty (it'll |
|---|
| 105 | n/a | # just equal zero. |
|---|
| 106 | n/a | if v is None: |
|---|
| 107 | n/a | pass |
|---|
| 108 | n/a | elif v < 0 or v > 255: |
|---|
| 109 | n/a | i = ew.index(INSERT) |
|---|
| 110 | n/a | if event.char: |
|---|
| 111 | n/a | contents = contents[:i-1] + contents[i:] |
|---|
| 112 | n/a | icursor -= 1 |
|---|
| 113 | n/a | ew.bell() |
|---|
| 114 | n/a | elif self.__hexp.get(): |
|---|
| 115 | n/a | contents = hex(v)[2:] |
|---|
| 116 | n/a | else: |
|---|
| 117 | n/a | contents = int(v) |
|---|
| 118 | n/a | ew.delete(0, END) |
|---|
| 119 | n/a | ew.insert(0, contents) |
|---|
| 120 | n/a | ew.icursor(icursor) |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | def __maybeupdate(self, event=None): |
|---|
| 123 | n/a | if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'): |
|---|
| 124 | n/a | self.__update(event) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def __update(self, event=None): |
|---|
| 127 | n/a | redstr = self.__x.get() or '0' |
|---|
| 128 | n/a | greenstr = self.__y.get() or '0' |
|---|
| 129 | n/a | bluestr = self.__z.get() or '0' |
|---|
| 130 | n/a | if self.__hexp.get(): |
|---|
| 131 | n/a | base = 16 |
|---|
| 132 | n/a | else: |
|---|
| 133 | n/a | base = 10 |
|---|
| 134 | n/a | red, green, blue = [int(x, base) for x in (redstr, greenstr, bluestr)] |
|---|
| 135 | n/a | self.__sb.update_views(red, green, blue) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | def update_yourself(self, red, green, blue): |
|---|
| 138 | n/a | if self.__hexp.get(): |
|---|
| 139 | n/a | sred, sgreen, sblue = [hex(x)[2:] for x in (red, green, blue)] |
|---|
| 140 | n/a | else: |
|---|
| 141 | n/a | sred, sgreen, sblue = red, green, blue |
|---|
| 142 | n/a | x, y, z = self.__x, self.__y, self.__z |
|---|
| 143 | n/a | xicursor = x.index(INSERT) |
|---|
| 144 | n/a | yicursor = y.index(INSERT) |
|---|
| 145 | n/a | zicursor = z.index(INSERT) |
|---|
| 146 | n/a | x.delete(0, END) |
|---|
| 147 | n/a | y.delete(0, END) |
|---|
| 148 | n/a | z.delete(0, END) |
|---|
| 149 | n/a | x.insert(0, sred) |
|---|
| 150 | n/a | y.insert(0, sgreen) |
|---|
| 151 | n/a | z.insert(0, sblue) |
|---|
| 152 | n/a | x.icursor(xicursor) |
|---|
| 153 | n/a | y.icursor(yicursor) |
|---|
| 154 | n/a | z.icursor(zicursor) |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def hexp_var(self): |
|---|
| 157 | n/a | return self.__hexp |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | def save_options(self, optiondb): |
|---|
| 160 | n/a | optiondb['HEXTYPE'] = self.__hexp.get() |
|---|
| 161 | n/a | optiondb['UPWHILETYPE'] = self.__uwtyping.get() |
|---|