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