home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / cwilib / form.py < prev    next >
Text File  |  1993-11-01  |  4KB  |  171 lines

  1. # Fill-out form window
  2.  
  3. import stdwin
  4. from stdwinevents import *
  5.  
  6.  
  7. class Form:
  8.  
  9.     def __init__(self, title):
  10.         self.title = title
  11.         self.window = None
  12.         self.fields = {}
  13.         self.fieldnames = []
  14.         self.formwidth = self.formheight = 0
  15.         self.focusname = None
  16.         self.tefocus = None
  17.  
  18.     def define_field(self, name, label, lines, chars):
  19.         self.fieldnames.append(name)
  20.         lh = stdwin.lineheight()
  21.         cw = stdwin.textwidth('m')
  22.         left = 20*cw
  23.         top = self.formheight + 4
  24.         right = left + chars*cw
  25.         bottom = top + lines*lh
  26.         te = None
  27.         self.fields[name] = (label, left, top, right, bottom, te)
  28.         self.formheight = bottom + 2
  29.         self.formwidth = max(self.formwidth, right + 4)
  30.  
  31.     def open(self):
  32.         if self.window: return
  33.         self.formwidth = max(100, self.formwidth)
  34.         self.formheight = max(50, self.formheight)
  35.         stdwin.setdefwinsize(self.formwidth, self.formheight)
  36.         stdwin.setdefscrollbars(0, 0)
  37.         self.window = stdwin.open(self.title)
  38.         self.window.setdocsize(self.formwidth, self.formheight)
  39.         for name in self.fieldnames:
  40.             label, left, top, right, bottom, te = \
  41.                   self.fields[name]
  42.             rect = (left, top), (right, bottom)
  43.             te = self.window.textcreate(rect)
  44.             te.setactive(0)
  45.             te.setview(rect)
  46.             self.fields[name] = \
  47.                   label, left, top, right, bottom, te
  48.         if self.fieldnames:
  49.             self.setfocus(self.fieldnames[0])
  50.  
  51.     def setfocus(self, name):
  52.         if name <> self.focusname and self.tefocus:
  53.             self.tefocus.setactive(0)
  54.         self.focusname = name
  55.         if self.focusname:
  56.             self.tefocus = self.fields[self.focusname][-1]
  57.             self.tefocus.setactive(1)
  58.         else:
  59.             self.tefocus = None
  60.  
  61.     def dispatch(self, type, detail):
  62.         event = type, self.window, detail
  63.         if type == WE_NULL:
  64.             pass
  65.         elif type == WE_DRAW:
  66.             self.draw(detail)
  67.         elif type == WE_MOUSE_DOWN:
  68.             x, y = detail[0]
  69.             for name in self.fieldnames:
  70.                 label, left, top, right, bottom, te = \
  71.                       self.fields[name]
  72.                 if left <= x < right and \
  73.                       top <= y < bottom:
  74.                     self.setfocus(name)
  75.                     break
  76.             else:
  77.                 stdwin.fleep()
  78.                 return
  79.             if self.tefocus:
  80.                 (left, top), (right, bottom) = \
  81.                       self.tefocus.getrect()
  82.                 if x < left: x = left
  83.                 if x >= right: x = right-1
  84.                 if y < top: y = top
  85.                 if y >= bottom:
  86.                     y = bottom-1
  87.                     x = right-1
  88.                 event = type, self.window, ((x,y),)+detail[1:]
  89.                 if not self.tefocus.event(event):
  90.                     stdwin.fleep()
  91.         elif type in (WE_MOUSE_MOVE, WE_MOUSE_UP, WE_CHAR):
  92.             if not self.tefocus or not self.tefocus.event(event):
  93.                 stdwin.fleep()
  94.             elif type == WE_MOUSE_UP:
  95.                 button = detail[2]
  96.                 if button == 2:
  97.                     self.paste_selection()
  98.                 else:
  99.                     self.make_selection()
  100.         elif type == WE_COMMAND:
  101.             if detail in (WC_BACKSPACE, WC_UP, WC_DOWN,
  102.                       WC_LEFT, WC_RIGHT):
  103.                 if not self.tefocus or \
  104.                       not self.tefocus.event(event):
  105.                     stdwin.fleep()
  106.             elif detail == WC_RETURN:
  107.                 print '*** Submit query'
  108.             elif detail == WC_TAB:
  109.                 if not self.fields:
  110.                     stdwin.fleep()
  111.                     return
  112.                 if not self.focusname:
  113.                     i = 0
  114.                 else:
  115.                     i = self.fieldnames.index(
  116.                           self.focusname)
  117.                     i = (i+1) % len(self.fieldnames)
  118.                 self.setfocus(self.fieldnames[i])
  119.                 self.tefocus.setfocus(0, 0x7fff)
  120.                 self.make_selection()
  121.         elif type in (WE_ACTIVATE, WE_DEACTIVATE):
  122.             pass
  123.         elif type == WE_LOST_SEL:
  124.             if self.tefocus:
  125.                 a, b = self.tefocus.getfocus()
  126.                 self.tefocus.setfocus(a, a)
  127.         else:
  128.             print 'Form.dispatch(%d, %s)' % (type, `detail`)
  129.  
  130.     def draw(self, detail):
  131.         d = self.window.begindrawing()
  132.         d.cliprect(detail)
  133.         d.erase(detail)
  134.         self.drawform(d, detail)
  135.         d.noclip()
  136.         d.close()
  137.         # Stupid textedit objects can't draw with open draw object...
  138.         self.drawtextedit(detail)
  139.  
  140.     def drawform(self, d, detail):
  141.         for name in self.fieldnames:
  142.             label, left, top, right, bottom, te = self.fields[name]
  143.             d.text((0, top), label)
  144.             d.box((left-3, top-2), (right+4, bottom+2))
  145.  
  146.     def drawtextedit(self, detail):
  147.         for name in self.fieldnames:
  148.             label, left, top, right, bottom, te = self.fields[name]
  149.             te.draw(detail)
  150.  
  151.     def make_selection(self):
  152.         s = self.tefocus.getfocustext()
  153.         if not s:
  154.             return
  155.         stdwin.rotatecutbuffers(1)
  156.         stdwin.setcutbuffer(0, s)
  157.         if not self.window.setselection(WS_PRIMARY, s):
  158.             stdwin.fleep()
  159.  
  160.     def paste_selection(self):
  161.         if not self.tefocus:
  162.             stdwin.fleep()
  163.             return
  164.         s = stdwin.getselection(WS_PRIMARY)
  165.         if not s:
  166.             s = stdwin.getcutbuffer(0)
  167.             if not s:
  168.                 stdwin.fleep()
  169.                 return
  170.         self.tefocus.replace(s)
  171.