blob: 25794497af23cc1367b6cbaf0fb4a714d1ba77f8 [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
Just van Rossumf376ef02001-11-18 14:12:43 +000020 self._viewsize = 0
Just van Rossum40f9b7b1999-01-30 22:39:17 +000021
22 def open(self):
23 self._calcbounds()
24 self._control = Ctl.NewControl(self._parentwindow.wid,
25 self._bounds,
26 self._title,
27 1,
28 self._value,
29 self._min,
30 self._max,
31 self._procID,
32 0)
Just van Rossumf376ef02001-11-18 14:12:43 +000033 if self._viewsize:
34 self._control.SetControlViewSize(self._viewsize)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000035 self.enable(self._enabled)
36
37 def adjust(self, oldbounds):
38 self.SetPort()
39 self._control.HideControl()
40 self._control.MoveControl(self._bounds[0], self._bounds[1])
41 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
42 if self._visible:
43 Qd.EraseRect(self._bounds)
44 self._control.ShowControl()
Jack Jansen73023402001-01-23 14:58:20 +000045 self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000046
47 def close(self):
48 self._control.HideControl()
49 self._control = None
50 Wbase.ClickableWidget.close(self)
51
52 def enable(self, onoff):
53 if self._control and self._enabled <> onoff:
54 self._control.HiliteControl((not onoff) and 255)
55 self._enabled = onoff
56
57 def show(self, onoff):
58 self._visible = onoff
59 for w in self._widgets:
60 w.show(onoff)
61 if onoff:
62 self._control.ShowControl()
63 else:
64 self._control.HideControl()
65
66 def activate(self, onoff):
67 self._activated = onoff
68 if self._enabled:
Just van Rossuma73f78b2001-11-02 21:12:52 +000069 if onoff:
70 self._control.ActivateControl()
71 else:
72 self._control.DeactivateControl()
Just van Rossum40f9b7b1999-01-30 22:39:17 +000073
74 def draw(self, visRgn = None):
75 if self._visible:
76 self._control.Draw1Control()
77
78 def test(self, point):
Just van Rossumf376ef02001-11-18 14:12:43 +000079 if Qd.PtInRect(point, self._bounds) and self._enabled:
Just van Rossum40f9b7b1999-01-30 22:39:17 +000080 return 1
Just van Rossumf376ef02001-11-18 14:12:43 +000081 #ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
82 #if self._enabled and control == self._control:
83 # return 1
Just van Rossum40f9b7b1999-01-30 22:39:17 +000084
85 def click(self, point, modifiers):
86 if not self._enabled:
87 return
88 part = self._control.TrackControl(point)
89 if part:
90 if self._callback:
91 Wbase.CallbackCall(self._callback, 0)
92
93 def settitle(self, title):
94 if self._control:
95 self._control.SetControlTitle(title)
96 self._title = title
97
98 def gettitle(self):
99 return self._title
Just van Rossum2dd4d162001-11-02 22:51:42 +0000100
101 def set(self, value):
102 if self._control:
103 self._control.SetControl32BitValue(value)
104 else:
105 self._value = value
106
107 def get(self):
108 if self._control:
109 return self._control.GetControl32BitValue()
110 else:
111 return self._value
112
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000113
114class Button(ControlWidget):
115
116 """Standard push button."""
117
Just van Rossumf376ef02001-11-18 14:12:43 +0000118 procID = Controls.pushButProc | Controls.useWFont
119
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000120 def __init__(self, possize, title = "Button", callback = None):
Just van Rossumf376ef02001-11-18 14:12:43 +0000121 ControlWidget.__init__(self, possize, title, self.procID, callback, 0, 0, 1)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000122 self._isdefault = 0
123
124 def push(self):
125 if not self._enabled:
126 return
Just van Rossumf376ef02001-11-18 14:12:43 +0000127 # emulate the pushing of the button
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000128 import time
Just van Rossuma73f78b2001-11-02 21:12:52 +0000129 self._control.HiliteControl(Controls.kControlButtonPart)
Just van Rossumf376ef02001-11-18 14:12:43 +0000130 Qd.QDFlushPortBuffer(self._parentwindow.wid, None) # needed under OSX
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000131 time.sleep(0.1)
132 self._control.HiliteControl(0)
133 if self._callback:
134 Wbase.CallbackCall(self._callback, 0)
135
136 def enable(self, onoff):
137 if self._control and self._enabled <> onoff:
138 self._control.HiliteControl((not onoff) and 255)
139 self._enabled = onoff
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000140
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000141 def show(self, onoff):
142 ControlWidget.show(self, onoff)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000143
144 def draw(self, visRgn = None):
145 if self._visible:
146 self._control.Draw1Control()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000147
Just van Rossumf376ef02001-11-18 14:12:43 +0000148 def open(self):
149 ControlWidget.open(self)
150 if self._isdefault:
151 self._setdefault(self._isdefault)
152
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000153 def _setdefault(self, onoff):
Just van Rossumf376ef02001-11-18 14:12:43 +0000154 c = self._control
155 if c is not None:
156 if onoff:
157 data = "\xFF"
158 else:
159 data = "\0"
160 # hide before changing state, otherwise the button isn't always
161 # redrawn correctly, although it's quite different under Aqua
162 # and Classic...
163 c.HideControl()
164 c.SetControlData(Controls.kControlNoPart,
165 Controls.kControlPushButtonDefaultTag, data)
166 c.ShowControl()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000167 self._isdefault = onoff
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000168
169 def adjust(self, oldbounds):
170 if self._isdefault:
171 old = Qd.InsetRect(oldbounds, -4, -4)
172 new = Qd.InsetRect(self._bounds, -4, -4)
173 Qd.EraseRect(old)
Jack Jansen73023402001-01-23 14:58:20 +0000174 self.GetWindow().InvalWindowRect(old)
175 self.GetWindow().InvalWindowRect(new)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000176 ControlWidget.adjust(self, oldbounds)
177
178
Just van Rossumf376ef02001-11-18 14:12:43 +0000179class BevelButton(Button):
180 procID = Controls.kControlBevelButtonNormalBevelProc | Controls.useWFont
181
182
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000183class CheckBox(ControlWidget):
184
185 """Standard checkbox."""
186
187 def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
188 procID = Controls.checkBoxProc | Controls.useWFont
189 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
190
191 def click(self, point, modifiers):
192 if not self._enabled:
193 return
194 part = self._control.TrackControl(point)
195 if part:
196 self.toggle()
197 if self._callback:
198 Wbase.CallbackCall(self._callback, 0, self.get())
199
200 def push(self):
201 if not self._enabled:
202 return
203 self.toggle()
204 if self._callback:
205 Wbase.CallbackCall(self._callback, 0, self.get())
206
207 def toggle(self):
208 self.set(not self.get())
Just van Rossum2dd4d162001-11-02 22:51:42 +0000209
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000210
211class RadioButton(ControlWidget):
212
213 """Standard radiobutton."""
214
215 # XXX We need a radiogroup widget; this is too kludgy.
216
217 def __init__(self, possize, title, thebuttons, callback = None, value = 0):
218 procID = Controls.radioButProc | Controls.useWFont
219 ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
220 self.thebuttons = thebuttons
221 thebuttons.append(self)
222
223 def close(self):
224 self.thebuttons = None
225 ControlWidget.close(self)
226
227 def click(self, point, modifiers):
228 if not self._enabled:
229 return
230 part = self._control.TrackControl(point)
231 if part:
232 self.set(1)
233 if self._callback:
234 Wbase.CallbackCall(self._callback, 0, 1)
235
236 def push(self):
237 if not self._enabled:
238 return
239 self.set(1)
240 if self._callback:
241 Wbase.CallbackCall(self._callback, 0, 1)
242
243 def set(self, value):
244 for button in self.thebuttons:
245 if button._control:
246 button._control.SetControlValue(button == self)
247 else:
248 button._value = (button == self)
Just van Rossum2dd4d162001-11-02 22:51:42 +0000249
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000250
251class Scrollbar(ControlWidget):
252
253 """Standard scrollbar."""
254
255 def __init__(self, possize, callback = None, value = 0, min = 0, max = 0):
256 procID = Controls.scrollBarProc
257 ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
258
259 # interface
Just van Rossum2dd4d162001-11-02 22:51:42 +0000260# def set(self, value):
261# if self._callback:
262# Wbase.CallbackCall(self._callback, 1, value)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000263
264 def up(self):
265 if self._callback:
266 Wbase.CallbackCall(self._callback, 1, '+')
267
268 def down(self):
269 if self._callback:
270 Wbase.CallbackCall(self._callback, 1, '-')
271
272 def pageup(self):
273 if self._callback:
274 Wbase.CallbackCall(self._callback, 1, '++')
275
276 def pagedown(self):
277 if self._callback:
278 Wbase.CallbackCall(self._callback, 1, '--')
279
280 def setmin(self, min):
Just van Rossumf376ef02001-11-18 14:12:43 +0000281 if self._control is not None:
282 self._control.SetControl32BitMinimum(min)
283 else:
284 self._min = min
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000285
Just van Rossum2dd4d162001-11-02 22:51:42 +0000286 def setmax(self, max):
Just van Rossumf376ef02001-11-18 14:12:43 +0000287 if self._control is not None:
288 self._control.SetControl32BitMaximum(max)
289 else:
290 self._max = max
Just van Rossum2dd4d162001-11-02 22:51:42 +0000291
Just van Rossumf376ef02001-11-18 14:12:43 +0000292 def setviewsize(self, viewsize):
293 if self._control is not None:
294 self._control.SetControlViewSize(viewsize)
295 else:
296 self._viewsize = viewsize
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000297
298 def getmin(self):
Just van Rossum2dd4d162001-11-02 22:51:42 +0000299 return self._control.GetControl32BitMinimum()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000300
301 def getmax(self):
Just van Rossum2dd4d162001-11-02 22:51:42 +0000302 return self._control.GetControl32BitMaximum()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000303
304 # internals
305 def click(self, point, modifiers):
306 if not self._enabled:
307 return
308 # custom TrackControl. A mousedown in a scrollbar arrow or page area should
309 # generate _control hits as long as the mouse is a) down, b) still in the same part
310 part = self._control.TestControl(point)
311 if Controls.inUpButton <= part <= Controls.inPageDown:
312 self._control.HiliteControl(part)
313 self._hit(part)
314 oldpart = part
Just van Rossumeb67a7b1999-04-21 09:24:02 +0000315 # slight delay before scrolling at top speed...
316 now = Evt.TickCount()
317 while Evt.StillDown():
318 if (Evt.TickCount() - now) > 18: # 0.3 seconds
319 break
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000320 while Evt.StillDown():
321 part = self._control.TestControl(point)
322 if part == oldpart:
323 self._control.HiliteControl(part)
324 self._hit(part)
325 else:
326 self._control.HiliteControl(0)
327 self.SetPort()
328 point = Evt.GetMouse()
329 self._control.HiliteControl(0)
330 elif part == Controls.inThumb:
331 part = self._control.TrackControl(point)
332 if part:
333 self._hit(part)
334
335 def _hit(self, part):
336 if part == Controls.inThumb:
Just van Rossum2dd4d162001-11-02 22:51:42 +0000337 value = self._control.GetControl32BitValue()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000338 elif part == Controls.inUpButton:
339 value = "+"
340 elif part == Controls.inDownButton:
341 value = "-"
342 elif part == Controls.inPageUp:
343 value = "++"
344 elif part == Controls.inPageDown:
345 value = "--"
346 if self._callback:
347 Wbase.CallbackCall(self._callback, 1, value)
348
349 def draw(self, visRgn = None):
350 if self._visible:
351 self._control.Draw1Control()
Just van Rossumf376ef02001-11-18 14:12:43 +0000352 #Qd.FrameRect(self._bounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000353
354 def adjust(self, oldbounds):
355 self.SetPort()
Jack Jansen73023402001-01-23 14:58:20 +0000356 self.GetWindow().InvalWindowRect(oldbounds)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000357 self._control.HideControl()
358 self._control.MoveControl(self._bounds[0], self._bounds[1])
359 self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
360 if self._visible:
361 Qd.EraseRect(self._bounds)
362 if self._activated:
363 self._control.ShowControl()
364 else:
365 Qd.FrameRect(self._bounds)
Jack Jansen73023402001-01-23 14:58:20 +0000366 self.GetWindow().ValidWindowRect(self._bounds)
Just van Rossum2dd4d162001-11-02 22:51:42 +0000367
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000368
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000369def _scalebarvalue(absmin, absmax, curmin, curmax):
370 if curmin <= absmin and curmax >= absmax:
371 return None
372 if curmin <= absmin:
373 return 0
374 if curmax >= absmax:
375 return 32767
376 perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
377 return int(perc*32767)
378