AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

27

Implement TVGetOffset for callers to map clicks to text


jcs made amendment 27 over 2 years ago
--- textview.c Fri Oct 18 15:22:49 2024 +++ textview.c Fri Oct 18 21:05:32 2024 @@ -299,6 +299,92 @@ TVClick(TVHandle tvh, Point p, Boolean extend) TVUnlock(tv); } +unsigned long +TVGetOffset(TVHandle tvh, Point p) +{ + struct TVRec *tv; + struct TVChunk *chunk; + struct TVLine *line; + struct TVStyle *style; + struct TVSlab *text_slab; + unsigned long off, n; + short width; + char *text_slab_data; + long horiz, vert, linen, start, left, top, right, end, bottom; + unsigned char c; + + off = 0; + + tv = TVLock(tvh); + TVUseCanvas(tv); + + horiz = p.h + tv->scrolled.left - tv->view.left; + vert = p.v + tv->scrolled.top - tv->view.top; + + /* assume the caller is only interested in things the user could see */ + for (linen = tv->top_line; linen <= tv->bottom_line; linen++) { + line = TVGetLine(tv, linen); + if (line == NULL) + continue; + + /* can't use PtInRect because line->rect is a BigRect */ + if (horiz < line->rect.left || horiz > line->rect.right || + vert < line->rect.top || vert > line->rect.bottom) + continue; + + /* find chunk that includes this line and point */ + for (chunk = TVGetChunk(tv, tv->first_chunk); + chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) { + if (chunk->start_line > linen || chunk->end_line < linen) + continue; + if (chunk->start_line == linen && chunk->start_left > horiz) + continue; + if (chunk->end_line == linen && chunk->end_right < horiz) + continue; + + style = &tv->styles[chunk->style]; + if (style == NULL) + goto get_done; + + text_slab = TVGetTextSlab(tv, chunk->start); + if (text_slab == NULL) + goto get_done; + text_slab_data = *(text_slab->data); + + if (chunk->start_line == linen) { + start = chunk->start; + left = chunk->start_left; + } else { + start = line->start; + left = line->rect.left; + } + + if (chunk->end_line == linen) + end = chunk->end; + else + end = line->end; + + for (n = start; n < end; n++) { + c = text_slab_data[n - text_slab->base]; + width = style->width_cache[c]; + + if (left + width >= horiz) { + off = n; + goto get_done; + } + left += width; + } + + goto get_done; + } + } + +get_done: + TVRevealCanvas(tv); + TVUnlock(tv); + return off; +} + static TVHandle tv_track_control = NULL; void TVSetTrackScrollControl(TVHandle tvh) --- textview.h Fri Oct 18 15:17:34 2024 +++ textview.h Fri Oct 18 16:00:15 2024 @@ -132,6 +132,7 @@ Boolean TVAppend(TVHandle tvh, struct TVStyle *style, void TVUpdate(TVHandle tvh); void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller); void TVClick(TVHandle tvh, Point p, Boolean extend); +unsigned long TVGetOffset(TVHandle tvh, Point p); void TVScroll(TVHandle tvh, short horiz, short vert); void TVClear(TVHandle tvh);