AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

9

textview: Do everything in the shadow buffer


jcs made amendment 9 over 2 years ago
--- textview.c Wed Dec 27 14:56:55 2023 +++ textview.c Wed Dec 27 23:01:23 2023 @@ -8,12 +8,13 @@ #define IS_FIRST_EMPTY_CHUNK(tv) ((tv)->cur_chunk->style.size == 0) struct TVChunk * TVCreateChunk(struct TVRec *tv, unsigned long length); -void TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk); struct TVChunk * TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length); void TVApplyStyle(struct TVStyle *style); void TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off); +void TVBeginShadow(struct TVRec *tv); +void TVEndShadow(struct TVRec *tv); struct TVRec * TVNew(Rect *view, Rect *padding) @@ -31,8 +32,7 @@ TVNew(Rect *view, Rect *padding) tv->padding = *padding; tv->padding.left++; - tv->scrollRect.right = tv->view.right - tv->view.left - - tv->padding.left - tv->padding.right; + tv->scrollRect.right = tv->view.right - tv->view.left; tv->shadow.bounds.right = tv->view.right - tv->view.left; tv->shadow.bounds.bottom = tv->view.bottom - tv->view.top; @@ -50,6 +50,8 @@ TVNew(Rect *view, Rect *padding) tv->cur_chunk->free_length = CHUNK_ALLOC_SIZE - size; tv->cur_chunk->line = 1; + tv->cur_chunk->rect.top = tv->padding.top; + tv->cur_chunk->rect.left = tv->padding.left; tv->last_chunk = tv->cur_chunk; return tv; @@ -125,7 +127,7 @@ TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, tv->last_chunk = new_chunk; } - TVClearChunkRect(tv, chunk); + EraseRect(&chunk->rect); new_chunk->length = chunk->length - length; memcpy(new_chunk->text, chunk->text + length, @@ -167,8 +169,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char char c; Boolean new_chunk, new_line; - GetClip(old_rgn = NewRgn()); - ClipRect(&tv->view); + TVBeginShadow(tv); TVApplyStyle(style); GetFontInfo(&style->font_info); @@ -186,7 +187,8 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char } else { width = TextWidth(text, n, 1); - if (width + line_right > tv->scrollRect.right && c != ' ') { + if (c != ' ' && width + line_right > + tv->scrollRect.right - tv->padding.right) { new_chunk = true; new_line = true; @@ -240,7 +242,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char if (tchunk->line != tv->cur_chunk->line) break; - TVClearChunkRect(tv, tchunk); + EraseRect(&tchunk->rect); tchunk->baseline = tv->cur_chunk->baseline; TVDrawText(tv, tchunk, 0); @@ -258,7 +260,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char tv->cur_chunk->rect.top = tv->cur_chunk->rect.bottom = tv->cur_chunk->prev_chunk->rect.bottom + tv->cur_chunk->prev_chunk->style.font_info.leading; - tv->cur_chunk->rect.left = 0; + tv->cur_chunk->rect.left = tv->padding.left; tv->cur_chunk->rect.right = line_right; } @@ -289,8 +291,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char tv->scrollRect.bottom = tv->cur_chunk->rect.bottom + tv->scrollRect.top; - SetClip(old_rgn); - DisposeRgn(old_rgn); + TVEndShadow(tv); } void @@ -308,18 +309,16 @@ TVUpdateScrollbar(struct TVRec *tv, ControlHandle scro * redraw again, which can cause a jump if we're trying to keep the * scrollbar position in the same place (like at the bottom). */ - (*scroller)->contrlMax = tv->scrollRect.bottom - tv->scrollRect.top + - tv->padding.bottom - (tv->view.bottom - tv->view.top); + (*scroller)->contrlMax = tv->scrollRect.bottom - tv->scrollRect.top - + (tv->view.bottom - tv->view.top); SetCtlValue(scroller, -(tv->scrollRect.top)); -#if 1 rgn = NewRgn(); RectRgn(rgn, &(*scroller)->contrlRect); UpdtControl((*(scroller))->contrlOwner, rgn); CloseRgn(rgn); DisposeRgn(rgn); -#endif HUnlock(scroller); } @@ -344,7 +343,7 @@ TVTrackScrollControl(ControlHandle control, short part struct TVChunk *chunk, *top_chunk = NULL; BigRect visRect; short page, val, adj, per_line; - long n, visible, height; + long n, visible, height, top; Boolean horiz; if (tv_track_control == NULL) @@ -360,14 +359,13 @@ TVTrackScrollControl(ControlHandle control, short part for (chunk = (struct TVChunk *)tv->last_chunk; chunk != NULL; chunk = chunk->prev_chunk) { if (chunk->rect.bottom - ((chunk->rect.bottom - - chunk->rect.top) / 2) + tv->scrollRect.top > 0) + chunk->rect.top) / 2) > 0) /* already visible */ continue; if (chunk->prev_chunk) /* scroll to make this line entirely visible at the top */ - TVScroll(tv, 0, tv->scrollRect.top + chunk->rect.top + - tv->padding.top); + TVScroll(tv, 0, tv->scrollRect.top + chunk->rect.top); else /* close enough, just scroll to the top */ TVScroll(tv, 0, tv->scrollRect.top); @@ -381,19 +379,20 @@ TVTrackScrollControl(ControlHandle control, short part adj = -page; break; case inDownButton: - height = tv->view.top + tv->padding.top + tv->scrollRect.top; + height = tv->view.bottom - tv->view.top - 2; for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; chunk = chunk->next_chunk) { - if (height + chunk->rect.top + - ((chunk->rect.bottom - chunk->rect.top) / 2) < - tv->view.bottom - 2) + top = chunk->rect.top + (chunk->baseline / 2) + + tv->scrollRect.top; + + if (top < height) /* already visible */ continue; /* scroll to make this line entirely visible at the bottom */ - TVScroll(tv, 0, tv->scrollRect.top + chunk->rect.bottom - - (tv->view.bottom - tv->view.top - 2 - tv->padding.top)); + TVScroll(tv, 0, -((chunk->rect.bottom - height) + + tv->scrollRect.top)); break; } break; @@ -426,14 +425,16 @@ update_value: void TVScroll(struct TVRec *tv, long horiz, long vert) { - tv->scrollRect.top -= vert; - tv->scrollRect.bottom -= vert; + tv->scrollRect.top += vert; + tv->scrollRect.bottom += vert; - tv->scrollRect.left -= horiz; - tv->scrollRect.right -= horiz; + tv->scrollRect.left += horiz; + tv->scrollRect.right += horiz; /* TODO: move onscreen rect and only redraw new/partially obscured * chunks */ + memset(tv->shadow.baseAddr, 0, (size_t)tv->shadow.rowBytes * + (size_t)tv->shadow.bounds.bottom); TVUpdate(tv); } @@ -443,81 +444,69 @@ TVDrawText(struct TVRec *tv, struct TVChunk *chunk, un Point p; long l, b; - l = tv->padding.left + tv->scrollRect.left; - - if (off == 0) - l += chunk->rect.left; - else + if (off != 0) panic("need to implement redrawing a partial string"); + + l = chunk->rect.left + tv->scrollRect.left; + b = chunk->rect.top + chunk->baseline + tv->scrollRect.top; - b = tv->padding.top + tv->scrollRect.top + - chunk->rect.top + chunk->baseline; - - if (thePort->portBits.baseAddr != tv->shadow.baseAddr) { - l += tv->view.left; - b += tv->view.top; - } - MoveTo(l, b); TVApplyStyle(&chunk->style); DrawText(chunk->text, off, chunk->length - off); GetPen(&p); - chunk->rect.right = p.h - (tv->view.left + tv->padding.left + - tv->scrollRect.left); + chunk->rect.right = p.h - tv->scrollRect.left; chunk->rect.bottom = chunk->rect.top + chunk->baseline + chunk->style.font_info.descent; + #if 0 - FrameRect(&chunk->rect); + TVEndShadow(tv); + TVBeginShadow(tv); #endif } void -TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk) +TVUpdate(struct TVRec *tv) { - Rect r; - long o; + struct TVChunk *chunk; + long height; - o = tv->view.top + tv->scrollRect.top; - r.top = o + chunk->rect.top; - r.bottom = o + chunk->rect.bottom; + TVBeginShadow(tv); - o = tv->view.left + tv->scrollRect.left; - r.left = chunk->rect.left + o; - r.right = chunk->rect.right + o; + height = tv->view.bottom - tv->view.top - 2; - EraseRect(&r); + for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; + chunk = chunk->next_chunk) { + if (chunk->rect.bottom < -(tv->scrollRect.top)) + continue; + if (chunk->rect.top + tv->scrollRect.top > height) + break; + TVDrawText(tv, chunk, 0); + } + + TVEndShadow(tv); } + void -TVUpdate(struct TVRec *tv) +TVBeginShadow(struct TVRec *tv) { struct TVChunk *chunk; - BitMap old_bits; - RgnHandle old_rgn; - Rect r; - old_bits = thePort->portBits; + tv->port_bits = thePort->portBits; - /* swap to off-screen bits */ SetPortBits(&tv->shadow); - EraseRect(&tv->shadow.bounds); - - GetClip(old_rgn = NewRgn()); + GetClip(tv->port_region = NewRgn()); ClipRect(&tv->shadow.bounds); +} + +void +TVEndShadow(struct TVRec *tv) +{ + SetClip(tv->port_region); + DisposeRgn(tv->port_region); - for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; - chunk = chunk->next_chunk) { - if (chunk->rect.bottom < tv->scrollRect.top || - chunk->rect.top > tv->scrollRect.bottom) - continue; - TVDrawText(tv, chunk, 0); - } - - SetClip(old_rgn); - DisposeRgn(old_rgn); - - SetPortBits(&old_bits); - CopyBits(&tv->shadow, &old_bits, &tv->shadow.bounds, &tv->view, + SetPortBits(&tv->port_bits); + CopyBits(&tv->shadow, &tv->port_bits, &tv->shadow.bounds, &tv->view, srcCopy, NULL); } --- textview.h Mon Dec 18 13:42:33 2023 +++ textview.h Wed Dec 27 15:16:47 2023 @@ -30,7 +30,8 @@ struct TVRec { Rect padding; BitMap shadow; BigRect scrollRect; - RgnHandle region; + BitMap port_bits; + RgnHandle port_region; struct TVChunk *cur_chunk; struct TVChunk *last_chunk; Ptr chunks;