#include #include #include #include "textview.h" #include "util.h" #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; short event_in, n, finder_action, finder_count; ControlHandle tv_scroller; WindowPtr win; Rect bounds; short height, width; bool quitting = false; bool test_ran = false; char key; struct TVRec *tv; struct TVStyle style; Rect dest, view, padding, r; InitGraf(&thePort); InitFonts(); FlushEvents(everyEvent, 0); InitWindows(); InitMenus(); TEInit(); InitDialogs(0); InitCursor(); MaxApplZone(); util_init(); 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); 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); padding.top = 5; padding.left = 5; padding.right = 5; padding.bottom = 5; tv = TVNew(&view, &padding); if (!tv) panic("TVNew failed"); TVAutoScroll(tv, true); 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); 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: key = event.message & charCodeMask; break; case mouseDown: event_in = FindWindow(event.where, &event_win); switch (event_in) { case inMenuBar: //handle_menu(MenuSelect(event.where)); break; case inSysWindow: SystemClick(&event, event_win); break; case inDrag: DragWindow(event_win, event.where, &screenBits.bounds); break; case inGoAway: 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 = 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 == 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 == tv_scroller) TVScroll(tv, 0, adj); SetCtlValue(control, val); } break; } } break; } break; case updateEvt: event_win = (WindowPtr)event.message; BeginUpdate(event_win); TVUpdate(tv); UpdtControl(win, win->visRgn); EndUpdate(event_win); break; case activateEvt: break; case app4Evt: break; } } 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); }