AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

53

TVCalcLinesLocked+TVDrawMatch: Fix tabstop calculation

Turn tabstop into a number of pixels per tabstop rather than a number
of characters. This makes it behave more like a word processor that
will align characters of different fonts and sizes on a particular
vertical boundary, rather than indenting a number of spaces.
 
Also make TVDrawMatch bail early when drawing lines that are further
down than our viewport will show.

jcs made amendment 53 over 2 years ago
--- textview.c Wed Nov 13 20:31:30 2024 +++ textview.c Thu Nov 14 13:58:02 2024 @@ -96,6 +96,8 @@ TVNew(Rect *view, Rect *padding) tv->linewrap = true; tv->autocalc = true; + memset(&tv->tab_pat, 0x88, sizeof(tv->tab_pat)); + #ifdef USE_CANVAS if (!TVSetupCanvas(tv)) goto fail; @@ -360,7 +362,7 @@ TVLineWrap(TVHandle tvh, Boolean wrap) } void -TVTabStop(TVHandle tvh, unsigned char tabstop) +TVTabStop(TVHandle tvh, unsigned short tabstop) { struct TVRec *tv; @@ -1400,10 +1402,10 @@ TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dir struct TVStyle *style; struct TVLine *line, *prev_line, *next_line; unsigned long avail, start, nline, max_right; - long n, back_width, back_n; + long n, back_width, back_n, next_tabstop; short width, space_width, height; char *text_slab_data; - unsigned char c; + register unsigned char c; Boolean new_line; if (ignore_dirty) @@ -1488,13 +1490,17 @@ reload_chunk: } else { if (style->bitmap_data_size) width = style->font_info.widMax; - else if (!(width = style->width_cache[c])) + else if (c == '\t' && tv->tabstop) { + next_tabstop = (long)tv->tabstop * + (((chunk->end_right - line->rect.left) / + tv->tabstop) + 1); + if (next_tabstop == chunk->end_right) + next_tabstop += tv->tabstop; + width = next_tabstop - chunk->end_right - + line->rect.left; + } else 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 == ' ' && !style->bitmap_data_size) { tv->last_space = n; space_width = 0; @@ -1606,14 +1612,24 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch void *cookie) { unsigned long n, j; + long next_tabstop; unsigned char c; char *text, *spanstart; short ts; Rect r; + Point pen; if (match->text_end == match->text_start) return true; + if (match->line->rect.bottom < -(tv->scrolled.top)) + /* no sense drawing what we can't see yet */ + return true; + + if (match->line->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 = match->line->rect.left; r.top = tv->scrolled.top + match->line->rect.top; @@ -1646,12 +1662,25 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch 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(' '); + GetPen(&pen); + next_tabstop = (long)tv->tabstop * + (((pen.h - match->rect.left) / tv->tabstop) + 1); + if (next_tabstop == pen.h) + next_tabstop += tv->tabstop; +#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; --- textview.h Wed Nov 13 20:15:07 2024 +++ textview.h Thu Nov 14 13:51:04 2024 @@ -79,8 +79,8 @@ struct TVLine { unsigned long end; BigRect rect; unsigned long nline; - unsigned char baseline; unsigned short leading; + unsigned char baseline; Boolean grew; }; @@ -145,10 +145,13 @@ struct TVRec { unsigned long sel_start; unsigned long sel_end; + /* number of pixels for each tabstop, usually CharWidth(' ')*4 or 8 */ + unsigned short tabstop; + Pattern tab_pat; + Boolean autocalc; Boolean autoscroll; Boolean linewrap; - unsigned char tabstop; char unlock_refcnt; struct TVLine *top_line; @@ -167,7 +170,7 @@ Boolean TVAppendBitMap(TVHandle tvh, BitMap *bitmap, void TVAutoCalc(TVHandle tvh, Boolean calc); void TVAutoScroll(TVHandle tvh, Boolean scroll); void TVLineWrap(TVHandle tvh, Boolean wrap); -void TVTabStop(TVHandle tvh, unsigned char tabstop); +void TVTabStop(TVHandle tvh, unsigned short tabstop); Boolean TVResize(TVHandle tvh); void TVUpdate(TVHandle tvh); void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller);