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