Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Tools / pynche / TypeinViewer.py
blobbbecd9bb898fbca48b4baeea834a1318251a3180
1 """TypeinViewer class.
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.
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 """
15 from Tkinter import *
16 import string
17 import re
19 class TypeinViewer:
20 def __init__(self, switchboard, master=None):
21 # non-gui ivars
22 self.__sb = switchboard
23 optiondb = switchboard.optiondb()
24 self.__hexp = BooleanVar()
25 self.__hexp.set(optiondb.get('HEXTYPE', 0))
26 self.__uwtyping = BooleanVar()
27 self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
28 # create the gui
29 self.__frame = Frame(master, relief=RAISED, borderwidth=1)
30 self.__frame.grid(row=3, column=1, sticky='NSEW')
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)
38 self.__x.bind_class('Update' , '<Key>', self.__maybeupdate)
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'))
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'))
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',
59 variable=self.__hexp,
60 command=self.__togglehex)
61 self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
63 def __togglehex(self, event=None):
64 red, green, blue = self.__sb.current_rgb()
65 self.update_yourself(red, green, blue)
67 def __normalize(self, event=None):
68 ew = event.widget
69 contents = ew.get()
70 icursor = ew.index(INSERT)
71 if contents == '':
72 contents = '0'
73 if contents[0] in 'xX' and self.__hexp.get():
74 contents = '0' + contents
75 # figure out what the contents value is in the current base
76 try:
77 if self.__hexp.get():
78 v = string.atoi(contents, 16)
79 else:
80 v = string.atoi(contents)
81 except ValueError:
82 v = None
83 # if value is not legal, delete the last character inserted and ring
84 # the bell
85 if v is None or v < 0 or v > 255:
86 i = ew.index(INSERT)
87 if event.char:
88 contents = contents[:i-1] + contents[i:]
89 icursor = icursor-1
90 ew.bell()
91 elif self.__hexp.get():
92 contents = hex(v)
93 else:
94 contents = int(v)
95 ew.delete(0, END)
96 ew.insert(0, contents)
97 ew.icursor(icursor)
99 def __maybeupdate(self, event=None):
100 if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
101 self.__update(event)
103 def __update(self, event=None):
104 redstr = self.__x.get()
105 greenstr = self.__y.get()
106 bluestr = self.__z.get()
107 if self.__hexp.get():
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)
115 def update_yourself(self, red, green, blue):
116 if self.__hexp.get():
117 redstr, greenstr, bluestr = map(hex, (red, green, blue))
118 else:
119 redstr, greenstr, bluestr = red, green, blue
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)
134 def hexp_var(self):
135 return self.__hexp
137 def save_options(self, optiondb):
138 optiondb['HEXTYPE'] = self.__hexp.get()
139 optiondb['UPWHILETYPE'] = self.__uwtyping.get()