AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

14

Allocate a whole GrafPort for canvas, don't just swap bits

Macintosh Technical Note 41 says to do this because it can maintain
its own visRgn. Otherwise sometimes drawing on the canvas is clipped
and it doesn't clear properly.

jcs made amendment 14 over 2 years ago
--- textview.c Mon Oct 7 12:47:05 2024 +++ textview.c Mon Oct 7 16:05:19 2024 @@ -39,15 +39,14 @@ struct TVRec * TVNew(Rect *view, Rect *padding) { struct TVRec *tv; - unsigned long size; long width, height; + GrafPtr old_port; - size = sizeof(struct TVRec) + sizeof(struct TVStyle); - tv = malloc(CHUNK_ALLOC_SIZE); + tv = xmalloc(CHUNK_ALLOC_SIZE); if (tv == NULL) panic("malloc(%ul) failed", (unsigned long)CHUNK_ALLOC_SIZE); - - memset(tv, 0, sizeof(struct TVRec)); + + memset(tv, 0, sizeof(struct TVRec) + sizeof(struct TVChunk)); tv->view = *view; tv->view.left++; tv->view.top++; @@ -63,15 +62,28 @@ TVNew(Rect *view, Rect *padding) width = tv->view.right - tv->view.left; height = tv->view.bottom - tv->view.top; - tv->view_height = height; - tv->canvas.bounds.right = width; - tv->canvas.bounds.bottom = height; - tv->canvas.rowBytes = (((width - 1) / 16) + 1) * 2; - tv->canvas_size = (long)tv->canvas.rowBytes * height; - tv->canvas.baseAddr = xmalloczero(tv->canvas_size); - if (tv->canvas.baseAddr == NULL) + tv->canvas = xmalloczero(sizeof(GrafPort)); + if (tv->canvas == NULL) panic("malloc"); + GetPort(&old_port); + OpenPort(tv->canvas); + tv->canvas->portRect.right = width; + tv->canvas->portRect.bottom = height; + + tv->canvas->portBits.bounds = tv->canvas->portRect; + tv->canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2; + tv->canvas_size = (long)tv->canvas->portBits.rowBytes * height; + tv->canvas->portBits.baseAddr = xmalloczero(tv->canvas_size); + if (tv->canvas->portBits.baseAddr == NULL) + panic("malloc(%lu)", tv->canvas_size); + + RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect); + RectRgn(tv->canvas->visRgn, &tv->canvas->portRect); + SetPort(old_port); + + tv->view_height = height; + tv->bounds.right = width; tv->bounds.bottom = tv->padding.top + tv->padding.bottom; tv->scrolled = tv->bounds; @@ -79,19 +91,20 @@ TVNew(Rect *view, Rect *padding) /* create an initial blank chunk */ tv->chunks = (Ptr)tv + sizeof(struct TVRec); tv->cur_chunk = (struct TVChunk *)tv->chunks; - memset(tv->cur_chunk, 0, sizeof(struct TVChunk)); - tv->cur_chunk->free_length = CHUNK_ALLOC_SIZE - size; + tv->cur_chunk->free_length = CHUNK_ALLOC_SIZE - + (sizeof(struct TVRec) + sizeof(struct TVStyle)); tv->cur_chunk->line = 1; tv->cur_chunk->rect.top = tv->padding.top; tv->cur_chunk->rect.left = tv->padding.left; tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top; tv->cur_chunk->rect.right = tv->cur_chunk->rect.left; tv->last_chunk = tv->cur_chunk; + tv->first_chunk = tv->cur_chunk; #ifdef DEBUG_DRAWING - EraseRect(&tv->canvas.bounds); - FrameRect(&tv->canvas.bounds); + EraseRect(&tv->canvas->portRect); + FrameRect(&tv->canvas->portRect); #endif return tv; @@ -102,9 +115,11 @@ TVDispose(struct TVRec **tvp) { struct TVRec *tv = (struct TVRec *)*tvp; - if (tv->canvas.baseAddr) - xfree(&tv->canvas.baseAddr); - + TVClear(tv); + ClosePort(tv->canvas); + xfree(&tv->canvas); + xfree(&tv->chunks); + xfree(&tv->canvas->portBits.baseAddr); xfree(tvp); } @@ -120,10 +135,11 @@ TVCreateChunk(struct TVRec *tv, unsigned long length) if (length + sizeof(struct TVChunk) > tv->cur_chunk->free_length) { /* allocate new buffer */ - chunk = malloc(CHUNK_ALLOC_SIZE); + chunk = xmalloczero(CHUNK_ALLOC_SIZE); if (chunk == NULL) /* TODO: try to allocate a smaller chunk */ panic("malloc(%ld) failed", (unsigned long)CHUNK_ALLOC_SIZE); + chunk->mallocked = true; } else { /* create new chunk in existing alloc */ addr = (unsigned long)tv->cur_chunk + sizeof(struct TVChunk) + @@ -407,6 +423,7 @@ TVDrawText(struct TVRec *tv, struct TVChunk *chunk, un b = tv->scrolled.top + chunk->rect.top + chunk->baseline; MoveTo(l, b); TVApplyStyle(&chunk->style); + DrawText(chunk->text, off, chunk->length - off); } @@ -462,8 +479,8 @@ return; p.v -= tv->view.top; p.h -= tv->view.left; - p.v += -(tv->canvas.bounds.top); - p.h += -(tv->canvas.bounds.left); + p.v += -(tv->canvas->portRect.top); + p.h += -(tv->canvas->portRect.left); /* StillDown() then WaitMouseUp() */ @@ -613,13 +630,7 @@ TVScroll(struct TVRec *tv, short horiz, short vert) vacated = NewRgn(); left = tv->scrolled.left - was.left; top = tv->scrolled.top - was.top; -#ifdef DEBUG_DRAWING - InsetRect(&tv->canvas.bounds, 1, 1); -#endif - ScrollRect(&tv->canvas.bounds, left, top, vacated); -#ifdef DEBUG_DRAWING - InsetRect(&tv->canvas.bounds, -1, -1); -#endif + ScrollRect(&tv->canvas->portRect, left, top, vacated); rvacated = tv->scrolled; rvacated.bottom = top; @@ -638,6 +649,47 @@ TVScroll(struct TVRec *tv, short horiz, short vert) } void +TVClear(struct TVRec *tv) +{ + struct TVChunk *tchunk, *last_mallocked = NULL; + + /* free all malloced chunks except first one which contains tv */ + for (tchunk = tv->first_chunk; tchunk != NULL; + tchunk = tchunk->next_chunk) { + if (tchunk->mallocked) { + if (last_mallocked) + xfree(&last_mallocked); + last_mallocked = tchunk; + } + } + if (last_mallocked) + xfree(&last_mallocked); + + memset(tv + sizeof(struct TVRec), 0xff, + CHUNK_ALLOC_SIZE - sizeof(struct TVRec)); + + tv->chunks = (Ptr)tv + sizeof(struct TVRec); + tv->cur_chunk = (struct TVChunk *)tv->chunks; + memset(tv->cur_chunk, 0, sizeof(struct TVChunk)); + + tv->cur_chunk->free_length = CHUNK_ALLOC_SIZE - + (sizeof(struct TVRec) + sizeof(struct TVStyle)); + tv->cur_chunk->line = 1; + tv->cur_chunk->rect.top = tv->padding.top; + tv->cur_chunk->rect.left = tv->padding.left; + tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top; + tv->cur_chunk->rect.right = tv->cur_chunk->rect.left; + + tv->bounds.bottom = tv->padding.top + tv->padding.bottom; + tv->scrolled = tv->bounds; + memset(&tv->last_style, 0, sizeof(tv->last_style)); + + TVDrawOnCanvas(tv); + EraseRect(&tv->canvas->portRect); + TVShowCanvas(tv); +} + +void TVUpdate(struct TVRec *tv) { struct TVChunk *chunk; @@ -667,10 +719,8 @@ TVDrawOnCanvas(struct TVRec *tv) return; #endif - GetClip(tv->port_region = NewRgn()); - tv->canvas_cur_bits = thePort->portBits; - SetPortBits(&tv->canvas); - ClipRect(&tv->canvas.bounds); + GetPort(&tv->canvas_old_port); + SetPort(tv->canvas); } void @@ -686,18 +736,12 @@ TVShowCanvas(struct TVRec *tv) return; #endif - SetPortBits(&tv->canvas_cur_bits); - SetClip(tv->port_region); - if (tv->canvas_refcnt == 0) - DisposeRgn(tv->port_region); + SetPort(tv->canvas_old_port); - CopyBits(&tv->canvas, &tv->canvas_cur_bits, &tv->canvas.bounds, - &tv->view, srcCopy, NULL); + CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits, + &tv->canvas->portRect, &tv->view, srcCopy, NULL); - if (tv->canvas_refcnt != 0) { - SetPortBits(&tv->canvas); - ClipRect(&tv->canvas.bounds); - } - ValidRect(&tv->view); + + tv->canvas_old_port = NULL; } --- textview.h Sat Oct 5 22:53:11 2024 +++ textview.h Mon Oct 7 16:06:12 2024 @@ -48,6 +48,7 @@ struct TVChunk { struct TVStyle style; unsigned long baseline; BigRect rect; + bool mallocked; char text[]; }; @@ -60,19 +61,19 @@ struct TVRec { /* padding into bounds where we should start text */ Rect padding; /* off-screen canvas where we draw text that is visible */ - BitMap canvas; + GrafPtr canvas; short canvas_refcnt; /* bytes allocated for canvas backing */ size_t canvas_size; /* while drawing on canvas, this stores old thePort */ - BitMap canvas_cur_bits; - RgnHandle port_region; + GrafPtr canvas_old_port; /* bounds as it is scrolled */ BigRect scrolled; struct TVStyle last_style; char width_cache[256]; bool autoscroll; + struct TVChunk *first_chunk; struct TVChunk *cur_chunk; struct TVChunk *last_chunk; struct TVChunk *sel_start; @@ -89,6 +90,7 @@ void TVUpdate(struct TVRec *tv); void TVUpdateScrollbar(struct TVRec *tv, ControlHandle scroller); void TVClick(struct TVRec *tv, Point p, bool extend); void TVScroll(struct TVRec *tv, short horiz, short vert); +void TVClear(struct TVRec *tv); void TVSetTrackScrollControl(struct TVRec *tv); pascal void TVTrackScrollControl(ControlHandle control, short part);