home *** CD-ROM | disk | FTP | other *** search
- /*
- MSMain.c
-
- Version 3.1
-
- Copyright © 1995 Apple Computer, Inc., all rights reserved.
-
- MenuScripter by Nigel Humphreys and Jon Lansdell
- AppleEvent to script extensions by Greg Sutton
-
- Changes from 3.0d9
-
- - Full native PPC support. 3.0d9 was missing some UPPs.
-
- - Added support of the 'contents' property for the selection. 'get the
- contents of the selection of window 1' will now return the text of the
- selection.
-
- - Removed Publish and Subscribe support, since it was buggy and has never
- been properly fixed.
-
- - Updated code for connecting to the Scripting component.
- */
-
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Types.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Traps.h>
- #include <Packages.h>
- #include <PPCToolBox.h>
- #include <Printing.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <Scrap.h>
- #include <OSEvents.h>
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include <Errors.h>
- #include <PLStringFuncs.h>
-
- #include "MSGlobals.h"
- #include "MSUtils.h"
- #include "MSAppleEvents.h"
- #include "MSWindow.h"
- #include "MSFile.h"
- #include "MSScript.h"
- #include "MSAEScript.h"
-
- /*-----------------------------------------------------------------------*/
- /**---------- Standard Main routines --------------**/
- /*-----------------------------------------------------------------------*/
-
- #pragma segment Main
-
- DialogPtr PoseWaitDialog()
-
- {
- DialogPtr progressDialog;
-
- /* pose a dialog indicating that we are connecting to the AppleScript component */
- progressDialog = GetNewDialog(1006, nil, (WindowPtr)-1);
- SetPort(progressDialog);
- DrawDialog(progressDialog);
- return progressDialog;
-
- }
-
-
- #pragma segment Main
-
- pascal void MaintainCursor(void)
- {
- Point pt;
- WindowPtr wPtr;
- GrafPtr savePort;
- DPtr theDoc;
-
- wPtr = FrontWindow();
- if (Ours(wPtr))
- {
- theDoc = DPtrFromWindowPtr(wPtr);
- GetPort(&savePort);
- SetPort(wPtr);
- GetMouse(&pt);
- if (theDoc->theText)
- if (PtInRect(pt, &(**(theDoc->theText)).viewRect))
- SetCursor(&editCursor);
- else
- SetCursor(&qd.arrow);
- else
- SetCursor(&qd.arrow);
-
- if (theDoc->theText)
- TEIdle(theDoc->theText);
-
- SetPort(savePort);
- }
- }
-
- #pragma segment Main
-
- pascal void MaintainMenus(void)
- {
- DPtr theDoc;
- WindowPtr firstWindow;
-
- firstWindow = FrontWindow();
- if (!Ours(firstWindow))
- {
- EnableItem(myMenus[fileM], fmNew);
- EnableItem(myMenus[fileM], fmOpen);
- DisableItem(myMenus[fileM], fmClose);
- DisableItem(myMenus[fileM], fmSave);
- DisableItem(myMenus[fileM], fmSaveAs);
- DisableItem(myMenus[fileM], fmRevert);
- DisableItem(myMenus[fileM], fmPrint);
- DisableItem(myMenus[fileM], fmPageSetUp);
- DisableItem(myMenus[scriptM], cCompile);
- DisableItem(myMenus[scriptM], cExecute);
-
- EnableAEScriptItems(false);
-
- if (firstWindow)
- {
- EnableItem(myMenus[editM],undoCommand);
- EnableItem(myMenus[editM],cutCommand);
- EnableItem(myMenus[editM],copyCommand);
- EnableItem(myMenus[editM],pasteCommand);
- EnableItem(myMenus[editM],clearCommand);
- }
- }
- else
- {
- theDoc = DPtrFromWindowPtr(firstWindow);
- EnableItem(myMenus[editM], pasteCommand);
- EnableItem(myMenus[fileM], fmClose);
- EnableItem(myMenus[fileM], fmSaveAs);
- EnableItem(myMenus[fileM], fmPrint);
- EnableItem(myMenus[fileM], fmPageSetUp);
-
- if (theDoc->dirty)
- EnableItem(myMenus[fileM], fmRevert);
- else
- DisableItem(myMenus[fileM], fmRevert);
-
- if ((theDoc->dirty) && (theDoc->everSaved))
- EnableItem(myMenus[fileM], fmSave);
- else
- DisableItem(myMenus[fileM], fmSave);
-
- DisableItem(myMenus[editM], undoCommand);
-
- if (((**(theDoc->theText)).selEnd - (**(theDoc->theText)).selStart) < 0)
- {
- DisableItem(myMenus[editM],cutCommand);
- DisableItem(myMenus[editM],copyCommand);
- DisableItem(myMenus[editM],clearCommand);
- }
- else
- {
- EnableItem(myMenus[editM],cutCommand);
- EnableItem(myMenus[editM],copyCommand);
- EnableItem(myMenus[editM],clearCommand);
- }
-
- EnableItem(myMenus[scriptM], cCompile);
- EnableItem(myMenus[scriptM], cExecute);
-
- EnableAEScriptItems(true);
- }
- }
-
- #pragma segment Main
-
- pascal void SetUpCursors(void)
-
- {
- CursHandle hCurs;
-
- hCurs = GetCursor(1);
- editCursor = **hCurs;
- hCurs = GetCursor(watchCursor);
- waitCursor = **hCurs;
- }
-
- #pragma segment Main
-
- pascal void SetUpMenus(void)
- {
- short i;
-
- myMenus[appleM] = GetMenu(appleID);
- AddResMenu(myMenus[appleM], 'DRVR');
- myMenus[fileM] = GetMenu(fileID);
- myMenus[editM] = GetMenu(editID);
- myMenus[fontM] = GetMenu(mfontID);
- AddResMenu(myMenus[fontM], 'FONT');
- myMenus[sizeM] = GetMenu(sizeID);
- myMenus[styleM] = GetMenu(styleID);
- myMenus[scriptM] = GetMenu(mscriptID);
-
- for (i = appleM; i <= kLastMenu; i++)
- {
- InsertMenu(myMenus[i], 0);
- }
-
- SetItemStyle(myMenus[styleM], cPlain, 0);
- SetItemStyle(myMenus[styleM], cBold, bold);
- SetItemStyle(myMenus[styleM], cItalic, italic);
- SetItemStyle(myMenus[styleM], cUnderline, underline);
- SetItemStyle(myMenus[styleM], cOutline, outline);
- SetItemStyle(myMenus[styleM], cShadow, shadow);
- SetItemStyle(myMenus[styleM], cCondense, condense);
- SetItemStyle(myMenus[styleM], cExtend, extend);
-
- SetShortMenus(); /* Does a DrawMenuBar() */
- }
-
-
- #pragma segment Main
-
- // IdleProc for AESend
- pascal Boolean IdleProc(EventRecord *myEvent, long *sleep, RgnHandle *mouseRgn)
- {
- DPtr theDoc;
- WindowPtr theWindow;
- Boolean activate;
-
- switch (myEvent->what)
- {
- case nullEvent:
- *sleep = 0; // no null processing in this sample
- mouseRgn = nil;
- break;
-
- case activateEvt:
- activate = ((myEvent->modifiers & activeFlag) != 0);
- theWindow = (WindowPtr)myEvent->message;
- DoActivate(theWindow, activate);
- break;
-
- case updateEvt:
- theDoc = DPtrFromWindowPtr((WindowPtr)myEvent->message);
- DoUpdate(theDoc);
- break;
-
- case kOSEvent:
- switch (myEvent->message & osEvtMessageMask) // High byte of message
- {
- case 0x01000000:
- {
- gInBackground = ((myEvent->message & resumeFlag) == 0);
- DoActivate(FrontWindow(), ! gInBackground);
- }
- break;
-
- default:
- SysBeep(1);
- break;
- }
- break;
-
- default:
- SysBeep(1);
- break;
- }
-
- return(false); // I'll wait forever
- }
-
-
- #pragma segment Main
-
-
- pascal void ScrollVActionProc(ControlHandle control, short part);
- pascal void VActionProc(ControlHandle control, short part);
- pascal void HActionProc(ControlHandle control, short part);
- pascal void DrawStyledTextEditRec(DialogPtr theDialog, short theItem);
- pascal void DrawScrollBar(DialogPtr theDialog, short theItem);
-
-
- pascal void SetUpUniversalProcedures(void)
- {
- gScrollDocVActionUPP = NewControlActionProc(VActionProc);
- gScrollDocHActionUPP = NewControlActionProc(HActionProc);
-
- gScrollScriptVActionUPP = NewControlActionProc(ScrollVActionProc);
- gDrawStyledTextUPP = NewControlActionProc(DrawStyledTextEditRec);
- gDrawScrollBarUPP = NewControlActionProc(DrawScrollBar);
-
- gAEIdleUPP = NewAEIdleProc(IdleProc);
- }
-
-
- pascal void DoFile(short theItem)
- {
- short alertResult;
- DPtr theDoc;
- FSSpec theFSSpec;
- OSErr fileErr;
- TPrint thePSetup;
- Str255 revertName;
-
- switch (theItem){
- case fmNew : IssueAENewWindow();
- break;
- case fmOpen: if (GetFile(&theFSSpec)==noErr)
- fileErr = IssueAEOpenDoc(theFSSpec);
- break;
- case fmClose:IssueCloseCommand(FrontWindow());
- break;
- case fmSave:
- case fmSaveAs:
- theDoc = DPtrFromWindowPtr(FrontWindow());
-
- if (theDoc->everSaved == false || theItem == fmSaveAs)
- {
- fileErr = GetFileNameToSaveAs(theDoc);
- if (fileErr == userCanceledErr)
- break;
- else if (fileErr != noErr)
- FileError((unsigned char *)"\perror saving ", theDoc->theFileName);
- else
- fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
-
- if (fileErr == noErr)
- {
- SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
- theDoc->everSaved = true;
- }
- }
- else
- fileErr = IssueSaveCommand(theDoc->theWindow, nil);
- break;
-
- case fmRevert:SetCursor(&qd.arrow);
- theDoc = DPtrFromWindowPtr(FrontWindow());
-
- if (theDoc->everSaved)
- PLstrcpy(revertName,theDoc->theFileName);
- else
- GetWTitle(theDoc->theWindow, revertName);
-
- ParamText((unsigned char *)"\pRevert to the last saved version of",
- revertName, (unsigned char *)"", (unsigned char *)"");
- alertResult = Alert(AdviseAlert, nil);
- switch (alertResult){
- case aaSave:if (IssueRevertCommand(theDoc->theWindow))
- FileError((unsigned char *)"\perror reverting ",
- theDoc->theFileName);
- }
- break;
-
- case fmPageSetUp:
- theDoc = DPtrFromWindowPtr(FrontWindow());
- if (DoPageSetup(theDoc))
- {
- thePSetup = **(theDoc->thePrintSetup);
- IssuePageSetupWindow(theDoc->theWindow, thePSetup);
- }
- break;
-
- case fmPrint: IssuePrintWindow(FrontWindow());
- break;
-
- case fmQuit : IssueQuitCommand();
- break;
- } /*of switch*/
- }
-
-
-
-
- #pragma segment Main
-
- pascal void DoCommand(long mResult)
- {
- short theItem;
- short theMenuID;
- short err;
- long result;
- Str255 name;
- DPtr theDocument;
- Boolean exists;
- Boolean useOldCode;
- Boolean wasDirty;
-
- if (!gInBackground) HiliteMenu(0);
-
- theDocument = DPtrFromWindowPtr(FrontWindow());
-
- theItem = LoWord(mResult);
- theMenuID = HiWord(mResult);
-
- err = ScriptForMenuExists(theMenuID, theItem, &exists);
-
- if (err==noErr && exists==true)
- useOldCode = (ExecuteScriptForMenu(theMenuID,theItem)!=noErr);
- else
- useOldCode = true;
-
- if (useOldCode)
- switch (theMenuID){
-
- case appleID:
- if (theItem == aboutItem)
- {
- SetCursor(&qd.arrow);
- result = Alert(258,nil);
- }
- else
- {
- GetItem(myMenus[appleM], theItem, name);
- err = OpenDeskAcc(name);
- SetPort(FrontWindow());
- }
- break;
-
- case fileID: DoFile(theItem);
- break;
-
- case editID:
- if (SystemEdit(theItem - 1) == false)
- {
- }
- switch (theItem){
- case cutCommand :IssueCutCommand(theDocument);
- break;
-
- case copyCommand : IssueCopyCommand(theDocument);
- break;
-
- case pasteCommand : IssuePasteCommand(theDocument);
- break;
-
- case clearCommand : IssueClearCommand(theDocument);
- break;
-
- case selectAllCommand :
- if (theDocument) TESetSelect(0, (**(theDocument->theText)).teLength,
- theDocument->theText);
- break;
- } /*of switch*/
- ShowSelect(theDocument);
- break;
-
- case mfontID: IssueFontCommand(theDocument,theItem);
- break;
-
- case sizeID: IssueSizeCommand(theDocument,theItem);
- break;
-
- case styleID: IssueStyleCommand(theDocument, theItem);
- break;
-
- case mscriptID: switch (theItem){
-
- case cCompile : CompileDocument(theDocument);
- break;
-
- case cExecute : ExecuteDocument(theDocument);
- break;
-
- case cScript1 : ExecuteScript1(theDocument);
- break;
-
- case cScript2 : ExecuteScript2(theDocument);
- break;
-
- case cScript3 : ExecuteScript3(theDocument);
- break;
-
- case cScript4 : wasDirty = theDocument->dirty;
- theDocument->dirty = true;
- DoFile(fmSaveAs);
-
- if (! theDocument->dirty)
- ExecuteScript4(theDocument);
-
- else
- theDocument->dirty = wasDirty;
- break;
-
- }
- break;
-
- } /*of switch*/
-
- } /* DoCommand */
-
-
- #pragma segment Main
-
- pascal void DoMouseDown(const EventRecord *myEvent)
- {
- WindowPtr whichWindow;
- Point p;
- Rect dragRect;
- Rect oldPosn;
- DPtr theDoc;
- long menuResult;
- OSErr myErr;
- Boolean scriptExists;
- WindowPtr oldFront;
-
- p = myEvent->where;
- switch (FindWindow(p, &whichWindow)){
-
- case inDesk: SysBeep(10);
- break;
-
- case inGoAway:if (Ours(whichWindow))
- if (TrackGoAway(whichWindow, p))
- IssueCloseCommand(whichWindow);
- break;
- case inMenuBar:
- SetCursor(&qd.arrow);
- theDoc = DPtrFromWindowPtr(FrontWindow());
- if (theDoc)
- {
- SetFontMenu(theDoc);
- }
-
- menuResult = MenuSelect(p);
-
- /*
- Check for script editing -
- ctrl+option when selecting the menu = Edit Script )if any)
- */
-
- if (OptionKeyPressed(myEvent) && CtrlKeyPressed(myEvent))
- {
- myErr = ScriptForMenuExists(HiWord(menuResult), LoWord(menuResult), &scriptExists);
- if (scriptExists && myErr==noErr)
- EditMenuScript(HiWord(menuResult), LoWord(menuResult));
- else
- SysBeep(10);
- }
- else
- DoCommand(menuResult);
-
- HiliteMenu(0);
-
- break;
-
- case inSysWindow: SystemClick(myEvent, whichWindow);
- break;
-
- case inDrag:
-
- dragRect = qd.screenBits.bounds;
-
- if (Ours(whichWindow))
- {
- oldFront = FrontWindow();
- oldPosn = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
-
- DragWindow(whichWindow, p, &dragRect);
-
- if ((whichWindow==FrontWindow()) &&
- (whichWindow!=oldFront))
- IssueSelectWindowCommand(whichWindow, oldFront); // Record the select part of drag
- /*
- As rgnBBox may be passed by address
- */
- dragRect = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
- /*
- The windows already there, but still tell
- the our AppleEvents core about the move in case
- they want to do anything - or record
- */
- if (EqualRect(&dragRect, &oldPosn)==false)
- IssueMoveWindow(whichWindow, dragRect);
- }
- break;
-
- case inGrow:SetCursor(&qd.arrow);
- if (Ours(whichWindow))
- MyGrowWindow(whichWindow, p);
- break;
-
- case inZoomIn : DoZoom(whichWindow, inZoomIn, p);
- break;
-
- case inZoomOut: DoZoom(whichWindow, inZoomOut, p);
- break;
- case inContent: if (whichWindow != FrontWindow())
- {
- IssueSelectWindowCommand(whichWindow, FrontWindow()); // Record the selectWindow
- SelectWindow(whichWindow);
- }
- else
- if (Ours(whichWindow))
- DoContent(whichWindow, *myEvent);
- break;
- } /*of switch*/
- }
-
- #pragma segment Main
-
- pascal long GetSleep(void)
- {
- long sleep;
- WindowPtr theWindow;
- DPtr theDoc;
-
- sleep = 0x7fffffff;
- if (!gInBackground)
- {
- theWindow = FrontWindow();
- if (theWindow)
- {
- theDoc = DPtrFromWindowPtr(theWindow);
- if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
- sleep = GetCaretTime();
- }
- }
- return(sleep);
- } /*GetSleep*/
-
- #pragma segment Main
-
-
- pascal void MainEvent(void)
- {
- DPtr theDoc;
- EventRecord myEvent;
- char theChar;
- WindowPtr theWindow;
- Boolean activate;
-
-
- MaintainCursor(); /* TEIdle in here for now */
- MaintainMenus();
-
- if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
- {
- switch (myEvent.what) {
- case mouseDown: FlushAndRecordTypingBuffer();
- DoMouseDown(&myEvent);
- break;
- case keyDown:
- case autoKey:
- theDoc = DPtrFromWindowPtr(FrontWindow());
-
- theChar = myEvent.message & charCodeMask;
-
- if (theChar==3) // Enter key == compile
- {
- CompileDocument(theDoc);
- }
- else
- if ((myEvent.modifiers & cmdKey) == cmdKey)
- {
- DoCommand(MenuKey(theChar));
- }
-
- else
- if (theDoc->theText)
- {
- AddKeyToTypingBuffer(theDoc, theChar);
-
- TEKey(theChar, theDoc->theText);
-
- AdjustScrollbars(theDoc, false);
- ShowSelect(theDoc);
-
- theDoc->dirty = true;
- }
- break;
-
- case activateEvt:
- activate = ((myEvent.modifiers & activeFlag) != 0);
- theWindow = (WindowPtr)myEvent.message;
- DoActivate(theWindow, activate);
- break;
-
- case updateEvt:
- theDoc = DPtrFromWindowPtr((WindowPtr)myEvent.message);
- DoUpdate(theDoc);
- break;
-
- case kHighLevelEvent: FlushAndRecordTypingBuffer();
- DoAppleEvent(myEvent);
- break;
- case kOSEvent:
-
- switch (myEvent.message & osEvtMessageMask) { /*high byte of message*/
- case 0x01000000:
- {
- gInBackground = ((myEvent.message & resumeFlag) == 0);
- DoActivate(FrontWindow(), !gInBackground);
- }
- }
- }
- } /*of switch*/
- }
-
-
- #pragma segment Main
-
- pascal void DoSVEdit(void)
- {
- OSErr err;
- short result;
- DialogPtr progressDialog;
- GrafPtr oldPort;
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- MaxApplZone();
- SetUpCursors();
-
- SetUpMenus();
-
- SetUpUniversalProcedures();
-
- gWCount = 0;
- gNewDocCount = 0;
- gQuitting = false;
- gFontMItem = 0;
-
- gGestaltAvailable = false;
- gAppleEventsImplemented = false;
- gAliasManagerImplemented = false;
- gOutlineFontsImplemented = false;
-
- /*check environment checks to see if we are running 7.0*/
- if (!CheckEnvironment())
- {
- SetCursor(&qd.arrow);
- /*pose the only 7.0 alert*/
- result = Alert(302, nil);
- return;
- }
-
- GetVolumeAndDirectory(); // Call before SetUpScripts() - so files can be found
-
- err = AEObjectInit();
- if (err)
- {
- ShowError((unsigned char *)"\pAEObjectInit", err);
- gQuitting = true;
- }
-
- InitAppleEvents();
-
- err = PPCInit();
- if (err)
- {
- ShowError((unsigned char *)"\pPPCInit", err);
- gQuitting = true;
- }
-
- GetPort(&oldPort);
-
- progressDialog = PoseWaitDialog();
-
- err = InitEditorScripting();
- if (err)
- {
- DisposeDialog(progressDialog);
- SetPort(oldPort);
- ShowError((unsigned char *)"\pInitEditorScripting", err);
- gQuitting = true;
- }
- else
- {
- SetUpScripts(); // Need gScriptingComponent loaded so do after InitEditorScripting()
-
- DisposeDialog(progressDialog);
-
- SetPort(oldPort);
-
- SetOSAActiveProcedure(); // Set after all of our scripts are loaded
-
- while (!gQuitting)
- MainEvent();
-
- err = CleanUpAEScripts();
- err = CloseEditorScripting();
- }
-
- }
-
- void main ()
- {
- /*the main routine starts here*/
- DoSVEdit();
- }
-