Teach Windows build and installer about new _symtable module/DLL.
[python/dscho.git] / Mac / Tools / IDE / Wcontrols.py
blob7a88ccad6d85d23ab820e74ee89ef45ee275f47a
1 import Ctl
2 import Controls
3 import Win
4 import Wbase
5 import Qd
6 import Evt
8 class ControlWidget(Wbase.ClickableWidget):
10 """Baseclass for all native controls."""
12 def __init__(self, possize, title = "Control", procID = 0, callback = None, value = 0, min = 0, max = 1):
13 Wbase.ClickableWidget.__init__(self, possize)
14 self._control = None
15 self._title = title
16 self._callback = callback
17 self._procID = procID
18 self._value = value
19 self._min = min
20 self._max = max
21 self._enabled = 1
23 def open(self):
24 self._calcbounds()
25 self._control = Ctl.NewControl(self._parentwindow.wid,
26 self._bounds,
27 self._title,
28 1,
29 self._value,
30 self._min,
31 self._max,
32 self._procID,
34 self.SetPort()
35 #self.GetWindow().ValidWindowRect(self._bounds)
36 self.enable(self._enabled)
38 def adjust(self, oldbounds):
39 self.SetPort()
40 self._control.HideControl()
41 self._control.MoveControl(self._bounds[0], self._bounds[1])
42 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
43 if self._visible:
44 Qd.EraseRect(self._bounds)
45 self._control.ShowControl()
46 self.GetWindow().ValidWindowRect(self._bounds)
48 def close(self):
49 self._control.HideControl()
50 self._control = None
51 Wbase.ClickableWidget.close(self)
53 def enable(self, onoff):
54 if self._control and self._enabled <> onoff:
55 self._control.HiliteControl((not onoff) and 255)
56 self._enabled = onoff
58 def show(self, onoff):
59 self._visible = onoff
60 for w in self._widgets:
61 w.show(onoff)
62 if onoff:
63 self._control.ShowControl()
64 else:
65 self._control.HideControl()
67 def activate(self, onoff):
68 self._activated = onoff
69 if self._enabled:
70 self._control.HiliteControl((not onoff) and 255)
72 def draw(self, visRgn = None):
73 if self._visible:
74 self._control.Draw1Control()
76 def test(self, point):
77 ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
78 if self._enabled and control == self._control:
79 return 1
81 def click(self, point, modifiers):
82 if not self._enabled:
83 return
84 part = self._control.TrackControl(point)
85 if part:
86 if self._callback:
87 Wbase.CallbackCall(self._callback, 0)
89 def settitle(self, title):
90 if self._control:
91 self._control.SetControlTitle(title)
92 self._title = title
94 def gettitle(self):
95 return self._title
97 class Button(ControlWidget):
99 """Standard push button."""
101 def __init__(self, possize, title = "Button", callback = None):
102 procID = Controls.pushButProc | Controls.useWFont
103 ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
104 self._isdefault = 0
106 def push(self):
107 if not self._enabled:
108 return
109 import time
110 self._control.HiliteControl(1)
111 time.sleep(0.1)
112 self._control.HiliteControl(0)
113 if self._callback:
114 Wbase.CallbackCall(self._callback, 0)
116 def enable(self, onoff):
117 if self._control and self._enabled <> onoff:
118 self._control.HiliteControl((not onoff) and 255)
119 self._enabled = onoff
120 if self._isdefault and self._visible:
121 self.SetPort()
122 self.drawfatframe(onoff)
124 def activate(self, onoff):
125 self._activated = onoff
126 if self._enabled:
127 self._control.HiliteControl((not onoff) and 255)
128 if self._isdefault and self._visible:
129 self.SetPort()
130 self.drawfatframe(onoff)
132 def show(self, onoff):
133 ControlWidget.show(self, onoff)
134 if self._isdefault:
135 self.drawfatframe(onoff and self._enabled)
137 def draw(self, visRgn = None):
138 if self._visible:
139 self._control.Draw1Control()
140 if self._isdefault and self._activated:
141 self.drawfatframe(self._enabled)
143 def drawfatframe(self, onoff):
144 state = Qd.GetPenState()
145 if onoff:
146 Qd.PenPat(Qd.qd.black)
147 else:
148 Qd.PenPat(Qd.qd.white)
149 fatrect = Qd.InsetRect(self._bounds, -4, -4)
150 Qd.PenSize(3, 3)
151 Qd.FrameRoundRect(fatrect, 16, 16)
152 Qd.SetPenState(state)
154 def _setdefault(self, onoff):
155 self._isdefault = onoff
156 if self._control and self._enabled:
157 self.SetPort()
158 self.drawfatframe(onoff)
160 def adjust(self, oldbounds):
161 if self._isdefault:
162 old = Qd.InsetRect(oldbounds, -4, -4)
163 new = Qd.InsetRect(self._bounds, -4, -4)
164 Qd.EraseRect(old)
165 self.GetWindow().InvalWindowRect(old)
166 self.GetWindow().InvalWindowRect(new)
167 ControlWidget.adjust(self, oldbounds)
170 class CheckBox(ControlWidget):
172 """Standard checkbox."""
174 def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
175 procID = Controls.checkBoxProc | Controls.useWFont
176 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
178 def click(self, point, modifiers):
179 if not self._enabled:
180 return
181 part = self._control.TrackControl(point)
182 if part:
183 self.toggle()
184 if self._callback:
185 Wbase.CallbackCall(self._callback, 0, self.get())
187 def push(self):
188 if not self._enabled:
189 return
190 self.toggle()
191 if self._callback:
192 Wbase.CallbackCall(self._callback, 0, self.get())
194 def toggle(self):
195 self.set(not self.get())
197 def set(self, value):
198 if self._control:
199 self._control.SetControlValue(value)
200 else:
201 self._value = value
203 def get(self):
204 if self._control:
205 return self._control.GetControlValue()
206 else:
207 return self._value
210 class RadioButton(ControlWidget):
212 """Standard radiobutton."""
214 # XXX We need a radiogroup widget; this is too kludgy.
216 def __init__(self, possize, title, thebuttons, callback = None, value = 0):
217 procID = Controls.radioButProc | Controls.useWFont
218 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
219 self.thebuttons = thebuttons
220 thebuttons.append(self)
222 def close(self):
223 self.thebuttons = None
224 ControlWidget.close(self)
226 def click(self, point, modifiers):
227 if not self._enabled:
228 return
229 part = self._control.TrackControl(point)
230 if part:
231 self.set(1)
232 if self._callback:
233 Wbase.CallbackCall(self._callback, 0, 1)
235 def push(self):
236 if not self._enabled:
237 return
238 self.set(1)
239 if self._callback:
240 Wbase.CallbackCall(self._callback, 0, 1)
242 def set(self, value):
243 for button in self.thebuttons:
244 if button._control:
245 button._control.SetControlValue(button == self)
246 else:
247 button._value = (button == self)
249 def get(self):
250 if self._control:
251 return self._control.GetControlValue()
252 else:
253 return self._value
256 class Scrollbar(ControlWidget):
258 """Standard scrollbar."""
260 def __init__(self, possize, callback = None, value = 0, min = 0, max = 0):
261 procID = Controls.scrollBarProc
262 ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
264 # interface
265 def set(self, value):
266 if self._callback:
267 Wbase.CallbackCall(self._callback, 1, value)
269 def up(self):
270 if self._callback:
271 Wbase.CallbackCall(self._callback, 1, '+')
273 def down(self):
274 if self._callback:
275 Wbase.CallbackCall(self._callback, 1, '-')
277 def pageup(self):
278 if self._callback:
279 Wbase.CallbackCall(self._callback, 1, '++')
281 def pagedown(self):
282 if self._callback:
283 Wbase.CallbackCall(self._callback, 1, '--')
285 def setmin(self, min):
286 self._control.SetControlMinimum(min)
288 def setmax(self, min):
289 self._control.SetControlMinimum(max)
291 def getmin(self):
292 return self._control.GetControlMinimum()
294 def getmax(self):
295 return self._control.GetControlMinimum()
297 # internals
298 def click(self, point, modifiers):
299 if not self._enabled:
300 return
301 # custom TrackControl. A mousedown in a scrollbar arrow or page area should
302 # generate _control hits as long as the mouse is a) down, b) still in the same part
303 part = self._control.TestControl(point)
304 if Controls.inUpButton <= part <= Controls.inPageDown:
305 self._control.HiliteControl(part)
306 self._hit(part)
307 oldpart = part
308 # slight delay before scrolling at top speed...
309 now = Evt.TickCount()
310 while Evt.StillDown():
311 if (Evt.TickCount() - now) > 18: # 0.3 seconds
312 break
313 while Evt.StillDown():
314 part = self._control.TestControl(point)
315 if part == oldpart:
316 self._control.HiliteControl(part)
317 self._hit(part)
318 else:
319 self._control.HiliteControl(0)
320 self.SetPort()
321 point = Evt.GetMouse()
322 self._control.HiliteControl(0)
323 elif part == Controls.inThumb:
324 part = self._control.TrackControl(point)
325 if part:
326 self._hit(part)
328 def _hit(self, part):
329 if part == Controls.inThumb:
330 value = self._control.GetControlValue()
331 elif part == Controls.inUpButton:
332 value = "+"
333 elif part == Controls.inDownButton:
334 value = "-"
335 elif part == Controls.inPageUp:
336 value = "++"
337 elif part == Controls.inPageDown:
338 value = "--"
339 if self._callback:
340 Wbase.CallbackCall(self._callback, 1, value)
342 def draw(self, visRgn = None):
343 if self._visible:
344 self._control.Draw1Control()
345 Qd.FrameRect(self._bounds)
347 def adjust(self, oldbounds):
348 self.SetPort()
349 self.GetWindow().InvalWindowRect(oldbounds)
350 self._control.HideControl()
351 self._control.MoveControl(self._bounds[0], self._bounds[1])
352 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
353 if self._visible:
354 Qd.EraseRect(self._bounds)
355 if self._activated:
356 self._control.ShowControl()
357 else:
358 Qd.FrameRect(self._bounds)
359 self.GetWindow().ValidWindowRect(self._bounds)
361 def activate(self, onoff):
362 self._activated = onoff
363 if self._visible:
364 if onoff:
365 self._control.ShowControl()
366 else:
367 self._control.HideControl()
368 self.draw(None)
369 self.GetWindow().ValidWindowRect(self._bounds)
371 def set(self, value):
372 if self._control:
373 self._control.SetControlValue(value)
374 else:
375 self._value = value
377 def get(self):
378 if self._control:
379 return self._control.GetControlValue()
380 else:
381 return self._value
384 class __xxxx_PopupControl(ControlWidget):
386 def __init__(self, possize, title = "Button", callback = None):
387 procID = Controls.popupMenuProc # | Controls.useWFont
388 ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
389 self._isdefault = 0
392 def _scalebarvalue(absmin, absmax, curmin, curmax):
393 if curmin <= absmin and curmax >= absmax:
394 return None
395 if curmin <= absmin:
396 return 0
397 if curmax >= absmax:
398 return 32767
399 perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
400 return int(perc*32767)