jcs
/textview
/amendments
/7
textview: Per-line scrolling and less-flickery redrawing
jcs made amendment 7 over 2 years ago
--- textview.c Sat Dec 16 23:23:20 2023
+++ textview.c Wed Dec 27 14:56:55 2023
@@ -34,6 +34,15 @@ TVNew(Rect *view, Rect *padding)
tv->scrollRect.right = tv->view.right - tv->view.left -
tv->padding.left - tv->padding.right;
+ tv->shadow.bounds.right = tv->view.right - tv->view.left;
+ tv->shadow.bounds.bottom = tv->view.bottom - tv->view.top;
+
+ tv->shadow.rowBytes = (((tv->shadow.bounds.right - 1) / 16) + 1) * 2;
+ tv->shadow.baseAddr = xmalloczero((size_t)tv->shadow.rowBytes *
+ (size_t)tv->shadow.bounds.bottom);
+ if (tv->shadow.baseAddr == NULL)
+ panic("malloc");
+
/* create an initial blank chunk */
tv->chunks = (Ptr)tv + sizeof(struct TVRec);
tv->cur_chunk = (struct TVChunk *)tv->chunks;
@@ -46,6 +55,17 @@ TVNew(Rect *view, Rect *padding)
return tv;
}
+void
+TVDispose(struct TVRec **tvp)
+{
+ struct TVRec *tv = (struct TVRec *)*tvp;
+
+ if (tv->shadow.baseAddr)
+ xfree(&tv->shadow.baseAddr);
+
+ xfree(tvp);
+}
+
struct TVChunk *
TVCreateChunk(struct TVRec *tv, unsigned long length)
{
@@ -423,16 +443,21 @@ TVDrawText(struct TVRec *tv, struct TVChunk *chunk, un
Point p;
long l, b;
- l = tv->view.left + tv->padding.left + tv->scrollRect.left;
+ l = tv->padding.left + tv->scrollRect.left;
if (off == 0)
l += chunk->rect.left;
else
panic("need to implement redrawing a partial string");
- b = tv->view.top + tv->padding.top + 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);
@@ -468,18 +493,31 @@ void
TVUpdate(struct TVRec *tv)
{
struct TVChunk *chunk;
+ BitMap old_bits;
RgnHandle old_rgn;
-
- EraseRect(&tv->view);
+ Rect r;
+ old_bits = thePort->portBits;
+
+ /* swap to off-screen bits */
+ SetPortBits(&tv->shadow);
+ EraseRect(&tv->shadow.bounds);
+
GetClip(old_rgn = NewRgn());
- ClipRect(&tv->view);
+ ClipRect(&tv->shadow.bounds);
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,
+ srcCopy, NULL);
}
--- textview.h Sat Dec 16 16:00:51 2023
+++ textview.h Mon Dec 18 13:42:33 2023
@@ -28,6 +28,7 @@ struct TVChunk {
struct TVRec {
Rect view;
Rect padding;
+ BitMap shadow;
BigRect scrollRect;
RgnHandle region;
struct TVChunk *cur_chunk;
@@ -36,6 +37,7 @@ struct TVRec {
};
struct TVRec * TVNew(Rect *view, Rect *padding);
+void TVDispose(struct TVRec **tvp);
void TVAppend(struct TVRec *tv, struct TVStyle *style, char *text,
unsigned long length);
void TVUpdate(struct TVRec *tv);