blob: 399a930f7cfbd04f20ec9215002cfd7279b41c8b [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:
68 self._control.HiliteControl((not onoff) and 255)
69
70 def draw(self, visRgn = None):
71 if self._visible:
72 self._control.Draw1Control()
73
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
78
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)
86
87 def settitle(self, title):
88 if self._control:
89 self._control.SetControlTitle(title)
90 self._title = title
91
92 def gettitle(self):
93 return self._title
94
95class Button(ControlWidget):
96
97 """Standard push button."""
98
99 def __init__(self, possize, title = "Button", callback = None):
100 procID = Controls.pushButProc | Controls.useWFont
101 ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
102 self._isdefault = 0
103
104 def push(self):
105 if not self._enabled:
106 return
107 import time
108 self._control.HiliteControl(1)
109 time.sleep(0.1)
110 self._control.HiliteControl(0)
111 if self._callback:
112 Wbase.CallbackCall(self._callback, 0)
113
114 def enable(self, onoff):
115 if self._control and self._enabled <> onoff:
116 self._control.HiliteControl((not onoff) and 255)
117 self._enabled = onoff
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000118
119 def activate(self, onoff):
120 self._activated = onoff
121 if self._enabled:
122 self._control.HiliteControl((not onoff) and 255)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000123
124 def show(self, onoff):
125 ControlWidget.show(self, onoff)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000126
127 def draw(self, visRgn = None):
128 if self._visible:
129 self._control.Draw1Control()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000130
131 def _setdefault(self, onoff):
132 self._isdefault = onoff
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000133
134 def adjust(self, oldbounds):
135 if self._isdefault:
136 old = Qd.InsetRect(oldbounds, -4, -4)
137 new = Qd.InsetRect(self._bounds, -4, -4)
138 Qd.EraseRect(old)
Jack Jansen73023402001-01-23 14:58:20 +0000139 self.GetWindow().InvalWindowRect(old)
140 self.GetWindow().InvalWindowRect(new)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000141 ControlWidget.adjust(self, oldbounds)
142
143
144class CheckBox(ControlWidget):
145
146 """Standard checkbox."""
147
148 def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
149 procID = Controls.checkBoxProc | Controls.useWFont
150 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
151
152 def click(self, point, modifiers):
153 if not self._enabled:
154 return
155 part = self._control.TrackControl(point)
156 if part:
157 self.toggle()
158 if self._callback:
159 Wbase.CallbackCall(self._callback, 0, self.get())
160
161 def push(self):
162 if not self._enabled:
163 return
164 self.toggle()
165 if self._callback:
166 Wbase.CallbackCall(self._callback, 0, self.get())
167
168 def toggle(self):
169 self.set(not self.get())
170
171 def set(self, value):
172 if self._control:
173 self._control.SetControlValue(value)
174 else:
175 self._value = value
176
177 def get(self):
178 if self._control:
179 return self._control.GetControlValue()
180 else:
181 return self._value
182
183
184class RadioButton(ControlWidget):
185
186 """Standard radiobutton."""
187
188 # XXX We need a radiogroup widget; this is too kludgy.
189
190 def __init__(self, possize, title, thebuttons, callback = None, value = 0):
191 procID = Controls.radioButProc | Controls.useWFont
192 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
193 self.thebuttons = thebuttons
194 thebuttons.append(self)
195
196 def close(self):
197 self.thebuttons = None
198 ControlWidget.close(self)
199
200 def click(self, point, modifiers):
201 if not self._enabled:
202 return
203 part = self._control.TrackControl(point)
204 if part:
205 self.set(1)
206 if self._callback:
207 Wbase.CallbackCall(self._callback, 0, 1)
208
209 def push(self):
210 if not self._enabled:
211 return
212 self.set(1)
213 if self._callback:
214 Wbase.CallbackCall(self._callback, 0, 1)
215
216 def set(self, value):
217 for button in self.thebuttons:
218 if button._control:
219 button._control.SetControlValue(button == self)
220 else:
221 button._value = (button == self)
222
223 def get(self):
224 if self._control:
225 return self._control.GetControlValue()
226 else:
227 return self._value
228
229
230class Scrollbar(ControlWidget):
231
232 """Standard scrollbar."""
233
234 def __init__(self, possize, callback = None, value = 0, min = 0, max = 0):
235 procID = Controls.scrollBarProc
236 ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
237
238 # interface
239 def set(self, value):
240 if self._callback:
241 Wbase.CallbackCall(self._callback, 1, value)
242
243 def up(self):
244 if self._callback:
245 Wbase.CallbackCall(self._callback, 1, '+')
246
247 def down(self):
248 if self._callback:
249 Wbase.CallbackCall(self._callback, 1, '-')
250
251 def pageup(self):
252 if self._callback:
253 Wbase.CallbackCall(self._callback, 1, '++')
254
255 def pagedown(self):
256 if self._callback:
257 Wbase.CallbackCall(self._callback, 1, '--')
258
259 def setmin(self, min):
260 self._control.SetControlMinimum(min)
261
262 def setmax(self, min):
263 self._control.SetControlMinimum(max)
264
265 def getmin(self):
266 return self._control.GetControlMinimum()
267
268 def getmax(self):
269 return self._control.GetControlMinimum()
270
271 # internals
272 def click(self, point, modifiers):
273 if not self._enabled:
274 return
275 # custom TrackControl. A mousedown in a scrollbar arrow or page area should
276 # generate _control hits as long as the mouse is a) down, b) still in the same part
277 part = self._control.TestControl(point)
278 if Controls.inUpButton <= part <= Controls.inPageDown:
279 self._control.HiliteControl(part)
280 self._hit(part)
281 oldpart = part
Just van Rossumeb67a7b1999-04-21 09:24:02 +0000282 # slight delay before scrolling at top speed...
283 now = Evt.TickCount()
284 while Evt.StillDown():
285 if (Evt.TickCount() - now) > 18: # 0.3 seconds
286 break
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000287 while Evt.StillDown():
288 part = self._control.TestControl(point)
289 if part == oldpart:
290 self._control.HiliteControl(part)
291 self._hit(part)
292 else:
293 self._control.HiliteControl(0)
294 self.SetPort()
295 point = Evt.GetMouse()
296 self._control.HiliteControl(0)
297 elif part == Controls.inThumb:
298 part = self._control.TrackControl(point)
299 if part:
300 self._hit(part)
301
302 def _hit(self, part):
303 if part == Controls.inThumb:
304 value = self._control.GetControlValue()
305 elif part == Controls.inUpButton:
306 value = "+"
307 elif part == Controls.inDownButton:
308 value = "-"
309 elif part == Controls.inPageUp:
310 value = "++"
311 elif part == Controls.inPageDown:
312 value = "--"
313 if self._callback:
314 Wbase.CallbackCall(self._callback, 1, value)
315
316 def draw(self, visRgn = None):
317 if self._visible:
318 self._control.Draw1Control()
319 Qd.FrameRect(self._bounds)
320
321 def adjust(self, oldbounds):
322 self.SetPort()
Jack Jansen73023402001-01-23 14:58:20 +0000323 self.GetWindow().InvalWindowRect(oldbounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000324 self._control.HideControl()
325 self._control.MoveControl(self._bounds[0], self._bounds[1])
326 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
327 if self._visible:
328 Qd.EraseRect(self._bounds)
329 if self._activated:
330 self._control.ShowControl()
331 else:
332 Qd.FrameRect(self._bounds)
Jack Jansen73023402001-01-23 14:58:20 +0000333 self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000334
335 def activate(self, onoff):
336 self._activated = onoff
337 if self._visible:
338 if onoff:
339 self._control.ShowControl()
340 else:
341 self._control.HideControl()
342 self.draw(None)
Jack Jansen73023402001-01-23 14:58:20 +0000343 self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000344
345 def set(self, value):
346 if self._control:
347 self._control.SetControlValue(value)
348 else:
349 self._value = value
350
351 def get(self):
352 if self._control:
353 return self._control.GetControlValue()
354 else:
355 return self._value
356
357
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000358def _scalebarvalue(absmin, absmax, curmin, curmax):
359 if curmin <= absmin and curmax >= absmax:
360 return None
361 if curmin <= absmin:
362 return 0
363 if curmax >= absmax:
364 return 32767
365 perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
366 return int(perc*32767)
367