home *** CD-ROM | disk | FTP | other *** search
- /*
- MSMain.c
-
- Version 1.0d4
-
- Copyright © Apple Computer UK Ltd. 1992
-
- All rights reserved.
-
- Produced by : UK Developer Technical Support
- AppleLink : UK.DTS
-
- Changes for 1.0d2 :
-
- 11-Aug-92 : NH : Quit via AppleEvent
-
- Changes for 1.0d3 :
-
- 27-Aug-92 : NH : Duplicate ScriptForMenuExists, ExecuteScriptForMenu in DoFile zapped
- Prompt on new file when reverting fixed.
- added failure check on ExecuteScriptForMenu - saves problems if
- gustav quits before we do.
- */
-
- #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 <Editions.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 "MSEditions.h"
- #include "MSAppleEvents.h"
- #include "MSWindow.h"
- #include "MSFile.h"
- #include "MSScript.h"
-
- /*-----------------------------------------------------------------------*/
- /**---------- Standard Main routines --------------**/
- /*-----------------------------------------------------------------------*/
-
- #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;
- SectHandle currSection;
-
- 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[editM], cPublisher);
- DisableItem(myMenus[editM], cSubscriber);
- DisableItem(myMenus[editM], cOptions);
- DisableItem(myMenus[editM], cBorders);
-
- DisableItem(myMenus[scriptM], cCompile);
- DisableItem(myMenus[scriptM], cExecute);
-
- 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[editM], cBorders);
- 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);
- DisableItem(myMenus[editM], cPublisher);
- }
- else
- {
- EnableItem(myMenus[editM], cutCommand);
- EnableItem(myMenus[editM], copyCommand);
- EnableItem(myMenus[editM], clearCommand);
- EnableItem(myMenus[editM], cPublisher);
- }
-
- currSection = GetSection((**(theDoc->theText)).selStart,
- (**(theDoc->theText)).selEnd,
- theDoc);
- if (currSection)
- {
- DisableItem(myMenus[editM], cPublisher);
- DisableItem(myMenus[editM], cSubscriber);
- EnableItem(myMenus[editM], cOptions);
- if ((**(**currSection).fSectHandle).kind == stPublisher)
- SetItem(myMenus[editM], cOptions, "\pPublisher Options…");
- else
- SetItem(myMenus[editM], cOptions, "\pSubscriber Options…");
- }
- else
- {
- EnableItem(myMenus[editM], cPublisher);
- EnableItem(myMenus[editM], cSubscriber);
- DisableItem(myMenus[editM], cOptions);
- }
-
- EnableItem(myMenus[scriptM], cCompile);
- EnableItem(myMenus[scriptM], cExecute);
- }
- }
-
- #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() */
- }
-
- 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!=noErr && fileErr!=userCanceledErr)
- FileError("\perror saving ", theDoc->theFileName);
- else
- fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
-
- if (fileErr == noErr)
- SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
- }
- 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("\pRevert to the last saved version of ", revertName, "", "");
- alertResult = Alert(AdviseAlert, nil);
- switch (alertResult){
- case aaSave:if (IssueRevertCommand(theDoc->theWindow))
- FileError("\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;
-
- 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;
-
- case cPublisher : IssueCreatePublisher(theDocument);
- break;
-
- case cSubscriber: DoSubscribe(theDocument);
- break;
-
- case cOptions : DoSectionOptions(theDocument);
- break;
-
- case cBorders : IssueShowBorders(theDocument->theWindow,
- !theDocument->showBorders);
- 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;
- }
- 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);
- SetEditMenu(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;
- Boolean keyIsOk;
- SectHandle currSection;
-
-
- 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));
- HiliteMenu(0);
- }
- else
- if (theDoc->theText)
- {
- keyIsOk = true;
- /*
- don't allow a subscriber to be changed
- */
- currSection = GetSection((**(theDoc->theText)).selStart,
- (**(theDoc->theText)).selEnd,
- theDoc);
-
- if (currSection)
- if ((**(**currSection).fSectHandle).kind == stSubscriber)
- keyIsOk = KeyOKinSubscriber(theChar);
-
- if (keyIsOk)
- {
- DoTEKeySectionRecalc(theDoc, theChar);
-
- AddKeyToTypingBuffer(theDoc, theChar);
-
- TEKey(theChar, theDoc->theText);
-
- RecalcChangedSectionBorders(theDoc);
- 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;
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- MaxApplZone();
- SetUpCursors();
-
- SetUpMenus();
-
- gWCount = 0;
- gNewDocCount = 0;
- gQuitting = false;
- gFontMItem = 0;
-
- gGestaltAvailable = false;
- gAppleEventsImplemented = false;
- gAliasManagerImplemented = false;
- gEditionManagerImplemented = 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;
- }
-
- err = InitEditionPack();
- if (err)
- {
- ShowError("\pInitEditionPack", err);
- gQuitting = true;
- }
-
- err = AEObjectInit();
- if (err)
- {
- ShowError("\pAEObjectInit", err);
- gQuitting = true;
- }
-
- InitAppleEvents();
-
- err = PPCInit();
- if (err)
- {
- ShowError("\pPPCInit", err);
- gQuitting = true;
- }
-
- err = InitEditorScripting();
- if (err)
- {
- ShowError("\pInitEditorScripting", err);
- gQuitting = true;
- }
- else
- {
- while (!gQuitting)
- MainEvent();
-
- err = CloseEditorScripting();
- }
- }
-
- main ()
- {
- /*the main routine starts here*/
- DoSVEdit();
- }
-