Rev 193 | Rev 268 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 193 | Rev 259 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | This file is part of a common library |
2 | This file is part of a common library |
3 | Copyright (C) 1990-2006 Toby Thain, toby@telegraphics.com.au |
3 | Copyright (C) 1990-2006 Toby Thain, toby@telegraphics.com.au |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
9 | 9 | ||
10 | This program is distributed in the hope that it will be useful, |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. |
13 | GNU General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
18 | */ |
19 | 19 | ||
20 | #include <devices.h> // OpenDriver |
20 | #include <devices.h> // OpenDriver |
21 | #include <dialogs.h> // InitDialogs |
21 | #include <dialogs.h> // InitDialogs |
22 | #include <files.h> |
22 | #include <files.h> |
23 | #include <fp.h> |
23 | #include <fp.h> |
24 | #include <gestalt.h> |
24 | #include <gestalt.h> |
25 | #include <lists.h> |
25 | #include <lists.h> |
26 | #include <lowmem.h> // LMGetUnitTableEntryCount, LMGetUTableBase |
26 | #include <lowmem.h> // LMGetUnitTableEntryCount, LMGetUTableBase |
27 | #include <memory.h> |
27 | #include <memory.h> |
28 | #include <plstringfuncs.h> // PLstrcpy |
28 | #include <plstringfuncs.h> // PLstrcpy |
29 | #include <quickdraw.h> // qd |
29 | #include <quickdraw.h> // qd |
30 | #include <resources.h> |
30 | #include <resources.h> |
31 | #include <segload.h> // AppFile etc. |
31 | #include <segload.h> // AppFile etc. |
32 | #include <stdio.h> // sprintf |
32 | #include <stdio.h> // sprintf |
33 | #include <strings.h> // c2pstr |
33 | #include <strings.h> // c2pstr |
34 | #include <string.h> // strlen |
34 | #include <string.h> // strlen |
35 | #include <textedit.h> // TEInit |
35 | #include <textedit.h> // TEInit |
36 | 36 | ||
37 | #include "qd.h" |
37 | #include "qd.h" |
38 | #include "misc-mac.h" |
38 | #include "misc-mac.h" |
39 | #include "str.h" |
39 | #include "str.h" |
40 | 40 | ||
41 | Boolean gestalt_attr(OSType s,int a){ long r; |
41 | Boolean gestalt_attr(OSType s,int a){ long r; |
42 | // MPW glue checks the availability of _Gestalt |
42 | // MPW glue checks the availability of _Gestalt |
43 | return /*TrapAvailable(_Gestalt) &&*/ !Gestalt(s,&r) && (r & (1<<a)); |
43 | return /*TrapAvailable(_Gestalt) &&*/ !Gestalt(s,&r) && (r & (1<<a)); |
44 | } |
44 | } |
45 | 45 | ||
46 | int count_selected(ListRef l){ |
46 | int count_selected(ListRef l){ |
47 | Cell c; |
47 | Cell c; |
48 | int n; |
48 | int n; |
49 | 49 | ||
50 | for(c.h = c.v = n = 0; LGetSelect(true, &c, l);){ |
50 | for(c.h = c.v = n = 0; LGetSelect(true, &c, l);){ |
51 | n++; |
51 | n++; |
52 | if(!LNextCell(true, true, &c, l)) |
52 | if(!LNextCell(true, true, &c, l)) |
53 | break; // all done |
53 | break; // all done |
54 | } |
54 | } |
55 | return n; |
55 | return n; |
56 | } |
56 | } |
57 | 57 | ||
58 | Boolean any_selected(ListRef l){ |
58 | Boolean any_selected(ListRef l){ |
59 | Cell c = {0,0}; |
59 | Cell c = {0,0}; |
60 | return LGetSelect(true, &c, l); |
60 | return LGetSelect(true, &c, l); |
61 | } |
61 | } |
62 | 62 | ||
63 | void select_all(ListRef l){ |
63 | void select_all(ListRef l){ |
64 | Cell c = {0,0}; |
64 | Cell c = {0,0}; |
65 | do |
65 | do |
66 | LSetSelect(true,c,l); |
66 | LSetSelect(true,c,l); |
67 | while(LNextCell(true,true,&c,l)); |
67 | while(LNextCell(true,true,&c,l)); |
68 | } |
68 | } |
69 | 69 | ||
70 | OSErr read_line(short rn,Handle l){ |
70 | OSErr read_line(short rn,Handle l){ |
71 | OSErr e; |
71 | OSErr e; |
72 | ParamBlockRec pb; |
72 | ParamBlockRec pb; |
73 | char hs = HGetState(l); |
73 | char hs = HGetState(l); |
74 | long n = 0; |
74 | long n = 0; |
75 | #define P pb.ioParam |
75 | #define P pb.ioParam |
76 | 76 | ||
77 | P.ioCompletion = 0; |
77 | P.ioCompletion = 0; |
78 | P.ioRefNum = rn; |
78 | P.ioRefNum = rn; |
79 | P.ioReqCount = 0x200; // read this much at a time |
79 | P.ioReqCount = 0x200; // read this much at a time |
80 | P.ioPosMode = '\n'<<8 | 0x80 | fsAtMark; |
80 | P.ioPosMode = '\n'<<8 | 0x80 | fsAtMark; |
81 | do{ |
81 | do{ |
82 | SetHandleSize(l,n + P.ioReqCount); |
82 | SetHandleSize(l,n + P.ioReqCount); |
83 | if(e = MemError()) |
83 | if(e = MemError()) |
84 | break; |
84 | break; |
85 | HLock(l); |
85 | HLock(l); |
86 | P.ioBuffer = *l + n; |
86 | P.ioBuffer = *l + n; |
87 | e = PBReadSync(&pb); |
87 | e = PBReadSync(&pb); |
88 | HSetState(l,hs); |
88 | HSetState(l,hs); |
89 | n += P.ioActCount; |
89 | n += P.ioActCount; |
90 | }while(!e && P.ioBuffer[P.ioActCount-1] != '\n'); |
90 | }while(!e && P.ioBuffer[P.ioActCount-1] != '\n'); |
91 | SetHandleSize(l,n); |
91 | SetHandleSize(l,n); |
92 | return e; // returns eofErr if called after the last newline-terminated line has been read |
92 | return e; // returns eofErr if called after the last newline-terminated line has been read |
93 | } |
93 | } |
94 | 94 | ||
95 | Boolean starts_with(Handle l,char *key){ |
95 | Boolean starts_with(Handle l,char *key){ |
96 | short n = strlen(key); |
96 | short n = strlen(key); |
97 | if(GetHandleSize(l)<n) |
97 | if(GetHandleSize(l)<n) |
98 | return false; |
98 | return false; |
99 | else{ // calling strncmp may move memory if it is not in the caller's segment! |
99 | else{ // calling strncmp may move memory if it is not in the caller's segment! |
100 | Boolean f; |
100 | Boolean f; |
101 | char hs = HGetState(l); |
101 | char hs = HGetState(l); |
102 | HLock(l); |
102 | HLock(l); |
103 | f = !strncmp(*l,key,n); |
103 | f = !strncmp(*l,key,n); |
104 | HSetState(l,hs); |
104 | HSetState(l,hs); |
105 | return f; |
105 | return f; |
106 | } |
106 | } |
107 | } |
107 | } |
108 | 108 | ||
109 | OSErr write_pstring(short rn,unsigned char *s){ |
109 | OSErr write_pstring(short rn,unsigned char *s){ |
110 | long c = *s; |
110 | long c = *s; |
111 | return FSWrite(rn,&c,s+1); |
111 | return FSWrite(rn,&c,s+1); |
112 | } |
112 | } |
113 | 113 | ||
114 | OSErr debug_err(char *s,OSErr e){ |
114 | OSErr debug_err(char *s,OSErr e){ |
115 | char t[0x100]; |
115 | char t[0x100]; |
116 | if(e){ |
116 | if(e){ |
117 | sprintf(t,"%s:%d",s,e); |
117 | sprintf(t,"%s:%d",s,e); |
118 | DebugStr(myc2pstr(t)); |
118 | DebugStr(myc2pstr(t)); |
119 | } |
119 | } |
120 | return e; |
120 | return e; |
121 | } |
121 | } |
122 | 122 | ||
123 | Boolean GZCritical(void){ // see IM vol. 1, memory manager chapter |
123 | Boolean GZCritical(void){ // see IM vol. 1, memory manager chapter |
124 | return !LMGetGZMoveHnd() || LMGetGZMoveHnd() == LMGetGZRootHnd(); |
124 | return !LMGetGZMoveHnd() || LMGetGZMoveHnd() == LMGetGZRootHnd(); |
125 | } |
125 | } |
126 | 126 | ||
127 | #if 0 //def __MWERKS__ // TrapAvailable is defined in MPW's fcntl.h (but not CW) |
127 | #if 0 //def __MWERKS__ // TrapAvailable is defined in MPW's fcntl.h (but not CW) |
128 | #define NumToolboxTraps() \ |
128 | #define NumToolboxTraps() \ |
129 | (NGetTrapAddress(_InitGraf,ToolTrap) == NGetTrapAddress(0xaa6e,ToolTrap) ? 0x200 : 0x400) |
129 | (NGetTrapAddress(_InitGraf,ToolTrap) == NGetTrapAddress(0xaa6e,ToolTrap) ? 0x200 : 0x400) |
130 | 130 | ||
131 | #define GetTrapType(theTrap) (theTrap & 0x800 ? ToolTrap : OSTrap) |
131 | #define GetTrapType(theTrap) (theTrap & 0x800 ? ToolTrap : OSTrap) |
132 | 132 | ||
133 | Boolean TrapAvailable(short theTrap){ |
133 | Boolean TrapAvailable(short theTrap){ |
134 | TrapType tType = GetTrapType(theTrap); |
134 | TrapType tType = GetTrapType(theTrap); |
135 | return (tType != ToolTrap || (theTrap &= 0x7ff) < NumToolboxTraps()) |
135 | return (tType != ToolTrap || (theTrap &= 0x7ff) < NumToolboxTraps()) |
136 | && NGetTrapAddress(theTrap,tType) != NGetTrapAddress(_Unimplemented,ToolTrap); |
136 | && NGetTrapAddress(theTrap,tType) != NGetTrapAddress(_Unimplemented,ToolTrap); |
137 | } |
137 | } |
138 | #endif |
138 | #endif |
139 | 139 | ||
140 | OSErr remove1resource(ResType t,short id){ OSErr e; Handle h; |
140 | OSErr remove1resource(ResType t,short id){ OSErr e; Handle h; |
141 | Boolean old_ResLoad = LMGetResLoad(); |
141 | Boolean old_ResLoad = LMGetResLoad(); |
142 | SetResLoad(false); |
142 | SetResLoad(false); |
143 | if(h = Get1Resource(t,id)) |
143 | if(h = Get1Resource(t,id)) |
144 | RemoveResource(h); |
144 | RemoveResource(h); |
145 | e = ResError(); |
145 | e = ResError(); |
146 | SetResLoad(old_ResLoad); |
146 | SetResLoad(old_ResLoad); |
147 | return e; |
147 | return e; |
148 | } |
148 | } |
149 | 149 | ||
150 | #pragma segment init |
150 | #pragma segment init |
151 | 151 | ||
152 | #if 0 // OBSOLETE |
152 | #if 0 // OBSOLETE |
153 | void open_app_files(void){ AppFile f; short c,m; FSSpec fss; |
153 | void open_app_files(void){ AppFile f; short c,m; FSSpec fss; |
154 | CountAppFiles(&m,&c); |
154 | CountAppFiles(&m,&c); |
155 | if(c) |
155 | if(c) |
156 | do{ |
156 | do{ |
157 | GetAppFiles(c,&f); |
157 | GetAppFiles(c,&f); |
158 | if(gestalt_attr(gestaltFSAttr,gestaltHasFSSpecCalls)) |
158 | if(gestalt_attr(gestaltFSAttr,gestaltHasFSSpecCalls)) |
159 | FSMakeFSSpec(f.vRefNum,0,f.fName,&fss); |
159 | FSMakeFSSpec(f.vRefNum,0,f.fName,&fss); |
160 | else{ // construct a fake FSSpec for open_doc |
160 | else{ // construct a fake FSSpec for open_doc |
161 | fss.vRefNum = f.vRefNum; |
161 | fss.vRefNum = f.vRefNum; |
162 | fss.parID = 0; |
162 | fss.parID = 0; |
163 | PLstrcpy(fss.name,f.fName); |
163 | PLstrcpy(fss.name,f.fName); |
164 | } |
164 | } |
165 | switch(m){ |
165 | switch(m){ |
166 | case appOpen: open_doc(&fss); break; |
166 | case appOpen: open_doc(&fss); break; |
167 | // case appPrint: print_doc(&fss); break; |
167 | // case appPrint: print_doc(&fss); break; |
168 | } |
168 | } |
169 | ClrAppFiles(c); |
169 | ClrAppFiles(c); |
170 | }while(--c); |
170 | }while(--c); |
171 | else |
171 | else |
172 | open_app(); |
172 | open_app(); |
173 | } |
173 | } |
174 | #endif |
174 | #endif |
175 | 175 | ||
176 | void init_toolbox(void){ |
176 | void init_toolbox(void){ |
177 | #if ! TARGET_API_MAC_CARBON |
177 | #if ! TARGET_API_MAC_CARBON |
178 | MaxApplZone(); |
178 | MaxApplZone(); |
179 | InitGraf(&qd.thePort); |
179 | InitGraf(&qd.thePort); |
180 | InitCursor(); |
180 | InitCursor(); |
181 | InitFonts(); |
181 | InitFonts(); |
182 | InitWindows(); |
182 | InitWindows(); |
183 | InitMenus(); |
183 | InitMenus(); |
184 | TEInit(); |
184 | TEInit(); |
185 | InitDialogs(0); |
185 | InitDialogs(0); |
186 | #endif |
186 | #endif |
187 | } |
187 | } |
188 | 188 | ||
189 | #if CALL_NOT_IN_CARBON |
189 | #if CALL_NOT_IN_CARBON |
190 | /* the following driver-opening technique, more or less, was recommended by |
190 | /* the following driver-opening technique, more or less, was recommended by |
191 | Tim Enwall in "Writing a device driver in C++," develop 4, p. 376Ð399 */ |
191 | Tim Enwall in "Writing a device driver in C++," develop 4, p. 376Ð399 */ |
192 | 192 | ||
193 | // locate the last free slot in the unit table |
193 | // locate the last free slot in the unit table |
194 | short find_slot(void); |
194 | short find_slot(void); |
195 | short find_slot(void){ short i; void **u; |
195 | short find_slot(void){ short i; void **u; |
196 | for(i = LMGetUnitTableEntryCount(),u = (void**)LMGetUTableBase() + i;i-- && *(--u);) |
196 | for(i = LMGetUnitTableEntryCount(),u = (void**)LMGetUTableBase() + i;i-- && *(--u);) |
197 | ; |
197 | ; |
198 | return i; // will return -1 if unit table is full |
198 | return i; // will return -1 if unit table is full |
199 | } |
199 | } |
200 | 200 | ||
201 | OSErr open_driver(StringPtr driver_name,short *rn){ |
201 | OSErr open_driver(StringPtr driver_name,short *rn){ |
202 | short id,theID; |
202 | short id,theID; |
203 | Handle h; |
203 | Handle h; |
204 | ResType theType; |
204 | ResType theType; |
205 | Str255 name; |
205 | Str255 name; |
206 | OSErr e; |
206 | OSErr e; |
207 | 207 | ||
208 | if((id = find_slot()) == -1) |
208 | if((id = find_slot()) == -1) |
209 | e = unitTblFullErr; |
209 | e = unitTblFullErr; |
210 | else{ |
210 | else{ |
211 | if(h = GetNamedResource('DRVR',driver_name)){ |
211 | if(h = GetNamedResource('DRVR',driver_name)){ |
212 | // change the resource ID so that the driver is loaded in the slot we want |
212 | // change the resource ID so that the driver is loaded in the slot we want |
213 | GetResInfo(h,&theID,&theType,name); |
213 | GetResInfo(h,&theID,&theType,name); |
214 | SetResInfo(h,id,0); |
214 | SetResInfo(h,id,0); |
215 | // won't work if application is not writeable! |
215 | // won't work if application is not writeable! |
216 | // (workaround could be to copy the driver into a temporary resource file |
216 | // (workaround could be to copy the driver into a temporary resource file |
217 | // with the desired ID, then open the driver...) |
217 | // with the desired ID, then open the driver...) |
218 | e = OpenDriver(driver_name,rn); |
218 | e = OpenDriver(driver_name,rn); |
219 | SetResInfo(h,theID,0); |
219 | SetResInfo(h,theID,0); |
220 | }else e = ResError(); |
220 | }else e = ResError(); |
221 | } |
221 | } |
222 | return e; |
222 | return e; |
223 | } |
223 | } |
224 | #endif |
224 | #endif |
225 | 225 | ||
226 | // store text of floating point number in a string, |
226 | // store text of floating point number in a string, |
227 | // to specified # of places |
227 | // to specified # of places |
228 | 228 | ||
229 | void flt(double_t x,char *s,int places){ |
229 | void flt(double_t x,char *s,int places){ |
230 | decform df; |
230 | decform df; |
231 | decimal d; |
231 | decimal d; |
232 | 232 | ||
233 | df.style = FIXEDDECIMAL; |
233 | df.style = FIXEDDECIMAL; |
234 | df.digits = places; |
234 | df.digits = places; |
235 | num2dec(&df,x,&d); |
235 | num2dec(&df,x,&d); |
236 | dec2str(&df,&d,s); |
236 | dec2str(&df,&d,s); |
237 | } |
237 | } |
238 | 238 | ||
239 | 239 |