Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame^] | 1 | import Ctl |
| 2 | import Controls |
| 3 | import Win |
| 4 | import Wbase |
| 5 | import Qd |
| 6 | import Evt |
| 7 | |
| 8 | class ControlWidget(Wbase.ClickableWidget): |
| 9 | |
| 10 | """Baseclass for all native controls.""" |
| 11 | |
| 12 | def __init__(self, possize, title = "Control", procID = 0, callback = None, value = 0, min = 0, max = 1): |
| 13 | Wbase.ClickableWidget.__init__(self, possize) |
| 14 | self._control = None |
| 15 | self._title = title |
| 16 | self._callback = callback |
| 17 | self._procID = procID |
| 18 | self._value = value |
| 19 | self._min = min |
| 20 | self._max = max |
| 21 | self._enabled = 1 |
| 22 | |
| 23 | def open(self): |
| 24 | self._calcbounds() |
| 25 | self._control = Ctl.NewControl(self._parentwindow.wid, |
| 26 | self._bounds, |
| 27 | self._title, |
| 28 | 1, |
| 29 | self._value, |
| 30 | self._min, |
| 31 | self._max, |
| 32 | self._procID, |
| 33 | 0) |
| 34 | self.SetPort() |
| 35 | #Win.ValidRect(self._bounds) |
| 36 | self.enable(self._enabled) |
| 37 | |
| 38 | def adjust(self, oldbounds): |
| 39 | self.SetPort() |
| 40 | self._control.HideControl() |
| 41 | self._control.MoveControl(self._bounds[0], self._bounds[1]) |
| 42 | self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1]) |
| 43 | if self._visible: |
| 44 | Qd.EraseRect(self._bounds) |
| 45 | self._control.ShowControl() |
| 46 | Win.ValidRect(self._bounds) |
| 47 | |
| 48 | def close(self): |
| 49 | self._control.HideControl() |
| 50 | self._control = None |
| 51 | Wbase.ClickableWidget.close(self) |
| 52 | |
| 53 | def enable(self, onoff): |
| 54 | if self._control and self._enabled <> onoff: |
| 55 | self._control.HiliteControl((not onoff) and 255) |
| 56 | self._enabled = onoff |
| 57 | |
| 58 | def show(self, onoff): |
| 59 | self._visible = onoff |
| 60 | for w in self._widgets: |
| 61 | w.show(onoff) |
| 62 | if onoff: |
| 63 | self._control.ShowControl() |
| 64 | else: |
| 65 | self._control.HideControl() |
| 66 | |
| 67 | def activate(self, onoff): |
| 68 | self._activated = onoff |
| 69 | if self._enabled: |
| 70 | self._control.HiliteControl((not onoff) and 255) |
| 71 | |
| 72 | def draw(self, visRgn = None): |
| 73 | if self._visible: |
| 74 | self._control.Draw1Control() |
| 75 | |
| 76 | def test(self, point): |
| 77 | ctltype, control = Ctl.FindControl(point, self._parentwindow.wid) |
| 78 | if self._enabled and control == self._control: |
| 79 | return 1 |
| 80 | |
| 81 | def click(self, point, modifiers): |
| 82 | if not self._enabled: |
| 83 | return |
| 84 | part = self._control.TrackControl(point) |
| 85 | if part: |
| 86 | if self._callback: |
| 87 | Wbase.CallbackCall(self._callback, 0) |
| 88 | |
| 89 | def settitle(self, title): |
| 90 | if self._control: |
| 91 | self._control.SetControlTitle(title) |
| 92 | self._title = title |
| 93 | |
| 94 | def gettitle(self): |
| 95 | return self._title |
| 96 | |
| 97 | class Button(ControlWidget): |
| 98 | |
| 99 | """Standard push button.""" |
| 100 | |
| 101 | def __init__(self, possize, title = "Button", callback = None): |
| 102 | procID = Controls.pushButProc | Controls.useWFont |
| 103 | ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1) |
| 104 | self._isdefault = 0 |
| 105 | |
| 106 | def push(self): |
| 107 | if not self._enabled: |
| 108 | return |
| 109 | import time |
| 110 | self._control.HiliteControl(1) |
| 111 | time.sleep(0.1) |
| 112 | self._control.HiliteControl(0) |
| 113 | if self._callback: |
| 114 | Wbase.CallbackCall(self._callback, 0) |
| 115 | |
| 116 | def enable(self, onoff): |
| 117 | if self._control and self._enabled <> onoff: |
| 118 | self._control.HiliteControl((not onoff) and 255) |
| 119 | self._enabled = onoff |
| 120 | if self._isdefault and self._visible: |
| 121 | self.SetPort() |
| 122 | self.drawfatframe(onoff) |
| 123 | |
| 124 | def activate(self, onoff): |
| 125 | self._activated = onoff |
| 126 | if self._enabled: |
| 127 | self._control.HiliteControl((not onoff) and 255) |
| 128 | if self._isdefault and self._visible: |
| 129 | self.SetPort() |
| 130 | self.drawfatframe(onoff) |
| 131 | |
| 132 | def show(self, onoff): |
| 133 | ControlWidget.show(self, onoff) |
| 134 | if self._isdefault: |
| 135 | self.drawfatframe(onoff and self._enabled) |
| 136 | |
| 137 | def draw(self, visRgn = None): |
| 138 | if self._visible: |
| 139 | self._control.Draw1Control() |
| 140 | if self._isdefault and self._activated: |
| 141 | self.drawfatframe(self._enabled) |
| 142 | |
| 143 | def drawfatframe(self, onoff): |
| 144 | state = Qd.GetPenState() |
| 145 | if onoff: |
| 146 | Qd.PenPat(Qd.qd.black) |
| 147 | else: |
| 148 | Qd.PenPat(Qd.qd.white) |
| 149 | fatrect = Qd.InsetRect(self._bounds, -4, -4) |
| 150 | Qd.PenSize(3, 3) |
| 151 | Qd.FrameRoundRect(fatrect, 16, 16) |
| 152 | Qd.SetPenState(state) |
| 153 | |
| 154 | def _setdefault(self, onoff): |
| 155 | self._isdefault = onoff |
| 156 | if self._control and self._enabled: |
| 157 | self.SetPort() |
| 158 | self.drawfatframe(onoff) |
| 159 | |
| 160 | def adjust(self, oldbounds): |
| 161 | if self._isdefault: |
| 162 | old = Qd.InsetRect(oldbounds, -4, -4) |
| 163 | new = Qd.InsetRect(self._bounds, -4, -4) |
| 164 | Qd.EraseRect(old) |
| 165 | Win.InvalRect(old) |
| 166 | Win.InvalRect(new) |
| 167 | ControlWidget.adjust(self, oldbounds) |
| 168 | |
| 169 | |
| 170 | class CheckBox(ControlWidget): |
| 171 | |
| 172 | """Standard checkbox.""" |
| 173 | |
| 174 | def __init__(self, possize, title = "Checkbox", callback = None, value = 0): |
| 175 | procID = Controls.checkBoxProc | Controls.useWFont |
| 176 | ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1) |
| 177 | |
| 178 | def click(self, point, modifiers): |
| 179 | if not self._enabled: |
| 180 | return |
| 181 | part = self._control.TrackControl(point) |
| 182 | if part: |
| 183 | self.toggle() |
| 184 | if self._callback: |
| 185 | Wbase.CallbackCall(self._callback, 0, self.get()) |
| 186 | |
| 187 | def push(self): |
| 188 | if not self._enabled: |
| 189 | return |
| 190 | self.toggle() |
| 191 | if self._callback: |
| 192 | Wbase.CallbackCall(self._callback, 0, self.get()) |
| 193 | |
| 194 | def toggle(self): |
| 195 | self.set(not self.get()) |
| 196 | |
| 197 | def set(self, value): |
| 198 | if self._control: |
| 199 | self._control.SetControlValue(value) |
| 200 | else: |
| 201 | self._value = value |
| 202 | |
| 203 | def get(self): |
| 204 | if self._control: |
| 205 | return self._control.GetControlValue() |
| 206 | else: |
| 207 | return self._value |
| 208 | |
| 209 | |
| 210 | class RadioButton(ControlWidget): |
| 211 | |
| 212 | """Standard radiobutton.""" |
| 213 | |
| 214 | # XXX We need a radiogroup widget; this is too kludgy. |
| 215 | |
| 216 | def __init__(self, possize, title, thebuttons, callback = None, value = 0): |
| 217 | procID = Controls.radioButProc | Controls.useWFont |
| 218 | ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1) |
| 219 | self.thebuttons = thebuttons |
| 220 | thebuttons.append(self) |
| 221 | |
| 222 | def close(self): |
| 223 | self.thebuttons = None |
| 224 | ControlWidget.close(self) |
| 225 | |
| 226 | def click(self, point, modifiers): |
| 227 | if not self._enabled: |
| 228 | return |
| 229 | part = self._control.TrackControl(point) |
| 230 | if part: |
| 231 | self.set(1) |
| 232 | if self._callback: |
| 233 | Wbase.CallbackCall(self._callback, 0, 1) |
| 234 | |
| 235 | def push(self): |
| 236 | if not self._enabled: |
| 237 | return |
| 238 | self.set(1) |
| 239 | if self._callback: |
| 240 | Wbase.CallbackCall(self._callback, 0, 1) |
| 241 | |
| 242 | def set(self, value): |
| 243 | for button in self.thebuttons: |
| 244 | if button._control: |
| 245 | button._control.SetControlValue(button == self) |
| 246 | else: |
| 247 | button._value = (button == self) |
| 248 | |
| 249 | def get(self): |
| 250 | if self._control: |
| 251 | return self._control.GetControlValue() |
| 252 | else: |
| 253 | return self._value |
| 254 | |
| 255 | |
| 256 | class Scrollbar(ControlWidget): |
| 257 | |
| 258 | """Standard scrollbar.""" |
| 259 | |
| 260 | def __init__(self, possize, callback = None, value = 0, min = 0, max = 0): |
| 261 | procID = Controls.scrollBarProc |
| 262 | ControlWidget.__init__(self, possize, "", procID, callback, value, min, max) |
| 263 | |
| 264 | # interface |
| 265 | def set(self, value): |
| 266 | if self._callback: |
| 267 | Wbase.CallbackCall(self._callback, 1, value) |
| 268 | |
| 269 | def up(self): |
| 270 | if self._callback: |
| 271 | Wbase.CallbackCall(self._callback, 1, '+') |
| 272 | |
| 273 | def down(self): |
| 274 | if self._callback: |
| 275 | Wbase.CallbackCall(self._callback, 1, '-') |
| 276 | |
| 277 | def pageup(self): |
| 278 | if self._callback: |
| 279 | Wbase.CallbackCall(self._callback, 1, '++') |
| 280 | |
| 281 | def pagedown(self): |
| 282 | if self._callback: |
| 283 | Wbase.CallbackCall(self._callback, 1, '--') |
| 284 | |
| 285 | def setmin(self, min): |
| 286 | self._control.SetControlMinimum(min) |
| 287 | |
| 288 | def setmax(self, min): |
| 289 | self._control.SetControlMinimum(max) |
| 290 | |
| 291 | def getmin(self): |
| 292 | return self._control.GetControlMinimum() |
| 293 | |
| 294 | def getmax(self): |
| 295 | return self._control.GetControlMinimum() |
| 296 | |
| 297 | # internals |
| 298 | def click(self, point, modifiers): |
| 299 | if not self._enabled: |
| 300 | return |
| 301 | # custom TrackControl. A mousedown in a scrollbar arrow or page area should |
| 302 | # generate _control hits as long as the mouse is a) down, b) still in the same part |
| 303 | part = self._control.TestControl(point) |
| 304 | if Controls.inUpButton <= part <= Controls.inPageDown: |
| 305 | self._control.HiliteControl(part) |
| 306 | self._hit(part) |
| 307 | oldpart = part |
| 308 | while Evt.StillDown(): |
| 309 | part = self._control.TestControl(point) |
| 310 | if part == oldpart: |
| 311 | self._control.HiliteControl(part) |
| 312 | self._hit(part) |
| 313 | else: |
| 314 | self._control.HiliteControl(0) |
| 315 | self.SetPort() |
| 316 | point = Evt.GetMouse() |
| 317 | self._control.HiliteControl(0) |
| 318 | elif part == Controls.inThumb: |
| 319 | part = self._control.TrackControl(point) |
| 320 | if part: |
| 321 | self._hit(part) |
| 322 | |
| 323 | def _hit(self, part): |
| 324 | if part == Controls.inThumb: |
| 325 | value = self._control.GetControlValue() |
| 326 | elif part == Controls.inUpButton: |
| 327 | value = "+" |
| 328 | elif part == Controls.inDownButton: |
| 329 | value = "-" |
| 330 | elif part == Controls.inPageUp: |
| 331 | value = "++" |
| 332 | elif part == Controls.inPageDown: |
| 333 | value = "--" |
| 334 | if self._callback: |
| 335 | Wbase.CallbackCall(self._callback, 1, value) |
| 336 | |
| 337 | def draw(self, visRgn = None): |
| 338 | if self._visible: |
| 339 | self._control.Draw1Control() |
| 340 | Qd.FrameRect(self._bounds) |
| 341 | |
| 342 | def adjust(self, oldbounds): |
| 343 | self.SetPort() |
| 344 | Win.InvalRect(oldbounds) |
| 345 | self._control.HideControl() |
| 346 | self._control.MoveControl(self._bounds[0], self._bounds[1]) |
| 347 | self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1]) |
| 348 | if self._visible: |
| 349 | Qd.EraseRect(self._bounds) |
| 350 | if self._activated: |
| 351 | self._control.ShowControl() |
| 352 | else: |
| 353 | Qd.FrameRect(self._bounds) |
| 354 | Win.ValidRect(self._bounds) |
| 355 | |
| 356 | def activate(self, onoff): |
| 357 | self._activated = onoff |
| 358 | if self._visible: |
| 359 | if onoff: |
| 360 | self._control.ShowControl() |
| 361 | else: |
| 362 | self._control.HideControl() |
| 363 | self.draw(None) |
| 364 | Win.ValidRect(self._bounds) |
| 365 | |
| 366 | def set(self, value): |
| 367 | if self._control: |
| 368 | self._control.SetControlValue(value) |
| 369 | else: |
| 370 | self._value = value |
| 371 | |
| 372 | def get(self): |
| 373 | if self._control: |
| 374 | return self._control.GetControlValue() |
| 375 | else: |
| 376 | return self._value |
| 377 | |
| 378 | |
| 379 | class __xxxx_PopupControl(ControlWidget): |
| 380 | |
| 381 | def __init__(self, possize, title = "Button", callback = None): |
| 382 | procID = Controls.popupMenuProc # | Controls.useWFont |
| 383 | ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1) |
| 384 | self._isdefault = 0 |
| 385 | |
| 386 | |
| 387 | def _scalebarvalue(absmin, absmax, curmin, curmax): |
| 388 | if curmin <= absmin and curmax >= absmax: |
| 389 | return None |
| 390 | if curmin <= absmin: |
| 391 | return 0 |
| 392 | if curmax >= absmax: |
| 393 | return 32767 |
| 394 | perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin)) |
| 395 | return int(perc*32767) |
| 396 | |