/* * Copyright (c) 2023-2024 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include "textview.h" void TVDisposeSlabs(Handle slabh); TVHandle TVNew(Rect *view, Rect *padding) { TVHandle tvh; struct TVRec *tv; struct TVLine *line; tvh = (TVHandle)NewHandleClear(sizeof(struct TVRec)); if (tvh == NULL) return NULL; HLock(tvh); tv = *tvh; tv->view = *view; tv->padding = *padding; tv->padding.left++; SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left, tv->view.bottom - tv->view.top); tv->linewrap = true; tv->autocalc = true; tv->active = true; memset(&tv->tab_pat, 0x88, sizeof(tv->tab_pat)); #ifdef USE_CANVAS if (!TVSetupCanvas(tv)) goto fail; #endif tv->bounds.right = tv->frame.right; tv->bounds.bottom = tv->padding.top + tv->padding.bottom; tv->scrolled = tv->bounds; EraseRect(&tv->view); if (TVAllocateSlab(tv, TV_STYLE_SLAB_SIZE, &tv->first_style_slab, &tv->last_style_slab) == NULL) goto fail; if (TVAllocateSlab(tv, TV_TEXT_SLAB_SIZE, &tv->first_text_slab, &tv->last_text_slab) == NULL) goto fail; if (TVAllocateSlab(tv, TV_CHUNK_SLAB_SIZE, &tv->first_chunk_slab, &tv->last_chunk_slab) == NULL) goto fail; if (TVAllocateLineGroup(tv, 0) == NULL) goto fail; HUnlock(tvh); return tvh; fail: if (tv->first_chunk_slab) DisposHandle(tv->first_chunk_slab); if (tv->first_text_slab) DisposHandle(tv->first_text_slab); if (tv->first_style_slab) DisposHandle(tv->first_style_slab); if (tv->canvas_baseaddr) DisposHandle(tv->canvas_baseaddr); if (tv->canvas) { ClosePort(tv->canvas); DisposPtr(tv->canvas); } DisposHandle(tvh); return NULL; } Boolean TVAppend(TVHandle tvh, struct TVStyle *style, char *text, 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 TVAppendBitMap(TVHandle tvh, BitMap *bitmap, struct TVStyle *bstyle) { struct TVRec *tv; struct TVStyle *style; long bitmap_size, width, height; Boolean ret; width = bitmap->bounds.right - bitmap->bounds.left; height = bitmap->bounds.bottom - bitmap->bounds.top; bitmap_size = (long)bitmap->rowBytes * height; style = (struct TVStyle *)NewPtrClear(sizeof(struct TVStyle) + bitmap_size); if (style == NULL) return false; style->bitmap_data_size = bitmap_size; style->bitmap = *bitmap; memcpy(style->bitmap_data, bitmap->baseAddr, bitmap_size); style->bitmap.baseAddr = (Ptr)style->bitmap_data; tv = TVLock(tvh); if (!bstyle) bstyle = TVGetStyle(tv, tv->cur_style); if (bstyle) { style->font = bstyle->font; style->style = bstyle->style; style->size = bstyle->size; style->font_info = bstyle->font_info; style->tag = bstyle->tag; } ret = TVAppendLocked(tv, style, " ", 1, 0); TVUnlock(tv); DisposPtr(style); return ret; } Boolean TVCalcLines(TVHandle tvh) { struct TVRec *tv; Boolean ret; tv = TVLock(tvh); ret = TVCalcLinesLocked(tv, false); TVUnlock(tv); return ret; } Boolean TVFindInRect(TVHandle tvh, BigRect *findrect, long startline, TVFindInRectCallback callback, void *cookie) { struct TVRec *tv; Boolean ret; tv = TVLock(tvh); ret = TVFindInRectLocked(tv, findrect, startline, callback, cookie); TVUnlock(tv); return ret; } void TVAutoCalc(TVHandle tvh, Boolean calc) { struct TVRec *tv; tv = TVLock(tvh); tv->autocalc = calc; if (calc && tv->pending_redraw) { tv->pending_redraw = false; TVRedrawAppendLocked(tv, tv->pending_from_line, tv->pending_from_end); } TVUnlock(tv); } void TVAutoScroll(TVHandle tvh, Boolean scroll) { struct TVRec *tv; tv = TVLock(tvh); tv->autoscroll = scroll; TVUnlock(tv); } void TVLineWrap(TVHandle tvh, Boolean wrap) { struct TVRec *tv; tv = TVLock(tvh); tv->linewrap = wrap; TVUnlock(tv); } void TVTabStop(TVHandle tvh, unsigned short tabstop) { struct TVRec *tv; tv = TVLock(tvh); tv->tabstop = tabstop; TVUnlock(tv); } void TVSetScrollbar(TVHandle tvh, ControlHandle scroller) { struct TVRec *tv; tv = TVLock(tvh); tv->scroller = scroller; TVUnlock(tv); } unsigned long TVGetText(TVHandle tvh, char *dst, unsigned long start, unsigned long end) { struct TVRec *tv; struct TVSlab *slab; unsigned long slabstart, slabend, read, size; tv = TVLock(tvh); if (end > tv->text_length) end = tv->text_length; read = 0; while (start + read < end) { slab = TVGetTextSlab(tv, start + read); if (slab == NULL) { read = 0; break; } slabstart = slab->base; slabend = slab->base + slab->size; if (start > slabstart) slabstart = start; if (end < slabend) slabend = end; size = slabend - slabstart; if (size == 0) Debugger(); memcpy(dst, slab->data + (slabstart - slab->base), size); dst += size; read += size; } TVUnlock(tv); return read; } void TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, Rect *rect) { struct TVRec *tv; tv = TVLock(tvh); rect->left = bigrect->left + tv->scrolled.left + tv->view.left; rect->top = bigrect->top + tv->scrolled.top + tv->view.top; rect->right = bigrect->right + tv->scrolled.left + tv->view.left; rect->bottom = bigrect->bottom + tv->scrolled.top + tv->view.top; TVUnlock(tv); } void TVClear(TVHandle tvh) { Handle slabh; struct TVRec *tv; struct TVChunk *tchunk, *cur_chunk; struct TVSlab *slab; short n; tv = TVLock(tvh); /* free style slabs except first one */ while (tv->last_style_slab != tv->first_style_slab) { slabh = tv->last_style_slab; HLock(slabh); slab = (struct TVSlab *)*slabh; tv->last_style_slab = slab->prev_slab; DisposeHandle(slabh); } HLock(tv->first_style_slab); slab = (struct TVSlab *)*(tv->first_style_slab); memset(slab->data, 0, slab->size); slab->pos = 0; slab->next_slab = NULL; HUnlock(tv->first_style_slab); tv->cur_style = 0; /* free chunk slabs except first one */ while (tv->last_chunk_slab != tv->first_chunk_slab) { slabh = tv->last_chunk_slab; HLock(slabh); slab = (struct TVSlab *)*slabh; tv->last_chunk_slab = slab->prev_slab; DisposeHandle(slabh); } HLock(tv->first_chunk_slab); slab = (struct TVSlab *)*(tv->first_chunk_slab); memset(slab->data, 0, slab->size); slab->pos = 0; slab->next_slab = NULL; HUnlock(tv->first_chunk_slab); tv->first_chunk = 0; tv->last_chunk = 0; /* free text slabs except first one */ while (tv->last_text_slab != tv->first_text_slab) { slabh = tv->last_text_slab; HLock(slabh); slab = (struct TVSlab *)*slabh; tv->last_text_slab = slab->prev_slab; DisposeHandle(slabh); } HLock(tv->first_text_slab); slab = (struct TVSlab *)*(tv->first_text_slab); memset(slab->data, 0, slab->size); slab->pos = 0; slab->next_slab = NULL; HUnlock(tv->first_text_slab); tv->text_slab_hint = NULL; tv->style_slab_hint = NULL; tv->chunk_slab_hint = NULL; tv->text_length = 0; tv->nlines = 0; tv->last_space = 0; tv->space_width = 0; tv->sel_start = 0; tv->sel_end = 0; tv->click_count = 0; tv->pending_redraw = false; /* clear line groups except first one */ for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]); n++) { if (tv->line_groups[n] == NULL) continue; DisposeHandle(tv->line_groups[n]); tv->line_groups[n] = NULL; } HLock(tv->line_groups[0]); memset((char *)*(tv->line_groups[0]), 0, TV_LINE_GROUP_SIZE); HUnlock(tv->line_groups[0]); tv->bounds.bottom = tv->padding.top + tv->padding.bottom; tv->scrolled = tv->bounds; TVUseCanvas(tv); EraseRect(&tv->frame); tv->canvas_dirty = true; TVRevealCanvas(tv); TVUnlock(tv); } void TVDisposeSlabs(Handle slabh) { Handle nexth; while (slabh != NULL) { nexth = ((struct TVSlab *)*slabh)->next_slab; DisposeHandle(slabh); slabh = nexth; } } void TVDispose(TVHandle tvh) { struct TVRec *tv; short n; /* do no drawing in here */ HLock(tvh); tv = *tvh; TVDisposeSlabs(tv->first_style_slab); TVDisposeSlabs(tv->first_chunk_slab); TVDisposeSlabs(tv->first_text_slab); for (n = 0; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]); n++) { if (tv->line_groups[n] != NULL) DisposeHandle(tv->line_groups[n]); } #ifdef USE_CANVAS ClosePort(tv->canvas); DisposPtr(tv->canvas); DisposeHandle(tv->canvas_baseaddr); #endif DisposeHandle(tvh); } /* * internal functions * anything taking a TVRec assumes everything is locked for dereferencing */ struct TVRec * TVLock(TVHandle tvh) { struct TVRec *tv; HLock(tvh); tv = *tvh; tv->unlock_refcnt++; return tv; } void TVUnlock(struct TVRec *tv) { Handle slabh, tslabh; struct TVSlab *slab; short n; if (--tv->unlock_refcnt != 0) { if (tv->unlock_refcnt < 0) Debugger(); return; } slabh = tv->first_style_slab; while (slabh) { slab = (struct TVSlab *)*slabh; tslabh = slabh; slabh = slab->next_slab; HUnlock(tslabh); } slabh = tv->first_text_slab; while (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; tslabh = slabh; slabh = slab->next_slab; HUnlock(tslabh); } slabh = tv->first_chunk_slab; while (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; tslabh = slabh; slabh = slab->next_slab; HUnlock(tslabh); } for (n = 0; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]); n++) { if (tv->line_groups[n]) HUnlock(tv->line_groups[n]); } HUnlock(RecoverHandle(tv)); } 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; struct TVLine *fromline; Handle text_slabh; unsigned long avail, fromnline, fromend; TVSlabLocator prev_style, prev_chunk_loc; char *text_slab_data; Boolean new_chunk; if (terminator) length++; if (length == 0) return false; new_chunk = false; fromnline = tv->nlines; fromline = TVGetLine(tv, fromnline); fromend = fromline ? fromline->end : 0; /* setup style */ prev_style = tv->cur_style; cur_style = TVFindStyle(tv, style); if (cur_style == NULL) return false; tv->cur_style = cur_style->loc; if (tv->cur_style != prev_style) new_chunk = true; /* setup text slab */ HLock(tv->last_text_slab); text_slab = (struct TVSlab *)*(tv->last_text_slab); /* setup chunk */ prev_chunk = NULL; chunk = TVGetChunk(tv, tv->last_chunk); while (length) { avail = text_slab->size - text_slab->pos; if (avail == 0) { /* new text slab, new chunk */ if ((text_slabh = TVAllocateSlab(tv, TV_TEXT_SLAB_SIZE, &tv->first_text_slab, &tv->last_text_slab)) == NULL) return false; text_slab = (struct TVSlab *)*text_slabh; avail = text_slab->size; new_chunk = true; } if (chunk == NULL || new_chunk) { new_chunk = false; prev_chunk_loc = chunk ? chunk->loc : 0; chunk = TVCreateChunk(tv); if (chunk == NULL) return false; prev_chunk = TVGetChunk(tv, prev_chunk_loc); chunk->start = text_slab->base + text_slab->pos; chunk->end = chunk->start; chunk->style = tv->cur_style; if (prev_chunk) { chunk->prev_chunk = prev_chunk->loc; prev_chunk->next_chunk = chunk->loc; } } text_slab_data = text_slab->data + text_slab->pos; if (length < avail) avail = length; if (terminator && avail == length) { memcpy(text_slab_data, text, avail - 1); text_slab_data[avail - 1] = terminator; } else memcpy(text_slab_data, text, avail); text += avail; text_slab->pos += avail; length -= avail; tv->text_length += avail; chunk->end += avail; chunk->need_recalc = true; } if (!tv->autocalc) { if (!tv->pending_redraw) { tv->pending_redraw = true; tv->pending_from_line = fromnline; tv->pending_from_end = fromend; } return true; } if (tv->pending_redraw) { tv->pending_redraw = false; fromnline = tv->pending_from_line; fromend = tv->pending_from_end; } return TVRedrawAppendLocked(tv, fromnline, fromend); } Boolean TVRedrawAppendLocked(struct TVRec *tv, unsigned long fromnline, unsigned long fromend) { struct TVLine *fromline, *toline; RgnHandle vacated; BigRect redraw; Rect r; unsigned long wastop; if (!TVCalcLinesLocked(tv, false)) return false; fromline = TVGetLine(tv, fromnline); /* keep scrolling and redrawing in one CopyBits */ TVUseCanvas(tv); if (tv->autoscroll && tv->scrolled.bottom > tv->frame.bottom) { wastop = tv->scrolled.top; tv->scrolled.bottom = tv->frame.bottom; tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; vacated = NewRgn(); ScrollRect(&tv->frame, 0, tv->scrolled.top - wastop, vacated); DisposeRgn(vacated); tv->canvas_dirty = true; } if (fromline && fromline->nline == tv->nlines) toline = fromline; else toline = TVGetLine(tv, tv->nlines); redraw.top = fromline ? fromline->rect.top : tv->padding.top; redraw.bottom = toline->rect.bottom; redraw.left = 0; redraw.right = tv->frame.right; if (tv->scrolled.top + redraw.bottom > 0L && tv->scrolled.top + redraw.top < tv->frame.bottom) { r.left = redraw.left + tv->scrolled.left; r.right = redraw.right + tv->scrolled.left; if (tv->scrolled.top + redraw.top < 0L) r.top = 0; else r.top = tv->scrolled.top + redraw.top; if (tv->scrolled.top + redraw.bottom > tv->frame.bottom) r.bottom = tv->frame.bottom; else r.bottom = tv->scrolled.top + redraw.bottom; TVRedrawRect(tv, &r, fromline ? fromline->nline : 0, (fromline != NULL && fromline->end < fromend) || (tv->active && tv->sel_start != tv->sel_end && fromline != NULL && tv->sel_end > fromline->start)); } TVRevealCanvas(tv); return true; }