|
4 | 4 | import string |
5 | 5 | import types |
6 | 6 | import re |
| 7 | +from Carbon import Qd, Icn, Fm, QuickDraw |
| 8 | +from Carbon.List import GetListPort |
| 9 | +from Carbon.QuickDraw import hilitetransfermode |
| 10 | + |
7 | 11 |
|
8 | 12 | nullid = '\0\0' |
9 | 13 | closedid = struct.pack('h', 468) |
@@ -44,9 +48,62 @@ def double_repr(key, value, truncvalue = 0, |
44 | 48 | return key + '\t' + value |
45 | 49 |
|
46 | 50 |
|
47 | | -class BrowserWidget(W.List): |
| 51 | +def truncString(s, maxwid): |
| 52 | + if maxwid < 1: |
| 53 | + return 1, "" |
| 54 | + strlen = len(s) |
| 55 | + strwid = Qd.TextWidth(s, 0, strlen); |
| 56 | + if strwid <= maxwid: |
| 57 | + return 0, s |
| 58 | + |
| 59 | + Qd.TextFace(QuickDraw.condense) |
| 60 | + strwid = Qd.TextWidth(s, 0, strlen) |
| 61 | + ellipsis = Qd.StringWidth('\xc9') |
48 | 62 |
|
49 | | - LDEF_ID = 471 |
| 63 | + if strwid <= maxwid: |
| 64 | + Qd.TextFace(0) |
| 65 | + return 1, s |
| 66 | + if strwid < 1: |
| 67 | + Qd.TextFace(0) |
| 68 | + return 1, "" |
| 69 | + |
| 70 | + mid = int(strlen * maxwid / strwid) |
| 71 | + while 1: |
| 72 | + if mid <= 0: |
| 73 | + mid = 0 |
| 74 | + break |
| 75 | + strwid = Qd.TextWidth(s, 0, mid) + ellipsis |
| 76 | + strwid2 = Qd.TextWidth(s, 0, mid + 1) + ellipsis |
| 77 | + if strwid <= maxwid and maxwid <= strwid2: |
| 78 | + if maxwid == strwid2: |
| 79 | + mid += 1 |
| 80 | + break |
| 81 | + if strwid > maxwid: |
| 82 | + mid -= 1 |
| 83 | + if mid <= 0: |
| 84 | + mid = 0 |
| 85 | + break |
| 86 | + elif strwid2 < maxwid: |
| 87 | + mid += 1 |
| 88 | + Qd.TextFace(0) |
| 89 | + return 1, s[:mid] + '\xc9' |
| 90 | + |
| 91 | + |
| 92 | +def drawTextCell(text, cellRect, ascent, theList): |
| 93 | + l, t, r, b = cellRect |
| 94 | + cellwidth = r - l |
| 95 | + Qd.MoveTo(l + 2, t + ascent) |
| 96 | + condense, text = truncString(text, cellwidth - 3) |
| 97 | + if condense: |
| 98 | + Qd.TextFace(QuickDraw.condense) |
| 99 | + Qd.DrawText(text, 0, len(text)) |
| 100 | + Qd.TextFace(0) |
| 101 | + |
| 102 | + |
| 103 | +PICTWIDTH = 16 |
| 104 | + |
| 105 | + |
| 106 | +class BrowserWidget(W.CustomList): |
50 | 107 |
|
51 | 108 | def __init__(self, possize, object = None, col = 100, closechildren = 0): |
52 | 109 | W.List.__init__(self, possize, callback = self.listhit) |
@@ -299,6 +356,89 @@ def domenu_copy(self, *args): |
299 | 356 | sc = Scrap.GetCurrentScrap() |
300 | 357 | sc.PutScrapFlavor('TEXT', 0, text) |
301 | 358 |
|
| 359 | + def listDefDraw(self, selected, cellRect, theCell, |
| 360 | + dataOffset, dataLen, theList): |
| 361 | + self.myDrawCell(0, selected, cellRect, theCell, |
| 362 | + dataOffset, dataLen, theList) |
| 363 | + |
| 364 | + def listDefHighlight(self, selected, cellRect, theCell, |
| 365 | + dataOffset, dataLen, theList): |
| 366 | + self.myDrawCell(1, selected, cellRect, theCell, |
| 367 | + dataOffset, dataLen, theList) |
| 368 | + |
| 369 | + def myDrawCell(self, onlyHilite, selected, cellRect, theCell, |
| 370 | + dataOffset, dataLen, theList): |
| 371 | + savedPort = Qd.GetPort() |
| 372 | + Qd.SetPort(GetListPort(theList)) |
| 373 | + savedClip = Qd.NewRgn() |
| 374 | + Qd.GetClip(savedClip) |
| 375 | + Qd.ClipRect(cellRect) |
| 376 | + savedPenState = Qd.GetPenState() |
| 377 | + Qd.PenNormal() |
| 378 | + |
| 379 | + l, t, r, b = cellRect |
| 380 | + |
| 381 | + if not onlyHilite: |
| 382 | + Qd.EraseRect(cellRect) |
| 383 | + |
| 384 | + ascent, descent, leading, size, hm = Fm.FontMetrics() |
| 385 | + linefeed = ascent + descent + leading |
| 386 | + |
| 387 | + if dataLen >= 6: |
| 388 | + data = theList.LGetCell(dataLen, theCell) |
| 389 | + iconId, indent, tab = struct.unpack("hhh", data[:6]) |
| 390 | + key, value = data[6:].split("\t", 1) |
| 391 | + |
| 392 | + if iconId: |
| 393 | + theIcon = Icn.GetCIcon(iconId) |
| 394 | + rect = (0, 0, 16, 16) |
| 395 | + rect = Qd.OffsetRect(rect, l, t) |
| 396 | + rect = Qd.OffsetRect(rect, 0, (theList.cellSize[1] - (rect[3] - rect[1])) / 2) |
| 397 | + Icn.PlotCIcon(rect, theIcon) |
| 398 | + |
| 399 | + if len(key) >= 0: |
| 400 | + cl, ct, cr, cb = cellRect |
| 401 | + vl, vt, vr, vb = self._viewbounds |
| 402 | + cl = vl + PICTWIDTH + indent |
| 403 | + cr = vl + tab |
| 404 | + if cr > vr: |
| 405 | + cr = vr |
| 406 | + if cl < cr: |
| 407 | + drawTextCell(key, (cl, ct, cr, cb), ascent, theList) |
| 408 | + cl = vl + tab |
| 409 | + cr = vr |
| 410 | + if cl < cr: |
| 411 | + drawTextCell(value, (cl, ct, cr, cb), ascent, theList) |
| 412 | + #elif dataLen != 0: |
| 413 | + # drawTextCell("???", 3, cellRect, ascent, theList) |
| 414 | + |
| 415 | + # draw nice dotted line |
| 416 | + l, t, r, b = cellRect |
| 417 | + l = self._viewbounds[0] + tab |
| 418 | + r = l + 1; |
| 419 | + if not (theList.cellSize[1] & 0x01) or (t & 0x01): |
| 420 | + myPat = "\xff\x00\xff\x00\xff\x00\xff\x00" |
| 421 | + else: |
| 422 | + myPat = "\x00\xff\x00\xff\x00\xff\x00\xff" |
| 423 | + Qd.PenPat(myPat) |
| 424 | + Qd.PenMode(QuickDraw.srcCopy) |
| 425 | + Qd.PaintRect((l, t, r, b)) |
| 426 | + Qd.PenNormal() |
| 427 | + |
| 428 | + if selected or onlyHilite: |
| 429 | + l, t, r, b = cellRect |
| 430 | + l = self._viewbounds[0] + PICTWIDTH |
| 431 | + r = self._viewbounds[2] |
| 432 | + Qd.PenMode(hilitetransfermode) |
| 433 | + Qd.PaintRect((l, t, r, b)) |
| 434 | + |
| 435 | + # restore graphics environment |
| 436 | + Qd.SetPort(savedPort) |
| 437 | + Qd.SetClip(savedClip) |
| 438 | + Qd.DisposeRgn(savedClip) |
| 439 | + Qd.SetPenState(savedPenState) |
| 440 | + |
| 441 | + |
302 | 442 |
|
303 | 443 | class Browser: |
304 | 444 |
|
|
0 commit comments