Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:54 UTC

view on githubraw file Latest commit ec6cf3b0 on 2003-08-26 20:45:25 UTC
ec6cf3b09d Ed H*0001 /* macninit.c -- general mac nifty application library initialization
                0002  */
                0003 /* (C) Copyright 1995 by Carnegie Mellon University
                0004  * All Rights Reserved.
                0005  *
                0006  * Permission to use, copy, modify, distribute, and sell this software
                0007  * and its documentation for any purpose is hereby granted without
                0008  * fee, provided that the above copyright notice appear in all copies
                0009  * and that both that copyright notice and this permission notice
                0010  * appear in supporting documentation, and that the name of Carnegie
                0011  * Mellon University not be used in advertising or publicity
                0012  * pertaining to distribution of the software without specific,
                0013  * written prior permission.  Carnegie Mellon University makes no
                0014  * representations about the suitability of this software for any
                0015  * purpose.  It is provided "as is" without express or implied
                0016  * warranty.
                0017  *
                0018  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0019  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0020  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0021  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0022  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0023  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0024  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                0025  * SOFTWARE.
                0026  */
                0027 /* (C) Copyright 1990-1994 by Christopher J. Newman
                0028  * All Rights Reserved.
                0029  *
                0030  * Permission to use, copy, modify, distribute, and sell this software and its
                0031  * documentation for any purpose is hereby granted without fee, provided that
                0032  * the above copyright notice appear in all copies and that both that
                0033  * copyright notice and this permission notice appear in supporting
                0034  * documentation, and that the name of Christopher J. Newman not be used in
                0035  * advertising or publicity pertaining to distribution of the software without
                0036  * specific, written prior permission.  Christopher J. Newman makes no
                0037  * representations about the suitability of this software for any purpose.  It
                0038  * is provided "as is" without express or implied warranty.
                0039  *
                0040  * CHRISTOPHER J. NEWMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
                0041  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
                0042  * SHALL CHRISTOPHER J. NEWMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
                0043  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
                0044  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                0045  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
                0046  * OF THIS SOFTWARE.
                0047  *
                0048  * Author:  Christopher J. Newman
                0049  * Message: This is a nifty program.
                0050  */
                0051 
                0052 #ifndef THINK_C
                0053 #include <Fonts.h>
                0054 #include <SegLoad.h>
                0055 #include <OSEvents.h>
                0056 
                0057 extern void _DataInit();            /* MPW initialize routine */
                0058 #endif
                0059 #include <string.h>
                0060 #include <Traps.h>
                0061 #include <AppleEvents.h>
                0062 #include <GestaltEqu.h>
                0063 #include <Balloons.h>
                0064 #include "macnapp.h"
                0065 
                0066 extern pascal OSErr NArequiredAE(AppleEvent *, AppleEvent *, long);
                0067 
                0068 #ifndef _Unimplemented
                0069 #define _Unimplemented                  0xA89F
                0070 #endif
                0071 #ifndef _WaitNextEvent
                0072 #define _WaitNextEvent                  0xA860
                0073 #endif
                0074 #ifndef _Gestalt
                0075 #define _Gestalt                        0xA1AD
                0076 #endif
                0077 
                0078 /* global to hold the application heap zone */
                0079 extern THz NAappzone;
                0080 
                0081 static Boolean TrapAvailable(short);
                0082 
                0083 /* this checks if a given trap is available on the system
                0084  */
                0085 static Boolean TrapAvailable(short tNumber)
                0086 {
                0087     short numtraps = 0x400;
                0088     TrapType tType;
                0089     
                0090     tType = tNumber & 0x800 ? ToolTrap : OSTrap;
                0091     if (tType == ToolTrap) {
                0092         if (NGetTrapAddress(_InitGraf, ToolTrap)
                0093             == NGetTrapAddress(0xAA6E, ToolTrap)) {
                0094             numtraps = 0x200;
                0095         }
                0096         tNumber = tNumber & 0x07FF;
                0097         if (tNumber >= numtraps)
                0098             tNumber = _Unimplemented;
                0099     }
                0100     
                0101     return (NGetTrapAddress(tNumber, tType)
                0102             != NGetTrapAddress(_Unimplemented, ToolTrap));
                0103 }
                0104 
                0105 /* initialize the Macintosh managers and niftyapp internal variables.
                0106  * call this first.
                0107  */
                0108 short NAinit(short minK, short masters, na_openp openp, na_menup menup,
                0109     short nummenu, short numapple, short newitem, short closeitem)
                0110  {
                0111     long         total, contig, response, procid;
                0112     EventRecord  event;
                0113     short        count;
                0114     Handle       menuBar;
                0115     short        message, i, aeflag = 0;
                0116     AppFile      afile;
                0117     FSSpec       fspec;
                0118     Str255       str;
                0119     MenuHandle   mh, helpmh;
                0120     CInfoPBRec   catinfo;
                0121 
                0122 #ifndef THINK_C
                0123     UnloadSeg((Ptr) _DataInit);     /* unload MPW init routine */
                0124 #endif
                0125     MaxApplZone();                  /* expand heap so code loads at top */
                0126     
                0127     /* initialize all the managers */
                0128     InitGraf((Ptr) &QD(thePort));
                0129     InitFonts();
                0130     InitWindows();
                0131     InitMenus();
                0132     TEInit();
                0133     InitDialogs(NULL);
                0134     InitCursor();
                0135     
                0136     /* allocate enough master pointers */
                0137     for (count = 1; count <= masters; count++) {
                0138         MoreMasters();
                0139         if (MemError() != noErr) return (-1);
                0140     }
                0141     
                0142     /* hack to bring to front in MultiFinder */
                0143     for (count = 1; count <= 3; count++) EventAvail(everyEvent, &event);
                0144         
                0145     /* get the system environment */
                0146     (void) SysEnvirons(1, &NAsysenv);
                0147     
                0148     /* verify we have 128K ROMS, WaitNextEvent is available, and we have enough memory */
                0149     if (NAsysenv.machineType < 0
                0150             || !TrapAvailable(_WaitNextEvent)
                0151             || (long) GetApplLimit() - (long) ApplicZone() < minK) {
                0152         return (-1);
                0153     }
                0154     
                0155     /* check for some Gestalt things and set up Apple Event Handler
                0156      * We assume that the Gestalt compatibility glue is used
                0157      */
                0158     NAgestaltBits = 0;
                0159     if (Gestalt(gestaltAppleEventsAttr, &response) == noErr) {
                0160         NAgestaltBits |= NA_HASAEVENTS;
                0161         if (AEInstallEventHandler(kCoreEventClass, typeWildCard,
                0162                 NArequiredAE, (long) openp, FALSE) == noErr) {
                0163             aeflag = 1;
                0164         }
                0165     }
                0166     if (Gestalt(gestaltFSAttr, &response) == noErr
                0167         && (response & gestaltHasFSSpecCalls)) {
                0168         NAgestaltBits |= NA_HASFSSPEC;
                0169     }
                0170     if (Gestalt(gestaltStandardFileAttr, &response) == noErr) {
                0171         NAgestaltBits |= NA_HASSTDFILE;
                0172     }
                0173     
                0174     /* clean up and check available free memory */
                0175     PurgeSpace(&total, &contig);
                0176     if (total < minK) return (-1);
                0177     
                0178     /* store our application heap zone */
                0179     NAappzone = ApplicZone();
                0180     
                0181     /* if the user wants automatic menu handling, do it now */
                0182     NAmenus = NULL;
                0183     if (menup != (na_menup) NULL) {
                0184         NAmenus = (MenuHandle **) NewHandle(sizeof (MenuHandle) * (nummenu + 1));
                0185         if (NAmenus == (MenuHandle **) NULL) return (-1);
                0186         HLock((Handle) NAmenus);
                0187         for (i = 0; i < nummenu; ++i) {
                0188             InsertMenu((*NAmenus)[i] = GetMenu(mApple + i), 0);
                0189         }
                0190         (*NAmenus)[i] = NULL;
                0191         HUnlock((Handle) NAmenus);
                0192         mh = **NAmenus; /* Apple menu */
                0193         *str = 0;
                0194         GetIndString(str, NA_HELPSTR, 1);
                0195         if (*str) {
                0196             if (Gestalt(gestaltHelpMgrAttr, &response) != noErr
                0197                 || HMGetHelpMenuHandle(&helpmh) != noErr) {
                0198                 helpmh = mh;
                0199             } else {
                0200                 NAhelpcount = CountMItems(helpmh);
                0201             }
                0202             for (i = 2; *str; ++i) {
                0203                 ++NAhelpitems;
                0204                 AppendMenu(helpmh, str);
                0205                 GetIndString(str, NA_HELPSTR, i);
                0206             }
                0207         }
                0208         AppendMenu(mh, "\p-");
                0209         AddResMenu(mh, 'DRVR');
                0210         DrawMenuBar();
                0211         NAcloseitem = closeitem;
                0212         NAnewitem = newitem;
                0213         NAappleitems = numapple;
                0214         if (nummenu > 2) NAhasedit = true;
                0215         NAmenup = menup;
                0216     }
                0217     
                0218     /* create full & empty regions */
                0219     NAfullRgn = NewRgn();
                0220     NAnullRgn = NewRgn();
                0221     if (!NAfullRgn || !NAnullRgn) return (-1);
                0222     SetRectRgn(NAfullRgn, -32767, -32767, 32767, 32767);
                0223     
                0224     /* save ibeam cursor */
                0225     NAibeam = **GetCursor(iBeamCursor);
                0226     
                0227     /* handle startup files (prior to system 7) */
                0228     if (!aeflag) {
                0229         count = 0;
                0230         if (openp != (na_openp) NULL) {
                0231             CountAppFiles(&message, &count);
                0232             if (count) {
                0233                 for (i = 1; i <= count; i++) {
                0234                     GetAppFiles(i, &afile);
                0235                     procid = 0;
                0236                     catinfo.hFileInfo.ioNamePtr = (StringPtr) &afile.fName;
                0237                     catinfo.hFileInfo.ioVRefNum = afile.vRefNum;
                0238                     catinfo.hFileInfo.ioFDirIndex = 0;
                0239                     catinfo.hFileInfo.ioDirID = 0;
                0240                     GetWDInfo(afile.vRefNum, &catinfo.hFileInfo.ioVRefNum,
                0241                         &catinfo.hFileInfo.ioDirID, &procid);
                0242                     if (PBGetCatInfoSync(&catinfo) == noErr) {
                0243                         fspec.vRefNum = catinfo.hFileInfo.ioVRefNum;
                0244                         fspec.parID = catinfo.hFileInfo.ioFlParID;
                0245                         memcpy(fspec.name, afile.fName, *afile.fName + 1);
                0246                         if ((*openp)(message, &fspec, &catinfo.hFileInfo.ioFlFndrInfo) < 0) {
                0247                             break;
                0248                         }
                0249                     }
                0250                 }
                0251                 ClrAppFiles(count);
                0252                 if (message == appPrint
                0253                         && NAcloseWindows(NAhead, NA_REQCLOSEALL) == NA_ALLCLOSED) {
                0254                     return (1);
                0255                 }
                0256             }
                0257         }
                0258         if (newitem && !count && menup != (na_menup) NULL) {
                0259             (*menup)(NULL, mFile, newitem);
                0260         }
                0261     }
                0262 
                0263     return (0);
                0264 }