| 1 |
/* |
| 2 |
* Copyright (c) 2023-2024 joshua stein <jcs@jcs.org> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <string.h> |
| 20 |
#include "textview.h" |
| 21 |
|
| 22 |
void |
| 23 |
TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller) |
| 24 |
{ |
| 25 |
struct TVRec *tv; |
| 26 |
long max; |
| 27 |
RgnHandle rgn; |
| 28 |
short wasval, wasmax, val, div; |
| 29 |
|
| 30 |
tv = TVLock(tvh); |
| 31 |
div = TVScrollDivisor(tv); |
| 32 |
|
| 33 |
/* |
| 34 |
* Avoid SetCtlMin/Max because it will redraw and then SetCtlValue will |
| 35 |
* redraw again, which can cause a jump if we're trying to keep the |
| 36 |
* scrollbar position in the same place (like at the bottom). |
| 37 |
*/ |
| 38 |
max = (tv->bounds.bottom - tv->bounds.top - |
| 39 |
(tv->view.bottom - tv->view.top)) / div; |
| 40 |
if (max < 0) |
| 41 |
max = 0; |
| 42 |
|
| 43 |
wasval = GetCtlValue(scroller); |
| 44 |
|
| 45 |
HLock(scroller); |
| 46 |
wasmax = (*scroller)->contrlMax; |
| 47 |
(*scroller)->contrlMin = 0; |
| 48 |
val = -(tv->scrolled.top) / div; |
| 49 |
(*scroller)->contrlMax = max; |
| 50 |
SetCtlValue(scroller, val); |
| 51 |
HUnlock(scroller); |
| 52 |
|
| 53 |
if ((wasmax == 0 && max != 0) || (wasmax != 0 && max == 0)) |
| 54 |
goto redraw; |
| 55 |
if (wasval == 0 && val == 0) |
| 56 |
goto done; |
| 57 |
if (wasval == wasmax && val == max && tv->autoscroll) |
| 58 |
goto done; |
| 59 |
|
| 60 |
redraw: |
| 61 |
rgn = NewRgn(); |
| 62 |
HLock(scroller); |
| 63 |
RectRgn(rgn, &(*scroller)->contrlRect); |
| 64 |
UpdtControl((*(scroller))->contrlOwner, rgn); |
| 65 |
HUnlock(scroller); |
| 66 |
DisposeRgn(rgn); |
| 67 |
|
| 68 |
done: |
| 69 |
TVUnlock(tv); |
| 70 |
} |
| 71 |
|
| 72 |
static TVHandle tv_track_control = NULL; |
| 73 |
void |
| 74 |
TVSetTrackScrollControl(TVHandle tvh) |
| 75 |
{ |
| 76 |
tv_track_control = tvh; |
| 77 |
} |
| 78 |
|
| 79 |
pascal void |
| 80 |
TVTrackScrollControl(ControlHandle control, short part) |
| 81 |
{ |
| 82 |
struct TVRec *tv; |
| 83 |
KeyMap km; |
| 84 |
Boolean cmd_down; |
| 85 |
|
| 86 |
if (tv_track_control == NULL) { |
| 87 |
/* TVTrackScrollControl without TVSetTrackScrollControl */ |
| 88 |
Debugger(); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
GetKeys(&km); |
| 93 |
cmd_down = (km[1] & (1L << 15)) != 0; |
| 94 |
|
| 95 |
TVScrollPart(tv_track_control, part, cmd_down); |
| 96 |
|
| 97 |
tv = TVLock(tv_track_control); |
| 98 |
if (control != tv->scroller) |
| 99 |
SetCtlValue(control, -(tv->scrolled.top) / TVScrollDivisor(tv)); |
| 100 |
TVUnlock(tv); |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
TVScrollPart(TVHandle tvh, short part, Boolean fine) |
| 105 |
{ |
| 106 |
struct TVRec *tv; |
| 107 |
struct TVLine *top_line, *bottom_line, *line, *oline; |
| 108 |
|
| 109 |
tv = TVLock(tvh); |
| 110 |
|
| 111 |
top_line = TVGetLine(tv, TVLineForY(tv, -(tv->scrolled.top))); |
| 112 |
bottom_line = TVGetLine(tv, TVLineForY(tv, -(tv->scrolled.top) + |
| 113 |
tv->frame.bottom - 1)); |
| 114 |
if (top_line == NULL || bottom_line == NULL) { |
| 115 |
TVUnlock(tv); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
switch (part) { |
| 120 |
case inUpButton: |
| 121 |
if (fine) { |
| 122 |
TVScrollLocked(tv, 0, 1); |
| 123 |
break; |
| 124 |
} |
| 125 |
|
| 126 |
/* scroll to the line before the top-most line */ |
| 127 |
if (top_line->nline > 1) { |
| 128 |
oline = TVGetLine(tv, top_line->nline - 1); |
| 129 |
if (oline == NULL) |
| 130 |
break; |
| 131 |
TVScrollLocked(tv, 0, top_line->rect.top - |
| 132 |
oline->rect.top + 1); |
| 133 |
break; |
| 134 |
} |
| 135 |
|
| 136 |
/* nothing found, just scroll to the top */ |
| 137 |
TVScrollLocked(tv, 0, tv->bounds.bottom); |
| 138 |
break; |
| 139 |
case inPageUp: |
| 140 |
/* scroll to put the top mostly-visible line at the bottom */ |
| 141 |
TVScrollLocked(tv, 0, (long)tv->frame.bottom - |
| 142 |
(tv->scrolled.top + top_line->rect.bottom) - 1); |
| 143 |
break; |
| 144 |
case inDownButton: |
| 145 |
if (fine) { |
| 146 |
TVScrollLocked(tv, 0, -1); |
| 147 |
break; |
| 148 |
} |
| 149 |
|
| 150 |
/* scroll to the line after the bottom-most line */ |
| 151 |
if (bottom_line->nline < tv->nlines) { |
| 152 |
line = TVGetLine(tv, bottom_line->nline + 1); |
| 153 |
if (line == NULL) |
| 154 |
break; |
| 155 |
TVScrollLocked(tv, 0, |
| 156 |
-(tv->scrolled.top - tv->frame.bottom + line->rect.bottom)); |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
/* nothing found, just scroll to the bottom */ |
| 161 |
TVScrollLocked(tv, 0, -(tv->bounds.bottom)); |
| 162 |
break; |
| 163 |
case inPageDown: |
| 164 |
/* scroll to put the bottom mostly-visible line at the top */ |
| 165 |
if (bottom_line->nline < tv->nlines) { |
| 166 |
line = TVGetLine(tv, bottom_line->nline + 1); |
| 167 |
if (line == NULL) |
| 168 |
break; |
| 169 |
TVScrollLocked(tv, 0, -(line->rect.top + tv->scrolled.top) + |
| 170 |
(line->rect.bottom - line->rect.top) + 1); |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
/* nothing found, just scroll to the bottom */ |
| 175 |
TVScrollLocked(tv, 0, -(tv->bounds.bottom)); |
| 176 |
break; |
| 177 |
} |
| 178 |
|
| 179 |
if (tv->scroller) |
| 180 |
SetCtlValue(tv->scroller, |
| 181 |
-(tv->scrolled.top) / TVScrollDivisor(tv)); |
| 182 |
TVUnlock(tv); |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
TVScroll(TVHandle tvh, long horiz, long vert) |
| 187 |
{ |
| 188 |
struct TVRec *tv; |
| 189 |
short div; |
| 190 |
|
| 191 |
tv = TVLock(tvh); |
| 192 |
div = TVScrollDivisor(tv); |
| 193 |
TVScrollLocked(tv, horiz * div, vert * div); |
| 194 |
TVUnlock(tv); |
| 195 |
} |
| 196 |
|
| 197 |
void |
| 198 |
TVScrollToOffset(TVHandle tvh, unsigned long off) |
| 199 |
{ |
| 200 |
struct TVRec *tv; |
| 201 |
struct TVLine *line; |
| 202 |
short div; |
| 203 |
|
| 204 |
tv = TVLock(tvh); |
| 205 |
div = TVScrollDivisor(tv); |
| 206 |
line = TVGetLine(tv, TVLineForOffset(tv, off)); |
| 207 |
if (line != NULL) |
| 208 |
TVScrollLocked(tv, 0, |
| 209 |
-((line->rect.top / div) * div) - tv->scrolled.top); |
| 210 |
TVUnlock(tv); |
| 211 |
} |
| 212 |
|
| 213 |
void |
| 214 |
TVUpdate(TVHandle tvh) |
| 215 |
{ |
| 216 |
struct TVRec *tv; |
| 217 |
|
| 218 |
tv = TVLock(tvh); |
| 219 |
TVRedrawRect(tv, &tv->frame, 0, true); |
| 220 |
TVUnlock(tv); |
| 221 |
} |
| 222 |
|
| 223 |
Boolean |
| 224 |
TVSetupCanvas(struct TVRec *tv) |
| 225 |
{ |
| 226 |
long width, height; |
| 227 |
GrafPtr old_port, canvas = NULL; |
| 228 |
Handle canvas_baseaddr = NULL; |
| 229 |
|
| 230 |
/* |
| 231 |
* TODO: make canvas 3 times vis portal height so we can pre-render |
| 232 |
* pages above and below to make scrolling seem faster? |
| 233 |
*/ |
| 234 |
width = tv->view.right - tv->view.left; |
| 235 |
height = tv->view.bottom - tv->view.top; |
| 236 |
|
| 237 |
/* this must be a Ptr because it cannot move once opened */ |
| 238 |
canvas = (GrafPtr)NewPtrClear(sizeof(GrafPort)); |
| 239 |
if (canvas == NULL) |
| 240 |
goto fail; |
| 241 |
|
| 242 |
canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2; |
| 243 |
canvas_baseaddr = NewHandleClear((long)canvas->portBits.rowBytes * |
| 244 |
height); |
| 245 |
if (canvas_baseaddr == NULL) |
| 246 |
goto fail; |
| 247 |
|
| 248 |
/* everything allocated, commit to new canvas */ |
| 249 |
|
| 250 |
if (tv->canvas) { |
| 251 |
ClosePort(tv->canvas); |
| 252 |
DisposPtr(tv->canvas); |
| 253 |
} |
| 254 |
if (tv->canvas_baseaddr) |
| 255 |
DisposHandle(tv->canvas_baseaddr); |
| 256 |
|
| 257 |
tv->canvas = canvas; |
| 258 |
|
| 259 |
/* opening the port resets portRect/portBits */ |
| 260 |
GetPort(&old_port); |
| 261 |
OpenPort(tv->canvas); |
| 262 |
RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect); |
| 263 |
RectRgn(tv->canvas->visRgn, &tv->canvas->portRect); |
| 264 |
SetPort(old_port); |
| 265 |
|
| 266 |
tv->canvas->portRect.right = width; |
| 267 |
tv->canvas->portRect.bottom = height; |
| 268 |
tv->canvas->portBits.bounds = tv->canvas->portRect; |
| 269 |
tv->canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2; |
| 270 |
|
| 271 |
tv->canvas_baseaddr = canvas_baseaddr; |
| 272 |
HLock(tv->canvas_baseaddr); |
| 273 |
tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr); |
| 274 |
|
| 275 |
return true; |
| 276 |
|
| 277 |
fail: |
| 278 |
if (canvas) |
| 279 |
DisposPtr(canvas); |
| 280 |
if (canvas_baseaddr) |
| 281 |
DisposHandle(canvas_baseaddr); |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
short |
| 286 |
TVScrollDivisor(struct TVRec *tv) |
| 287 |
{ |
| 288 |
short div; |
| 289 |
long max; |
| 290 |
|
| 291 |
div = 1; |
| 292 |
max = tv->bounds.bottom - tv->bounds.top - |
| 293 |
(tv->view.bottom - tv->view.top); |
| 294 |
while (max > SHRT_MAX) { |
| 295 |
div *= 10; |
| 296 |
max /= 10; |
| 297 |
} |
| 298 |
|
| 299 |
if (div == 0) |
| 300 |
Debugger(); |
| 301 |
return div; |
| 302 |
} |
| 303 |
|
| 304 |
Boolean |
| 305 |
TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match, |
| 306 |
void *cookie) |
| 307 |
{ |
| 308 |
unsigned long n; |
| 309 |
long next_tabstop; |
| 310 |
unsigned char c; |
| 311 |
char *text, *spanstart; |
| 312 |
Rect r; |
| 313 |
Point pen; |
| 314 |
|
| 315 |
if (match->text_end == match->text_start) |
| 316 |
return true; |
| 317 |
|
| 318 |
if (match->rect.bottom < -(tv->scrolled.top)) |
| 319 |
/* no sense drawing what we can't see yet */ |
| 320 |
return true; |
| 321 |
|
| 322 |
if (match->rect.top >= -(tv->scrolled.top) + tv->frame.bottom) |
| 323 |
/* further down than the scrolled view, we've drawn enough lines */ |
| 324 |
return false; |
| 325 |
|
| 326 |
if (match->line->grew) { |
| 327 |
r.left = tv->scrolled.left + match->line->rect.left; |
| 328 |
r.top = tv->scrolled.top + match->line->rect.top; |
| 329 |
r.right = tv->scrolled.left + match->line->rect.right; |
| 330 |
r.bottom = tv->scrolled.top + match->line->rect.bottom; |
| 331 |
EraseRect(&r); |
| 332 |
match->line->grew = false; |
| 333 |
} else if (match->style->align == TV_ALIGN_CENTER || |
| 334 |
match->style->align == TV_ALIGN_RIGHT) { |
| 335 |
/* erase from this chunk to the right of the view */ |
| 336 |
r.left = tv->scrolled.left + match->rect.left; |
| 337 |
r.top = tv->scrolled.top + match->line->rect.top; |
| 338 |
r.right = tv->scrolled.left + tv->view.right - tv->padding.right; |
| 339 |
r.bottom = tv->scrolled.top + match->line->rect.bottom; |
| 340 |
EraseRect(&r); |
| 341 |
} |
| 342 |
|
| 343 |
MoveTo(tv->scrolled.left + match->rect.left, |
| 344 |
tv->scrolled.top + match->rect.bottom - match->line->baseline); |
| 345 |
if (match->style->bitmap_data_size) { |
| 346 |
r = match->style->bitmap.bounds; |
| 347 |
r.left = tv->scrolled.left + match->rect.left; |
| 348 |
r.right = r.left + (match->style->bitmap.bounds.right - |
| 349 |
match->style->bitmap.bounds.left); |
| 350 |
r.bottom = tv->scrolled.top + match->line->rect.bottom; |
| 351 |
r.top = r.bottom - (match->style->bitmap.bounds.bottom - |
| 352 |
match->style->bitmap.bounds.top); |
| 353 |
|
| 354 |
match->style->bitmap.baseAddr = (QDPtr)match->style->bitmap_data; |
| 355 |
CopyBits(&match->style->bitmap, &thePort->portBits, |
| 356 |
&match->style->bitmap.bounds, &r, srcOr, NULL); |
| 357 |
} else if (tv->tabstop) { |
| 358 |
text = match->text_slab->data + (match->text_start - |
| 359 |
match->text_slab->base); |
| 360 |
spanstart = NULL; |
| 361 |
for (n = 0; n < match->text_end - match->text_start; n++) { |
| 362 |
c = *text; |
| 363 |
if (c == '\t') { |
| 364 |
if (spanstart != NULL) |
| 365 |
DrawText(spanstart, 0, text - spanstart); |
| 366 |
GetPen(&pen); |
| 367 |
next_tabstop = pen.h + TVCharWidth(tv, match->style, |
| 368 |
match->line, pen.h - tv->scrolled.left - |
| 369 |
match->line->indent, '\t'); |
| 370 |
#if 0 |
| 371 |
/* draw dotted lines for tabs */ |
| 372 |
/* XXX: disabled for now but might be useful as an option */ |
| 373 |
PenPat(&tv->tab_pat); |
| 374 |
MoveTo(pen.h, tv->scrolled.top + match->line->rect.top + |
| 375 |
((match->line->rect.bottom - match->line->rect.top) / 2)); |
| 376 |
GetPen(&pen); |
| 377 |
LineTo(next_tabstop - 1, pen.v); |
| 378 |
PenPat(black); |
| 379 |
MoveTo(next_tabstop, tv->scrolled.top + |
| 380 |
match->line->rect.bottom - match->line->baseline); |
| 381 |
#else |
| 382 |
MoveTo(next_tabstop, pen.v); |
| 383 |
#endif |
| 384 |
spanstart = NULL; |
| 385 |
} else if (spanstart == NULL) |
| 386 |
spanstart = text; |
| 387 |
text++; |
| 388 |
} |
| 389 |
if (spanstart != NULL) |
| 390 |
DrawText(spanstart, 0, text - spanstart); |
| 391 |
} else |
| 392 |
DrawText(match->text_slab->data + (match->text_start - |
| 393 |
match->text_slab->base), 0, match->text_end - match->text_start); |
| 394 |
tv->canvas_dirty = true; |
| 395 |
|
| 396 |
return true; |
| 397 |
} |
| 398 |
|
| 399 |
void |
| 400 |
TVScrollLocked(struct TVRec *tv, long horiz, long vert) |
| 401 |
{ |
| 402 |
long wastop, top, wasleft, left; |
| 403 |
Rect r; |
| 404 |
RgnHandle vacated; |
| 405 |
|
| 406 |
if (!horiz && !vert) |
| 407 |
return; |
| 408 |
|
| 409 |
TVUseCanvas(tv); |
| 410 |
|
| 411 |
wastop = tv->scrolled.top; |
| 412 |
wasleft = tv->scrolled.left; |
| 413 |
|
| 414 |
tv->scrolled.bottom += vert; |
| 415 |
tv->scrolled.top += vert; |
| 416 |
if (tv->scrolled.bottom < tv->frame.bottom) { |
| 417 |
/* can never scroll down lower than last line */ |
| 418 |
tv->scrolled.bottom = tv->frame.bottom; |
| 419 |
tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; |
| 420 |
} |
| 421 |
if (tv->scrolled.top > 0) { |
| 422 |
/* can never scroll up higher than first line */ |
| 423 |
tv->scrolled.top = 0; |
| 424 |
tv->scrolled.bottom = tv->bounds.bottom; |
| 425 |
} |
| 426 |
|
| 427 |
tv->scrolled.left += horiz; |
| 428 |
tv->scrolled.right += horiz; |
| 429 |
/* TODO: bound */ |
| 430 |
|
| 431 |
left = tv->scrolled.left - wasleft; |
| 432 |
top = tv->scrolled.top - wastop; |
| 433 |
|
| 434 |
if ((top < 0 ? -top : top) > tv->frame.bottom) { |
| 435 |
/* moving a lot so nothing to ScrollRect */ |
| 436 |
TVRedrawRect(tv, &tv->frame, 0, true); |
| 437 |
TVRevealCanvas(tv); |
| 438 |
return; |
| 439 |
} |
| 440 |
|
| 441 |
vacated = NewRgn(); |
| 442 |
ScrollRect(&tv->frame, left, top, vacated); |
| 443 |
tv->canvas_dirty = true; |
| 444 |
HLock(vacated); |
| 445 |
r = (**vacated).rgnBBox; |
| 446 |
HUnlock(vacated); |
| 447 |
DisposeRgn(vacated); |
| 448 |
|
| 449 |
/* only redraw what was vacated, the rest moved with its highlighting */ |
| 450 |
TVRedrawRect(tv, &r, 0, true); |
| 451 |
|
| 452 |
TVRevealCanvas(tv); |
| 453 |
} |
| 454 |
|
| 455 |
void |
| 456 |
TVUseCanvas(struct TVRec *tv) |
| 457 |
{ |
| 458 |
if (++tv->canvas_refcnt != 1) |
| 459 |
return; |
| 460 |
|
| 461 |
if (tv->canvas_refcnt == 0) |
| 462 |
Debugger(); |
| 463 |
|
| 464 |
#ifdef USE_CANVAS |
| 465 |
GetPort(&tv->canvas_old_port); |
| 466 |
|
| 467 |
HLock(tv->canvas_baseaddr); |
| 468 |
tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr); |
| 469 |
|
| 470 |
SetPort(tv->canvas); |
| 471 |
#else |
| 472 |
GetClip(tv->view_clip = NewRgn()); |
| 473 |
SetOrigin(-tv->view.left, -tv->view.top); |
| 474 |
ClipRect(&tv->frame); |
| 475 |
#endif |
| 476 |
} |
| 477 |
|
| 478 |
void |
| 479 |
TVRevealCanvas(struct TVRec *tv) |
| 480 |
{ |
| 481 |
if (tv->canvas_refcnt <= 0) |
| 482 |
Debugger(); |
| 483 |
|
| 484 |
if (--tv->canvas_refcnt != 0) |
| 485 |
return; |
| 486 |
|
| 487 |
#ifdef USE_CANVAS |
| 488 |
SetPort(tv->canvas_old_port); |
| 489 |
|
| 490 |
if (tv->canvas_dirty) |
| 491 |
CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits, |
| 492 |
&tv->canvas->portRect, &tv->view, srcCopy, NULL); |
| 493 |
#else |
| 494 |
SetOrigin(0, 0); |
| 495 |
SetClip(tv->view_clip); |
| 496 |
DisposeRgn(tv->view_clip); |
| 497 |
#endif |
| 498 |
|
| 499 |
ValidRect(&tv->view); |
| 500 |
|
| 501 |
tv->canvas_old_port = NULL; |
| 502 |
tv->canvas_dirty = false; |
| 503 |
#ifdef USE_CANVAS |
| 504 |
HUnlock(tv->canvas_baseaddr); |
| 505 |
#endif |
| 506 |
} |
| 507 |
|
| 508 |
void |
| 509 |
TVRedrawRect(struct TVRec *tv, Rect *r, long startline, Boolean erase) |
| 510 |
{ |
| 511 |
BigRect find; |
| 512 |
Rect clip; |
| 513 |
|
| 514 |
if (!SectRect(r, &tv->frame, &clip)) |
| 515 |
return; |
| 516 |
|
| 517 |
TVUseCanvas(tv); |
| 518 |
ClipRect(&clip); |
| 519 |
|
| 520 |
if (erase) |
| 521 |
EraseRect(&clip); |
| 522 |
|
| 523 |
find.left = clip.left - tv->scrolled.left; |
| 524 |
find.top = clip.top - tv->scrolled.top; |
| 525 |
find.right = clip.right - tv->scrolled.left; |
| 526 |
find.bottom = clip.bottom - tv->scrolled.top; |
| 527 |
TVFindInRectLocked(tv, &find, startline, TVDrawMatch, NULL); |
| 528 |
|
| 529 |
if (tv->active && tv->sel_start != tv->sel_end) |
| 530 |
TVInvertRange(tv, tv->sel_start, tv->sel_end, &clip); |
| 531 |
|
| 532 |
ClipRect(&tv->frame); |
| 533 |
tv->canvas_dirty = true; |
| 534 |
TVRevealCanvas(tv); |
| 535 |
} |
| 536 |
|
| 537 |
void |
| 538 |
TVInvertRange(struct TVRec *tv, unsigned long a, unsigned long b, |
| 539 |
Rect *clip) |
| 540 |
{ |
| 541 |
struct TVLine *line; |
| 542 |
unsigned long nline, cline, t; |
| 543 |
long left, right, top, bottom, edge; |
| 544 |
Boolean chars, trail; |
| 545 |
Rect r; |
| 546 |
|
| 547 |
if (a > b) { |
| 548 |
t = a; |
| 549 |
a = b; |
| 550 |
b = t; |
| 551 |
} |
| 552 |
if (a == b || tv->nlines == 0) |
| 553 |
return; |
| 554 |
|
| 555 |
nline = TVLineForOffset(tv, a); |
| 556 |
if (nline > 1) { |
| 557 |
/* a wrapped line ends where the next begins, include its trail */ |
| 558 |
line = TVGetLine(tv, nline - 1); |
| 559 |
if (line != NULL && line->end >= a) |
| 560 |
nline--; |
| 561 |
} |
| 562 |
|
| 563 |
/* skip lines above the clip */ |
| 564 |
cline = TVLineForY(tv, (long)clip->top - tv->scrolled.top); |
| 565 |
if (cline > nline) |
| 566 |
nline = cline; |
| 567 |
|
| 568 |
edge = tv->frame.right - tv->padding.right; |
| 569 |
|
| 570 |
TVUseCanvas(tv); |
| 571 |
|
| 572 |
for (; nline <= tv->nlines; nline++) { |
| 573 |
line = TVGetLine(tv, nline); |
| 574 |
if (line == NULL || line->start >= b) |
| 575 |
break; |
| 576 |
|
| 577 |
top = tv->scrolled.top + line->rect.top; |
| 578 |
bottom = tv->scrolled.top + line->rect.bottom; |
| 579 |
if (top >= clip->bottom) |
| 580 |
break; |
| 581 |
if (bottom <= clip->top) |
| 582 |
continue; |
| 583 |
|
| 584 |
/* any of this line's characters, then the area past its end */ |
| 585 |
chars = (a < line->end && b > line->start); |
| 586 |
trail = (a <= line->end && b > line->end); |
| 587 |
if (!chars && !trail) |
| 588 |
continue; |
| 589 |
|
| 590 |
if (a <= line->start) |
| 591 |
left = line->rect.left + line->indent; |
| 592 |
else |
| 593 |
left = TVXForOffset(tv, line, a); |
| 594 |
|
| 595 |
if (b >= line->end) |
| 596 |
right = line->rect.right + line->indent; |
| 597 |
else |
| 598 |
right = TVXForOffset(tv, line, b); |
| 599 |
if (trail && right < edge) |
| 600 |
right = edge; |
| 601 |
|
| 602 |
if (right <= left) |
| 603 |
continue; |
| 604 |
|
| 605 |
if (top < clip->top) |
| 606 |
top = clip->top; |
| 607 |
if (bottom > clip->bottom) |
| 608 |
bottom = clip->bottom; |
| 609 |
|
| 610 |
r.left = tv->scrolled.left + left; |
| 611 |
r.top = top; |
| 612 |
r.right = tv->scrolled.left + right; |
| 613 |
r.bottom = bottom; |
| 614 |
if (SectRect(&r, clip, &r)) |
| 615 |
InvertRect(&r); |
| 616 |
} |
| 617 |
|
| 618 |
tv->canvas_dirty = true; |
| 619 |
TVRevealCanvas(tv); |
| 620 |
} |