AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

30

Do autoscroll from TVAppend, pass start line hint to TVFindInRect


jcs made amendment 30 over 2 years ago
--- textview.c Mon Oct 21 15:27:03 2024 +++ textview.c Mon Oct 21 22:14:53 2024 @@ -58,7 +58,7 @@ struct TVStyle * TVFindStyle(struct TVRec *tv, struct void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert); Boolean TVGetOffsetCallback(struct TVRec *tv, struct TVFindInRectMatch *match, void *cookie); -Boolean TVFindInRect(struct TVRec *tv, BigRect *findrect, +Boolean TVFindInRect(struct TVRec *tv, BigRect *findrect, short startline, TVFindInRectCallback callback, void *cookie); Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match, void *cookie); @@ -160,8 +160,10 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te struct TVSlab *text_slab; struct TVChunk *chunk, *prev_chunk; struct TVStyle *cur_style; + struct TVLine *fromline, *toline; + RgnHandle vacated; BigRect visible; - unsigned long avail; + unsigned long avail, wastop; short width, prev_style_idx; char *text_slab_data; Boolean new_chunk, ret; @@ -170,6 +172,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te new_chunk = false; ret = false; + fromline = TVGetLine(tv, tv->nlines); /* setup style */ prev_style_idx = tv->cur_style; @@ -228,18 +231,40 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te length -= avail; tv->text_length += avail; chunk->end += avail; - chunk->dirty = true; + chunk->need_recalc = true; } - - if (TVCalcLinesLocked(tv)) { - visible.top = -(tv->scrolled.top); - visible.left = -(tv->scrolled.left); - visible.bottom = visible.top + tv->view_height; - visible.right = visible.left + (tv->view.right - tv->view.left); - TVFindInRect(tv, &visible, TVDrawMatch, NULL); - ret = true; + + if (!TVCalcLinesLocked(tv)) + goto append_done; + + /* keep scrolling and redrawing in one CopyBits */ + TVUseCanvas(tv); + + if (tv->autoscroll && tv->scrolled.bottom > tv->view_height) { + wastop = tv->scrolled.top; + tv->scrolled.bottom = tv->view_height; + tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; + vacated = NewRgn(); + ScrollRect(&tv->canvas->portRect, 0, tv->scrolled.top - wastop, + vacated); + DisposeRgn(vacated); } + if (fromline && fromline->nline == tv->nlines) + toline = fromline; + else + toline = TVGetLine(tv, tv->nlines); + + visible.top = fromline ? fromline->rect.top : tv->padding.top; + visible.bottom = toline->rect.bottom; + visible.left = 0; + visible.right = tv->view.right - tv->view.left; + TVFindInRect(tv, &visible, fromline ? fromline->nline : 1, TVDrawMatch, + NULL); + + TVRevealCanvas(tv); + ret = true; + append_done: TVUnlock(tv); return ret; @@ -379,7 +404,7 @@ TVGetOffset(TVHandle tvh, Point p) prect.bottom = prect.top; off = 0; - TVFindInRect(tv, &prect, TVGetOffsetCallback, &off); + TVFindInRect(tv, &prect, 1, TVGetOffsetCallback, &off); TVRevealCanvas(tv); TVUnlock(tv); @@ -401,7 +426,7 @@ TVTrackScrollControl(ControlHandle control, short part struct TVLine *line, *oline; KeyMap km; long n, top; - unsigned short linen; + unsigned short nline; Boolean horiz, cmd_down; if (tv_track_control == NULL) { @@ -574,7 +599,6 @@ TVUpdate(TVHandle tvh) { struct TVRec *tv; BigRect visible; - Boolean ignore = true; tv = TVLock(tvh); TVUseCanvas(tv); @@ -584,7 +608,7 @@ TVUpdate(TVHandle tvh) visible.left = -(tv->scrolled.left); visible.bottom = visible.top + tv->view_height; visible.right = visible.left + (tv->view.right - tv->view.left); - TVFindInRect(tv, &visible, TVDrawMatch, &ignore); + TVFindInRect(tv, &visible, 1, TVDrawMatch, NULL); TVRevealCanvas(tv); TVUnlock(tv); @@ -885,7 +909,7 @@ TVCalcLinesLocked(struct TVRec *tv) struct TVChunk *chunk, *tchunk, *last_chunk, *prev_chunk, *next_chunk; struct TVStyle *style; struct TVLine *line, *prev_line, *next_line; - unsigned long avail, start, linen, last_space, max_right; + unsigned long avail, start, nline, last_space, max_right; long n, back_width, back_n; short width, space_width, height; char *text_slab_data; @@ -897,7 +921,7 @@ TVCalcLinesLocked(struct TVRec *tv) chunk = last_chunk; while (chunk != NULL) { prev_chunk = TVGetChunk(tv, chunk->prev_chunk); - if (!prev_chunk || !prev_chunk->dirty) + if (!prev_chunk || !prev_chunk->need_recalc) break; chunk = prev_chunk; } @@ -934,17 +958,17 @@ TVCalcLinesLocked(struct TVRec *tv) return false; text_slab_data = *(text_slab->data); - linen = chunk->end_line; - line = TVGetLine(tv, linen); + nline = chunk->end_line; + line = TVGetLine(tv, nline); if (line == NULL) return false; - if (linen == 1) { + if (nline == 1) { line->rect.left = tv->padding.left; line->rect.top = tv->padding.top; line->start = 0; start = line->end; } else { - prev_line = TVGetLine(tv, linen - 1); + prev_line = TVGetLine(tv, nline - 1); if (prev_line == NULL) return false; line->rect.top = prev_line->rect.bottom; @@ -954,6 +978,7 @@ TVCalcLinesLocked(struct TVRec *tv) start = prev_line->end; } + line->nline = nline; new_line = false; last_space = 0; height = style->font_info.ascent + style->font_info.descent; @@ -1002,12 +1027,13 @@ TVCalcLinesLocked(struct TVRec *tv) } else if (height > line->rect.bottom - line->rect.top) line->rect.bottom = line->rect.bottom + height; - linen++; - chunk->end_line = linen; + nline++; + chunk->end_line = nline; chunk->end_right = tv->padding.left; - next_line = TVGetLine(tv, linen); + next_line = TVGetLine(tv, nline); if (next_line == NULL) return false; + next_line->nline = nline; next_line->start = n; if (c == '\r') next_line->start++; @@ -1032,14 +1058,14 @@ TVCalcLinesLocked(struct TVRec *tv) line->end = chunk->end; line->rect.right = chunk->end_right; - chunk->end_line = linen; - chunk->dirty = false; + chunk->end_line = nline; + chunk->need_recalc = false; } done_chunks: - tv->nlines = linen; + tv->nlines = nline; - line = TVGetLine(tv, linen); + line = TVGetLine(tv, nline); if (line == NULL) return false; @@ -1048,11 +1074,6 @@ done_chunks: tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom; } - if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) { - tv->scrolled.bottom = tv->view_height; - tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; - } - return true; } @@ -1060,13 +1081,6 @@ Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match, void *cookie) { - Boolean ignore_dirty = false; - - if (cookie != NULL) - ignore_dirty = *((Boolean *)cookie); - - if (match->chunk->dirty && !ignore_dirty) - return true; if (match->text_end == match->text_start) return true; @@ -1079,11 +1093,11 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch } Boolean -TVFindInRect(struct TVRec *tv, BigRect *rect, TVFindInRectCallback callback, - void *cookie) +TVFindInRect(struct TVRec *tv, BigRect *rect, short startline, + TVFindInRectCallback callback, void *cookie) { struct TVFindInRectMatch match; - unsigned short linen; + unsigned short nline; Boolean ret; TVUseCanvas(tv); @@ -1092,8 +1106,8 @@ TVFindInRect(struct TVRec *tv, BigRect *rect, TVFindIn match.chunk = NULL; ret = false; - for (linen = 1; linen <= tv->nlines; linen++) { - match.line = TVGetLine(tv, linen); + for (nline = startline; nline <= tv->nlines; nline++) { + match.line = TVGetLine(tv, nline); if (match.line == NULL) goto draw_done; @@ -1106,14 +1120,14 @@ TVFindInRect(struct TVRec *tv, BigRect *rect, TVFindIn break; /* advance chunk if we need to catch up to this line */ - if (match.chunk == NULL || match.chunk->end_line < linen) { + if (match.chunk == NULL || match.chunk->end_line < nline) { if (match.chunk == NULL) { match.chunk = TVGetChunk(tv, tv->first_chunk); if (match.chunk == NULL) goto draw_done; } - while (match.chunk->end_line < linen || + while (match.chunk->end_line < nline || match.chunk->end < match.line->start) { match.chunk = TVGetChunk(tv, match.chunk->next_chunk); if (!match.chunk) @@ -1154,7 +1168,7 @@ prep_chunk: if (!callback(tv, &match, cookie)) goto draw_done; - if (match.chunk->end_line == linen && + if (match.chunk->end_line == nline && match.chunk->end < match.line->end) { /* more chunks on this line */ match.chunk = TVGetChunk(tv, match.chunk->next_chunk); @@ -1180,14 +1194,12 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short BigRect visible; RgnHandle vacated; char *text_slab_data; - Boolean ignore; if (!horiz && !vert) return; TVUseCanvas(tv); - ignore = true; wastop = tv->scrolled.top; wasleft = tv->scrolled.left; @@ -1212,13 +1224,12 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short top = tv->scrolled.top - wastop; if ((top < 0 ? -top : top) > tv->view_height) { - /* no point in scrolling, we have to redraw the whole view */ EraseRect(&tv->canvas->portRect); visible.top = -(tv->scrolled.top); visible.left = -(tv->scrolled.left); visible.bottom = visible.top + tv->view_height; visible.right = visible.left + (tv->view.right - tv->view.left); - TVFindInRect(tv, &visible, TVDrawMatch, &ignore); + TVFindInRect(tv, &visible, 1, TVDrawMatch, NULL); TVRevealCanvas(tv); return; } @@ -1233,7 +1244,7 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short HUnlock(vacated); DisposeRgn(vacated); - TVFindInRect(tv, &visible, TVDrawMatch, &ignore); + TVFindInRect(tv, &visible, 1, TVDrawMatch, NULL); TVRevealCanvas(tv); } --- textview.h Mon Oct 21 13:29:29 2024 +++ textview.h Mon Oct 21 20:18:31 2024 @@ -63,15 +63,15 @@ struct TVChunk { unsigned short start_left; unsigned short end_right; unsigned char style; - Boolean dirty; + Boolean need_recalc; }; struct TVLine { unsigned long start; unsigned long end; BigRect rect; + unsigned short nline; unsigned char baseline; - Boolean dirty; }; struct TVSlab {