File: macninit.c

package info (click to toggle)
mpack 1.6-8.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,144 kB
  • ctags: 1,184
  • sloc: ansic: 8,785; sh: 330; makefile: 75; perl: 25
file content (264 lines) | stat: -rw-r--r-- 8,279 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* macninit.c -- general mac nifty application library initialization
 */
/* (C) Copyright 1995 by Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Carnegie
 * Mellon University not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  Carnegie Mellon University makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
/* (C) Copyright 1990-1994 by Christopher J. Newman
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Christopher J. Newman not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Christopher J. Newman makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * CHRISTOPHER J. NEWMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
 * SHALL CHRISTOPHER J. NEWMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 *
 * Author:	Christopher J. Newman
 * Message:	This is a nifty program.
 */

#ifndef THINK_C
#include <Fonts.h>
#include <SegLoad.h>
#include <OSEvents.h>

extern void _DataInit();			/* MPW initialize routine */
#endif
#include <string.h>
#include <Traps.h>
#include <AppleEvents.h>
#include <GestaltEqu.h>
#include <Balloons.h>
#include "macnapp.h"

extern pascal OSErr NArequiredAE(AppleEvent *, AppleEvent *, long);

#ifndef _Unimplemented
#define _Unimplemented					0xA89F
#endif
#ifndef _WaitNextEvent
#define _WaitNextEvent					0xA860
#endif
#ifndef _Gestalt
#define _Gestalt						0xA1AD
#endif

/* global to hold the application heap zone */
extern THz NAappzone;

static Boolean TrapAvailable(short);

/* this checks if a given trap is available on the system
 */
static Boolean TrapAvailable(short tNumber)
{
	short numtraps = 0x400;
	TrapType tType;
	
	tType = tNumber & 0x800 ? ToolTrap : OSTrap;
	if (tType == ToolTrap) {
		if (NGetTrapAddress(_InitGraf, ToolTrap)
			== NGetTrapAddress(0xAA6E, ToolTrap)) {
			numtraps = 0x200;
		}
		tNumber = tNumber & 0x07FF;
		if (tNumber >= numtraps)
			tNumber = _Unimplemented;
	}
	
	return (NGetTrapAddress(tNumber, tType)
			!= NGetTrapAddress(_Unimplemented, ToolTrap));
}

/* initialize the Macintosh managers and niftyapp internal variables.
 * call this first.
 */
