jcs
/textview
/amendments
/33
Implement TVResize
Track last space globally so we can move back beyond chunk boundaries
jcs made amendment 33 over 2 years ago
--- textview.c Mon Oct 21 23:23:07 2024
+++ textview.c Tue Oct 22 13:00:20 2024
@@ -45,6 +45,7 @@ typedef Boolean (*TVFindInRectCallback)(struct TVRec *
struct TVRec * TVLock(TVHandle tvh);
void TVUnlock(struct TVRec *);
+Boolean TVSetupCanvas(struct TVRec *tv);
struct TVSlab * TVAllocateTextSlab(struct TVRec *tv);
struct TVSlab * TVAllocateChunkSlab(struct TVRec *tv);
Handle TVAllocateLineGroup(struct TVRec *tv, short groupn);
@@ -52,7 +53,7 @@ struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsign
struct TVChunk * TVGetChunk(struct TVRec *tv, TVChunkPtr ptr);
struct TVChunk * TVCreateChunk(struct TVRec *tv);
struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line);
-Boolean TVCalcLinesLocked(struct TVRec *tv);
+Boolean TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty);
void TVApplyStyle(struct TVStyle *style);
struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style);
void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert);
@@ -85,44 +86,11 @@ TVNew(Rect *view, Rect *padding)
tv->padding = *padding;
tv->padding.left++;
- /*
- * TODO: make canvas 3 times vis portal height so we can pre-render
- * pages above and below to make scrolling seem faster?
- */
- width = tv->view.right - tv->view.left;
- height = tv->view.bottom - tv->view.top;
-
- GetPort(&old_port);
- /* opening the port will reset portRect/portBits */
- tv->canvas = (GrafPtr)NewPtr(sizeof(GrafPort));
- if (tv->canvas == NULL)
- goto fail;
- OpenPort(tv->canvas);
-
- tv->canvas->portRect.right = width;
- tv->canvas->portRect.bottom = height;
- tv->canvas->portBits.bounds = tv->canvas->portRect;
- tv->canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2;
- tv->canvas_size = (long)tv->canvas->portBits.rowBytes * height;
-
- tv->canvas_baseaddr = NewHandleClear(tv->canvas_size);
- if (tv->canvas_baseaddr == NULL)
- goto fail;
- HLock(tv->canvas_baseaddr);
- tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
-
- RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect);
- RectRgn(tv->canvas->visRgn, &tv->canvas->portRect);
-
- SetPort(old_port);
- HUnlock(tv->canvas_baseaddr);
-
- tv->view_height = height;
+ tv->cur_style = NO_STYLE;
- tv->bounds.right = width;
- tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
- tv->scrolled = tv->bounds;
-
+ if (!TVSetupCanvas(tv))
+ goto fail;
+
if (TVAllocateTextSlab(tv) == NULL)
goto fail;
@@ -131,8 +99,6 @@ TVNew(Rect *view, Rect *padding)
if (TVAllocateLineGroup(tv, 0) == NULL)
goto fail;
-
- tv->cur_style = NO_STYLE;
HUnlock(tvh);
return tvh;
@@ -234,7 +200,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
chunk->need_recalc = true;
}
- if (!TVCalcLinesLocked(tv))
+ if (!TVCalcLinesLocked(tv, false))
goto append_done;
/* keep scrolling and redrawing in one CopyBits */
@@ -281,12 +247,105 @@ TVCalcLines(TVHandle tvh)
Boolean ret;
tv = TVLock(tvh);
- ret = TVCalcLinesLocked(tv);
+ ret = TVCalcLinesLocked(tv, false);
TVUnlock(tv);
return ret;
}
+Boolean
+TVResize(TVHandle tvh)
+{
+ struct TVRec *tv;
+ long width, height, n;
+ struct TVLine *line;
+ BigRect visible;
+ short nline;
+ unsigned long top_start;
+
+ tv = TVLock(tvh);
+
+ /* clear old canvas */
+ TVUseCanvas(tv);
+ EraseRect(&tv->canvas->portRect);
+ TVRevealCanvas(tv);
+
+ /* find text position of first line to try to stay there */
+ top_start = 0;
+ for (nline = 1; nline <= tv->nlines; nline++) {
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ break;
+
+ if (tv->scrolled.top + line->rect.bottom < 0L)
+ /* is not in the view at all yet */
+ continue;
+
+ top_start = line->start;
+ break;
+ }
+
+ if (!TVSetupCanvas(tv)) {
+ TVUnlock(tv);
+ return false;
+ }
+
+ /* clear line groups except first one */
+ for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
+ n++) {
+ if (tv->line_groups[n] == NULL)
+ continue;
+ DisposHandle(tv->line_groups[n]);
+ tv->line_groups[n] = NULL;
+ }
+ HLock(tv->line_groups[0]);
+ memset((char *)*(tv->line_groups[0]), 0, TV_LINE_GROUP_SIZE);
+ HUnlock(tv->line_groups[0]);
+
+ tv->nlines = 0;
+ tv->last_space = 0;
+
+ if (!TVCalcLinesLocked(tv, true)) {
+ /* XXX: if this fails, what are we supposed to do? */
+ TVUnlock(tv);
+ return false;
+ }
+
+ if (tv->bounds.bottom > tv->view_height) {
+ if (tv->autoscroll) {
+ tv->scrolled.bottom = tv->view_height;
+ tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
+ } else {
+ for (nline = 1; nline <= tv->nlines; nline++) {
+ line = TVGetLine(tv, nline);
+ if (line == NULL)
+ break;
+
+ if (line->start >= top_start && line->end <= top_start) {
+ /* keep this line at the top */
+ tv->scrolled.top = -(line->rect.top);
+ tv->scrolled.bottom = tv->scrolled.top +
+ tv->bounds.bottom;
+ break;
+ }
+ }
+ }
+ }
+
+ TVUseCanvas(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, 1, TVDrawMatch, NULL);
+
+ TVRevealCanvas(tv);
+ TVUnlock(tv);
+
+ return true;
+}
+
void
TVAutoScroll(TVHandle tvh, Boolean scroll)
{
@@ -593,6 +652,8 @@ TVClear(TVHandle tvh)
HUnlock(tv->text_slabs[0].data);
tv->last_text_slab = 0;
tv->text_length = 0;
+ tv->nlines = 0;
+ tv->last_space = 0;
/* clear line groups except first one */
for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
@@ -703,6 +764,78 @@ TVUnlock(struct TVRec *tv)
HUnlock(RecoverHandle(tv));
}
+Boolean
+TVSetupCanvas(struct TVRec *tv)
+{
+ struct TVLine *line;
+ long width, height;
+ GrafPtr old_port, canvas = NULL;
+ Handle canvas_baseaddr = NULL;
+ unsigned long canvas_size;
+
+ /*
+ * TODO: make canvas 3 times vis portal height so we can pre-render
+ * pages above and below to make scrolling seem faster?
+ */
+ width = tv->view.right - tv->view.left;
+ height = tv->view.bottom - tv->view.top;
+
+ /* this must be a Ptr because it cannot move once opened */
+ canvas = (GrafPtr)NewPtrClear(sizeof(GrafPort));
+ if (canvas == NULL)
+ goto fail;
+
+ canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2;
+ canvas_size = (long)canvas->portBits.rowBytes * height;
+ canvas_baseaddr = NewHandleClear(canvas_size);
+ if (canvas_baseaddr == NULL)
+ goto fail;
+
+ /* everything allocated, commit to new canvas */
+
+ if (tv->canvas) {
+ ClosePort(tv->canvas);
+ DisposPtr(tv->canvas);
+ }
+ if (tv->canvas_baseaddr)
+ DisposHandle(tv->canvas_baseaddr);
+
+ tv->canvas = canvas;
+
+ /* opening the port resets portRect/portBits */
+ GetPort(&old_port);
+ OpenPort(tv->canvas);
+ RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect);
+ RectRgn(tv->canvas->visRgn, &tv->canvas->portRect);
+ SetPort(old_port);
+ HUnlock(tv->canvas_baseaddr);
+
+ tv->canvas->portRect.right = width;
+ tv->canvas->portRect.bottom = height;
+ tv->canvas->portBits.bounds = canvas->portRect;
+ tv->canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2;
+ tv->canvas_size = canvas_size;
+
+ tv->canvas_baseaddr = canvas_baseaddr;
+ 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:
+ if (canvas)
+ DisposPtr(canvas);
+ if (canvas_baseaddr)
+ DisposHandle(canvas_baseaddr);
+ return false;
+}
+
struct TVSlab *
TVAllocateTextSlab(struct TVRec *tv)
{
@@ -930,32 +1063,40 @@ TVFindStyle(struct TVRec *tv, struct TVStyle *style)
}
Boolean
-TVCalcLinesLocked(struct TVRec *tv)
+TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty)
{
struct TVSlab *text_slab;
struct TVChunk *chunk, *tchunk, *last_chunk, *prev_chunk, *next_chunk;
struct TVStyle *style;
struct TVLine *line, *prev_line, *next_line;
- unsigned long avail, start, nline, last_space, max_right;
+ unsigned long avail, start, nline, max_right;
long n, back_width, back_n;
short width, space_width, height;
char *text_slab_data;
unsigned char c;
Boolean new_line;
- /* find first dirty chunk */
- last_chunk = TVGetChunk(tv, tv->last_chunk);
- chunk = last_chunk;
- while (chunk != NULL) {
- prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
- if (!prev_chunk || !prev_chunk->need_recalc)
- break;
- chunk = prev_chunk;
+ if (ignore_dirty)
+ chunk = TVGetChunk(tv, tv->first_chunk);
+ else {
+ /* find first dirty chunk but search from the end */
+ last_chunk = TVGetChunk(tv, tv->last_chunk);
+ chunk = last_chunk;
+ while (chunk != NULL) {
+ prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ if (!prev_chunk || !prev_chunk->need_recalc)
+ break;
+ chunk = prev_chunk;
+ }
}
- max_right = tv->bounds.right - tv->padding.right;
+ 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;
+
+reload_chunk:
style = &tv->styles[chunk->style];
if (style == NULL)
return false;
@@ -1006,8 +1147,6 @@ TVCalcLinesLocked(struct TVRec *tv)
}
line->nline = nline;
- new_line = false;
- last_space = 0;
height = style->font_info.ascent + style->font_info.descent;
for (n = start; n < chunk->end; n++) {
@@ -1021,27 +1160,39 @@ TVCalcLinesLocked(struct TVRec *tv)
width = style->width_cache[c] = CharWidth(c);
if (c == ' ') {
- last_space = n;
+ tv->last_space = n;
space_width = 0;
- } else if (chunk->end_right + width >= max_right) {
+ } else if (!new_line &&
+ chunk->end_right + width >= max_right) {
new_line = true;
- if (last_space) {
+ if (tv->last_space) {
/* break after the last space */
- n = last_space + 1;
+ n = tv->last_space + 1;
chunk->end_right -= space_width;
- c = text_slab_data[n];
- } else if (chunk->start > line->start) {
- n = chunk->start;
- chunk->end_right = chunk->start_left;
- c = text_slab_data[n];
- } /* otherwise, hard-break this long word */
- }
+ line->end = tv->last_space + 1;
+ line->rect.right = chunk->end_right;
+
+ while (n < chunk->start) {
+ if (!chunk->prev_chunk) {
+ /* how did we get here? */
+ Debugger();
+ return false;
+ }
+ chunk = TVGetChunk(tv, chunk->prev_chunk);
+ }
+
+ /* setup chunk with new_line set */
+ goto reload_chunk;
+ }
+ /* otherwise, hard-break this long word */
+ } else
+ space_width += width;
}
if (new_line) {
new_line = false;
space_width = 0;
- last_space = 0;
+ tv->last_space = 0;
line->end = n;
line->rect.right = chunk->end_right;
@@ -1327,10 +1478,3 @@ TVRevealCanvas(struct TVRec *tv)
tv->canvas_dirty = false;
HUnlock(tv->canvas_baseaddr);
}
-
-#if 0
-void
-TVResize(struct TVRec *tv)
-{
-}
-#endif
\ No newline at end of file
--- textview.h Mon Oct 21 23:00:37 2024
+++ textview.h Tue Oct 22 11:49:07 2024
@@ -96,7 +96,7 @@ struct TVRec {
GrafPtr canvas;
Handle canvas_baseaddr;
short canvas_refcnt;
- size_t canvas_size;
+ unsigned long canvas_size;
/* while drawing on canvas, this stores old thePort */
GrafPtr canvas_old_port;
Boolean canvas_dirty;
@@ -111,6 +111,7 @@ struct TVRec {
#define TV_TEXT_SLAB_SIZE (16 * 1024)
unsigned char last_text_slab;
unsigned long text_length;
+ unsigned long last_space;
struct TVSlab chunk_slabs[64];
#define TV_CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50)
@@ -130,9 +131,10 @@ typedef struct TVRec ** TVHandle;
TVHandle TVNew(Rect *view, Rect *padding);
void TVDispose(TVHandle tvh);
-void TVAutoScroll(TVHandle tvh, Boolean scroll);
Boolean TVAppend(TVHandle tvh, struct TVStyle *style, char *text,
unsigned long length);
+void TVAutoScroll(TVHandle tvh, Boolean scroll);
+Boolean TVResize(TVHandle tvh);
void TVUpdate(TVHandle tvh);
void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller);
void TVClick(TVHandle tvh, Point p, Boolean extend);