Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # Module 'listwin' |
| 2 | # List windows, a subclass of gwin |
| 3 | |
| 4 | import gwin |
| 5 | import stdwin |
| 6 | |
| 7 | def maxlinewidth(a): # Compute maximum textwidth of lines in a sequence |
| 8 | max = 0 |
| 9 | for line in a: |
| 10 | width = stdwin.textwidth(line) |
| 11 | if width > max: max = width |
| 12 | return max |
| 13 | |
| 14 | def action(w, string, i, detail): # Default item selection method |
| 15 | pass |
| 16 | |
| 17 | def mup(w, detail): # Mouse up method |
| 18 | (h, v), clicks, button, mask = detail |
| 19 | i = divmod(v, w.lineheight)[0] |
| 20 | if 0 <= i < len(w.data): |
| 21 | w.action(w, w.data[i], i, detail) |
| 22 | |
| 23 | def draw(w, ((left, top), (right, bottom))): # Text window draw method |
| 24 | data = w.data |
| 25 | d = w.begindrawing() |
| 26 | lh = w.lineheight |
| 27 | itop = top/lh |
| 28 | ibot = (bottom-1)/lh + 1 |
| 29 | if itop < 0: itop = 0 |
| 30 | if ibot > len(data): ibot = len(data) |
| 31 | for i in range(itop, ibot): d.text((0, i*lh), data[i]) |
| 32 | |
| 33 | def open(title, data): # Display a list of texts in a window |
| 34 | lineheight = stdwin.lineheight() |
| 35 | h, v = maxlinewidth(data), len(data)*lineheight |
| 36 | h0, v0 = h + stdwin.textwidth(' '), v + lineheight |
| 37 | if h0 > stdwin.textwidth(' ')*80: h0 = 0 |
| 38 | if v0 > stdwin.lineheight()*24: v0 = 0 |
| 39 | stdwin.setdefwinsize(h0, v0) |
| 40 | w = gwin.open(title) |
| 41 | w.setdocsize(h, v) |
| 42 | w.lineheight = lineheight |
| 43 | w.data = data |
| 44 | w.draw = draw |
| 45 | w.action = action |
| 46 | w.mup = mup |
| 47 | return w |