jcs
/textview
/amendments
/94
TVOffsetFor*: Add caret flag, add TVScrollToOffset
Finding the offset for a click depends on whether the caller wants
to find text displayed at it or whether they are selecting text which
can start outside of that text.
jcs made amendment 94 14 days ago
--- textview.h Tue Nov 3 15:33:16 2026
+++ textview.h Wed Nov 11 13:40:59 2026
@@ -213,6 +213,7 @@ 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 TVScrollToOffset(TVHandle tvh, unsigned long off);
Boolean TVCopy(TVHandle tvh);
void TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end);
void TVGetSelect(TVHandle tvh, unsigned long *start, unsigned long *end);
@@ -271,8 +272,9 @@ 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);
+long TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x,
+ Boolean caret);
+long TVOffsetForPoint(struct TVRec *tv, long h, long v, Boolean caret);
void TVRedrawRect(struct TVRec *tv, Rect *r, long startline, Boolean erase);
void TVInvertRange(struct TVRec *tv, unsigned long a, unsigned long b,
Rect *clip);
--- tv_draw.c Mon Nov 2 15:08:33 2026
+++ tv_draw.c Wed Nov 11 13:39:56 2026
@@ -182,6 +182,22 @@ TVScroll(TVHandle tvh, long horiz, long vert)
}
void
+TVScrollToOffset(TVHandle tvh, unsigned long off)
+{
+ struct TVRec *tv;
+ struct TVLine *line;
+ short div;
+
+ tv = TVLock(tvh);
+ div = TVScrollDivisor(tv);
+ line = TVGetLine(tv, TVLineForOffset(tv, off));
+ if (line != NULL)
+ TVScrollLocked(tv, 0,
+ -((line->rect.top / div) * div) - tv->scrolled.top);
+ TVUnlock(tv);
+}
+
+void
TVUpdate(TVHandle tvh)
{
struct TVRec *tv;
--- tv_layout.c Mon Nov 2 21:46:42 2026
+++ tv_layout.c Wed Nov 11 13:33:11 2026
@@ -120,8 +120,9 @@ TVGetTextOffset(TVHandle tvh, Point p)
long off;
tv = TVLock(tvh);
- off = TVOffsetForPoint(tv, (long)p.h - tv->scrolled.left - tv->view.left,
- (long)p.v - tv->scrolled.top - tv->view.top);
+ off = TVOffsetForPoint(tv,
+ (long)p.h - tv->scrolled.left - tv->view.left,
+ (long)p.v - tv->scrolled.top - tv->view.top, false);
TVUnlock(tv);
return off;
}
@@ -242,7 +243,7 @@ reload_chunk:
tv->last_space = n;
tv->space_width = 0;
} else if (!new_line && tv->linewrap &&
- chunk->end_right + width >= max_right) {
+ chunk->end_right + width > max_right + tv->padding.left) {
new_line = true;
if (tv->last_space) {
/* break after the last space */
@@ -628,8 +629,8 @@ TVXForOffset(struct TVRec *tv, struct TVLine *line, un
return x + line->indent;
}
-unsigned long
-TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x)
+long
+TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x, Boolean caret)
{
struct TVChunk *chunk;
struct TVStyle *style;
@@ -662,7 +663,12 @@ TVOffsetForX(struct TVRec *tv, struct TVLine *line, lo
for (; n < end; n++) {
width = TVCharWidth(tv, style, line, lx,
slab->data[n - slab->base]);
- if (x < lx + (width / 2))
+ if (caret) {
+ if (x < lx + (width / 2))
+ return n;
+ } else if (x < lx)
+ return -1;
+ else if (x < lx + width)
return n;
lx += width;
}
@@ -672,28 +678,28 @@ TVOffsetForX(struct TVRec *tv, struct TVLine *line, lo
break;
}
- return line->end;
+ return (caret ? line->end : -1);
}
-unsigned long
-TVOffsetForPoint(struct TVRec *tv, long h, long v)
+long
+TVOffsetForPoint(struct TVRec *tv, long h, long v, Boolean caret)
{
struct TVLine *line;
unsigned long nline;
if (tv->nlines == 0)
- return 0;
+ return (caret ? 0 : -1);
line = TVGetLine(tv, 1);
if (line == NULL || v < line->rect.top)
- return 0;
+ return (caret ? 0 : -1);
nline = TVLineForY(tv, v);
line = TVGetLine(tv, nline);
if (line == NULL)
- return 0;
+ return (caret ? 0 : -1);
if (nline == tv->nlines && v >= line->rect.bottom)
- return tv->text_length;
+ return (caret ? tv->text_length : -1);
- return TVOffsetForX(tv, line, h);
+ return TVOffsetForX(tv, line, h, caret);
}
--- tv_select.c Thu Oct 15 09:38:27 2026
+++ tv_select.c Wed Nov 11 13:31:15 2026
@@ -47,8 +47,9 @@ TVClick(TVHandle tvh, Point p, Boolean extend)
tv = TVLock(tvh);
- off = TVOffsetForPoint(tv, (long)p.h - tv->view.left - tv->scrolled.left,
- (long)p.v - tv->view.top - tv->scrolled.top);
+ off = TVOffsetForPoint(tv,
+ (long)p.h - tv->view.left - tv->scrolled.left,
+ (long)p.v - tv->view.top - tv->scrolled.top, true);
dh = p.h - tv->last_click_point.h;
dv = p.v - tv->last_click_point.v;
@@ -123,7 +124,7 @@ TVClick(TVHandle tvh, Point p, Boolean extend)
off = TVOffsetForPoint(tv,
(long)mp.h - tv->view.left - tv->scrolled.left,
- v - tv->view.top - tv->scrolled.top);
+ v - tv->view.top - tv->scrolled.top, true);
}
tv->last_click_ticks = TickCount();