home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / TYPEINVIEWER.PY < prev    next >
Encoding:
Python Source  |  2001-07-10  |  6.1 KB  |  164 lines

  1. """TypeinViewer class.
  2.  
  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.
  8.  
  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. """
  14.  
  15. import sys
  16. import re
  17. from Tkinter import *
  18.  
  19.  
  20.  
  21. class TypeinViewer:
  22.     def __init__(self, switchboard, master=None):
  23.         # non-gui ivars
  24.         self.__sb = switchboard
  25.         optiondb = switchboard.optiondb()
  26.         self.__hexp = BooleanVar()
  27.         self.__hexp.set(optiondb.get('HEXTYPE', 0))
  28.         self.__uwtyping = BooleanVar()
  29.         self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
  30.         # create the gui
  31.         self.__frame = Frame(master, relief=RAISED, borderwidth=1)
  32.         self.__frame.grid(row=3, column=1, sticky='NSEW')
  33.         # Red
  34.         self.__xl = Label(self.__frame, text='Red:')
  35.         self.__xl.grid(row=0, column=0, sticky=E)
  36.         subframe = Frame(self.__frame)
  37.         subframe.grid(row=0, column=1)
  38.         self.__xox = Label(subframe, text='0x')
  39.         self.__xox.grid(row=0, column=0, sticky=E)
  40.         self.__xox['font'] = 'courier'
  41.         self.__x = Entry(subframe, width=3)
  42.         self.__x.grid(row=0, column=1)
  43.         self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update'))
  44.         self.__x.bind_class('Normalize', '<Key>', self.__normalize)
  45.         self.__x.bind_class('Update'   , '<Key>', self.__maybeupdate)
  46.         # Green
  47.         self.__yl = Label(self.__frame, text='Green:')
  48.         self.__yl.grid(row=1, column=0, sticky=E)
  49.         subframe = Frame(self.__frame)
  50.         subframe.grid(row=1, column=1)
  51.         self.__yox = Label(subframe, text='0x')
  52.         self.__yox.grid(row=0, column=0, sticky=E)
  53.         self.__yox['font'] = 'courier'
  54.         self.__y = Entry(subframe, width=3)
  55.         self.__y.grid(row=0, column=1)
  56.         self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update'))
  57.         # Blue
  58.         self.__zl = Label(self.__frame, text='Blue:')
  59.         self.__zl.grid(row=2, column=0, sticky=E)
  60.         subframe = Frame(self.__frame)
  61.         subframe.grid(row=2, column=1)
  62.         self.__zox = Label(subframe, text='0x')
  63.         self.__zox.grid(row=0, column=0, sticky=E)
  64.         self.__zox['font'] = 'courier'
  65.         self.__z = Entry(subframe, width=3)
  66.         self.__z.grid(row=0, column=1)
  67.         self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update'))
  68.         # Update while typing?
  69.         self.__uwt = Checkbutton(self.__frame,
  70.                                  text='Update while typing',
  71.                                  variable=self.__uwtyping)
  72.         self.__uwt.grid(row=3, column=0, columnspan=2, sticky=W)
  73.         # Hex/Dec
  74.         self.__hex = Checkbutton(self.__frame,
  75.                                  text='Hexadecimal',
  76.                                  variable=self.__hexp,
  77.                                  command=self.__togglehex)
  78.         self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
  79.  
  80.     def __togglehex(self, event=None):
  81.         red, green, blue = self.__sb.current_rgb()
  82.         if self.__hexp.get():
  83.             label = '0x'
  84.         else:
  85.             label = '  '
  86.         self.__xox['text'] = label
  87.         self.__yox['text'] = label
  88.         self.__zox['text'] = label
  89.         self.update_yourself(red, green, blue)
  90.  
  91.     def __normalize(self, event=None):
  92.         ew = event.widget
  93.         contents = ew.get()
  94.         icursor = ew.index(INSERT)
  95.         if contents and contents[0] in 'xX' and self.__hexp.get():
  96.             contents = '0' + contents
  97.         # Figure out the contents in the current base.
  98.         try:
  99.             if self.__hexp.get():
  100.                 v = int(contents, 16)
  101.             else:
  102.                 v = int(contents)
  103.         except ValueError:
  104.             v = None
  105.         # If value is not legal, or empty, delete the last character inserted
  106.         # and ring the bell.  Don't ring the bell if the field is empty (it'll
  107.         # just equal zero.
  108.         if v is None:
  109.             pass
  110.         elif v < 0 or v > 255:
  111.             i = ew.index(INSERT)
  112.             if event.char:
  113.                 contents = contents[:i-1] + contents[i:]
  114.                 icursor -= 1
  115.             ew.bell()
  116.         elif self.__hexp.get():
  117.             contents = hex(v)[2:]
  118.         else:
  119.             contents = int(v)
  120.         ew.delete(0, END)
  121.         ew.insert(0, contents)
  122.         ew.icursor(icursor)
  123.  
  124.     def __maybeupdate(self, event=None):
  125.         if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
  126.             self.__update(event)
  127.  
  128.     def __update(self, event=None):
  129.         redstr = self.__x.get() or '0'
  130.         greenstr = self.__y.get() or '0'
  131.         bluestr = self.__z.get() or '0'
  132.         if self.__hexp.get():
  133.             base = 16
  134.         else:
  135.             base = 10
  136.         red, green, blue = [int(x, base) for x in (redstr, greenstr, bluestr)]
  137.         self.__sb.update_views(red, green, blue)
  138.  
  139.     def update_yourself(self, red, green, blue):
  140.         if self.__hexp.get():
  141.             sred, sgreen, sblue = [hex(x)[2:] for x in (red, green, blue)]
  142.         else:
  143.             sred, sgreen, sblue = red, green, blue
  144.         x, y, z = self.__x, self.__y, self.__z
  145.         xicursor = x.index(INSERT)
  146.         yicursor = y.index(INSERT)
  147.         zicursor = z.index(INSERT)
  148.         x.delete(0, END)
  149.         y.delete(0, END)
  150.         z.delete(0, END)
  151.         x.insert(0, sred)
  152.         y.insert(0, sgreen)
  153.         z.insert(0, sblue)
  154.         x.icursor(xicursor)
  155.         y.icursor(yicursor)
  156.         z.icursor(zicursor)
  157.  
  158.     def hexp_var(self):
  159.         return self.__hexp
  160.  
  161.     def save_options(self, optiondb):
  162.         optiondb['HEXTYPE'] = self.__hexp.get()
  163.         optiondb['UPWHILETYPE'] = self.__uwtyping.get()
  164.