AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

13

main: Do append loop only after idling


jcs made amendment 13 over 2 years ago
--- main.c Thu Dec 28 16:11:16 2023 +++ main.c Mon Oct 7 11:54:44 2024 @@ -6,26 +6,24 @@ #define SCROLLBAR_WIDTH 16 +#define PADDING 10 + +void test(WindowPtr win, struct TVRec *tv, ControlHandle tv_scroller); + short main(void) { Handle mbar; EventRecord event; WindowPtr event_win; - GrafPtr old_port; short event_in, n, finder_action, finder_count; - bool quitting = false; - char key; - - TEHandle te; - TextStyle testyle; - ControlHandle te_scroller, tv_scroller; + ControlHandle tv_scroller; WindowPtr win; Rect bounds; - long size, pos, count, linewidth, tvwidth, tvy, bufoff; - short err, fn, width, wcount; - char buf[512]; - char *word; + short height, width; + bool quitting = false; + bool test_ran = false; + char key; struct TVRec *tv; struct TVStyle style; @@ -43,137 +41,53 @@ main(void) util_init(); - bounds = screenBits.bounds; - bounds.right = (bounds.right - bounds.left) / 2; - bounds.top += 40; + width = screenBits.bounds.right - screenBits.bounds.left - PADDING; + if (width > 540) + width = 540; + height = screenBits.bounds.bottom - screenBits.bounds.top - + PADDING - (GetMBarHeight() * 2); + if (height > 340) + height = 300; + center_in_screen(width, height, true, &bounds); +bounds.bottom -= 170; win = NewWindow(0L, &bounds, "\ptextview test", false, noGrowDocProc, (WindowPtr)-1L, true, 0); ShowWindow(win); SelectWindow(win); SetPort(win); - - { - dest.top = 5; - dest.left = 5; - dest.bottom = ((win->portRect.bottom - win->portRect.top) / 2) - 5; - dest.right = win->portRect.right - SCROLLBAR_WIDTH - 5; - FrameRect(&dest); - InsetRect(&dest, 1, 1); - view = dest; - InsetRect(&dest, 5, 5); - - te = TEStylNew(&dest, &view); - if (!te) - panic("TEStylNew failed"); - testyle.tsFont = monaco; - 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); - } - { - 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); - - 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); - } + view.top = PADDING; + view.left = PADDING; + view.bottom = win->portRect.bottom - PADDING; + view.right = win->portRect.right - SCROLLBAR_WIDTH - PADDING; + FrameRect(&view); + InsetRect(&view, 1, 1); - err = FSOpen("\pMacintosh:sources:playground:lorem.txt", 0, &fn); - if (fn < 1) - panic("fsopen: %d", err); + padding.top = 5; + padding.left = 5; + padding.right = 5; + padding.bottom = 5; + tv = TVNew(&view, &padding); + if (!tv) + panic("TVNew failed"); + TVAutoScroll(tv, true); - GetEOF(fn, &size); - -if (1) { - for (pos = 0; pos < size; ) { - count = sizeof(buf); - err = FSRead(fn, &count, buf); - if (err != 0 && err != eofErr) - panic("fsread %d", err); - pos += count; - - TEStylInsert(buf, count, 0, te); - TVAppend(tv, &style, buf, count); - - break; - } -} + 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); - FSClose(fn); - -{ - testyle.tsFont = geneva; - testyle.tsSize = 24; - TESetStyle(doFont | doSize, &testyle, false, te); - TEStylInsert("Wello, there", 12, 0, te); - - style.font = geneva; - style.size = 24; - style.style = 0; - TVAppend(tv, &style, "Wello, there", 12); - - TEStylInsert(" this is a wrapped line", 26, 0, te); - TVAppend(tv, &style, " this is a wrapped line", 26); - - TEStylInsert(" t", 2, 0, te); - TVAppend(tv, &style, " t", 2); - - TEStylInsert("h", 1, 0, te); - TVAppend(tv, &style, "h", 1); - - TEStylInsert("i", 1, 0, te); - TVAppend(tv, &style, "i", 1); - - TEStylInsert("s. ", 3, 0, te); - TVAppend(tv, &style, "s. ", 3); - - testyle.tsFont = geneva; - testyle.tsSize = 18; - TESetStyle(doFont | doSize, &testyle, false, te); - TEStylInsert("This is some longer text that will scroll.", 42, 0, te); - - 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); switch (event.what) { case nullEvent: + if (!test_ran) { + test(win, tv, tv_scroller); + test_ran = true; + } break; case keyDown: case autoKey: @@ -205,12 +119,6 @@ if (1) { 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)); @@ -224,10 +132,7 @@ if (1) { case inDownButton: case inPageUp: case inPageDown: - if (control == te_scroller) { - SetTrackControlTE(te); - TrackControl(control, p, TrackMouseDownInControl); - } else if (control == tv_scroller) { + if (control == tv_scroller) { TVSetTrackScrollControl(tv); TrackControl(control, p, TVTrackScrollControl); } @@ -239,9 +144,7 @@ if (1) { adj = val - GetCtlValue(control); if (adj != 0) { val -= adj; - if (control == te_scroller) - TEScroll(0, adj * TEGetHeight(0, 0, te), te); - else if (control == tv_scroller) + if (control == tv_scroller) TVScroll(tv, 0, adj); SetCtlValue(control, val); } @@ -256,13 +159,7 @@ if (1) { BeginUpdate(event_win); - #if 0 - r = (*te)->viewRect; - FillRect(&r, white); - TEUpdate(&r, te); - InsetRect(&r, -5, -5); - FrameRect(&r); - #endif + TVUpdate(tv); UpdtControl(win, win->visRgn); EndUpdate(event_win); @@ -276,4 +173,66 @@ if (1) { } return 0; -} +} + +void +test(WindowPtr win, struct TVRec *tv, ControlHandle tv_scroller) +{ + struct TVStyle style; + long size, pos, count; + char buf[512]; + char *word; + short err, fn, n; + + style.font = geneva; //monaco; + style.size = 10; + style.style = 0; + + err = FSOpen("\plorem.txt", 0, &fn); + if (fn < 1) + panic("fsopen: %d", err); + + GetEOF(fn, &size); + + for (n = 0; n < 1; n++) { + SetFPos(fn, fsFromStart, 0); + + for (pos = 0; pos < size; ) { + count = sizeof(buf); + err = FSRead(fn, &count, buf); + if (err != 0 && err != eofErr) + panic("fsread %d", err); + pos += count; + + TVAppend(tv, &style, buf, count); + TVUpdateScrollbar(tv, tv_scroller); + } + } + + FSClose(fn); + +#define A "This is a line that will get moved down" + TVAppend(tv, &style, A, strlen(A)); + + style.font = geneva; + style.size = 24; + style.style = 0; + TVAppend(tv, &style, "Wello, there", 12); + + TVAppend(tv, &style, " this is a wrapped line", 26); + + TVAppend(tv, &style, " t", 2); + + TVAppend(tv, &style, "h", 1); + + TVAppend(tv, &style, "i", 1); + + TVAppend(tv, &style, "s. ", 3); + + style.size = 18; + TVAppend(tv, &style, "This is some longer text that will scroll.", 42); + + TVUpdateScrollbar(tv, tv_scroller); + + DrawControls(win); +} \ No newline at end of file