home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / InitMenus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-26  |  11.8 KB  |  447 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "CursorBalloon.h"
  14. #include    "FabACursors.h"
  15. #include    "FabWList.h"
  16. #include    "InitCursorBalloon.h"
  17. #include    "SoundHandling.h"
  18. #include    "Independents.h"
  19. #include    "Internet.h"
  20. #include    "FabLibResIDs.h"
  21. #include    "SimResIDs.h"
  22.  
  23. #include    "Globals.h"
  24. #include    "AEHandlers.h"
  25. #include    "Animation.h"
  26. #include    "ControlStore.h"
  27. #include    "Disasm.h"
  28. #include    "DoEditDialog.h"
  29. #include    "DoMenu.h"
  30. #include    "DragManSim.h"
  31. #include    "Dump.h"
  32. #include    "InitMenus.h"
  33. #include    "Input.h"
  34. #include    "Main.h"
  35. #include    "Microprogram_Ed.h"
  36. #include    "Registers.h"
  37. #include    "SimAsm.h"
  38. #include    "SimUtils.h"
  39. #include    "GrowZone.h"
  40.  
  41. #if defined(FabSystem7orlater)
  42.  
  43. #pragma segment Main
  44.  
  45. static AEEventHandlerUPP    gmyHandleIO_UPP, gmyHandleGenericAlert_UPP;
  46.  
  47. #pragma segment Init
  48.  
  49. static void Init_My_Menus(void);
  50. static void GestaltCheck(void);
  51. static Boolean InitPrefs(void);
  52. static OSErr InitIO(void);
  53. static void FatalErrorAlert(short alertID, OSErr reason);
  54.  
  55. /* Init_My_Menus: sets up the menu bar */
  56.  
  57. static void Init_My_Menus(void)
  58. {
  59.     {
  60.     register Handle menuBar;
  61.     
  62.     SetMenuBar(menuBar = GetNewMBar(kRes_Menu_Apple));
  63.     DisposeHandle(menuBar);
  64.     }
  65.  
  66. AppendResMenu(gMenu_Apple = GetMenuHandle(kRes_Menu_Apple),'DRVR');
  67. gMenu_File = GetMenuHandle(kRes_Menu_File);
  68. gMenu_Edit = GetMenuHandle(kRes_Menu_Edit);
  69. gMenu_Windows = GetMenuHandle(kRes_Menu_Windows);
  70. gMenu_Control = GetMenuHandle(kRes_Menu_Control);
  71. gMenu_Assembler = GetMenuHandle(kRes_Menu_Assembler);
  72. InsertMenu(GetMenu(kRes_Menu_HMemory), hierMenu);
  73. InsertMenu(GetMenu(kRes_Menu_HRegs), hierMenu);
  74. InsertMenu(GetMenu(kRes_Menu_HDisasm), hierMenu);
  75. InsertMenu(GetMenu(kRes_Menu_HDump), hierMenu);
  76. InsertMenu(GetMenu(kMENU_HCopyWebLocation), hierMenu);
  77. InsertMenu(GetMenu(kMENU_HVisitWebLocation), hierMenu);
  78. gPopMenu = GetMenu(kRes_Menu_PopDump);
  79. //DrawMenuBar();
  80. }
  81.  
  82. /* InitAll: initializes the whole environment */
  83.  
  84. void InitAll(void)
  85. {
  86. EventRecord    dummyEvent;
  87. Rect    startingRect, endingRect;
  88. Point    dummyPt = {0, 0};
  89. DialogPtr    agh;
  90. Handle    splash;
  91. OSErr    err;
  92. Boolean    prefsFileWasAbsent;
  93.  
  94. MoreMasters();
  95. MoreMasters();
  96. MoreMasters();
  97. MoreMasters();
  98. MoreMasters();
  99. MoreMasters();
  100. InitGraf(&qd.thePort);
  101. InitFonts();
  102. InitWindows();
  103. InitMenus();
  104. TEInit();
  105. InitDialogs(nil);
  106.  
  107. FlushEvents(everyEvent, 0);
  108. InitCursor();
  109.  
  110. (void)UnloadScrap();
  111.  
  112. GestaltCheck();
  113.  
  114. gWatchHandle = GetCursor(watchCursor);
  115. SetCursor(*gWatchHandle);
  116. (void) FabLoadCursors(128);
  117.  
  118. InitMySoundHandling();
  119.  
  120. gInstrClikLoopUPP = MyNewListClickLoopProc(instrClikLoop);
  121. gCommentClikLoopUPP = MyNewListClickLoopProc(commentClikLoop);
  122. gSwitchCursClikLoopUPP = MyNewListClickLoopProc(switchCursClikLoop);
  123.  
  124. (void)InitCursorBalloonManager();
  125.  
  126. splash = OpenSplash(&agh, nil);
  127. SetGrafPortOfDialog(agh);
  128. endingRect = agh->portRect;
  129. LocalToGlobal(&topLeft(endingRect));
  130. LocalToGlobal(&botRight(endingRect));
  131. startingRect = endingRect;
  132. InsetRect(&startingRect, ((endingRect.right - endingRect.left) >> 1) - 2,
  133.             ((endingRect.bottom - endingRect.top) >> 1) - 2);
  134.  
  135. FabRotateCursor();
  136.  
  137. ZoomingRects(&gPrefs.zfr, &startingRect, &endingRect, zoomAccelerate);
  138. ShowWindow(agh);
  139. DrawDialog(agh);
  140.  
  141. gIBeamHandle = GetCursor(iBeamCursor);
  142. gPlusHandle = GetCursor(plusCursor);
  143.  
  144. FabRotateCursor();
  145.  
  146. gMMemory = NewPtrClear(kSIZE_RAM);
  147. gCsMemory = (union u_mir *)NewPtrClear(kSIZE_ASSMEM + kSIZE_CSMEM);
  148. if (gCsMemory)
  149.     gAssMemory = (((Ptr)gCsMemory) + kSIZE_CSMEM);
  150.  
  151. MyFillBuffer((long *)gAssMemory, numOfLongs(kSIZE_ASSMEM), 0x03030303);
  152.  
  153. FabRotateCursor();
  154.  
  155. Init_My_Menus();
  156.  
  157. gRegs[kREG_ZERO] = 0;
  158. gRegs[kREG_ONE] = 1;
  159. gRegs[kREG_MINUS1] = -1;
  160.  
  161. FabRotateCursor();
  162.  
  163. prefsFileWasAbsent = InitPrefs();
  164.  
  165. FabRotateCursor();
  166.  
  167. ResetMemory();
  168. ResetRegisters();
  169. UnloadSeg(ResetRegisters);
  170.  
  171. FabRotateCursor();
  172.  
  173. if (Init_Animation()) ExitToShell();
  174.  
  175. FabRotateCursor();
  176.  
  177. if (Init_Microprogram_Ed()) ExitToShell();
  178.  
  179. FabRotateCursor();
  180.  
  181. if (Init_Registers()) ExitToShell();
  182.  
  183. FabRotateCursor();
  184.  
  185. if (InitIO()) ExitToShell();
  186.  
  187. FabRotateCursor();
  188.  
  189. if (InitDump()) ExitToShell();
  190.  
  191. FabRotateCursor();
  192.  
  193. if (Init_Disasm()) ExitToShell();
  194.  
  195.  
  196. SetMir(-1L);
  197. FabRotateCursor();
  198. SetMir(0L);
  199.  
  200. if (prefsFileWasAbsent)
  201.     gPrefs.remembWind = true;
  202.  
  203. if(err = InstallRequiredAEHandlers(myHandleOAPP, myHandleODOC, myHandlePDOC, myHandleQUIT)) {
  204.     FatalErrorAlert(kALRT_GENERICERROR, err);
  205.     gDoneFlag = true;
  206.     }
  207.  
  208. FabRotateCursor();
  209.  
  210. gmyHandleIO_UPP = NewAEEventHandlerProc(myHandleIO);
  211. gmyHandleGenericAlert_UPP = NewAEEventHandlerProc(myHandleGenericAlert);
  212. if(err = AEInstallEventHandler(kFCR_MINE, kAEmySignalIO, gmyHandleIO_UPP, 0, false)) {
  213.     FatalErrorAlert(kALRT_GENERICERROR, err);
  214.     gDoneFlag = true;
  215.     }
  216.  
  217. if(err = AEInstallEventHandler(kCreat, kAEAlert, gmyHandleGenericAlert_UPP, 0, false)) {
  218.     FatalErrorAlert(kALRT_GENERICERROR, err);
  219.     gDoneFlag = true;
  220.     }
  221.  
  222. (void)TEFromScrap();
  223.  
  224. FabRotateCursor();
  225.  
  226. gICerr = MyICInit(&gICinst, kFCR_MINE);
  227. UnloadSeg(ICStart);
  228.  
  229. FabRotateCursor();
  230.  
  231. DisposeSplash(splash, agh);
  232. UnloadSeg(DisposeSplash);
  233.  
  234. FabRotateCursor();
  235.  
  236. (void) EventAvail(0, &dummyEvent);
  237.  
  238. FabRotateCursor();
  239.  
  240. if (gPrefs.AnimVisible)
  241.     ShowWindowAndCheckMenu(gWPtr_Animation, kMItem_Animation);
  242. if (gPrefs.DisasmVisible) {
  243.     DecideActivation(&dummyEvent, gWPtr_Disasm, dummyPt, false);
  244.     ShowWindowAndCheckMenu(gWPtr_Disasm, kMItem_Disasm);
  245.     }
  246. if (gPrefs.DumpVisible) {
  247.     DecideActivation(&dummyEvent, gWPtr_Dump, dummyPt, false);
  248.     ShowWindowAndCheckMenu(gWPtr_Dump, kMItem_Dump);
  249.     }
  250. if (gPrefs.IOVisible) {
  251.     DecideActivation(&dummyEvent, gWPtr_IO, dummyPt, false);
  252.     ShowWindowAndCheckMenu(gWPtr_IO, kMItem_IO);
  253.     }
  254. if (gPrefs.RegsVisible)
  255.     ShowWindowAndCheckMenu(gWPtr_Registers, kMItem_Registers);
  256.  
  257. gILCBase = gRegs[kREG_PC];
  258.  
  259. FabRotateCursor();
  260.  
  261. AdjustMenus(nil);
  262. DrawMenuBar();
  263. FabFreeCursors();
  264. InitGrowZone();
  265. InitCursor();
  266. }
  267.  
  268. /* GestaltCheck: is this Mac set up with all we need to run? */
  269.  
  270. static void GestaltCheck(void)
  271. {
  272. #define    kMinimumMemoryFree    (236L*1024)
  273.  
  274. #define    POWERMANAGER    ((1L << gestaltPMgrExists)|(1L << gestaltPMgrCPUIdle))
  275. #define TEMPMEMMANAGER    ((1L << gestaltTempMemSupport)|(1L << gestaltRealTempMemory)|(1L << gestaltTempMemTracked))
  276. long    Gresp;
  277. unsigned long    totalSize, contigSize;
  278.  
  279. PurgeSpace((long *)&totalSize, (long *)&contigSize);
  280. if (totalSize < kMinimumMemoryFree) {
  281.     (void)StopAlert_UPP(kALRT_MEMEXHAUSTED, nil);
  282.     ExitToShell();
  283.     }
  284.  
  285. if (TrapAvailable(_Gestalt)) {
  286.     gDragManagerActive = HasDragManager();
  287.     if (Gestalt(gestaltQuickdrawVersion, &Gresp) == noErr)
  288.         gHasColorQD = ((Byte)(Gresp >> 8)) > 0;
  289.     if (Gestalt(gestaltPowerMgrAttr, &Gresp) == noErr)
  290.         gPwrManagerIsPresent = ((Gresp & POWERMANAGER) == POWERMANAGER) != 0;
  291.     if (Gestalt(gestaltAliasMgrAttr, &Gresp) == noErr)
  292.         if (Gresp & (1L << gestaltAliasMgrPresent))
  293.             if (Gestalt(gestaltAppleEventsAttr, &Gresp) == noErr)
  294.                 if (Gresp & (1L << gestaltAppleEventsPresent))
  295.                     if (Gestalt(gestaltDITLExtAttr, &Gresp) == noErr)
  296.                         if (Gresp & (1L << gestaltDITLExtPresent))
  297.                             if (Gestalt(gestaltFindFolderAttr, &Gresp) == noErr)
  298.                                 if (Gresp & (1L << gestaltFindFolderPresent))
  299.                             if (Gestalt(gestaltFSAttr, &Gresp) == noErr)
  300.                                 if (Gresp & (1L << gestaltHasFSSpecCalls))
  301.                             if (Gestalt(gestaltHelpMgrAttr, &Gresp) == noErr)
  302.                                 if (Gresp & (1L << gestaltHelpMgrPresent))
  303.                             if (Gestalt(gestaltPopupAttr, &Gresp) == noErr)
  304.                                 if (Gresp & (1L << gestaltPopupPresent))
  305. // System 7.0 bug: it doesn't install the gestaltResourceMgrAttr selector
  306.                     //        if (Gestalt(gestaltResourceMgrAttr, &Gresp) == noErr)
  307.                     //            if (Gresp & (1L << gestaltPartialRsrcs))
  308.                             if (Gestalt(gestaltStandardFileAttr, &Gresp) == noErr)
  309.                                 if (Gresp & (1L << gestaltStandardFile58))
  310.                             if (Gestalt(gestaltTextEditVersion, &Gresp) == noErr)
  311.                                 if (Gresp >= gestaltTE4)
  312.                             if (Gestalt(gestaltOSAttr, &Gresp) == noErr)
  313.                                 if ((Gresp & TEMPMEMMANAGER) == TEMPMEMMANAGER)
  314.                             /* proceed because everything is OK */
  315.                                 return;
  316.     }
  317. (void)StopAlert_UPP(kALRT_OLDSYSTEM, nil);
  318. ExitToShell();
  319. }
  320.  
  321.  
  322. /* InitPrefs: loads preferences if present */
  323.  
  324. Boolean InitPrefs(void)
  325. {
  326. ParamBlockRec    myPB;
  327. EventRecord    dummyEv;
  328. FSSpec    myFSS;
  329. Handle    myStrHand, prefsBuffer;
  330. short    prefsFRefNum;
  331. Boolean    targetFolder, isAnAlias;
  332. OSErr    err;
  333. SignedByte    oldState;
  334. Boolean    prefsFileAbsent = false;
  335.  
  336. oldState = WantThisHandleSafe(myStrHand = (Handle)GetString(kSTR_PREFSFILENAME));
  337. if ((err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  338.             &myFSS.vRefNum, &myFSS.parID)) == noErr) {
  339.     err = FSMakeFSSpecCompat(myFSS.vRefNum, myFSS.parID, (ConstStr255Param)*myStrHand, &myFSS);
  340.     if (err == fnfErr)
  341.         prefsFileAbsent = true;
  342.     else if (err == noErr)
  343.         if ((err = ResolveAliasFile(&myFSS, true, &targetFolder, &isAnAlias)) == noErr)
  344.             if (targetFolder)
  345.                 err = paramErr;
  346.             else
  347.                 if ((err = FSpOpenDFCompat(&myFSS, fsRdPerm, &prefsFRefNum)) == noErr) {
  348.                     if (prefsBuffer = NewHandleGeneral(sizeof(struct myprefs))) {
  349.                         myPB.ioParam.ioCompletion = nil;
  350.                         myPB.ioParam.ioRefNum = prefsFRefNum;
  351.                         HLockHi(prefsBuffer);
  352.                         myPB.ioParam.ioBuffer = *prefsBuffer;
  353.                         myPB.ioParam.ioReqCount = sizeof(struct myprefs);
  354.                         myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  355.                         myPB.ioParam.ioPosOffset = 0L;
  356.                         (void)PBReadAsync(&myPB);
  357.                         while (myPB.ioParam.ioResult > 0) {
  358.                             SystemTask();
  359.                             (void)EventAvail(everyEvent, &dummyEv);
  360.                             }
  361.                         if ((err = myPB.ioParam.ioResult) == noErr)
  362.                             if (((struct myprefs *)*prefsBuffer)->prefsVersion == kCurPrefsVersion) {
  363.                                 gPrefs = *(struct myprefs *)*prefsBuffer;
  364.                                 }
  365.                             else if (((struct myprefs *)*prefsBuffer)->prefsVersion > kCurPrefsVersion) {
  366.                                 gDoNotOverwritePrefs = gDoneFlag = CautionAlert_UPP(kALRT_PREFSWARNING, nil) == cancel;
  367.                                 SetCursor(*gWatchHandle);
  368.                                 }
  369.                         DisposeHandle(prefsBuffer);
  370.                         }
  371.                     (void)FSClose(prefsFRefNum);
  372.                     }
  373.     }
  374. HSetState(myStrHand, oldState);
  375.  
  376. return prefsFileAbsent;
  377. }
  378.  
  379. /* InitIO: initializes the input/output window */
  380.  
  381. static OSErr InitIO(void)
  382. {
  383. Rect    destRect, viewRect;
  384. FabWindowPtr    thefabw;
  385. register WindowPtr    window;
  386. register DocumentIOPeek doc;
  387. register OSErr    err = appMemFullErr;
  388.  
  389. gWPtr_IO = window = GetNewWindow(kWIND_IO, nil, (WindowPtr)-1L);
  390. if (window) {
  391.     SetPortWindowPort(window);
  392.     
  393.     if (gPrefs.remembWind) {
  394.         if (IsOnScreen(&gPrefs.IOUserState)) {
  395.             (*(WStateDataHandle)((WindowPeek)window)->dataHandle)->userState = gPrefs.IOUserState;
  396.             ZoomWindow(window, inZoomIn, false);
  397.             }
  398.         }
  399.     doc = &gIODoc;
  400.     GetTERect(window, &viewRect);
  401.     destRect = viewRect;
  402.     destRect.right = destRect.left +
  403.                         (qd.screenBits.bounds.right - qd.screenBits.bounds.left);
  404.     if ( doc->docTE = TENew(&destRect, &viewRect) ) {
  405.         (void)TEFeatureFlag(teFOutlineHilite, teBitSet, doc->docTE);
  406.         TEAutoView(true, doc->docTE);
  407.         if (doc->docVScroll = GetNewControl(kCNTL_IOVScroll, window)) {
  408.             if (doc->docHScroll = GetNewControl(kCNTL_IOHScroll, window)) {
  409. // adjust & draw the controls, draw the window
  410.                 AdjustScrollbars(window, true);
  411.  
  412.                 thefabw = AddWindowToList(window);
  413.                 doc->docWindow = thefabw;
  414.                 SetActivate(thefabw, DoActivateWindow);
  415.                 SetUpdate(thefabw, DoUpdateWindow);
  416.                 SetGrow(thefabw, DoGrowWindow);
  417.                 SetZoom(thefabw, DoZoomWindow);
  418.                 SetGoAway(thefabw, CloseIO);
  419.                 SetContent(thefabw, DoContentClick);
  420.                 
  421.                 InstallRgnHandler(thefabw, NewRgn(), RecalcIO, gIBeamHandle,
  422.                         toMenu(kBalloons_Dump, kBIO_Contents), 0, 0);
  423.                 ResizeObjects(window);
  424.                 RecalcGlobalCoords(thefabw);
  425.  
  426.                 err = noErr;
  427.                 }
  428.             }
  429.         }
  430.     }
  431. return err;
  432. } /* InitIO */
  433.  
  434. /* FatalErrorAlert: errors while initing windows */
  435.  
  436. static void FatalErrorAlert(short alertID, OSErr reason)
  437. {
  438. Str255    tempS;
  439.  
  440. MyNumToString(reason, tempS);
  441. ParamText(tempS, nil, nil, nil);
  442. (void)StopAlert_UPP(alertID, nil);
  443. }
  444.  
  445. #endif
  446.  
  447.