AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

5

textview: Get up and down scrolling working properly

Instead of passing dest and view rects, pass view rect and padding

jcs made amendment 5 over 2 years ago
--- textview.c Mon Dec 11 11:15:29 2023 +++ textview.c Sat Dec 16 23:23:20 2023 @@ -7,16 +7,16 @@ #define CHUNK_ALLOC_SIZE (16 * 1024) #define IS_FIRST_EMPTY_CHUNK(tv) ((tv)->cur_chunk->style.size == 0) -struct TVChunk * _TVCreateChunk(struct TVRec *tv, unsigned long length); -void _TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk); -struct TVChunk * _TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, +struct TVChunk * TVCreateChunk(struct TVRec *tv, unsigned long length); +void TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk); +struct TVChunk * TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length); void TVApplyStyle(struct TVStyle *style); -void _TVDrawText(struct TVRec *tv, struct TVChunk *chunk, - unsigned long off, bool append); +void TVDrawText(struct TVRec *tv, struct TVChunk *chunk, + unsigned long off); struct TVRec * -TVNew(Rect *destRect, Rect *viewRect) +TVNew(Rect *view, Rect *padding) { struct TVRec *tv; unsigned long size; @@ -27,9 +27,13 @@ TVNew(Rect *destRect, Rect *viewRect) panic("malloc(%ul) failed", (unsigned long)CHUNK_ALLOC_SIZE); memset(tv, 0, sizeof(struct TVRec)); - tv->destRect = *destRect; - tv->viewRect = *viewRect; + tv->view = *view; + tv->padding = *padding; + tv->padding.left++; + tv->scrollRect.right = tv->view.right - tv->view.left - + tv->padding.left - tv->padding.right; + /* create an initial blank chunk */ tv->chunks = (Ptr)tv + sizeof(struct TVRec); tv->cur_chunk = (struct TVChunk *)tv->chunks; @@ -37,16 +41,13 @@ TVNew(Rect *destRect, Rect *viewRect) tv->cur_chunk->free_length = CHUNK_ALLOC_SIZE - size; tv->cur_chunk->line = 1; - tv->cur_chunk->rect.top = tv->cur_chunk->rect.bottom = tv->destRect.top; - tv->cur_chunk->rect.left = tv->cur_chunk->rect.right = tv->destRect.left; - tv->pen.v = tv->destRect.top; - tv->pen.h = tv->destRect.left; + tv->last_chunk = tv->cur_chunk; return tv; } struct TVChunk * -_TVCreateChunk(struct TVRec *tv, unsigned long length) +TVCreateChunk(struct TVRec *tv, unsigned long length) { struct TVChunk *chunk; unsigned long addr; @@ -80,18 +81,18 @@ _TVCreateChunk(struct TVRec *tv, unsigned long length) } struct TVChunk * -_TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length) +TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length) { struct TVChunk *new_chunk, *prev_chunk, *next_chunk; short width; /* * This leaks some space by not putting the new chunk right after - * chunk->text[pos], but it avoids having to create a temporary buffer - * to copy chunk->text[pos .. length] to just to move it to - * new_chunk->text. + * chunk->text[length], but it avoids having to create a temporary + * buffer to copy chunk->text[length .. total length] to just to move + * it to new_chunk->text. */ - new_chunk = _TVCreateChunk(tv, chunk->length - length); + new_chunk = TVCreateChunk(tv, chunk->length - length); new_chunk->baseline = chunk->baseline; new_chunk->style = chunk->style; @@ -99,9 +100,12 @@ _TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, if (chunk->next_chunk) { chunk->next_chunk->prev_chunk = new_chunk; chunk->next_chunk = new_chunk; + } else { + chunk->next_chunk = new_chunk; + tv->last_chunk = new_chunk; } - _TVClearChunkRect(tv, chunk); + TVClearChunkRect(tv, chunk); new_chunk->length = chunk->length - length; memcpy(new_chunk->text, chunk->text + length, @@ -111,7 +115,7 @@ _TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, TVApplyStyle(&chunk->style); width = TextWidth(chunk->text, 0, chunk->length); chunk->rect.right -= width; - _TVDrawText(tv, chunk, 0, false); + TVDrawText(tv, chunk, 0); new_chunk->baseline = chunk->baseline; new_chunk->rect.top = chunk->rect.top; @@ -136,19 +140,21 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char unsigned long length) { struct TVChunk *chunk = NULL, *prev_chunk, *tchunk; - unsigned long n, free_length, line_right; - long j, last_break, carry_chars; - Point p; + unsigned long n, line_right; + long j; + RgnHandle old_rgn; short width; char c; - bool new_chunk, new_line; + Boolean new_chunk, new_line; + GetClip(old_rgn = NewRgn()); + ClipRect(&tv->view); + TVApplyStyle(style); GetFontInfo(&style->font_info); line_right = tv->cur_chunk->rect.right; - carry_chars = 0; for (n = 0; n < length; n++) { c = text[n]; new_chunk = false; @@ -160,17 +166,16 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char } else { width = TextWidth(text, n, 1); - if (width + line_right > tv->destRect.right && c != ' ') { + if (width + line_right > tv->scrollRect.right && c != ' ') { new_chunk = true; new_line = true; /* walk back to last space and break after it */ - last_break = 0; for (j = tv->cur_chunk->length; j > 0; j--) { if (tv->cur_chunk->text[j - 1] != ' ') continue; - tv->cur_chunk = _TVSplitChunk(tv, tv->cur_chunk, j); + 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; @@ -193,19 +198,20 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char } if (new_chunk) { - chunk = _TVCreateChunk(tv, length - n); + chunk = TVCreateChunk(tv, length - n); chunk->baseline = style->font_info.ascent; chunk->style = *style; GetFontInfo(&chunk->style.font_info); if (tv->cur_chunk != chunk) { if (n > 0) - _TVDrawText(tv, tv->cur_chunk, 0, false); + TVDrawText(tv, tv->cur_chunk, 0); prev_chunk = tv->cur_chunk; tv->cur_chunk = chunk; 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 */ @@ -214,10 +220,10 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char if (tchunk->line != tv->cur_chunk->line) break; - _TVClearChunkRect(tv, tchunk); + TVClearChunkRect(tv, tchunk); tchunk->baseline = tv->cur_chunk->baseline; - _TVDrawText(tv, tchunk, 0, false); + TVDrawText(tv, tchunk, 0); } } else if (tv->cur_chunk->baseline < prev_chunk->baseline) tv->cur_chunk->baseline = @@ -232,7 +238,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char tv->cur_chunk->rect.top = tv->cur_chunk->rect.bottom = tv->cur_chunk->prev_chunk->rect.bottom + tv->cur_chunk->prev_chunk->style.font_info.leading; - tv->cur_chunk->rect.left = tv->destRect.left; + tv->cur_chunk->rect.left = 0; tv->cur_chunk->rect.right = line_right; } @@ -243,64 +249,237 @@ 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) + if (tv->cur_chunk->length == 0) { tv->cur_chunk->rect.bottom = tv->cur_chunk->baseline + tv->cur_chunk->style.font_info.descent; + tv->scrollRect.bottom = tv->cur_chunk->rect.bottom; + } } else { tv->cur_chunk->text[tv->cur_chunk->length++] = c; tv->cur_chunk->free_length--; line_right += width; -//_TVDrawText(tv, tv->cur_chunk, 0, false); } } if (tv->cur_chunk->length) - _TVDrawText(tv, tv->cur_chunk, 0, false); + 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; + + SetClip(old_rgn); + DisposeRgn(old_rgn); } void -_TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off, - bool append) +TVUpdateScrollbar(struct TVRec *tv, ControlHandle scroller) { + struct TVChunk *first; + RgnHandle rgn; + + HLock(scroller); + + SetCtlMin(scroller, 0); + + /* + * Avoid SetCtlMax 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 + + tv->padding.bottom - (tv->view.bottom - tv->view.top); + + SetCtlValue(scroller, -(tv->scrollRect.top)); + +#if 1 + rgn = NewRgn(); + RectRgn(rgn, &(*scroller)->contrlRect); + UpdtControl((*(scroller))->contrlOwner, rgn); + CloseRgn(rgn); + DisposeRgn(rgn); +#endif + + HUnlock(scroller); +} + +void +TVClick(struct TVRec *tv, Point p, Boolean extend) +{ + +} + +static struct TVRec *tv_track_control = NULL; +void +TVSetTrackScrollControl(struct TVRec *tv) +{ + tv_track_control = tv; +} + +pascal void +TVTrackScrollControl(ControlHandle control, short part) +{ + struct TVRec *tv; + struct TVChunk *chunk, *top_chunk = NULL; + BigRect visRect; + short page, val, adj, per_line; + long n, visible, height; + Boolean horiz; + + if (tv_track_control == NULL) + panic("TVTrackScrollControl without TVSetTrackScrollControl"); + tv = tv_track_control; + + horiz = (((*control)->contrlRect.bottom - (*control)->contrlRect.top) < + ((*control)->contrlRect.right - (*control)->contrlRect.left)); + + adj = 0; + switch (part) { + case inUpButton: + for (chunk = (struct TVChunk *)tv->last_chunk; chunk != NULL; + chunk = chunk->prev_chunk) { + if (chunk->rect.bottom - ((chunk->rect.bottom - + chunk->rect.top) / 2) + tv->scrollRect.top > 0) + /* already visible */ + continue; + + if (chunk->prev_chunk) + /* scroll to make this line entirely visible at the top */ + TVScroll(tv, 0, tv->scrollRect.top + chunk->rect.top + + tv->padding.top); + else + /* close enough, just scroll to the top */ + TVScroll(tv, 0, tv->scrollRect.top); + goto update_value; + } + + /* nothing found, just scroll to the top */ + TVScroll(tv, 0, tv->scrollRect.top); + break; + case inPageUp: + adj = -page; + break; + case inDownButton: + height = tv->view.top + tv->padding.top + tv->scrollRect.top; + + for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; + chunk = chunk->next_chunk) { + if (height + chunk->rect.top + + ((chunk->rect.bottom - chunk->rect.top) / 2) < + tv->view.bottom - 2) + /* already visible */ + continue; + + /* scroll to make this line entirely visible at the bottom */ + TVScroll(tv, 0, tv->scrollRect.top + chunk->rect.bottom - + (tv->view.bottom - tv->view.top - 2 - tv->padding.top)); + break; + } + break; + case inPageDown: + adj = page; + break; + } + +update_value: + SetCtlValue(control, -(tv->scrollRect.top)); + +#if 0 + val = GetCtlValue(control); + if (val + adj < GetCtlMin(control)) + adj = -(val - GetCtlMin(control)); + if (val + adj > GetCtlMax(control)) + adj = (GetCtlMax(control) - val); + if (adj == 0) + return; + + if (horiz) + TEScroll(-adj * fwidth, 0, track_control_te); + else + TEScroll(0, -adj * fheight, track_control_te); + + SetCtlValue(control, val + adj); +#endif +} + +void +TVScroll(struct TVRec *tv, long horiz, long vert) +{ + tv->scrollRect.top -= vert; + tv->scrollRect.bottom -= vert; + + tv->scrollRect.left -= horiz; + tv->scrollRect.right -= horiz; + + /* TODO: move onscreen rect and only redraw new/partially obscured + * chunks */ + TVUpdate(tv); +} + +void +TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off) +{ Point p; - short l, b; + long l, b; + l = tv->view.left + tv->padding.left + tv->scrollRect.left; + if (off == 0) - l = chunk->rect.left; - else if (append) - l = chunk->rect.right; + l += chunk->rect.left; else panic("need to implement redrawing a partial string"); - b = chunk->rect.top + chunk->baseline; + b = tv->view.top + tv->padding.top + tv->scrollRect.top + + chunk->rect.top + chunk->baseline; MoveTo(l, b); TVApplyStyle(&chunk->style); DrawText(chunk->text, off, chunk->length - off); GetPen(&p); - chunk->rect.right = p.h; - chunk->rect.bottom = p.v + chunk->style.font_info.descent; -#if 1 + chunk->rect.right = p.h - (tv->view.left + tv->padding.left + + tv->scrollRect.left); + chunk->rect.bottom = chunk->rect.top + chunk->baseline + + chunk->style.font_info.descent; +#if 0 FrameRect(&chunk->rect); #endif } void -_TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk) +TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk) { - EraseRect(&chunk->rect); + Rect r; + long o; + + o = tv->view.top + tv->scrollRect.top; + r.top = o + chunk->rect.top; + r.bottom = o + chunk->rect.bottom; + + o = tv->view.left + tv->scrollRect.left; + r.left = chunk->rect.left + o; + r.right = chunk->rect.right + o; + + EraseRect(&r); } void -TVUpdate(Rect *rect, struct TVRec *tv) +TVUpdate(struct TVRec *tv) { struct TVChunk *chunk; + RgnHandle old_rgn; + + EraseRect(&tv->view); - /* TODO: constrain to rect */ + GetClip(old_rgn = NewRgn()); + ClipRect(&tv->view); for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL; chunk = chunk->next_chunk) { - _TVDrawText(tv, chunk, 0, false); + TVDrawText(tv, chunk, 0); } + + SetClip(old_rgn); + DisposeRgn(old_rgn); } --- textview.h Fri Dec 8 19:57:24 2023 +++ textview.h Sat Dec 16 16:00:51 2023 @@ -5,6 +5,14 @@ struct TVStyle { unsigned char size; }; +struct BigRect { + long top; + long left; + long bottom; + long right; +}; +typedef struct BigRect BigRect; + struct TVChunk { unsigned long length; struct TVChunk *prev_chunk; @@ -13,19 +21,27 @@ struct TVChunk { unsigned long line; struct TVStyle style; unsigned long baseline; - Rect rect; + BigRect rect; char text[]; }; struct TVRec { - Rect destRect; - Rect viewRect; - Point pen; + Rect view; + Rect padding; + BigRect scrollRect; + RgnHandle region; struct TVChunk *cur_chunk; + struct TVChunk *last_chunk; Ptr chunks; }; -struct TVRec * TVNew(Rect *destRect, Rect *viewRect); +struct TVRec * TVNew(Rect *view, Rect *padding); void TVAppend(struct TVRec *tv, struct TVStyle *style, char *text, unsigned long length); -void TVUpdate(Rect *rect, struct TVRec *tv); +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 TVSetTrackScrollControl(struct TVRec *tv); +pascal void TVTrackScrollControl(ControlHandle control, short part);