jcs
/textview
/amendments
/67
*: Add text selection helpers
jcs made amendment 67 about 1 month ago
--- textview.c Wed Sep 23 13:51:52 2026
+++ textview.c Wed Sep 30 17:32:24 2026
@@ -42,7 +42,8 @@ TVNew(Rect *view, Rect *padding)
tv->linewrap = true;
tv->autocalc = true;
-
+ tv->active = true;
+
memset(&tv->tab_pat, 0x88, sizeof(tv->tab_pat));
#ifdef USE_CANVAS
@@ -225,32 +226,12 @@ TVTabStop(TVHandle tvh, unsigned short tabstop)
}
void
-TVClick(TVHandle tvh, Point p, Boolean extend)
+TVSetScrollbar(TVHandle tvh, ControlHandle scroller)
{
struct TVRec *tv;
- struct TVChunk *chunk;
-
- tv = TVLock(tvh);
-
-#if 0
- p.v -= tv->view.top;
- p.h -= tv->view.left;
-
- p.v += -(tv->canvas->portRect.top);
- 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 &&
- p.h >= chunk->rect.left && p.h < chunk->rect.right) {
- tv->sel_start = chunk;
- TVUpdate(tv);
- break;
- }
- }
-#endif
+ tv = TVLock(tvh);
+ tv->scroller = scroller;
TVUnlock(tv);
}
@@ -309,16 +290,6 @@ TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, R
}
void
-TVCopy(TVHandle tvh)
-{
-}
-
-void
-TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end)
-{
-}
-
-void
TVClear(TVHandle tvh)
{
Handle slabh;
@@ -386,7 +357,10 @@ TVClear(TVHandle tvh)
tv->nlines = 0;
tv->last_space = 0;
tv->space_width = 0;
-
+ tv->sel_start = 0;
+ tv->sel_end = 0;
+ tv->click_count = 0;
+
/* clear line groups except first one */
for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
n++) {
@@ -509,9 +483,10 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
Handle text_slabh;
RgnHandle vacated;
BigRect redraw;
+ Rect r;
unsigned long avail, wastop;
short width;
- TVSlabLocator prev_style;
+ TVSlabLocator prev_style, prev_chunk_loc;
char *text_slab_data;
Boolean new_chunk;
@@ -545,11 +520,12 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
while (length) {
if (chunk == NULL || new_chunk) {
new_chunk = false;
- prev_chunk = chunk;
+ 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;
@@ -620,10 +596,27 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
redraw.right = tv->frame.right;
if (tv->scrolled.top + redraw.bottom > 0L &&
- tv->scrolled.top + redraw.top < tv->frame.bottom)
- TVFindInRectLocked(tv, &redraw, fromline ? fromline->nline : 0,
- TVDrawMatch, NULL);
-
+ 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;
+
+ /*
+ * Text is normally drawn right over what's there, but if any of
+ * it is highlighted it has to be erased first.
+ */
+ TVRedrawRect(tv, &r, fromline ? fromline->nline : 0,
+ (tv->active && tv->sel_start != tv->sel_end &&
+ fromline != NULL && tv->sel_end > fromline->start));
+ }
+
TVRevealCanvas(tv);
return true;
}
--- textview.h Wed Sep 23 13:51:52 2026
+++ textview.h Wed Oct 14 16:43:49 2026
@@ -85,6 +85,7 @@ struct TVChunk {
struct TVLine {
unsigned long start;
unsigned long end;
+ TVSlabLocator chunk;
BigRect rect;
unsigned long indent;
unsigned long nline;
@@ -122,7 +123,6 @@ struct TVRec {
Handle canvas_baseaddr;
short canvas_refcnt;
unsigned long canvas_size;
- /* while drawing on canvas, this stores old thePort */
GrafPtr canvas_old_port;
Boolean canvas_dirty;
@@ -154,7 +154,12 @@ struct TVRec {
unsigned long sel_start;
unsigned long sel_end;
-
+ unsigned long last_click_ticks;
+ Point last_click_point;
+ short click_count;
+ Boolean active;
+ ControlHandle scroller;
+
/* number of pixels for each tabstop, usually CharWidth(' ')*4 or 8 */
unsigned short tabstop;
Pattern tab_pat;
@@ -178,7 +183,6 @@ struct TVFindInRectMatch {
char *text_slab_data;
unsigned long text_start;
unsigned long text_end;
- /* the original rect we were asked to find */
BigRect findrect;
};
@@ -208,8 +212,12 @@ unsigned long TVGetText(TVHandle tvh, char *dst, unsig
long TVGetTextOffset(TVHandle tvh, Point p);
void TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, Rect *rect);
void TVScroll(TVHandle tvh, long horiz, long vert);
-void TVCopy(TVHandle tvh);
+Boolean TVCopy(TVHandle tvh);
void TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end);
+void TVGetSelect(TVHandle tvh, unsigned long *start, unsigned long *end);
+void TVSelectAll(TVHandle tvh);
+void TVActivate(TVHandle tvh, Boolean active);
+void TVSetScrollbar(TVHandle tvh, ControlHandle scroller);
void TVClear(TVHandle tvh);
Boolean TVCalcLines(TVHandle tvh);
@@ -249,13 +257,27 @@ struct TVStyle * TVFindStyle(struct TVRec *tv, struct
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);
+unsigned char TVCharAt(struct TVRec *tv, unsigned long off);
+short TVCharWidth(struct TVRec *tv, struct TVStyle *style,
+ struct TVLine *line, long x, unsigned char c);
+unsigned long TVLineForY(struct TVRec *tv, long y);
+unsigned long TVLineForOffset(struct TVRec *tv, unsigned long off);
+struct TVChunk * TVLineChunk(struct TVRec *tv, struct TVLine *line);
+long TVXForOffset(struct TVRec *tv, struct TVLine *line, unsigned long off);
+unsigned long TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x);
+unsigned long TVOffsetForPoint(struct TVRec *tv, long h, long v);
+void TVRedrawRect(struct TVRec *tv, Rect *r, long startline, Boolean erase);
+void TVInvertRange(struct TVRec *tv, unsigned long a, unsigned long b,
+ Rect *clip);
+void TVSetSelectLocked(struct TVRec *tv, unsigned long start,
+ unsigned long end);
+void TVSelectUnit(struct TVRec *tv, unsigned long off, short unit,
+ unsigned long *start, unsigned long *end);
#endif /* __TEXTVIEW__ */
\ No newline at end of file
--- tv_draw.c Wed Sep 23 13:51:52 2026
+++ tv_draw.c Wed Oct 14 16:44:47 2026
@@ -203,23 +203,9 @@ 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);
+ tv = TVLock(tvh);
+ TVRedrawRect(tv, &tv->frame, 0, true);
TVUnlock(tv);
}
@@ -310,7 +296,7 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
void *cookie)
{
unsigned long n, j;
- long next_tabstop, origin;
+ long next_tabstop;
unsigned char c;
char *text, *spanstart;
short ts;
@@ -366,7 +352,6 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
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;
@@ -376,8 +361,8 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
if (spanstart != NULL)
DrawText(spanstart, 0, text - spanstart);
GetPen(&pen);
- next_tabstop = origin + ((long)tv->tabstop *
- (((pen.h - origin) / tv->tabstop) + 1));
+ next_tabstop = pen.h + TVCharWidth(tv, match->style,
+ match->line, pen.h - match->line->indent, '\t');
#if 0
/* draw dotted lines for tabs */
/* XXX: disabled for now but might be useful as an option */
@@ -427,7 +412,7 @@ TVScrollUnlocked(struct TVRec *tv, long horiz, long ve
struct TVStyle *style;
struct TVSlab *text_slab;
long wastop, top, wasleft, left;
- BigRect visible;
+ Rect r;
RgnHandle vacated;
char *text_slab_data;
@@ -461,31 +446,22 @@ TVScrollUnlocked(struct TVRec *tv, long horiz, long ve
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);
+ TVRedrawRect(tv, &tv->frame, 0, true);
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;
+ r = (**vacated).rgnBBox;
HUnlock(vacated);
DisposeRgn(vacated);
- TVFindInRectLocked(tv, &visible, 0, TVDrawMatch, NULL);
-
+ /* only redraw what was vacated, the rest moved with its highlighting */
+ TVRedrawRect(tv, &r, 0, true);
+
TVRevealCanvas(tv);
}
@@ -538,4 +514,110 @@ TVRevealCanvas(struct TVRec *tv)
tv->canvas_old_port = NULL;
tv->canvas_dirty = false;
HUnlock(tv->canvas_baseaddr);
+}
+
+void
+TVRedrawRect(struct TVRec *tv, Rect *r, long startline, Boolean erase)
+{
+ BigRect find;
+ Rect clip;
+
+ if (!SectRect(r, &tv->frame, &clip))
+ return;
+
+ TVUseCanvas(tv);
+ ClipRect(&clip);
+
+ if (erase)
+ EraseRect(&clip);
+
+ find.left = clip.left - tv->scrolled.left;
+ find.top = clip.top - tv->scrolled.top;
+ find.right = clip.right - tv->scrolled.left;
+ find.bottom = clip.bottom - tv->scrolled.top;
+ TVFindInRectLocked(tv, &find, startline, TVDrawMatch, NULL);
+
+ if (tv->active && tv->sel_start != tv->sel_end)
+ TVInvertRange(tv, tv->sel_start, tv->sel_end, &clip);
+
+ ClipRect(&tv->frame);
+ tv->canvas_dirty = true;
+ TVRevealCanvas(tv);
+}
+
+void
+TVInvertRange(struct TVRec *tv, unsigned long a, unsigned long b,
+ Rect *clip)
+{
+ struct TVLine *line;
+ unsigned long nline, cline, t;
+ long left, right, top, bottom, edge;
+ Boolean chars, trail;
+ Rect r;
+
+ if (a > b) {
+ t = a;
+ a = b;
+ b = t;
+ }
+ if (a == b || tv->nlines == 0)
+ return;
+
+ nline = TVLineForOffset(tv, a);
+ if (nline > 1) {
+ /* a wrapped line ends where the next begins, include its trail */
+ line = TVGetLine(tv, nline - 1);
+ if (line != NULL && line->end >= a)
+ nline--;
+ }
+
+ /* skip lines above the clip */
+ cline = TVLineForY(tv, (long)clip->top - tv->scrolled.top);
+ if (cline > nline)
+ nline = cline;
+
+ edge = tv->frame.right - tv->padding.right;
+
+ TVUseCanvas(tv);
+
+ for (; nline <= tv->nlines; nline++) {
+ line = TVGetLine(tv, nline);
+ if (line == NULL || line->start >= b)
+ break;
+
+ top = tv->scrolled.top + line->rect.top;
+ bottom = tv->scrolled.top + line->rect.bottom;
+ if (top >= clip->bottom)
+ break;
+ if (bottom <= clip->top)
+ continue;
+
+ /* any of this line's characters, then the area past its end */
+ chars = (a < line->end && b > line->start);
+ trail = (a <= line->end && b > line->end);
+ if (!chars && !trail)
+ continue;
+
+ left = TVXForOffset(tv, line, a > line->start ? a : line->start);
+ right = TVXForOffset(tv, line, b < line->end ? b : line->end);
+ if (trail && right < edge)
+ right = edge;
+ if (right <= left)
+ continue;
+
+ if (top < clip->top)
+ top = clip->top;
+ if (bottom > clip->bottom)
+ bottom = clip->bottom;
+
+ r.left = tv->scrolled.left + left;
+ r.top = top;
+ r.right = tv->scrolled.left + right;
+ r.bottom = bottom;
+ if (SectRect(&r, clip, &r))
+ InvertRect(&r);
+ }
+
+ tv->canvas_dirty = true;
+ TVRevealCanvas(tv);
}
--- tv_layout.c Wed Sep 23 13:51:52 2026
+++ tv_layout.c Wed Oct 14 16:46:23 2026
@@ -115,44 +115,9 @@ TVResize(TVHandle tvh)
}
}
- 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);
+ TVRedrawRect(tv, &tv->frame, 0, true);
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;
}
@@ -160,22 +125,11 @@ 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);
+ off = TVOffsetForPoint(tv, (long)p.h - tv->scrolled.left - tv->view.left,
+ (long)p.v - tv->scrolled.top - tv->view.top);
TVUnlock(tv);
return off;
}
@@ -254,6 +208,7 @@ reload_chunk:
line->rect.left = tv->padding.left;
line->rect.top = tv->padding.top;
line->start = 0;
+ line->chunk = tv->first_chunk;
start = line->end;
} else {
prev_line = TVGetLine(tv, nline - 1);
@@ -281,18 +236,9 @@ reload_chunk:
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);
-
+ width = TVCharWidth(tv, style, line, chunk->end_right,
+ c);
+
if (c == ' ' && !style->bitmap_data_size) {
tv->last_space = n;
tv->space_width = 0;
@@ -303,7 +249,7 @@ reload_chunk:
/* break after the last space */
n = tv->last_space + 1;
chunk->end_right -= tv->space_width;
- line->end = tv->last_space + 1;
+ line->end = n;
line->rect.right = chunk->end_right;
while (n < chunk->start) {
@@ -312,9 +258,14 @@ reload_chunk:
Debugger();
return false;
}
+ chunk->end_line = 0;
chunk = TVGetChunk(tv, chunk->prev_chunk);
+ if (chunk == NULL)
+ return false;
}
+ chunk->end_right = line->rect.right;
+
/* setup chunk with new_line set */
goto reload_chunk;
}
@@ -365,6 +316,7 @@ reload_chunk:
next_line->start = n;
if (c == '\r')
next_line->start++;
+ next_line->chunk = chunk->loc;
next_line->leading = style->font_info.leading;
next_line->rect.top = line->rect.bottom;
next_line->rect.left = tv->padding.left;
@@ -480,7 +432,9 @@ TVFindInRectLocked(struct TVRec *tv, BigRect *rect, lo
/* 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);
+ /* start at this line's chunk rather than the first one */
+ match.chunk = TVGetChunk(tv, match.line->chunk ?
+ match.line->chunk : tv->first_chunk);
if (match.chunk == NULL)
goto draw_done;
}
@@ -542,4 +496,214 @@ prep_chunk:
draw_done:
TVRevealCanvas(tv);
return ret;
+}
+
+short
+TVCharWidth(struct TVRec *tv, struct TVStyle *style, struct TVLine *line,
+ long x, unsigned char c)
+{
+ long next_tabstop;
+ short width;
+
+ if (c == '\r')
+ return 0;
+
+ if (style->bitmap_data_size)
+ return style->bitmap.bounds.right - style->bitmap.bounds.left;
+
+ if (c == '\t' && tv->tabstop) {
+ next_tabstop = line->rect.left + (long)tv->tabstop *
+ (((x - line->rect.left) / tv->tabstop) + 1);
+ return next_tabstop - x;
+ }
+
+ if (!(width = style->width_cache[c]))
+ width = style->width_cache[c] = CharWidth(c);
+
+ return width;
+}
+
+unsigned long
+TVLineForY(struct TVRec *tv, long y)
+{
+ struct TVLine *line;
+ unsigned long lo, hi, mid;
+
+ if (tv->nlines == 0)
+ return 0;
+
+ /* find the last line whose top is at or above y */
+ lo = 1;
+ hi = tv->nlines;
+ while (lo < hi) {
+ mid = (lo + hi + 1) / 2;
+ line = TVGetLine(tv, mid);
+ if (line == NULL)
+ return 0;
+ if (line->rect.top <= y)
+ lo = mid;
+ else
+ hi = mid - 1;
+ }
+
+ return lo;
+}
+
+unsigned long
+TVLineForOffset(struct TVRec *tv, unsigned long off)
+{
+ struct TVLine *line;
+ unsigned long lo, hi, mid;
+
+ if (tv->nlines == 0)
+ return 0;
+
+ /* find the last line starting at or before off */
+ lo = 1;
+ hi = tv->nlines;
+ while (lo < hi) {
+ mid = (lo + hi + 1) / 2;
+ line = TVGetLine(tv, mid);
+ if (line == NULL)
+ return 0;
+ if (line->start <= off)
+ lo = mid;
+ else
+ hi = mid - 1;
+ }
+
+ return lo;
+}
+
+struct TVChunk *
+TVLineChunk(struct TVRec *tv, struct TVLine *line)
+{
+ struct TVChunk *chunk, *tchunk;
+
+ chunk = TVGetChunk(tv, line->chunk ? line->chunk : tv->first_chunk);
+
+ /* the hint should be right, but don't trust it */
+ while (chunk != NULL && chunk->start > line->start &&
+ (tchunk = TVGetChunk(tv, chunk->prev_chunk)) != NULL)
+ chunk = tchunk;
+ while (chunk != NULL && chunk->end <= line->start &&
+ (tchunk = TVGetChunk(tv, chunk->next_chunk)) != NULL)
+ chunk = tchunk;
+
+ return chunk;
+}
+
+long
+TVXForOffset(struct TVRec *tv, struct TVLine *line, unsigned long off)
+{
+ struct TVChunk *chunk;
+ struct TVStyle *style;
+ struct TVSlab *slab;
+ unsigned long n, end;
+ long x;
+
+ if (off > line->end)
+ off = line->end;
+
+ x = line->rect.left;
+
+ for (chunk = TVLineChunk(tv, line); chunk != NULL;
+ chunk = TVGetChunk(tv, chunk->next_chunk)) {
+ if (chunk->start > off)
+ break;
+
+ /* same as TVFindInRectLocked, so we measure where it drew */
+ if (chunk->start >= line->start)
+ x = chunk->start_left;
+
+ n = (chunk->start > line->start ? chunk->start : line->start);
+ end = (chunk->end < off ? chunk->end : off);
+ if (n < end) {
+ style = TVGetStyle(tv, chunk->style);
+ slab = TVGetTextSlab(tv, chunk->start);
+ if (style == NULL || slab == NULL)
+ break;
+ if (!style->bitmap_data_size)
+ TVApplyStyle(style);
+
+ for (; n < end; n++)
+ x += TVCharWidth(tv, style, line, x,
+ slab->data[n - slab->base]);
+ }
+
+ if (chunk->end >= off)
+ break;
+ }
+
+ return x + line->indent;
+}
+
+unsigned long
+TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x)
+{
+ struct TVChunk *chunk;
+ struct TVStyle *style;
+ struct TVSlab *slab;
+ unsigned long n, end;
+ long lx;
+ short width;
+
+ x -= line->indent;
+ lx = line->rect.left;
+
+ for (chunk = TVLineChunk(tv, line); chunk != NULL;
+ chunk = TVGetChunk(tv, chunk->next_chunk)) {
+ if (chunk->start > line->end)
+ break;
+
+ if (chunk->start >= line->start)
+ lx = chunk->start_left;
+
+ n = (chunk->start > line->start ? chunk->start : line->start);
+ end = (chunk->end < line->end ? chunk->end : line->end);
+ if (n < end) {
+ style = TVGetStyle(tv, chunk->style);
+ slab = TVGetTextSlab(tv, chunk->start);
+ if (style == NULL || slab == NULL)
+ break;
+ if (!style->bitmap_data_size)
+ TVApplyStyle(style);
+
+ for (; n < end; n++) {
+ width = TVCharWidth(tv, style, line, lx,
+ slab->data[n - slab->base]);
+ if (x < lx + (width / 2))
+ return n;
+ lx += width;
+ }
+ }
+
+ if (chunk->end >= line->end)
+ break;
+ }
+
+ return line->end;
+}
+
+unsigned long
+TVOffsetForPoint(struct TVRec *tv, long h, long v)
+{
+ struct TVLine *line;
+ unsigned long nline;
+
+ if (tv->nlines == 0)
+ return 0;
+
+ line = TVGetLine(tv, 1);
+ if (line == NULL || v < line->rect.top)
+ return 0;
+
+ nline = TVLineForY(tv, v);
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ return 0;
+ if (nline == tv->nlines && v >= line->rect.bottom)
+ return tv->text_length;
+
+ return TVOffsetForX(tv, line, h);
}
--- tv_store.c Wed Sep 23 13:51:52 2026
+++ tv_store.c Wed Sep 30 17:20:26 2026
@@ -34,11 +34,10 @@ TVAllocateSlab(struct TVRec *tv, unsigned long size, H
slab = (struct TVSlab *)**last;
if (slab->id == USHRT_MAX) {
Debugger();
- HUnlock(*last);
return NULL;
}
id = slab->id + 1;
- HUnlock(*last);
+ /* caller must HUnlock(*last) */
}
slabh = NewHandleClear(sizeof(struct TVSlab) + size);
@@ -99,8 +98,23 @@ TVGetTextSlab(struct TVRec *tv, unsigned long addr)
slabh = slab->next_slab;
}
-
+
return NULL;
+}
+
+unsigned char
+TVCharAt(struct TVRec *tv, unsigned long off)
+{
+ struct TVSlab *slab;
+
+ if (off >= tv->text_length)
+ return 0;
+
+ slab = TVGetTextSlab(tv, off);
+ if (slab == NULL)
+ return 0;
+
+ return slab->data[off - slab->base];
}
struct TVChunk *