AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

6

main: Add scrollbars


jcs made amendment 6 over 2 years ago
--- main.c Wed Dec 6 16:17:33 2023 +++ main.c Fri Dec 15 13:26:43 2023 @@ -4,6 +4,8 @@ #include "textview.h" #include "util.h" +#define SCROLLBAR_WIDTH 16 + int main(void) { @@ -12,13 +14,14 @@ main(void) WindowPtr event_win; GrafPtr old_port; short event_in, n, finder_action, finder_count; - short quitting = 0; + bool quitting = false; char key; TEHandle te; TextStyle testyle; + ControlHandle te_scroller, tv_scroller; WindowPtr win; - Rect bounds, terect; + Rect bounds; long size, pos, count, linewidth, tvwidth, tvy, bufoff; short err, fn, width, wcount; char buf[512]; @@ -26,7 +29,7 @@ main(void) struct TVRec *tv; struct TVStyle style; - Rect dest, view; + Rect dest, view, padding, r; InitGraf(&thePort); InitFonts(); @@ -50,14 +53,13 @@ main(void) SetPort(win); { - dest.top = 10; - dest.left = 10; + dest.top = 5; + dest.left = 5; dest.bottom = ((win->portRect.bottom - win->portRect.top) / 2) - 5; - dest.right = win->portRect.right - 10; + dest.right = win->portRect.right - SCROLLBAR_WIDTH - 5; FrameRect(&dest); InsetRect(&dest, 1, 1); view = dest; - terect = dest; InsetRect(&dest, 5, 5); te = TEStylNew(&dest, &view); @@ -67,25 +69,41 @@ main(void) testyle.tsSize = 9; TESetStyle(doFont | doSize, &testyle, false, te); TEAutoView(true, te); + + bounds.left = view.right; + bounds.right = bounds.left + SCROLLBAR_WIDTH; + bounds.bottom = view.bottom + 1; + bounds.top = view.top - 1; + te_scroller = NewControl(win, &bounds, "\p", true, 1, 1, 1, + scrollBarProc, 0L); } { - dest.top = ((win->portRect.bottom - win->portRect.top) / 2) + 5; - dest.left = 10; - dest.bottom = win->portRect.bottom - 10; - dest.right = win->portRect.right - 10; - FrameRect(&dest); - InsetRect(&dest, 1, 1); - view = dest; - InsetRect(&dest, 5, 5); + view.top = ((win->portRect.bottom - win->portRect.top) / 2); + view.left = 5; + view.bottom = win->portRect.bottom - 10; + view.right = win->portRect.right - SCROLLBAR_WIDTH - 5; + FrameRect(&view); + InsetRect(&view, 1, 1); - tv = TVNew(&dest, &view); + padding.top = 5; + padding.left = 5; + padding.right = 5; + padding.bottom = 5; + tv = TVNew(&view, &padding); if (!tv) panic("TVNew failed"); style.font = monaco; style.size = 9; style.style = 0; + + bounds.left = view.right; + bounds.right = bounds.left + SCROLLBAR_WIDTH; + bounds.bottom = view.bottom + 1; + bounds.top = view.top - 1; + tv_scroller = NewControl(win, &bounds, "\p", true, 1, 1, 1, + scrollBarProc, 0L); } err = FSOpen("\pMacintosh:sources:playground:lorem.txt", 0, &fn); @@ -94,9 +112,9 @@ main(void) GetEOF(fn, &size); -if (0) { +if (1) { for (pos = 0; pos < size; ) { - count = sizeof(buf) / 2; + count = sizeof(buf); err = FSRead(fn, &count, buf); if (err != 0 && err != eofErr) panic("fsread %d", err); @@ -115,17 +133,16 @@ if (0) { testyle.tsFont = geneva; testyle.tsSize = 24; TESetStyle(doFont | doSize, &testyle, false, te); - TEStylInsert("hellothere", 10, 0, te); + TEStylInsert("Wello, there", 12, 0, te); style.font = geneva; style.size = 24; style.style = 0; - TVAppend(tv, &style, "hellothere", 10); + TVAppend(tv, &style, "Wello, there", 12); - TEStylInsert(" this is a wrapped line", 23, 0, te); - TVAppend(tv, &style, " this is a wrapped line", 23); + TEStylInsert(" this is a wrapped line", 26, 0, te); + TVAppend(tv, &style, " this is a wrapped line", 26); -#if 0 TEStylInsert(" t", 2, 0, te); TVAppend(tv, &style, " t", 2); @@ -135,42 +152,23 @@ if (0) { TEStylInsert("i", 1, 0, te); TVAppend(tv, &style, "i", 1); - TEStylInsert("s", 1, 0, te); - TVAppend(tv, &style, "s", 1); - - EraseRect(&terect); - - TEStylInsert("i", 1, 0, te); - TVAppend(tv, &style, "i", 1); - - testyle.tsSize = 36; + TEStylInsert("s. ", 3, 0, te); + TVAppend(tv, &style, "s. ", 3); + + testyle.tsFont = geneva; + testyle.tsSize = 18; TESetStyle(doFont | doSize, &testyle, false, te); - TEStylInsert("s", 1, 0, te); + TEStylInsert("This is some longer text that will scroll.", 42, 0, te); - style.size = 36; - TVAppend(tv, &style, "s", 1); - - TEStylInsert("a", 1, 0, te); - TVAppend(tv, &style, "a", 1); - - testyle.tsSize = 24; - TESetStyle(doFont | doSize, &testyle, false, te); - TEStylInsert("t", 1, 0, te); - - style.size = 24; - TVAppend(tv, &style, "t", 1); - - TEStylInsert("e", 1, 0, te); - TVAppend(tv, &style, "e", 1); - - TEStylInsert("s", 1, 0, te); - TVAppend(tv, &style, "s", 1); - - TEStylInsert("t", 1, 0, te); - TVAppend(tv, &style, "t", 1); -#endif + style.size = 18; + TVAppend(tv, &style, "This is some longer text that will scroll.", 42); } + UpdateScrollbarForTE(win, te_scroller, te, false); + TVUpdateScrollbar(tv, tv_scroller); + + DrawControls(win); + while (!quitting) { WaitNextEvent(everyEvent, &event, 5L, 0L); @@ -195,16 +193,78 @@ if (0) { DragWindow(event_win, event.where, &screenBits.bounds); break; case inGoAway: - if (TrackGoAway(event_win, event.where)) { - quitting = 1; + if (TrackGoAway(event_win, event.where)) + quitting = true; + break; + case inContent: { + Point p; + ControlHandle control; + short val, adj; + Rect r; + + p = event.where; + GlobalToLocal(&p); + + r = (*te)->viewRect; + if (PtInRect(p, &r)) { + TEClick(p, ((event.modifiers & shiftKey) != 0), te); + break; } + + r = tv->view; + if (PtInRect(p, &r)) { + TVClick(tv, p, ((event.modifiers & shiftKey) != 0)); + break; + } + + switch (FindControl(p, win, &control)) { + case inButton: + break; + case inUpButton: + case inDownButton: + case inPageUp: + case inPageDown: + if (control == te_scroller) { + SetTrackControlTE(te); + TrackControl(control, p, TrackMouseDownInControl); + } else if (control == tv_scroller) { + TVSetTrackScrollControl(tv); + TrackControl(control, p, TVTrackScrollControl); + } + break; + case inThumb: + val = GetCtlValue(control); + if (TrackControl(control, p, 0L) == 0) + break; + adj = val - GetCtlValue(control); + if (adj != 0) { + val -= adj; + if (control == te_scroller) + TEScroll(0, adj * TEGetHeight(0, 0, te), te); + SetCtlValue(control, val); + } + break; + } + } break; - case inContent: - break; } break; case updateEvt: event_win = (WindowPtr)event.message; + + BeginUpdate(event_win); + + #if 0 + r = (*te)->viewRect; + FillRect(&r, white); + TEUpdate(&r, te); + InsetRect(&r, -5, -5); + FrameRect(&r); + #endif + + UpdtControl(win, win->visRgn); + EndUpdate(event_win); + break; case activateEvt: break;