blob: 739d99ec3527e7bc23fa16d9acabd4b37f2f59da [file] [log] [blame]
Jack Jansen94bebc02001-08-08 13:17:31 +00001/***********************************************************
2Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25
26#include "Python.h"
Jack Jansen94bebc02001-08-08 13:17:31 +000027#include "pymactoolbox.h"
28
Jack Jansen94bebc02001-08-08 13:17:31 +000029#ifdef WITHOUT_FRAMEWORKS
Jack Jansend844a5f2001-08-08 15:28:03 +000030#include <Script.h>
31#include <Resources.h>
Jack Jansen94bebc02001-08-08 13:17:31 +000032#endif
33
34/*
35** Find out what the current script is.
36** Donated by Fredrik Lund.
37*/
38char *PyMac_getscript()
39{
40 int font, script, lang;
41 font = 0;
42 font = GetSysFont();
43 script = FontToScript(font);
44 switch (script) {
45 case smRoman:
46 lang = GetScriptVariable(script, smScriptLang);
47 if (lang == langIcelandic)
48 return "mac-iceland";
49 else if (lang == langTurkish)
50 return "mac-turkish";
51 else if (lang == langGreek)
52 return "mac-greek";
53 else
54 return "mac-roman";
55 break;
56#if 0
57 /* We don't have a codec for this, so don't return it */
58 case smJapanese:
59 return "mac-japan";
60#endif
61 case smGreek:
62 return "mac-greek";
63 case smCyrillic:
64 return "mac-cyrillic";
65 default:
66 return "ascii"; /* better than nothing */
67 }
68}
69
70/* Like strerror() but for Mac OS error numbers */
71char *PyMac_StrError(int err)
72{
73 static char buf[256];
74 Handle h;
75 char *str;
76
77 h = GetResource('Estr', err);
78 if ( h ) {
79 HLock(h);
80 str = (char *)*h;
81 memcpy(buf, str+1, (unsigned char)str[0]);
82 buf[(unsigned char)str[0]] = '\0';
83 HUnlock(h);
84 ReleaseResource(h);
85 } else {
86 sprintf(buf, "Mac OS error code %d", err);
87 }
88 return buf;
89}
90
91/* Exception object shared by all Mac specific modules for Mac OS errors */
92PyObject *PyMac_OSErrException;
93
94/* Initialize and return PyMac_OSErrException */
95PyObject *
96PyMac_GetOSErrException()
97{
98 if (PyMac_OSErrException == NULL)
99 PyMac_OSErrException = PyString_FromString("MacOS.Error");
100 return PyMac_OSErrException;
101}
102
103/* Set a MAC-specific error from errno, and return NULL; return None if no error */
104PyObject *
105PyErr_Mac(PyObject *eobj, int err)
106{
107 char *msg;
108 PyObject *v;
109
110 if (err == 0 && !PyErr_Occurred()) {
111 Py_INCREF(Py_None);
112 return Py_None;
113 }
114 if (err == -1 && PyErr_Occurred())
115 return NULL;
116 msg = PyMac_StrError(err);
117 v = Py_BuildValue("(is)", err, msg);
118 PyErr_SetObject(eobj, v);
119 Py_DECREF(v);
120 return NULL;
121}
122
123/* Call PyErr_Mac with PyMac_OSErrException */
124PyObject *
125PyMac_Error(OSErr err)
126{
127 return PyErr_Mac(PyMac_GetOSErrException(), err);
128}
129
130/* Convert a 4-char string object argument to an OSType value */
131int
132PyMac_GetOSType(PyObject *v, OSType *pr)
133{
134 if (!PyString_Check(v) || PyString_Size(v) != 4) {
135 PyErr_SetString(PyExc_TypeError,
136 "OSType arg must be string of 4 chars");
137 return 0;
138 }
139 memcpy((char *)pr, PyString_AsString(v), 4);
140 return 1;
141}
142
143/* Convert an OSType value to a 4-char string object */
144PyObject *
145PyMac_BuildOSType(OSType t)
146{
147 return PyString_FromStringAndSize((char *)&t, 4);
148}
149
150/* Convert an NumVersion value to a 4-element tuple */
151PyObject *
152PyMac_BuildNumVersion(NumVersion t)
153{
154 return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev);
155}
156
157
158/* Convert a Python string object to a Str255 */
159int
160PyMac_GetStr255(PyObject *v, Str255 pbuf)
161{
162 int len;
163 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
164 PyErr_SetString(PyExc_TypeError,
165 "Str255 arg must be string of at most 255 chars");
166 return 0;
167 }
168 pbuf[0] = len;
169 memcpy((char *)(pbuf+1), PyString_AsString(v), len);
170 return 1;
171}
172
173/* Convert a Str255 to a Python string object */
174PyObject *
175PyMac_BuildStr255(Str255 s)
176{
177 if ( s == NULL ) {
178 PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL");
179 return NULL;
180 }
181 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
182}
183
184PyObject *
185PyMac_BuildOptStr255(Str255 s)
186{
187 if ( s == NULL ) {
188 Py_INCREF(Py_None);
189 return Py_None;
190 }
191 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
192}
193
194
195
196/* Convert a Python object to a Rect.
197 The object must be a (left, top, right, bottom) tuple.
198 (This differs from the order in the struct but is consistent with
199 the arguments to SetRect(), and also with STDWIN). */
200int
201PyMac_GetRect(PyObject *v, Rect *r)
202{
203 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
204}
205
206/* Convert a Rect to a Python object */
207PyObject *
208PyMac_BuildRect(Rect *r)
209{
210 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
211}
212
213
214/* Convert a Python object to a Point.
215 The object must be a (h, v) tuple.
216 (This differs from the order in the struct but is consistent with
217 the arguments to SetPoint(), and also with STDWIN). */
218int
219PyMac_GetPoint(PyObject *v, Point *p)
220{
221 return PyArg_Parse(v, "(hh)", &p->h, &p->v);
222}
223
224/* Convert a Point to a Python object */
225PyObject *
226PyMac_BuildPoint(Point p)
227{
228 return Py_BuildValue("(hh)", p.h, p.v);
229}
230
231
232/* Convert a Python object to an EventRecord.
233 The object must be a (what, message, when, (v, h), modifiers) tuple. */
234int
235PyMac_GetEventRecord(PyObject *v, EventRecord *e)
236{
237 return PyArg_Parse(v, "(Hll(hh)H)",
238 &e->what,
239 &e->message,
240 &e->when,
241 &e->where.h,
242 &e->where.v,
243 &e->modifiers);
244}
245
246/* Convert a Rect to an EventRecord object */
247PyObject *
248PyMac_BuildEventRecord(EventRecord *e)
249{
250 return Py_BuildValue("(hll(hh)h)",
251 e->what,
252 e->message,
253 e->when,
254 e->where.h,
255 e->where.v,
256 e->modifiers);
257}
258
259/* Convert Python object to Fixed */
260int
261PyMac_GetFixed(PyObject *v, Fixed *f)
262{
263 double d;
264
265 if( !PyArg_Parse(v, "d", &d))
266 return 0;
267 *f = (Fixed)(d * 0x10000);
268 return 1;
269}
270
Jack Jansen06bd3232001-08-27 14:01:05 +0000271/* Convert a Fixed to a Python object */
Jack Jansen94bebc02001-08-08 13:17:31 +0000272PyObject *
273PyMac_BuildFixed(Fixed f)
274{
275 double d;
276
277 d = f;
278 d = d / 0x10000;
279 return Py_BuildValue("d", d);
280}
281
282/* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */
283int
284PyMac_Getwide(PyObject *v, wide *rv)
285{
286 if (PyInt_Check(v)) {
287 rv->hi = 0;
288 rv->lo = PyInt_AsLong(v);
289 if( rv->lo & 0x80000000 )
290 rv->hi = -1;
291 return 1;
292 }
293 return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo);
294}
295
296
297PyObject *
298PyMac_Buildwide(wide *w)
299{
300 if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) ||
301 (w->hi == -1 && (w->lo & 0x80000000) ) )
302 return PyInt_FromLong(w->lo);
303 return Py_BuildValue("(ll)", w->hi, w->lo);
304}
305
306#ifdef USE_TOOLBOX_OBJECT_GLUE
307/*
308** Glue together the toolbox objects.
309**
310** Because toolbox modules interdepend on each other, they use each others
311** object types, on MacOSX/MachO this leads to the situation that they
312** cannot be dynamically loaded (or they would all have to be lumped into
313** a single .so, but this would be bad for extensibility).
314**
315** This file defines wrappers for all the _New and _Convert functions,
316** which are the Py_BuildValue and PyArg_ParseTuple helpers. The wrappers
317** check an indirection function pointer, and if it isn't filled in yet
318** they import the appropriate module, whose init routine should fill in
319** the pointer.
320*/
321
322#define GLUE_NEW(object, routinename, module) \
323PyObject *(*PyMacGluePtr_##routinename)(object); \
324\
325PyObject *routinename(object cobj) { \
326 if (!PyMacGluePtr_##routinename) { \
327 if (!PyImport_ImportModule(module)) return NULL; \
328 if (!PyMacGluePtr_##routinename) { \
329 PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \
330 return NULL; \
331 } \
332 } \
333 return (*PyMacGluePtr_##routinename)(cobj); \
334}
335
336#define GLUE_CONVERT(object, routinename, module) \
337int (*PyMacGluePtr_##routinename)(PyObject *, object *); \
338\
339int routinename(PyObject *pyobj, object *cobj) { \
340 if (!PyMacGluePtr_##routinename) { \
341 if (!PyImport_ImportModule(module)) return NULL; \
342 if (!PyMacGluePtr_##routinename) { \
343 PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \
344 return NULL; \
345 } \
346 } \
347 return (*PyMacGluePtr_##routinename)(pyobj, cobj); \
348}
349GLUE_CONVERT(FSSpec, PyMac_GetFSSpec, "macfs")
350
Jack Jansen06bd3232001-08-27 14:01:05 +0000351GLUE_NEW(AppleEvent *, AEDesc_New, "Carbon.AE") /* XXXX Why by address? */
352GLUE_CONVERT(AppleEvent, AEDesc_Convert, "Carbon.AE")
Jack Jansen94bebc02001-08-08 13:17:31 +0000353
Jack Jansen06bd3232001-08-27 14:01:05 +0000354GLUE_NEW(Component, CmpObj_New, "Carbon.Cm")
355GLUE_CONVERT(Component, CmpObj_Convert, "Carbon.Cm")
356GLUE_NEW(ComponentInstance, CmpInstObj_New, "Carbon.Cm")
357GLUE_CONVERT(ComponentInstance, CmpInstObj_Convert, "Carbon.Cm")
Jack Jansen94bebc02001-08-08 13:17:31 +0000358
Jack Jansen06bd3232001-08-27 14:01:05 +0000359GLUE_NEW(ControlHandle, CtlObj_New, "Carbon.Ctl")
360GLUE_CONVERT(ControlHandle, CtlObj_Convert, "Carbon.Ctl")
Jack Jansen94bebc02001-08-08 13:17:31 +0000361
Jack Jansen06bd3232001-08-27 14:01:05 +0000362GLUE_NEW(DialogPtr, DlgObj_New, "Carbon.Dlg")
363GLUE_CONVERT(DialogPtr, DlgObj_Convert, "Carbon.Dlg")
364GLUE_NEW(DialogPtr, DlgObj_WhichDialog, "Carbon.Dlg")
Jack Jansen94bebc02001-08-08 13:17:31 +0000365
Jack Jansen06bd3232001-08-27 14:01:05 +0000366GLUE_NEW(DragReference, DragObj_New, "Carbon.Drag")
367GLUE_CONVERT(DragReference, DragObj_Convert, "Carbon.Drag")
Jack Jansen94bebc02001-08-08 13:17:31 +0000368
Jack Jansen06bd3232001-08-27 14:01:05 +0000369GLUE_NEW(ListHandle, ListObj_New, "Carbon.List")
370GLUE_CONVERT(ListHandle, ListObj_Convert, "Carbon.List")
Jack Jansen94bebc02001-08-08 13:17:31 +0000371
Jack Jansen06bd3232001-08-27 14:01:05 +0000372GLUE_NEW(MenuHandle, MenuObj_New, "Carbon.Menu")
373GLUE_CONVERT(MenuHandle, MenuObj_Convert, "Carbon.Menu")
Jack Jansen94bebc02001-08-08 13:17:31 +0000374
Jack Jansen06bd3232001-08-27 14:01:05 +0000375GLUE_NEW(GrafPtr, GrafObj_New, "Carbon.Qd")
376GLUE_CONVERT(GrafPtr, GrafObj_Convert, "Carbon.Qd")
377GLUE_NEW(BitMapPtr, BMObj_New, "Carbon.Qd")
378GLUE_CONVERT(BitMapPtr, BMObj_Convert, "Carbon.Qd")
379GLUE_NEW(RGBColor *, QdRGB_New, "Carbon.Qd") /* XXXX Why? */
380GLUE_CONVERT(RGBColor, QdRGB_Convert, "Carbon.Qd")
Jack Jansen94bebc02001-08-08 13:17:31 +0000381
Jack Jansen06bd3232001-08-27 14:01:05 +0000382GLUE_NEW(GWorldPtr, GWorldObj_New, "Carbon.Qdoffs")
383GLUE_CONVERT(GWorldPtr, GWorldObj_Convert, "Carbon.Qdoffs")
Jack Jansen94bebc02001-08-08 13:17:31 +0000384
Jack Jansen06bd3232001-08-27 14:01:05 +0000385GLUE_NEW(Track, TrackObj_New, "Carbon.Qt")
386GLUE_CONVERT(Track, TrackObj_Convert, "Carbon.Qt")
387GLUE_NEW(Movie, MovieObj_New, "Carbon.Qt")
388GLUE_CONVERT(Movie, MovieObj_Convert, "Carbon.Qt")
389GLUE_NEW(MovieController, MovieCtlObj_New, "Carbon.Qt")
390GLUE_CONVERT(MovieController, MovieCtlObj_Convert, "Carbon.Qt")
391GLUE_NEW(TimeBase, TimeBaseObj_New, "Carbon.Qt")
392GLUE_CONVERT(TimeBase, TimeBaseObj_Convert, "Carbon.Qt")
393GLUE_NEW(UserData, UserDataObj_New, "Carbon.Qt")
394GLUE_CONVERT(UserData, UserDataObj_Convert, "Carbon.Qt")
395GLUE_NEW(Media, MediaObj_New, "Carbon.Qt")
396GLUE_CONVERT(Media, MediaObj_Convert, "Carbon.Qt")
Jack Jansen94bebc02001-08-08 13:17:31 +0000397
Jack Jansen06bd3232001-08-27 14:01:05 +0000398GLUE_NEW(Handle, ResObj_New, "Carbon.Res")
399GLUE_CONVERT(Handle, ResObj_Convert, "Carbon.Res")
400GLUE_NEW(Handle, OptResObj_New, "Carbon.Res")
401GLUE_CONVERT(Handle, OptResObj_Convert, "Carbon.Res")
Jack Jansen94bebc02001-08-08 13:17:31 +0000402
Jack Jansen06bd3232001-08-27 14:01:05 +0000403GLUE_NEW(TEHandle, TEObj_New, "Carbon.TE")
404GLUE_CONVERT(TEHandle, TEObj_Convert, "Carbon.TE")
Jack Jansen94bebc02001-08-08 13:17:31 +0000405
Jack Jansen06bd3232001-08-27 14:01:05 +0000406GLUE_NEW(WindowPtr, WinObj_New, "Carbon.Win")
407GLUE_CONVERT(WindowPtr, WinObj_Convert, "Carbon.Win")
408GLUE_NEW(WindowPtr, WinObj_WhichWindow, "Carbon.Win")
Jack Jansen94bebc02001-08-08 13:17:31 +0000409
410#endif /* USE_TOOLBOX_OBJECT_GLUE */