blob: 6e50f0f9b8c4302cf926429a304a765220e60acb [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 Warsaw885b3711998-09-29 19:51:18 +000020 def __init__(self, switchboard, parent=None):
21 # non-gui ivars
22 self.__sb = switchboard
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000023 self.__hexp = BooleanVar()
24 self.__uwtyping = BooleanVar()
Barry Warsaw885b3711998-09-29 19:51:18 +000025 # create the gui
Barry Warsaw231c8c01998-10-01 14:26:57 +000026 self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2)
27 self.__frame.grid(row=3, column=1, sticky='NS')
Barry Warsaw885b3711998-09-29 19:51:18 +000028 # Red
29 self.__xl = Label(self.__frame, text='Red:')
30 self.__xl.grid(row=0, column=0, sticky=E)
31 self.__x = Entry(self.__frame, width=4)
32 self.__x.grid(row=0, column=1)
33 self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update'))
34 self.__x.bind_class('Normalize', '<Key>', self.__normalize)
Barry Warsaw42020191998-09-29 20:02:27 +000035 self.__x.bind_class('Update' , '<Key>', self.__maybeupdate)
Barry Warsaw885b3711998-09-29 19:51:18 +000036 # Green
37 self.__yl = Label(self.__frame, text='Green:')
38 self.__yl.grid(row=1, column=0, sticky=E)
39 self.__y = Entry(self.__frame, width=4)
40 self.__y.grid(row=1, column=1)
41 self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update'))
Barry Warsaw885b3711998-09-29 19:51:18 +000042 # Blue
43 self.__zl = Label(self.__frame, text='Blue:')
44 self.__zl.grid(row=2, column=0, sticky=E)
45 self.__z = Entry(self.__frame, width=4)
46 self.__z.grid(row=2, column=1)
47 self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update'))
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000048 # Update while typing?
49 self.__uwt = Checkbutton(self.__frame,
50 text='Update while typing',
51 variable=self.__uwtyping)
52 self.__uwt.grid(row=3, column=0, columnspan=2, sticky=W)
53 # Hex/Dec
54 self.__hex = Checkbutton(self.__frame,
55 text='Hexadecimal',
Barry Warsaw85e69651998-10-01 13:41:05 +000056 variable=self.__hexp,
57 command=self.__togglehex)
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000058 self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000059
Barry Warsaw85e69651998-10-01 13:41:05 +000060 def __togglehex(self, event=None):
Barry Warsawc2d8f571998-10-06 16:08:39 +000061 red, green, blue = self.__sb.current_rgb()
62 self.update_yourself(red, green, blue)
Barry Warsaw85e69651998-10-01 13:41:05 +000063
Barry Warsaw885b3711998-09-29 19:51:18 +000064 def __normalize(self, event=None):
65 ew = event.widget
66 contents = ew.get()
Barry Warsawa3989241998-10-06 15:47:45 +000067 icursor = ew.index(INSERT)
Barry Warsaw885b3711998-09-29 19:51:18 +000068 if contents == '':
69 contents = '0'
70 # figure out what the contents value is in the current base
71 try:
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000072 if self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +000073 v = string.atoi(contents, 16)
74 else:
75 v = string.atoi(contents)
76 except ValueError:
77 v = None
Barry Warsaw85e69651998-10-01 13:41:05 +000078 # if value is not legal, delete the last character inserted and ring
79 # the bell
Barry Warsaw885b3711998-09-29 19:51:18 +000080 if v is None or v < 0 or v > 255:
Barry Warsaw85e69651998-10-01 13:41:05 +000081 i = ew.index(INSERT)
82 contents = contents[:i-1] + contents[i:]
Barry Warsaw885b3711998-09-29 19:51:18 +000083 ew.bell()
Barry Warsawa3989241998-10-06 15:47:45 +000084 icursor = icursor-1
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000085 elif self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +000086 contents = hex(v)
87 else:
88 contents = int(v)
89 ew.delete(0, END)
90 ew.insert(0, contents)
Barry Warsawa3989241998-10-06 15:47:45 +000091 ew.icursor(icursor)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +000092
Barry Warsaw42020191998-09-29 20:02:27 +000093 def __maybeupdate(self, event=None):
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +000094 if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
Barry Warsaw42020191998-09-29 20:02:27 +000095 self.__update(event)
96
Barry Warsaw885b3711998-09-29 19:51:18 +000097 def __update(self, event=None):
98 redstr = self.__x.get()
99 greenstr = self.__y.get()
100 bluestr = self.__z.get()
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000101 if self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +0000102 red = string.atoi(redstr, 16)
103 green = string.atoi(greenstr, 16)
104 blue = string.atoi(bluestr, 16)
105 else:
106 red, green, blue = map(string.atoi, (redstr, greenstr, bluestr))
107 self.__sb.update_views(red, green, blue)
Barry Warsaw2e6c13f1998-02-10 00:13:06 +0000108
Barry Warsaw885b3711998-09-29 19:51:18 +0000109 def update_yourself(self, red, green, blue):
Barry Warsaw0fa6e4b1998-10-01 02:57:05 +0000110 if self.__hexp.get():
Barry Warsaw885b3711998-09-29 19:51:18 +0000111 redstr, greenstr, bluestr = map(hex, (red, green, blue))
112 else:
Barry Warsaw85e69651998-10-01 13:41:05 +0000113 redstr, greenstr, bluestr = red, green, blue
Barry Warsaw885b3711998-09-29 19:51:18 +0000114 self.__x.delete(0, END)
115 self.__y.delete(0, END)
116 self.__z.delete(0, END)
117 self.__x.insert(0, redstr)
118 self.__y.insert(0, greenstr)
119 self.__z.insert(0, bluestr)
Barry Warsaw6831c301998-10-06 15:49:19 +0000120
Barry Warsaw73290df1998-10-06 15:50:36 +0000121 def hexp_var(self):
122 return self.__hexp