blob: d56c1b32c09f7bf61f887d14634714f1ccead0fc [file] [log] [blame]
Barry Warsaw69a4ab51998-10-02 16:27:40 +00001"""TypeinViewer class.
2
3The TypeinViewer is what you see at the lower right of the main Pynche
4widget. It contains three text entry fields, one each for red, green, blue.
5Input into these windows is highly constrained; it only allows you to enter
6values that are legal for a color axis. This usually means 0-255 for decimal
7input and 0x0 - 0xff for hex input.
8
9You can toggle whether you want to view and input the values in either decimal
10or hex by clicking on Hexadecimal. By clicking on Update while typing, the
11color selection will be made on every change to the text field. Otherwise,
12you must hit Return or Tab to select the color.
13"""
14
Barry Warsaw780028e2001-07-10 21:50:44 +000015from Tkinter import *
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000016
Barry Warsaw780028e2001-07-10 21:50:44 +000017
18
Barry Warsaw42020191998-09-29 20:02:27 +000019class TypeinViewer:
Barry Warsawca07ba01998-10-22 03:25:59 +000020 def __init__(self, switchboard, master=None):
Barry Warsaw885b3711998-09-29 19:51:18 +000021 # non-gui ivars
22 self.__sb = switchboard
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000023 optiondb = switchboard.optiondb()
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000024 self.__hexp = BooleanVar()
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000025 self.__hexp.set(optiondb.get('HEXTYPE', 0))
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000026 self.__uwtyping = BooleanVar()
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000027 self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
Barry Warsaw885b3711998-09-29 19:51:18 +000028 # create the gui
Barry Warsawca07ba01998-10-22 03:25:59 +000029 self.__frame = Frame(master, relief=RAISED, borderwidth=1)
Barry Warsawf72d5801998-10-22 18:48:45 +000030 self.__frame.grid(row=3, column=1, sticky='NSEW')
Barry Warsaw885b3711998-09-29 19:51:18 +000031 # Red
32 self.__xl = Label(self.__frame, text='Red:')
33 self.__xl.grid(row=0, column=0, sticky=E)
Barry Warsaw780028e2001-07-10 21:50:44 +000034 subframe = Frame(self.__frame)
35 subframe.grid(row=0, column=1)
36 self.__xox = Label(subframe, text='0x')
37 self.__xox.grid(row=0, column=0, sticky=E)
38 self.__xox['font'] = 'courier'
39 self.__x = Entry(subframe, width=3)
Barry Warsaw885b3711998-09-29 19:51:18 +000040 self.__x.grid(row=0, column=1)
41 self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update'))
42 self.__x.bind_class('Normalize', '<Key>', self.__normalize)
Barry Warsaw42020191998-09-29 20:02:27 +000043 self.__x.bind_class('Update' , '<Key>', self.__maybeupdate)
Barry Warsaw885b3711998-09-29 19:51:18 +000044 # Green
45 self.__yl = Label(self.__frame, text='Green:')
46 self.__yl.grid(row=1, column=0, sticky=E)
Barry Warsaw780028e2001-07-10 21:50:44 +000047 subframe = Frame(self.__frame)
48 subframe.grid(row=1, column=1)
49 self.__yox = Label(subframe, text='0x')
50 self.__yox.grid(row=0, column=0, sticky=E)
51 self.__yox['font'] = 'courier'
52 self.__y = Entry(subframe, width=3)
53 self.__y.grid(row=0, column=1)
Barry Warsaw885b3711998-09-29 19:51:18 +000054 self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update'))
Barry Warsaw885b3711998-09-29 19:51:18 +000055 # Blue
56 self.__zl = Label(self.__frame, text='Blue:')
57 self.__zl.grid(row=2, column=0, sticky=E)
Barry Warsaw780028e2001-07-10 21:50:44 +000058 subframe = Frame(self.__frame)
59 subframe.grid(row=2, column=1)
60 self.__zox = Label(subframe, text='0x')
61 self.__zox.grid(row=0, column=0, sticky=E)
62 self.__zox['font'] = 'courier'
63 self.__z = Entry(subframe, width=3)
64 self.__z.grid(row=0, column=1)
Barry Warsaw885b3711998-09-29 19:51:18 +000065 self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update'))
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000066 # Update while typing?
67 self.__uwt = Checkbutton(self.__frame,
68 text='Update while typing',
69 variable=self.__uwtyping)
70 self.__uwt.grid(row=3, column=0, columnspan=2, sticky=W)
71 # Hex/Dec
72 self.__hex = Checkbutton(self.__frame,
73 text='Hexadecimal',
Barry Warsaw85e69651998-10-01 13:41:05 +000074 variable=self.__hexp,
75 command=self.__togglehex)
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000076 self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000077
Barry Warsaw85e69651998-10-01 13:41:05 +000078 def __togglehex(self, event=None):
Barry Warsawc2d8f571998-10-06 16:08:39 +000079 red, green, blue = self.__sb.current_rgb()
Barry Warsaw780028e2001-07-10 21:50:44 +000080 if self.__hexp.get():
81 label = '0x'
82 else:
83 label = ' '
84 self.__xox['text'] = label
85 self.__yox['text'] = label
86 self.__zox['text'] = label
Barry Warsawc2d8f571998-10-06 16:08:39 +000087 self.update_yourself(red, green, blue)
Barry Warsaw85e69651998-10-01 13:41:05 +000088
Barry Warsaw885b3711998-09-29 19:51:18 +000089 def __normalize(self, event=None):
90 ew = event.widget
91 contents = ew.get()
Barry Warsawa3989241998-10-06 15:47:45 +000092 icursor = ew.index(INSERT)
Barry Warsaw1bbc0482001-02-01 21:31:58 +000093 if contents and contents[0] in 'xX' and self.__hexp.get():
Barry Warsaw8934af01998-10-07 02:44:17 +000094 contents = '0' + contents
Barry Warsaw780028e2001-07-10 21:50:44 +000095 # Figure out the contents in the current base.
Barry Warsaw885b3711998-09-29 19:51:18 +000096 try:
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000097 if self.__hexp.get():
Barry Warsaw780028e2001-07-10 21:50:44 +000098 v = int(contents, 16)
Barry Warsaw885b3711998-09-29 19:51:18 +000099 else:
Barry Warsaw780028e2001-07-10 21:50:44 +0000100 v = int(contents)
Barry Warsaw885b3711998-09-29 19:51:18 +0000101 except ValueError:
102 v = None
Barry Warsaw780028e2001-07-10 21:50:44 +0000103 # If value is not legal, or empty, delete the last character inserted
104 # and ring the bell. Don't ring the bell if the field is empty (it'll
105 # just equal zero.
106 if v is None:
107 pass
108 elif v < 0 or v > 255:
Barry Warsaw85e69651998-10-01 13:41:05 +0000109 i = ew.index(INSERT)
Barry Warsaw8934af01998-10-07 02:44:17 +0000110 if event.char:
111 contents = contents[:i-1] + contents[i:]
Barry Warsaw780028e2001-07-10 21:50:44 +0000112 icursor -= 1
Barry Warsaw885b3711998-09-29 19:51:18 +0000113 ew.bell()
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000114 elif self.__hexp.get():
Barry Warsaw780028e2001-07-10 21:50:44 +0000115 contents = hex(v)[2:]
Barry Warsaw885b3711998-09-29 19:51:18 +0000116 else:
117 contents = int(v)
118 ew.delete(0, END)
119 ew.insert(0, contents)
Barry Warsawa3989241998-10-06 15:47:45 +0000120 ew.icursor(icursor)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +0000121
Barry Warsaw42020191998-09-29 20:02:27 +0000122 def __maybeupdate(self, event=None):
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000123 if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
Barry Warsaw42020191998-09-29 20:02:27 +0000124 self.__update(event)
125
Barry Warsaw885b3711998-09-29 19:51:18 +0000126 def __update(self, event=None):
Barry Warsaw780028e2001-07-10 21:50:44 +0000127 redstr = self.__x.get() or '0'
128 greenstr = self.__y.get() or '0'
129 bluestr = self.__z.get() or '0'
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000130 if self.__hexp.get():
Barry Warsaw780028e2001-07-10 21:50:44 +0000131 base = 16
Barry Warsaw885b3711998-09-29 19:51:18 +0000132 else:
Barry Warsaw780028e2001-07-10 21:50:44 +0000133 base = 10
134 red, green, blue = [int(x, base) for x in (redstr, greenstr, bluestr)]
Barry Warsaw885b3711998-09-29 19:51:18 +0000135 self.__sb.update_views(red, green, blue)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +0000136
Barry Warsaw885b3711998-09-29 19:51:18 +0000137 def update_yourself(self, red, green, blue):
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000138 if self.__hexp.get():
Barry Warsaw780028e2001-07-10 21:50:44 +0000139 sred, sgreen, sblue = [hex(x)[2:] for x in (red, green, blue)]
Barry Warsaw885b3711998-09-29 19:51:18 +0000140 else:
Barry Warsaw780028e2001-07-10 21:50:44 +0000141 sred, sgreen, sblue = red, green, blue
Barry Warsaw8934af01998-10-07 02:44:17 +0000142 x, y, z = self.__x, self.__y, self.__z
143 xicursor = x.index(INSERT)
144 yicursor = y.index(INSERT)
145 zicursor = z.index(INSERT)
146 x.delete(0, END)
147 y.delete(0, END)
148 z.delete(0, END)
Barry Warsaw780028e2001-07-10 21:50:44 +0000149 x.insert(0, sred)
150 y.insert(0, sgreen)
151 z.insert(0, sblue)
Barry Warsaw8934af01998-10-07 02:44:17 +0000152 x.icursor(xicursor)
153 y.icursor(yicursor)
154 z.icursor(zicursor)
Barry Warsaw6831c301998-10-06 15:49:19 +0000155
Barry Warsaw73290df1998-10-06 15:50:36 +0000156 def hexp_var(self):
157 return self.__hexp
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000158
159 def save_options(self, optiondb):
160 optiondb['HEXTYPE'] = self.__hexp.get()
161 optiondb['UPWHILETYPE'] = self.__uwtyping.get()