blob: bbecd9bb898fbca48b4baeea834a1318251a3180 [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 Warsaw2e6c13f1998-02-10 00:13:06 +000015from Tkinter import *
Barry Warsaw4a445c61998-02-11 17:11:34 +000016import string
Barry Warsaw885b3711998-09-29 19:51:18 +000017import re
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000018
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)
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 Warsaw42020191998-09-29 20:02:27 +000038 self.__x.bind_class('Update' , '<Key>', self.__maybeupdate)
Barry Warsaw885b3711998-09-29 19:51:18 +000039 # 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 Warsaw885b3711998-09-29 19:51:18 +000045 # 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 Warsaw0fa6e4b1998-10-01 02:57:05 +000051 # 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 Warsaw85e69651998-10-01 13:41:05 +000059 variable=self.__hexp,
60 command=self.__togglehex)
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000061 self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000062
Barry Warsaw85e69651998-10-01 13:41:05 +000063 def __togglehex(self, event=None):
Barry Warsawc2d8f571998-10-06 16:08:39 +000064 red, green, blue = self.__sb.current_rgb()
65 self.update_yourself(red, green, blue)
Barry Warsaw85e69651998-10-01 13:41:05 +000066
Barry Warsaw885b3711998-09-29 19:51:18 +000067 def __normalize(self, event=None):
68 ew = event.widget
69 contents = ew.get()
Barry Warsawa3989241998-10-06 15:47:45 +000070 icursor = ew.index(INSERT)
Barry Warsaw885b3711998-09-29 19:51:18 +000071 if contents == '':
72 contents = '0'
Barry Warsaw8934af01998-10-07 02:44:17 +000073 if contents[0] in 'xX' and self.__hexp.get():
74 contents = '0' + contents
Barry Warsaw885b3711998-09-29 19:51:18 +000075 # figure out what the contents value is in the current base
76 try:
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000077 if self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +000078 v = string.atoi(contents, 16)
79 else:
80 v = string.atoi(contents)
81 except ValueError:
82 v = None
Barry Warsaw85e69651998-10-01 13:41:05 +000083 # if value is not legal, delete the last character inserted and ring
84 # the bell
Barry Warsaw885b3711998-09-29 19:51:18 +000085 if v is None or v < 0 or v > 255:
Barry Warsaw85e69651998-10-01 13:41:05 +000086 i = ew.index(INSERT)
Barry Warsaw8934af01998-10-07 02:44:17 +000087 if event.char:
88 contents = contents[:i-1] + contents[i:]
89 icursor = icursor-1
Barry Warsaw885b3711998-09-29 19:51:18 +000090 ew.bell()
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000091 elif self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +000092 contents = hex(v)
93 else:
94 contents = int(v)
95 ew.delete(0, END)
96 ew.insert(0, contents)
Barry Warsawa3989241998-10-06 15:47:45 +000097 ew.icursor(icursor)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000098
Barry Warsaw42020191998-09-29 20:02:27 +000099 def __maybeupdate(self, event=None):
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000100 if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
Barry Warsaw42020191998-09-29 20:02:27 +0000101 self.__update(event)
102
Barry Warsaw885b3711998-09-29 19:51:18 +0000103 def __update(self, event=None):
104 redstr = self.__x.get()
105 greenstr = self.__y.get()
106 bluestr = self.__z.get()
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000107 if self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +0000108 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 Warsaw2e6c13f1998-02-10 00:13:06 +0000114
Barry Warsaw885b3711998-09-29 19:51:18 +0000115 def update_yourself(self, red, green, blue):
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000116 if self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +0000117 redstr, greenstr, bluestr = map(hex, (red, green, blue))
118 else:
Barry Warsaw85e69651998-10-01 13:41:05 +0000119 redstr, greenstr, bluestr = red, green, blue
Barry Warsaw8934af01998-10-07 02:44:17 +0000120 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 Warsaw6831c301998-10-06 15:49:19 +0000133
Barry Warsaw73290df1998-10-06 15:50:36 +0000134 def hexp_var(self):
135 return self.__hexp
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000136
137 def save_options(self, optiondb):
138 optiondb['HEXTYPE'] = self.__hexp.get()
139 optiondb['UPWHILETYPE'] = self.__uwtyping.get()