AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

36

Add toggle for line wrapping and a configurable tab stop

When tab stop is non-zero, tab characters are rendered as spaces to
that tab stop which only really makes sense right now for fixed-width
fonts

jcs made amendment 36 over 2 years ago
--- textview.c Thu Oct 24 10:11:59 2024 +++ textview.c Fri Oct 25 10:26:36 2024 @@ -87,6 +87,7 @@ TVNew(Rect *view, Rect *padding) tv->padding.left++; tv->cur_style = NO_STYLE; + tv->linewrap = true; if (!TVSetupCanvas(tv)) goto fail; @@ -357,6 +358,26 @@ TVAutoScroll(TVHandle tvh, Boolean scroll) } void +TVLineWrap(TVHandle tvh, Boolean wrap) +{ + struct TVRec *tv; + + tv = TVLock(tvh); + tv->linewrap = wrap; + TVUnlock(tv); +} + +void +TVTabStop(TVHandle tvh, unsigned char tabstop) +{ + struct TVRec *tv; + + tv = TVLock(tvh); + tv->tabstop = tabstop; + TVUnlock(tv); +} + +void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller) { struct TVRec *tv; @@ -1157,10 +1178,14 @@ reload_chunk: if (!(width = style->width_cache[c])) width = style->width_cache[c] = CharWidth(c); + if (c == '\t' && tv->tabstop) + width = CharWidth(' ') * + ((n - line->start + 1) % tv->tabstop); + if (c == ' ') { tv->last_space = n; space_width = 0; - } else if (!new_line && + } else if (!new_line && tv->linewrap && chunk->end_right + width >= max_right) { new_line = true; if (tv->last_space) { @@ -1257,13 +1282,41 @@ Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match, void *cookie) { + unsigned long n, j; + unsigned char c; + char *text, *spanstart; + short ts; + if (match->text_end == match->text_start) return true; MoveTo(match->rect.left, tv->scrolled.top + match->line->rect.bottom - match->line->baseline); - DrawText(match->text_slab_data + (match->text_start - - match->text_slab->base), 0, match->text_end - match->text_start); + if (tv->tabstop) { + 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); + ts = (match->text_start - match->line->start + n + 2) % + tv->tabstop; + if (ts < 1 || ts > tv->tabstop) + ts = tv->tabstop; + for (j = 0; j < ts; j++) + DrawChar(' '); + 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; --- textview.h Tue Oct 22 11:49:07 2024 +++ textview.h Thu Oct 24 21:34:25 2024 @@ -14,6 +14,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef __TEXTVIEW_H__ +#define __TEXTVIEW_H__ + #include <stddef.h> #include <limits.h> @@ -124,8 +127,10 @@ struct TVRec { #define TV_LINE_GROUP_SIZE (sizeof(struct TVLine) * TV_LINE_GROUP_NLINES) unsigned short nlines; - char unlock_refcnt; Boolean autoscroll; + Boolean linewrap; + unsigned char tabstop; + char unlock_refcnt; }; typedef struct TVRec ** TVHandle; @@ -134,6 +139,8 @@ void TVDispose(TVHandle tvh); Boolean TVAppend(TVHandle tvh, struct TVStyle *style, char *text, unsigned long length); void TVAutoScroll(TVHandle tvh, Boolean scroll); +void TVLineWrap(TVHandle tvh, Boolean wrap); +void TVTabStop(TVHandle tvh, unsigned char tabstop); Boolean TVResize(TVHandle tvh); void TVUpdate(TVHandle tvh); void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller); @@ -145,3 +152,5 @@ Boolean TVCalcLines(TVHandle tvh); void TVSetTrackScrollControl(TVHandle tvh); pascal void TVTrackScrollControl(ControlHandle control, short part); + +#endif /* __TEXTVIEW__ */ \ No newline at end of file