jcs
/textview
/amendments
/28
Use a common interation function for Draw, Scroll, and GetOffset
jcs made amendment 28 over 2 years ago
--- textview.c Fri Oct 18 21:05:32 2024
+++ textview.c Mon Oct 21 13:10:31 2024
@@ -27,6 +27,22 @@
(_style)->style == 0 && (_style)->size == 0)
#define NO_STYLE 0xff
+struct TVFindInRectMatch {
+ BigRect rect;
+ struct TVChunk *chunk;
+ struct TVLine *line;
+ struct TVStyle *style;
+ struct TVSlab *text_slab;
+ char *text_slab_data;
+ unsigned long text_start;
+ unsigned long text_end;
+ /* the original rect we were asked to find */
+ BigRect findrect;
+};
+
+typedef Boolean (*TVFindInRectCallback)(struct TVRec *tv,
+ struct TVFindInRectMatch *match, void *cookie);
+
struct TVRec * TVLock(TVHandle tvh);
void TVUnlock(struct TVRec *);
struct TVSlab * TVAllocateTextSlab(struct TVRec *tv);
@@ -40,7 +56,12 @@ Boolean TVCalcLines(struct TVRec *tv);
void TVApplyStyle(struct TVStyle *style);
struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style);
void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert);
-Boolean TVDrawVisibleLines(struct TVRec *tv);
+Boolean TVGetOffsetCallback(struct TVRec *tv,
+ struct TVFindInRectMatch *match, void *cookie);
+Boolean TVFindInRect(struct TVRec *tv, BigRect *findrect,
+ TVFindInRectCallback callback, void *cookie);
+Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie);
void TVUseCanvas(struct TVRec *tv);
void TVRevealCanvas(struct TVRec *tv);
@@ -113,10 +134,6 @@ TVNew(Rect *view, Rect *padding)
tv->cur_style = NO_STYLE;
- TVUseCanvas(tv);
- EraseRect(&tv->canvas->portRect);
- TVRevealCanvas(tv);
-
HUnlock(tvh);
return tvh;
@@ -143,6 +160,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
struct TVSlab *text_slab;
struct TVChunk *chunk, *prev_chunk;
struct TVStyle *cur_style;
+ BigRect visible;
unsigned long avail;
short width, prev_style_idx;
char *text_slab_data;
@@ -211,7 +229,11 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
}
if (TVCalcLines(tv)) {
- TVDrawVisibleLines(tv);
+ visible.top = -(tv->scrolled.top);
+ visible.left = -(tv->scrolled.left);
+ visible.bottom = visible.top + tv->view_height;
+ visible.right = visible.left + (tv->view.right - tv->view.left);
+ TVFindInRect(tv, &visible, TVDrawMatch, NULL);
ret = true;
}
@@ -299,87 +321,50 @@ TVClick(TVHandle tvh, Point p, Boolean extend)
TVUnlock(tv);
}
-unsigned long
-TVGetOffset(TVHandle tvh, Point p)
+Boolean
+TVGetOffsetCallback(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie)
{
- struct TVRec *tv;
- struct TVChunk *chunk;
- struct TVLine *line;
- struct TVStyle *style;
- struct TVSlab *text_slab;
- unsigned long off, n;
+ unsigned long n, left, *off;
short width;
- char *text_slab_data;
- long horiz, vert, linen, start, left, top, right, end, bottom;
unsigned char c;
- off = 0;
+ off = (unsigned 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;
+}
+
+unsigned long
+TVGetOffset(TVHandle tvh, Point p)
+{
+ struct TVRec *tv;
+ BigRect prect;
+ unsigned long off;
+
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);
+ prect.left = p.h + tv->scrolled.left - tv->view.left;
+ prect.top = p.v + tv->scrolled.top - tv->view.top;
+ prect.right = prect.left;
+ prect.bottom = prect.top;
- 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:
+ off = 0;
+ TVFindInRect(tv, &prect, TVGetOffsetCallback, &off);
+
TVRevealCanvas(tv);
TVUnlock(tv);
return off;
@@ -810,12 +795,10 @@ TVGetLine(struct TVRec *tv, unsigned long line)
idx = line - 1 - (groupn * TV_LINE_GROUP_NLINES);
group = tv->line_groups[groupn];
- if (group == NULL) {
- if ((group = TVAllocateLineGroup(tv, groupn)) == NULL)
- return NULL;
- }
- HLock(group);
+ if (group == NULL && (group = TVAllocateLineGroup(tv, groupn)) == NULL)
+ return NULL;
+ HLock(group);
/* keep group locked */
return (struct TVLine *)(*group + (sizeof(struct TVLine) * idx));
@@ -1044,100 +1027,108 @@ done_chunks:
}
Boolean
-TVDrawVisibleLines(struct TVRec *tv)
+TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match,
+ void *cookie)
{
- struct TVSlab *text_slab;
- struct TVChunk *chunk;
- struct TVStyle *style;
- struct TVLine *line, *next_line;
- Rect r;
- char c, *text_slab_data;
- unsigned long bottom, start, end, len;
- unsigned short chars, linen;
- long left;
+ Boolean ignore_dirty = false;
+
+ if (cookie != NULL)
+ ignore_dirty = *((Boolean *)cookie);
+
+ if (match->chunk->dirty && !ignore_dirty)
+ return true;
+ 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);
+
+ return true;
+}
+
+Boolean
+TVFindInRect(struct TVRec *tv, BigRect *rect, TVFindInRectCallback callback,
+ void *cookie)
+{
+ struct TVFindInRectMatch match;
+ unsigned short linen;
Boolean ret;
TVUseCanvas(tv);
- EraseRect(&tv->canvas->portRect);
- tv->top_line = 0;
- tv->bottom_line = 0;
- chunk = NULL;
+ match.findrect = *rect;
+ match.chunk = NULL;
ret = false;
for (linen = 1; linen <= tv->nlines; linen++) {
- line = TVGetLine(tv, linen);
- if (line == NULL)
+ match.line = TVGetLine(tv, linen);
+ if (match.line == NULL)
goto draw_done;
- if (tv->scrolled.top + line->rect.bottom < 0L)
+ if (match.line->rect.bottom < rect->top)
/* is not in the view at all yet */
continue;
- if (tv->scrolled.top + line->rect.top >= tv->view_height)
+ if (match.line->rect.top >= rect->bottom)
/* is further down than the view, we've done enough lines */
break;
/* advance chunk if we need to catch up to this line */
- if (chunk == NULL || chunk->end_line < linen) {
- if (chunk == NULL) {
- chunk = TVGetChunk(tv, tv->first_chunk);
- if (chunk == NULL)
+ if (match.chunk == NULL || match.chunk->end_line < linen) {
+ if (match.chunk == NULL) {
+ match.chunk = TVGetChunk(tv, tv->first_chunk);
+ if (match.chunk == NULL)
goto draw_done;
- } else {
- while (chunk->end_line < linen) {
- chunk = TVGetChunk(tv, chunk->next_chunk);
- if (!chunk)
- goto draw_done;
- }
}
+
+ while (match.chunk->end_line < linen ||
+ match.chunk->end < match.line->start) {
+ match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
+ if (!match.chunk)
+ goto draw_done;
+ }
prep_chunk:
- text_slab = TVGetTextSlab(tv, chunk->start);
- if (text_slab == NULL)
+ match.text_slab = TVGetTextSlab(tv, match.chunk->start);
+ if (match.text_slab == NULL)
goto draw_done;
- text_slab_data = *(text_slab->data);
+ match.text_slab_data = *(match.text_slab->data);
- style = &tv->styles[chunk->style];
- if (style == NULL)
+ match.style = &tv->styles[match.chunk->style];
+ if (match.style == NULL)
goto draw_done;
- TVApplyStyle(style);
+ TVApplyStyle(match.style);
}
-
- if (line->start > chunk->start) {
- start = line->start;
- left = line->rect.left;
+
+ match.rect.top = match.line->rect.top;
+ match.rect.bottom = match.line->rect.bottom;
+
+ if (match.line->start > match.chunk->start) {
+ match.text_start = match.line->start;
+ match.rect.left = match.line->rect.left;
} else {
- start = chunk->start;
- left = chunk->start_left;
+ match.text_start = match.chunk->start;
+ match.rect.left = match.chunk->start_left;
}
- if (line->end < chunk->end)
- end = line->end;
- else
- end = chunk->end;
-
- if (tv->top_line == 0 && (tv->scrolled.top + line->rect.top +
- ((line->rect.bottom - line->rect.top) / 2) > 0))
- /* this line is more than half visible at the top */
- tv->top_line = linen;
-
- if ((tv->scrolled.top + line->rect.top +
- ((line->rect.bottom - line->rect.top) / 2) < tv->view_height))
- /* this line is more than half visible at the bottom */
- tv->bottom_line = linen;
-
- if (end != start) {
- MoveTo(left, tv->scrolled.top + line->rect.bottom -
- line->baseline);
- DrawText(text_slab_data + (start - text_slab->base), 0,
- end - start);
+ if (match.line->end < match.chunk->end) {
+ match.text_end = match.line->end;
+ match.rect.right = match.line->rect.right;
+ } else {
+ match.text_end = match.chunk->end;
+ match.rect.right = match.chunk->end_right;
}
- if (chunk->end_line == linen && chunk->end < line->end) {
+ if (!callback(tv, &match, cookie))
+ goto draw_done;
+
+ if (match.chunk->end_line == linen &&
+ match.chunk->end < match.line->end) {
/* more chunks on this line */
- chunk = TVGetChunk(tv, chunk->next_chunk);
- if (chunk != NULL)
+ match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
+ if (match.chunk != NULL)
goto prep_chunk;
}
}
@@ -1151,17 +1142,25 @@ draw_done:
void
TVScrollUnlocked(struct TVRec *tv, short horiz, short vert)
{
- short top, left;
- Rect was, sect;
+ struct TVChunk *chunk;
+ struct TVLine *line;
+ struct TVStyle *style;
+ struct TVSlab *text_slab;
+ long wastop, top, wasleft, left;
+ BigRect visible;
RgnHandle vacated;
+ char *text_slab_data;
+ Boolean ignore;
if (!horiz && !vert)
return;
TVUseCanvas(tv);
-
- BigRectToRect(tv->scrolled, was);
+ ignore = true;
+ wastop = tv->scrolled.top;
+ wasleft = tv->scrolled.left;
+
tv->scrolled.bottom += vert;
tv->scrolled.top += vert;
if (tv->scrolled.bottom < tv->view_height) {
@@ -1179,16 +1178,33 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short
tv->scrolled.right += horiz;
/* TODO: bound */
-#if 0
- /* TODO: scrollrect, mark lines in vacated space dirty, draw those */
+ left = tv->scrolled.left - wasleft;
+ top = tv->scrolled.top - wastop;
+
+ if ((top < 0 ? -top : top) > tv->view_height) {
+ /* no point in scrolling, we have to redraw the whole view */
+ EraseRect(&tv->canvas->portRect);
+ visible.top = -(tv->scrolled.top);
+ visible.left = -(tv->scrolled.left);
+ visible.bottom = visible.top + tv->view_height;
+ visible.right = visible.left + (tv->view.right - tv->view.left);
+ TVFindInRect(tv, &visible, TVDrawMatch, &ignore);
+ TVRevealCanvas(tv);
+ return;
+ }
+
vacated = NewRgn();
- left = tv->scrolled.left - was.left;
- top = tv->scrolled.top - was.top;
- ScrollRect(&tv->canvas.portRect, left, top, vacated);
+ ScrollRect(&tv->canvas->portRect, left, top, vacated);
+ 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;
+ HUnlock(vacated);
DisposeRgn(vacated);
-#endif
+
+ TVFindInRect(tv, &visible, TVDrawMatch, &ignore);
- TVDrawVisibleLines(tv);
TVRevealCanvas(tv);
}
--- textview.h Fri Oct 18 16:00:15 2024
+++ textview.h Fri Oct 18 22:16:18 2024
@@ -71,6 +71,7 @@ struct TVLine {
unsigned long end;
BigRect rect;
unsigned char baseline;
+ Boolean dirty;
};
struct TVSlab {