AmendHub

Download

jcs

/

textview

/

main.c

 

(View History)

jcs   main: Do append loop only after idling Latest amendment: 13 on 2024-10-07

1 #include <stdio.h>
2 #include <stddef.h>
3 #include <string.h>
4 #include "textview.h"
5 #include "util.h"
6
7 #define SCROLLBAR_WIDTH 16
8
9 #define PADDING 10
10
11 void test(WindowPtr win, struct TVRec *tv, ControlHandle tv_scroller);
12
13 short
14 main(void)
15 {
16 Handle mbar;
17 EventRecord event;
18 WindowPtr event_win;
19 short event_in, n, finder_action, finder_count;
20 ControlHandle tv_scroller;
21 WindowPtr win;
22 Rect bounds;
23 short height, width;
24 bool quitting = false;
25 bool test_ran = false;
26 char key;
27
28 struct TVRec *tv;
29 struct TVStyle style;
30 Rect dest, view, padding, r;
31
32 InitGraf(&thePort);
33 InitFonts();
34 FlushEvents(everyEvent, 0);
35 InitWindows();
36 InitMenus();
37 TEInit();
38 InitDialogs(0);
39 InitCursor();
40 MaxApplZone();
41
42 util_init();
43
44 width = screenBits.bounds.right - screenBits.bounds.left - PADDING;
45 if (width > 540)
46 width = 540;
47 height = screenBits.bounds.bottom - screenBits.bounds.top -
48 PADDING - (GetMBarHeight() * 2);
49 if (height > 340)
50 height = 300;
51 center_in_screen(width, height, true, &bounds);
52 bounds.bottom -= 170;
53 win = NewWindow(0L, &bounds, "\ptextview test", false, noGrowDocProc,
54 (WindowPtr)-1L, true, 0);
55 ShowWindow(win);
56 SelectWindow(win);
57 SetPort(win);
58
59 view.top = PADDING;
60 view.left = PADDING;
61 view.bottom = win->portRect.bottom - PADDING;
62 view.right = win->portRect.right - SCROLLBAR_WIDTH - PADDING;
63 FrameRect(&view);
64 InsetRect(&view, 1, 1);
65
66 padding.top = 5;
67 padding.left = 5;
68 padding.right = 5;
69 padding.bottom = 5;
70 tv = TVNew(&view, &padding);
71 if (!tv)
72 panic("TVNew failed");
73 TVAutoScroll(tv, true);
74
75 bounds.left = view.right;
76 bounds.right = bounds.left + SCROLLBAR_WIDTH;
77 bounds.bottom = view.bottom + 1;
78 bounds.top = view.top - 1;
79 tv_scroller = NewControl(win, &bounds, "\p", true, 1, 1, 1,
80 scrollBarProc, 0L);
81
82 while (!quitting) {
83 WaitNextEvent(everyEvent, &event, 5L, 0L);
84
85 switch (event.what) {
86 case nullEvent:
87 if (!test_ran) {
88 test(win, tv, tv_scroller);
89 test_ran = true;
90 }
91 break;
92 case keyDown:
93 case autoKey:
94 key = event.message & charCodeMask;
95 break;
96 case mouseDown:
97 event_in = FindWindow(event.where, &event_win);
98
99 switch (event_in) {
100 case inMenuBar:
101 //handle_menu(MenuSelect(event.where));
102 break;
103 case inSysWindow:
104 SystemClick(&event, event_win);
105 break;
106 case inDrag:
107 DragWindow(event_win, event.where, &screenBits.bounds);
108 break;
109 case inGoAway:
110 if (TrackGoAway(event_win, event.where))
111 quitting = true;
112 break;
113 case inContent: {
114 Point p;
115 ControlHandle control;
116 short val, adj;
117 Rect r;
118
119 p = event.where;
120 GlobalToLocal(&p);
121
122 r = tv->view;
123 if (PtInRect(p, &r)) {
124 TVClick(tv, p, ((event.modifiers & shiftKey) != 0));
125 break;
126 }
127
128 switch (FindControl(p, win, &control)) {
129 case inButton:
130 break;
131 case inUpButton:
132 case inDownButton:
133 case inPageUp:
134 case inPageDown:
135 if (control == tv_scroller) {
136 TVSetTrackScrollControl(tv);
137 TrackControl(control, p, TVTrackScrollControl);
138 }
139 break;
140 case inThumb:
141 val = GetCtlValue(control);
142 if (TrackControl(control, p, 0L) == 0)
143 break;
144 adj = val - GetCtlValue(control);
145 if (adj != 0) {
146 val -= adj;
147 if (control == tv_scroller)
148 TVScroll(tv, 0, adj);
149 SetCtlValue(control, val);
150 }
151 break;
152 }
153 }
154 break;
155 }
156 break;
157 case updateEvt:
158 event_win = (WindowPtr)event.message;
159
160 BeginUpdate(event_win);
161
162 TVUpdate(tv);
163
164 UpdtControl(win, win->visRgn);
165 EndUpdate(event_win);
166
167 break;
168 case activateEvt:
169 break;
170 case app4Evt:
171 break;
172 }
173 }
174
175 return 0;
176 }
177
178 void
179 test(WindowPtr win, struct TVRec *tv, ControlHandle tv_scroller)
180 {
181 struct TVStyle style;
182 long size, pos, count;
183 char buf[512];
184 char *word;
185 short err, fn, n;
186
187 style.font = geneva; //monaco;
188 style.size = 10;
189 style.style = 0;
190
191 err = FSOpen("\plorem.txt", 0, &fn);
192 if (fn < 1)
193 panic("fsopen: %d", err);
194
195 GetEOF(fn, &size);
196
197 for (n = 0; n < 1; n++) {
198 SetFPos(fn, fsFromStart, 0);
199
200 for (pos = 0; pos < size; ) {
201 count = sizeof(buf);
202 err = FSRead(fn, &count, buf);
203 if (err != 0 && err != eofErr)
204 panic("fsread %d", err);
205 pos += count;
206
207 TVAppend(tv, &style, buf, count);
208 TVUpdateScrollbar(tv, tv_scroller);
209 }
210 }
211
212 FSClose(fn);
213
214 #define A "This is a line that will get moved down"
215 TVAppend(tv, &style, A, strlen(A));
216
217 style.font = geneva;
218 style.size = 24;
219 style.style = 0;
220 TVAppend(tv, &style, "Wello, there", 12);
221
222 TVAppend(tv, &style, " this is a wrapped line", 26);
223
224 TVAppend(tv, &style, " t", 2);
225
226 TVAppend(tv, &style, "h", 1);
227
228 TVAppend(tv, &style, "i", 1);
229
230 TVAppend(tv, &style, "s. ", 3);
231
232 style.size = 18;
233 TVAppend(tv, &style, "This is some longer text that will scroll.", 42);
234
235 TVUpdateScrollbar(tv, tv_scroller);
236
237 DrawControls(win);
238 }