blob: 879dab5cdaa2038427f5ad7decd47667aed78169 [file] [log] [blame]
Just van Rossum64350b42001-11-02 19:17:16 +00001from Carbon import Ctl, Controls
2from Carbon import Evt, Qd, Win
Just van Rossum40f9b7b1999-01-30 22:39:17 +00003import Wbase
Just van Rossum64350b42001-11-02 19:17:16 +00004
Just van Rossum40f9b7b1999-01-30 22:39:17 +00005
6class ControlWidget(Wbase.ClickableWidget):
7
8 """Baseclass for all native controls."""
9
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
20
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,
31 0)
32 self.SetPort()
Jack Jansen73023402001-01-23 14:58:20 +000033 #self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000034 self.enable(self._enabled)
35
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()
Jack Jansen73023402001-01-23 14:58:20 +000044 self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000045
46 def close(self):
47 self._control.HideControl()
48 self._control = None
49 Wbase.ClickableWidget.close(self)
50
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
55
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()
64
65 def activate(self, onoff):
66 self._activated = onoff
67 if self._enabled:
Just van Rossuma73f78b2001-11-02 21:12:52 +000068 if onoff:
69 self._control.ActivateControl()
70 else:
71 self._control.DeactivateControl()
Just van Rossum40f9b7b1999-01-30 22:39:17 +000072
73 def draw(self, visRgn = None):
74 if self._visible:
75 self._control.Draw1Control()
76
77 def test(self, point):
78 ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
79 if self._enabled and control == self._control:
80 return 1
81
82 def click(self, point, modifiers):
83 if not self._enabled:
84 return
85 part = self._control.TrackControl(point)
86 if part:
87 if self._callback:
88 Wbase.CallbackCall(self._callback, 0)
89
90 def settitle(self, title):
91 if self._control:
92 self._control.SetControlTitle(title)
93 self._title = title
94
95 def gettitle(self):
96 return self._title
Just van Rossum2dd4d162001-11-02 22:51:42 +000097
98 def set(self, value):
99 if self._control:
100 self._control.SetControl32BitValue(value)
101 else:
102 self._value = value
103
104 def get(self):
105 if self._control:
106 return self._control.GetControl32BitValue()
107 else:
108 return self._value
109
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000110
111class Button(ControlWidget):
112
113 """Standard push button."""
114
115 def __init__(self, possize, title = "Button", callback = None):
116 procID = Controls.pushButProc | Controls.useWFont
117 ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
118 self._isdefault = 0
119
120 def push(self):
121 if not self._enabled:
122 return
123 import time
Just van Rossuma73f78b2001-11-02 21:12:52 +0000124 self._control.HiliteControl(Controls.kControlButtonPart)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000125 time.sleep(0.1)
126 self._control.HiliteControl(0)
127 if self._callback:
128 Wbase.CallbackCall(self._callback, 0)
129
130 def enable(self, onoff):
131 if self._control and self._enabled <> onoff:
132 self._control.HiliteControl((not onoff) and 255)
133 self._enabled = onoff
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000134
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000135 def show(self, onoff):
136 ControlWidget.show(self, onoff)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000137
138 def draw(self, visRgn = None):
139 if self._visible:
140 self._control.Draw1Control()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000141
142 def _setdefault(self, onoff):
143 self._isdefault = onoff
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000144
145 def adjust(self, oldbounds):
146 if self._isdefault:
147 old = Qd.InsetRect(oldbounds, -4, -4)
148 new = Qd.InsetRect(self._bounds, -4, -4)
149 Qd.EraseRect(old)
Jack Jansen73023402001-01-23 14:58:20 +0000150 self.GetWindow().InvalWindowRect(old)
151 self.GetWindow().InvalWindowRect(new)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000152 ControlWidget.adjust(self, oldbounds)
153
154
155class CheckBox(ControlWidget):
156
157 """Standard checkbox."""
158
159 def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
160 procID = Controls.checkBoxProc | Controls.useWFont
161 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
162
163 def click(self, point, modifiers):
164 if not self._enabled:
165 return
166 part = self._control.TrackControl(point)
167 if part:
168 self.toggle()
169 if self._callback:
170 Wbase.CallbackCall(self._callback, 0, self.get())
171
172 def push(self):
173 if not self._enabled:
174 return
175 self.toggle()
176 if self._callback:
177 Wbase.CallbackCall(self._callback, 0, self.get())
178
179 def toggle(self):
180 self.set(not self.get())
Just van Rossum2dd4d162001-11-02 22:51:42 +0000181
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000182
183class RadioButton(ControlWidget):
184
185 """Standard radiobutton."""
186
187 # XXX We need a radiogroup widget; this is too kludgy.
188
189 def __init__(self, possize, title, thebuttons, callback = None, value = 0):
190 procID = Controls.radioButProc | Controls.useWFont
191 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
192 self.thebuttons = thebuttons
193 thebuttons.append(self)
194
195 def close(self):
196 self.thebuttons = None
197 ControlWidget.close(self)
198
199 def click(self, point, modifiers):
200 if not self._enabled:
201 return
202 part = self._control.TrackControl(point)
203 if part:
204 self.set(1)
205 if self._callback:
206 Wbase.CallbackCall(self._callback, 0, 1)
207
208 def push(self):
209 if not self._enabled:
210 return
211 self.set(1)
212 if self._callback:
213 Wbase.CallbackCall(self._callback, 0, 1)
214
215 def set(self, value):
216 for button in self.thebuttons:
217 if button._control:
218 button._control.SetControlValue(button == self)
219 else:
220 button._value = (button == self)
Just van Rossum2dd4d162001-11-02 22:51:42 +0000221
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000222
223class Scrollbar(ControlWidget):
224
225 """Standard scrollbar."""
226
227 def __init__(self, possize, callback = None, value = 0, min = 0, max = 0):
228 procID = Controls.scrollBarProc
229 ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
230
231 # interface
Just van Rossum2dd4d162001-11-02 22:51:42 +0000232# def set(self, value):
233# if self._callback:
234# Wbase.CallbackCall(self._callback, 1, value)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000235
236 def up(self):
237 if self._callback:
238 Wbase.CallbackCall(self._callback, 1, '+')
239
240 def down(self):
241 if self._callback:
242 Wbase.CallbackCall(self._callback, 1, '-')
243
244 def pageup(self):
245 if self._callback:
246 Wbase.CallbackCall(self._callback, 1, '++')
247
248 def pagedown(self):
249 if self._callback:
250 Wbase.CallbackCall(self._callback, 1, '--')
251
252 def setmin(self, min):
Just van Rossum2dd4d162001-11-02 22:51:42 +0000253 self._control.SetControl32BitMinimum(min)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000254
Just van Rossum2dd4d162001-11-02 22:51:42 +0000255 def setmax(self, max):
256 self._control.SetControl32BitMaximum(max)
257
258 def setviewsize(self, view):
259 self._control.SetControlViewSize(view)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000260
261 def getmin(self):
Just van Rossum2dd4d162001-11-02 22:51:42 +0000262 return self._control.GetControl32BitMinimum()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000263
264 def getmax(self):
Just van Rossum2dd4d162001-11-02 22:51:42 +0000265 return self._control.GetControl32BitMaximum()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000266
267 # internals
268 def click(self, point, modifiers):
269 if not self._enabled:
270 return
271 # custom TrackControl. A mousedown in a scrollbar arrow or page area should
272 # generate _control hits as long as the mouse is a) down, b) still in the same part
273 part = self._control.TestControl(point)
274 if Controls.inUpButton <= part <= Controls.inPageDown:
275 self._control.HiliteControl(part)
276 self._hit(part)
277 oldpart = part
Just van Rossumeb67a7b1999-04-21 09:24:02 +0000278 # slight delay before scrolling at top speed...
279 now = Evt.TickCount()
280 while Evt.StillDown():
281 if (Evt.TickCount() - now) > 18: # 0.3 seconds
282 break
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000283 while Evt.StillDown():
284 part = self._control.TestControl(point)
285 if part == oldpart:
286 self._control.HiliteControl(part)
287 self._hit(part)
288 else:
289 self._control.HiliteControl(0)
290 self.SetPort()
291 point = Evt.GetMouse()
292 self._control.HiliteControl(0)
293 elif part == Controls.inThumb:
294 part = self._control.TrackControl(point)
295 if part:
296 self._hit(part)
297
298 def _hit(self, part):
299 if part == Controls.inThumb:
Just van Rossum2dd4d162001-11-02 22:51:42 +0000300 value = self._control.GetControl32BitValue()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000301 elif part == Controls.inUpButton:
302 value = "+"
303 elif part == Controls.inDownButton:
304 value = "-"
305 elif part == Controls.inPageUp:
306 value = "++"
307 elif part == Controls.inPageDown:
308 value = "--"
309 if self._callback:
310 Wbase.CallbackCall(self._callback, 1, value)
311
312 def draw(self, visRgn = None):
313 if self._visible:
314 self._control.Draw1Control()
315 Qd.FrameRect(self._bounds)
316
317 def adjust(self, oldbounds):
318 self.SetPort()
Jack Jansen73023402001-01-23 14:58:20 +0000319 self.GetWindow().InvalWindowRect(oldbounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000320 self._control.HideControl()
321 self._control.MoveControl(self._bounds[0], self._bounds[1])
322 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
323 if self._visible:
324 Qd.EraseRect(self._bounds)
325 if self._activated:
326 self._control.ShowControl()
327 else:
328 Qd.FrameRect(self._bounds)
Jack Jansen73023402001-01-23 14:58:20 +0000329 self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum2dd4d162001-11-02 22:51:42 +0000330
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000331
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000332def _scalebarvalue(absmin, absmax, curmin, curmax):
333 if curmin <= absmin and curmax >= absmax:
334 return None
335 if curmin <= absmin:
336 return 0
337 if curmax >= absmax:
338 return 32767
339 perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
340 return int(perc*32767)
341