jcs
/textview
/amendments
/65
*: Split up into logical files for storage, layout, and drawing
This file is getting huge and hard to manage. We can always cat
everything together later for a single distributable file.
jcs made amendment 65 2 months ago
--- textview.c Wed Sep 23 13:10:54 2026
+++ textview.c Wed Sep 23 13:51:52 2026
@@ -19,46 +19,6 @@
#include <string.h>
#include "textview.h"
-//#define USE_CANVAS 1
-
-/* NOTE: update TVFindStyle when additional fields are added */
-#define ARE_STYLES_EQUAL(one, two) \
- ((one)->font == (two)->font && \
- (one)->style == (two)->style && \
- (one)->size == (two)->size && \
- (one)->align == (two)->align && \
- (one)->tag == (two)->tag && \
- (one)->bitmap_data_size == (two)->bitmap_data_size)
-
-struct TVRec * TVLock(TVHandle tvh);
-void TVUnlock(struct TVRec *);
-Boolean TVSetupCanvas(struct TVRec *tv);
-Handle TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first,
- Handle *last);
-Handle TVAllocateLineGroup(struct TVRec *tv, short groupn);
-struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsigned long addr);
-struct TVChunk * TVGetChunk(struct TVRec *tv, TVSlabLocator loc);
-struct TVChunk * TVCreateChunk(struct TVRec *tv);
-struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line);
-Boolean TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text,
- unsigned long length, char terminator);
-short TVScrollDivisor(struct TVRec *tv);
-Boolean TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty);
-void TVApplyStyle(struct TVStyle *style);
-struct TVStyle * TVGetStyle(struct TVRec *tv, TVSlabLocator loc);
-struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style);
-void TVScrollUnlocked(struct TVRec *tv, long horiz, long vert);
-Boolean TVFindInRectLocked(struct TVRec *tv, BigRect *findrect,
- long startline, TVFindInRectCallback callback, void *cookie);
-Boolean TVGetTextOffsetCallback(struct TVRec *tv,
- struct TVFindInRectMatch *match, void *cookie);
-Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
- void *cookie);
-Boolean TVSetTopBottomMatch(struct TVRec *tv,
- struct TVFindInRectMatch *match, void *cookie);
-void TVUseCanvas(struct TVRec *tv);
-void TVRevealCanvas(struct TVRec *tv);
-
TVHandle
TVNew(Rect *view, Rect *padding)
{
@@ -224,116 +184,6 @@ TVFindInRect(TVHandle tvh, BigRect *findrect, long sta
return ret;
}
-Boolean
-TVResize(TVHandle tvh)
-{
- struct TVRec *tv;
- long width, height, n;
- struct TVLine *line;
- struct TVChunk *chunk;
- BigRect visible;
- long nline;
- unsigned long top_start;
-
- tv = TVLock(tvh);
-
- TVUseCanvas(tv);
- EraseRect(&tv->frame);
- TVRevealCanvas(tv);
-
- /* find text position of first line to try to stay there */
- top_start = 0;
- for (nline = 1; nline <= tv->nlines; nline++) {
- line = TVGetLine(tv, nline);
- if (line == NULL)
- break;
-
- if (tv->scrolled.top + line->rect.bottom < 0L)
- /* is not in the view at all yet */
- continue;
-
- top_start = line->start;
- break;
- }
-
-#ifdef USE_CANVAS
- if (!TVSetupCanvas(tv)) {
- TVUnlock(tv);
- return false;
- }
-#endif
- SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left,
- tv->view.bottom - tv->view.top);
- tv->bounds.right = tv->frame.right;
- tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
- tv->scrolled = tv->bounds;
-
- /* 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;
- DisposHandle(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->nlines = 0;
- tv->last_space = 0;
- tv->space_width = 0;
-
- for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->next_chunk)) {
- chunk->start_line = 0;
- chunk->end_line = 0;
- chunk->start_left = 0;
- chunk->end_right = 0;
- chunk->need_recalc = true;
- }
-
- if (!TVCalcLinesLocked(tv, true)) {
- /* XXX: if this fails, what are we supposed to do? */
- TVUnlock(tv);
- return false;
- }
-
- if (tv->bounds.bottom > tv->frame.bottom) {
- if (tv->autoscroll) {
- tv->scrolled.bottom = tv->frame.bottom;
- tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
- } else {
- for (nline = 1; nline <= tv->nlines; nline++) {
- line = TVGetLine(tv, nline);
- if (line == NULL)
- break;
-
- if (line->start <= top_start && top_start < line->end) {
- /* keep this line at the top */
- tv->scrolled.top = -(line->rect.top);
- tv->scrolled.bottom = tv->scrolled.top +
- tv->bounds.bottom;
- break;
- }
- }
- }
- }
-
- TVUseCanvas(tv);
-
- visible.top = -(tv->scrolled.top);
- visible.left = -(tv->scrolled.left);
- visible.bottom = visible.top + tv->frame.bottom;
- visible.right = visible.left + tv->frame.right;
- TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
-
- TVRevealCanvas(tv);
- TVUnlock(tv);
-
- return true;
-}
-
void
TVAutoCalc(TVHandle tvh, Boolean calc)
{
@@ -375,55 +225,6 @@ TVTabStop(TVHandle tvh, unsigned short tabstop)
}
void
-TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller)
-{
- struct TVRec *tv;
- long max;
- RgnHandle rgn;
- short wasval, wasmax, val;
-
- 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).
- */
- max = (tv->bounds.bottom - tv->bounds.top -
- (tv->view.bottom - tv->view.top)) / TVScrollDivisor(tv);
- if (max < 0)
- max = 0;
-
- wasval = GetCtlValue(scroller);
-
- HLock(scroller);
- wasmax = (*scroller)->contrlMax;
- (*scroller)->contrlMin = 0;
- val = -(tv->scrolled.top) / TVScrollDivisor(tv);
- (*scroller)->contrlMax = max;
- SetCtlValue(scroller, val);
- HUnlock(scroller);
-
- if ((wasmax == 0 && max != 0) || (wasmax != 0 && max == 0))
- goto redraw;
- if (wasval == 0 && val == 0)
- goto done;
- if (wasval == wasmax && val == max && tv->autoscroll)
- goto done;
-
-redraw:
- rgn = NewRgn();
- HLock(scroller);
- RectRgn(rgn, &(*scroller)->contrlRect);
- UpdtControl((*(scroller))->contrlOwner, rgn);
- HUnlock(scroller);
- DisposeRgn(rgn);
-
-done:
- TVUnlock(tv);
-}
-
-void
TVClick(TVHandle tvh, Point p, Boolean extend)
{
struct TVRec *tv;
@@ -453,33 +254,6 @@ TVClick(TVHandle tvh, Point p, Boolean extend)
TVUnlock(tv);
}
-Boolean
-TVGetTextOffsetCallback(struct TVRec *tv, struct TVFindInRectMatch *match,
- void *cookie)
-{
- unsigned long n, left;
- long *off;
- short width;
- unsigned char c;
-
- off = (long *)cookie;
- left = match->rect.left;
-
- for (n = match->text_start; n < match->text_end; n++) {
- c = match->text_slab_data[n - match->text_slab->base];
- width = match->style->width_cache[c];
-
- if (left + width >= match->findrect.left) {
- *off = n;
- /* stop getting additional matches */
- return false;
- }
- left += width;
- }
-
- return true;
-}
-
unsigned long
TVGetText(TVHandle tvh, char *dst, unsigned long start, unsigned long end)
{
@@ -519,30 +293,6 @@ TVGetText(TVHandle tvh, char *dst, unsigned long start
return read;
}
-long
-TVGetTextOffset(TVHandle tvh, Point p)
-{
- struct TVRec *tv;
- BigRect prect;
- long off;
-
- tv = TVLock(tvh);
- TVUseCanvas(tv);
-
- prect.left = (long)p.h - tv->scrolled.left - tv->view.left;
- prect.top = (long)p.v - tv->scrolled.top - tv->view.top;
- prect.right = prect.left;
- prect.bottom = prect.top;
-
- off = 0;
- if (!TVFindInRectLocked(tv, &prect, 0, TVGetTextOffsetCallback, &off))
- off = -1;
-
- TVRevealCanvas(tv);
- TVUnlock(tv);
- return off;
-}
-
void
TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, Rect *rect)
{
@@ -558,138 +308,7 @@ TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, R
TVUnlock(tv);
}
-static TVHandle tv_track_control = NULL;
void
-TVSetTrackScrollControl(TVHandle tvh)
-{
- tv_track_control = tvh;
-}
-
-pascal void
-TVTrackScrollControl(ControlHandle control, short part)
-{
- struct TVRec *tv;
- struct TVChunk *chunk, *top_chunk = NULL;
- struct TVLine *line, *oline;
- KeyMap km;
- BigRect visible;
- long n, top;
- unsigned long nline;
- Boolean horiz, cmd_down;
-
- 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));
-
- GetKeys(&km);
- cmd_down = (km[1] & (1 << 15)) != 0;
-
- tv->top_line = NULL;
- tv->bottom_line = NULL;
-
- visible.top = -(tv->scrolled.top);
- visible.left = -(tv->scrolled.left);
- visible.bottom = visible.top + tv->frame.bottom;
- visible.right = visible.left + tv->frame.right;
- TVFindInRectLocked(tv, &visible, 0, TVSetTopBottomMatch, NULL);
-
- if (tv->top_line == NULL) {
- tv->top_line = TVGetLine(tv, 1);
- if (tv->top_line == NULL) {
- TVUnlock(tv);
- return;
- }
- }
- if (tv->bottom_line == NULL) {
- tv->bottom_line = TVGetLine(tv, tv->nlines);
- if (tv->bottom_line == NULL) {
- TVUnlock(tv);
- return;
- }
- }
-
- switch (part) {
- case inUpButton:
- if (cmd_down) {
- TVScrollUnlocked(tv, 0, 1);
- break;
- }
-
- /* scroll to the line before the top-most line */
- if (tv->top_line->nline > 1) {
- oline = TVGetLine(tv, tv->top_line->nline - 1);
- if (oline == NULL)
- break;
- TVScrollUnlocked(tv, 0, tv->top_line->rect.top -
- oline->rect.top + 1);
- break;
- }
-
- /* nothing found, just scroll to the top */
- TVScrollUnlocked(tv, 0, tv->bounds.bottom);
- break;
- case inPageUp:
- /* scroll to put the top mostly-visible line at the bottom */
- TVScrollUnlocked(tv, 0, (long)tv->frame.bottom -
- (tv->scrolled.top + tv->top_line->rect.bottom) - 1);
- break;
- case inDownButton:
- if (cmd_down) {
- TVScrollUnlocked(tv, 0, -1);
- break;
- }
-
- /* scroll to the line after the bottom-most line */
- if (tv->bottom_line->nline < tv->nlines) {
- line = TVGetLine(tv, tv->bottom_line->nline + 1);
- if (line == NULL)
- break;
- TVScrollUnlocked(tv, 0,
- -(tv->scrolled.top - tv->frame.bottom + line->rect.bottom));
- break;
- }
-
- /* nothing found, just scroll to the bottom */
- TVScrollUnlocked(tv, 0, -(tv->bounds.bottom));
- break;
- case inPageDown:
- /* scroll to put the bottom mostly-visible line at the top */
- if (tv->bottom_line->nline < tv->nlines) {
- line = TVGetLine(tv, tv->bottom_line->nline + 1);
- if (line == NULL)
- break;
- TVScrollUnlocked(tv, 0, -(line->rect.top + tv->scrolled.top) +
- (line->rect.bottom - line->rect.top) + 1);
- break;
- }
-
- /* nothing found, just scroll to the bottom */
- TVScrollUnlocked(tv, 0, -(tv->bounds.bottom));
- break;
- }
-
- SetCtlValue(control, -(tv->scrolled.top) / TVScrollDivisor(tv));
- TVUnlock(tv);
-}
-
-void
-TVScroll(TVHandle tvh, long horiz, long vert)
-{
- struct TVRec *tv;
-
- tv = TVLock(tvh);
- TVScrollUnlocked(tv, horiz * TVScrollDivisor(tv),
- vert * TVScrollDivisor(tv));
- TVUnlock(tv);
-}
-
-void
TVCopy(TVHandle tvh)
{
}
@@ -791,30 +410,6 @@ TVClear(TVHandle tvh)
}
void
-TVUpdate(TVHandle tvh)
-{
- struct TVRec *tv;
- BigRect visible;
-
- tv = TVLock(tvh);
- TVUseCanvas(tv);
-
-#ifdef USE_CANVAS
- EraseRect(&tv->frame);
- tv->canvas_dirty = true;
-#endif
-
- visible.top = -(tv->scrolled.top);
- visible.left = -(tv->scrolled.left);
- visible.bottom = visible.top + tv->frame.bottom;
- visible.right = visible.left + tv->frame.right;
- TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
-
- TVRevealCanvas(tv);
- TVUnlock(tv);
-}
-
-void
TVDispose(TVHandle tvh)
{
struct TVRec *tv;
@@ -904,384 +499,6 @@ TVUnlock(struct TVRec *tv)
}
Boolean
-TVSetupCanvas(struct TVRec *tv)
-{
- struct TVLine *line;
- long width, height;
- GrafPtr old_port, canvas = NULL;
- Handle canvas_baseaddr = NULL;
-
- /*
- * TODO: make canvas 3 times vis portal height so we can pre-render
- * pages above and below to make scrolling seem faster?
- */
- width = tv->view.right - tv->view.left;
- height = tv->view.bottom - tv->view.top;
-
- /* this must be a Ptr because it cannot move once opened */
- canvas = (GrafPtr)NewPtrClear(sizeof(GrafPort));
- if (canvas == NULL)
- goto fail;
-
- canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2;
- canvas_baseaddr = NewHandleClear((long)canvas->portBits.rowBytes *
- height);
- if (canvas_baseaddr == NULL)
- goto fail;
-
- /* everything allocated, commit to new canvas */
-
- if (tv->canvas) {
- ClosePort(tv->canvas);
- DisposPtr(tv->canvas);
- }
- if (tv->canvas_baseaddr)
- DisposHandle(tv->canvas_baseaddr);
-
- tv->canvas = canvas;
-
- /* opening the port resets portRect/portBits */
- GetPort(&old_port);
- OpenPort(tv->canvas);
- RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect);
- RectRgn(tv->canvas->visRgn, &tv->canvas->portRect);
- SetPort(old_port);
-
- 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_baseaddr = canvas_baseaddr;
- HLock(tv->canvas_baseaddr);
- tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
-
- return true;
-
-fail:
- if (canvas)
- DisposPtr(canvas);
- if (canvas_baseaddr)
- DisposHandle(canvas_baseaddr);
- return false;
-}
-
-Handle
-TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first,
- Handle *last)
-{
- Handle slabh;
- struct TVSlab *slab, *lslab;
- unsigned short id;
-
- if (last == NULL || *last == NULL)
- id = 1;
- else {
- HLock(*last);
- slab = (struct TVSlab *)**last;
- if (slab->id == USHRT_MAX) {
- Debugger();
- HUnlock(*last);
- return NULL;
- }
- id = slab->id + 1;
- HUnlock(*last);
- }
-
- slabh = NewHandleClear(sizeof(struct TVSlab) + size);
- if (slabh == NULL)
- /* TODO: try smaller allocations */
- return NULL;
-
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
- slab->id = id;
- slab->size = size;
-
- if (last != NULL && *last != NULL) {
- HLock(*last);
- lslab = (struct TVSlab *)**last;
- lslab->next_slab = slabh;
- slab->prev_slab = *last;
- slab->base = lslab->base + lslab->size;
- }
-
- if (first != NULL && *first == NULL)
- *first = slabh;
- if (last != NULL)
- *last = slabh;
-
- /* leave locked */
-
- return slabh;
-}
-
-Handle
-TVAllocateLineGroup(struct TVRec *tv, short groupn)
-{
- short n;
-
- if (groupn >= sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))
- return NULL;
-
- if (tv->line_groups[groupn] == NULL)
- tv->line_groups[groupn] = NewHandleClear(TV_LINE_GROUP_SIZE);
-
- return tv->line_groups[groupn];
-}
-
-struct TVSlab *
-TVGetTextSlab(struct TVRec *tv, unsigned long addr)
-{
- Handle slabh;
- struct TVSlab *slab;
-
- slabh = tv->first_text_slab;
- while (slabh) {
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
- if (addr >= slab->base && addr < slab->base + slab->size)
- /* leave locked */
- return slab;
-
- slabh = slab->next_slab;
- }
-
- return NULL;
-}
-
-struct TVChunk *
-TVGetChunk(struct TVRec *tv, TVSlabLocator loc)
-{
- Handle slabh;
- struct TVSlab *slab;
- char *addr;
-
- if (loc == 0)
- return NULL;
-
- slabh = tv->first_chunk_slab;
- while (slabh) {
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
- if (slab->id == SLABLOC_SLABID(loc))
- break;
- slabh = slab->next_slab;
- }
- if (slabh == NULL)
- return NULL;
-
- addr = slab->data + SLABLOC_OFFSET(loc);
- if ((unsigned long)addr % 2 != 0) {
- Debugger();
- return NULL;
- }
-
- /* keep slab locked */
-
- return (struct TVChunk *)addr;
-}
-
-struct TVChunk *
-TVCreateChunk(struct TVRec *tv)
-{
- Handle slabh;
- struct TVSlab *slab;
- struct TVChunk *chunk;
- TVSlabLocator cloc;
-
- slabh = tv->last_chunk_slab;
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
-
- if (slab->size - slab->pos < sizeof(struct TVChunk)) {
- if ((slabh = TVAllocateSlab(tv, TV_CHUNK_SLAB_SIZE,
- &tv->first_chunk_slab, &tv->last_chunk_slab)) == NULL)
- return NULL;
- slab = (struct TVSlab *)*slabh;
- }
-
- SET_SLABLOC_SLABID(cloc, slab->id);
- SET_SLABLOC_OFFSET(cloc, slab->pos);
-
- chunk = TVGetChunk(tv, cloc);
- chunk->loc = cloc;
- slab->pos += sizeof(struct TVChunk);
-
- if (!tv->first_chunk)
- tv->first_chunk = cloc;
-
- tv->last_chunk = cloc;
-
- return chunk;
-}
-
-struct TVLine *
-TVGetLine(struct TVRec *tv, unsigned long line)
-{
- Handle group;
- unsigned short groupn, idx;
-
- if (line == 0)
- return NULL;
-
- groupn = (line - 1) / TV_LINE_GROUP_NLINES;
- if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0])))
- return NULL;
-
- idx = line - 1 - (groupn * TV_LINE_GROUP_NLINES);
-
- group = tv->line_groups[groupn];
- if (group == NULL && (group = TVAllocateLineGroup(tv, groupn)) == NULL)
- return NULL;
-
- HLock(group);
- /* keep group locked */
-
- return (struct TVLine *)(*group + (sizeof(struct TVLine) * idx));
-}
-
-void
-TVApplyStyle(struct TVStyle *style)
-{
- TextFont(style->font);
- TextFace(style->style);
- TextSize(style->size);
-}
-
-struct TVStyle *
-TVGetStyle(struct TVRec *tv, TVSlabLocator loc)
-{
- Handle slabh;
- struct TVSlab *slab;
- struct TVStyle *style;
- short n;
-
- slabh = tv->first_style_slab;
- while (slabh) {
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
-
- if (slab->id != SLABLOC_SLABID(loc)) {
- slabh = slab->next_slab;
- continue;
- }
-
- style = (struct TVStyle *)(slab->data + SLABLOC_OFFSET(loc));
- if (style->loc != loc) {
- /* locator pointed at bogus style */
- Debugger();
- return NULL;
- }
- return style;
- }
-
- return NULL;
-}
-
-struct TVStyle *
-TVFindStyle(struct TVRec *tv, struct TVStyle *style)
-{
- Handle slabh;
- struct TVSlab *slab;
- struct TVStyle *tstyle;
- short n;
-
- if (style->loc)
- return TVGetStyle(tv, style->loc);
-
- slabh = tv->first_style_slab;
- while (slabh) {
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
- tstyle = (struct TVStyle *)(slab->data);
-
-iterate_slab:
- if (tstyle->loc == 0)
- goto next_slab;
-
- if (!ARE_STYLES_EQUAL(tstyle, style))
- goto next_style;
-
- if (tstyle->bitmap_data_size &&
- memcmp(tstyle->bitmap_data, style->bitmap.baseAddr,
- tstyle->bitmap_data_size) != 0)
- goto next_style;
-
- TVApplyStyle(tstyle);
- tv->cur_style = tstyle->loc;
- return tstyle;
-
-next_style:
- if (SLABLOC_OFFSET(tstyle->loc) + sizeof(struct TVStyle) +
- tstyle->bitmap_data_size + sizeof(struct TVStyle) > slab->size)
- goto next_slab;
-
- tstyle = (struct TVStyle *)((char *)tstyle +
- sizeof(struct TVStyle) + tstyle->bitmap_data_size);
- if (tstyle->loc == 0) {
- if (slab->next_slab)
- goto next_slab;
- goto create_style;
- }
- goto iterate_slab;
-
-next_slab:
- slabh = slab->next_slab;
- }
-
- /* no matching style found, create a new one */
-create_style:
- slabh = tv->last_style_slab;
- HLock(slabh);
- slab = (struct TVSlab *)*slabh;
-
- if (slab->pos + sizeof(struct TVStyle) + style->bitmap_data_size >
- slab->size) {
- /* need a new slab */
- slabh = TVAllocateSlab(tv, TV_STYLE_SLAB_SIZE,
- &tv->first_style_slab, &tv->last_style_slab);
- if (slabh == NULL)
- return NULL;
- slab = (struct TVSlab *)*slabh;
- }
-
- tstyle = (struct TVStyle *)(slab->data + slab->pos);
- SET_SLABLOC_SLABID(tstyle->loc, slab->id);
- SET_SLABLOC_OFFSET(tstyle->loc, slab->pos);
-
- /* only copy over the important fields, rest will stay 0 */
- /* NOTE: keep this in sync with ARE_STYLES_EQUAL */
- tstyle->font = style->font;
- tstyle->style = style->style;
- tstyle->size = style->size;
- tstyle->align = style->align;
- tstyle->tag = style->tag;
- if (style->bitmap_data_size) {
- tstyle->font_info = style->font_info;
- tstyle->bitmap_data_size = style->bitmap_data_size;
- tstyle->bitmap = style->bitmap;
- memcpy(tstyle->bitmap_data, style->bitmap.baseAddr,
- style->bitmap_data_size);
- /* this must be recalculated at draw time once slab is locked */
- tstyle->bitmap.baseAddr = 0;
- }
-
- slab->pos += sizeof(struct TVStyle) + tstyle->bitmap_data_size;
- if (slab->pos > slab->size) {
- Debugger();
- return NULL;
- }
-
- TVApplyStyle(tstyle);
- if (!style->bitmap_data_size || style->font_info.widMax == 0)
- GetFontInfo(&tstyle->font_info);
- tv->cur_style = tstyle->loc;
-
- return tstyle;
-}
-
-Boolean
TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text,
unsigned long length, char terminator)
{
@@ -1409,622 +626,4 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
TVRevealCanvas(tv);
return true;
-}
-
-short
-TVScrollDivisor(struct TVRec *tv)
-{
- short div;
- long max;
-
- div = 1;
- max = tv->bounds.bottom - tv->bounds.top -
- (tv->view.bottom - tv->view.top);
- while (max > SHRT_MAX) {
- div *= 10;
- max /= 10;
- }
-
- if (div == 0)
- Debugger();
- return div;
-}
-
-Boolean
-TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty)
-{
- struct TVSlab *text_slab;
- struct TVChunk *chunk, *tchunk, *last_chunk, *prev_chunk, *next_chunk;
- struct TVStyle *style;
- struct TVLine *line, *prev_line, *next_line;
- unsigned long avail, start, nline, max_right;
- long n, back_width, back_n, next_tabstop;
- short width, height;
- char *text_slab_data;
- register unsigned char c;
- Boolean new_line;
-
- if (ignore_dirty)
- chunk = TVGetChunk(tv, tv->first_chunk);
- else {
- /* find first dirty chunk but search from the end */
- last_chunk = TVGetChunk(tv, tv->last_chunk);
- chunk = last_chunk;
- while (chunk != NULL) {
- prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
- if (!prev_chunk || !prev_chunk->need_recalc)
- break;
- chunk = prev_chunk;
- }
- }
-
- max_right = tv->view.right - tv->view.left - tv->padding.left -
- tv->padding.right;
-
- for (; chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) {
- new_line = false;
-
-reload_chunk:
- style = TVGetStyle(tv, chunk->style);
- if (style == NULL)
- return false;
- TVApplyStyle(style);
-
- if (chunk->end_line == 0) {
- /* working with a new text chunk */
- if (chunk->prev_chunk == 0) {
- prev_chunk = NULL;
- chunk->start_left = tv->padding.left;
- chunk->start_line = 1;
- chunk->end_right = chunk->start_left;
- chunk->end_line = 1;
- } else {
- prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
- if (prev_chunk == NULL)
- return false;
- chunk->start_left = prev_chunk->end_right;
- chunk->start_line = prev_chunk->end_line;
- chunk->end_right = chunk->start_left;
- chunk->end_line = chunk->start_line;
- }
- }
-
- /* setup text slab */
- text_slab = TVGetTextSlab(tv, chunk->start);
- if (text_slab == NULL)
- return false;
- text_slab_data = text_slab->data;
-
- /* find our current line */
- nline = chunk->end_line;
- line = TVGetLine(tv, nline);
- if (line == NULL)
- return false;
- if (nline == 1) {
- line->rect.left = tv->padding.left;
- line->rect.top = tv->padding.top;
- line->start = 0;
- start = line->end;
- } else {
- prev_line = TVGetLine(tv, nline - 1);
- if (prev_line == NULL)
- return false;
- line->rect.top = prev_line->rect.bottom;
- if (line->end)
- start = line->end;
- else
- start = prev_line->end;
- }
-
- line->nline = nline;
- height = style->font_info.ascent + style->font_info.descent +
- style->font_info.leading;
-
- /*
- * Walk each character and find its width, possibly wrapping and
- * starting a new line.
- */
- for (n = start; n < chunk->end; n++) {
- c = text_slab_data[n - text_slab->base];
-
- if (c == '\r') {
- new_line = true;
- width = 0;
- } else {
- if (style->bitmap_data_size)
- width = style->bitmap.bounds.right -
- style->bitmap.bounds.left;
- else if (c == '\t' && tv->tabstop) {
- next_tabstop = line->rect.left +
- (long)tv->tabstop *
- (((chunk->end_right - line->rect.left) /
- tv->tabstop) + 1);
- width = next_tabstop - chunk->end_right;
- } else if (!(width = style->width_cache[c]))
- width = style->width_cache[c] = CharWidth(c);
-
- if (c == ' ' && !style->bitmap_data_size) {
- tv->last_space = n;
- tv->space_width = 0;
- } else if (!new_line && tv->linewrap &&
- chunk->end_right + width >= max_right) {
- new_line = true;
- if (tv->last_space) {
- /* break after the last space */
- n = tv->last_space + 1;
- chunk->end_right -= tv->space_width;
- line->end = tv->last_space + 1;
- line->rect.right = chunk->end_right;
-
- while (n < chunk->start) {
- if (!chunk->prev_chunk) {
- /* how did we get here? */
- Debugger();
- return false;
- }
- chunk = TVGetChunk(tv, chunk->prev_chunk);
- }
-
- /* setup chunk with new_line set */
- goto reload_chunk;
- }
- /* otherwise, hard-break this long word */
- } else
- tv->space_width += width;
- }
-
- if (line->start == line->end) {
- /* start of new line disregards previous line's style */
- line->baseline = style->font_info.descent;
- line->leading = style->font_info.leading;
- line->rect.bottom = line->rect.top + height + line->leading;
- }
-
- if (new_line) {
- new_line = false;
- tv->space_width = 0;
- tv->last_space = 0;
-
- line->end = n;
- line->rect.right = chunk->end_right;
-
- if (style->align == TV_ALIGN_CENTER)
- line->indent = (max_right - line->rect.right -
- line->rect.left) / 2;
- else if (style->align == TV_ALIGN_RIGHT)
- line->indent = max_right - line->rect.right -
- line->rect.left;
- else
- line->indent = 0;
-
- if (n == chunk->start) {
- /* no text on previous line, start on next line */
- chunk->start_left = tv->padding.left;
- chunk->end_right = chunk->start_left;
- chunk->start_line++;
- } else if (height > line->rect.bottom - line->rect.top)
- line->rect.bottom = line->rect.top + height;
-
- nline++;
- chunk->end_line = nline;
- chunk->end_right = tv->padding.left;
- next_line = TVGetLine(tv, nline);
- if (next_line == NULL)
- return false;
- next_line->nline = nline;
- next_line->start = n;
- if (c == '\r')
- next_line->start++;
- next_line->leading = style->font_info.leading;
- next_line->rect.top = line->rect.bottom;
- next_line->rect.left = tv->padding.left;
- next_line->rect.right = chunk->end_right;
- next_line->rect.bottom = next_line->rect.top;
- if (n + 1 < chunk->end)
- next_line->rect.bottom += height;
-
- line = next_line;
- }
-
- chunk->end_right += width;
-
- if (line->start != line->end) {
- /* if style changes mid-line, previous chunks may grow */
- if (style->font_info.descent > line->baseline) {
- if (line->baseline)
- line->grew = true;
- line->baseline = style->font_info.descent;
- }
- if (style->font_info.leading > line->leading) {
- if (line->leading)
- line->grew = true;
- line->leading = style->font_info.leading;
- }
- if (line->rect.bottom == 0 ||
- (line->rect.bottom - line->rect.top < height)) {
- if (line->rect.bottom != line->rect.top &&
- line->rect.bottom != 0)
- line->grew = true;
- line->rect.bottom = line->rect.top + height;
- }
- }
- }
-
- line->end = chunk->end;
- line->rect.right = chunk->end_right;
- chunk->end_line = nline;
- chunk->need_recalc = false;
-
- if (style->align == TV_ALIGN_CENTER)
- line->indent = (max_right - line->rect.right -
- line->rect.left) / 2;
- else if (style->align == TV_ALIGN_RIGHT)
- line->indent = max_right - line->rect.right - line->rect.left;
- else
- line->indent = 0;
- }
-
-done_chunks:
- tv->nlines = nline;
-
- line = TVGetLine(tv, nline);
- if (line == NULL)
- return false;
-
- if (line->rect.bottom > tv->bounds.bottom - tv->padding.bottom) {
- tv->bounds.bottom = line->rect.bottom + tv->padding.bottom;
- tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
- }
-
- return true;
-}
-
-Boolean
-TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
- void *cookie)
-{
- unsigned long n, j;
- long next_tabstop, origin;
- unsigned char c;
- char *text, *spanstart;
- short ts;
- Rect r;
- Point pen;
-
- if (match->text_end == match->text_start)
- return true;
-
- if (match->rect.bottom < -(tv->scrolled.top))
- /* no sense drawing what we can't see yet */
- return true;
-
- if (match->rect.top >= -(tv->scrolled.top) + tv->frame.bottom)
- /* further down than the scrolled view, we've drawn enough lines */
- return false;
-
- if (match->line->grew) {
- r.left = tv->scrolled.left + match->line->rect.left;
- r.top = tv->scrolled.top + match->line->rect.top;
- r.right = tv->scrolled.left + match->line->rect.right;
- r.bottom = tv->scrolled.top + match->line->rect.bottom;
- EraseRect(&r);
- match->line->grew = false;
- } else if (match->style->align == TV_ALIGN_CENTER) {
- /* erase from this chunk to the right of the view */
- r.left = tv->scrolled.left + match->rect.left;
- r.top = tv->scrolled.top + match->line->rect.top;
- r.right = tv->scrolled.left + tv->view.right - tv->padding.right;
- r.bottom = tv->scrolled.top + match->line->rect.bottom;
- EraseRect(&r);
- } else if (match->style->align == TV_ALIGN_RIGHT) {
- /* erase from the right of the view to the right of the view */
- r.left = tv->scrolled.left + match->rect.left;
- r.top = tv->scrolled.top + match->line->rect.top;
- r.right = tv->scrolled.left + tv->view.right - tv->padding.right;
- r.bottom = tv->scrolled.top + match->line->rect.bottom;
- EraseRect(&r);
- }
-
- MoveTo(match->rect.left, tv->scrolled.top + match->rect.bottom -
- match->line->baseline);
- if (match->style->bitmap_data_size) {
- r = match->style->bitmap.bounds;
- r.left = match->rect.left;
- r.right = r.left + (match->style->bitmap.bounds.right -
- match->style->bitmap.bounds.left);
- r.bottom = tv->scrolled.top + match->line->rect.bottom;
- r.top = r.bottom - (match->style->bitmap.bounds.bottom -
- match->style->bitmap.bounds.top);
-
- match->style->bitmap.baseAddr = (QDPtr)match->style->bitmap_data;
- CopyBits(&match->style->bitmap, &thePort->portBits,
- &match->style->bitmap.bounds, &r, srcOr, NULL);
- } else if (tv->tabstop) {
- origin = match->line->rect.left + match->line->indent;
- text = match->text_slab_data + (match->text_start -
- match->text_slab->base);
- spanstart = NULL;
- for (n = 0; n < match->text_end - match->text_start; n++) {
- c = *text;
- if (c == '\t') {
- if (spanstart != NULL)
- DrawText(spanstart, 0, text - spanstart);
- GetPen(&pen);
- next_tabstop = origin + ((long)tv->tabstop *
- (((pen.h - origin) / tv->tabstop) + 1));
-#if 0
- /* draw dotted lines for tabs */
- /* XXX: disabled for now but might be useful as an option */
- PenPat(&tabpat);
- MoveTo(pen.h, tv->scrolled.top + match->line->rect.top +
- ((match->line->rect.bottom - match->line->rect.top) / 2));
- GetPen(&pen);
- LineTo(next_tabstop - 1, pen.v);
- PenPat(black);
- MoveTo(next_tabstop, tv->scrolled.top +
- match->line->rect.bottom - match->line->baseline);
-#else
- MoveTo(next_tabstop, pen.v);
-#endif
- spanstart = NULL;
- } else if (spanstart == NULL)
- spanstart = text;
- text++;
- }
- if (spanstart != NULL)
- DrawText(spanstart, 0, text - spanstart);
- } else
- DrawText(match->text_slab_data + (match->text_start -
- match->text_slab->base), 0, match->text_end - match->text_start);
- tv->canvas_dirty = true;
-
- return true;
-}
-
-Boolean
-TVFindInRectLocked(struct TVRec *tv, BigRect *rect, long startline,
- TVFindInRectCallback callback, void *cookie)
-{
- struct TVFindInRectMatch match;
- unsigned long nline, ppl;
- Boolean ret;
-
- TVUseCanvas(tv);
-
- match.findrect = *rect;
- match.chunk = NULL;
- ret = false;
-
- if (startline == 0 && tv->nlines > 1) {
- /* take a stab at getting us closer */
- ppl = (tv->bounds.bottom / tv->nlines);
- if (ppl == 0)
- ppl = 1;
- startline = rect->top / ppl;
- if (startline > tv->nlines)
- startline = tv->nlines;
- if (startline > 1) {
- match.line = TVGetLine(tv, startline);
- while (startline > 1 && match.line != NULL &&
- match.line->rect.top > rect->top) {
- startline--;
- match.line = TVGetLine(tv, startline);
- }
- }
- }
-
- if (startline == 0)
- startline = 1;
-
- for (nline = startline; nline <= tv->nlines; nline++) {
- match.line = TVGetLine(tv, nline);
- if (match.line == NULL)
- goto draw_done;
-
- if (match.line->rect.bottom < rect->top)
- /* is not in the view at all yet */
- continue;
-
- if (match.line->rect.top >= rect->bottom)
- /* is further down than the view, we've done enough lines */
- break;
-
- /* advance chunk if we need to catch up to this line */
- if (match.chunk == NULL || match.chunk->end_line < nline) {
- if (match.chunk == NULL) {
- match.chunk = TVGetChunk(tv, tv->first_chunk);
- if (match.chunk == NULL)
- goto draw_done;
- }
-
- while (match.chunk->end_line < nline ||
- match.chunk->end < match.line->start) {
- match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
- if (!match.chunk)
- goto draw_done;
- }
-
-prep_chunk:
- match.text_slab = TVGetTextSlab(tv, match.chunk->start);
- if (match.text_slab == NULL)
- goto draw_done;
- match.text_slab_data = match.text_slab->data;
-
- match.style = TVGetStyle(tv, match.chunk->style);
- if (match.style == NULL)
- goto draw_done;
- if (!match.style->bitmap_data_size)
- TVApplyStyle(match.style);
- }
-
- match.rect.top = match.line->rect.top;
- match.rect.bottom = match.line->rect.bottom;
-
- if (match.line->start > match.chunk->start) {
- match.text_start = match.line->start;
- match.rect.left = match.line->rect.left + match.line->indent;
- } else {
- match.text_start = match.chunk->start;
- match.rect.left = match.chunk->start_left + match.line->indent;
- }
-
- if (match.line->end < match.chunk->end) {
- match.text_end = match.line->end;
- match.rect.right = match.line->rect.right + match.line->indent;
- } else {
- match.text_end = match.chunk->end;
- match.rect.right = match.chunk->end_right + match.line->indent;
- }
-
- ret = true;
-
- if (callback(tv, &match, cookie) == false)
- /* callback requested we stop matching */
- goto draw_done;
-
- if (match.chunk->end_line == nline &&
- match.chunk->end < match.line->end) {
- /* more chunks on this line */
- match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
- if (match.chunk != NULL)
- goto prep_chunk;
- }
- }
-
-draw_done:
- TVRevealCanvas(tv);
- return ret;
-}
-
-Boolean
-TVSetTopBottomMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
- void *cookie)
-{
- if (tv->top_line == NULL)
- tv->top_line = match->line;
-
- tv->bottom_line = match->line;
-
- return true;
-}
-
-void
-TVScrollUnlocked(struct TVRec *tv, long horiz, long vert)
-{
- struct TVChunk *chunk;
- struct TVLine *line;
- struct TVStyle *style;
- struct TVSlab *text_slab;
- long wastop, top, wasleft, left;
- BigRect visible;
- RgnHandle vacated;
- char *text_slab_data;
-
- if (!horiz && !vert)
- return;
-
- TVUseCanvas(tv);
-
- wastop = tv->scrolled.top;
- wasleft = tv->scrolled.left;
-
- tv->scrolled.bottom += vert;
- tv->scrolled.top += vert;
- if (tv->scrolled.bottom < tv->frame.bottom) {
- /* can never scroll down lower than last line */
- tv->scrolled.bottom = tv->frame.bottom;
- tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
- }
- if (tv->scrolled.top > 0) {
- /* can never scroll up higher than first line */
- tv->scrolled.top = 0;
- tv->scrolled.bottom = tv->bounds.bottom;
- }
-
- tv->scrolled.left += horiz;
- tv->scrolled.right += horiz;
- /* TODO: bound */
-
- left = tv->scrolled.left - wasleft;
- top = tv->scrolled.top - wastop;
-
- if ((top < 0 ? -top : top) > tv->frame.bottom) {
- /* moving a lot so nothing to ScrollRect */
- EraseRect(&tv->frame);
- tv->canvas_dirty = true;
-
- visible.top = -(tv->scrolled.top);
- visible.left = -(tv->scrolled.left);
- visible.bottom = visible.top + tv->frame.bottom;
- visible.right = visible.left + tv->frame.right;
- TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
- TVRevealCanvas(tv);
- return;
- }
-
- vacated = NewRgn();
- ScrollRect(&tv->frame, left, top, vacated);
- tv->canvas_dirty = true;
- HLock(vacated);
- visible.left = (**vacated).rgnBBox.left - tv->scrolled.left;
- visible.top = (**vacated).rgnBBox.top - tv->scrolled.top;
- visible.right = (**vacated).rgnBBox.right - tv->scrolled.left;
- visible.bottom = (**vacated).rgnBBox.bottom - tv->scrolled.top;
- HUnlock(vacated);
- DisposeRgn(vacated);
-
- TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
-
- TVRevealCanvas(tv);
-}
-
-void
-TVUseCanvas(struct TVRec *tv)
-{
- if (++tv->canvas_refcnt != 1)
- return;
-
- if (tv->canvas_refcnt == 0)
- Debugger();
-
-#ifdef USE_CANVAS
- GetPort(&tv->canvas_old_port);
-
- HLock(tv->canvas_baseaddr);
- tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
-
- SetPort(tv->canvas);
-#else
- GetClip(tv->view_clip = NewRgn());
- SetOrigin(-tv->view.left, -tv->view.top);
- ClipRect(&tv->frame);
-#endif
-}
-
-void
-TVRevealCanvas(struct TVRec *tv)
-{
- if (tv->canvas_refcnt <= 0)
- Debugger();
-
- if (--tv->canvas_refcnt != 0)
- return;
-
-#ifdef USE_CANVAS
- SetPort(tv->canvas_old_port);
-
- if (tv->canvas_dirty)
- CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits,
- &tv->canvas->portRect, &tv->view, srcCopy, NULL);
-#else
- SetOrigin(0, 0);
- SetClip(tv->view_clip);
- DisposeRgn(tv->view_clip);
-#endif
-
- ValidRect(&tv->view);
-
- tv->canvas_old_port = NULL;
- tv->canvas_dirty = false;
- HUnlock(tv->canvas_baseaddr);
}
--- textview.h Wed Sep 23 13:10:54 2026
+++ textview.h Wed Sep 23 13:51:52 2026
@@ -216,4 +216,46 @@ Boolean TVCalcLines(TVHandle tvh);
void TVSetTrackScrollControl(TVHandle tvh);
pascal void TVTrackScrollControl(ControlHandle control, short part);
+/* internal, shared between textview source files */
+
+//#define USE_CANVAS 1
+
+/* NOTE: update TVFindStyle when additional fields are added */
+#define ARE_STYLES_EQUAL(one, two) \
+ ((one)->font == (two)->font && \
+ (one)->style == (two)->style && \
+ (one)->size == (two)->size && \
+ (one)->align == (two)->align && \
+ (one)->tag == (two)->tag && \
+ (one)->bitmap_data_size == (two)->bitmap_data_size)
+
+struct TVRec * TVLock(TVHandle tvh);
+void TVUnlock(struct TVRec *);
+Boolean TVSetupCanvas(struct TVRec *tv);
+Handle TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first,
+ Handle *last);
+Handle TVAllocateLineGroup(struct TVRec *tv, short groupn);
+struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsigned long addr);
+struct TVChunk * TVGetChunk(struct TVRec *tv, TVSlabLocator loc);
+struct TVChunk * TVCreateChunk(struct TVRec *tv);
+struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line);
+Boolean TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text,
+ unsigned long length, char terminator);
+short TVScrollDivisor(struct TVRec *tv);
+Boolean TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty);
+void TVApplyStyle(struct TVStyle *style);
+struct TVStyle * TVGetStyle(struct TVRec *tv, TVSlabLocator loc);
+struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style);
+void TVScrollUnlocked(struct TVRec *tv, long horiz, long vert);
+Boolean TVFindInRectLocked(struct TVRec *tv, BigRect *findrect,
+ long startline, TVFindInRectCallback callback, void *cookie);
+Boolean TVGetTextOffsetCallback(struct TVRec *tv,
+ struct TVFindInRectMatch *match, void *cookie);
+Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie);
+Boolean TVSetTopBottomMatch(struct TVRec *tv,
+ struct TVFindInRectMatch *match, void *cookie);
+void TVUseCanvas(struct TVRec *tv);
+void TVRevealCanvas(struct TVRec *tv);
+
#endif /* __TEXTVIEW__ */
\ No newline at end of file
--- tv_draw.c Wed Sep 23 13:51:52 2026
+++ tv_draw.c Wed Sep 23 13:51:52 2026
@@ -0,0 +1,541 @@
+/*
+ * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org>
+ *
+ * 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 <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include "textview.h"
+
+void
+TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller)
+{
+ struct TVRec *tv;
+ long max;
+ RgnHandle rgn;
+ short wasval, wasmax, val;
+
+ 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).
+ */
+ max = (tv->bounds.bottom - tv->bounds.top -
+ (tv->view.bottom - tv->view.top)) / TVScrollDivisor(tv);
+ if (max < 0)
+ max = 0;
+
+ wasval = GetCtlValue(scroller);
+
+ HLock(scroller);
+ wasmax = (*scroller)->contrlMax;
+ (*scroller)->contrlMin = 0;
+ val = -(tv->scrolled.top) / TVScrollDivisor(tv);
+ (*scroller)->contrlMax = max;
+ SetCtlValue(scroller, val);
+ HUnlock(scroller);
+
+ if ((wasmax == 0 && max != 0) || (wasmax != 0 && max == 0))
+ goto redraw;
+ if (wasval == 0 && val == 0)
+ goto done;
+ if (wasval == wasmax && val == max && tv->autoscroll)
+ goto done;
+
+redraw:
+ rgn = NewRgn();
+ HLock(scroller);
+ RectRgn(rgn, &(*scroller)->contrlRect);
+ UpdtControl((*(scroller))->contrlOwner, rgn);
+ HUnlock(scroller);
+ DisposeRgn(rgn);
+
+done:
+ TVUnlock(tv);
+}
+
+static TVHandle tv_track_control = NULL;
+void
+TVSetTrackScrollControl(TVHandle tvh)
+{
+ tv_track_control = tvh;
+}
+
+pascal void
+TVTrackScrollControl(ControlHandle control, short part)
+{
+ struct TVRec *tv;
+ struct TVChunk *chunk, *top_chunk = NULL;
+ struct TVLine *line, *oline;
+ KeyMap km;
+ BigRect visible;
+ long n, top;
+ unsigned long nline;
+ Boolean horiz, cmd_down;
+
+ 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));
+
+ GetKeys(&km);
+ cmd_down = (km[1] & (1 << 15)) != 0;
+
+ tv->top_line = NULL;
+ tv->bottom_line = NULL;
+
+ visible.top = -(tv->scrolled.top);
+ visible.left = -(tv->scrolled.left);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
+ TVFindInRectLocked(tv, &visible, 0, TVSetTopBottomMatch, NULL);
+
+ if (tv->top_line == NULL) {
+ tv->top_line = TVGetLine(tv, 1);
+ if (tv->top_line == NULL) {
+ TVUnlock(tv);
+ return;
+ }
+ }
+ if (tv->bottom_line == NULL) {
+ tv->bottom_line = TVGetLine(tv, tv->nlines);
+ if (tv->bottom_line == NULL) {
+ TVUnlock(tv);
+ return;
+ }
+ }
+
+ switch (part) {
+ case inUpButton:
+ if (cmd_down) {
+ TVScrollUnlocked(tv, 0, 1);
+ break;
+ }
+
+ /* scroll to the line before the top-most line */
+ if (tv->top_line->nline > 1) {
+ oline = TVGetLine(tv, tv->top_line->nline - 1);
+ if (oline == NULL)
+ break;
+ TVScrollUnlocked(tv, 0, tv->top_line->rect.top -
+ oline->rect.top + 1);
+ break;
+ }
+
+ /* nothing found, just scroll to the top */
+ TVScrollUnlocked(tv, 0, tv->bounds.bottom);
+ break;
+ case inPageUp:
+ /* scroll to put the top mostly-visible line at the bottom */
+ TVScrollUnlocked(tv, 0, (long)tv->frame.bottom -
+ (tv->scrolled.top + tv->top_line->rect.bottom) - 1);
+ break;
+ case inDownButton:
+ if (cmd_down) {
+ TVScrollUnlocked(tv, 0, -1);
+ break;
+ }
+
+ /* scroll to the line after the bottom-most line */
+ if (tv->bottom_line->nline < tv->nlines) {
+ line = TVGetLine(tv, tv->bottom_line->nline + 1);
+ if (line == NULL)
+ break;
+ TVScrollUnlocked(tv, 0,
+ -(tv->scrolled.top - tv->frame.bottom + line->rect.bottom));
+ break;
+ }
+
+ /* nothing found, just scroll to the bottom */
+ TVScrollUnlocked(tv, 0, -(tv->bounds.bottom));
+ break;
+ case inPageDown:
+ /* scroll to put the bottom mostly-visible line at the top */
+ if (tv->bottom_line->nline < tv->nlines) {
+ line = TVGetLine(tv, tv->bottom_line->nline + 1);
+ if (line == NULL)
+ break;
+ TVScrollUnlocked(tv, 0, -(line->rect.top + tv->scrolled.top) +
+ (line->rect.bottom - line->rect.top) + 1);
+ break;
+ }
+
+ /* nothing found, just scroll to the bottom */
+ TVScrollUnlocked(tv, 0, -(tv->bounds.bottom));
+ break;
+ }
+
+ SetCtlValue(control, -(tv->scrolled.top) / TVScrollDivisor(tv));
+ TVUnlock(tv);
+}
+
+void
+TVScroll(TVHandle tvh, long horiz, long vert)
+{
+ struct TVRec *tv;
+
+ tv = TVLock(tvh);
+ TVScrollUnlocked(tv, horiz * TVScrollDivisor(tv),
+ vert * TVScrollDivisor(tv));
+ TVUnlock(tv);
+}
+
+void
+TVUpdate(TVHandle tvh)
+{
+ struct TVRec *tv;
+ BigRect visible;
+
+ tv = TVLock(tvh);
+ TVUseCanvas(tv);
+
+#ifdef USE_CANVAS
+ EraseRect(&tv->frame);
+ tv->canvas_dirty = true;
+#endif
+
+ visible.top = -(tv->scrolled.top);
+ visible.left = -(tv->scrolled.left);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
+ TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
+
+ TVRevealCanvas(tv);
+ TVUnlock(tv);
+}
+
+Boolean
+TVSetupCanvas(struct TVRec *tv)
+{
+ struct TVLine *line;
+ long width, height;
+ GrafPtr old_port, canvas = NULL;
+ Handle canvas_baseaddr = NULL;
+
+ /*
+ * TODO: make canvas 3 times vis portal height so we can pre-render
+ * pages above and below to make scrolling seem faster?
+ */
+ width = tv->view.right - tv->view.left;
+ height = tv->view.bottom - tv->view.top;
+
+ /* this must be a Ptr because it cannot move once opened */
+ canvas = (GrafPtr)NewPtrClear(sizeof(GrafPort));
+ if (canvas == NULL)
+ goto fail;
+
+ canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2;
+ canvas_baseaddr = NewHandleClear((long)canvas->portBits.rowBytes *
+ height);
+ if (canvas_baseaddr == NULL)
+ goto fail;
+
+ /* everything allocated, commit to new canvas */
+
+ if (tv->canvas) {
+ ClosePort(tv->canvas);
+ DisposPtr(tv->canvas);
+ }
+ if (tv->canvas_baseaddr)
+ DisposHandle(tv->canvas_baseaddr);
+
+ tv->canvas = canvas;
+
+ /* opening the port resets portRect/portBits */
+ GetPort(&old_port);
+ OpenPort(tv->canvas);
+ RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect);
+ RectRgn(tv->canvas->visRgn, &tv->canvas->portRect);
+ SetPort(old_port);
+
+ 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_baseaddr = canvas_baseaddr;
+ HLock(tv->canvas_baseaddr);
+ tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
+
+ return true;
+
+fail:
+ if (canvas)
+ DisposPtr(canvas);
+ if (canvas_baseaddr)
+ DisposHandle(canvas_baseaddr);
+ return false;
+}
+
+short
+TVScrollDivisor(struct TVRec *tv)
+{
+ short div;
+ long max;
+
+ div = 1;
+ max = tv->bounds.bottom - tv->bounds.top -
+ (tv->view.bottom - tv->view.top);
+ while (max > SHRT_MAX) {
+ div *= 10;
+ max /= 10;
+ }
+
+ if (div == 0)
+ Debugger();
+ return div;
+}
+
+Boolean
+TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie)
+{
+ unsigned long n, j;
+ long next_tabstop, origin;
+ unsigned char c;
+ char *text, *spanstart;
+ short ts;
+ Rect r;
+ Point pen;
+
+ if (match->text_end == match->text_start)
+ return true;
+
+ if (match->rect.bottom < -(tv->scrolled.top))
+ /* no sense drawing what we can't see yet */
+ return true;
+
+ if (match->rect.top >= -(tv->scrolled.top) + tv->frame.bottom)
+ /* further down than the scrolled view, we've drawn enough lines */
+ return false;
+
+ if (match->line->grew) {
+ r.left = tv->scrolled.left + match->line->rect.left;
+ r.top = tv->scrolled.top + match->line->rect.top;
+ r.right = tv->scrolled.left + match->line->rect.right;
+ r.bottom = tv->scrolled.top + match->line->rect.bottom;
+ EraseRect(&r);
+ match->line->grew = false;
+ } else if (match->style->align == TV_ALIGN_CENTER) {
+ /* erase from this chunk to the right of the view */
+ r.left = tv->scrolled.left + match->rect.left;
+ r.top = tv->scrolled.top + match->line->rect.top;
+ r.right = tv->scrolled.left + tv->view.right - tv->padding.right;
+ r.bottom = tv->scrolled.top + match->line->rect.bottom;
+ EraseRect(&r);
+ } else if (match->style->align == TV_ALIGN_RIGHT) {
+ /* erase from the right of the view to the right of the view */
+ r.left = tv->scrolled.left + match->rect.left;
+ r.top = tv->scrolled.top + match->line->rect.top;
+ r.right = tv->scrolled.left + tv->view.right - tv->padding.right;
+ r.bottom = tv->scrolled.top + match->line->rect.bottom;
+ EraseRect(&r);
+ }
+
+ MoveTo(match->rect.left, tv->scrolled.top + match->rect.bottom -
+ match->line->baseline);
+ if (match->style->bitmap_data_size) {
+ r = match->style->bitmap.bounds;
+ r.left = match->rect.left;
+ r.right = r.left + (match->style->bitmap.bounds.right -
+ match->style->bitmap.bounds.left);
+ r.bottom = tv->scrolled.top + match->line->rect.bottom;
+ r.top = r.bottom - (match->style->bitmap.bounds.bottom -
+ match->style->bitmap.bounds.top);
+
+ match->style->bitmap.baseAddr = (QDPtr)match->style->bitmap_data;
+ CopyBits(&match->style->bitmap, &thePort->portBits,
+ &match->style->bitmap.bounds, &r, srcOr, NULL);
+ } else if (tv->tabstop) {
+ origin = match->line->rect.left + match->line->indent;
+ text = match->text_slab_data + (match->text_start -
+ match->text_slab->base);
+ spanstart = NULL;
+ for (n = 0; n < match->text_end - match->text_start; n++) {
+ c = *text;
+ if (c == '\t') {
+ if (spanstart != NULL)
+ DrawText(spanstart, 0, text - spanstart);
+ GetPen(&pen);
+ next_tabstop = origin + ((long)tv->tabstop *
+ (((pen.h - origin) / tv->tabstop) + 1));
+#if 0
+ /* draw dotted lines for tabs */
+ /* XXX: disabled for now but might be useful as an option */
+ PenPat(&tabpat);
+ MoveTo(pen.h, tv->scrolled.top + match->line->rect.top +
+ ((match->line->rect.bottom - match->line->rect.top) / 2));
+ GetPen(&pen);
+ LineTo(next_tabstop - 1, pen.v);
+ PenPat(black);
+ MoveTo(next_tabstop, tv->scrolled.top +
+ match->line->rect.bottom - match->line->baseline);
+#else
+ MoveTo(next_tabstop, pen.v);
+#endif
+ spanstart = NULL;
+ } else if (spanstart == NULL)
+ spanstart = text;
+ text++;
+ }
+ if (spanstart != NULL)
+ DrawText(spanstart, 0, text - spanstart);
+ } else
+ DrawText(match->text_slab_data + (match->text_start -
+ match->text_slab->base), 0, match->text_end - match->text_start);
+ tv->canvas_dirty = true;
+
+ return true;
+}
+
+Boolean
+TVSetTopBottomMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie)
+{
+ if (tv->top_line == NULL)
+ tv->top_line = match->line;
+
+ tv->bottom_line = match->line;
+
+ return true;
+}
+
+void
+TVScrollUnlocked(struct TVRec *tv, long horiz, long vert)
+{
+ struct TVChunk *chunk;
+ struct TVLine *line;
+ struct TVStyle *style;
+ struct TVSlab *text_slab;
+ long wastop, top, wasleft, left;
+ BigRect visible;
+ RgnHandle vacated;
+ char *text_slab_data;
+
+ if (!horiz && !vert)
+ return;
+
+ TVUseCanvas(tv);
+
+ wastop = tv->scrolled.top;
+ wasleft = tv->scrolled.left;
+
+ tv->scrolled.bottom += vert;
+ tv->scrolled.top += vert;
+ if (tv->scrolled.bottom < tv->frame.bottom) {
+ /* can never scroll down lower than last line */
+ tv->scrolled.bottom = tv->frame.bottom;
+ tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
+ }
+ if (tv->scrolled.top > 0) {
+ /* can never scroll up higher than first line */
+ tv->scrolled.top = 0;
+ tv->scrolled.bottom = tv->bounds.bottom;
+ }
+
+ tv->scrolled.left += horiz;
+ tv->scrolled.right += horiz;
+ /* TODO: bound */
+
+ left = tv->scrolled.left - wasleft;
+ top = tv->scrolled.top - wastop;
+
+ if ((top < 0 ? -top : top) > tv->frame.bottom) {
+ /* moving a lot so nothing to ScrollRect */
+ EraseRect(&tv->frame);
+ tv->canvas_dirty = true;
+
+ visible.top = -(tv->scrolled.top);
+ visible.left = -(tv->scrolled.left);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
+ TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
+ TVRevealCanvas(tv);
+ return;
+ }
+
+ vacated = NewRgn();
+ ScrollRect(&tv->frame, left, top, vacated);
+ tv->canvas_dirty = true;
+ HLock(vacated);
+ visible.left = (**vacated).rgnBBox.left - tv->scrolled.left;
+ visible.top = (**vacated).rgnBBox.top - tv->scrolled.top;
+ visible.right = (**vacated).rgnBBox.right - tv->scrolled.left;
+ visible.bottom = (**vacated).rgnBBox.bottom - tv->scrolled.top;
+ HUnlock(vacated);
+ DisposeRgn(vacated);
+
+ TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
+
+ TVRevealCanvas(tv);
+}
+
+void
+TVUseCanvas(struct TVRec *tv)
+{
+ if (++tv->canvas_refcnt != 1)
+ return;
+
+ if (tv->canvas_refcnt == 0)
+ Debugger();
+
+#ifdef USE_CANVAS
+ GetPort(&tv->canvas_old_port);
+
+ HLock(tv->canvas_baseaddr);
+ tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
+
+ SetPort(tv->canvas);
+#else
+ GetClip(tv->view_clip = NewRgn());
+ SetOrigin(-tv->view.left, -tv->view.top);
+ ClipRect(&tv->frame);
+#endif
+}
+
+void
+TVRevealCanvas(struct TVRec *tv)
+{
+ if (tv->canvas_refcnt <= 0)
+ Debugger();
+
+ if (--tv->canvas_refcnt != 0)
+ return;
+
+#ifdef USE_CANVAS
+ SetPort(tv->canvas_old_port);
+
+ if (tv->canvas_dirty)
+ CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits,
+ &tv->canvas->portRect, &tv->view, srcCopy, NULL);
+#else
+ SetOrigin(0, 0);
+ SetClip(tv->view_clip);
+ DisposeRgn(tv->view_clip);
+#endif
+
+ ValidRect(&tv->view);
+
+ tv->canvas_old_port = NULL;
+ tv->canvas_dirty = false;
+ HUnlock(tv->canvas_baseaddr);
+}
--- tv_layout.c Wed Sep 23 13:51:52 2026
+++ tv_layout.c Wed Sep 23 13:51:52 2026
@@ -0,0 +1,545 @@
+/*
+ * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org>
+ *
+ * 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 <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include "textview.h"
+
+Boolean
+TVResize(TVHandle tvh)
+{
+ struct TVRec *tv;
+ long width, height, n;
+ struct TVLine *line;
+ struct TVChunk *chunk;
+ BigRect visible;
+ long nline;
+ unsigned long top_start;
+
+ tv = TVLock(tvh);
+
+ TVUseCanvas(tv);
+ EraseRect(&tv->frame);
+ TVRevealCanvas(tv);
+
+ /* find text position of first line to try to stay there */
+ top_start = 0;
+ for (nline = 1; nline <= tv->nlines; nline++) {
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ break;
+
+ if (tv->scrolled.top + line->rect.bottom < 0L)
+ /* is not in the view at all yet */
+ continue;
+
+ top_start = line->start;
+ break;
+ }
+
+#ifdef USE_CANVAS
+ if (!TVSetupCanvas(tv)) {
+ TVUnlock(tv);
+ return false;
+ }
+#endif
+ SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left,
+ tv->view.bottom - tv->view.top);
+ tv->bounds.right = tv->frame.right;
+ tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
+ tv->scrolled = tv->bounds;
+
+ /* 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;
+ DisposHandle(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->nlines = 0;
+ tv->last_space = 0;
+ tv->space_width = 0;
+
+ for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
+ chunk = TVGetChunk(tv, chunk->next_chunk)) {
+ chunk->start_line = 0;
+ chunk->end_line = 0;
+ chunk->start_left = 0;
+ chunk->end_right = 0;
+ chunk->need_recalc = true;
+ }
+
+ if (!TVCalcLinesLocked(tv, true)) {
+ /* XXX: if this fails, what are we supposed to do? */
+ TVUnlock(tv);
+ return false;
+ }
+
+ if (tv->bounds.bottom > tv->frame.bottom) {
+ if (tv->autoscroll) {
+ tv->scrolled.bottom = tv->frame.bottom;
+ tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
+ } else {
+ for (nline = 1; nline <= tv->nlines; nline++) {
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ break;
+
+ if (line->start <= top_start && top_start < line->end) {
+ /* keep this line at the top */
+ tv->scrolled.top = -(line->rect.top);
+ tv->scrolled.bottom = tv->scrolled.top +
+ tv->bounds.bottom;
+ break;
+ }
+ }
+ }
+ }
+
+ TVUseCanvas(tv);
+
+ visible.top = -(tv->scrolled.top);
+ visible.left = -(tv->scrolled.left);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
+ TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
+
+ TVRevealCanvas(tv);
+ TVUnlock(tv);
+
+ return true;
+}
+
+Boolean
+TVGetTextOffsetCallback(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie)
+{
+ unsigned long n, left;
+ long *off;
+ short width;
+ unsigned char c;
+
+ off = (long *)cookie;
+ left = match->rect.left;
+
+ for (n = match->text_start; n < match->text_end; n++) {
+ c = match->text_slab_data[n - match->text_slab->base];
+ width = match->style->width_cache[c];
+
+ if (left + width >= match->findrect.left) {
+ *off = n;
+ /* stop getting additional matches */
+ return false;
+ }
+ left += width;
+ }
+
+ return true;
+}
+
+long
+TVGetTextOffset(TVHandle tvh, Point p)
+{
+ struct TVRec *tv;
+ BigRect prect;
+ long off;
+
+ tv = TVLock(tvh);
+ TVUseCanvas(tv);
+
+ prect.left = (long)p.h - tv->scrolled.left - tv->view.left;
+ prect.top = (long)p.v - tv->scrolled.top - tv->view.top;
+ prect.right = prect.left;
+ prect.bottom = prect.top;
+
+ off = 0;
+ if (!TVFindInRectLocked(tv, &prect, 0, TVGetTextOffsetCallback, &off))
+ off = -1;
+
+ TVRevealCanvas(tv);
+ TVUnlock(tv);
+ return off;
+}
+
+Boolean
+TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty)
+{
+ struct TVSlab *text_slab;
+ struct TVChunk *chunk, *tchunk, *last_chunk, *prev_chunk, *next_chunk;
+ struct TVStyle *style;
+ struct TVLine *line, *prev_line, *next_line;
+ unsigned long avail, start, nline, max_right;
+ long n, back_width, back_n, next_tabstop;
+ short width, height;
+ char *text_slab_data;
+ register unsigned char c;
+ Boolean new_line;
+
+ if (ignore_dirty)
+ chunk = TVGetChunk(tv, tv->first_chunk);
+ else {
+ /* find first dirty chunk but search from the end */
+ last_chunk = TVGetChunk(tv, tv->last_chunk);
+ chunk = last_chunk;
+ while (chunk != NULL) {
+ prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ if (!prev_chunk || !prev_chunk->need_recalc)
+ break;
+ chunk = prev_chunk;
+ }
+ }
+
+ max_right = tv->view.right - tv->view.left - tv->padding.left -
+ tv->padding.right;
+
+ for (; chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) {
+ new_line = false;
+
+reload_chunk:
+ style = TVGetStyle(tv, chunk->style);
+ if (style == NULL)
+ return false;
+ TVApplyStyle(style);
+
+ if (chunk->end_line == 0) {
+ /* working with a new text chunk */
+ if (chunk->prev_chunk == 0) {
+ prev_chunk = NULL;
+ chunk->start_left = tv->padding.left;
+ chunk->start_line = 1;
+ chunk->end_right = chunk->start_left;
+ chunk->end_line = 1;
+ } else {
+ prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ if (prev_chunk == NULL)
+ return false;
+ chunk->start_left = prev_chunk->end_right;
+ chunk->start_line = prev_chunk->end_line;
+ chunk->end_right = chunk->start_left;
+ chunk->end_line = chunk->start_line;
+ }
+ }
+
+ /* setup text slab */
+ text_slab = TVGetTextSlab(tv, chunk->start);
+ if (text_slab == NULL)
+ return false;
+ text_slab_data = text_slab->data;
+
+ /* find our current line */
+ nline = chunk->end_line;
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ return false;
+ if (nline == 1) {
+ line->rect.left = tv->padding.left;
+ line->rect.top = tv->padding.top;
+ line->start = 0;
+ start = line->end;
+ } else {
+ prev_line = TVGetLine(tv, nline - 1);
+ if (prev_line == NULL)
+ return false;
+ line->rect.top = prev_line->rect.bottom;
+ if (line->end)
+ start = line->end;
+ else
+ start = prev_line->end;
+ }
+
+ line->nline = nline;
+ height = style->font_info.ascent + style->font_info.descent +
+ style->font_info.leading;
+
+ /*
+ * Walk each character and find its width, possibly wrapping and
+ * starting a new line.
+ */
+ for (n = start; n < chunk->end; n++) {
+ c = text_slab_data[n - text_slab->base];
+
+ if (c == '\r') {
+ new_line = true;
+ width = 0;
+ } else {
+ if (style->bitmap_data_size)
+ width = style->bitmap.bounds.right -
+ style->bitmap.bounds.left;
+ else if (c == '\t' && tv->tabstop) {
+ next_tabstop = line->rect.left +
+ (long)tv->tabstop *
+ (((chunk->end_right - line->rect.left) /
+ tv->tabstop) + 1);
+ width = next_tabstop - chunk->end_right;
+ } else if (!(width = style->width_cache[c]))
+ width = style->width_cache[c] = CharWidth(c);
+
+ if (c == ' ' && !style->bitmap_data_size) {
+ tv->last_space = n;
+ tv->space_width = 0;
+ } else if (!new_line && tv->linewrap &&
+ chunk->end_right + width >= max_right) {
+ new_line = true;
+ if (tv->last_space) {
+ /* break after the last space */
+ n = tv->last_space + 1;
+ chunk->end_right -= tv->space_width;
+ line->end = tv->last_space + 1;
+ line->rect.right = chunk->end_right;
+
+ while (n < chunk->start) {
+ if (!chunk->prev_chunk) {
+ /* how did we get here? */
+ Debugger();
+ return false;
+ }
+ chunk = TVGetChunk(tv, chunk->prev_chunk);
+ }
+
+ /* setup chunk with new_line set */
+ goto reload_chunk;
+ }
+ /* otherwise, hard-break this long word */
+ } else
+ tv->space_width += width;
+ }
+
+ if (line->start == line->end) {
+ /* start of new line disregards previous line's style */
+ line->baseline = style->font_info.descent;
+ line->leading = style->font_info.leading;
+ line->rect.bottom = line->rect.top + height + line->leading;
+ }
+
+ if (new_line) {
+ new_line = false;
+ tv->space_width = 0;
+ tv->last_space = 0;
+
+ line->end = n;
+ line->rect.right = chunk->end_right;
+
+ if (style->align == TV_ALIGN_CENTER)
+ line->indent = (max_right - line->rect.right -
+ line->rect.left) / 2;
+ else if (style->align == TV_ALIGN_RIGHT)
+ line->indent = max_right - line->rect.right -
+ line->rect.left;
+ else
+ line->indent = 0;
+
+ if (n == chunk->start) {
+ /* no text on previous line, start on next line */
+ chunk->start_left = tv->padding.left;
+ chunk->end_right = chunk->start_left;
+ chunk->start_line++;
+ } else if (height > line->rect.bottom - line->rect.top)
+ line->rect.bottom = line->rect.top + height;
+
+ nline++;
+ chunk->end_line = nline;
+ chunk->end_right = tv->padding.left;
+ next_line = TVGetLine(tv, nline);
+ if (next_line == NULL)
+ return false;
+ next_line->nline = nline;
+ next_line->start = n;
+ if (c == '\r')
+ next_line->start++;
+ next_line->leading = style->font_info.leading;
+ next_line->rect.top = line->rect.bottom;
+ next_line->rect.left = tv->padding.left;
+ next_line->rect.right = chunk->end_right;
+ next_line->rect.bottom = next_line->rect.top;
+ if (n + 1 < chunk->end)
+ next_line->rect.bottom += height;
+
+ line = next_line;
+ }
+
+ chunk->end_right += width;
+
+ if (line->start != line->end) {
+ /* if style changes mid-line, previous chunks may grow */
+ if (style->font_info.descent > line->baseline) {
+ if (line->baseline)
+ line->grew = true;
+ line->baseline = style->font_info.descent;
+ }
+ if (style->font_info.leading > line->leading) {
+ if (line->leading)
+ line->grew = true;
+ line->leading = style->font_info.leading;
+ }
+ if (line->rect.bottom == 0 ||
+ (line->rect.bottom - line->rect.top < height)) {
+ if (line->rect.bottom != line->rect.top &&
+ line->rect.bottom != 0)
+ line->grew = true;
+ line->rect.bottom = line->rect.top + height;
+ }
+ }
+ }
+
+ line->end = chunk->end;
+ line->rect.right = chunk->end_right;
+ chunk->end_line = nline;
+ chunk->need_recalc = false;
+
+ if (style->align == TV_ALIGN_CENTER)
+ line->indent = (max_right - line->rect.right -
+ line->rect.left) / 2;
+ else if (style->align == TV_ALIGN_RIGHT)
+ line->indent = max_right - line->rect.right - line->rect.left;
+ else
+ line->indent = 0;
+ }
+
+done_chunks:
+ tv->nlines = nline;
+
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ return false;
+
+ if (line->rect.bottom > tv->bounds.bottom - tv->padding.bottom) {
+ tv->bounds.bottom = line->rect.bottom + tv->padding.bottom;
+ tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
+ }
+
+ return true;
+}
+
+Boolean
+TVFindInRectLocked(struct TVRec *tv, BigRect *rect, long startline,
+ TVFindInRectCallback callback, void *cookie)
+{
+ struct TVFindInRectMatch match;
+ unsigned long nline, ppl;
+ Boolean ret;
+
+ TVUseCanvas(tv);
+
+ match.findrect = *rect;
+ match.chunk = NULL;
+ ret = false;
+
+ if (startline == 0 && tv->nlines > 1) {
+ /* take a stab at getting us closer */
+ ppl = (tv->bounds.bottom / tv->nlines);
+ if (ppl == 0)
+ ppl = 1;
+ startline = rect->top / ppl;
+ if (startline > tv->nlines)
+ startline = tv->nlines;
+ if (startline > 1) {
+ match.line = TVGetLine(tv, startline);
+ while (startline > 1 && match.line != NULL &&
+ match.line->rect.top > rect->top) {
+ startline--;
+ match.line = TVGetLine(tv, startline);
+ }
+ }
+ }
+
+ if (startline == 0)
+ startline = 1;
+
+ for (nline = startline; nline <= tv->nlines; nline++) {
+ match.line = TVGetLine(tv, nline);
+ if (match.line == NULL)
+ goto draw_done;
+
+ if (match.line->rect.bottom < rect->top)
+ /* is not in the view at all yet */
+ continue;
+
+ if (match.line->rect.top >= rect->bottom)
+ /* is further down than the view, we've done enough lines */
+ break;
+
+ /* advance chunk if we need to catch up to this line */
+ if (match.chunk == NULL || match.chunk->end_line < nline) {
+ if (match.chunk == NULL) {
+ match.chunk = TVGetChunk(tv, tv->first_chunk);
+ if (match.chunk == NULL)
+ goto draw_done;
+ }
+
+ while (match.chunk->end_line < nline ||
+ match.chunk->end < match.line->start) {
+ match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
+ if (!match.chunk)
+ goto draw_done;
+ }
+
+prep_chunk:
+ match.text_slab = TVGetTextSlab(tv, match.chunk->start);
+ if (match.text_slab == NULL)
+ goto draw_done;
+ match.text_slab_data = match.text_slab->data;
+
+ match.style = TVGetStyle(tv, match.chunk->style);
+ if (match.style == NULL)
+ goto draw_done;
+ if (!match.style->bitmap_data_size)
+ TVApplyStyle(match.style);
+ }
+
+ match.rect.top = match.line->rect.top;
+ match.rect.bottom = match.line->rect.bottom;
+
+ if (match.line->start > match.chunk->start) {
+ match.text_start = match.line->start;
+ match.rect.left = match.line->rect.left + match.line->indent;
+ } else {
+ match.text_start = match.chunk->start;
+ match.rect.left = match.chunk->start_left + match.line->indent;
+ }
+
+ if (match.line->end < match.chunk->end) {
+ match.text_end = match.line->end;
+ match.rect.right = match.line->rect.right + match.line->indent;
+ } else {
+ match.text_end = match.chunk->end;
+ match.rect.right = match.chunk->end_right + match.line->indent;
+ }
+
+ ret = true;
+
+ if (callback(tv, &match, cookie) == false)
+ /* callback requested we stop matching */
+ goto draw_done;
+
+ if (match.chunk->end_line == nline &&
+ match.chunk->end < match.line->end) {
+ /* more chunks on this line */
+ match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
+ if (match.chunk != NULL)
+ goto prep_chunk;
+ }
+ }
+
+draw_done:
+ TVRevealCanvas(tv);
+ return ret;
+}
--- tv_store.c Wed Sep 23 13:51:52 2026
+++ tv_store.c Wed Sep 23 13:51:52 2026
@@ -0,0 +1,335 @@
+/*
+ * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org>
+ *
+ * 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 <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include "textview.h"
+
+Handle
+TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first,
+ Handle *last)
+{
+ Handle slabh;
+ struct TVSlab *slab, *lslab;
+ unsigned short id;
+
+ if (last == NULL || *last == NULL)
+ id = 1;
+ else {
+ HLock(*last);
+ slab = (struct TVSlab *)**last;
+ if (slab->id == USHRT_MAX) {
+ Debugger();
+ HUnlock(*last);
+ return NULL;
+ }
+ id = slab->id + 1;
+ HUnlock(*last);
+ }
+
+ slabh = NewHandleClear(sizeof(struct TVSlab) + size);
+ if (slabh == NULL)
+ /* TODO: try smaller allocations */
+ return NULL;
+
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+ slab->id = id;
+ slab->size = size;
+
+ if (last != NULL && *last != NULL) {
+ HLock(*last);
+ lslab = (struct TVSlab *)**last;
+ lslab->next_slab = slabh;
+ slab->prev_slab = *last;
+ slab->base = lslab->base + lslab->size;
+ }
+
+ if (first != NULL && *first == NULL)
+ *first = slabh;
+ if (last != NULL)
+ *last = slabh;
+
+ /* leave locked */
+
+ return slabh;
+}
+
+Handle
+TVAllocateLineGroup(struct TVRec *tv, short groupn)
+{
+ short n;
+
+ if (groupn >= sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))
+ return NULL;
+
+ if (tv->line_groups[groupn] == NULL)
+ tv->line_groups[groupn] = NewHandleClear(TV_LINE_GROUP_SIZE);
+
+ return tv->line_groups[groupn];
+}
+
+struct TVSlab *
+TVGetTextSlab(struct TVRec *tv, unsigned long addr)
+{
+ Handle slabh;
+ struct TVSlab *slab;
+
+ slabh = tv->first_text_slab;
+ while (slabh) {
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+ if (addr >= slab->base && addr < slab->base + slab->size)
+ /* leave locked */
+ return slab;
+
+ slabh = slab->next_slab;
+ }
+
+ return NULL;
+}
+
+struct TVChunk *
+TVGetChunk(struct TVRec *tv, TVSlabLocator loc)
+{
+ Handle slabh;
+ struct TVSlab *slab;
+ char *addr;
+
+ if (loc == 0)
+ return NULL;
+
+ slabh = tv->first_chunk_slab;
+ while (slabh) {
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+ if (slab->id == SLABLOC_SLABID(loc))
+ break;
+ slabh = slab->next_slab;
+ }
+ if (slabh == NULL)
+ return NULL;
+
+ addr = slab->data + SLABLOC_OFFSET(loc);
+ if ((unsigned long)addr % 2 != 0) {
+ Debugger();
+ return NULL;
+ }
+
+ /* keep slab locked */
+
+ return (struct TVChunk *)addr;
+}
+
+struct TVChunk *
+TVCreateChunk(struct TVRec *tv)
+{
+ Handle slabh;
+ struct TVSlab *slab;
+ struct TVChunk *chunk;
+ TVSlabLocator cloc;
+
+ slabh = tv->last_chunk_slab;
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+
+ if (slab->size - slab->pos < sizeof(struct TVChunk)) {
+ if ((slabh = TVAllocateSlab(tv, TV_CHUNK_SLAB_SIZE,
+ &tv->first_chunk_slab, &tv->last_chunk_slab)) == NULL)
+ return NULL;
+ slab = (struct TVSlab *)*slabh;
+ }
+
+ SET_SLABLOC_SLABID(cloc, slab->id);
+ SET_SLABLOC_OFFSET(cloc, slab->pos);
+
+ chunk = TVGetChunk(tv, cloc);
+ chunk->loc = cloc;
+ slab->pos += sizeof(struct TVChunk);
+
+ if (!tv->first_chunk)
+ tv->first_chunk = cloc;
+
+ tv->last_chunk = cloc;
+
+ return chunk;
+}
+
+struct TVLine *
+TVGetLine(struct TVRec *tv, unsigned long line)
+{
+ Handle group;
+ unsigned short groupn, idx;
+
+ if (line == 0)
+ return NULL;
+
+ groupn = (line - 1) / TV_LINE_GROUP_NLINES;
+ if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0])))
+ return NULL;
+
+ idx = line - 1 - (groupn * TV_LINE_GROUP_NLINES);
+
+ group = tv->line_groups[groupn];
+ if (group == NULL && (group = TVAllocateLineGroup(tv, groupn)) == NULL)
+ return NULL;
+
+ HLock(group);
+ /* keep group locked */
+
+ return (struct TVLine *)(*group + (sizeof(struct TVLine) * idx));
+}
+
+void
+TVApplyStyle(struct TVStyle *style)
+{
+ TextFont(style->font);
+ TextFace(style->style);
+ TextSize(style->size);
+}
+
+struct TVStyle *
+TVGetStyle(struct TVRec *tv, TVSlabLocator loc)
+{
+ Handle slabh;
+ struct TVSlab *slab;
+ struct TVStyle *style;
+ short n;
+
+ slabh = tv->first_style_slab;
+ while (slabh) {
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+
+ if (slab->id != SLABLOC_SLABID(loc)) {
+ slabh = slab->next_slab;
+ continue;
+ }
+
+ style = (struct TVStyle *)(slab->data + SLABLOC_OFFSET(loc));
+ if (style->loc != loc) {
+ /* locator pointed at bogus style */
+ Debugger();
+ return NULL;
+ }
+ return style;
+ }
+
+ return NULL;
+}
+
+struct TVStyle *
+TVFindStyle(struct TVRec *tv, struct TVStyle *style)
+{
+ Handle slabh;
+ struct TVSlab *slab;
+ struct TVStyle *tstyle;
+ short n;
+
+ if (style->loc)
+ return TVGetStyle(tv, style->loc);
+
+ slabh = tv->first_style_slab;
+ while (slabh) {
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+ tstyle = (struct TVStyle *)(slab->data);
+
+iterate_slab:
+ if (tstyle->loc == 0)
+ goto next_slab;
+
+ if (!ARE_STYLES_EQUAL(tstyle, style))
+ goto next_style;
+
+ if (tstyle->bitmap_data_size &&
+ memcmp(tstyle->bitmap_data, style->bitmap.baseAddr,
+ tstyle->bitmap_data_size) != 0)
+ goto next_style;
+
+ TVApplyStyle(tstyle);
+ tv->cur_style = tstyle->loc;
+ return tstyle;
+
+next_style:
+ if (SLABLOC_OFFSET(tstyle->loc) + sizeof(struct TVStyle) +
+ tstyle->bitmap_data_size + sizeof(struct TVStyle) > slab->size)
+ goto next_slab;
+
+ tstyle = (struct TVStyle *)((char *)tstyle +
+ sizeof(struct TVStyle) + tstyle->bitmap_data_size);
+ if (tstyle->loc == 0) {
+ if (slab->next_slab)
+ goto next_slab;
+ goto create_style;
+ }
+ goto iterate_slab;
+
+next_slab:
+ slabh = slab->next_slab;
+ }
+
+ /* no matching style found, create a new one */
+create_style:
+ slabh = tv->last_style_slab;
+ HLock(slabh);
+ slab = (struct TVSlab *)*slabh;
+
+ if (slab->pos + sizeof(struct TVStyle) + style->bitmap_data_size >
+ slab->size) {
+ /* need a new slab */
+ slabh = TVAllocateSlab(tv, TV_STYLE_SLAB_SIZE,
+ &tv->first_style_slab, &tv->last_style_slab);
+ if (slabh == NULL)
+ return NULL;
+ slab = (struct TVSlab *)*slabh;
+ }
+
+ tstyle = (struct TVStyle *)(slab->data + slab->pos);
+ SET_SLABLOC_SLABID(tstyle->loc, slab->id);
+ SET_SLABLOC_OFFSET(tstyle->loc, slab->pos);
+
+ /* only copy over the important fields, rest will stay 0 */
+ /* NOTE: keep this in sync with ARE_STYLES_EQUAL */
+ tstyle->font = style->font;
+ tstyle->style = style->style;
+ tstyle->size = style->size;
+ tstyle->align = style->align;
+ tstyle->tag = style->tag;
+ if (style->bitmap_data_size) {
+ tstyle->font_info = style->font_info;
+ tstyle->bitmap_data_size = style->bitmap_data_size;
+ tstyle->bitmap = style->bitmap;
+ memcpy(tstyle->bitmap_data, style->bitmap.baseAddr,
+ style->bitmap_data_size);
+ /* this must be recalculated at draw time once slab is locked */
+ tstyle->bitmap.baseAddr = 0;
+ }
+
+ slab->pos += sizeof(struct TVStyle) + tstyle->bitmap_data_size;
+ if (slab->pos > slab->size) {
+ Debugger();
+ return NULL;
+ }
+
+ TVApplyStyle(tstyle);
+ if (!style->bitmap_data_size || style->font_info.widMax == 0)
+ GetFontInfo(&tstyle->font_info);
+ tv->cur_style = tstyle->loc;
+
+ return tstyle;
+}