jcs
/textview
/amendments
/23
Better error propagation, shorter DrawText loop, fix canvas disappearing
THINK Reference has a note in the OpenPort documentation that says
to not use a Handle for the GrafPort because it could move addresses
between being opened and closed and get lost. Since we were putting
it in the main TVRec which is a Handle and could move, our canvas
could get lost, which is why it was disappearing under heavy memory
pressure and drawing seemed to have no effect.
jcs made amendment 23 over 2 years ago
--- textview.c Thu Oct 17 15:01:50 2024
+++ textview.c Thu Oct 17 17:52:01 2024
@@ -82,24 +82,25 @@ TVNew(Rect *view, Rect *padding)
GetPort(&old_port);
/* opening the port will reset portRect/portBits */
- OpenPort(&tv->canvas);
+ 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->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) {
- DisposHandle(tvh);
- return NULL;
- }
+ if (tv->canvas_baseaddr == NULL)
+ goto fail;
HLock(tv->canvas_baseaddr);
- tv->canvas.portBits.baseAddr = *(tv->canvas_baseaddr);
+ tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
- RectRgn(tv->canvas.clipRgn, &tv->canvas.portRect);
- RectRgn(tv->canvas.visRgn, &tv->canvas.portRect);
+ RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect);
+ RectRgn(tv->canvas->visRgn, &tv->canvas->portRect);
SetPort(old_port);
HUnlock(tv->canvas_baseaddr);
@@ -110,31 +111,33 @@ TVNew(Rect *view, Rect *padding)
tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
tv->scrolled = tv->bounds;
- if (TVAllocateTextSlab(tv) == NULL) {
- DisposHandle(tv->canvas_baseaddr);
- DisposHandle(tvh);
- return NULL;
- }
+ if (TVAllocateTextSlab(tv) == NULL)
+ goto fail;
- if (TVAllocateChunkSlab(tv) == NULL) {
- DisposHandle(tv->text_slabs[0].data);
- DisposHandle(tv->canvas_baseaddr);
- DisposHandle(tvh);
- return NULL;
- }
+ if (TVAllocateChunkSlab(tv) == NULL)
+ goto fail;
- if (TVAllocateLineGroup(tv, 0) == NULL) {
- DisposHandle(tv->chunk_slabs[0].data);
- DisposHandle(tv->text_slabs[0].data);
- DisposHandle(tv->canvas_baseaddr);
- DisposHandle(tvh);
- return NULL;
- }
+ if (TVAllocateLineGroup(tv, 0) == NULL)
+ goto fail;
tv->cur_style = NO_STYLE;
HUnlock(tvh);
return tvh;
+
+fail:
+ if (tv->chunk_slabs[0].data)
+ DisposHandle(tv->chunk_slabs[0].data);
+ if (tv->text_slabs[0].data)
+ DisposHandle(tv->text_slabs[0].data);
+ if (tv->canvas_baseaddr)
+ DisposHandle(tv->canvas_baseaddr);
+ if (tv->canvas) {
+ ClosePort(tv->canvas);
+ DisposPtr(tv->canvas);
+ }
+ DisposHandle(tvh);
+ return NULL;
}
bool
@@ -333,15 +336,19 @@ TVTrackScrollControl(ControlHandle control, short part
case inUpButton:
if (cmd_down) {
TVScrollUnlocked(tv, 0, 1);
- goto update_value;
+ break;
}
/* scroll to the line before the top-most line */
if (tv->top_line > 1) {
line = TVGetLine(tv, tv->top_line);
+ if (line == NULL)
+ break;
oline = TVGetLine(tv, tv->top_line - 1);
+ if (oline == NULL)
+ break;
TVScrollUnlocked(tv, 0, line->rect.top - oline->rect.top + 1);
- goto update_value;
+ break;
}
/* nothing found, just scroll to the top */
@@ -351,9 +358,11 @@ TVTrackScrollControl(ControlHandle control, short part
/* scroll to put the top mostly-visible line at the bottom */
if (tv->top_line) {
line = TVGetLine(tv, tv->top_line);
+ if (line == NULL)
+ break;
TVScrollUnlocked(tv, 0, tv->view_height -
(tv->scrolled.top + line->rect.bottom) - 1);
- goto update_value;
+ break;
}
/* nothing found, just scroll to the top */
@@ -362,7 +371,7 @@ TVTrackScrollControl(ControlHandle control, short part
case inDownButton:
if (cmd_down) {
TVScrollUnlocked(tv, 0, -1);
- goto update_value;
+ break;
}
/* scroll to the line after the bottom-most line */
@@ -370,7 +379,7 @@ TVTrackScrollControl(ControlHandle control, short part
line = TVGetLine(tv, tv->bottom_line + 1);
TVScrollUnlocked(tv, 0,
-(tv->scrolled.top - tv->view_height + line->rect.bottom));
- goto update_value;
+ break;
}
/* nothing found, just scroll to the bottom */
@@ -380,9 +389,11 @@ TVTrackScrollControl(ControlHandle control, short part
/* scroll to put the bottom mostly-visible line at the top */
if (tv->bottom_line < tv->nlines) {
line = TVGetLine(tv, tv->bottom_line + 1);
+ if (line == NULL)
+ break;
TVScrollUnlocked(tv, 0, -(line->rect.top + tv->scrolled.top) +
(line->rect.bottom - line->rect.top) + 1);
- goto update_value;
+ break;
}
/* nothing found, just scroll to the bottom */
@@ -390,7 +401,6 @@ TVTrackScrollControl(ControlHandle control, short part
break;
}
-update_value:
SetCtlValue(control, -(tv->scrolled.top));
TVUnlock(tv);
}
@@ -451,15 +461,16 @@ TVClear(TVHandle tvh)
tv->text_length = 0;
/* clear line groups except first one */
- for (n = 1; n < sizeof(tv->lines) / sizeof(tv->lines[0]); n++) {
- if (tv->lines[n] == NULL)
+ for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
+ n++) {
+ if (tv->line_groups[n] == NULL)
continue;
- DisposHandle(tv->lines[n]);
- tv->lines[n] = NULL;
+ DisposHandle(tv->line_groups[n]);
+ tv->line_groups[n] = NULL;
}
- HLock(tv->lines[0]);
- memset((char *)*(tv->lines[0]), 0, LINE_GROUP_SIZE);
- HUnlock(tv->lines[0]);
+ HLock(tv->line_groups[0]);
+ memset((char *)*(tv->line_groups[0]), 0, LINE_GROUP_SIZE);
+ HUnlock(tv->line_groups[0]);
tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
tv->scrolled = tv->bounds;
@@ -468,7 +479,7 @@ TVClear(TVHandle tvh)
tv->cur_style = NO_STYLE;
TVUseCanvas(tv);
- EraseRect(&tv->canvas.portRect);
+ EraseRect(&tv->canvas->portRect);
TVRevealCanvas(tv);
TVUnlock(tv);
}
@@ -496,7 +507,7 @@ TVDispose(TVHandle tvh)
DisposeHandle(tv->chunk_slabs[0].data);
DisposeHandle(tv->text_slabs[0].data);
- DisposeHandle(tv->lines[0]);
+ DisposeHandle(tv->line_groups[0]);
ClosePort(&tv->canvas);
DisposeHandle(tv->canvas_baseaddr);
@@ -517,9 +528,6 @@ TVLock(TVHandle tvh)
HLock(tvh);
tv = *tvh;
tv->unlock_refcnt++;
-
- HLock(tv->canvas_baseaddr);
- tv->canvas.portBits.baseAddr = *(tv->canvas_baseaddr);
return tv;
}
@@ -541,12 +549,11 @@ TVUnlock(struct TVRec *tv)
for (n = 0; n < sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0]); n++)
if (tv->chunk_slabs[n].data)
HUnlock(tv->chunk_slabs[n].data);
- for (n = 0; n < sizeof(tv->lines) / sizeof(tv->lines[0]); n++) {
- if (tv->lines[n])
- HUnlock(tv->lines[n]);
+ for (n = 0; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]); n++) {
+ if (tv->line_groups[n])
+ HUnlock(tv->line_groups[n]);
}
- HUnlock(tv->canvas_baseaddr);
HUnlock(RecoverHandle(tv));
}
@@ -625,17 +632,13 @@ TVAllocateLineGroup(struct TVRec *tv, short groupn)
{
short n;
- if (groupn > sizeof(tv->lines) / sizeof(tv->lines[0]))
+ if (groupn > sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))
return NULL;
- if (tv->lines[groupn] == NULL) {
- tv->lines[groupn] = NewHandleClear(LINE_GROUP_SIZE);
+ if (tv->line_groups[groupn] == NULL)
+ tv->line_groups[groupn] = NewHandleClear(LINE_GROUP_SIZE);
- if (tv->lines[groupn] == NULL)
- Debugger();
- }
-
- return tv->lines[groupn];
+ return tv->line_groups[groupn];
}
struct TVSlab *
@@ -696,6 +699,8 @@ TVCreateChunk(struct TVRec *tv)
return NULL;
}
+ HLock(slab->data);
+
if (tv->last_chunk_slab == 0 && slab->pos == 0 && !tv->first_chunk)
/* for first chunk, offset pos so it's not confused with NULL */
slab->pos = 2;
@@ -722,12 +727,12 @@ TVGetLine(struct TVRec *tv, unsigned long line)
unsigned short groupn, idx;
groupn = (line - 1) / LINE_GROUP_NLINES;
- if (groupn >= (sizeof(tv->lines) / sizeof(tv->lines[0])))
+ if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0])))
return NULL;
idx = line - 1 - (groupn * LINE_GROUP_NLINES);
- group = tv->lines[groupn];
+ group = tv->line_groups[groupn];
if (group == NULL) {
if ((group = TVAllocateLineGroup(tv, groupn)) == NULL)
return NULL;
@@ -735,6 +740,7 @@ TVGetLine(struct TVRec *tv, unsigned long line)
HLock(group);
/* keep group locked */
+
return (struct TVLine *)(*group + (sizeof(struct TVLine) * idx));
}
@@ -823,6 +829,8 @@ TVCalcLines(struct TVRec *tv)
chunk->end_line = 1;
} else {
prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ if (prev_chunk == NULL)
+ return false;
chunk->start_left = prev_chunk->end_right;
chunk->start_line = prev_chunk->end_line;
chunk->end_right = chunk->start_left;
@@ -832,10 +840,14 @@ TVCalcLines(struct TVRec *tv)
/* setup text slab */
text_slab = TVGetTextSlab(tv, chunk->start);
+ if (text_slab == NULL)
+ return false;
text_slab_data = *(text_slab->data);
linen = chunk->end_line;
line = TVGetLine(tv, linen);
+ if (line == NULL)
+ return false;
if (linen == 1) {
line->rect.left = tv->padding.left;
line->rect.top = tv->padding.top;
@@ -843,6 +855,8 @@ TVCalcLines(struct TVRec *tv)
start = line->end;
} else {
prev_line = TVGetLine(tv, linen - 1);
+ if (prev_line == NULL)
+ return false;
line->rect.top = prev_line->rect.bottom;
if (line->end)
start = line->end;
@@ -897,10 +911,13 @@ TVCalcLines(struct TVRec *tv)
chunk->end_line = linen;
chunk->end_right = tv->padding.left;
next_line = TVGetLine(tv, linen);
+ if (next_line == NULL)
+ return false;
next_line->start = n;
if (c == '\r')
next_line->start++;
- next_line->rect.top = line->rect.bottom;// - 1;
+ next_line->rect.top = line->rect.bottom +
+ style->font_info.leading;
next_line->rect.left = tv->padding.left;
next_line->rect.right = chunk->end_right;
next_line->rect.bottom = next_line->rect.top;
@@ -926,14 +943,17 @@ TVCalcLines(struct TVRec *tv)
done_chunks:
tv->nlines = linen;
+
line = TVGetLine(tv, linen);
-
+ if (line == NULL)
+ return false;
+
if (line->rect.bottom > tv->bounds.bottom - tv->padding.bottom) {
tv->bounds.bottom = line->rect.bottom + tv->padding.bottom;
tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
}
- if (0 && tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) {
+ if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) {
tv->scrolled.bottom = tv->view_height;
tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
}
@@ -950,109 +970,85 @@ TVDrawVisibleLines(struct TVRec *tv)
struct TVLine *line, *next_line;
Rect r;
char c, *text_slab_data;
- unsigned long bottom, start, end, textlen;
+ unsigned long bottom, start, end, len;
unsigned short chars, linen;
- long top, left;
+ long left;
TVUseCanvas(tv);
- EraseRect(&tv->canvas.portRect);
+ EraseRect(&tv->canvas->portRect);
- chunk = TVGetChunk(tv, tv->first_chunk);
- text_slab = TVGetTextSlab(tv, chunk->start);
- text_slab_data = *(text_slab->data);
-
tv->top_line = 0;
tv->bottom_line = 0;
+ chunk = NULL;
for (linen = 1; linen <= tv->nlines; linen++) {
line = TVGetLine(tv, linen);
+ if (line == NULL)
+ return false;
if (tv->scrolled.top + line->rect.bottom < 0L)
/* is not in the view at all yet */
continue;
+
if (tv->scrolled.top + line->rect.top >= tv->view_height)
- /* is further down than the view */
+ /* is further down than the view, we've done enough lines */
break;
- while (chunk && chunk->end_line < linen) {
- chunk = TVGetChunk(tv, chunk->next_chunk);
- if (!chunk)
- 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)
+ return false;
+ } else {
+ while (chunk->end_line < linen) {
+ chunk = TVGetChunk(tv, chunk->next_chunk);
+ if (!chunk)
+ return false;
+ }
+ }
+
text_slab = TVGetTextSlab(tv, chunk->start);
+ if (text_slab == NULL)
+ return false;
text_slab_data = *(text_slab->data);
- }
- while (chunk != NULL) {
style = &tv->styles[chunk->style];
if (style == NULL)
return false;
TVApplyStyle(style);
+ }
- if (line->start > chunk->start) {
- start = line->start;
- left = line->rect.left;
- } else {
- start = chunk->start;
- left = chunk->start_left;
- }
-
- if (line->end < chunk->end)
- end = line->end;
- else
- end = chunk->end;
+ if (line->start > chunk->start) {
+ start = line->start;
+ left = line->rect.left;
+ } else {
+ start = chunk->start;
+ left = 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->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 ((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)
+ continue;
- top = tv->scrolled.top + line->rect.bottom - line->baseline;
- MoveTo(left, top);
-
-#ifdef DEBUG_DRAWING
- {
- Rect r;
- struct TVStyle ls;
- char l[10];
- sprintf(l, "%d", linen);
- ls.font = geneva;
- ls.size = 10;
- ls.style = 0;
- TVApplyStyle(&ls);
- DrawText(l, 0, strlen(l));
- TVApplyStyle(style);
- MoveTo(left + 20, top);
- }
-#endif
- DrawText(text_slab_data + (start - text_slab->base), 0,
- end - start);
-
- if (end < chunk->end)
- /* span to next line */
- break;
-
- /* otherwise load next chunk for this line */
- chunk = TVGetChunk(tv, chunk->next_chunk);
- if (!chunk)
- break;
- text_slab = TVGetTextSlab(tv, chunk->start);
- text_slab_data = *(text_slab->data);
- }
+ MoveTo(left, tv->scrolled.top + line->rect.bottom - line->baseline);
+ DrawText(text_slab_data + (start - text_slab->base), 0,
+ end - start);
}
-#if 0
- if (!(chars == 1 && start[0] == '\r') &&
- tv->scrolled.top + line->rect.bottom >= 0 &&
- tv->scrolled.top + line->rect.top <= tv->view_height) {
- MoveTo(chunk->start_left,
- tv->scrolled.top + line->rect.bottom - line->baseline);
- DrawText(start, 0, chars);
- }
-#endif
TVRevealCanvas(tv);
return true;
@@ -1117,7 +1113,11 @@ TVUseCanvas(struct TVRec *tv)
#endif
GetPort(&tv->canvas_old_port);
- SetPort(&tv->canvas);
+
+ HLock(tv->canvas_baseaddr);
+ tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr);
+
+ SetPort(tv->canvas);
}
void
@@ -1136,28 +1136,18 @@ TVRevealCanvas(struct TVRec *tv)
SetPort(tv->canvas_old_port);
- CopyBits(&tv->canvas.portBits, &tv->canvas_old_port->portBits,
- &tv->canvas.portRect, &tv->view, srcCopy, NULL);
+ CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits,
+ &tv->canvas->portRect, &tv->view, srcCopy, NULL);
ValidRect(&tv->view);
tv->canvas_old_port = NULL;
+ HUnlock(tv->canvas_baseaddr);
}
#if 0
void
TVResize(struct TVRec *tv)
{
-#if 0
- for (n = 0; n < sizeof(tv->lines) / sizeof(tv->lines[0]); n++) {
- if (tv->lines[n]) {
- HLock(tv->lines[n]);
- memset((char *)*(tv->lines[n]), 0, LINE_GROUP_SIZE);
- }
- }
-
- tv->start_line = 1;
- tv->end_line = 1;
-#endif
}
#endif
\ No newline at end of file
--- textview.h Thu Oct 17 10:17:51 2024
+++ textview.h Thu Oct 17 17:42:52 2024
@@ -90,7 +90,7 @@ struct TVRec {
/* padding into bounds where we should start text */
Rect padding;
/* off-screen canvas where we draw text that is visible */
- GrafPort canvas;
+ GrafPtr canvas;
Handle canvas_baseaddr;
short canvas_refcnt;
size_t canvas_size;
@@ -111,8 +111,7 @@ struct TVRec {
TVChunkPtr first_chunk;
TVChunkPtr last_chunk;
- /* each lines item holds 1000 TVLine records */
- Handle lines[16];
+ Handle line_groups[16];
unsigned short nlines;
unsigned short top_line;
unsigned short bottom_line;