4 from stdwinevents
import *
9 def __init__(self
, title
):
14 self
.formwidth
= self
.formheight
= 0
18 def define_field(self
, name
, label
, lines
, chars
):
19 self
.fieldnames
.append(name
)
20 lh
= stdwin
.lineheight()
21 cw
= stdwin
.textwidth('m')
23 top
= self
.formheight
+ 4
24 right
= left
+ chars
*cw
25 bottom
= top
+ lines
*lh
27 self
.fields
[name
] = (label
, left
, top
, right
, bottom
, te
)
28 self
.formheight
= bottom
+ 2
29 self
.formwidth
= max(self
.formwidth
, right
+ 4)
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
= \
42 rect
= (left
, top
), (right
, bottom
)
43 te
= self
.window
.textcreate(rect
)
47 label
, left
, top
, right
, bottom
, te
49 self
.setfocus(self
.fieldnames
[0])
51 def setfocus(self
, name
):
52 if name
<> self
.focusname
and self
.tefocus
:
53 self
.tefocus
.setactive(0)
56 self
.tefocus
= self
.fields
[self
.focusname
][-1]
57 self
.tefocus
.setactive(1)
61 def dispatch(self
, type, detail
):
62 event
= type, self
.window
, detail
67 elif type == WE_MOUSE_DOWN
:
69 for name
in self
.fieldnames
:
70 label
, left
, top
, right
, bottom
, te
= \
72 if left
<= x
< right
and \
80 (left
, top
), (right
, bottom
) = \
81 self
.tefocus
.getrect()
83 if x
>= right
: x
= right
-1
88 event
= type, self
.window
, ((x
,y
),)+detail
[1:]
89 if not self
.tefocus
.event(event
):
91 elif type in (WE_MOUSE_MOVE
, WE_MOUSE_UP
, WE_CHAR
):
92 if not self
.tefocus
or not self
.tefocus
.event(event
):
94 elif type == WE_MOUSE_UP
:
97 self
.paste_selection()
100 elif type == WE_COMMAND
:
101 if detail
in (WC_BACKSPACE
, WC_UP
, WC_DOWN
,
103 if not self
.tefocus
or \
104 not self
.tefocus
.event(event
):
106 elif detail
== WC_RETURN
:
107 print '*** Submit query'
108 elif detail
== WC_TAB
:
112 if not self
.focusname
:
115 i
= self
.fieldnames
.index(
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
):
123 elif type == WE_LOST_SEL
:
125 a
, b
= self
.tefocus
.getfocus()
126 self
.tefocus
.setfocus(a
, a
)
128 print 'Form.dispatch(%d, %s)' % (type, `detail`
)
130 def draw(self
, detail
):
131 d
= self
.window
.begindrawing()
134 self
.drawform(d
, detail
)
137 # Stupid textedit objects can't draw with open draw object...
138 self
.drawtextedit(detail
)
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))
146 def drawtextedit(self
, detail
):
147 for name
in self
.fieldnames
:
148 label
, left
, top
, right
, bottom
, te
= self
.fields
[name
]
151 def make_selection(self
):
152 s
= self
.tefocus
.getfocustext()
155 stdwin
.rotatecutbuffers(1)
156 stdwin
.setcutbuffer(0, s
)
157 if not self
.window
.setselection(WS_PRIMARY
, s
):
160 def paste_selection(self
):
164 s
= stdwin
.getselection(WS_PRIMARY
)
166 s
= stdwin
.getcutbuffer(0)
170 self
.tefocus
.replace(s
)