Subversion Repositories filter_foundry

Rev

Rev 193 | Rev 268 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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