Clarify portability and main program.
[python/dscho.git] / Mac / Contrib / PyIDE-src / IDELib / Widgets / Wcontrols.py
blob8c3c39505c23e87ea5c70f95b225b4b17a7e88e7
1 import Ctl
2 import Controls
3 import Win
4 import Wbase
5 import Qd
6 import Evt
8 class ControlWidget(Wbase.ClickableWidget):
10 def __init__(self, possize, title = "Control", procID = 0, callback = None, value = 0, min = 0, max = 1):
11 Wbase.ClickableWidget.__init__(self, possize)
12 self._control = None
13 self._title = title
14 self._callback = callback
15 self._procID = procID
16 self._value = value
17 self._min = min
18 self._max = max
19 self._enabled = 1
21 def open(self):
22 self._calcbounds()
23 self._control = Ctl.NewControl(self._parentwindow.wid,
24 self._bounds,
25 self._title,
26 1,
27 self._value,
28 self._min,
29 self._max,
30 self._procID,
32 self.SetPort()
33 Win.ValidRect(self._bounds)
34 self.enable(self._enabled)
36 def adjust(self, oldbounds):
37 self.SetPort()
38 self._control.HideControl()
39 self._control.MoveControl(self._bounds[0], self._bounds[1])
40 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
41 if self._visible:
42 Qd.EraseRect(self._bounds)
43 self._control.ShowControl()
44 Win.ValidRect(self._bounds)
46 def close(self):
47 self._control.HideControl()
48 self._control = None
49 Wbase.ClickableWidget.close(self)
51 def enable(self, onoff):
52 if self._control and self._enabled <> onoff:
53 self._control.HiliteControl((not onoff) and 255)
54 self._enabled = onoff
56 def show(self, onoff):
57 self._visible = onoff
58 for w in self._widgets:
59 w.show(onoff)
60 if onoff:
61 self._control.ShowControl()
62 else:
63 self._control.HideControl()
65 def activate(self, onoff):
66 self._activated = onoff
67 if self._enabled:
68 self._control.HiliteControl((not onoff) and 255)
70 def draw(self, visRgn = None):
71 if self._visible:
72 self._control.Draw1Control()
74 def test(self, point):
75 ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
76 if self._enabled and control == self._control:
77 return 1
79 def click(self, point, modifiers):
80 if not self._enabled:
81 return
82 part = self._control.TrackControl(point)
83 if part:
84 if self._callback:
85 Wbase.CallbackCall(self._callback, 0)
87 def settitle(self, title):
88 if self._control:
89 self._control.SetControlTitle(title)
90 self._title = title
92 def gettitle(self):
93 return self._title
95 class Button(ControlWidget):
97 def __init__(self, possize, title = "Button", callback = None):
98 procID = Controls.pushButProc | Controls.useWFont
99 ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
100 self._isdefault = 0
102 def push(self):
103 if not self._enabled:
104 return
105 import time
106 self._control.HiliteControl(1)
107 time.sleep(0.1)
108 self._control.HiliteControl(0)
109 if self._callback:
110 Wbase.CallbackCall(self._callback, 0)
112 def enable(self, onoff):
113 if self._control and self._enabled <> onoff:
114 self._control.HiliteControl((not onoff) and 255)
115 self._enabled = onoff
116 if self._isdefault and self._visible:
117 self.SetPort()
118 self.drawfatframe(onoff)
120 def activate(self, onoff):
121 self._activated = onoff
122 if self._enabled:
123 self._control.HiliteControl((not onoff) and 255)
124 if self._isdefault and self._visible:
125 self.SetPort()
126 self.drawfatframe(onoff)
128 def show(self, onoff):
129 ControlWidget.show(self, onoff)
130 if self._isdefault:
131 self.drawfatframe(onoff and self._enabled)
133 def draw(self, visRgn = None):
134 if self._visible:
135 self._control.Draw1Control()
136 if self._isdefault and self._activated:
137 self.drawfatframe(self._enabled)
139 def drawfatframe(self, onoff):
140 state = Qd.GetPenState()
141 if onoff:
142 Qd.PenPat(Qd.qd.black)
143 else:
144 Qd.PenPat(Qd.qd.white)
145 fatrect = Qd.InsetRect(self._bounds, -4, -4)
146 Qd.PenSize(3, 3)
147 Qd.FrameRoundRect(fatrect, 16, 16)
148 Qd.SetPenState(state)
150 def _setdefault(self, onoff):
151 self._isdefault = onoff
152 if self._control:
153 self.SetPort()
154 self.drawfatframe(onoff)
156 def adjust(self, oldbounds):
157 if self._isdefault:
158 old = Qd.InsetRect(oldbounds, -4, -4)
159 new = Qd.InsetRect(self._bounds, -4, -4)
160 Qd.EraseRect(old)
161 Win.InvalRect(old)
162 Win.InvalRect(new)
163 ControlWidget.adjust(self, oldbounds)
166 class CheckBox(ControlWidget):
168 def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
169 procID = Controls.checkBoxProc | Controls.useWFont
170 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
172 def click(self, point, modifiers):
173 if not self._enabled:
174 return
175 part = self._control.TrackControl(point)
176 if part:
177 self.toggle()
178 if self._callback:
179 Wbase.CallbackCall(self._callback, 0, self.get())
181 def push(self):
182 if not self._enabled:
183 return
184 self.toggle()
185 if self._callback:
186 Wbase.CallbackCall(self._callback, 0, self.get())
188 def toggle(self):
189 self.set(not self.get())
191 def set(self, value):
192 if self._control:
193 self._control.SetControlValue(value)
194 else:
195 self._value = value
197 def get(self):
198 if self._control:
199 return self._control.GetControlValue()
200 else:
201 return self._value
204 class RadioButton(ControlWidget):
206 def __init__(self, possize, title = "Radiobutton", thebuttons, callback = None, value = 0):
207 procID = Controls.radioButProc | Controls.useWFont
208 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
209 self.thebuttons = thebuttons
210 thebuttons.append(self)
212 def close(self):
213 self.thebuttons = None
214 ControlWidget.close(self)
216 def click(self, point, modifiers):
217 if not self._enabled:
218 return
219 part = self._control.TrackControl(point)
220 if part:
221 self.set(1)
222 if self._callback:
223 Wbase.CallbackCall(self._callback, 0, 1)
225 def push(self):
226 if not self._enabled:
227 return
228 self.set(1)
229 if self._callback:
230 Wbase.CallbackCall(self._callback, 0, 1)
232 def set(self, value):
233 for button in self.thebuttons:
234 if button._control:
235 button._control.SetControlValue(button == self)
236 else:
237 button._value = (button == self)
239 def get(self):
240 if self._control:
241 return self._control.GetControlValue()
242 else:
243 return self._value
246 class Scrollbar(ControlWidget):
248 def __init__(self, possize, callback = None, value = 0, min = 0, max = 0):
249 procID = Controls.scrollBarProc
250 ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
252 # interface
253 def set(self, value):
254 if self._callback:
255 Wbase.CallbackCall(self._callback, 1, value)
257 def up(self):
258 if self._callback:
259 Wbase.CallbackCall(self._callback, 1, '+')
261 def down(self):
262 if self._callback:
263 Wbase.CallbackCall(self._callback, 1, '-')
265 def pageup(self):
266 if self._callback:
267 Wbase.CallbackCall(self._callback, 1, '++')
269 def pagedown(self):
270 if self._callback:
271 Wbase.CallbackCall(self._callback, 1, '--')
273 def setmin(self, min):
274 self._control.SetControlMinimum(min)
276 def setmax(self, min):
277 self._control.SetControlMinimum(max)
279 def getmin(self):
280 return self._control.GetControlMinimum()
282 def getmax(self):
283 return self._control.GetControlMinimum()
285 # internals
286 def click(self, point, modifiers):
287 if not self._enabled:
288 return
289 # custom TrackControl. A mousedown in a scrollbar arrow or page area should
290 # generate _control hits as long as the mouse is a) down, b) still in the same part
291 part = self._control.TestControl(point)
292 if Controls.inUpButton <= part <= Controls.inPageDown:
293 self._control.HiliteControl(part)
294 self._hit(part)
295 oldpart = part
296 while Evt.StillDown():
297 part = self._control.TestControl(point)
298 if part == oldpart:
299 self._control.HiliteControl(part)
300 self._hit(part)
301 else:
302 self._control.HiliteControl(0)
303 self.SetPort()
304 point = Evt.GetMouse()
305 self._control.HiliteControl(0)
306 elif part == Controls.inThumb:
307 part = self._control.TrackControl(point)
308 if part:
309 self._hit(part)
311 def _hit(self, part):
312 if part == Controls.inThumb:
313 value = self._control.GetControlValue()
314 elif part == Controls.inUpButton:
315 value = "+"
316 elif part == Controls.inDownButton:
317 value = "-"
318 elif part == Controls.inPageUp:
319 value = "++"
320 elif part == Controls.inPageDown:
321 value = "--"
322 if self._callback:
323 Wbase.CallbackCall(self._callback, 1, value)
325 def draw(self, visRgn = None):
326 if self._visible:
327 self._control.Draw1Control()
328 Qd.FrameRect(self._bounds)
330 def adjust(self, oldbounds):
331 self.SetPort()
332 Win.InvalRect(oldbounds)
333 self._control.HideControl()
334 self._control.MoveControl(self._bounds[0], self._bounds[1])
335 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
336 if self._visible:
337 Qd.EraseRect(self._bounds)
338 if self._activated:
339 self._control.ShowControl()
340 else:
341 Qd.FrameRect(self._bounds)
342 Win.ValidRect(self._bounds)
344 def activate(self, onoff):
345 self._activated = onoff
346 if self._visible:
347 if onoff:
348 self._control.ShowControl()
349 else:
350 self._control.HideControl()
351 self.draw(None)
352 Win.ValidRect(self._bounds)
354 def set(self, value):
355 if self._control:
356 self._control.SetControlValue(value)
357 else:
358 self._value = value
360 def get(self):
361 if self._control:
362 return self._control.GetControlValue()
363 else:
364 return self._value
367 def _scalebarvalue(absmin, absmax, curmin, curmax):
368 if curmin <= absmin and curmax >= absmax:
369 return None
370 if curmin <= absmin:
371 return 0
372 if curmax >= absmax:
373 return 32767
374 perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
375 return int(perc*32767)