jcs
/textview
/amendments
/45
*: Stop using the canvas, it's slowing us down
It looks smoother but slower, so if we just speed things up we won't
need it
jcs made amendment 45 over 2 years ago
--- textview.c Fri Nov 8 09:59:28 2024
+++ textview.c Fri Nov 8 17:34:08 2024
@@ -19,7 +19,7 @@
#include <string.h>
#include "textview.h"
-//#define DEBUG_DRAWING 1
+//#define USE_CANVAS 1
#define ARE_STYLES_EQUAL(one, two) ((one)->font == (two)->font && \
(one)->style == (two)->style && (one)->size == (two)->size)
@@ -89,12 +89,20 @@ TVNew(Rect *view, Rect *padding)
tv->padding = *padding;
tv->padding.left++;
+ SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left,
+ tv->view.bottom - tv->view.top);
+
tv->cur_style = NO_STYLE;
tv->linewrap = true;
tv->autocalc = true;
+#ifdef USE_CANVAS
if (!TVSetupCanvas(tv))
goto fail;
+#endif
+ tv->bounds.right = tv->frame.right;
+ tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
+ tv->scrolled = tv->bounds;
if (TVAllocateSlab(tv, TV_TEXT_SLAB_SIZE, &tv->first_text_slab,
&tv->last_text_slab) == NULL)
@@ -250,13 +258,12 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
/* keep scrolling and redrawing in one CopyBits */
TVUseCanvas(tv);
- if (tv->autoscroll && tv->scrolled.bottom > tv->view_height) {
+ if (tv->autoscroll && tv->scrolled.bottom > tv->frame.bottom) {
wastop = tv->scrolled.top;
- tv->scrolled.bottom = tv->view_height;
+ tv->scrolled.bottom = tv->frame.bottom;
tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
vacated = NewRgn();
- ScrollRect(&tv->canvas->portRect, 0, tv->scrolled.top - wastop,
- vacated);
+ ScrollRect(&tv->frame, 0, tv->scrolled.top - wastop, vacated);
DisposeRgn(vacated);
tv->canvas_dirty = true;
}
@@ -269,10 +276,10 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
redraw.top = fromline ? fromline->rect.top : tv->padding.top;
redraw.bottom = toline->rect.bottom;
redraw.left = 0;
- redraw.right = tv->view.right - tv->view.left;
+ redraw.right = tv->frame.right;
if (tv->scrolled.top + redraw.bottom > 0L &&
- tv->scrolled.top + redraw.top < tv->view_height)
+ tv->scrolled.top + redraw.top < tv->frame.bottom)
TVFindInRect(tv, &redraw, fromline ? fromline->nline : 1,
TVDrawMatch, NULL);
@@ -305,9 +312,8 @@ TVResize(TVHandle tvh)
tv = TVLock(tvh);
- /* clear old canvas */
TVUseCanvas(tv);
- EraseRect(&tv->canvas->portRect);
+ EraseRect(&tv->frame);
TVRevealCanvas(tv);
/* find text position of first line to try to stay there */
@@ -325,11 +331,18 @@ TVResize(TVHandle tvh)
break;
}
+#ifdef USE_CANVAS
if (!TVSetupCanvas(tv)) {
TVUnlock(tv);
return false;
}
-
+#endif
+ SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left,
+ tv->view.bottom - tv->view.top);
+ tv->bounds.right = tv->frame.right;
+ tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
+ tv->scrolled = tv->bounds;
+
/* clear line groups except first one */
for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
n++) {
@@ -351,9 +364,9 @@ TVResize(TVHandle tvh)
return false;
}
- if (tv->bounds.bottom > tv->view_height) {
+ if (tv->bounds.bottom > tv->frame.bottom) {
if (tv->autoscroll) {
- tv->scrolled.bottom = tv->view_height;
+ tv->scrolled.bottom = tv->frame.bottom;
tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
} else {
for (nline = 1; nline <= tv->nlines; nline++) {
@@ -376,8 +389,8 @@ TVResize(TVHandle tvh)
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);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
TVFindInRect(tv, &visible, 1, TVDrawMatch, NULL);
TVRevealCanvas(tv);
@@ -635,7 +648,7 @@ TVTrackScrollControl(ControlHandle control, short part
/* is not in the view at all yet */
continue;
- if (tv->scrolled.top + line->rect.top >= tv->view_height)
+ if (tv->scrolled.top + line->rect.top >= tv->frame.bottom)
/* is further down than the view, we've done enough lines */
break;
@@ -677,7 +690,7 @@ TVTrackScrollControl(ControlHandle control, short part
break;
case inPageUp:
/* scroll to put the top mostly-visible line at the bottom */
- TVScrollUnlocked(tv, 0, tv->view_height -
+ TVScrollUnlocked(tv, 0, (long)tv->frame.bottom -
(tv->scrolled.top + top_line->rect.bottom) - 1);
break;
case inDownButton:
@@ -690,7 +703,7 @@ TVTrackScrollControl(ControlHandle control, short part
if (bottom_line->nline < tv->nlines) {
line = TVGetLine(tv, bottom_line->nline + 1);
TVScrollUnlocked(tv, 0,
- -(tv->scrolled.top - tv->view_height + line->rect.bottom));
+ -(tv->scrolled.top - tv->frame.bottom + line->rect.bottom));
break;
}
@@ -709,7 +722,8 @@ TVTrackScrollControl(ControlHandle control, short part
}
/* nothing found, just scroll to the bottom */
- TVScrollUnlocked(tv, 0, tv->view_height - tv->scrolled.bottom);
+ TVScrollUnlocked(tv, 0, (long)tv->frame.bottom -
+ tv->scrolled.bottom);
break;
}
@@ -805,7 +819,7 @@ TVClear(TVHandle tvh)
tv->cur_style = NO_STYLE;
TVUseCanvas(tv);
- EraseRect(&tv->canvas->portRect);
+ EraseRect(&tv->frame);
tv->canvas_dirty = true;
TVRevealCanvas(tv);
TVUnlock(tv);
@@ -820,13 +834,15 @@ TVUpdate(TVHandle tvh)
tv = TVLock(tvh);
TVUseCanvas(tv);
- EraseRect(&tv->canvas->portRect);
+#ifdef USE_CANVAS
+ EraseRect(&tv->frame);
tv->canvas_dirty = true;
+#endif
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);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
TVFindInRect(tv, &visible, 1, TVDrawMatch, NULL);
TVRevealCanvas(tv);
@@ -847,8 +863,11 @@ TVDispose(TVHandle tvh)
DisposeHandle(tv->first_text_slab);
DisposeHandle(tv->line_groups[0]);
+#ifdef USE_CANVAS
ClosePort(tv->canvas);
DisposeHandle(tv->canvas_baseaddr);
+#endif
+
DisposeHandle(tvh);
}
@@ -963,12 +982,6 @@ TVSetupCanvas(struct TVRec *tv)
HLock(tv->canvas_baseaddr);
tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
- tv->view_height = height;
-
- tv->bounds.right = width;
- tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
- tv->scrolled = tv->bounds;
-
return true;
fail:
@@ -1246,7 +1259,7 @@ TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dir
max_right = tv->view.right - tv->view.left - tv->padding.left -
tv->padding.right;
-
+
for (; chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) {
new_line = false;
@@ -1363,10 +1376,6 @@ reload_chunk:
} else if (height > line->rect.bottom - line->rect.top)
line->rect.bottom = line->rect.bottom + height;
- if (nline == USHRT_MAX) {
- Debugger();
- return false;
- }
nline++;
chunk->end_line = nline;
chunk->end_right = tv->padding.left;
@@ -1428,7 +1437,7 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
if (match->text_end == match->text_start)
return true;
-
+
MoveTo(match->rect.left, tv->scrolled.top + match->line->rect.bottom -
match->line->baseline);
if (tv->tabstop) {
@@ -1487,7 +1496,7 @@ TVFindInRect(struct TVRec *tv, BigRect *rect, short st
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 (match.chunk == NULL || match.chunk->end_line < nline) {
if (match.chunk == NULL) {
@@ -1576,9 +1585,9 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short
tv->scrolled.bottom += vert;
tv->scrolled.top += vert;
- if (tv->scrolled.bottom < tv->view_height) {
+ if (tv->scrolled.bottom < tv->frame.bottom) {
/* can never scroll down lower than last line */
- tv->scrolled.bottom = tv->view_height;
+ tv->scrolled.bottom = tv->frame.bottom;
tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
}
if (tv->scrolled.top > 0) {
@@ -1594,20 +1603,22 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short
left = tv->scrolled.left - wasleft;
top = tv->scrolled.top - wastop;
- if ((top < 0 ? -top : top) > tv->view_height) {
- EraseRect(&tv->canvas->portRect);
+ if ((top < 0 ? -top : top) > tv->frame.bottom) {
+ /* moving a lot so nothing to ScrollRect */
+ EraseRect(&tv->frame);
tv->canvas_dirty = true;
+
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);
+ visible.bottom = visible.top + tv->frame.bottom;
+ visible.right = visible.left + tv->frame.right;
TVFindInRect(tv, &visible, 1, TVDrawMatch, NULL);
TVRevealCanvas(tv);
return;
}
vacated = NewRgn();
- ScrollRect(&tv->canvas->portRect, left, top, vacated);
+ ScrollRect(&tv->frame, left, top, vacated);
tv->canvas_dirty = true;
HLock(vacated);
visible.left = (**vacated).rgnBBox.left - tv->scrolled.left;
@@ -1631,17 +1642,18 @@ TVUseCanvas(struct TVRec *tv)
if (tv->canvas_refcnt == 0)
Debugger();
-#ifdef DEBUG_DRAWING
- SetOrigin(-tv->view.left, -tv->view.top);
- return;
-#endif
-
+#ifdef USE_CANVAS
GetPort(&tv->canvas_old_port);
HLock(tv->canvas_baseaddr);
tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
SetPort(tv->canvas);
+#else
+ GetClip(tv->view_clip = NewRgn());
+ SetOrigin(-tv->view.left, -tv->view.top);
+ ClipRect(&tv->frame);
+#endif
}
void
@@ -1653,16 +1665,17 @@ TVRevealCanvas(struct TVRec *tv)
if (--tv->canvas_refcnt != 0)
return;
-#ifdef DEBUG_DRAWING
- SetOrigin(0, 0);
- return;
-#endif
-
+#ifdef USE_CANVAS
SetPort(tv->canvas_old_port);
if (tv->canvas_dirty)
CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits,
&tv->canvas->portRect, &tv->view, srcCopy, NULL);
+#else
+ SetOrigin(0, 0);
+ SetClip(tv->view_clip);
+ DisposeRgn(tv->view_clip);
+#endif
ValidRect(&tv->view);
--- textview.h Thu Nov 7 16:39:24 2024
+++ textview.h Fri Nov 8 17:13:17 2024
@@ -73,7 +73,7 @@ struct TVLine {
unsigned long start;
unsigned long end;
BigRect rect;
- unsigned short nline;
+ unsigned long nline;
unsigned char baseline;
};
@@ -90,8 +90,11 @@ struct TVSlab {
struct TVRec {
/* bounds of where we live in window's port */
Rect view;
- long view_height;
+ RgnHandle view_clip;
+ /* visible rect relative to view */
+ Rect frame;
+
/* full size of all text, bottom grows as more text is added */
BigRect bounds;
@@ -128,7 +131,7 @@ struct TVRec {
Handle line_groups[32];
#define TV_LINE_GROUP_NLINES 500
#define TV_LINE_GROUP_SIZE (sizeof(struct TVLine) * TV_LINE_GROUP_NLINES)
- unsigned short nlines;
+ unsigned long nlines;
unsigned long sel_start;
unsigned long sel_end;