AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

16

Rewrite to use Handles


jcs made amendment 16 over 2 years ago
--- textview.c Tue Oct 8 09:17:51 2024 +++ textview.c Thu Oct 10 09:57:17 2024 @@ -18,35 +18,41 @@ #include <stddef.h> #include <string.h> #include "textview.h" -#include "util.h" #define CHUNK_ALLOC_SIZE (16 * 1024) -#define IS_FIRST_EMPTY_CHUNK(tv) ((tv)->cur_chunk->style.size == 0) +#define IS_FIRST_EVER_CHUNK(chunk) ((chunk)->ptr == 0) //#define DEBUG_DRAWING 1 +struct TVRec * TVLock(TVHandle tvh); +void TVUnlock(struct TVRec *); +struct TVChunk * TVGetChunk(struct TVRec *tv, TVChunkPtr ptr); struct TVChunk * TVCreateChunk(struct TVRec *tv, unsigned long length); struct TVChunk * TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length); void TVApplyStyle(struct TVStyle *style); void TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk); -void TVDrawText(struct TVRec *tv, struct TVChunk *chunk, - unsigned long off); +void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert); +void TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off); void TVDrawOnCanvas(struct TVRec *tv); void TVShowCanvas(struct TVRec *tv); -struct TVRec * +TVHandle TVNew(Rect *view, Rect *padding) { + TVHandle tvh; struct TVRec *tv; + struct TVChunk *chunk; long width, height; GrafPtr old_port; - tv = xmalloc(CHUNK_ALLOC_SIZE); - if (tv == NULL) - panic("malloc(%ul) failed", (unsigned long)CHUNK_ALLOC_SIZE); + tvh = (TVHandle)NewHandleClear(sizeof(struct TVRec)); + if (tvh == NULL) + return NULL; - memset(tv, 0, sizeof(struct TVRec) + sizeof(struct TVChunk)); + HLock(tvh); + tv = *tvh; + tv->view = *view; tv->view.left++; tv->view.top++; @@ -62,25 +68,29 @@ TVNew(Rect *view, Rect *padding) width = tv->view.right - tv->view.left; height = tv->view.bottom - tv->view.top; - 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); + /* opening the port will reset portRect/portBits */ + 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_baseaddr = NewHandleClear(tv->canvas_size); + if (tv->canvas_baseaddr == NULL) { + DisposHandle(tvh); + return NULL; + } + HLock(tv->canvas_baseaddr); + tv->canvas.portBits.baseAddr = *(tv->canvas_baseaddr); + + RectRgn(tv->canvas.clipRgn, &tv->canvas.portRect); + RectRgn(tv->canvas.visRgn, &tv->canvas.portRect); + SetPort(old_port); + HUnlock(tv->canvas_baseaddr); tv->view_height = height; @@ -89,181 +99,65 @@ TVNew(Rect *view, Rect *padding) tv->scrolled = tv->bounds; /* create an initial blank chunk */ - tv->chunks = (Ptr)tv + sizeof(struct TVRec); - tv->cur_chunk = (struct TVChunk *)tv->chunks; - - 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->portRect); - FrameRect(&tv->canvas->portRect); -#endif - - return tv; -} - -void -TVDispose(struct TVRec **tvp) -{ - struct TVRec *tv = (struct TVRec *)*tvp; - - TVClear(tv); - ClosePort(tv->canvas); - xfree(&tv->canvas); - xfree(&tv->chunks); - xfree(&tv->canvas->portBits.baseAddr); - xfree(tvp); -} - -struct TVChunk * -TVCreateChunk(struct TVRec *tv, unsigned long length) -{ - struct TVChunk *chunk; - unsigned long addr; - - if (IS_FIRST_EMPTY_CHUNK(tv)) - /* new TV with empty chunk */ - return tv->cur_chunk; - - if (length + sizeof(struct TVChunk) > tv->cur_chunk->free_length) { - /* allocate new buffer */ - 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; - chunk->free_length = CHUNK_ALLOC_SIZE - sizeof(struct TVStyle); - } else { - /* create new chunk in existing alloc */ - addr = (unsigned long)tv->cur_chunk + sizeof(struct TVChunk) + - tv->cur_chunk->length; - if (addr % 2 != 0) - addr++; - chunk = (struct TVChunk *)addr; - memset(chunk, 0, sizeof(struct TVChunk)); - chunk->free_length = tv->cur_chunk->free_length - - sizeof(struct TVStyle); + tv->chunk_slabs[0] = NewHandleClear(CHUNK_ALLOC_SIZE); + if (tv->chunk_slabs[0] == NULL) { + DisposHandle(tv->canvas_baseaddr); + DisposHandle(tvh); + return NULL; } + HLock(tv->chunk_slabs[0]); - chunk->line = tv->cur_chunk->line; - chunk->rect.top = tv->cur_chunk->rect.top; - chunk->rect.left = chunk->rect.right = tv->cur_chunk->rect.right; - chunk->rect.bottom = tv->cur_chunk->rect.bottom; - return chunk; -} + chunk = (struct TVChunk *)(*(tv->chunk_slabs[0])); + chunk->next_chunk = EMPTY_CHUNKPTR; + chunk->prev_chunk = EMPTY_CHUNKPTR; + chunk->free_length = CHUNK_ALLOC_SIZE - sizeof(struct TVChunk); + chunk->line = 1; + chunk->rect.top = tv->padding.top; + chunk->rect.left = tv->padding.left; + chunk->rect.bottom = chunk->rect.top; + chunk->rect.right = chunk->rect.left; -struct TVChunk * -TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length) -{ - Rect r; - struct TVChunk *new_chunk, *prev_chunk, *next_chunk; - short width; - long oldright; + HUnlock(tv->chunk_slabs[0]); - /* - * This leaks some space by not putting the new chunk right after - * chunk->text[length], but it avoids having to create a temporary - * buffer to copy chunk->text[length .. total length] to just to move - * it to new_chunk->text. - */ - new_chunk = TVCreateChunk(tv, chunk->length - length); - new_chunk->baseline = chunk->baseline; - new_chunk->style = chunk->style; - - new_chunk->prev_chunk = chunk; - if (chunk->next_chunk) { - chunk->next_chunk->prev_chunk = new_chunk; - chunk->next_chunk = new_chunk; - } else { - chunk->next_chunk = new_chunk; - tv->last_chunk = new_chunk; - } - - new_chunk->length = chunk->length - length; - memcpy(new_chunk->text, chunk->text + length, chunk->length - length); - chunk->length = length; - - TVApplyStyle(&chunk->style); - width = TextWidth(chunk->text, 0, chunk->length); - oldright = chunk->rect.right; - chunk->rect.right = chunk->rect.left + width; - if (width) { - BigRectToRect(chunk->rect, r); - r.top += tv->scrolled.top; - r.bottom += tv->scrolled.top; - r.left = tv->scrolled.left + chunk->rect.right; - r.right = tv->scrolled.left + oldright; #ifdef DEBUG_DRAWING - FillRect(&r, ltGray); -#else - EraseRect(&r); + EraseRect(&tv->canvas.portRect); + FrameRect(&tv->canvas.portRect); #endif - TVDrawText(tv, chunk, 0); - } - - /* line us up next to the current chunk */ - new_chunk->baseline = chunk->baseline; - new_chunk->rect.top = chunk->rect.top; - new_chunk->rect.bottom = chunk->rect.bottom; - new_chunk->rect.left = chunk->rect.right; - new_chunk->rect.right = new_chunk->rect.left + - TextWidth(new_chunk->text, 0, new_chunk->length); - - /* new chunk not drawn */ - return new_chunk; -} -void -TVApplyStyle(struct TVStyle *style) -{ - TextFont(style->font); - TextFace(style->style); - TextSize(style->size); + HUnlock(tvh); + return tvh; } -void -TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk) -{ - if (chunk->rect.bottom > tv->bounds.bottom - tv->padding.bottom) { - tv->bounds.bottom = chunk->rect.bottom + tv->padding.bottom; - tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom; - } - - if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) - TVScroll(tv, 0, -SHRT_MAX); -} +#define CHUNK_HEIGHT(chunk) (chunk)->baseline + \ + (chunk)->style.font_info.descent; -void -TVAppend(struct TVRec *tv, struct TVStyle *style, char *text, +bool +TVAppend(TVHandle tvh, struct TVStyle *style, char *text, unsigned long length) { - struct TVChunk *chunk = NULL, *prev_chunk, *tchunk; + struct TVRec *tv; + struct TVChunk *chunk = NULL, *cur_chunk, *prev_chunk, *tchunk; Rect r; long j, n, line_right; short width; char c; - bool new_chunk = false, new_line = false; + bool new_chunk = false, new_line = false, ret = true, new_style = false; + tv = TVLock(tvh); TVDrawOnCanvas(tv); - TVApplyStyle(style); + cur_chunk = TVGetChunk(tv, tv->cur_chunk); + line_right = cur_chunk->rect.right; if (memcmp(&tv->last_style, style, sizeof(struct TVStyle)) != 0) { GetFontInfo(&style->font_info); memset(tv->width_cache, 0, sizeof(tv->width_cache)); tv->last_style = *style; new_chunk = true; + new_style = true; } - line_right = tv->cur_chunk->rect.right; + TVApplyStyle(style); for (n = 0; n < length; n++) { c = text[n]; @@ -279,54 +173,64 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char tv->bounds.right - tv->padding.right) { new_chunk = true; new_line = true; - + } else if (cur_chunk->free_length == 0) { + new_chunk = true; + } + + if (new_chunk && cur_chunk->length && !new_style) { /* walk back to last space and break after it */ - for (j = tv->cur_chunk->length; j > 0; j--) { - if (tv->cur_chunk->text[j - 1] != ' ') + for (j = cur_chunk->length; j > 0; j--) { + if (cur_chunk->text[j - 1] != ' ') continue; - tv->cur_chunk = TVSplitChunk(tv, tv->cur_chunk, j); + tchunk = TVSplitChunk(tv, cur_chunk, j); + if (tchunk == NULL) { + ret = false; + goto done; + } + cur_chunk = tchunk; + tv->cur_chunk = cur_chunk->ptr; new_chunk = false; break; } - - if (new_chunk) { - /* - * TODO: if no space in that chunk, keep checking - * previous chunks in this line - */ - /* XXX: just do a hard break and split with 0 length? */ - Debugger(); - } - - TVApplyStyle(&tv->cur_chunk->style); } } if (new_chunk) { new_chunk = false; - chunk = TVCreateChunk(tv, length - n); + new_style = false; + + if (!new_line && IS_FIRST_EVER_CHUNK(cur_chunk)) + chunk = cur_chunk; + else { + chunk = TVCreateChunk(tv, length - n); + if (chunk == NULL) { + ret = false; + goto done; + } + chunk->prev_chunk = tv->cur_chunk; + cur_chunk->next_chunk = chunk->ptr; + prev_chunk = cur_chunk; + } + chunk->next_chunk = EMPTY_CHUNKPTR; chunk->baseline = style->font_info.ascent; chunk->style = *style; - chunk->rect.bottom = chunk->rect.top + chunk->baseline + - chunk->style.font_info.descent; + chunk->rect.bottom = chunk->rect.top + CHUNK_HEIGHT(chunk); - /* TVCreateChunk can return cur_chunk */ - if (tv->cur_chunk != chunk) { - if (n > 0) - TVDrawText(tv, tv->cur_chunk, 0); - - prev_chunk = tv->cur_chunk; - tv->cur_chunk = chunk; - tv->cur_chunk->prev_chunk = prev_chunk; - prev_chunk->next_chunk = tv->cur_chunk; - tv->last_chunk = tv->cur_chunk; + if (cur_chunk->length) + TVDrawText(tv, cur_chunk, 0); + + cur_chunk = chunk; + tv->cur_chunk = cur_chunk->ptr; + tv->last_chunk = cur_chunk->ptr; - if (tv->cur_chunk->baseline > prev_chunk->baseline) { + if (!IS_FIRST_EVER_CHUNK(cur_chunk)) { + if (cur_chunk->baseline > prev_chunk->baseline) { /* all chunks on this line must grow */ - for (tchunk = prev_chunk; tchunk != NULL; - tchunk = tchunk->prev_chunk) { - if (tchunk->line != tv->cur_chunk->line) + tchunk = prev_chunk; + for (tchunk; tchunk != NULL; + tchunk = TVGetChunk(tv, tchunk->prev_chunk)) { + if (tchunk->line != cur_chunk->line) break; if (tchunk->length) { @@ -338,146 +242,125 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char EraseRect(&r); } - tchunk->baseline = tv->cur_chunk->baseline; + tchunk->baseline = cur_chunk->baseline; tchunk->rect.bottom = tchunk->rect.top + - tchunk->baseline + - tchunk->style.font_info.descent; + CHUNK_HEIGHT(tchunk); if (tchunk->rect.bottom < chunk->rect.bottom) tchunk->rect.bottom = chunk->rect.bottom; TVGrowBoundsToFit(tv, tchunk); TVDrawText(tv, tchunk, 0); } - } else if (tv->cur_chunk->baseline < prev_chunk->baseline) { - tv->cur_chunk->baseline = - tv->cur_chunk->prev_chunk->baseline; - if (tv->cur_chunk->rect.bottom < - tv->cur_chunk->prev_chunk->rect.bottom) - tv->cur_chunk->rect.bottom = - tv->cur_chunk->prev_chunk->rect.bottom; + } else if (cur_chunk->baseline < prev_chunk->baseline) { + cur_chunk->baseline = prev_chunk->baseline; + if (cur_chunk->rect.bottom < prev_chunk->rect.bottom) + cur_chunk->rect.bottom = prev_chunk->rect.bottom; } - TVApplyStyle(&tv->cur_chunk->style); + TVApplyStyle(&cur_chunk->style); } } if (new_line) { new_line = false; - tv->cur_chunk->line++; - tv->cur_chunk->rect.top = - tv->cur_chunk->prev_chunk->rect.bottom + - tv->cur_chunk->prev_chunk->style.font_info.leading; - tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top + - tv->cur_chunk->baseline + - tv->cur_chunk->style.font_info.descent; - - tv->cur_chunk->rect.right -= tv->cur_chunk->rect.left - - tv->padding.left; - tv->cur_chunk->rect.left = tv->padding.left; - line_right = tv->cur_chunk->rect.right; + cur_chunk->line++; + prev_chunk = TVGetChunk(tv, cur_chunk->prev_chunk); + cur_chunk->rect.top = prev_chunk->rect.bottom - 1; + cur_chunk->rect.bottom = cur_chunk->rect.top + + CHUNK_HEIGHT(cur_chunk); + cur_chunk->rect.right -= cur_chunk->rect.left - + tv->padding.left; + cur_chunk->rect.left = tv->padding.left; + line_right = cur_chunk->rect.right; - TVGrowBoundsToFit(tv, tv->cur_chunk); - TVDrawText(tv, tv->cur_chunk, 0); + TVGrowBoundsToFit(tv, cur_chunk); + TVDrawText(tv, cur_chunk, 0); } if (c == '\r') { - /* - * TODO: remember that this was a hard line break and not - * just a wrapped line - */ + cur_chunk->linebreak = true; + /* next line will need to know how tall our empty space was */ - if (tv->cur_chunk->length == 0) { - tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top + - tv->cur_chunk->baseline + - tv->cur_chunk->style.font_info.descent; + if (cur_chunk->length == 0) { + prev_chunk = TVGetChunk(tv, cur_chunk->prev_chunk); + cur_chunk->rect.top = prev_chunk->rect.bottom - 1; + cur_chunk->rect.bottom = cur_chunk->rect.top + + CHUNK_HEIGHT(cur_chunk); } line_right = tv->padding.left; } else { - tv->cur_chunk->text[tv->cur_chunk->length++] = c; - tv->cur_chunk->free_length--; + cur_chunk->text[cur_chunk->length++] = c; + cur_chunk->free_length--; line_right += width; - tv->cur_chunk->rect.right += width; + cur_chunk->rect.right += width; #ifdef DEBUG_DRAWING - TVDrawText(tv, tv->cur_chunk, 0); + TVDrawText(tv, cur_chunk, 0); #endif } } - TVDrawText(tv, tv->cur_chunk, 0); - TVShowCanvas(tv); +done: + TVDrawText(tv, cur_chunk, 0); - if (tv->canvas_refcnt != 0) - Debugger(); + TVShowCanvas(tv); + TVUnlock(tv); + return ret; } void -TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off) +TVAutoScroll(TVHandle tvh, bool scroll) { - Point p; - long l, b; - Rect r; + struct TVRec *tv; - if (off != 0) - panic("need to implement redrawing a partial string"); - - if (chunk->length - off > 0) { - l = tv->scrolled.left + chunk->rect.left; - b = tv->scrolled.top + chunk->rect.top + chunk->baseline; - MoveTo(l, b); - TVApplyStyle(&chunk->style); - - DrawText(chunk->text, off, chunk->length - off); - } - - if (tv->sel_start == chunk) { - r.left = l; - r.right = p.h; - r.top = b - chunk->baseline; - r.bottom = chunk->rect.bottom; - InvertRect(&r); - } -} - -void -TVAutoScroll(struct TVRec *tv, bool scroll) -{ + tv = TVLock(tvh); tv->autoscroll = scroll; + TVUnlock(tv); } void -TVUpdateScrollbar(struct TVRec *tv, ControlHandle scroller) +TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller) { + struct TVRec *tv; long max; - struct TVChunk *first; RgnHandle rgn; - HLock(scroller); + tv = TVLock(tvh); /* * Avoid SetCtlMin/Max because it will redraw and then SetCtlValue will * redraw again, which can cause a jump if we're trying to keep the * scrollbar position in the same place (like at the bottom). */ + HLock(scroller); + if ((*scroller)->nextControl != 0) + Debugger(); (*scroller)->contrlMin = 0; max = tv->bounds.bottom - tv->bounds.top - (tv->view.bottom - tv->view.top); (*scroller)->contrlMax = (max > 0 ? max : 0); - SetCtlValue(scroller, -(tv->scrolled.top)); + HUnlock(scroller); rgn = NewRgn(); + HLock(scroller); RectRgn(rgn, &(*scroller)->contrlRect); UpdtControl((*(scroller))->contrlOwner, rgn); + HUnlock(scroller); DisposeRgn(rgn); HUnlock(scroller); + TVUnlock(tv); } void -TVClick(struct TVRec *tv, Point p, bool extend) +TVClick(TVHandle tvh, Point p, bool extend) { + struct TVRec *tv; struct TVChunk *chunk; -return; + + tv = TVLock(tvh); + +#if 0 p.v -= tv->view.top; p.h -= tv->view.left; @@ -485,7 +368,6 @@ return; p.h += -(tv->canvas->portRect.left); /* StillDown() then WaitMouseUp() */ - for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; chunk = chunk->next_chunk) { if (p.v >= chunk->rect.top && p.v < chunk->rect.bottom && @@ -495,13 +377,16 @@ return; break; } } +#endif + + TVUnlock(tv); } -static struct TVRec *tv_track_control = NULL; +static TVHandle tv_track_control = NULL; void -TVSetTrackScrollControl(struct TVRec *tv) +TVSetTrackScrollControl(TVHandle tvh) { - tv_track_control = tv; + tv_track_control = tvh; } pascal void @@ -512,9 +397,12 @@ TVTrackScrollControl(ControlHandle control, short part long n, top; bool horiz; - if (tv_track_control == NULL) - panic("TVTrackScrollControl without TVSetTrackScrollControl"); - tv = tv_track_control; + if (tv_track_control == NULL) { + /* TVTrackScrollControl without TVSetTrackScrollControl */ + Debugger(); + return; + } + tv = TVLock(tv_track_control); horiz = (((*control)->contrlRect.bottom - (*control)->contrlRect.top) < ((*control)->contrlRect.right - (*control)->contrlRect.left)); @@ -522,26 +410,26 @@ TVTrackScrollControl(ControlHandle control, short part switch (part) { case inUpButton: /* scroll to the previous line that is not yet mostly-visible */ - for (chunk = (struct TVChunk *)tv->last_chunk; chunk != NULL; - chunk = chunk->prev_chunk) { + for (chunk = TVGetChunk(tv, tv->last_chunk); chunk != NULL; + chunk = TVGetChunk(tv, chunk->prev_chunk)) { if ((chunk->rect.top + (long)(chunk->baseline / 2) + tv->scrolled.top) > 0) continue; - if (!chunk->prev_chunk) + if (!TVGetChunk(tv, chunk->prev_chunk)) break; - TVScroll(tv, 0, -(tv->scrolled.top) - chunk->rect.top); + TVScrollUnlocked(tv, 0, -(tv->scrolled.top) - chunk->rect.top); goto update_value; } /* nothing found, just scroll to the top */ - TVScroll(tv, 0, -(tv->scrolled.top)); + TVScrollUnlocked(tv, 0, -(tv->scrolled.top)); break; case inPageUp: /* scroll to put the top mostly-visible line at the bottom */ - for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; - chunk = chunk->next_chunk) { + for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL; + chunk = TVGetChunk(tv, chunk->next_chunk)) { if ((chunk->rect.top + (long)(chunk->baseline / 2) + tv->scrolled.top) < 0) continue; @@ -551,59 +439,437 @@ TVTrackScrollControl(ControlHandle control, short part if (tv->scrolled.top + top > 0) break; - TVScroll(tv, 0, top); + TVScrollUnlocked(tv, 0, top); goto update_value; } /* nothing found, just scroll to the top */ - TVScroll(tv, 0, -(tv->scrolled.top)); + TVScrollUnlocked(tv, 0, -(tv->scrolled.top)); break; case inDownButton: /* scroll to the line after the bottom-most line */ - for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; - chunk = chunk->next_chunk) { + for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL; + chunk = TVGetChunk(tv, chunk->next_chunk)) { top = chunk->rect.top + (chunk->baseline / 2) + tv->scrolled.top; if (top < tv->view_height) continue; - TVScroll(tv, 0, -((chunk->rect.bottom - tv->view_height) + + TVScrollUnlocked(tv, 0, + -((chunk->rect.bottom - tv->view_height) + tv->scrolled.top)); goto update_value; } /* nothing found, just scroll to the bottom */ - TVScroll(tv, 0, tv->view_height - tv->scrolled.bottom); + TVScrollUnlocked(tv, 0, tv->view_height - tv->scrolled.bottom); break; case inPageDown: /* scroll to put the bottom mostly-visible line at the top */ - for (chunk = (struct TVChunk *)tv->last_chunk; chunk != NULL; - chunk = chunk->prev_chunk) { + for (chunk = TVGetChunk(tv, tv->last_chunk); chunk != NULL; + chunk = TVGetChunk(tv, chunk->prev_chunk)) { if (chunk->rect.bottom - (long)(chunk->baseline / 2) + tv->scrolled.top > tv->view_height) continue; - TVScroll(tv, 0, -(tv->scrolled.top) - chunk->rect.top); + TVScrollUnlocked(tv, 0, -(tv->scrolled.top) - chunk->rect.top); goto update_value; } /* nothing found, just scroll to the bottom */ - TVScroll(tv, 0, tv->view_height - tv->scrolled.bottom); + TVScrollUnlocked(tv, 0, tv->view_height - tv->scrolled.bottom); break; } update_value: SetCtlValue(control, -(tv->scrolled.top)); + TVUnlock(tv); } void -TVScroll(struct TVRec *tv, short horiz, short vert) +TVScroll(TVHandle tvh, short horiz, short vert) { + struct TVRec *tv; + + tv = TVLock(tvh); + TVScrollUnlocked(tv, horiz, vert); + TVUnlock(tv); +} + +void +TVClear(TVHandle tvh) +{ + struct TVRec *tv; + struct TVChunk *tchunk, *cur_chunk; + short n; + + tv = TVLock(tvh); + + /* don't dispose of first one */ + for (n = 1; n < sizeof(tv->chunk_slabs) / sizeof(Handle); n++) { + if (tv->chunk_slabs[n] != NULL) { + DisposHandle(tv->chunk_slabs[n]); + tv->chunk_slabs[n] = 0; + } + } + + HLock(tv->chunk_slabs[0]); + memset((char *)*(tv->chunk_slabs[0]), 0, CHUNK_ALLOC_SIZE); + + tv->cur_chunk = 0; + cur_chunk = TVGetChunk(tv, tv->cur_chunk); + cur_chunk->free_length = CHUNK_ALLOC_SIZE - sizeof(struct TVChunk); + cur_chunk->line = 1; + cur_chunk->rect.top = tv->padding.top; + cur_chunk->rect.left = tv->padding.left; + cur_chunk->rect.bottom = cur_chunk->rect.top; + cur_chunk->rect.right = cur_chunk->rect.left; + cur_chunk->next_chunk = EMPTY_CHUNKPTR; + cur_chunk->prev_chunk = EMPTY_CHUNKPTR; + HUnlock(tv->chunk_slabs[0]); + + tv->last_chunk = tv->cur_chunk; + tv->first_chunk = tv->cur_chunk; + tv->first_visible_chunk = tv->cur_chunk; + tv->last_visible_chunk = tv->cur_chunk; + + 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); + TVUnlock(tv); +} + +void +TVUpdate(TVHandle tvh) +{ + struct TVRec *tv; + struct TVChunk *chunk; + + tv = TVLock(tvh); + TVDrawOnCanvas(tv); + + for (chunk = TVGetChunk(tv, tv->first_visible_chunk); chunk != NULL; + chunk = TVGetChunk(tv, chunk->next_chunk)) { + if (chunk->rect.bottom < -(tv->scrolled.top)) + continue; + if (chunk->rect.top + tv->scrolled.top > tv->view_height) + break; + TVDrawText(tv, chunk, 0); + } + + TVShowCanvas(tv); + TVUnlock(tv); +} + +void +TVDispose(TVHandle tvh) +{ + struct TVRec *tv; + + /* this will free all but the first slab */ + TVClear(tvh); + + tv = TVLock(tvh); + + DisposeHandle(tv->chunk_slabs[0]); + + ClosePort(&tv->canvas); + DisposeHandle(tv->canvas_baseaddr); + DisposeHandle(tvh); +} + +/* + * internal functions + * anything taking a TVRec assumes everything is locked for dereferencing + */ + +struct TVRec * +TVLock(TVHandle tvh) +{ + struct TVRec *tv; + short n; + + HLock(tvh); + tv = *tvh; + tv->unlock_refcnt++; + + HLock(tv->canvas_baseaddr); + tv->canvas.portBits.baseAddr = *(tv->canvas_baseaddr); + + return tv; +} + +void +TVUnlock(struct TVRec *tv) +{ + short n; + + if (tv->unlock_refcnt-- != 0) { + if (tv->unlock_refcnt < 0) + Debugger(); + return; + } + + for (n = 0; n < sizeof(tv->chunk_slabs) / sizeof(Handle); n++) + if (tv->chunk_slabs[n]) + HUnlock(tv->chunk_slabs[n]); + + HUnlock(tv->canvas_baseaddr); + HUnlock(RecoverHandle(tv)); +} + +struct TVChunk * +TVGetChunk(struct TVRec *tv, TVChunkPtr ptr) +{ + struct TVChunk *chunk; + unsigned short off; + Handle slab; + char *addr; + + if (ptr == 0xffffffffL) /* XXX: why doesn't EMPTY_CHUNKPTR work? */ + return NULL; + + HLock(tv->chunk_slabs[CHUNKPTR_SLAB(ptr)]); + addr = *(tv->chunk_slabs[CHUNKPTR_SLAB(ptr)]); + + off = CHUNKPTR_OFFSET(ptr); + if (off % 2 != 0) + Debugger(); + addr += off; + chunk = (struct TVChunk *)addr; + + return chunk; +} + +struct TVChunk * +TVCreateChunk(struct TVRec *tv, unsigned long length) +{ + struct TVChunk *chunk, *cur_chunk; + TVChunkPtr cptr; + unsigned long addr, size; + unsigned short off; + Handle slab; + short slab_slot, n; + + cur_chunk = TVGetChunk(tv, tv->cur_chunk); + + if (length + sizeof(struct TVChunk) + 1 > cur_chunk->free_length) { + /* allocate new slab */ + slab_slot = 0; + for (n = 1; n < sizeof(tv->chunk_slabs) / sizeof(Handle); n++) { + if (tv->chunk_slabs[n] == NULL) { + slab_slot = n; + break; + } + } + if (slab_slot == 0) + return NULL; + size = CHUNK_ALLOC_SIZE; + slab = NewHandleClear(size + 128); + if (slab == NULL) { + /* out of memory, try to allocate smaller chunks */ + size /= 2; + slab = NewHandleClear(size); + if (slab == NULL) { + size /= 2; + slab = NewHandleClear(size); + if (slab == NULL) + /* oh well, we tried all we could */ + return NULL; + } + } + HLock(slab); + tv->chunk_slabs[slab_slot] = slab; + SET_CHUNKPTR_SLAB(cptr, slab_slot); + SET_CHUNKPTR_OFFSET(cptr, 0); + chunk = TVGetChunk(tv, cptr); + chunk->ptr = cptr; + chunk->free_length = size - sizeof(struct TVChunk); + } else { + /* create new chunk in existing alloc */ + off = CHUNKPTR_OFFSET(cur_chunk->ptr) + sizeof(struct TVChunk) + + cur_chunk->length; + if (off % 2 != 0) + off++; + SET_CHUNKPTR_SLAB(cptr, CHUNKPTR_SLAB(cur_chunk->ptr)); + SET_CHUNKPTR_OFFSET(cptr, off); + chunk = TVGetChunk(tv, cptr); + chunk->ptr = cptr; + chunk->free_length = cur_chunk->free_length - + sizeof(struct TVChunk); + } + + cur_chunk->free_length = 0; + + chunk->prev_chunk = cur_chunk->ptr; + chunk->next_chunk = EMPTY_CHUNKPTR; + + chunk->line = cur_chunk->line; + chunk->rect.top = cur_chunk->rect.top; + chunk->rect.left = chunk->rect.right = cur_chunk->rect.right; + chunk->rect.bottom = cur_chunk->rect.bottom; + + return chunk; +} + +struct TVChunk * +TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length) +{ + Rect r; + struct TVChunk *new_chunk, *prev_chunk, *next_chunk; + short width; + long oldright; + + /* + * This leaks some space by not putting the new chunk right after + * chunk->text[length], but it avoids having to create a temporary + * buffer to copy chunk->text[length .. total length] to just to move + * it to new_chunk->text. + */ + new_chunk = TVCreateChunk(tv, chunk->length - length); + new_chunk->baseline = chunk->baseline; + new_chunk->style = chunk->style; + + if ((next_chunk = TVGetChunk(tv, chunk->next_chunk))) { + /* insert new_chunk between chunk and next_chunk */ + chunk->next_chunk = new_chunk->ptr; + next_chunk->prev_chunk = new_chunk->ptr; + new_chunk->prev_chunk = chunk->ptr; + new_chunk->next_chunk = next_chunk->ptr; + } else { + /* append */ + chunk->next_chunk = new_chunk->ptr; + new_chunk->prev_chunk = chunk->ptr; + new_chunk->next_chunk = EMPTY_CHUNKPTR; + tv->last_chunk = new_chunk->ptr; + } + + /* assigning to tv->cur_chunk must be done by caller */ + + new_chunk->length = chunk->length - length; + new_chunk->free_length -= new_chunk->length; + memcpy(new_chunk->text, chunk->text + length, chunk->length - length); + chunk->length = length; + + TVApplyStyle(&chunk->style); + width = TextWidth(chunk->text, 0, chunk->length); + oldright = chunk->rect.right; + chunk->rect.right = chunk->rect.left + width; + if (width) { + BigRectToRect(chunk->rect, r); + r.top += tv->scrolled.top; + r.bottom += tv->scrolled.top; + r.left = tv->scrolled.left + chunk->rect.right; + r.right = tv->scrolled.left + oldright; +#ifdef DEBUG_DRAWING + FillRect(&r, ltGray); +#else + EraseRect(&r); +#endif + TVDrawText(tv, chunk, 0); + } + + /* line us up next to the current chunk */ + new_chunk->baseline = chunk->baseline; + new_chunk->rect.top = chunk->rect.top; + new_chunk->rect.bottom = chunk->rect.bottom; + new_chunk->rect.left = chunk->rect.right; + new_chunk->rect.right = new_chunk->rect.left + + TextWidth(new_chunk->text, 0, new_chunk->length); + + /* new chunk not drawn */ + return new_chunk; +} + +void +TVApplyStyle(struct TVStyle *style) +{ + TextFont(style->font); + TextFace(style->style); + TextSize(style->size); +} + +void +TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk) +{ + if (chunk->rect.bottom > tv->bounds.bottom - tv->padding.bottom) { + tv->bounds.bottom = chunk->rect.bottom + tv->padding.bottom; + tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom; + } + + if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) + TVScrollUnlocked(tv, 0, -SHRT_MAX); +} + +void +TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off) +{ + struct TVChunk *last; + Point p; + long l, b; + Rect r; + + if (off != 0) { + /* TODO: implement redrawing a partial string */ + Debugger(); + return; + } + + if (chunk->length - off > 0) { + l = tv->scrolled.left + chunk->rect.left; + b = tv->scrolled.top + chunk->rect.top + chunk->baseline; + + if (b + chunk->style.font_info.descent >= 0 && + tv->scrolled.top + chunk->rect.top <= tv->view_height) { + MoveTo(l, b); + TVApplyStyle(&chunk->style); + DrawText(chunk->text, off, chunk->length - off); + +#ifdef DEBUG_DRAWING + r.top = chunk->rect.top + tv->scrolled.top; + r.bottom = chunk->rect.bottom + tv->scrolled.top; + r.left = tv->scrolled.left + chunk->rect.left; + r.right = tv->scrolled.left + chunk->rect.right; + if (r.right == r.left) + r.right++; + FrameRect(&r); +#endif + + if (tv->cur_chunk != chunk->ptr) { + last = TVGetChunk(tv, tv->last_visible_chunk); + if (chunk->rect.top > last->rect.top) + tv->last_visible_chunk = chunk->ptr; + } + } + } + +#if 0 + if (tv->sel_start == chunk) { + r.left = l; + r.right = p.h; + r.top = b - chunk->baseline; + r.bottom = chunk->rect.bottom; + InvertRect(&r); + } +#endif + +TVShowCanvas(tv); +TVDrawOnCanvas(tv); +} + +void +TVScrollUnlocked(struct TVRec *tv, short horiz, short vert) +{ short top, left; BigRect rvacated; Rect was, sect; RgnHandle vacated; - struct TVChunk *chunk; + struct TVChunk *chunk, *start, *last = NULL; + bool first = false; if (!horiz && !vert) return; @@ -632,97 +898,57 @@ TVScroll(struct TVRec *tv, short horiz, short vert) vacated = NewRgn(); left = tv->scrolled.left - was.left; top = tv->scrolled.top - was.top; - ScrollRect(&tv->canvas->portRect, left, top, vacated); - + ScrollRect(&tv->canvas.portRect, left, top, vacated); + +#if 0 rvacated = tv->scrolled; rvacated.bottom = top; - for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; - chunk = chunk->next_chunk) { + if (top < 0) + start = TVGetChunk(tv, tv->last_visible_chunk); + else + start = TVGetChunk(tv, tv->first_visible_chunk); + + for (chunk = start; chunk != NULL; + chunk = TVGetChunk(tv, + (top < 0 ? chunk->prev_chunk : chunk->next_chunk))) { if (chunk->rect.top > rvacated.bottom) continue; if (chunk->rect.bottom < rvacated.top) continue; + if (!first) { + tv->first_visible_chunk = chunk->ptr; + first = true; + } TVDrawText(tv, chunk, 0); + last = chunk; } + if (last) + tv->last_visible_chunk = last->ptr; + else + tv->last_visible_chunk = tv->first_visible_chunk; +#endif + DisposeRgn(vacated); TVShowCanvas(tv); } 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; - long height; - - TVDrawOnCanvas(tv); - - for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; - chunk = chunk->next_chunk) { - if (chunk->rect.bottom < -(tv->scrolled.top)) - continue; - if (chunk->rect.top + tv->scrolled.top > tv->view_height) - break; - TVDrawText(tv, chunk, 0); - } - - TVShowCanvas(tv); -} - -void TVDrawOnCanvas(struct TVRec *tv) { if (++tv->canvas_refcnt != 1) return; + + if (tv->canvas_refcnt == 0) + Debugger(); #ifdef DEBUG_DRAWING return; #endif GetPort(&tv->canvas_old_port); - SetPort(tv->canvas); + SetPort(&tv->canvas); } void @@ -740,8 +966,8 @@ TVShowCanvas(struct TVRec *tv) SetPort(tv->canvas_old_port); - CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits, - &tv->canvas->portRect, &tv->view, srcCopy, NULL); + CopyBits(&tv->canvas.portBits, &tv->canvas_old_port->portBits, + &tv->canvas.portRect, &tv->view, srcCopy, NULL); ValidRect(&tv->view); --- textview.h Mon Oct 7 16:45:25 2024 +++ textview.h Wed Oct 9 17:26:55 2024 @@ -14,11 +14,13 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "util.h" +#include <stddef.h> +#include <limits.h> +#include "stdint.h" struct TVStyle { struct FontInfo font_info; - short font; + unsigned char font; unsigned char style; unsigned char size; }; @@ -37,18 +39,31 @@ typedef struct BigRect BigRect; (br).right = (r).right; \ (br).bottom = (r).bottom; ) +typedef unsigned long TVChunkPtr; +#define EMPTY_CHUNKPTR 0xffffffffL; + +/* high 4 are slab index, low are byte offset */ +#define CHUNKPTR_SLAB(x) (((x) >> 16) & 0xffff) +#define CHUNKPTR_OFFSET(x) ((x) & 0xffff) + +#define SET_CHUNKPTR_SLAB(ptr, x) \ + { (ptr) = ((unsigned long)(x) << 16) | (ptr) & 0xffff; } +#define SET_CHUNKPTR_OFFSET(ptr, x) \ + { (ptr) = (ptr) & 0xffff0000 | (x) & 0xffff; } + struct TVChunk { - struct TVChunk *prev_chunk; - struct TVChunk *next_chunk; - unsigned long free_length; + TVChunkPtr ptr; + TVChunkPtr prev_chunk; + TVChunkPtr next_chunk; + long free_length; unsigned long line; + struct TVStyle style; BigRect rect; unsigned short length; unsigned short sel_start; unsigned short sel_finish; - unsigned short baseline; - struct TVStyle style; - bool mallocked; + unsigned char baseline; + bool linebreak; char text[]; }; @@ -61,36 +76,44 @@ struct TVRec { /* padding into bounds where we should start text */ Rect padding; /* off-screen canvas where we draw text that is visible */ - GrafPtr canvas; + GrafPort canvas; + Handle canvas_baseaddr; short canvas_refcnt; - /* bytes allocated for canvas backing */ size_t canvas_size; /* while drawing on canvas, this stores old thePort */ 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; + + Handle chunk_slabs[64]; + TVChunkPtr first_chunk; + TVChunkPtr cur_chunk; + TVChunkPtr last_chunk; + TVChunkPtr first_visible_chunk; + TVChunkPtr last_visible_chunk; + struct TVChunk *sel_start; struct TVChunk *sel_finish; - Ptr chunks; + + char unlock_refcnt; + bool autoscroll; }; +typedef struct TVRec ** TVHandle; -struct TVRec * TVNew(Rect *view, Rect *padding); -void TVDispose(struct TVRec **tvp); -void TVAutoScroll(struct TVRec *tv, bool scroll); -void TVAppend(struct TVRec *tv, struct TVStyle *style, char *text, +TVHandle TVNew(Rect *view, Rect *padding); +void TVDispose(TVHandle tvh); +void TVAutoScroll(TVHandle tvh, bool scroll); +bool TVAppend(TVHandle tvh, struct TVStyle *style, char *text, unsigned long length); -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 TVUpdate(TVHandle tvh); +void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller); +void TVClick(TVHandle tvh, Point p, bool extend); +void TVScroll(TVHandle tvh, short horiz, short vert); +void TVClear(TVHandle tvh); -void TVSetTrackScrollControl(struct TVRec *tv); +void TVSetTrackScrollControl(TVHandle tvh); pascal void TVTrackScrollControl(ControlHandle control, short part);