|
| 1 | +from FrameWork import * |
| 2 | +import Win |
| 3 | +import Qd |
| 4 | +import Controls |
| 5 | +import Ctl |
| 6 | +import TE |
| 7 | +import List |
| 8 | +import os |
| 9 | +import string |
| 10 | +import macfs |
| 11 | + |
| 12 | +SCROLLBAR=16 |
| 13 | +MARGIN=2 |
| 14 | +ICONSIZE=16 |
| 15 | +TEXTWIDTH=4096 # More-or-less random value |
| 16 | + |
| 17 | +PIC_CURRENT=512 |
| 18 | +PIC_BREAK=513 |
| 19 | +picture_cache={} |
| 20 | + |
| 21 | +class MT_TextWidget: |
| 22 | + def __init__(self, wid, r): |
| 23 | + self.wid = wid |
| 24 | + self.rect = r |
| 25 | + left, top, right, bottom = r |
| 26 | + self.terect = left+MARGIN+ICONSIZE, top+MARGIN, \ |
| 27 | + right-(MARGIN+SCROLLBAR), bottom-(MARGIN+SCROLLBAR) |
| 28 | + dr = self.terect[0], self.terect[1], TEXTWIDTH, self.terect[3] |
| 29 | + Qd.SetPort(wid) |
| 30 | + Qd.TextFont(4) |
| 31 | + Qd.TextSize(9) |
| 32 | + self.ted = TE.TENew(dr, self.terect) |
| 33 | + self.ted.TEAutoView(1) |
| 34 | + self.activate(1) |
| 35 | + |
| 36 | + rect = right-SCROLLBAR, top, right, bottom-SCROLLBAR+1 |
| 37 | + self.bary = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0) |
| 38 | + rect = left, bottom-SCROLLBAR, right-SCROLLBAR+1, bottom |
| 39 | + self.barx = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0) |
| 40 | + |
| 41 | + self.have_data = 0 |
| 42 | + self.line_index = [] |
| 43 | + |
| 44 | + def close(self): |
| 45 | + del self.barx |
| 46 | + del self.bary |
| 47 | + del self.ted |
| 48 | + |
| 49 | + def scrollbars(self): |
| 50 | + pass |
| 51 | + |
| 52 | + def setcontent(self, file): |
| 53 | + self.line_index = [] |
| 54 | + if file == None: |
| 55 | + data = '' |
| 56 | + self.have_data = 0 |
| 57 | + else: |
| 58 | + try: |
| 59 | + fp = open(file, 'rb') # NOTE the binary |
| 60 | + data = fp.read() |
| 61 | + self.have_data = 1 |
| 62 | + except IOError, arg: |
| 63 | + data = 'Cannot open file:\r'+`arg` |
| 64 | + self.have_data = 0 |
| 65 | + if len(data) > 32767: |
| 66 | + self.have_data = 0 |
| 67 | + data = 'File too big' |
| 68 | + self.ted.TESetText(data) |
| 69 | + if self.have_data: |
| 70 | + cur = 0 |
| 71 | + while 1: |
| 72 | + self.line_index.append(cur) |
| 73 | + try: |
| 74 | + cur = string.index(data, '\r', cur+1) |
| 75 | + except ValueError: |
| 76 | + break |
| 77 | + self.line_index.append(len(data)) |
| 78 | + Win.InvalRect(self.rect) |
| 79 | + self.ted.TESetSelect(0,0) |
| 80 | + self.ted.TECalText() |
| 81 | + self.ted.TESelView() |
| 82 | + self.setscrollbars() |
| 83 | + |
| 84 | + def setscrollbars(self): |
| 85 | + docleft, doctop, docright, docbot = self.ted.destRect |
| 86 | + winleft, wintop, winright, winbot = self.ted.viewRect |
| 87 | + docbot = self.ted.nLines*self.ted.lineHeight + doctop |
| 88 | + self.setbar(self.barx, docleft, docright, winleft, winright) |
| 89 | + self.setbar(self.bary, doctop, docbot, wintop, winbot) |
| 90 | + |
| 91 | + def setbar(self, bar, minmin, maxmax, curmin, curmax): |
| 92 | + if maxmax-minmin > 32767 or (curmin <= minmin and curmax >= maxmax): |
| 93 | + bar.SetControlMinimum(0) |
| 94 | + bar.SetControlMaximum(0) |
| 95 | + bar.SetControlValue(0) |
| 96 | + return |
| 97 | + bar.SetControlMinimum(minmin) |
| 98 | + bar.SetControlMaximum(maxmax-(curmax-curmin)) |
| 99 | + bar.SetControlValue(curmin) |
| 100 | + |
| 101 | + def update(self, rgn): |
| 102 | + Qd.EraseRect(self.terect) |
| 103 | + Qd.FrameRect(self.rect) |
| 104 | + self.ted.TEUpdate(self.terect) |
| 105 | + |
| 106 | + def activate(self, onoff): |
| 107 | + if onoff: |
| 108 | + self.ted.TEActivate() |
| 109 | + else: |
| 110 | + self.ted.TEDeactivate() |
| 111 | + |
| 112 | + def select(self, line): |
| 113 | + if line == None or line <= 0 or not self.have_data: |
| 114 | + self.ted.TESetSelect(0,0) |
| 115 | + else: |
| 116 | + line = line - 1 |
| 117 | + if line > len(self.line_index)-1: line = len(self.line_index)-1 |
| 118 | + if line == 1: |
| 119 | + self.ted.TESetSelect(0, self.line_index[1]) |
| 120 | + else: |
| 121 | + self.ted.TESetSelect(self.line_index[line]+1, self.line_index[line+1]) |
| 122 | + self.setscrollbars() |
| 123 | + |
| 124 | + def click(self, where, modifiers): |
| 125 | + # First check scrollbars |
| 126 | + ctltype, control = Ctl.FindControl(where, self.wid) |
| 127 | + if ctltype and control: |
| 128 | + partcode = control.TrackControl(where) |
| 129 | + if partcode: |
| 130 | + self.controlhit(control, partcode) |
| 131 | + return None, 0 |
| 132 | + off = self.ted.TEGetOffset(where) |
| 133 | + inborder = where[0] < self.terect[0] |
| 134 | + return self.offsettoline(off), inborder |
| 135 | + |
| 136 | + def offsettoline(self, offset): |
| 137 | + for i in range(len(self.line_index)): |
| 138 | + if offset < self.line_index[i]: |
| 139 | + return i # Not i-1: 1-based line numbers in files |
| 140 | + return None |
| 141 | + |
| 142 | + def controlhit(self, control, partcode): |
| 143 | + if partcode <> Controls.inThumb: |
| 144 | + if control == self.barx: |
| 145 | + if partcode == Controls.inUpButton: |
| 146 | + delta = -10 |
| 147 | + if partcode == Controls.inDownButton: |
| 148 | + delta = 10 |
| 149 | + if partcode == Controls.inPageUp: |
| 150 | + delta = 10-(self.terect[2]-self.terect[0]) |
| 151 | + if partcode == Controls.inPageDown: |
| 152 | + delta = (self.terect[2]-self.terect[0])-10 |
| 153 | + old = control.GetControlValue() |
| 154 | + control.SetControlValue(old+delta) |
| 155 | + if control == self.bary: |
| 156 | + if partcode == Controls.inUpButton: |
| 157 | + delta = -self.ted.lineHeight |
| 158 | + if partcode == Controls.inDownButton: |
| 159 | + delta = self.ted.lineHeight |
| 160 | + if partcode == Controls.inPageUp: |
| 161 | + delta = self.ted.lineHeight-(self.terect[3]-self.terect[1]) |
| 162 | + if partcode == Controls.inPageDown: |
| 163 | + delta = (self.terect[3]-self.terect[1])-self.ted.lineHeight |
| 164 | + old = control.GetControlValue() |
| 165 | + control.SetControlValue(old+delta) |
| 166 | + newx = self.barx.GetControlValue() |
| 167 | + newy = self.bary.GetControlValue() |
| 168 | + oldx = self.ted.viewRect[0] |
| 169 | + oldy = self.ted.viewRect[1] |
| 170 | + self.ted.TEPinScroll(oldx-newx, oldy-newy) |
| 171 | + self.setscrollbars() # XXXX Bibbert, maar hoe anders? |
| 172 | + |
| 173 | +class MT_IconTextWidget(MT_TextWidget): |
| 174 | + def __init__(self, wid, r): |
| 175 | + MT_TextWidget.__init__(self, wid, r) |
| 176 | + self.breakpointlist = [] |
| 177 | + self.curline = None |
| 178 | + self.iconrect = (self.rect[0]+1, self.rect[1]+1, |
| 179 | + self.terect[0]-1, self.terect[3]-1) |
| 180 | + self.curlinerange = (self.terect[1]+self.ted.lineHeight, |
| 181 | + self.terect[3]-2*self.ted.lineHeight) |
| 182 | + |
| 183 | + def setbreaks(self, list): |
| 184 | + self.breakpointlist = list[:] |
| 185 | + Qd.SetPort(self.wid) |
| 186 | + Win.InvalRect(self.iconrect) |
| 187 | + |
| 188 | + def setcurline(self, line): |
| 189 | + self.curline = line |
| 190 | + Qd.SetPort(self.wid) |
| 191 | + self.showline(line) |
| 192 | + |
| 193 | + def showline(self, line): |
| 194 | + if line <= 0: line = 1 |
| 195 | + if line >= len(self.line_index): line = len(self.line_index)-1 |
| 196 | + if line < 0: return |
| 197 | + off = self.line_index[line] |
| 198 | + x, y = self.ted.TEGetPoint(off) |
| 199 | + if self.curlinerange[0] <= y <= self.curlinerange[1]: |
| 200 | + return # It is in view |
| 201 | + middle = (self.curlinerange[0]+self.curlinerange[1])/2 |
| 202 | + self.ted.TEPinScroll(0, middle-y) # Of andersom? |
| 203 | + self.setscrollbars() |
| 204 | + |
| 205 | + def setscrollbars(self): |
| 206 | + MT_TextWidget.setscrollbars(self) |
| 207 | + Win.InvalRect(self.iconrect) |
| 208 | + |
| 209 | + def update(self, rgn): |
| 210 | + MT_TextWidget.update(self, rgn) |
| 211 | + self.drawallicons() |
| 212 | + |
| 213 | + def drawallicons(self): |
| 214 | + Qd.EraseRect(self.iconrect) |
| 215 | + Qd.MoveTo(self.iconrect[2], self.iconrect[1]) |
| 216 | + Qd.LineTo(self.iconrect[2], self.iconrect[3]) |
| 217 | + topoffset = self.ted.TEGetOffset((self.terect[0], self.terect[1])) |
| 218 | + botoffset = self.ted.TEGetOffset((self.terect[0], self.terect[3])) |
| 219 | + topline = self.offsettoline(topoffset) |
| 220 | + botline = self.offsettoline(botoffset) |
| 221 | + if topline == None: topline = 1 # ??? |
| 222 | + if botline == None: botline = len(self.line_index) |
| 223 | + for i in self.breakpointlist: |
| 224 | + if topline <= i <= botline: |
| 225 | + self.draw1icon(i, PIC_BREAK) |
| 226 | + if self.curline <> None and topline <= self.curline <= botline: |
| 227 | + self.draw1icon(self.curline, PIC_CURRENT) |
| 228 | + |
| 229 | + def draw1icon(self, line, which): |
| 230 | + offset = self.line_index[line] |
| 231 | + botx, boty = self.ted.TEGetPoint(offset) |
| 232 | + rect = self.rect[0]+2, boty-self.ted.lineHeight, \ |
| 233 | + self.rect[0]+ICONSIZE-2, boty |
| 234 | + if not picture_cache.has_key(which): |
| 235 | + print 'Get picture', which |
| 236 | + picture_cache[which] = Qd.GetPicture(which) |
| 237 | + self.drawicon(rect, picture_cache[which]) |
| 238 | + |
| 239 | + def drawicon(self, rect, which): |
| 240 | + Qd.DrawPicture(which, rect) |
| 241 | + |
| 242 | +class MT_IndexList: |
| 243 | + def __init__(self, wid, rect, width): |
| 244 | + # wid is the window (dialog) where our list is going to be in |
| 245 | + # rect is it's item rectangle (as in dialog item) |
| 246 | + self.rect = rect |
| 247 | + rect2 = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-1 |
| 248 | + self.list = List.LNew(rect2, (0, 0, width, 0), (0,0), 0, wid, |
| 249 | + 0, 1, 0, 1) |
| 250 | + self.wid = wid |
| 251 | + self.width = width |
| 252 | + |
| 253 | + def setcontent(self, *content): |
| 254 | + self.list.LDelRow(0, 1) |
| 255 | + self.list.LSetDrawingMode(0) |
| 256 | + self.list.LAddRow(len(content[0]), 0) |
| 257 | + for x in range(len(content)): |
| 258 | + column = content[x] |
| 259 | + for y in range(len(column)): |
| 260 | + self.list.LSetCell(column[y], (x, y)) |
| 261 | + self.list.LSetDrawingMode(1) |
| 262 | + Win.InvalRect(self.rect) |
| 263 | + |
| 264 | + def deselectall(self): |
| 265 | + while 1: |
| 266 | + ok, pt = self.list.LGetSelect(1, (0,0)) |
| 267 | + if not ok: return |
| 268 | + self.list.LSetSelect(0, pt) |
| 269 | + |
| 270 | + def select(self, num): |
| 271 | + self.deselectall() |
| 272 | + if num < 0: |
| 273 | + return |
| 274 | + for i in range(self.width): |
| 275 | + self.list.LSetSelect(1, (i, num)) |
| 276 | + |
| 277 | + def click(self, where, modifiers): |
| 278 | + is_double = self.list.LClick(where, modifiers) |
| 279 | + ok, (x, y) = self.list.LGetSelect(1, (0, 0)) |
| 280 | + if ok: |
| 281 | + return y, is_double |
| 282 | + else: |
| 283 | + return None, is_double |
| 284 | + |
| 285 | + # draw a frame around the list, List Manager doesn't do that |
| 286 | + def drawframe(self): |
| 287 | + Qd.SetPort(self.wid) |
| 288 | + Qd.FrameRect(self.rect) |
| 289 | + |
| 290 | + def update(self, rgn): |
| 291 | + self.drawframe() |
| 292 | + self.list.LUpdate(rgn) |
| 293 | + |
| 294 | + def activate(self, onoff): |
| 295 | + self.list.LActivate(onoff) |
| 296 | + |
| 297 | +class MT_AnyList(MT_IndexList): |
| 298 | + |
| 299 | + def click(self, where, modifiers): |
| 300 | + is_double = self.list.LClick(where, modifiers) |
| 301 | + ok, (x, y) = self.list.LGetSelect(1, (0, 0)) |
| 302 | + if ok: |
| 303 | + self.select(y) |
| 304 | + field0 = self.list.LGetCell(1000,(0,y)) |
| 305 | + else: |
| 306 | + field0 = None |
| 307 | + return field0, is_double |
| 308 | + |
0 commit comments