AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

11

Lots of work on drawing and scrolling, it's pretty fast now


jcs made amendment 11 over 2 years ago
--- textview.c Thu Dec 28 16:14:29 2023 +++ textview.c Sun Oct 6 23:03:07 2024 @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + #include <stdio.h> #include <stddef.h> #include <string.h> @@ -13,14 +29,15 @@ struct TVChunk * TVSplitChunk(struct TVRec *tv, struct void TVApplyStyle(struct TVStyle *style); void TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off); -void TVBeginShadow(struct TVRec *tv); -void TVEndShadow(struct TVRec *tv); +void TVDrawOnCanvas(struct TVRec *tv); +void TVShowCanvas(struct TVRec *tv); struct TVRec * TVNew(Rect *view, Rect *padding) { struct TVRec *tv; unsigned long size; + long width, height; size = sizeof(struct TVRec) + sizeof(struct TVStyle); tv = malloc(CHUNK_ALLOC_SIZE); @@ -29,20 +46,33 @@ TVNew(Rect *view, Rect *padding) memset(tv, 0, sizeof(struct TVRec)); tv->view = *view; + tv->view.left++; + tv->view.top++; + tv->view.right -= 2; + tv->view.bottom -= 2; tv->padding = *padding; tv->padding.left++; - tv->scrollRect.right = tv->view.right - tv->view.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; - tv->shadow.bounds.right = tv->view.right - tv->view.left; - tv->shadow.bounds.bottom = tv->view.bottom - tv->view.top; - - tv->shadow.rowBytes = (((tv->shadow.bounds.right - 1) / 16) + 1) * 2; - tv->shadow.baseAddr = xmalloczero((size_t)tv->shadow.rowBytes * - (size_t)tv->shadow.bounds.bottom); - if (tv->shadow.baseAddr == NULL) + tv->view_height = height; + tv->canvas.bounds.right = width; + tv->canvas.bounds.bottom = height; + tv->canvas.rowBytes = (((width - 1) / 16) + 1) * 2; + tv->canvas_size = (long)tv->canvas.rowBytes * height; + tv->canvas.baseAddr = xmalloczero(tv->canvas_size); + if (tv->canvas.baseAddr == NULL) panic("malloc"); - + + tv->bounds.right = width; + tv->bounds.bottom = tv->padding.top + tv->padding.bottom; + tv->scrolled = tv->bounds; + /* create an initial blank chunk */ tv->chunks = (Ptr)tv + sizeof(struct TVRec); tv->cur_chunk = (struct TVChunk *)tv->chunks; @@ -52,8 +82,10 @@ TVNew(Rect *view, Rect *padding) tv->cur_chunk->line = 1; tv->cur_chunk->rect.top = tv->padding.top; tv->cur_chunk->rect.left = tv->padding.left; + tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top; + tv->cur_chunk->rect.right = tv->cur_chunk->rect.left; tv->last_chunk = tv->cur_chunk; - + return tv; } @@ -62,8 +94,8 @@ TVDispose(struct TVRec **tvp) { struct TVRec *tv = (struct TVRec *)*tvp; - if (tv->shadow.baseAddr) - xfree(&tv->shadow.baseAddr); + if (tv->canvas.baseAddr) + xfree(&tv->canvas.baseAddr); xfree(tvp); } @@ -105,6 +137,7 @@ TVCreateChunk(struct TVRec *tv, unsigned long length) struct TVChunk * TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length) { + Rect r; struct TVChunk *new_chunk, *prev_chunk, *next_chunk; short width; @@ -127,25 +160,30 @@ TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, tv->last_chunk = new_chunk; } - EraseRect(&chunk->rect); + BigRectToRect(chunk->rect, r); + r.top += tv->scrolled.top; + r.left += tv->scrolled.left; + EraseRect(&r); new_chunk->length = chunk->length - length; - memcpy(new_chunk->text, chunk->text + length, - chunk->length - length); + memcpy(new_chunk->text, chunk->text + length, chunk->length - length); chunk->length = length; TVApplyStyle(&chunk->style); width = TextWidth(chunk->text, 0, chunk->length); - chunk->rect.right -= width; - TVDrawText(tv, chunk, 0); + chunk->rect.right = chunk->rect.left + width; + if (width) + TVDrawText(tv, chunk, 0); + /* line us up next to the current chunk */ new_chunk->baseline = chunk->baseline; new_chunk->rect.top = chunk->rect.top; new_chunk->rect.bottom = chunk->rect.bottom; new_chunk->rect.left = chunk->rect.right; + new_chunk->rect.right = new_chunk->rect.left + + TextWidth(new_chunk->text, 0, new_chunk->length); - /* not drawn */ - + /* new chunk not drawn */ return new_chunk; } @@ -162,17 +200,21 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char unsigned long length) { struct TVChunk *chunk = NULL, *prev_chunk, *tchunk; - unsigned long n, line_right; - long j; - RgnHandle old_rgn; + Rect r; + long j, n, line_right; short width; char c; - Boolean new_chunk, new_line; + bool new_chunk, new_line; - TVBeginShadow(tv); + TVDrawOnCanvas(tv); TVApplyStyle(style); GetFontInfo(&style->font_info); + + if (memcmp(&tv->last_style, style, sizeof(struct TVStyle)) != 0) { + memset(tv->width_cache, 0, sizeof(tv->width_cache)); + tv->last_style = *style; + } line_right = tv->cur_chunk->rect.right; @@ -185,10 +227,11 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char new_chunk = true; new_line = true; } else { - width = TextWidth(text, n, 1); - + if (!(width = tv->width_cache[c])) + width = tv->width_cache[c] = CharWidth(c); + if (c != ' ' && width + line_right > - tv->scrollRect.right - tv->padding.right) { + tv->bounds.right - tv->padding.right) { new_chunk = true; new_line = true; @@ -198,8 +241,6 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char continue; tv->cur_chunk = TVSplitChunk(tv, tv->cur_chunk, j); - line_right -= (tv->cur_chunk->prev_chunk->rect.right - - tv->cur_chunk->prev_chunk->rect.left); new_chunk = false; break; } @@ -209,7 +250,8 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char * TODO: if no space in that chunk, keep checking * previous chunks in this line */ - panic("walk back to previous chunk to find space"); + /* XXX: just do a hard break and split with 0 length? */ + Debugger(); } TVApplyStyle(&tv->cur_chunk->style); @@ -224,7 +266,10 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char chunk->baseline = style->font_info.ascent; chunk->style = *style; GetFontInfo(&chunk->style.font_info); + chunk->rect.bottom = chunk->rect.top + chunk->baseline + + chunk->style.font_info.descent; + /* TVCreateChunk can return cur_chunk */ if (tv->cur_chunk != chunk) { if (n > 0) TVDrawText(tv, tv->cur_chunk, 0); @@ -234,16 +279,19 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char tv->cur_chunk->prev_chunk = prev_chunk; prev_chunk->next_chunk = tv->cur_chunk; tv->last_chunk = tv->cur_chunk; - + if (tv->cur_chunk->baseline > prev_chunk->baseline) { /* all chunks on this line must grow */ for (tchunk = prev_chunk; tchunk != NULL; tchunk = tchunk->prev_chunk) { if (tchunk->line != tv->cur_chunk->line) break; - - EraseRect(&tchunk->rect); + BigRectToRect(tchunk->rect, r); + r.top += tv->scrolled.top; + r.left += tv->scrolled.left; + EraseRect(&r); + tchunk->baseline = tv->cur_chunk->baseline; TVDrawText(tv, tchunk, 0); } @@ -257,11 +305,28 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char if (new_line) { tv->cur_chunk->line++; - tv->cur_chunk->rect.top = tv->cur_chunk->rect.bottom = + tv->cur_chunk->rect.top = tv->cur_chunk->prev_chunk->rect.bottom + tv->cur_chunk->prev_chunk->style.font_info.leading; + tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top + + tv->cur_chunk->baseline + + tv->cur_chunk->style.font_info.descent; + + tv->cur_chunk->rect.right -= tv->cur_chunk->rect.left - + tv->padding.left; tv->cur_chunk->rect.left = tv->padding.left; - tv->cur_chunk->rect.right = line_right; + line_right = tv->cur_chunk->rect.right; + + if (tv->cur_chunk->rect.bottom > + tv->bounds.bottom - tv->padding.bottom) { + tv->bounds.bottom = tv->cur_chunk->rect.bottom + + tv->padding.bottom; + tv->scrolled.bottom = tv->scrolled.top + + tv->bounds.bottom; + } + + if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) + TVScroll(tv, 0, -SHRT_MAX); } if (c == '\r') { @@ -272,61 +337,110 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char /* next line will need to know how tall our empty space was */ if (tv->cur_chunk->length == 0) { - tv->cur_chunk->rect.bottom = tv->cur_chunk->baseline + + tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top + + tv->cur_chunk->baseline + tv->cur_chunk->style.font_info.descent; - tv->scrollRect.bottom = tv->cur_chunk->rect.bottom; } + line_right = 0; } else { tv->cur_chunk->text[tv->cur_chunk->length++] = c; tv->cur_chunk->free_length--; line_right += width; + tv->cur_chunk->rect.right += width; } } - if (tv->cur_chunk->length) - TVDrawText(tv, tv->cur_chunk, 0); - - if (tv->cur_chunk->rect.bottom > - (tv->scrollRect.top + tv->scrollRect.bottom)) - tv->scrollRect.bottom = tv->cur_chunk->rect.bottom + - tv->scrollRect.top; + TVDrawText(tv, tv->cur_chunk, 0); + TVShowCanvas(tv); - TVEndShadow(tv); + if (tv->canvas_refcnt != 0) + Debugger(); } void +TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off) +{ + Point p; + long l, b; + Rect r; + + if (off != 0) + panic("need to implement redrawing a partial string"); + + if (chunk->length - off > 0) { + l = tv->scrolled.left + chunk->rect.left; + b = tv->scrolled.top + chunk->rect.top + chunk->baseline; + MoveTo(l, b); + TVApplyStyle(&chunk->style); + DrawText(chunk->text, off, chunk->length - off); + } + + if (tv->sel_start == chunk) { + r.left = l; + r.right = p.h; + r.top = b - chunk->baseline; + r.bottom = chunk->rect.bottom; + InvertRect(&r); + } +} + +void +TVAutoScroll(struct TVRec *tv, bool scroll) +{ + tv->autoscroll = scroll; +} + +void TVUpdateScrollbar(struct TVRec *tv, ControlHandle scroller) { + long max; struct TVChunk *first; RgnHandle rgn; - + HLock(scroller); - SetCtlMin(scroller, 0); - /* - * Avoid SetCtlMax because it will redraw and then SetCtlValue will + * Avoid SetCtlMin/Max because it will redraw and then SetCtlValue will * redraw again, which can cause a jump if we're trying to keep the * scrollbar position in the same place (like at the bottom). */ - (*scroller)->contrlMax = tv->scrollRect.bottom - tv->scrollRect.top - + (*scroller)->contrlMin = 0; + max = tv->bounds.bottom - tv->bounds.top - (tv->view.bottom - tv->view.top); + (*scroller)->contrlMax = (max > 0 ? max : 0); - SetCtlValue(scroller, -(tv->scrollRect.top)); + SetCtlValue(scroller, -(tv->scrolled.top)); rgn = NewRgn(); RectRgn(rgn, &(*scroller)->contrlRect); UpdtControl((*(scroller))->contrlOwner, rgn); - CloseRgn(rgn); DisposeRgn(rgn); HUnlock(scroller); } void -TVClick(struct TVRec *tv, Point p, Boolean extend) +TVClick(struct TVRec *tv, Point p, bool extend) { - + struct TVChunk *chunk; +return; + p.v -= tv->view.top; + p.h -= tv->view.left; + + p.v += -(tv->canvas.bounds.top); + p.h += -(tv->canvas.bounds.left); + + /* StillDown() then WaitMouseUp() */ + + for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; + chunk = chunk->next_chunk) { + if (p.v >= chunk->rect.top && p.v < chunk->rect.bottom && + p.h >= chunk->rect.left && p.h < chunk->rect.right) { + tv->sel_start = chunk; + TVUpdate(tv); + break; + } + } } static struct TVRec *tv_track_control = NULL; @@ -341,8 +455,8 @@ TVTrackScrollControl(ControlHandle control, short part { struct TVRec *tv; struct TVChunk *chunk, *top_chunk = NULL; - long n, height, top; - Boolean horiz; + long n, top; + bool horiz; if (tv_track_control == NULL) panic("TVTrackScrollControl without TVSetTrackScrollControl"); @@ -351,38 +465,36 @@ TVTrackScrollControl(ControlHandle control, short part horiz = (((*control)->contrlRect.bottom - (*control)->contrlRect.top) < ((*control)->contrlRect.right - (*control)->contrlRect.left)); - height = tv->view.bottom - tv->view.top - 2; - switch (part) { case inUpButton: /* scroll to the previous line that is not yet mostly-visible */ for (chunk = (struct TVChunk *)tv->last_chunk; chunk != NULL; chunk = chunk->prev_chunk) { - if (chunk->rect.top + (long)(chunk->baseline / 2) + - tv->scrollRect.top > 0) + if ((chunk->rect.top + (long)(chunk->baseline / 2) + + tv->scrolled.top) > 0) continue; if (!chunk->prev_chunk) break; - TVScroll(tv, 0, -(tv->scrollRect.top) - chunk->rect.top); + TVScroll(tv, 0, -(tv->scrolled.top) - chunk->rect.top); goto update_value; } /* nothing found, just scroll to the top */ - TVScroll(tv, 0, -(tv->scrollRect.top)); + TVScroll(tv, 0, -(tv->scrolled.top)); break; case inPageUp: /* scroll to put the top mostly-visible line at the bottom */ for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; chunk = chunk->next_chunk) { - if (chunk->rect.top + (long)(chunk->baseline / 2) + - tv->scrollRect.top < 0) + if ((chunk->rect.top + (long)(chunk->baseline / 2) + + tv->scrolled.top) < 0) continue; - top = tv->scrollRect.top + chunk->rect.top + height - + top = tv->scrolled.top + chunk->rect.top + tv->view_height - (chunk->rect.bottom - chunk->rect.top); - if (tv->scrollRect.top + top > 0) + if (tv->scrolled.top + top > 0) break; TVScroll(tv, 0, top); @@ -390,90 +502,98 @@ TVTrackScrollControl(ControlHandle control, short part } /* nothing found, just scroll to the top */ - TVScroll(tv, 0, -(tv->scrollRect.top)); + TVScroll(tv, 0, -(tv->scrolled.top)); break; case inDownButton: /* scroll to the line after the bottom-most line */ for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; chunk = chunk->next_chunk) { top = chunk->rect.top + (chunk->baseline / 2) + - tv->scrollRect.top; - if (top < height) + tv->scrolled.top; + if (top < tv->view_height) continue; - TVScroll(tv, 0, -((chunk->rect.bottom - height) + - tv->scrollRect.top)); + TVScroll(tv, 0, -((chunk->rect.bottom - tv->view_height) + + tv->scrolled.top)); goto update_value; } /* nothing found, just scroll to the bottom */ - TVScroll(tv, 0, height - tv->scrollRect.bottom); + TVScroll(tv, 0, tv->view_height - tv->scrolled.bottom); break; case inPageDown: /* scroll to put the bottom mostly-visible line at the top */ for (chunk = (struct TVChunk *)tv->last_chunk; chunk != NULL; chunk = chunk->prev_chunk) { if (chunk->rect.bottom - (long)(chunk->baseline / 2) + - tv->scrollRect.top > height) + tv->scrolled.top > tv->view_height) continue; - TVScroll(tv, 0, -(tv->scrollRect.top) - chunk->rect.top); + TVScroll(tv, 0, -(tv->scrolled.top) - chunk->rect.top); goto update_value; } /* nothing found, just scroll to the bottom */ - TVScroll(tv, 0, height - tv->scrollRect.bottom); + TVScroll(tv, 0, tv->view_height - tv->scrolled.bottom); break; } update_value: - SetCtlValue(control, -(tv->scrollRect.top)); + SetCtlValue(control, -(tv->scrolled.top)); } void -TVScroll(struct TVRec *tv, long horiz, long vert) +TVScroll(struct TVRec *tv, short horiz, short vert) { + short top, left; + BigRect rvacated; + Rect was, sect; + RgnHandle vacated; + struct TVChunk *chunk; + if (!horiz && !vert) return; - - tv->scrollRect.top += vert; - tv->scrollRect.bottom += vert; - tv->scrollRect.left += horiz; - tv->scrollRect.right += horiz; + TVDrawOnCanvas(tv); - /* TODO: move onscreen rect and only redraw new/partially obscured - * chunks */ - memset(tv->shadow.baseAddr, 0, (size_t)tv->shadow.rowBytes * - (size_t)tv->shadow.bounds.bottom); - TVUpdate(tv); -} - -void -TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off) -{ - Point p; - long l, b; + BigRectToRect(tv->scrolled, was); - if (off != 0) - panic("need to implement redrawing a partial string"); - - l = chunk->rect.left + tv->scrollRect.left; - b = chunk->rect.top + chunk->baseline + tv->scrollRect.top; + tv->scrolled.bottom += vert; + tv->scrolled.top += vert; + if (tv->scrolled.bottom < tv->view_height) { + /* can never scroll down lower than last line */ + tv->scrolled.bottom = tv->view_height; + tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; + } + if (tv->scrolled.top > 0) { + /* can never scroll up higher than first line */ + tv->scrolled.top = 0; + tv->scrolled.bottom = tv->bounds.bottom; + } - MoveTo(l, b); - TVApplyStyle(&chunk->style); - DrawText(chunk->text, off, chunk->length - off); + tv->scrolled.left += horiz; + tv->scrolled.right += horiz; + /* TODO: bound */ + + vacated = NewRgn(); + left = tv->scrolled.left - was.left; + top = tv->scrolled.top - was.top; + ScrollRect(&tv->canvas.bounds, left, top, vacated); + + rvacated = tv->scrolled; + rvacated.bottom = top; - GetPen(&p); - chunk->rect.right = p.h - tv->scrollRect.left; - chunk->rect.bottom = chunk->rect.top + chunk->baseline + - chunk->style.font_info.descent; - -#if 0 - TVEndShadow(tv); - TVBeginShadow(tv); -#endif + for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; + chunk = chunk->next_chunk) { + if (chunk->rect.top > rvacated.bottom) + continue; + if (chunk->rect.bottom < rvacated.top) + continue; + TVDrawText(tv, chunk, 0); + } + + DisposeRgn(vacated); + TVShowCanvas(tv); } void @@ -481,43 +601,51 @@ TVUpdate(struct TVRec *tv) { struct TVChunk *chunk; long height; - - TVBeginShadow(tv); - - height = tv->view.bottom - tv->view.top - 2; - + + TVDrawOnCanvas(tv); + for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; chunk = chunk->next_chunk) { - if (chunk->rect.bottom < -(tv->scrollRect.top)) + if (chunk->rect.bottom < -(tv->scrolled.top)) continue; - if (chunk->rect.top + tv->scrollRect.top > height) + if (chunk->rect.top + tv->scrolled.top > tv->view_height) break; TVDrawText(tv, chunk, 0); } - TVEndShadow(tv); + TVShowCanvas(tv); } - void -TVBeginShadow(struct TVRec *tv) +TVDrawOnCanvas(struct TVRec *tv) { - struct TVChunk *chunk; + Rect r; - tv->port_bits = thePort->portBits; - - SetPortBits(&tv->shadow); + if (++tv->canvas_refcnt != 1) + return; + GetClip(tv->port_region = NewRgn()); - ClipRect(&tv->shadow.bounds); + tv->canvas_cur_bits = thePort->portBits; + SetPortBits(&tv->canvas); + + ClipRect(&tv->canvas.bounds); } void -TVEndShadow(struct TVRec *tv) +TVShowCanvas(struct TVRec *tv) { + if (tv->canvas_refcnt <= 0) + Debugger(); + + if (--tv->canvas_refcnt != 0) + return; + + SetPortBits(&tv->canvas_cur_bits); SetClip(tv->port_region); DisposeRgn(tv->port_region); - SetPortBits(&tv->port_bits); - CopyBits(&tv->shadow, &tv->port_bits, &tv->shadow.bounds, &tv->view, - srcCopy, NULL); + CopyBits(&tv->canvas, &tv->canvas_cur_bits, &tv->canvas.bounds, + &tv->view, srcCopy, NULL); + + ValidRect(&tv->view); } --- textview.h Wed Dec 27 15:16:47 2023 +++ textview.h Sat Oct 5 22:53:11 2024 @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "util.h" + struct TVStyle { struct FontInfo font_info; short font; @@ -12,6 +30,12 @@ struct BigRect { long right; }; typedef struct BigRect BigRect; +#define BigRectToRect(br, r) SetRect(&(r), (br).left, (br).top, (br).right, (br).bottom) +#define RectToBigRect(r, br) { \ + (br).left = (r).left; \ + (br).top = (r).top; \ + (br).right = (r).right; \ + (br).bottom = (r).bottom; ) struct TVChunk { unsigned long length; @@ -19,6 +43,8 @@ struct TVChunk { struct TVChunk *next_chunk; unsigned long free_length; unsigned long line; + unsigned long sel_start; + unsigned long sel_finish; struct TVStyle style; unsigned long baseline; BigRect rect; @@ -26,25 +52,43 @@ struct TVChunk { }; struct TVRec { + /* bounds of where we live in window's port */ Rect view; + long view_height; + /* full size of all text, bottom grows as more text is added */ + BigRect bounds; + /* padding into bounds where we should start text */ Rect padding; - BitMap shadow; - BigRect scrollRect; - BitMap port_bits; + /* off-screen canvas where we draw text that is visible */ + BitMap canvas; + short canvas_refcnt; + /* bytes allocated for canvas backing */ + size_t canvas_size; + /* while drawing on canvas, this stores old thePort */ + BitMap canvas_cur_bits; RgnHandle port_region; + /* bounds as it is scrolled */ + BigRect scrolled; + + struct TVStyle last_style; + char width_cache[256]; + bool autoscroll; struct TVChunk *cur_chunk; struct TVChunk *last_chunk; + struct TVChunk *sel_start; + struct TVChunk *sel_finish; Ptr chunks; }; struct TVRec * TVNew(Rect *view, Rect *padding); void TVDispose(struct TVRec **tvp); +void TVAutoScroll(struct TVRec *tv, bool scroll); void TVAppend(struct TVRec *tv, struct TVStyle *style, char *text, unsigned long length); void TVUpdate(struct TVRec *tv); void TVUpdateScrollbar(struct TVRec *tv, ControlHandle scroller); -void TVClick(struct TVRec *tv, Point p, Boolean extend); -void TVScroll(struct TVRec *tv, long horiz, long vert); +void TVClick(struct TVRec *tv, Point p, bool extend); +void TVScroll(struct TVRec *tv, short horiz, short vert); void TVSetTrackScrollControl(struct TVRec *tv); pascal void TVTrackScrollControl(ControlHandle control, short part);