AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

44

TVAppendLine: Add, reduce unnecessary redrawing in TVUpdateScrollbar

A common pattern is to TVAppend a line from a buffer but then have
to insert manual \r line breaks to skip over \n breaks in a buffer.
To avoid having to TVAppend(line) and TVAppend('\r'), move TVAppend
to TVAppendLocked and take a terminating character, and make
TVAppend pass 0.
 
Now a caller can just do TVAppendLine in a single call and not have
to waste time with all the setup and recalculation in TVAppend just
to append one linebreak.
 
Also do checking in TVUpdateScrollbar to ignore redrawing if the
scroll "elevator" won't be moving, such as when it's pegged to the
top or bottom.

jcs made amendment 44 over 2 years ago
--- textview.c Sun Nov 3 22:08:08 2024 +++ textview.c Fri Nov 8 09:59:28 2024 @@ -53,6 +53,8 @@ struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsign struct TVChunk * TVGetChunk(struct TVRec *tv, TVChunkPtr ptr); struct TVChunk * TVCreateChunk(struct TVRec *tv); struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line); +Boolean TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text, + unsigned long length, char terminator); short TVScrollDivisor(struct TVRec *tv); Boolean TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty); void TVApplyStyle(struct TVStyle *style); @@ -128,6 +130,33 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te unsigned long length) { struct TVRec *tv; + Boolean ret; + + tv = TVLock(tvh); + ret = TVAppendLocked(tv, style, text, length, 0); + TVUnlock(tv); + + return ret; +} + +Boolean +TVAppendLine(TVHandle tvh, struct TVStyle *style, char *text, + unsigned long length) +{ + struct TVRec *tv; + Boolean ret; + + tv = TVLock(tvh); + ret = TVAppendLocked(tv, style, text, length, '\r'); + TVUnlock(tv); + + return ret; +} + +Boolean +TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text, + unsigned long length, char terminator) +{ struct TVSlab *text_slab; struct TVChunk *chunk, *prev_chunk; struct TVStyle *cur_style; @@ -138,19 +167,22 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te unsigned long avail, wastop; short width, prev_style_idx; char *text_slab_data; - Boolean new_chunk, ret; + Boolean new_chunk; - tv = TVLock(tvh); + if (terminator) + length++; + + if (length == 0) + return false; new_chunk = false; - ret = false; fromline = TVGetLine(tv, tv->nlines); /* setup style */ prev_style_idx = tv->cur_style; cur_style = TVFindStyle(tv, style); if (cur_style == NULL) - goto append_done; + return false; if (tv->cur_style != prev_style_idx) new_chunk = true; @@ -162,16 +194,13 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te prev_chunk = NULL; chunk = TVGetChunk(tv, tv->last_chunk); - if (length == 0) - length = strlen(text); - while (length) { if (chunk == NULL || new_chunk) { new_chunk = false; prev_chunk = chunk; chunk = TVCreateChunk(tv); if (chunk == NULL) - goto append_done; + return false; chunk->start = text_slab->base + text_slab->pos; chunk->end = chunk->start; @@ -189,7 +218,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te if ((text_slabh = TVAllocateSlab(tv, TV_TEXT_SLAB_SIZE, &tv->first_text_slab, &tv->last_text_slab)) == NULL) - goto append_done; + return false; text_slab = (struct TVSlab *)*text_slabh; new_chunk = true; @@ -207,15 +236,16 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te tv->text_length += avail; chunk->end += avail; chunk->need_recalc = true; + + if (length == 0 && terminator) + text_slab->data[text_slab->pos - 1] = terminator; } - if (!tv->autocalc) { - ret = true; - goto append_done; - } + if (!tv->autocalc) + return true; if (!TVCalcLinesLocked(tv, false)) - goto append_done; + return false; /* keep scrolling and redrawing in one CopyBits */ TVUseCanvas(tv); @@ -247,11 +277,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te TVDrawMatch, NULL); TVRevealCanvas(tv); - ret = true; - -append_done: - TVUnlock(tv); - return ret; + return true; } Boolean @@ -406,6 +432,7 @@ TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller struct TVRec *tv; long max; RgnHandle rgn; + short wasval, wasmax, val; tv = TVLock(tvh); @@ -416,12 +443,27 @@ TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller */ max = (tv->bounds.bottom - tv->bounds.top - (tv->view.bottom - tv->view.top)) / TVScrollDivisor(tv); + if (max < 0) + max = 0; + + wasval = GetCtlValue(scroller); + HLock(scroller); + wasmax = (*scroller)->contrlMax; (*scroller)->contrlMin = 0; - (*scroller)->contrlMax = (max > 0 ? max : 0); - SetCtlValue(scroller, -(tv->scrolled.top) / TVScrollDivisor(tv)); + val = -(tv->scrolled.top) / TVScrollDivisor(tv); + (*scroller)->contrlMax = max; + SetCtlValue(scroller, val); HUnlock(scroller); - + + if ((wasmax == 0 && max != 0) || (wasmax != 0 && max == 0)) + goto redraw; + if (wasval == 0 && val == 0) + goto done; + if (wasval == wasmax && val == max && tv->autoscroll) + goto done; + +redraw: rgn = NewRgn(); HLock(scroller); RectRgn(rgn, &(*scroller)->contrlRect); @@ -429,7 +471,7 @@ TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller HUnlock(scroller); DisposeRgn(rgn); - HUnlock(scroller); +done: TVUnlock(tv); } --- textview.h Tue Oct 29 13:32:56 2024 +++ textview.h Thu Nov 7 16:39:24 2024 @@ -145,6 +145,8 @@ TVHandle TVNew(Rect *view, Rect *padding); void TVDispose(TVHandle tvh); Boolean TVAppend(TVHandle tvh, struct TVStyle *style, char *text, unsigned long length); +Boolean TVAppendLine(TVHandle tvh, struct TVStyle *style, char *text, + unsigned long length); void TVAutoCalc(TVHandle tvh, Boolean calc); void TVAutoScroll(TVHandle tvh, Boolean scroll); void TVLineWrap(TVHandle tvh, Boolean wrap);