Just van Rossum | f7f9388 | 2001-11-02 19:24:41 +0000 | [diff] [blame] | 1 | from Carbon import Evt, Events, Fm, Fonts |
| 2 | from Carbon import Qd, Res, Scrap |
| 3 | from Carbon import TE, TextEdit, Win |
Just van Rossum | f376ef0 | 2001-11-18 14:12:43 +0000 | [diff] [blame] | 4 | from Carbon import App |
| 5 | from Carbon.Appearance import kThemeStateActive, kThemeStateInactive |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 6 | import waste |
| 7 | import WASTEconst |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 8 | import Wbase |
| 9 | import Wkeys |
| 10 | import Wcontrols |
| 11 | import PyFontify |
Just van Rossum | f7f9388 | 2001-11-02 19:24:41 +0000 | [diff] [blame] | 12 | import string |
| 13 | from types import TupleType, StringType |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | class TextBox(Wbase.Widget): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 17 | |
| 18 | """A static text widget""" |
| 19 | |
| 20 | def __init__(self, possize, text="", align=TextEdit.teJustLeft, |
| 21 | fontsettings=None, |
| 22 | backgroundcolor=(0xffff, 0xffff, 0xffff) |
| 23 | ): |
| 24 | if fontsettings is None: |
| 25 | import W |
| 26 | fontsettings = W.getdefaultfont() |
| 27 | Wbase.Widget.__init__(self, possize) |
| 28 | self.fontsettings = fontsettings |
| 29 | self.text = text |
| 30 | self.align = align |
| 31 | self._backgroundcolor = backgroundcolor |
| 32 | |
| 33 | def draw(self, visRgn = None): |
| 34 | if self._visible: |
| 35 | (font, style, size, color) = self.fontsettings |
| 36 | fontid = GetFNum(font) |
| 37 | savestate = Qd.GetPenState() |
| 38 | Qd.TextFont(fontid) |
| 39 | Qd.TextFace(style) |
| 40 | Qd.TextSize(size) |
| 41 | Qd.RGBForeColor(color) |
| 42 | Qd.RGBBackColor(self._backgroundcolor) |
| 43 | TE.TETextBox(self.text, self._bounds, self.align) |
| 44 | Qd.RGBBackColor((0xffff, 0xffff, 0xffff)) |
| 45 | Qd.SetPenState(savestate) |
| 46 | |
| 47 | def get(self): |
| 48 | return self.text |
| 49 | |
| 50 | def set(self, text): |
| 51 | self.text = text |
| 52 | if self._parentwindow and self._parentwindow.wid: |
| 53 | self.SetPort() |
| 54 | self.draw() |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | class _ScrollWidget: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 58 | |
| 59 | # to be overridden |
| 60 | def getscrollrects(self): |
| 61 | """Return (destrect, viewrect).""" |
| 62 | return None, None |
| 63 | |
| 64 | # internal method |
| 65 | |
| 66 | def updatescrollbars(self): |
| 67 | (dl, dt, dr, db), (vl, vt, vr, vb) = self.getscrollrects() |
| 68 | if self._parent._barx: |
| 69 | viewwidth = vr - vl |
| 70 | destwidth = dr - dl |
| 71 | bar = self._parent._barx |
| 72 | bar.setmax(destwidth - viewwidth) |
| 73 | |
| 74 | # MacOS 8.1 doesn't automatically disable |
| 75 | # scrollbars whose max <= min |
| 76 | bar.enable(destwidth > viewwidth) |
| 77 | |
| 78 | bar.setviewsize(viewwidth) |
| 79 | bar.set(vl - dl) |
| 80 | if self._parent._bary: |
| 81 | viewheight = vb - vt |
| 82 | destheight = db - dt |
| 83 | bar = self._parent._bary |
| 84 | bar.setmax(destheight - viewheight) |
| 85 | |
| 86 | # MacOS 8.1 doesn't automatically disable |
| 87 | # scrollbars whose max <= min |
| 88 | bar.enable(destheight > viewheight) |
| 89 | |
| 90 | bar.setviewsize(viewheight) |
| 91 | bar.set(vt - dt) |
Just van Rossum | f376ef0 | 2001-11-18 14:12:43 +0000 | [diff] [blame] | 92 | |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 93 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 94 | UNDOLABELS = [ # Indexed by WEGetUndoInfo() value |
| 95 | None, "", "typing", "Cut", "Paste", "Clear", "Drag", "Style", |
| 96 | "Ruler", "backspace", "delete", "transform", "resize"] |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | class EditText(Wbase.SelectableWidget, _ScrollWidget): |
Jack Jansen | b6b6c6c | 2001-12-04 13:30:29 +0000 | [diff] [blame] | 100 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 101 | """A text edit widget, mainly for simple entry fields.""" |
Jack Jansen | b6b6c6c | 2001-12-04 13:30:29 +0000 | [diff] [blame] | 102 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 103 | def __init__(self, possize, text="", |
| 104 | callback=None, inset=(3, 3), |
| 105 | fontsettings=None, |
| 106 | tabsettings = (32, 0), |
| 107 | readonly = 0): |
| 108 | if fontsettings is None: |
| 109 | import W |
| 110 | fontsettings = W.getdefaultfont() |
| 111 | Wbase.SelectableWidget.__init__(self, possize) |
| 112 | self.temptext = text |
| 113 | self.ted = None |
| 114 | self.selection = None |
| 115 | self.oldselection = None |
| 116 | self._callback = callback |
| 117 | self.changed = 0 |
| 118 | self.selchanged = 0 |
| 119 | self._selected = 0 |
| 120 | self._enabled = 1 |
| 121 | self.wrap = 1 |
| 122 | self.readonly = readonly |
| 123 | self.fontsettings = fontsettings |
| 124 | self.tabsettings = tabsettings |
| 125 | if type(inset) <> TupleType: |
| 126 | self.inset = (inset, inset) |
| 127 | else: |
| 128 | self.inset = inset |
| 129 | |
| 130 | def open(self): |
| 131 | if not hasattr(self._parent, "_barx"): |
| 132 | self._parent._barx = None |
| 133 | if not hasattr(self._parent, "_bary"): |
| 134 | self._parent._bary = None |
| 135 | self._calcbounds() |
| 136 | self.SetPort() |
| 137 | viewrect, destrect = self._calctextbounds() |
| 138 | flags = self._getflags() |
| 139 | self.ted = waste.WENew(destrect, viewrect, flags) |
| 140 | self.ted.WEInstallTabHooks() |
| 141 | self.ted.WESetAlignment(WASTEconst.weFlushLeft) |
| 142 | self.setfontsettings(self.fontsettings) |
| 143 | self.settabsettings(self.tabsettings) |
| 144 | self.ted.WEUseText(Res.Resource(self.temptext)) |
| 145 | self.ted.WECalText() |
| 146 | if self.selection: |
| 147 | self.setselection(self.selection[0], self.selection[1]) |
| 148 | self.selection = None |
| 149 | else: |
| 150 | self.selview() |
| 151 | self.temptext = None |
| 152 | self.updatescrollbars() |
| 153 | self.bind("pageup", self.scrollpageup) |
| 154 | self.bind("pagedown", self.scrollpagedown) |
| 155 | self.bind("top", self.scrolltop) |
| 156 | self.bind("bottom", self.scrollbottom) |
| 157 | self.selchanged = 0 |
| 158 | |
| 159 | def close(self): |
| 160 | self._parent._barx = None |
| 161 | self._parent._bary = None |
| 162 | self.ted = None |
| 163 | self.temptext = None |
| 164 | Wbase.SelectableWidget.close(self) |
| 165 | |
| 166 | def textchanged(self, all=0): |
| 167 | self.changed = 1 |
| 168 | |
| 169 | def selectionchanged(self): |
| 170 | self.selchanged = 1 |
| 171 | self.oldselection = self.getselection() |
| 172 | |
| 173 | def gettabsettings(self): |
| 174 | return self.tabsettings |
| 175 | |
| 176 | def settabsettings(self, (tabsize, tabmode)): |
| 177 | self.tabsettings = (tabsize, tabmode) |
| 178 | if hasattr(self.ted, "WESetTabSize"): |
| 179 | port = self._parentwindow.wid.GetWindowPort() |
| 180 | if tabmode: |
| 181 | (font, style, size, color) = self.getfontsettings() |
| 182 | savesettings = GetPortFontSettings(port) |
| 183 | SetPortFontSettings(port, (font, style, size)) |
| 184 | tabsize = Qd.StringWidth(' ' * tabsize) |
| 185 | SetPortFontSettings(port, savesettings) |
| 186 | tabsize = max(tabsize, 1) |
| 187 | self.ted.WESetTabSize(tabsize) |
| 188 | self.SetPort() |
| 189 | Qd.EraseRect(self.ted.WEGetViewRect()) |
| 190 | self.ted.WEUpdate(port.visRgn) |
| 191 | |
| 192 | def getfontsettings(self): |
| 193 | from Carbon import Res |
| 194 | (font, style, size, color) = self.ted.WEGetRunInfo(0)[4] |
| 195 | font = Fm.GetFontName(font) |
| 196 | return (font, style, size, color) |
| 197 | |
| 198 | def setfontsettings(self, (font, style, size, color)): |
| 199 | self.SetPort() |
| 200 | if type(font) <> StringType: |
| 201 | font = Fm.GetFontName(font) |
| 202 | self.fontsettings = (font, style, size, color) |
| 203 | fontid = GetFNum(font) |
| 204 | readonly = self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, -1) |
| 205 | if readonly: |
| 206 | self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 0) |
| 207 | try: |
| 208 | self.ted.WEFeatureFlag(WASTEconst.weFInhibitRecal, 1) |
| 209 | selstart, selend = self.ted.WEGetSelection() |
| 210 | self.ted.WESetSelection(0, self.ted.WEGetTextLength()) |
| 211 | self.ted.WESetStyle(WASTEconst.weDoFace, (0, 0, 0, (0, 0, 0))) |
| 212 | self.ted.WESetStyle(WASTEconst.weDoFace | |
| 213 | WASTEconst.weDoColor | |
| 214 | WASTEconst.weDoFont | |
| 215 | WASTEconst.weDoSize, |
| 216 | (fontid, style, size, color)) |
| 217 | self.ted.WEFeatureFlag(WASTEconst.weFInhibitRecal, 0) |
| 218 | self.ted.WECalText() |
| 219 | self.ted.WESetSelection(selstart, selend) |
| 220 | finally: |
| 221 | if readonly: |
| 222 | self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 1) |
| 223 | viewrect = self.ted.WEGetViewRect() |
| 224 | Qd.EraseRect(viewrect) |
| 225 | self.ted.WEUpdate(self._parentwindow.wid.GetWindowPort().visRgn) |
| 226 | self.selectionchanged() |
| 227 | self.updatescrollbars() |
| 228 | |
| 229 | def adjust(self, oldbounds): |
| 230 | self.SetPort() |
| 231 | # Note: if App.DrawThemeEditTextFrame is ever used, it will be necessary |
| 232 | # to unconditionally outset the invalidated rectangles, since Appearance |
| 233 | # frames are drawn outside the bounds. |
| 234 | if self._selected and self._parentwindow._hasselframes: |
| 235 | self.GetWindow().InvalWindowRect(Qd.InsetRect(oldbounds, -3, -3)) |
| 236 | self.GetWindow().InvalWindowRect(Qd.InsetRect(self._bounds, -3, -3)) |
| 237 | else: |
| 238 | self.GetWindow().InvalWindowRect(oldbounds) |
| 239 | self.GetWindow().InvalWindowRect(self._bounds) |
| 240 | viewrect, destrect = self._calctextbounds() |
| 241 | self.ted.WESetViewRect(viewrect) |
| 242 | self.ted.WESetDestRect(destrect) |
| 243 | if self.wrap: |
| 244 | self.ted.WECalText() |
| 245 | if self.ted.WEGetDestRect()[3] < viewrect[1]: |
| 246 | self.selview() |
| 247 | self.updatescrollbars() |
| 248 | |
| 249 | # interface ----------------------- |
| 250 | # selection stuff |
| 251 | def selview(self): |
| 252 | self.ted.WESelView() |
| 253 | |
| 254 | def selectall(self): |
| 255 | self.ted.WESetSelection(0, self.ted.WEGetTextLength()) |
| 256 | self.selectionchanged() |
| 257 | self.updatescrollbars() |
| 258 | |
| 259 | def selectline(self, lineno, charoffset = 0): |
| 260 | newselstart, newselend = self.ted.WEGetLineRange(lineno) |
| 261 | # Autoscroll makes the *end* of the selection visible, which, |
| 262 | # in the case of a whole line, is the beginning of the *next* line. |
| 263 | # So sometimes it leaves our line just above the view rect. |
| 264 | # Let's fool Waste by initially selecting one char less: |
| 265 | self.ted.WESetSelection(newselstart + charoffset, newselend-1) |
| 266 | self.ted.WESetSelection(newselstart + charoffset, newselend) |
| 267 | self.selectionchanged() |
| 268 | self.updatescrollbars() |
| 269 | |
| 270 | def getselection(self): |
| 271 | if self.ted: |
| 272 | return self.ted.WEGetSelection() |
| 273 | else: |
| 274 | return self.selection |
| 275 | |
| 276 | def setselection(self, selstart, selend): |
| 277 | self.selectionchanged() |
| 278 | if self.ted: |
| 279 | self.ted.WESetSelection(selstart, selend) |
| 280 | self.ted.WESelView() |
| 281 | self.updatescrollbars() |
| 282 | else: |
| 283 | self.selection = selstart, selend |
| 284 | |
| 285 | def offsettoline(self, offset): |
| 286 | return self.ted.WEOffsetToLine(offset) |
| 287 | |
| 288 | def countlines(self): |
| 289 | return self.ted.WECountLines() |
| 290 | |
| 291 | def getselectedtext(self): |
| 292 | selstart, selend = self.ted.WEGetSelection() |
| 293 | return self.ted.WEGetText().data[selstart:selend] |
| 294 | |
| 295 | def expandselection(self): |
| 296 | oldselstart, oldselend = self.ted.WEGetSelection() |
| 297 | selstart, selend = min(oldselstart, oldselend), max(oldselstart, oldselend) |
| 298 | if selstart <> selend and chr(self.ted.WEGetChar(selend-1)) == '\r': |
| 299 | selend = selend - 1 |
| 300 | newselstart, dummy = self.ted.WEFindLine(selstart, 1) |
| 301 | dummy, newselend = self.ted.WEFindLine(selend, 1) |
| 302 | if oldselstart <> newselstart or oldselend <> newselend: |
| 303 | self.ted.WESetSelection(newselstart, newselend) |
| 304 | self.updatescrollbars() |
| 305 | self.selectionchanged() |
| 306 | |
| 307 | def insert(self, text): |
| 308 | self.ted.WEInsert(text, None, None) |
| 309 | self.textchanged() |
| 310 | self.selectionchanged() |
| 311 | |
| 312 | # text |
| 313 | def set(self, text): |
| 314 | if not self.ted: |
| 315 | self.temptext = text |
| 316 | else: |
| 317 | self.ted.WEUseText(Res.Resource(text)) |
| 318 | self.ted.WECalText() |
| 319 | self.SetPort() |
| 320 | viewrect, destrect = self._calctextbounds() |
| 321 | self.ted.WESetViewRect(viewrect) |
| 322 | self.ted.WESetDestRect(destrect) |
| 323 | rgn = Qd.NewRgn() |
| 324 | Qd.RectRgn(rgn, viewrect) |
| 325 | Qd.EraseRect(viewrect) |
| 326 | self.draw(rgn) |
| 327 | self.updatescrollbars() |
| 328 | self.textchanged(1) |
| 329 | |
| 330 | def get(self): |
| 331 | if not self._parent: |
| 332 | return self.temptext |
| 333 | else: |
| 334 | return self.ted.WEGetText().data |
| 335 | |
| 336 | # events |
| 337 | def key(self, char, event): |
| 338 | (what, message, when, where, modifiers) = event |
| 339 | if self._enabled and not modifiers & Events.cmdKey or char in Wkeys.arrowkeys: |
| 340 | self.ted.WEKey(ord(char), modifiers) |
| 341 | if char not in Wkeys.navigationkeys: |
| 342 | self.textchanged() |
| 343 | if char not in Wkeys.scrollkeys: |
| 344 | self.selectionchanged() |
| 345 | self.updatescrollbars() |
| 346 | if self._callback: |
| 347 | Wbase.CallbackCall(self._callback, 0, char, modifiers) |
| 348 | |
| 349 | def click(self, point, modifiers): |
| 350 | if not self._enabled: |
| 351 | return |
| 352 | self.ted.WEClick(point, modifiers, Evt.TickCount()) |
| 353 | self.selectionchanged() |
| 354 | self.updatescrollbars() |
| 355 | return 1 |
| 356 | |
| 357 | def idle(self): |
| 358 | self.SetPort() |
| 359 | self.ted.WEIdle() |
| 360 | |
| 361 | def rollover(self, point, onoff): |
| 362 | if onoff: |
| 363 | Wbase.SetCursor("iBeam") |
| 364 | |
| 365 | def activate(self, onoff): |
| 366 | self._activated = onoff |
| 367 | if self._visible: |
| 368 | self.SetPort() |
| 369 | |
| 370 | # DISABLED! There are too many places where it is assumed that |
| 371 | # the frame of an EditText item is 1 pixel, inside the bounds. |
| 372 | #state = [kThemeStateActive, kThemeStateInactive][not onoff] |
| 373 | #App.DrawThemeEditTextFrame(Qd.InsetRect(self._bounds, 1, 1), state) |
| 374 | |
| 375 | if self._selected: |
| 376 | if onoff: |
| 377 | self.ted.WEActivate() |
| 378 | else: |
| 379 | self.ted.WEDeactivate() |
| 380 | self.drawselframe(onoff) |
| 381 | |
| 382 | def select(self, onoff, isclick = 0): |
| 383 | if Wbase.SelectableWidget.select(self, onoff): |
| 384 | return |
| 385 | self.SetPort() |
| 386 | if onoff: |
| 387 | self.ted.WEActivate() |
| 388 | if self._parentwindow._tabbable and not isclick: |
| 389 | self.selectall() |
| 390 | else: |
| 391 | self.ted.WEDeactivate() |
| 392 | self.drawselframe(onoff) |
| 393 | |
| 394 | def draw(self, visRgn = None): |
| 395 | if self._visible: |
| 396 | if not visRgn: |
| 397 | visRgn = self._parentwindow.wid.GetWindowPort().visRgn |
| 398 | self.ted.WEUpdate(visRgn) |
| 399 | |
| 400 | # DISABLED! There are too many places where it is assumed that |
| 401 | # the frame of an EditText item is 1 pixel, inside the bounds. |
| 402 | #state = [kThemeStateActive, kThemeStateInactive][not self._activated] |
| 403 | #App.DrawThemeEditTextFrame(Qd.InsetRect(self._bounds, 1, 1), state) |
| 404 | Qd.FrameRect(self._bounds) |
| 405 | |
| 406 | if self._selected and self._activated: |
| 407 | self.drawselframe(1) |
| 408 | |
| 409 | # scrolling |
| 410 | def scrollpageup(self): |
| 411 | if self._parent._bary and self._parent._bary._enabled: |
| 412 | self.vscroll("++") |
| 413 | |
| 414 | def scrollpagedown(self): |
| 415 | if self._parent._bary and self._parent._bary._enabled: |
| 416 | self.vscroll("--") |
| 417 | |
| 418 | def scrolltop(self): |
| 419 | if self._parent._bary and self._parent._bary._enabled: |
| 420 | self.vscroll(self._parent._bary.getmin()) |
| 421 | if self._parent._barx and self._parent._barx._enabled: |
| 422 | self.hscroll(self._parent._barx.getmin()) |
| 423 | |
| 424 | def scrollbottom(self): |
| 425 | if self._parent._bary and self._parent._bary._enabled: |
| 426 | self.vscroll(self._parent._bary.getmax()) |
| 427 | |
| 428 | # menu handlers |
| 429 | def domenu_copy(self, *args): |
| 430 | selbegin, selend = self.ted.WEGetSelection() |
| 431 | if selbegin == selend: |
| 432 | return |
| 433 | if hasattr(Scrap, 'ZeroScrap'): |
| 434 | Scrap.ZeroScrap() |
| 435 | else: |
| 436 | Scrap.ClearCurrentScrap() |
| 437 | self.ted.WECopy() |
| 438 | self.updatescrollbars() |
| 439 | |
| 440 | def domenu_cut(self, *args): |
| 441 | selbegin, selend = self.ted.WEGetSelection() |
| 442 | if selbegin == selend: |
| 443 | return |
| 444 | if hasattr(Scrap, 'ZeroScrap'): |
| 445 | Scrap.ZeroScrap() |
| 446 | else: |
| 447 | Scrap.ClearCurrentScrap() |
| 448 | self.ted.WECut() |
| 449 | self.updatescrollbars() |
| 450 | self.selview() |
| 451 | self.textchanged() |
| 452 | self.selectionchanged() |
| 453 | if self._callback: |
| 454 | Wbase.CallbackCall(self._callback, 0, "", None) |
| 455 | |
| 456 | def domenu_paste(self, *args): |
| 457 | if not self.ted.WECanPaste(): |
| 458 | return |
| 459 | self.selview() |
| 460 | self.ted.WEPaste() |
| 461 | self.updatescrollbars() |
| 462 | self.textchanged() |
| 463 | self.selectionchanged() |
| 464 | if self._callback: |
| 465 | Wbase.CallbackCall(self._callback, 0, "", None) |
| 466 | |
| 467 | def domenu_clear(self, *args): |
| 468 | self.ted.WEDelete() |
| 469 | self.selview() |
| 470 | self.updatescrollbars() |
| 471 | self.textchanged() |
| 472 | self.selectionchanged() |
| 473 | if self._callback: |
| 474 | Wbase.CallbackCall(self._callback, 0, "", None) |
| 475 | |
| 476 | def domenu_undo(self, *args): |
| 477 | which, redo = self.ted.WEGetUndoInfo() |
| 478 | if not which: |
| 479 | return |
| 480 | self.ted.WEUndo() |
| 481 | self.updatescrollbars() |
| 482 | self.textchanged() |
| 483 | self.selectionchanged() |
| 484 | if self._callback: |
| 485 | Wbase.CallbackCall(self._callback, 0, "", None) |
| 486 | |
| 487 | def can_undo(self, menuitem): |
| 488 | #doundo = self.ted.WEFeatureFlag(WASTEconst.weFUndo, -1) |
| 489 | #print doundo |
| 490 | #if not doundo: |
| 491 | # return 0 |
| 492 | which, redo = self.ted.WEGetUndoInfo() |
| 493 | if which < len(UNDOLABELS): |
| 494 | which = UNDOLABELS[which] |
| 495 | else: |
| 496 | which = "" |
| 497 | if which == None: |
| 498 | return None |
| 499 | if redo: |
| 500 | which = "Redo "+which |
| 501 | else: |
| 502 | which = "Undo "+which |
| 503 | menuitem.settext(which) |
| 504 | return 1 |
| 505 | |
| 506 | def domenu_selectall(self, *args): |
| 507 | self.selectall() |
| 508 | |
| 509 | # private |
| 510 | def getscrollrects(self): |
| 511 | return self.ted.WEGetDestRect(), self.ted.WEGetViewRect() |
| 512 | |
| 513 | def vscroll(self, value): |
| 514 | lineheight = self.ted.WEGetHeight(0, 1) |
| 515 | dr = self.ted.WEGetDestRect() |
| 516 | vr = self.ted.WEGetViewRect() |
| 517 | viewheight = vr[3] - vr[1] |
| 518 | maxdelta = vr[1] - dr[1] |
| 519 | mindelta = vr[3] - dr[3] |
| 520 | if value == "+": |
| 521 | delta = lineheight |
| 522 | elif value == "-": |
| 523 | delta = - lineheight |
| 524 | elif value == "++": |
| 525 | delta = viewheight - lineheight |
| 526 | elif value == "--": |
| 527 | delta = lineheight - viewheight |
| 528 | else: # in thumb |
| 529 | delta = vr[1] - dr[1] - value |
| 530 | delta = min(maxdelta, delta) |
| 531 | delta = max(mindelta, delta) |
| 532 | delta = int(delta) |
| 533 | self.ted.WEScroll(0, delta) |
| 534 | self.updatescrollbars() |
| 535 | |
| 536 | def hscroll(self, value): |
| 537 | dr = self.ted.WEGetDestRect() |
| 538 | vr = self.ted.WEGetViewRect() |
| 539 | destwidth = dr[2] - dr[0] |
| 540 | viewwidth = vr[2] - vr[0] |
| 541 | viewoffset = maxdelta = vr[0] - dr[0] |
| 542 | mindelta = vr[2] - dr[2] |
| 543 | if value == "+": |
| 544 | delta = 32 |
| 545 | elif value == "-": |
| 546 | delta = - 32 |
| 547 | elif value == "++": |
| 548 | delta = 0.5 * (vr[2] - vr[0]) |
| 549 | elif value == "--": |
| 550 | delta = 0.5 * (vr[0] - vr[2]) |
| 551 | else: # in thumb |
| 552 | delta = vr[0] - dr[0] - value |
| 553 | #cur = (32767 * viewoffset) / (destwidth - viewwidth) |
| 554 | #delta = (cur-value)*(destwidth - viewwidth)/32767 |
| 555 | #if abs(delta - viewoffset) <=2: |
| 556 | # # compensate for irritating rounding error |
| 557 | # delta = viewoffset |
| 558 | delta = min(maxdelta, delta) |
| 559 | delta = max(mindelta, delta) |
| 560 | delta = int(delta) |
| 561 | self.ted.WEScroll(delta, 0) |
| 562 | self.updatescrollbars() |
| 563 | |
| 564 | # some internals |
| 565 | def _getflags(self): |
| 566 | flags = WASTEconst.weDoAutoScroll | WASTEconst.weDoMonoStyled |
| 567 | if self.readonly: |
| 568 | flags = flags | WASTEconst.weDoReadOnly |
| 569 | else: |
| 570 | flags = flags | WASTEconst.weDoUndo |
| 571 | return flags |
| 572 | |
| 573 | def _getviewrect(self): |
| 574 | return Qd.InsetRect(self._bounds, self.inset[0], self.inset[1]) |
| 575 | |
| 576 | def _calctextbounds(self): |
| 577 | viewrect = l, t, r, b = self._getviewrect() |
| 578 | if self.ted: |
| 579 | dl, dt, dr, db = self.ted.WEGetDestRect() |
| 580 | vl, vt, vr, vb = self.ted.WEGetViewRect() |
| 581 | yshift = t - vt |
| 582 | if (db - dt) < (b - t): |
| 583 | destrect = viewrect |
| 584 | else: |
| 585 | destrect = l, dt + yshift, r, db + yshift |
| 586 | else: |
| 587 | destrect = viewrect |
| 588 | return viewrect, destrect |
| 589 | |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 590 | |
| 591 | class TextEditor(EditText): |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 592 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 593 | """A text edit widget.""" |
| 594 | |
| 595 | def __init__(self, possize, text="", callback=None, wrap=1, inset=(4, 4), |
| 596 | fontsettings=None, |
| 597 | tabsettings=(32, 0), |
| 598 | readonly=0): |
| 599 | EditText.__init__(self, possize, text, callback, inset, fontsettings, tabsettings, readonly) |
| 600 | self.wrap = wrap |
| 601 | |
| 602 | def _getflags(self): |
| 603 | flags = WASTEconst.weDoAutoScroll | WASTEconst.weDoMonoStyled | \ |
| 604 | WASTEconst.weDoOutlineHilite |
| 605 | if self.readonly: |
| 606 | flags = flags | WASTEconst.weDoReadOnly |
| 607 | else: |
| 608 | flags = flags | WASTEconst.weDoUndo |
| 609 | return flags |
| 610 | |
| 611 | def _getviewrect(self): |
| 612 | l, t, r, b = self._bounds |
| 613 | return (l + 5, t + 2, r, b - 2) |
| 614 | |
| 615 | def _calctextbounds(self): |
| 616 | if self.wrap: |
| 617 | return EditText._calctextbounds(self) |
| 618 | else: |
| 619 | viewrect = l, t, r, b = self._getviewrect() |
| 620 | if self.ted: |
| 621 | dl, dt, dr, db = self.ted.WEGetDestRect() |
| 622 | vl, vt, vr, vb = self.ted.WEGetViewRect() |
| 623 | xshift = l - vl |
| 624 | yshift = t - vt |
| 625 | if (db - dt) < (b - t): |
| 626 | yshift = t - dt |
| 627 | destrect = (dl + xshift, dt + yshift, dr + xshift, db + yshift) |
| 628 | else: |
| 629 | destrect = (l, t, r + 5000, b) |
| 630 | return viewrect, destrect |
| 631 | |
| 632 | def draw(self, visRgn = None): |
| 633 | if self._visible: |
| 634 | if not visRgn: |
| 635 | visRgn = self._parentwindow.wid.GetWindowPort().visRgn |
| 636 | self.ted.WEUpdate(visRgn) |
| 637 | if self._selected and self._activated: |
| 638 | self.drawselframe(1) |
| 639 | |
| 640 | def activate(self, onoff): |
| 641 | self._activated = onoff |
| 642 | if self._visible: |
| 643 | self.SetPort() |
| 644 | # doesn't draw frame, as EditText.activate does |
| 645 | if self._selected: |
| 646 | if onoff: |
| 647 | self.ted.WEActivate() |
| 648 | else: |
| 649 | self.ted.WEDeactivate() |
| 650 | self.drawselframe(onoff) |
Jack Jansen | b6b6c6c | 2001-12-04 13:30:29 +0000 | [diff] [blame] | 651 | |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 652 | |
Jack Jansen | 9ad2752 | 2001-02-21 13:54:31 +0000 | [diff] [blame] | 653 | import re |
Just van Rossum | b10eb84 | 2001-06-21 17:51:17 +0000 | [diff] [blame] | 654 | commentPat = re.compile("[ \t]*(#)") |
| 655 | indentPat = re.compile("[ \t]*") |
Just van Rossum | f7f9388 | 2001-11-02 19:24:41 +0000 | [diff] [blame] | 656 | kStringColor = (0, 0x7fff, 0) |
| 657 | kCommentColor = (0, 0, 0xb000) |
Just van Rossum | b10eb84 | 2001-06-21 17:51:17 +0000 | [diff] [blame] | 658 | |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 659 | |
| 660 | class PyEditor(TextEditor): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 661 | |
| 662 | """A specialized Python source edit widget""" |
| 663 | |
| 664 | def __init__(self, possize, text="", callback=None, inset=(4, 4), |
| 665 | fontsettings=None, |
| 666 | tabsettings=(32, 0), |
| 667 | readonly=0, |
| 668 | debugger=None, |
| 669 | file=''): |
| 670 | TextEditor.__init__(self, possize, text, callback, 0, inset, fontsettings, tabsettings, readonly) |
| 671 | self.bind("cmd[", self.domenu_shiftleft) |
| 672 | self.bind("cmd]", self.domenu_shiftright) |
| 673 | self.bind("cmdshift[", self.domenu_uncomment) |
| 674 | self.bind("cmdshift]", self.domenu_comment) |
| 675 | self.bind("cmdshiftd", self.alldirty) |
| 676 | self.file = file # only for debugger reference |
| 677 | self._debugger = debugger |
| 678 | if debugger: |
| 679 | debugger.register_editor(self, self.file) |
| 680 | self._dirty = (0, None) |
| 681 | self.do_fontify = 0 |
| 682 | |
| 683 | #def open(self): |
| 684 | # TextEditor.open(self) |
| 685 | # if self.do_fontify: |
| 686 | # self.fontify() |
| 687 | # self._dirty = (None, None) |
| 688 | |
| 689 | def _getflags(self): |
| 690 | flags = (WASTEconst.weDoDrawOffscreen | WASTEconst.weDoUseTempMem | |
| 691 | WASTEconst.weDoAutoScroll | WASTEconst.weDoOutlineHilite) |
| 692 | if self.readonly: |
| 693 | flags = flags | WASTEconst.weDoReadOnly |
| 694 | else: |
| 695 | flags = flags | WASTEconst.weDoUndo |
| 696 | return flags |
| 697 | |
| 698 | def textchanged(self, all=0): |
| 699 | self.changed = 1 |
| 700 | if all: |
| 701 | self._dirty = (0, None) |
| 702 | return |
| 703 | oldsel = self.oldselection |
| 704 | sel = self.getselection() |
| 705 | if not sel: |
| 706 | # XXX what to do? |
| 707 | return |
| 708 | selstart, selend = sel |
| 709 | selstart, selend = min(selstart, selend), max(selstart, selend) |
| 710 | if oldsel: |
| 711 | oldselstart, oldselend = min(oldsel), max(oldsel) |
| 712 | selstart, selend = min(selstart, oldselstart), max(selend, oldselend) |
| 713 | startline = self.offsettoline(selstart) |
| 714 | endline = self.offsettoline(selend) |
| 715 | selstart, _ = self.ted.WEGetLineRange(startline) |
| 716 | _, selend = self.ted.WEGetLineRange(endline) |
| 717 | if selstart > 0: |
| 718 | selstart = selstart - 1 |
| 719 | self._dirty = (selstart, selend) |
| 720 | |
| 721 | def idle(self): |
| 722 | self.SetPort() |
| 723 | self.ted.WEIdle() |
| 724 | if not self.do_fontify: |
| 725 | return |
| 726 | start, end = self._dirty |
| 727 | if start is None: |
| 728 | return |
| 729 | textLength = self.ted.WEGetTextLength() |
| 730 | if end is None: |
| 731 | end = textLength |
| 732 | if start >= end: |
| 733 | self._dirty = (None, None) |
| 734 | else: |
| 735 | self.fontify(start, end) |
| 736 | self._dirty = (None, None) |
| 737 | |
| 738 | def alldirty(self, *args): |
| 739 | self._dirty = (0, None) |
| 740 | |
| 741 | def fontify(self, start=0, end=None): |
| 742 | #W.SetCursor('watch') |
| 743 | if self.readonly: |
| 744 | self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 0) |
| 745 | self.ted.WEFeatureFlag(WASTEconst.weFOutlineHilite, 0) |
| 746 | self.ted.WEDeactivate() |
| 747 | self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 0) |
| 748 | self.ted.WEFeatureFlag(WASTEconst.weFUndo, 0) |
| 749 | pytext = self.get().replace("\r", "\n") |
| 750 | if end is None: |
| 751 | end = len(pytext) |
| 752 | else: |
| 753 | end = min(end, len(pytext)) |
| 754 | selstart, selend = self.ted.WEGetSelection() |
| 755 | self.ted.WESetSelection(start, end) |
| 756 | self.ted.WESetStyle(WASTEconst.weDoFace | WASTEconst.weDoColor, |
| 757 | (0, 0, 12, (0, 0, 0))) |
| 758 | |
| 759 | tags = PyFontify.fontify(pytext, start, end) |
| 760 | styles = { |
| 761 | 'string': (WASTEconst.weDoColor, (0, 0, 0, kStringColor)), |
| 762 | 'keyword': (WASTEconst.weDoFace, (0, 1, 0, (0, 0, 0))), |
| 763 | 'comment': (WASTEconst.weDoFace | WASTEconst.weDoColor, (0, 0, 0, kCommentColor)), |
| 764 | 'identifier': (WASTEconst.weDoColor, (0, 0, 0, (0xbfff, 0, 0))) |
| 765 | } |
| 766 | setselection = self.ted.WESetSelection |
| 767 | setstyle = self.ted.WESetStyle |
| 768 | for tag, start, end, sublist in tags: |
| 769 | setselection(start, end) |
| 770 | mode, style = styles[tag] |
| 771 | setstyle(mode, style) |
| 772 | self.ted.WESetSelection(selstart, selend) |
| 773 | self.SetPort() |
| 774 | self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 1) |
| 775 | self.ted.WEFeatureFlag(WASTEconst.weFUndo, 1) |
| 776 | self.ted.WEActivate() |
| 777 | self.ted.WEFeatureFlag(WASTEconst.weFOutlineHilite, 1) |
| 778 | if self.readonly: |
| 779 | self.ted.WEFeatureFlag(WASTEconst.weFReadOnly, 1) |
| 780 | |
| 781 | def domenu_shiftleft(self): |
| 782 | self.expandselection() |
| 783 | selstart, selend = self.ted.WEGetSelection() |
| 784 | selstart, selend = min(selstart, selend), max(selstart, selend) |
| 785 | snippet = self.getselectedtext() |
| 786 | lines = string.split(snippet, '\r') |
| 787 | for i in range(len(lines)): |
| 788 | if lines[i][:1] == '\t': |
| 789 | lines[i] = lines[i][1:] |
| 790 | snippet = string.join(lines, '\r') |
| 791 | self.insert(snippet) |
| 792 | self.ted.WESetSelection(selstart, selstart + len(snippet)) |
| 793 | |
| 794 | def domenu_shiftright(self): |
| 795 | self.expandselection() |
| 796 | selstart, selend = self.ted.WEGetSelection() |
| 797 | selstart, selend = min(selstart, selend), max(selstart, selend) |
| 798 | snippet = self.getselectedtext() |
| 799 | lines = string.split(snippet, '\r') |
| 800 | for i in range(len(lines) - (not lines[-1])): |
| 801 | lines[i] = '\t' + lines[i] |
| 802 | snippet = string.join(lines, '\r') |
| 803 | self.insert(snippet) |
| 804 | self.ted.WESetSelection(selstart, selstart + len(snippet)) |
| 805 | |
| 806 | def domenu_uncomment(self): |
| 807 | self.expandselection() |
| 808 | selstart, selend = self.ted.WEGetSelection() |
| 809 | selstart, selend = min(selstart, selend), max(selstart, selend) |
| 810 | snippet = self.getselectedtext() |
| 811 | lines = string.split(snippet, '\r') |
| 812 | for i in range(len(lines)): |
| 813 | m = commentPat.match(lines[i]) |
| 814 | if m: |
| 815 | pos = m.start(1) |
| 816 | lines[i] = lines[i][:pos] + lines[i][pos+1:] |
| 817 | snippet = string.join(lines, '\r') |
| 818 | self.insert(snippet) |
| 819 | self.ted.WESetSelection(selstart, selstart + len(snippet)) |
| 820 | |
| 821 | def domenu_comment(self): |
| 822 | self.expandselection() |
| 823 | selstart, selend = self.ted.WEGetSelection() |
| 824 | selstart, selend = min(selstart, selend), max(selstart, selend) |
| 825 | snippet = self.getselectedtext() |
| 826 | lines = string.split(snippet, '\r') |
| 827 | indent = 3000 # arbitrary large number... |
| 828 | for line in lines: |
| 829 | if string.strip(line): |
| 830 | m = indentPat.match(line) |
| 831 | if m: |
| 832 | indent = min(indent, m.regs[0][1]) |
| 833 | else: |
| 834 | indent = 0 |
| 835 | break |
| 836 | for i in range(len(lines) - (not lines[-1])): |
| 837 | lines[i] = lines[i][:indent] + "#" + lines[i][indent:] |
| 838 | snippet = string.join(lines, '\r') |
| 839 | self.insert(snippet) |
| 840 | self.ted.WESetSelection(selstart, selstart + len(snippet)) |
| 841 | |
| 842 | def setfile(self, file): |
| 843 | self.file = file |
| 844 | |
| 845 | def set(self, text, file = ''): |
| 846 | oldfile = self.file |
| 847 | self.file = file |
| 848 | if self._debugger: |
| 849 | self._debugger.unregister_editor(self, oldfile) |
| 850 | self._debugger.register_editor(self, file) |
| 851 | TextEditor.set(self, text) |
| 852 | |
| 853 | def close(self): |
| 854 | if self._debugger: |
| 855 | self._debugger.unregister_editor(self, self.file) |
| 856 | self._debugger = None |
| 857 | TextEditor.close(self) |
| 858 | |
| 859 | def click(self, point, modifiers): |
| 860 | if not self._enabled: |
| 861 | return |
| 862 | if self._debugger and self.pt_in_breaks(point): |
| 863 | self.breakhit(point, modifiers) |
| 864 | elif self._debugger: |
| 865 | bl, bt, br, bb = self._getbreakrect() |
| 866 | Qd.EraseRect((bl, bt, br-1, bb)) |
| 867 | TextEditor.click(self, point, modifiers) |
| 868 | self.drawbreakpoints() |
| 869 | else: |
| 870 | TextEditor.click(self, point, modifiers) |
| 871 | if self.ted.WEGetClickCount() >= 3: |
| 872 | # select block with our indent |
| 873 | lines = string.split(self.get(), '\r') |
| 874 | selstart, selend = self.ted.WEGetSelection() |
| 875 | lineno = self.ted.WEOffsetToLine(selstart) |
| 876 | tabs = 0 |
| 877 | line = lines[lineno] |
| 878 | while line[tabs:] and line[tabs] == '\t': |
| 879 | tabs = tabs + 1 |
| 880 | tabstag = '\t' * tabs |
| 881 | fromline = 0 |
| 882 | toline = len(lines) |
| 883 | if tabs: |
| 884 | for i in range(lineno - 1, -1, -1): |
| 885 | line = lines[i] |
| 886 | if line[:tabs] <> tabstag: |
| 887 | fromline = i + 1 |
| 888 | break |
| 889 | for i in range(lineno + 1, toline): |
| 890 | line = lines[i] |
| 891 | if line[:tabs] <> tabstag: |
| 892 | toline = i - 1 |
| 893 | break |
| 894 | selstart, dummy = self.ted.WEGetLineRange(fromline) |
| 895 | dummy, selend = self.ted.WEGetLineRange(toline) |
| 896 | self.ted.WESetSelection(selstart, selend) |
| 897 | |
| 898 | def breakhit(self, point, modifiers): |
| 899 | if not self.file: |
| 900 | return |
| 901 | destrect = self.ted.WEGetDestRect() |
| 902 | offset, edge = self.ted.WEGetOffset(point) |
| 903 | lineno = self.ted.WEOffsetToLine(offset) + 1 |
| 904 | if point[1] <= destrect[3]: |
| 905 | self._debugger.clear_breaks_above(self.file, self.countlines()) |
| 906 | self._debugger.toggle_break(self.file, lineno) |
| 907 | else: |
| 908 | self._debugger.clear_breaks_above(self.file, lineno) |
| 909 | |
| 910 | def key(self, char, event): |
| 911 | (what, message, when, where, modifiers) = event |
| 912 | if modifiers & Events.cmdKey and not char in Wkeys.arrowkeys: |
| 913 | return |
| 914 | if char == '\r': |
| 915 | selstart, selend = self.ted.WEGetSelection() |
| 916 | selstart, selend = min(selstart, selend), max(selstart, selend) |
| 917 | lastchar = chr(self.ted.WEGetChar(selstart-1)) |
| 918 | if lastchar <> '\r' and selstart: |
| 919 | pos, dummy = self.ted.WEFindLine(selstart, 0) |
| 920 | lineres = Res.Resource('') |
| 921 | self.ted.WECopyRange(pos, selstart, lineres, None, None) |
| 922 | line = lineres.data + '\n' |
| 923 | tabcount = self.extratabs(line) |
| 924 | self.ted.WEKey(ord('\r'), 0) |
| 925 | for i in range(tabcount): |
| 926 | self.ted.WEKey(ord('\t'), 0) |
| 927 | else: |
| 928 | self.ted.WEKey(ord('\r'), 0) |
| 929 | elif char in ')]}': |
| 930 | self.ted.WEKey(ord(char), modifiers) |
| 931 | self.balanceparens(char) |
| 932 | else: |
| 933 | self.ted.WEKey(ord(char), modifiers) |
| 934 | if char not in Wkeys.navigationkeys: |
| 935 | self.textchanged() |
| 936 | self.selectionchanged() |
| 937 | self.updatescrollbars() |
| 938 | |
| 939 | def balanceparens(self, char): |
| 940 | if char == ')': |
| 941 | target = '(' |
| 942 | elif char == ']': |
| 943 | target = '[' |
| 944 | elif char == '}': |
| 945 | target = '{' |
| 946 | recursionlevel = 1 |
| 947 | selstart, selend = self.ted.WEGetSelection() |
| 948 | count = min(selstart, selend) - 2 |
| 949 | mincount = max(0, count - 2048) |
| 950 | lastquote = None |
| 951 | while count > mincount: |
| 952 | testchar = chr(self.ted.WEGetChar(count)) |
| 953 | if testchar in "\"'" and chr(self.ted.WEGetChar(count - 1)) <> '\\': |
| 954 | if lastquote == testchar: |
| 955 | recursionlevel = recursionlevel - 1 |
| 956 | lastquote = None |
| 957 | elif not lastquote: |
| 958 | recursionlevel = recursionlevel + 1 |
| 959 | lastquote = testchar |
| 960 | elif not lastquote and testchar == char: |
| 961 | recursionlevel = recursionlevel + 1 |
| 962 | elif not lastquote and testchar == target: |
| 963 | recursionlevel = recursionlevel - 1 |
| 964 | if recursionlevel == 0: |
| 965 | import time |
| 966 | autoscroll = self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, -1) |
| 967 | if autoscroll: |
| 968 | self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 0) |
| 969 | self.ted.WESetSelection(count, count + 1) |
| 970 | self._parentwindow.wid.GetWindowPort().QDFlushPortBuffer(None) # needed under OSX |
| 971 | time.sleep(0.2) |
| 972 | self.ted.WESetSelection(selstart, selend) |
| 973 | if autoscroll: |
| 974 | self.ted.WEFeatureFlag(WASTEconst.weFAutoScroll, 1) |
| 975 | break |
| 976 | count = count - 1 |
| 977 | |
| 978 | def extratabs(self, line): |
| 979 | tabcount = 0 |
| 980 | for c in line: |
| 981 | if c <> '\t': |
| 982 | break |
| 983 | tabcount = tabcount + 1 |
| 984 | last = 0 |
| 985 | cleanline = '' |
| 986 | tags = PyFontify.fontify(line) |
| 987 | # strip comments and strings |
| 988 | for tag, start, end, sublist in tags: |
| 989 | if tag in ('string', 'comment'): |
| 990 | cleanline = cleanline + line[last:start] |
| 991 | last = end |
| 992 | cleanline = cleanline + line[last:] |
| 993 | cleanline = string.strip(cleanline) |
| 994 | if cleanline and cleanline[-1] == ':': |
| 995 | tabcount = tabcount + 1 |
| 996 | else: |
| 997 | # extra indent after unbalanced (, [ or { |
| 998 | for open, close in (('(', ')'), ('[', ']'), ('{', '}')): |
| 999 | count = string.count(cleanline, open) |
| 1000 | if count and count > string.count(cleanline, close): |
| 1001 | tabcount = tabcount + 2 |
| 1002 | break |
| 1003 | return tabcount |
| 1004 | |
| 1005 | def rollover(self, point, onoff): |
| 1006 | if onoff: |
| 1007 | if self._debugger and self.pt_in_breaks(point): |
| 1008 | Wbase.SetCursor("arrow") |
| 1009 | else: |
| 1010 | Wbase.SetCursor("iBeam") |
| 1011 | |
| 1012 | def draw(self, visRgn = None): |
| 1013 | TextEditor.draw(self, visRgn) |
| 1014 | if self._debugger: |
| 1015 | self.drawbreakpoints() |
| 1016 | |
| 1017 | def showbreakpoints(self, onoff): |
| 1018 | if (not not self._debugger) <> onoff: |
| 1019 | if onoff: |
| 1020 | if not __debug__: |
| 1021 | import W |
| 1022 | raise W.AlertError, "Can't debug in \"Optimize bytecode\" mode.\r(see \"Default startup options\" in EditPythonPreferences)" |
| 1023 | import PyDebugger |
| 1024 | self._debugger = PyDebugger.getdebugger() |
| 1025 | self._debugger.register_editor(self, self.file) |
| 1026 | elif self._debugger: |
| 1027 | self._debugger.unregister_editor(self, self.file) |
| 1028 | self._debugger = None |
| 1029 | self.adjust(self._bounds) |
| 1030 | |
| 1031 | def togglebreakpoints(self): |
| 1032 | self.showbreakpoints(not self._debugger) |
| 1033 | |
| 1034 | def clearbreakpoints(self): |
| 1035 | if self.file: |
| 1036 | self._debugger.clear_all_file_breaks(self.file) |
| 1037 | |
| 1038 | def editbreakpoints(self): |
| 1039 | if self._debugger: |
| 1040 | self._debugger.edit_breaks() |
| 1041 | self._debugger.breaksviewer.selectfile(self.file) |
| 1042 | |
| 1043 | def drawbreakpoints(self, eraseall = 0): |
| 1044 | breakrect = bl, bt, br, bb = self._getbreakrect() |
| 1045 | br = br - 1 |
| 1046 | self.SetPort() |
| 1047 | Qd.PenPat(Qd.GetQDGlobalsGray()) |
| 1048 | Qd.PaintRect((br, bt, br + 1, bb)) |
| 1049 | Qd.PenNormal() |
| 1050 | self._parentwindow.tempcliprect(breakrect) |
| 1051 | Qd.RGBForeColor((0xffff, 0, 0)) |
| 1052 | try: |
| 1053 | lasttop = bt |
| 1054 | self_ted = self.ted |
| 1055 | Qd_PaintOval = Qd.PaintOval |
| 1056 | Qd_EraseRect = Qd.EraseRect |
| 1057 | for lineno in self._debugger.get_file_breaks(self.file): |
| 1058 | start, end = self_ted.WEGetLineRange(lineno - 1) |
| 1059 | if lineno <> self_ted.WEOffsetToLine(start) + 1: |
| 1060 | # breakpoints beyond our text: erase rest, and back out |
| 1061 | Qd_EraseRect((bl, lasttop, br, bb)) |
| 1062 | break |
| 1063 | (x, y), h = self_ted.WEGetPoint(start, 0) |
| 1064 | bottom = y + h |
| 1065 | #print y, (lasttop, bottom) |
| 1066 | if bottom > lasttop: |
| 1067 | Qd_EraseRect((bl, lasttop, br, y + h * eraseall)) |
| 1068 | lasttop = bottom |
| 1069 | redbullet = bl + 2, y + 3, bl + 8, y + 9 |
| 1070 | Qd_PaintOval(redbullet) |
| 1071 | else: |
| 1072 | Qd_EraseRect((bl, lasttop, br, bb)) |
| 1073 | Qd.RGBForeColor((0, 0, 0)) |
| 1074 | finally: |
| 1075 | self._parentwindow.restoreclip() |
| 1076 | |
| 1077 | def updatescrollbars(self): |
| 1078 | if self._debugger: |
| 1079 | self.drawbreakpoints(1) |
| 1080 | TextEditor.updatescrollbars(self) |
| 1081 | |
| 1082 | def pt_in_breaks(self, point): |
| 1083 | return Qd.PtInRect(point, self._getbreakrect()) |
| 1084 | |
| 1085 | def _getbreakrect(self): |
| 1086 | if self._debugger: |
| 1087 | l, t, r, b = self._bounds |
| 1088 | return (l+1, t+1, l + 12, b-1) |
| 1089 | else: |
| 1090 | return (0, 0, 0, 0) |
| 1091 | |
| 1092 | def _getviewrect(self): |
| 1093 | l, t, r, b = self._bounds |
| 1094 | if self._debugger: |
| 1095 | return (l + 17, t + 2, r, b - 2) |
| 1096 | else: |
| 1097 | return (l + 5, t + 2, r, b - 2) |
| 1098 | |
| 1099 | def _calctextbounds(self): |
| 1100 | viewrect = l, t, r, b = self._getviewrect() |
| 1101 | if self.ted: |
| 1102 | dl, dt, dr, db = self.ted.WEGetDestRect() |
| 1103 | vl, vt, vr, vb = self.ted.WEGetViewRect() |
| 1104 | xshift = l - vl |
| 1105 | yshift = t - vt |
| 1106 | if (db - dt) < (b - t): |
| 1107 | yshift = t - dt |
| 1108 | destrect = (dl + xshift, dt + yshift, dr + xshift, db + yshift) |
| 1109 | else: |
| 1110 | destrect = (l, t, r + 5000, b) |
| 1111 | return viewrect, destrect |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 1112 | |
| 1113 | |
| 1114 | def GetFNum(fontname): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 1115 | """Same as Fm.GetFNum(), but maps a missing font to Monaco instead of the system font.""" |
| 1116 | if fontname <> Fm.GetFontName(0): |
| 1117 | fontid = Fm.GetFNum(fontname) |
| 1118 | if fontid == 0: |
| 1119 | fontid = Fonts.monaco |
| 1120 | else: |
| 1121 | fontid = 0 |
| 1122 | return fontid |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 1123 | |
| 1124 | # b/w compat. Anyone using this? |
| 1125 | GetFName = Fm.GetFontName |
| 1126 | |
| 1127 | def GetPortFontSettings(port): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 1128 | return Fm.GetFontName(port.GetPortTextFont()), port.GetPortTextFace(), port.GetPortTextSize() |
Just van Rossum | 40f9b7b | 1999-01-30 22:39:17 +0000 | [diff] [blame] | 1129 | |
| 1130 | def SetPortFontSettings(port, (font, face, size)): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 1131 | saveport = Qd.GetPort() |
| 1132 | Qd.SetPort(port) |
| 1133 | Qd.TextFont(GetFNum(font)) |
| 1134 | Qd.TextFace(face) |
| 1135 | Qd.TextSize(size) |
| 1136 | Qd.SetPort(saveport) |