1 /* macint.c - macintosh interface routines for xlisp 2.1e */
2 /* Written by Brian Kendig. */
3 /* The functions here are only called by macstuff.c. */
4
5 #include <Events.h>
6 #include <Gestalt.h>
7 #include <Memory.h>
8 #include <Menus.h>
9 #include <Events.h>
10 #include <Quickdraw.h>
11 #include <StandardFile.h>
12 #include <TextEdit.h>
13 #include <ToolUtils.h>
14 #include <Traps.h>
15 #include <Windows.h>
16 #include <Controls.h>
17 /* #include <ControlDefinitions.h> */
18 #include <SIOUX.h>
19 #include <AppleEvents.h>
20 #include "macint.h"
21 /* #define FALSE 0
22 #define TRUE 1 */
23 #define NIL ((void *) 0)
24
25
26 #include "MacCommandWin.h"
27 #include "macaboutbox.h"
28 #include "MacDrag.h"
29 #include "MacHandleEv.h"
30 #include "macstuff.h"
31 #include "stdio.h"
32 #define TEXTREC (*hTERec) /* the command window text record */
33 #define TEXTCHAR(i) ((*(TEXTREC->hText))[i])
34
35 // Struct for apple event handling
36 typedef struct AEventList {
37 AEEventClass evclass;
38 AEEventID evid;
39 void *handler;
40 long refcon;
41 } AEventList, *AEventListPtr;
42
43 //===========================================================================
44 // GLOBALS DEFINED HERE USE MacGlobals.h FOR ACCESS
45 //===========================================================================
46
47 // Menu handles
48 MenuHandle appleMenu, fileMenu, editMenu, controlMenu;
49
50 /* command and graphics windows */
51 WindowPtr gCommandWin, gGraphicsWin;
52 WindowRecord commandWinRec, bwGraphicsWinRec;
53 CWindowRecord colorGraphicsWinRec;
54 Boolean gGraphicsShown, gCommandWinResized = false;
55
56 // Screen size stuff
57 Rect dragRect, sizeRect;
58 int screenWidth, screenHeight; /* screen dimensions */
59 int sHorizontal, sVertical, sWidth, sHeight; /* command win, split screen */
60 int gHorizontal, gVertical, gWidth, gHeight; /* graphics win, split screen */
61
62 // The Text handle
63 TEHandle hTERec;
64
65 /* output is buffered */
66 //Handle hOutputBuffer = NULL;
67 int outputBufferLength = 0;
68
69 // Allocate space for UPPs
70 ControlActionUPP uppScrollProc;
71 TEClickLoopUPP uppScrollClickLoop;
72 //AEEventHandlerUPP uppAEOpenFiles, uppAEQuit;
73
74 // Text related globals
75 CharsHandle pastedTextH = NULL; /* a handle to pasted text */
76 int pastedLength = 0; /* how many chars there are in the paste buffer */
77 int flashTime = 0, cursorBeforeFlash; /* for flashing cursor when parens match */
78 char recentChar; /* the last character typed */
79 RgnHandle gMouseRgn; // holds current mouse regin
80
81 /* miscellaneous stuff */
82 Boolean gInBackground; /* are we in background or not */
83 int wneImplemented;
84 unsigned long startupTicks;
85 Boolean hasColorQD;
86
87 short howManyFiles = 0, whichFile = 0; /* keep track of files opened from Finder */
88
89
90 // Prototypes
91 static pascal OSErr AEQuit (AppleEvent *theAppleEvent, AppleEvent *theReply, long Refcon);
92 static pascal OSErr AEOpenFiles (AppleEvent *theAppleEvent, AppleEvent *theReply, long Refcon);
93 pascal Boolean ScrollClickLoop (void);
94 pascal void ScrollProc (ControlHandle control, short thePart);
95 Rect SetTERect (void);
96 void FlushOutput (void);
97
ShowGrafWin(void)98 void ShowGrafWin (void) {
99 /* make the graphics window visible */
100 ShowWindow (gGraphicsWin);
101 SelectWindow (gGraphicsWin);
102 SetMenuItemText (controlMenu, SHOW_GRAPHICS, "\pHide Graphics");
103 //AdjustCursor ();
104 gGraphicsShown = true;
105 }
106
HideGrafWin(void)107 void HideGrafWin (void) {
108 /* hide the graphics window */
109 HideWindow (gGraphicsWin);
110 SetMenuItemText (controlMenu, SHOW_GRAPHICS, "\pShow Graphics");
111 gGraphicsShown = false;
112 }
113
114
UpdateGraphWindow()115 static void UpdateGraphWindow ()
116 {
117 BeginUpdate (gGraphicsWin);
118 EndUpdate (gGraphicsWin);
119 }
InitMac(void)120 void InitMac (void) {
121 // { /* set up memory properly */
122 // int i;
123 // fix this later. -EAD
124 //if (DefltStack < STACKMIN) SetApplLimit (CurStackBase - STACKMIN);
125 // MaxApplZone ();
126 // for (i = 0; i < MASTERS; i++) MoreMasters ();
127 // }
128 AEventListPtr theAppleEvent;
129 AEventList theEventList[] = {
130 { kCoreEventClass, kAEOpenDocuments, AEOpenFiles, 0 },
131 { kCoreEventClass, kAEQuitApplication, AEQuit, 0 },
132 { 0, 0, nil, 0 }
133 };
134 int i;
135
136 /* do all the necessary initialization mumbo-jumbo */
137 if (StackSpace() < STACKMIN)
138 SetApplLimit(GetApplLimit() - STACKMIN);
139 MaxApplZone();
140 /* printf("New StackSpace %lx GetApplLimit %lx\n",
141 StackSpace(), GetApplLimit()); */
142 for (i = 0; i < MASTERS; i++) MoreMasters ();
143 /* getchar(); */
144
145 /* initialize the toolbox */
146 InitGraf (&qd.thePort);
147 InitFonts ();
148 FlushEvents (everyEvent, 0);
149 InitWindows ();
150 InitMenus ();
151 TEInit ();
152 InitDialogs (NIL);
153 InitCursor ();
154
155 // Setup Callbacks
156 uppScrollClickLoop = NewTEClickLoopProc(ScrollClickLoop);
157 uppScrollProc = NewControlActionProc(ScrollProc);
158
159 // Handlers for core apple events
160 for (theAppleEvent = theEventList; theAppleEvent->handler; theAppleEvent++)
161 if (AEInstallEventHandler(theAppleEvent->evclass, theAppleEvent->evid, NewAEEventHandlerProc((ProcPtr)theAppleEvent->handler),
162 theAppleEvent->refcon, 0) != noErr);
163
164 // Set up the SIOUX window
165 SIOUXSettings.initializeTB = FALSE; //Toolbox is alread inited
166 SIOUXSettings.setupmenus = FALSE; //keep the csound menus
167 SIOUXSettings.autocloseonquit = TRUE; //close sioux without asking for save
168 SIOUXSettings.showstatusline = FALSE; //no status line
169 SIOUXSettings.asktosaveonclose = FALSE; //don't ask to save
170 SIOUXSettings.toppixel = 40;
171 SIOUXSettings.leftpixel = 5;
172
173 /* see if we have WaitNextEvent and Color Quickdraw */
174 wneImplemented = (NGetTrapAddress (_WaitNextEvent, ToolTrap) != NGetTrapAddress (_Unimplemented, ToolTrap));
175 if (NGetTrapAddress ((short) Gestalt, ToolTrap) != NGetTrapAddress (_Unimplemented, ToolTrap)) {
176 long returnCode;
177 OSErr err = Gestalt (gestaltQuickdrawVersion, &returnCode);
178 hasColorQD = ((err == noErr) && (returnCode >= gestalt8BitQD));
179 } else hasColorQD = false;
180
181 { /* set up menus */
182 Handle theMenuBar = GetNewMBar (MBAR_RES);
183 SetMenuBar (theMenuBar);
184 appleMenu = (MenuHandle)GetMenuHandle (APPLE_MENU_RES);
185 fileMenu = (MenuHandle)GetMenuHandle (FILE_MENU_RES);
186 editMenu = (MenuHandle)GetMenuHandle (EDIT_MENU_RES);
187 controlMenu = (MenuHandle)GetMenuHandle (CONTROL_MENU_RES);
188 AppendResMenu (appleMenu, 'DRVR');
189 DrawMenuBar ();
190 }
191
192 /* get the size of the main screen */
193 screenWidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
194 screenHeight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
195
196 /* compute the size of the graphics window in split-screen mode */
197 gHorizontal = SCREEN_MARGIN;
198 gVertical = MBAR_HEIGHT + TITLEBAR_HEIGHT - 1;
199 gWidth = screenWidth - (SCREEN_MARGIN * 2);
200 gHeight = GRAFWIN_HEIGHT;
201
202 /* compute the size of the command window in split-screen mode */
203 sHorizontal = SCREEN_MARGIN;
204 sVertical = MBAR_HEIGHT + TITLEBAR_HEIGHT - 1 + SCREEN_MARGIN + GRAFWIN_HEIGHT;
205 sWidth = screenWidth - (SCREEN_MARGIN * 2);
206 sHeight = screenHeight - MBAR_HEIGHT - TITLEBAR_HEIGHT - (SCREEN_MARGIN * 2) - GRAFWIN_HEIGHT - 1;
207
208 /* set up size and drag rects */
209 dragRect = (*GetGrayRgn ())->rgnBBox;
210 // dragRect.left += DRAG_THRESHOLD;
211 // dragRect.right -= DRAG_THRESHOLD;
212 // dragRect.bottom -= DRAG_THRESHOLD;
213 sizeRect.top = MIN_WIN_HEIGHT;
214 sizeRect.left = MIN_WIN_WIDTH;
215 sizeRect.bottom = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
216 sizeRect.right = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
217
218 /* create the command window */
219 gCommandWin = GetNewWindow (CWINRES, &commandWinRec, (WindowPtr) -1L);
220 SetPort (gCommandWin);
221
222 /* create the graphics window */
223 if (hasColorQD) gGraphicsWin = GetNewCWindow (GWINRES, &colorGraphicsWinRec, (WindowPtr) -1L);
224 else gGraphicsWin = GetNewWindow (GWINRES, &bwGraphicsWinRec, (WindowPtr) -1L);
225
226 startupTicks = TickCount (); /* take note of what time we're starting up */
227
228 // Create mouse regin
229 gMouseRgn = NewRgn();
230
231 // Initalize some command window stuff
232 InitalizeCmdWindow();
233
234 // Turn on text outlineing
235 TEFeatureFlag(teFOutlineHilite, teBitSet, hTERec);
236
237 HideGrafWin ();
238
239 { /* see if the user launched the app by opening text files from the Finder */
240 short doWhat;\
241 // call to CountAppFiles was commented out, but that left doWhat uninitialized
242 // RBD added this ifdef, I wonder where CountAppFiles came from?
243 #ifdef CountAppFilesDefined
244 CountAppFiles (&doWhat, &howManyFiles);
245 if (doWhat != appOpen) howManyFiles = 0;
246 #else
247 howManyFiles = 0;
248 #endif
249 }
250
251 UpdateCmdWindow ();
252
253 }
254
255
256
DoAppleMenu(int theItem)257 static void DoAppleMenu (int theItem) {
258 switch (theItem) {
259 case ABOUT_ITEM:
260 DoAboutBox ();
261 break;
262 default: {
263 Str255 name;
264 GetMenuItemText (appleMenu, theItem, name);
265 OpenDeskAcc (name);
266 break;
267 }
268 }
269 }
270 /* this should really be in a header for MacFileUtils.c */
271 void GetFullPath(FSSpec *theSpec, StringPtr theName);
272
273
DoFileMenu(int theItem)274 static void DoFileMenu (int theItem) {
275 extern xlload (char *, int, int);
276 extern xlabort(char *);
277 extern xlisp_wrapup (void);
278 StandardFileReply theFile;
279
280 SFTypeList fileTypes;
281 Point pt = { 100, 100 };
282
283 fileTypes[0] = 'TEXT';
284 switch (theItem) {
285 case LOAD:
286 case LOAD_NOISILY:
287 StopPasting ();
288 StandardGetFile(NIL, 1, fileTypes, &theFile);
289 if (theFile.sfGood) {
290 Str255 theFullPath;
291 short wdRefNum;
292
293 OSErr err;
294 HiliteMenu (0);
295
296 err = OpenWD(theFile.sfFile.vRefNum, theFile.sfFile.parID, 'Nyqu', &wdRefNum);
297 err = SetVol(NIL, wdRefNum);
298 SetSelection (TEXTREC->teLength, TEXTREC->teLength); /* send cursor to end */
299
300 GetFullPath(&theFile.sfFile, theFullPath);
301 P2CStr(theFullPath);
302
303 if ((xlload((char *) theFullPath, 1, (theItem == LOAD_NOISILY))) == 0) {
304 xlabort("load error");
305 }
306 macputs ("> ");
307 PrepareForInput ();
308 }
309 break;
310 case QUIT:
311 xlisp_wrapup ();
312 }
313 }
314
DoEditMenu(int theItem)315 static void DoEditMenu (int theItem) {
316 if (SystemEdit (theItem-1) == false)
317 switch (theItem) {
318 case CUT: case COPY:
319 if (ZeroScrap () == noErr) {
320 TECopy (hTERec); /* after copying, export the TE scrap */
321 if (TEToScrap () != noErr) ZeroScrap ();
322 }
323 if (theItem == CUT) DeleteRange ();
324 break;
325 case PASTE: {
326 long scrapOffset;
327 if (pastedTextH) DisposeHandle (pastedTextH);
328 pastedTextH = (CharsHandle) NewHandle (0);
329 pastedLength = GetScrap (pastedTextH, 'TEXT', &scrapOffset);
330 if (pastedLength < 0) pastedLength = 0; /* error */
331 else {
332 SetHandleSize (pastedTextH, pastedLength + 1);
333 HLock (pastedTextH);
334 ((char *)(*pastedTextH))[pastedLength] = '\0';
335 HUnlock (pastedTextH);
336 }
337 } /* and fall through ... */
338 case CLEAR:
339 DeleteRange ();
340 break;
341 }
342 }
343
DoControlMenu(int theItem)344 static void DoControlMenu (int theItem) {
345 extern xlbreak (char *, char *);
346 extern char *s_unbound;
347 extern xlcontinue (void);
348 extern xlcleanup (void);
349 extern xlabort (char *);
350 extern xltoplevel (void);
351
352 scrflush ();
353 HiliteMenu (0);
354 switch (theItem) {
355 case BREAK: StopPasting (); xlbreak ("user break", s_unbound); PrepareForInput (); break;
356 case CONTINUE: StopPasting (); xlcontinue (); PrepareForInput (); break;
357 case CLEAN_UP: StopPasting (); xlcleanup (); PrepareForInput (); break;
358 case CANCEL_INPUT: StopPasting (); xlabort ("input canceled"); PrepareForInput (); break;
359 case TOP_LEVEL: StopPasting (); xltoplevel (); PrepareForInput (); break;
360 case SHOW_GRAPHICS:
361 if (gGraphicsShown) HideGrafWin ();
362 else ShowGrafWin ();
363 break;
364 case SPLIT_SCREEN:
365 MoveWindow (gCommandWin, sHorizontal, sVertical, -1);
366 SizeWindow (gCommandWin, sWidth, sHeight, -1);
367 InvalRect (&gCommandWin->portRect);
368 SetTERect ();
369 SetScrollRect ();
370 ShowGrafWin ();
371 MoveWindow (gGraphicsWin, gHorizontal, gVertical, -1);
372 SizeWindow (gGraphicsWin, gWidth, gHeight, -1);
373 break;
374 }
375 }
376
DoMenu(long choice)377 void DoMenu (long choice) {
378 int theMenu = HiWord (choice), theItem = LoWord (choice);
379
380 HiliteMenu (theMenu);
381 switch (theMenu) {
382 case APPLE_MENU_RES: DoAppleMenu (theItem); break;
383 case FILE_MENU_RES: DoFileMenu (theItem); break;
384 case EDIT_MENU_RES: DoEditMenu (theItem); break;
385 case CONTROL_MENU_RES: DoControlMenu (theItem); break;
386 }
387 HiliteMenu (0);
388 }
389
AdjustMenus(void)390 void AdjustMenus (void) {
391 /* turn the stuff in the Edit menu on and off as necessary */
392 long temp;
393 DisableItem (editMenu, UNDO);
394 if (TEXTREC->selStart != TEXTREC->selEnd) {
395 EnableItem (editMenu, CUT);
396 EnableItem (editMenu, COPY);
397 EnableItem (editMenu, CLEAR);
398 } else {
399 DisableItem (editMenu, CUT);
400 DisableItem (editMenu, COPY);
401 DisableItem (editMenu, CLEAR);
402 }
403 if (GetScrap (NIL, 'TEXT', &temp) > 0) EnableItem (editMenu, PASTE);
404 else DisableItem (editMenu, PASTE);
405 }
406
407 RgnHandle rgn = nil;
408
DoContent(EventRecord * theEvent)409 void DoContent (EventRecord *theEvent) {
410 /* handle a click in a window's content region */
411 ControlHandle theScrollBar;
412 GrafPtr oldPort;
413 int scrollValue;
414 Point mouse = theEvent->where;
415 int thePart;
416 // RgnHandle rgn = nil;
417
418 GetPort (&oldPort);
419 SetPort (gCommandWin);
420 GlobalToLocal (&mouse);
421
422 // Get Selected text
423 rgn = NewRgn();
424 TEGetHiliteRgn(rgn, hTERec);
425
426 if (thePart = FindControl (mouse, gCommandWin, &theScrollBar)) {
427 switch (thePart) {
428 case kControlUpButtonPart:
429 case kControlDownButtonPart:
430 case kControlPageUpPart:
431 case kControlPageDownPart:
432 scrollValue = TrackControl (theScrollBar, mouse, uppScrollProc);
433 break;
434 case kControlIndicatorPart:
435 scrollValue = GetControlValue (theScrollBar);
436 thePart = TrackControl (theScrollBar, mouse, NIL);
437 if (thePart) {
438 scrollValue -= GetControlValue (theScrollBar);
439 if (scrollValue) TEScroll (0, scrollValue * LINEHEIGHT, hTERec);
440 }
441 break;
442 }
443 } else if (PtInRgn(mouse, rgn)) {
444 if (!DragText(theEvent)) {
445 TEClick(mouse, false, hTERec);
446 }
447 } else if (PtInRect (mouse, &(TEXTREC->viewRect))) {
448 TEClick (mouse, (theEvent->modifiers & shiftKey) != 0, hTERec);
449 }
450 SetPort (oldPort);
451 DisposeRgn(rgn);
452 }
453
454
DoEvent(void)455 void DoEvent (void) {
456 EventRecord theEvent;
457
458 if ((flashTime) && (--flashTime == 0)) SetSelection (cursorBeforeFlash, cursorBeforeFlash);
459 if (outputBufferLength) FlushOutput ();
460 if (FrontWindow () == gCommandWin) TEIdle (hTERec);
461 recentChar = '\0';
462
463 if (WaitNextEvent (everyEvent, &theEvent, 0, gMouseRgn)) {
464
465 AdjustCursor (theEvent.where, gMouseRgn);
466
467 switch (theEvent.what) {
468 case kHighLevelEvent:
469 AEProcessAppleEvent(&theEvent);
470 break;
471 case mouseDown:
472 DoMouseDown (&theEvent);
473 break;
474 case keyDown:
475 case autoKey:
476 DoKeyPress (&theEvent);
477 break;
478 case activateEvt: {
479 WindowPtr whichWindow = (WindowPtr)theEvent.message;
480 SetPort (whichWindow);
481 if (whichWindow == gCommandWin) {
482 if ((theEvent.modifiers & activeFlag) == 1) {
483 ActivateCmdWindow();
484 } else {
485 DeactivateCmdWindow();
486 }
487 }
488 break;
489 }
490 case updateEvt: {
491 if ((WindowPtr)theEvent.message == gCommandWin) UpdateCmdWindow ();
492 if ((WindowPtr)theEvent.message == gGraphicsWin) UpdateGraphWindow ();
493 break;
494 }
495 case osEvt:
496 if (((theEvent.message >> 24) & 0xff) == suspendResumeMessage) {
497 if (theEvent.message & resumeFlag) {
498 gInBackground = false;
499 if (FrontWindow () == gCommandWin) {
500 ActivateCmdWindow();
501 }
502 } else {
503 gInBackground = true;
504 if (FrontWindow () == gCommandWin) {
505 SetPort (gCommandWin);
506 DeactivateCmdWindow();
507 }
508 }
509 }
510 break;
511
512 }
513 }
514 AdjustCursor (theEvent.where, gMouseRgn);
515 }
516
MacWrapUp(void)517 void MacWrapUp (void) {
518 /* take everything down in preparation for quitting */
519 CleanupCmdWindow();
520 CloseWindow (gGraphicsWin);
521 }
522