Refactoring for fileConfig. Contributed by Shane Hathaway.
[python.git] / Mac / Tools / IDE / Wcontrols.py
blob00b06ac0d35760a1234fe8ca5dc4ee2b1016c3a9
1 from Carbon import Ctl, Controls
2 from Carbon import Evt, Qd, Win
3 import Wbase
6 class ControlWidget(Wbase.ClickableWidget):
8 """Baseclass for all native controls."""
10 def __init__(self, possize, title = "Control", procID = 0, callback = None, value = 0, min = 0, max = 1, viewsize = 0):
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
20 self._viewsize = viewsize
22 def open(self):
23 self._calcbounds()
25 # NewControl doesn't accept 32-bit value, min, or max, so for consistency
26 # with the new 32-bit set/get methods, out-of-range values are initially
27 # set as zero, followed by a 32-bit set of the actual value.
28 # Values not representable in 16 bits will fail on MacOS 8.1, however
29 # the vast majority of control usage should still be compatible.
30 _value, _min, _max = self._value, self._min, self._max
31 if -32768 <= _value <= 32767:
32 bigvalue = None
33 else:
34 bigvalue = _value
35 _value = 0
36 if -32768 <= _min <= 32767:
37 bigmin = None
38 else:
39 bigmin = _min
40 _min = 0
41 if -32768 <= _max <= 32767:
42 bigmax = None
43 else:
44 bigmax = _max
45 _max = 0
46 self._control = Ctl.NewControl(self._parentwindow.wid,
47 self._bounds,
48 self._title,
50 _value,
51 _min,
52 _max,
53 self._procID,
55 if bigvalue:
56 self._control.SetControl32BitValue(bigvalue)
57 if bigmin:
58 self._control.SetControl32BitMinimum(bigmin)
59 if bigmax:
60 self._control.SetControl32BitMaximum(bigmax)
61 if self._viewsize:
62 try:
63 self._control.SetControlViewSize(self._viewsize)
64 # Not available in MacOS 8.1, but that's OK since it only affects
65 # proportional scrollbars which weren't available in 8.1 either.
66 except NotImplementedError:
67 pass
68 self.enable(self._enabled)
70 def adjust(self, oldbounds):
71 self.SetPort()
72 self._control.HideControl()
73 self._control.MoveControl(self._bounds[0], self._bounds[1])
74 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
75 if self._visible:
76 Qd.EraseRect(self._bounds)
77 self._control.ShowControl()
78 self.GetWindow().ValidWindowRect(self._bounds)
80 def close(self):
81 self._control.HideControl()
82 self._control = None
83 Wbase.ClickableWidget.close(self)
85 def enable(self, onoff):
86 if self._control and self._enabled <> onoff:
87 self._control.HiliteControl((not onoff) and 255)
88 self._enabled = onoff
90 def show(self, onoff):
91 self._visible = onoff
92 for w in self._widgets:
93 w.show(onoff)
94 if onoff:
95 self._control.ShowControl()
96 else:
97 self._control.HideControl()
99 def activate(self, onoff):
100 self._activated = onoff
101 if self._enabled:
102 if onoff:
103 self._control.ActivateControl()
104 else:
105 self._control.DeactivateControl()
107 def draw(self, visRgn = None):
108 if self._visible:
109 self._control.Draw1Control()
111 def test(self, point):
112 if Qd.PtInRect(point, self._bounds) and self._enabled:
113 return 1
114 #ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
115 #if self._enabled and control == self._control:
116 # return 1
118 def click(self, point, modifiers):
119 if not self._enabled:
120 return
121 part = self._control.TrackControl(point)
122 if part:
123 if self._callback:
124 Wbase.CallbackCall(self._callback, 0)
126 def settitle(self, title):
127 if self._control:
128 self._control.SetControlTitle(title)
129 self._title = title
131 def gettitle(self):
132 return self._title
134 def set(self, value):
135 if self._control:
136 if -32768 <= value <= 32767:
137 # No 32-bit control support in MacOS 8.1, so use
138 # the 16-bit interface when possible.
139 self._control.SetControlValue(value)
140 else:
141 self._control.SetControl32BitValue(value)
142 else:
143 self._value = value
145 def get(self):
146 if self._control:
147 try:
148 return self._control.GetControl32BitValue()
149 # No 32-bit control support in MacOS 8.1, so fall
150 # back to the 16-bit interface when needed.
151 except NotImplementedError:
152 return self._control.GetControlValue()
153 else:
154 return self._value
157 class Button(ControlWidget):
159 """Standard push button."""
161 procID = Controls.pushButProc | Controls.useWFont
163 def __init__(self, possize, title = "Button", callback = None):
164 ControlWidget.__init__(self, possize, title, self.procID, callback, 0, 0, 1)
165 self._isdefault = 0
167 def push(self):
168 if not self._enabled:
169 return
170 # emulate the pushing of the button
171 import time
172 self._control.HiliteControl(Controls.kControlButtonPart)
173 self._parentwindow.wid.GetWindowPort().QDFlushPortBuffer(None) # needed under OSX
174 time.sleep(0.1)
175 self._control.HiliteControl(0)
176 if self._callback:
177 Wbase.CallbackCall(self._callback, 0)
179 def enable(self, onoff):
180 if self._control and self._enabled <> onoff:
181 self._control.HiliteControl((not onoff) and 255)
182 self._enabled = onoff
184 def show(self, onoff):
185 ControlWidget.show(self, onoff)
187 def draw(self, visRgn = None):
188 if self._visible:
189 self._control.Draw1Control()
191 def open(self):
192 ControlWidget.open(self)
193 if self._isdefault:
194 self._setdefault(self._isdefault)
196 def _setdefault(self, onoff):
197 c = self._control
198 if c is not None:
199 if onoff:
200 data = "\xFF"
201 else:
202 data = "\0"
203 # hide before changing state, otherwise the button isn't always
204 # redrawn correctly, although it's quite different under Aqua
205 # and Classic...
206 c.HideControl()
207 c.SetControlData(Controls.kControlNoPart,
208 Controls.kControlPushButtonDefaultTag, data)
209 c.ShowControl()
210 self._isdefault = onoff
212 def adjust(self, oldbounds):
213 if self._isdefault:
214 old = Qd.InsetRect(oldbounds, -4, -4)
215 new = Qd.InsetRect(self._bounds, -4, -4)
216 Qd.EraseRect(old)
217 self.GetWindow().InvalWindowRect(old)
218 self.GetWindow().InvalWindowRect(new)
219 ControlWidget.adjust(self, oldbounds)
222 class BevelButton(Button):
223 procID = Controls.kControlBevelButtonNormalBevelProc | Controls.useWFont
226 class CheckBox(ControlWidget):
228 """Standard checkbox."""
230 def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
231 procID = Controls.checkBoxProc | Controls.useWFont
232 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
234 def click(self, point, modifiers):
235 if not self._enabled:
236 return
237 part = self._control.TrackControl(point)
238 if part:
239 self.toggle()
240 if self._callback:
241 Wbase.CallbackCall(self._callback, 0, self.get())
243 def push(self):
244 if not self._enabled:
245 return
246 self.toggle()
247 if self._callback:
248 Wbase.CallbackCall(self._callback, 0, self.get())
250 def toggle(self):
251 self.set(not self.get())
254 class RadioButton(ControlWidget):
256 """Standard radiobutton."""
258 # XXX We need a radiogroup widget; this is too kludgy.
260 def __init__(self, possize, title, thebuttons, callback = None, value = 0):
261 procID = Controls.radioButProc | Controls.useWFont
262 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
263 self.thebuttons = thebuttons
264 thebuttons.append(self)
266 def close(self):
267 self.thebuttons = None
268 ControlWidget.close(self)
270 def click(self, point, modifiers):
271 if not self._enabled:
272 return
273 part = self._control.TrackControl(point)
274 if part:
275 self.set(1)
276 if self._callback:
277 Wbase.CallbackCall(self._callback, 0, 1)
279 def push(self):
280 if not self._enabled:
281 return
282 self.set(1)
283 if self._callback:
284 Wbase.CallbackCall(self._callback, 0, 1)
286 def set(self, value):
287 for button in self.thebuttons:
288 if button._control:
289 button._control.SetControlValue(button == self)
290 else:
291 button._value = (button == self)
294 class Scrollbar(ControlWidget):
296 """Standard scrollbar."""
298 def __init__(self, possize, callback=None, value=0, min=0, max=0, livefeedback=1):
299 if livefeedback:
300 procID = Controls.kControlScrollBarLiveProc
301 else:
302 procID = Controls.scrollBarProc
303 ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
305 # interface
306 # def set(self, value):
307 # if self._callback:
308 # Wbase.CallbackCall(self._callback, 1, value)
310 def up(self):
311 if self._callback:
312 Wbase.CallbackCall(self._callback, 1, '+')
314 def down(self):
315 if self._callback:
316 Wbase.CallbackCall(self._callback, 1, '-')
318 def pageup(self):
319 if self._callback:
320 Wbase.CallbackCall(self._callback, 1, '++')
322 def pagedown(self):
323 if self._callback:
324 Wbase.CallbackCall(self._callback, 1, '--')
326 def setmin(self, min):
327 if self._control is not None:
328 if -32768 <= min <= 32767:
329 # No 32-bit control support in MacOS 8.1, so use
330 # the 16-bit interface when possible.
331 self._control.SetControlMinimum(min)
332 else:
333 self._control.SetControl32BitMinimum(min)
334 else:
335 self._min = min
337 def setmax(self, max):
338 if self._control is not None:
339 if -32768 <= max <= 32767:
340 # No 32-bit control support in MacOS 8.1, so use
341 # the 16-bit interface when possible.
342 self._control.SetControlMaximum(max)
343 else:
344 self._control.SetControl32BitMaximum(max)
345 else:
346 self._max = max
348 def setviewsize(self, viewsize):
349 if self._control is not None:
350 try:
351 self._control.SetControlViewSize(viewsize)
352 # Not available in MacOS 8.1, but that's OK since it only affects
353 # proportional scrollbars which weren't available in 8.1 either.
354 except NotImplementedError:
355 pass
356 else:
357 self._viewsize = viewsize
359 def getmin(self):
360 try:
361 return self._control.GetControl32BitMinimum()
362 # No 32-bit control support in MacOS 8.1, so fall
363 # back to the 16-bit interface when needed.
364 except NotImplementedError:
365 return self._control.GetControlMinimum()
367 def getmax(self):
368 try:
369 return self._control.GetControl32BitMaximum()
370 # No 32-bit control support in MacOS 8.1, so fall
371 # back to the 16-bit interface when needed.
372 except NotImplementedError:
373 return self._control.GetControlMaximum()
375 # internals
376 def click(self, point, modifiers):
377 if not self._enabled:
378 return
379 def hitter(ctl, part, self=self):
380 if part:
381 self._hit(part)
382 part = self._control.TrackControl(point, hitter)
384 def _hit(self, part):
385 value = None
386 if part == Controls.inThumb:
387 try:
388 value = self._control.GetControl32BitValue()
389 # No 32-bit control support in MacOS 8.1, so fall
390 # back to the 16-bit interface when needed.
391 except NotImplementedError:
392 value = self._control.GetControlValue()
393 elif part == Controls.inUpButton:
394 value = "+"
395 elif part == Controls.inDownButton:
396 value = "-"
397 elif part == Controls.inPageUp:
398 value = "++"
399 elif part == Controls.inPageDown:
400 value = "--"
401 if value is not None and self._callback:
402 Wbase.CallbackCall(self._callback, 1, value)
404 def draw(self, visRgn = None):
405 if self._visible:
406 self._control.Draw1Control()
407 #Qd.FrameRect(self._bounds)
409 def adjust(self, oldbounds):
410 self.SetPort()
411 self.GetWindow().InvalWindowRect(oldbounds)
412 self._control.HideControl()
413 self._control.MoveControl(self._bounds[0], self._bounds[1])
414 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
415 if self._visible:
416 Qd.EraseRect(self._bounds)
417 if self._activated:
418 self._control.ShowControl()
419 else:
420 Qd.FrameRect(self._bounds)
421 self.GetWindow().ValidWindowRect(self._bounds)
424 def _scalebarvalue(absmin, absmax, curmin, curmax):
425 if curmin <= absmin and curmax >= absmax:
426 return None
427 if curmin <= absmin:
428 return 0
429 if curmax >= absmax:
430 return 32767
431 perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
432 return int(perc*32767)