short NAinit(short minK, short masters, na_openp openp, na_menup menup,
	short nummenu, short numapple, short newitem, short closeitem)
 {
	long		 total, contig, response, procid;
	EventRecord  event;
	short		 count;
	Handle		 menuBar;
	short		 message, i, aeflag = 0;
	AppFile		 afile;
	FSSpec		 fspec;
	Str255		 str;
	MenuHandle   mh, helpmh;
	CInfoPBRec   catinfo;

#ifndef THINK_C
	UnloadSeg((Ptr) _DataInit);		/* unload MPW init routine */
#endif
	MaxApplZone();					/* expand heap so code loads at top */
	
	/* initialize all the managers */
	InitGraf((Ptr) &QD(thePort));
	InitFonts();
	InitWindows();
	InitMenus();
	TEInit();
	InitDialogs(NULL);
	InitCursor();
	
	/* allocate enough master pointers */
	for (count = 1; count <= masters; count++) {
		MoreMasters();
		if (MemError() != noErr) return (-1);
	}
	
	/* hack to bring to front in MultiFinder */
	for (count = 1; count <= 3; count++) EventAvail(everyEvent, &event);
		
	/* get the system environment */
	(void) SysEnvirons(1, &NAsysenv);
	
	/* verify we have 128K ROMS, WaitNextEvent is available, and we have enough memory */
	if (NAsysenv.machineType < 0
			|| !TrapAvailable(_WaitNextEvent)
			|| (long) GetApplLimit() - (long) ApplicZone() < minK) {
		return (-1);
	}
	
	/* check for some Gestalt things and set up Apple Event Handler
	 * We assume that the Gestalt compatibility glue is used
	 */
	NAgestaltBits = 0;
	if (Gestalt(gestaltAppleEventsAttr, &response) == noErr) {
		NAgestaltBits |= NA_HASAEVENTS;
		if (AEInstallEventHandler(kCoreEventClass, typeWildCard,
				NArequiredAE, (long) openp, FALSE) == noErr) {
			aeflag = 1;
		}
	}
	if (Gestalt(gestaltFSAttr, &response) == noErr
		&& (response & gestaltHasFSSpecCalls)) {
		NAgestaltBits |= NA_HASFSSPEC;
	}
	if (Gestalt(gestaltStandardFileAttr, &response) == noErr) {
		NAgestaltBits |= NA_HASSTDFILE;
	}
	
	/* clean up and check available free memory */
	PurgeSpace(&total, &contig);
	if (total < minK) return (-1);
	
	/* store our application heap zone */
	NAappzone = ApplicZone();
	
	/* if the user wants automatic menu handling, do it now */
	NAmenus = NULL;
	if (menup != (na_menup) NULL) {
		NAmenus = (MenuHandle **) NewHandle(sizeof (MenuHandle) * (nummenu + 1));
		if (NAmenus == (MenuHandle **) NULL) return (-1);
		HLock((Handle) NAmenus);
		for (i = 0; i < nummenu; ++i) {
			InsertMenu((*NAmenus)[i] = GetMenu(mApple + i), 0);
		}
		(*NAmenus)[i] = NULL;
		HUnlock((Handle) NAmenus);
		mh = **NAmenus; /* Apple menu */
		*str = 0;
		GetIndString(str, NA_HELPSTR, 1);
		if (*str) {
			if (Gestalt(gestaltHelpMgrAttr, &response) != noErr
				|| HMGetHelpMenuHandle(&helpmh) != noErr) {
				helpmh = mh;
			} else {
				NAhelpcount = CountMItems(helpmh);
			}
			for (i = 2; *str; ++i) {
				++NAhelpitems;
				AppendMenu(helpmh, str);
				GetIndString(str, NA_HELPSTR, i);
			}
		}
		AppendMenu(mh, "\p-");
		AddResMenu(mh, 'DRVR');
		DrawMenuBar();
		NAcloseitem = closeitem;
		NAnewitem = newitem;
		NAappleitems = numapple;
		if (nummenu > 2) NAhasedit = true;
		NAmenup = menup;
	}
	
	/* create full & empty regions */
	NAfullRgn = NewRgn();
	NAnullRgn = NewRgn();
	if (!NAfullRgn || !NAnullRgn) return (-1);
	SetRectRgn(NAfullRgn, -32767, -32767, 32767, 32767);
	
	/* save ibeam cursor */
	NAibeam = **GetCursor(iBeamCursor);
	
	/* handle startup files (prior to system 7) */
	if (!aeflag) {
		count = 0;
		if (openp != (na_openp) NULL) {
			CountAppFiles(&message, &count);
			if (count) {
				for (i = 1; i <= count; i++) {
					GetAppFiles(i, &afile);
					procid = 0;
					catinfo.hFileInfo.ioNamePtr = (StringPtr) &afile.fName;
					catinfo.hFileInfo.ioVRefNum = afile.vRefNum;
					catinfo.hFileInfo.ioFDirIndex = 0;
					catinfo.hFileInfo.ioDirID = 0;
					GetWDInfo(afile.vRefNum, &catinfo.hFileInfo.ioVRefNum,
						&catinfo.hFileInfo.ioDirID, &procid);
					if (PBGetCatInfoSync(&catinfo) == noErr) {
						fspec.vRefNum = catinfo.hFileInfo.ioVRefNum;
						fspec.parID = catinfo.hFileInfo.ioFlParID;
						memcpy(fspec.name, afile.fName, *afile.fName + 1);
						if ((*openp)(message, &fspec, &catinfo.hFileInfo.ioFlFndrInfo) < 0) {
							break;
						}
					}
				}
				ClrAppFiles(count);
				if (message == appPrint
						&& NAcloseWindows(NAhead, NA_REQCLOSEALL) == NA_ALLCLOSED) {
					return (1);
				}
			}
		}
		if (newitem && !count && menup != (na_menup) NULL) {
			(*menup)(NULL, mFile, newitem);
		}
	}

	return (0);
}