1 from Carbon
import Ctl
, Controls
2 from Carbon
import Evt
, Qd
, Win
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
)
14 self
._callback
= callback
20 self
._viewsize
= viewsize
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:
36 if -32768 <= _min
<= 32767:
41 if -32768 <= _max
<= 32767:
46 self
._control
= Ctl
.NewControl(self
._parentwindow
.wid
,
56 self
._control
.SetControl32BitValue(bigvalue
)
58 self
._control
.SetControl32BitMinimum(bigmin
)
60 self
._control
.SetControl32BitMaximum(bigmax
)
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:
68 self
.enable(self
._enabled
)
70 def adjust(self
, oldbounds
):
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])
76 Qd
.EraseRect(self
._bounds
)
77 self
._control
.ShowControl()
78 self
.GetWindow().ValidWindowRect(self
._bounds
)
81 self
._control
.HideControl()
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)
90 def show(self
, onoff
):
92 for w
in self
._widgets
:
95 self
._control
.ShowControl()
97 self
._control
.HideControl()
99 def activate(self
, onoff
):
100 self
._activated
= onoff
103 self
._control
.ActivateControl()
105 self
._control
.DeactivateControl()
107 def draw(self
, visRgn
= None):
109 self
._control
.Draw1Control()
111 def test(self
, point
):
112 if Qd
.PtInRect(point
, self
._bounds
) and self
._enabled
:
114 #ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
115 #if self._enabled and control == self._control:
118 def click(self
, point
, modifiers
):
119 if not self
._enabled
:
121 part
= self
._control
.TrackControl(point
)
124 Wbase
.CallbackCall(self
._callback
, 0)
126 def settitle(self
, title
):
128 self
._control
.SetControlTitle(title
)
134 def set(self
, value
):
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
)
141 self
._control
.SetControl32BitValue(value
)
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()
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)
168 if not self
._enabled
:
170 # emulate the pushing of the button
172 self
._control
.HiliteControl(Controls
.kControlButtonPart
)
173 Qd
.QDFlushPortBuffer(self
._parentwindow
.wid
, None) # needed under OSX
175 self
._control
.HiliteControl(0)
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):
189 self
._control
.Draw1Control()
192 ControlWidget
.open(self
)
194 self
._setdefault
(self
._isdefault
)
196 def _setdefault(self
, onoff
):
203 # hide before changing state, otherwise the button isn't always
204 # redrawn correctly, although it's quite different under Aqua
207 c
.SetControlData(Controls
.kControlNoPart
,
208 Controls
.kControlPushButtonDefaultTag
, data
)
210 self
._isdefault
= onoff
212 def adjust(self
, oldbounds
):
214 old
= Qd
.InsetRect(oldbounds
, -4, -4)
215 new
= Qd
.InsetRect(self
._bounds
, -4, -4)
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
:
237 part
= self
._control
.TrackControl(point
)
241 Wbase
.CallbackCall(self
._callback
, 0, self
.get())
244 if not self
._enabled
:
248 Wbase
.CallbackCall(self
._callback
, 0, self
.get())
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
)
267 self
.thebuttons
= None
268 ControlWidget
.close(self
)
270 def click(self
, point
, modifiers
):
271 if not self
._enabled
:
273 part
= self
._control
.TrackControl(point
)
277 Wbase
.CallbackCall(self
._callback
, 0, 1)
280 if not self
._enabled
:
284 Wbase
.CallbackCall(self
._callback
, 0, 1)
286 def set(self
, value
):
287 for button
in self
.thebuttons
:
289 button
._control
.SetControlValue(button
== self
)
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):
300 procID
= Controls
.kControlScrollBarLiveProc
302 procID
= Controls
.scrollBarProc
303 ControlWidget
.__init
__(self
, possize
, "", procID
, callback
, value
, min, max)
306 # def set(self, value):
308 # Wbase.CallbackCall(self._callback, 1, value)
312 Wbase
.CallbackCall(self
._callback
, 1, '+')
316 Wbase
.CallbackCall(self
._callback
, 1, '-')
320 Wbase
.CallbackCall(self
._callback
, 1, '++')
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)
333 self
._control
.SetControl32BitMinimum(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)
344 self
._control
.SetControl32BitMaximum(max)
348 def setviewsize(self
, viewsize
):
349 if self
._control
is not None:
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:
357 self
._viewsize
= viewsize
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()
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()
376 def click(self
, point
, modifiers
):
377 if not self
._enabled
:
379 def hitter(ctl
, part
, self
=self
):
382 part
= self
._control
.TrackControl(point
, hitter
)
386 def _hit(self
, part
):
388 if part
== Controls
.inThumb
:
390 value
= self
._control
.GetControl32BitValue()
391 # No 32-bit control support in MacOS 8.1, so fall
392 # back to the 16-bit interface when needed.
393 except NotImplementedError:
394 value
= self
._control
.GetControlValue()
395 elif part
== Controls
.inUpButton
:
397 elif part
== Controls
.inDownButton
:
399 elif part
== Controls
.inPageUp
:
401 elif part
== Controls
.inPageDown
:
403 if value
is not None and self
._callback
:
404 Wbase
.CallbackCall(self
._callback
, 1, value
)
406 def draw(self
, visRgn
= None):
408 self
._control
.Draw1Control()
409 #Qd.FrameRect(self._bounds)
411 def adjust(self
, oldbounds
):
413 self
.GetWindow().InvalWindowRect(oldbounds
)
414 self
._control
.HideControl()
415 self
._control
.MoveControl(self
._bounds
[0], self
._bounds
[1])
416 self
._control
.SizeControl(self
._bounds
[2] - self
._bounds
[0], self
._bounds
[3] - self
._bounds
[1])
418 Qd
.EraseRect(self
._bounds
)
420 self
._control
.ShowControl()
422 Qd
.FrameRect(self
._bounds
)
423 self
.GetWindow().ValidWindowRect(self
._bounds
)
426 def _scalebarvalue(absmin
, absmax
, curmin
, curmax
):
427 if curmin
<= absmin
and curmax
>= absmax
:
433 perc
= float(curmin
-absmin
) / float((absmax
- absmin
) - (curmax
- curmin
))
434 return int(perc
*32767)