1998-10-02 13:27:40 -03:00
|
|
|
|
"""TypeinViewer class.
|
|
|
|
|
|
|
|
|
|
The TypeinViewer is what you see at the lower right of the main Pynche
|
|
|
|
|
widget. It contains three text entry fields, one each for red, green, blue.
|
|
|
|
|
Input into these windows is highly constrained; it only allows you to enter
|
|
|
|
|
values that are legal for a color axis. This usually means 0-255 for decimal
|
|
|
|
|
input and 0x0 - 0xff for hex input.
|
|
|
|
|
|
|
|
|
|
You can toggle whether you want to view and input the values in either decimal
|
|
|
|
|
or hex by clicking on Hexadecimal. By clicking on Update while typing, the
|
|
|
|
|
color selection will be made on every change to the text field. Otherwise,
|
|
|
|
|
you must hit Return or Tab to select the color.
|
|
|
|
|
"""
|
|
|
|
|
|
2001-07-10 18:50:44 -03:00
|
|
|
|
from Tkinter import *
|
|
|
|
|
|
1998-02-09 20:13:06 -04:00
|
|
|
|
|
2001-07-10 18:50:44 -03:00
|
|
|
|
|
1998-09-29 17:02:27 -03:00
|
|
|
|
class TypeinViewer:
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
def __init__(self, switchboard, master=None):
|
1998-09-29 16:51:18 -03:00
|
|
|
|
# non-gui ivars
|
|
|
|
|
self.__sb = switchboard
|
1998-10-20 17:45:46 -03:00
|
|
|
|
optiondb = switchboard.optiondb()
|
1998-09-30 23:57:05 -03:00
|
|
|
|
self.__hexp = BooleanVar()
|
1998-10-20 17:45:46 -03:00
|
|
|
|
self.__hexp.set(optiondb.get('HEXTYPE', 0))
|
1998-09-30 23:57:05 -03:00
|
|
|
|
self.__uwtyping = BooleanVar()
|
1998-10-20 17:45:46 -03:00
|
|
|
|
self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
|
1998-09-29 16:51:18 -03:00
|
|
|
|
# create the gui
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
self.__frame = Frame(master, relief=RAISED, borderwidth=1)
|
1998-10-22 15:48:45 -03:00
|
|
|
|
self.__frame.grid(row=3, column=1, sticky='NSEW')
|
1998-09-29 16:51:18 -03:00
|
|
|
|
# Red
|
|
|
|
|
self.__xl = Label(self.__frame, text='Red:')
|
|
|
|
|
self.__xl.grid(row=0, column=0, sticky=E)
|
2001-07-10 18:50:44 -03:00
|
|
|
|
subframe = Frame(self.__frame)
|
|
|
|
|
subframe.grid(row=0, column=1)
|
|
|
|
|
self.__xox = Label(subframe, text='0x')
|
|
|
|
|
self.__xox.grid(row=0, column=0, sticky=E)
|
|
|
|
|
self.__xox['font'] = 'courier'
|
|
|
|
|
self.__x = Entry(subframe, width=3)
|
1998-09-29 16:51:18 -03:00
|
|
|
|
self.__x.grid(row=0, column=1)
|
|
|
|
|
self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update'))
|
|
|
|
|
self.__x.bind_class('Normalize', '<Key>', self.__normalize)
|
1998-09-29 17:02:27 -03:00
|
|
|
|
self.__x.bind_class('Update' , '<Key>', self.__maybeupdate)
|
1998-09-29 16:51:18 -03:00
|
|
|
|
# Green
|
|
|
|
|
self.__yl = Label(self.__frame, text='Green:')
|
|
|
|
|
self.__yl.grid(row=1, column=0, sticky=E)
|
2001-07-10 18:50:44 -03:00
|
|
|
|
subframe = Frame(self.__frame)
|
|
|
|
|
subframe.grid(row=1, column=1)
|
|
|
|
|
self.__yox = Label(subframe, text='0x')
|
|
|
|
|
self.__yox.grid(row=0, column=0, sticky=E)
|
|
|
|
|
self.__yox['font'] = 'courier'
|
|
|
|
|
self.__y = Entry(subframe, width=3)
|
|
|
|
|
self.__y.grid(row=0, column=1)
|
1998-09-29 16:51:18 -03:00
|
|
|
|
self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update'))
|
|
|
|
|
# Blue
|
|
|
|
|
self.__zl = Label(self.__frame, text='Blue:')
|
|
|
|
|
self.__zl.grid(row=2, column=0, sticky=E)
|
2001-07-10 18:50:44 -03:00
|
|
|
|
subframe = Frame(self.__frame)
|
|
|
|
|
subframe.grid(row=2, column=1)
|
|
|
|
|
self.__zox = Label(subframe, text='0x')
|
|
|
|
|
self.__zox.grid(row=0, column=0, sticky=E)
|
|
|
|
|
self.__zox['font'] = 'courier'
|
|
|
|
|
self.__z = Entry(subframe, width=3)
|
|
|
|
|
self.__z.grid(row=0, column=1)
|
1998-09-29 16:51:18 -03:00
|
|
|
|
self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update'))
|
1998-09-30 23:57:05 -03:00
|
|
|
|
# Update while typing?
|
|
|
|
|
self.__uwt = Checkbutton(self.__frame,
|
|
|
|
|
text='Update while typing',
|
|
|
|
|
variable=self.__uwtyping)
|
|
|
|
|
self.__uwt.grid(row=3, column=0, columnspan=2, sticky=W)
|
|
|
|
|
# Hex/Dec
|
|
|
|
|
self.__hex = Checkbutton(self.__frame,
|
|
|
|
|
text='Hexadecimal',
|
1998-10-01 10:41:05 -03:00
|
|
|
|
variable=self.__hexp,
|
|
|
|
|
command=self.__togglehex)
|
1998-09-30 23:57:05 -03:00
|
|
|
|
self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
|
1998-02-09 20:13:06 -04:00
|
|
|
|
|
1998-10-01 10:41:05 -03:00
|
|
|
|
def __togglehex(self, event=None):
|
1998-10-06 13:08:39 -03:00
|
|
|
|
red, green, blue = self.__sb.current_rgb()
|
2001-07-10 18:50:44 -03:00
|
|
|
|
if self.__hexp.get():
|
|
|
|
|
label = '0x'
|
|
|
|
|
else:
|
|
|
|
|
label = ' '
|
|
|
|
|
self.__xox['text'] = label
|
|
|
|
|
self.__yox['text'] = label
|
|
|
|
|
self.__zox['text'] = label
|
1998-10-06 13:08:39 -03:00
|
|
|
|
self.update_yourself(red, green, blue)
|
1998-10-01 10:41:05 -03:00
|
|
|
|
|
1998-09-29 16:51:18 -03:00
|
|
|
|
def __normalize(self, event=None):
|
|
|
|
|
ew = event.widget
|
|
|
|
|
contents = ew.get()
|
1998-10-06 12:47:45 -03:00
|
|
|
|
icursor = ew.index(INSERT)
|
2001-02-01 17:31:58 -04:00
|
|
|
|
if contents and contents[0] in 'xX' and self.__hexp.get():
|
1998-10-06 23:44:17 -03:00
|
|
|
|
contents = '0' + contents
|
2001-07-10 18:50:44 -03:00
|
|
|
|
# Figure out the contents in the current base.
|
1998-09-29 16:51:18 -03:00
|
|
|
|
try:
|
1998-09-30 23:57:05 -03:00
|
|
|
|
if self.__hexp.get():
|
2001-07-10 18:50:44 -03:00
|
|
|
|
v = int(contents, 16)
|
1998-09-29 16:51:18 -03:00
|
|
|
|
else:
|
2001-07-10 18:50:44 -03:00
|
|
|
|
v = int(contents)
|
1998-09-29 16:51:18 -03:00
|
|
|
|
except ValueError:
|
|
|
|
|
v = None
|
2001-07-10 18:50:44 -03:00
|
|
|
|
# If value is not legal, or empty, delete the last character inserted
|
|
|
|
|
# and ring the bell. Don't ring the bell if the field is empty (it'll
|
|
|
|
|
# just equal zero.
|
|
|
|
|
if v is None:
|
|
|
|
|
pass
|
|
|
|
|
elif v < 0 or v > 255:
|
1998-10-01 10:41:05 -03:00
|
|
|
|
i = ew.index(INSERT)
|
1998-10-06 23:44:17 -03:00
|
|
|
|
if event.char:
|
|
|
|
|
contents = contents[:i-1] + contents[i:]
|
2001-07-10 18:50:44 -03:00
|
|
|
|
icursor -= 1
|
1998-09-29 16:51:18 -03:00
|
|
|
|
ew.bell()
|
1998-09-30 23:57:05 -03:00
|
|
|
|
elif self.__hexp.get():
|
2001-07-10 18:50:44 -03:00
|
|
|
|
contents = hex(v)[2:]
|
1998-09-29 16:51:18 -03:00
|
|
|
|
else:
|
|
|
|
|
contents = int(v)
|
|
|
|
|
ew.delete(0, END)
|
|
|
|
|
ew.insert(0, contents)
|
1998-10-06 12:47:45 -03:00
|
|
|
|
ew.icursor(icursor)
|
1998-02-09 20:13:06 -04:00
|
|
|
|
|
1998-09-29 17:02:27 -03:00
|
|
|
|
def __maybeupdate(self, event=None):
|
1998-09-30 23:57:05 -03:00
|
|
|
|
if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
|
1998-09-29 17:02:27 -03:00
|
|
|
|
self.__update(event)
|
|
|
|
|
|
1998-09-29 16:51:18 -03:00
|
|
|
|
def __update(self, event=None):
|
2001-07-10 18:50:44 -03:00
|
|
|
|
redstr = self.__x.get() or '0'
|
|
|
|
|
greenstr = self.__y.get() or '0'
|
|
|
|
|
bluestr = self.__z.get() or '0'
|
1998-09-30 23:57:05 -03:00
|
|
|
|
if self.__hexp.get():
|
2001-07-10 18:50:44 -03:00
|
|
|
|
base = 16
|
1998-09-29 16:51:18 -03:00
|
|
|
|
else:
|
2001-07-10 18:50:44 -03:00
|
|
|
|
base = 10
|
|
|
|
|
red, green, blue = [int(x, base) for x in (redstr, greenstr, bluestr)]
|
1998-09-29 16:51:18 -03:00
|
|
|
|
self.__sb.update_views(red, green, blue)
|
1998-02-09 20:13:06 -04:00
|
|
|
|
|
1998-09-29 16:51:18 -03:00
|
|
|
|
def update_yourself(self, red, green, blue):
|
1998-09-30 23:57:05 -03:00
|
|
|
|
if self.__hexp.get():
|
2001-07-10 18:50:44 -03:00
|
|
|
|
sred, sgreen, sblue = [hex(x)[2:] for x in (red, green, blue)]
|
1998-09-29 16:51:18 -03:00
|
|
|
|
else:
|
2001-07-10 18:50:44 -03:00
|
|
|
|
sred, sgreen, sblue = red, green, blue
|
1998-10-06 23:44:17 -03:00
|
|
|
|
x, y, z = self.__x, self.__y, self.__z
|
|
|
|
|
xicursor = x.index(INSERT)
|
|
|
|
|
yicursor = y.index(INSERT)
|
|
|
|
|
zicursor = z.index(INSERT)
|
|
|
|
|
x.delete(0, END)
|
|
|
|
|
y.delete(0, END)
|
|
|
|
|
z.delete(0, END)
|
2001-07-10 18:50:44 -03:00
|
|
|
|
x.insert(0, sred)
|
|
|
|
|
y.insert(0, sgreen)
|
|
|
|
|
z.insert(0, sblue)
|
1998-10-06 23:44:17 -03:00
|
|
|
|
x.icursor(xicursor)
|
|
|
|
|
y.icursor(yicursor)
|
|
|
|
|
z.icursor(zicursor)
|
1998-10-06 12:49:19 -03:00
|
|
|
|
|
1998-10-06 12:50:36 -03:00
|
|
|
|
def hexp_var(self):
|
|
|
|
|
return self.__hexp
|
1998-10-20 17:45:46 -03:00
|
|
|
|
|
|
|
|
|
def save_options(self, optiondb):
|
|
|
|
|
optiondb['HEXTYPE'] = self.__hexp.get()
|
|
|
|
|
optiondb['UPWHILETYPE'] = self.__uwtyping.get()
|