Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | |
| 26 | #include "Python.h" |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 27 | #include "pymactoolbox.h" |
| 28 | |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 29 | #ifdef WITHOUT_FRAMEWORKS |
Jack Jansen | d844a5f | 2001-08-08 15:28:03 +0000 | [diff] [blame] | 30 | #include <Script.h> |
| 31 | #include <Resources.h> |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | /* |
| 35 | ** Find out what the current script is. |
| 36 | ** Donated by Fredrik Lund. |
| 37 | */ |
| 38 | char *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 */ |
| 71 | char *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 */ |
| 92 | PyObject *PyMac_OSErrException; |
| 93 | |
| 94 | /* Initialize and return PyMac_OSErrException */ |
| 95 | PyObject * |
| 96 | PyMac_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 */ |
| 104 | PyObject * |
| 105 | PyErr_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 */ |
| 124 | PyObject * |
| 125 | PyMac_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 */ |
| 131 | int |
| 132 | PyMac_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 */ |
| 144 | PyObject * |
| 145 | PyMac_BuildOSType(OSType t) |
| 146 | { |
| 147 | return PyString_FromStringAndSize((char *)&t, 4); |
| 148 | } |
| 149 | |
| 150 | /* Convert an NumVersion value to a 4-element tuple */ |
| 151 | PyObject * |
| 152 | PyMac_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 */ |
| 159 | int |
| 160 | PyMac_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 */ |
| 174 | PyObject * |
| 175 | PyMac_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 | |
| 184 | PyObject * |
| 185 | PyMac_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). */ |
| 200 | int |
| 201 | PyMac_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 */ |
| 207 | PyObject * |
| 208 | PyMac_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). */ |
| 218 | int |
| 219 | PyMac_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 */ |
| 225 | PyObject * |
| 226 | PyMac_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. */ |
| 234 | int |
| 235 | PyMac_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 */ |
| 247 | PyObject * |
| 248 | PyMac_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 */ |
| 260 | int |
| 261 | PyMac_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 Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 271 | /* Convert a Fixed to a Python object */ |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 272 | PyObject * |
| 273 | PyMac_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 */ |
| 283 | int |
| 284 | PyMac_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 | |
| 297 | PyObject * |
| 298 | PyMac_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) \ |
| 323 | PyObject *(*PyMacGluePtr_##routinename)(object); \ |
| 324 | \ |
| 325 | PyObject *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) \ |
| 337 | int (*PyMacGluePtr_##routinename)(PyObject *, object *); \ |
| 338 | \ |
| 339 | int 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 | } |
| 349 | GLUE_CONVERT(FSSpec, PyMac_GetFSSpec, "macfs") |
| 350 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 351 | GLUE_NEW(AppleEvent *, AEDesc_New, "Carbon.AE") /* XXXX Why by address? */ |
| 352 | GLUE_CONVERT(AppleEvent, AEDesc_Convert, "Carbon.AE") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 353 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 354 | GLUE_NEW(Component, CmpObj_New, "Carbon.Cm") |
| 355 | GLUE_CONVERT(Component, CmpObj_Convert, "Carbon.Cm") |
| 356 | GLUE_NEW(ComponentInstance, CmpInstObj_New, "Carbon.Cm") |
| 357 | GLUE_CONVERT(ComponentInstance, CmpInstObj_Convert, "Carbon.Cm") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 358 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 359 | GLUE_NEW(ControlHandle, CtlObj_New, "Carbon.Ctl") |
| 360 | GLUE_CONVERT(ControlHandle, CtlObj_Convert, "Carbon.Ctl") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 361 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 362 | GLUE_NEW(DialogPtr, DlgObj_New, "Carbon.Dlg") |
| 363 | GLUE_CONVERT(DialogPtr, DlgObj_Convert, "Carbon.Dlg") |
| 364 | GLUE_NEW(DialogPtr, DlgObj_WhichDialog, "Carbon.Dlg") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 365 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 366 | GLUE_NEW(DragReference, DragObj_New, "Carbon.Drag") |
| 367 | GLUE_CONVERT(DragReference, DragObj_Convert, "Carbon.Drag") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 368 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 369 | GLUE_NEW(ListHandle, ListObj_New, "Carbon.List") |
| 370 | GLUE_CONVERT(ListHandle, ListObj_Convert, "Carbon.List") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 371 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 372 | GLUE_NEW(MenuHandle, MenuObj_New, "Carbon.Menu") |
| 373 | GLUE_CONVERT(MenuHandle, MenuObj_Convert, "Carbon.Menu") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 374 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 375 | GLUE_NEW(GrafPtr, GrafObj_New, "Carbon.Qd") |
| 376 | GLUE_CONVERT(GrafPtr, GrafObj_Convert, "Carbon.Qd") |
| 377 | GLUE_NEW(BitMapPtr, BMObj_New, "Carbon.Qd") |
| 378 | GLUE_CONVERT(BitMapPtr, BMObj_Convert, "Carbon.Qd") |
| 379 | GLUE_NEW(RGBColor *, QdRGB_New, "Carbon.Qd") /* XXXX Why? */ |
| 380 | GLUE_CONVERT(RGBColor, QdRGB_Convert, "Carbon.Qd") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 381 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 382 | GLUE_NEW(GWorldPtr, GWorldObj_New, "Carbon.Qdoffs") |
| 383 | GLUE_CONVERT(GWorldPtr, GWorldObj_Convert, "Carbon.Qdoffs") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 384 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 385 | GLUE_NEW(Track, TrackObj_New, "Carbon.Qt") |
| 386 | GLUE_CONVERT(Track, TrackObj_Convert, "Carbon.Qt") |
| 387 | GLUE_NEW(Movie, MovieObj_New, "Carbon.Qt") |
| 388 | GLUE_CONVERT(Movie, MovieObj_Convert, "Carbon.Qt") |
| 389 | GLUE_NEW(MovieController, MovieCtlObj_New, "Carbon.Qt") |
| 390 | GLUE_CONVERT(MovieController, MovieCtlObj_Convert, "Carbon.Qt") |
| 391 | GLUE_NEW(TimeBase, TimeBaseObj_New, "Carbon.Qt") |
| 392 | GLUE_CONVERT(TimeBase, TimeBaseObj_Convert, "Carbon.Qt") |
| 393 | GLUE_NEW(UserData, UserDataObj_New, "Carbon.Qt") |
| 394 | GLUE_CONVERT(UserData, UserDataObj_Convert, "Carbon.Qt") |
| 395 | GLUE_NEW(Media, MediaObj_New, "Carbon.Qt") |
| 396 | GLUE_CONVERT(Media, MediaObj_Convert, "Carbon.Qt") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 397 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 398 | GLUE_NEW(Handle, ResObj_New, "Carbon.Res") |
| 399 | GLUE_CONVERT(Handle, ResObj_Convert, "Carbon.Res") |
| 400 | GLUE_NEW(Handle, OptResObj_New, "Carbon.Res") |
| 401 | GLUE_CONVERT(Handle, OptResObj_Convert, "Carbon.Res") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 402 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 403 | GLUE_NEW(TEHandle, TEObj_New, "Carbon.TE") |
| 404 | GLUE_CONVERT(TEHandle, TEObj_Convert, "Carbon.TE") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 405 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame^] | 406 | GLUE_NEW(WindowPtr, WinObj_New, "Carbon.Win") |
| 407 | GLUE_CONVERT(WindowPtr, WinObj_Convert, "Carbon.Win") |
| 408 | GLUE_NEW(WindowPtr, WinObj_WhichWindow, "Carbon.Win") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 409 | |
| 410 | #endif /* USE_TOOLBOX_OBJECT_GLUE */ |