Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1 | |
| 2 | /* =========================== Module Res =========================== */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
| 6 | |
| 7 | |
| 8 | #define SystemSevenOrLater 1 |
| 9 | |
| 10 | #include "macglue.h" |
| 11 | #include <Memory.h> |
| 12 | #include <Dialogs.h> |
| 13 | #include <Menus.h> |
| 14 | #include <Controls.h> |
| 15 | |
| 16 | extern PyObject *ResObj_New(Handle); |
| 17 | extern int ResObj_Convert(PyObject *, Handle *); |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 18 | extern PyObject *OptResObj_New(Handle); |
| 19 | extern int OptResObj_Convert(PyObject *, Handle *); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 20 | |
| 21 | extern PyObject *WinObj_New(WindowPtr); |
| 22 | extern int WinObj_Convert(PyObject *, WindowPtr *); |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 23 | extern PyTypeObject Window_Type; |
| 24 | #define WinObj_Check(x) ((x)->ob_type == &Window_Type) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 25 | |
| 26 | extern PyObject *DlgObj_New(DialogPtr); |
| 27 | extern int DlgObj_Convert(PyObject *, DialogPtr *); |
| 28 | extern PyTypeObject Dialog_Type; |
| 29 | #define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type) |
| 30 | |
| 31 | extern PyObject *MenuObj_New(MenuHandle); |
| 32 | extern int MenuObj_Convert(PyObject *, MenuHandle *); |
| 33 | |
| 34 | extern PyObject *CtlObj_New(ControlHandle); |
| 35 | extern int CtlObj_Convert(PyObject *, ControlHandle *); |
| 36 | |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 37 | extern PyObject *GrafObj_New(GrafPtr); |
| 38 | extern int GrafObj_Convert(PyObject *, GrafPtr *); |
| 39 | |
| 40 | extern PyObject *BMObj_New(BitMapPtr); |
| 41 | extern int BMObj_Convert(PyObject *, BitMapPtr *); |
| 42 | |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 43 | extern PyObject *WinObj_WhichWindow(WindowPtr); |
| 44 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 45 | #include <Resources.h> |
Jack Jansen | eaf3c9b | 1997-08-15 14:36:45 +0000 | [diff] [blame] | 46 | #include <string.h> |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 47 | |
| 48 | #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */ |
| 49 | |
| 50 | static PyObject *Res_Error; |
| 51 | |
| 52 | /* ---------------------- Object type Resource ---------------------- */ |
| 53 | |
| 54 | PyTypeObject Resource_Type; |
| 55 | |
| 56 | #define ResObj_Check(x) ((x)->ob_type == &Resource_Type) |
| 57 | |
| 58 | typedef struct ResourceObject { |
| 59 | PyObject_HEAD |
| 60 | Handle ob_itself; |
| 61 | } ResourceObject; |
| 62 | |
| 63 | PyObject *ResObj_New(itself) |
Guido van Rossum | 227a423 | 1995-03-10 14:42:57 +0000 | [diff] [blame] | 64 | Handle itself; |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 65 | { |
| 66 | ResourceObject *it; |
| 67 | if (itself == NULL) return PyMac_Error(resNotFound); |
| 68 | it = PyObject_NEW(ResourceObject, &Resource_Type); |
| 69 | if (it == NULL) return NULL; |
| 70 | it->ob_itself = itself; |
| 71 | return (PyObject *)it; |
| 72 | } |
| 73 | ResObj_Convert(v, p_itself) |
| 74 | PyObject *v; |
| 75 | Handle *p_itself; |
| 76 | { |
| 77 | if (!ResObj_Check(v)) |
| 78 | { |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 79 | PyObject *tmp; |
| 80 | if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) |
| 81 | { |
| 82 | *p_itself = ((ResourceObject *)tmp)->ob_itself; |
| 83 | Py_DECREF(tmp); |
| 84 | return 1; |
| 85 | } |
| 86 | PyErr_Clear(); |
| 87 | } |
| 88 | if (!ResObj_Check(v)) |
| 89 | { |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 90 | PyErr_SetString(PyExc_TypeError, "Resource required"); |
| 91 | return 0; |
| 92 | } |
| 93 | *p_itself = ((ResourceObject *)v)->ob_itself; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static void ResObj_dealloc(self) |
| 98 | ResourceObject *self; |
| 99 | { |
| 100 | /* Cleanup of self->ob_itself goes here */ |
| 101 | PyMem_DEL(self); |
| 102 | } |
| 103 | |
| 104 | static PyObject *ResObj_HomeResFile(_self, _args) |
| 105 | ResourceObject *_self; |
| 106 | PyObject *_args; |
| 107 | { |
| 108 | PyObject *_res = NULL; |
| 109 | short _rv; |
| 110 | if (!PyArg_ParseTuple(_args, "")) |
| 111 | return NULL; |
| 112 | _rv = HomeResFile(_self->ob_itself); |
| 113 | { |
| 114 | OSErr _err = ResError(); |
| 115 | if (_err != noErr) return PyMac_Error(_err); |
| 116 | } |
| 117 | _res = Py_BuildValue("h", |
| 118 | _rv); |
| 119 | return _res; |
| 120 | } |
| 121 | |
Jack Jansen | 1c4e614 | 1998-04-21 15:23:55 +0000 | [diff] [blame] | 122 | static PyObject *ResObj_MacLoadResource(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 123 | ResourceObject *_self; |
| 124 | PyObject *_args; |
| 125 | { |
| 126 | PyObject *_res = NULL; |
| 127 | if (!PyArg_ParseTuple(_args, "")) |
| 128 | return NULL; |
Jack Jansen | 1c4e614 | 1998-04-21 15:23:55 +0000 | [diff] [blame] | 129 | MacLoadResource(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 130 | { |
| 131 | OSErr _err = ResError(); |
| 132 | if (_err != noErr) return PyMac_Error(_err); |
| 133 | } |
| 134 | Py_INCREF(Py_None); |
| 135 | _res = Py_None; |
| 136 | return _res; |
| 137 | } |
| 138 | |
| 139 | static PyObject *ResObj_ReleaseResource(_self, _args) |
| 140 | ResourceObject *_self; |
| 141 | PyObject *_args; |
| 142 | { |
| 143 | PyObject *_res = NULL; |
| 144 | if (!PyArg_ParseTuple(_args, "")) |
| 145 | return NULL; |
| 146 | ReleaseResource(_self->ob_itself); |
| 147 | { |
| 148 | OSErr _err = ResError(); |
| 149 | if (_err != noErr) return PyMac_Error(_err); |
| 150 | } |
| 151 | Py_INCREF(Py_None); |
| 152 | _res = Py_None; |
| 153 | return _res; |
| 154 | } |
| 155 | |
| 156 | static PyObject *ResObj_DetachResource(_self, _args) |
| 157 | ResourceObject *_self; |
| 158 | PyObject *_args; |
| 159 | { |
| 160 | PyObject *_res = NULL; |
| 161 | if (!PyArg_ParseTuple(_args, "")) |
| 162 | return NULL; |
| 163 | DetachResource(_self->ob_itself); |
| 164 | { |
| 165 | OSErr _err = ResError(); |
| 166 | if (_err != noErr) return PyMac_Error(_err); |
| 167 | } |
| 168 | Py_INCREF(Py_None); |
| 169 | _res = Py_None; |
| 170 | return _res; |
| 171 | } |
| 172 | |
| 173 | static PyObject *ResObj_GetResAttrs(_self, _args) |
| 174 | ResourceObject *_self; |
| 175 | PyObject *_args; |
| 176 | { |
| 177 | PyObject *_res = NULL; |
| 178 | short _rv; |
| 179 | if (!PyArg_ParseTuple(_args, "")) |
| 180 | return NULL; |
| 181 | _rv = GetResAttrs(_self->ob_itself); |
| 182 | { |
| 183 | OSErr _err = ResError(); |
| 184 | if (_err != noErr) return PyMac_Error(_err); |
| 185 | } |
| 186 | _res = Py_BuildValue("h", |
| 187 | _rv); |
| 188 | return _res; |
| 189 | } |
| 190 | |
| 191 | static PyObject *ResObj_GetResInfo(_self, _args) |
| 192 | ResourceObject *_self; |
| 193 | PyObject *_args; |
| 194 | { |
| 195 | PyObject *_res = NULL; |
| 196 | short theID; |
| 197 | ResType theType; |
| 198 | Str255 name; |
| 199 | if (!PyArg_ParseTuple(_args, "")) |
| 200 | return NULL; |
| 201 | GetResInfo(_self->ob_itself, |
| 202 | &theID, |
| 203 | &theType, |
| 204 | name); |
| 205 | { |
| 206 | OSErr _err = ResError(); |
| 207 | if (_err != noErr) return PyMac_Error(_err); |
| 208 | } |
| 209 | _res = Py_BuildValue("hO&O&", |
| 210 | theID, |
| 211 | PyMac_BuildOSType, theType, |
| 212 | PyMac_BuildStr255, name); |
| 213 | return _res; |
| 214 | } |
| 215 | |
| 216 | static PyObject *ResObj_SetResInfo(_self, _args) |
| 217 | ResourceObject *_self; |
| 218 | PyObject *_args; |
| 219 | { |
| 220 | PyObject *_res = NULL; |
| 221 | short theID; |
| 222 | Str255 name; |
| 223 | if (!PyArg_ParseTuple(_args, "hO&", |
| 224 | &theID, |
| 225 | PyMac_GetStr255, name)) |
| 226 | return NULL; |
| 227 | SetResInfo(_self->ob_itself, |
| 228 | theID, |
| 229 | name); |
| 230 | { |
| 231 | OSErr _err = ResError(); |
| 232 | if (_err != noErr) return PyMac_Error(_err); |
| 233 | } |
| 234 | Py_INCREF(Py_None); |
| 235 | _res = Py_None; |
| 236 | return _res; |
| 237 | } |
| 238 | |
| 239 | static PyObject *ResObj_AddResource(_self, _args) |
| 240 | ResourceObject *_self; |
| 241 | PyObject *_args; |
| 242 | { |
| 243 | PyObject *_res = NULL; |
| 244 | ResType theType; |
| 245 | short theID; |
| 246 | Str255 name; |
| 247 | if (!PyArg_ParseTuple(_args, "O&hO&", |
| 248 | PyMac_GetOSType, &theType, |
| 249 | &theID, |
| 250 | PyMac_GetStr255, name)) |
| 251 | return NULL; |
| 252 | AddResource(_self->ob_itself, |
| 253 | theType, |
| 254 | theID, |
| 255 | name); |
| 256 | { |
| 257 | OSErr _err = ResError(); |
| 258 | if (_err != noErr) return PyMac_Error(_err); |
| 259 | } |
| 260 | Py_INCREF(Py_None); |
| 261 | _res = Py_None; |
| 262 | return _res; |
| 263 | } |
| 264 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 265 | static PyObject *ResObj_GetResourceSizeOnDisk(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 266 | ResourceObject *_self; |
| 267 | PyObject *_args; |
| 268 | { |
| 269 | PyObject *_res = NULL; |
| 270 | long _rv; |
| 271 | if (!PyArg_ParseTuple(_args, "")) |
| 272 | return NULL; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 273 | _rv = GetResourceSizeOnDisk(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 274 | { |
| 275 | OSErr _err = ResError(); |
| 276 | if (_err != noErr) return PyMac_Error(_err); |
| 277 | } |
| 278 | _res = Py_BuildValue("l", |
| 279 | _rv); |
| 280 | return _res; |
| 281 | } |
| 282 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 283 | static PyObject *ResObj_GetMaxResourceSize(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 284 | ResourceObject *_self; |
| 285 | PyObject *_args; |
| 286 | { |
| 287 | PyObject *_res = NULL; |
| 288 | long _rv; |
| 289 | if (!PyArg_ParseTuple(_args, "")) |
| 290 | return NULL; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 291 | _rv = GetMaxResourceSize(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 292 | { |
| 293 | OSErr _err = ResError(); |
| 294 | if (_err != noErr) return PyMac_Error(_err); |
| 295 | } |
| 296 | _res = Py_BuildValue("l", |
| 297 | _rv); |
| 298 | return _res; |
| 299 | } |
| 300 | |
| 301 | static PyObject *ResObj_RsrcMapEntry(_self, _args) |
| 302 | ResourceObject *_self; |
| 303 | PyObject *_args; |
| 304 | { |
| 305 | PyObject *_res = NULL; |
| 306 | long _rv; |
| 307 | if (!PyArg_ParseTuple(_args, "")) |
| 308 | return NULL; |
| 309 | _rv = RsrcMapEntry(_self->ob_itself); |
| 310 | { |
| 311 | OSErr _err = ResError(); |
| 312 | if (_err != noErr) return PyMac_Error(_err); |
| 313 | } |
| 314 | _res = Py_BuildValue("l", |
| 315 | _rv); |
| 316 | return _res; |
| 317 | } |
| 318 | |
| 319 | static PyObject *ResObj_SetResAttrs(_self, _args) |
| 320 | ResourceObject *_self; |
| 321 | PyObject *_args; |
| 322 | { |
| 323 | PyObject *_res = NULL; |
| 324 | short attrs; |
| 325 | if (!PyArg_ParseTuple(_args, "h", |
| 326 | &attrs)) |
| 327 | return NULL; |
| 328 | SetResAttrs(_self->ob_itself, |
| 329 | attrs); |
| 330 | { |
| 331 | OSErr _err = ResError(); |
| 332 | if (_err != noErr) return PyMac_Error(_err); |
| 333 | } |
| 334 | Py_INCREF(Py_None); |
| 335 | _res = Py_None; |
| 336 | return _res; |
| 337 | } |
| 338 | |
| 339 | static PyObject *ResObj_ChangedResource(_self, _args) |
| 340 | ResourceObject *_self; |
| 341 | PyObject *_args; |
| 342 | { |
| 343 | PyObject *_res = NULL; |
| 344 | if (!PyArg_ParseTuple(_args, "")) |
| 345 | return NULL; |
| 346 | ChangedResource(_self->ob_itself); |
| 347 | { |
| 348 | OSErr _err = ResError(); |
| 349 | if (_err != noErr) return PyMac_Error(_err); |
| 350 | } |
| 351 | Py_INCREF(Py_None); |
| 352 | _res = Py_None; |
| 353 | return _res; |
| 354 | } |
| 355 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 356 | static PyObject *ResObj_RemoveResource(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 357 | ResourceObject *_self; |
| 358 | PyObject *_args; |
| 359 | { |
| 360 | PyObject *_res = NULL; |
| 361 | if (!PyArg_ParseTuple(_args, "")) |
| 362 | return NULL; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 363 | RemoveResource(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 364 | { |
| 365 | OSErr _err = ResError(); |
| 366 | if (_err != noErr) return PyMac_Error(_err); |
| 367 | } |
| 368 | Py_INCREF(Py_None); |
| 369 | _res = Py_None; |
| 370 | return _res; |
| 371 | } |
| 372 | |
| 373 | static PyObject *ResObj_WriteResource(_self, _args) |
| 374 | ResourceObject *_self; |
| 375 | PyObject *_args; |
| 376 | { |
| 377 | PyObject *_res = NULL; |
| 378 | if (!PyArg_ParseTuple(_args, "")) |
| 379 | return NULL; |
| 380 | WriteResource(_self->ob_itself); |
| 381 | { |
| 382 | OSErr _err = ResError(); |
| 383 | if (_err != noErr) return PyMac_Error(_err); |
| 384 | } |
| 385 | Py_INCREF(Py_None); |
| 386 | _res = Py_None; |
| 387 | return _res; |
| 388 | } |
| 389 | |
| 390 | static PyObject *ResObj_SetResourceSize(_self, _args) |
| 391 | ResourceObject *_self; |
| 392 | PyObject *_args; |
| 393 | { |
| 394 | PyObject *_res = NULL; |
| 395 | long newSize; |
| 396 | if (!PyArg_ParseTuple(_args, "l", |
| 397 | &newSize)) |
| 398 | return NULL; |
| 399 | SetResourceSize(_self->ob_itself, |
| 400 | newSize); |
| 401 | { |
| 402 | OSErr _err = ResError(); |
| 403 | if (_err != noErr) return PyMac_Error(_err); |
| 404 | } |
| 405 | Py_INCREF(Py_None); |
| 406 | _res = Py_None; |
| 407 | return _res; |
| 408 | } |
| 409 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 410 | static PyObject *ResObj_GetNextFOND(_self, _args) |
| 411 | ResourceObject *_self; |
| 412 | PyObject *_args; |
| 413 | { |
| 414 | PyObject *_res = NULL; |
| 415 | Handle _rv; |
| 416 | if (!PyArg_ParseTuple(_args, "")) |
| 417 | return NULL; |
| 418 | _rv = GetNextFOND(_self->ob_itself); |
| 419 | { |
| 420 | OSErr _err = ResError(); |
| 421 | if (_err != noErr) return PyMac_Error(_err); |
| 422 | } |
| 423 | _res = Py_BuildValue("O&", |
| 424 | ResObj_New, _rv); |
| 425 | return _res; |
| 426 | } |
| 427 | |
Jack Jansen | 1e05402 | 1995-06-18 20:20:27 +0000 | [diff] [blame] | 428 | static PyObject *ResObj_as_Control(_self, _args) |
| 429 | ResourceObject *_self; |
| 430 | PyObject *_args; |
| 431 | { |
| 432 | PyObject *_res = NULL; |
| 433 | |
| 434 | return CtlObj_New((ControlHandle)_self->ob_itself); |
| 435 | |
| 436 | } |
| 437 | |
| 438 | static PyObject *ResObj_as_Menu(_self, _args) |
| 439 | ResourceObject *_self; |
| 440 | PyObject *_args; |
| 441 | { |
| 442 | PyObject *_res = NULL; |
| 443 | |
| 444 | return MenuObj_New((MenuHandle)_self->ob_itself); |
| 445 | |
| 446 | } |
| 447 | |
Jack Jansen | e180d99 | 1998-04-24 10:28:20 +0000 | [diff] [blame] | 448 | static PyObject *ResObj_LoadResource(_self, _args) |
| 449 | ResourceObject *_self; |
| 450 | PyObject *_args; |
| 451 | { |
| 452 | PyObject *_res = NULL; |
| 453 | if (!PyArg_ParseTuple(_args, "")) |
| 454 | return NULL; |
| 455 | LoadResource(_self->ob_itself); |
| 456 | { |
| 457 | OSErr _err = ResError(); |
| 458 | if (_err != noErr) return PyMac_Error(_err); |
| 459 | } |
| 460 | Py_INCREF(Py_None); |
| 461 | _res = Py_None; |
| 462 | return _res; |
| 463 | } |
| 464 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 465 | static PyMethodDef ResObj_methods[] = { |
| 466 | {"HomeResFile", (PyCFunction)ResObj_HomeResFile, 1, |
| 467 | "() -> (short _rv)"}, |
Jack Jansen | 1c4e614 | 1998-04-21 15:23:55 +0000 | [diff] [blame] | 468 | {"MacLoadResource", (PyCFunction)ResObj_MacLoadResource, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 469 | "() -> None"}, |
| 470 | {"ReleaseResource", (PyCFunction)ResObj_ReleaseResource, 1, |
| 471 | "() -> None"}, |
| 472 | {"DetachResource", (PyCFunction)ResObj_DetachResource, 1, |
| 473 | "() -> None"}, |
| 474 | {"GetResAttrs", (PyCFunction)ResObj_GetResAttrs, 1, |
| 475 | "() -> (short _rv)"}, |
| 476 | {"GetResInfo", (PyCFunction)ResObj_GetResInfo, 1, |
| 477 | "() -> (short theID, ResType theType, Str255 name)"}, |
| 478 | {"SetResInfo", (PyCFunction)ResObj_SetResInfo, 1, |
| 479 | "(short theID, Str255 name) -> None"}, |
| 480 | {"AddResource", (PyCFunction)ResObj_AddResource, 1, |
| 481 | "(ResType theType, short theID, Str255 name) -> None"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 482 | {"GetResourceSizeOnDisk", (PyCFunction)ResObj_GetResourceSizeOnDisk, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 483 | "() -> (long _rv)"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 484 | {"GetMaxResourceSize", (PyCFunction)ResObj_GetMaxResourceSize, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 485 | "() -> (long _rv)"}, |
| 486 | {"RsrcMapEntry", (PyCFunction)ResObj_RsrcMapEntry, 1, |
| 487 | "() -> (long _rv)"}, |
| 488 | {"SetResAttrs", (PyCFunction)ResObj_SetResAttrs, 1, |
| 489 | "(short attrs) -> None"}, |
| 490 | {"ChangedResource", (PyCFunction)ResObj_ChangedResource, 1, |
| 491 | "() -> None"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 492 | {"RemoveResource", (PyCFunction)ResObj_RemoveResource, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 493 | "() -> None"}, |
| 494 | {"WriteResource", (PyCFunction)ResObj_WriteResource, 1, |
| 495 | "() -> None"}, |
| 496 | {"SetResourceSize", (PyCFunction)ResObj_SetResourceSize, 1, |
| 497 | "(long newSize) -> None"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 498 | {"GetNextFOND", (PyCFunction)ResObj_GetNextFOND, 1, |
| 499 | "() -> (Handle _rv)"}, |
Jack Jansen | 1e05402 | 1995-06-18 20:20:27 +0000 | [diff] [blame] | 500 | {"as_Control", (PyCFunction)ResObj_as_Control, 1, |
| 501 | "Return this resource/handle as a Control"}, |
| 502 | {"as_Menu", (PyCFunction)ResObj_as_Menu, 1, |
| 503 | "Return this resource/handle as a Menu"}, |
Jack Jansen | e180d99 | 1998-04-24 10:28:20 +0000 | [diff] [blame] | 504 | {"LoadResource", (PyCFunction)ResObj_LoadResource, 1, |
| 505 | "() -> None"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 506 | {NULL, NULL, 0} |
| 507 | }; |
| 508 | |
| 509 | PyMethodChain ResObj_chain = { ResObj_methods, NULL }; |
| 510 | |
| 511 | static PyObject *ResObj_getattr(self, name) |
| 512 | ResourceObject *self; |
| 513 | char *name; |
| 514 | { |
| 515 | |
| 516 | if (strcmp(name, "size") == 0) |
| 517 | return PyInt_FromLong(GetHandleSize(self->ob_itself)); |
| 518 | if (strcmp(name, "data") == 0) { |
| 519 | PyObject *res; |
| 520 | char state; |
| 521 | state = HGetState(self->ob_itself); |
| 522 | HLock(self->ob_itself); |
| 523 | res = PyString_FromStringAndSize( |
| 524 | *self->ob_itself, |
| 525 | GetHandleSize(self->ob_itself)); |
| 526 | HUnlock(self->ob_itself); |
| 527 | HSetState(self->ob_itself, state); |
| 528 | return res; |
| 529 | } |
| 530 | if (strcmp(name, "__members__") == 0) |
| 531 | return Py_BuildValue("[ss]", "data", "size"); |
| 532 | |
| 533 | return Py_FindMethodInChain(&ResObj_chain, (PyObject *)self, name); |
| 534 | } |
| 535 | |
Jack Jansen | 1e05402 | 1995-06-18 20:20:27 +0000 | [diff] [blame] | 536 | static int |
| 537 | ResObj_setattr(self, name, value) |
| 538 | ResourceObject *self; |
| 539 | char *name; |
| 540 | PyObject *value; |
| 541 | { |
| 542 | char *data; |
| 543 | long size; |
| 544 | |
| 545 | if (strcmp(name, "data") != 0 || value == NULL ) |
| 546 | return -1; |
| 547 | if ( !PyString_Check(value) ) |
| 548 | return -1; |
| 549 | size = PyString_Size(value); |
| 550 | data = PyString_AsString(value); |
| 551 | /* XXXX Do I need the GetState/SetState calls? */ |
| 552 | SetHandleSize(self->ob_itself, size); |
| 553 | if ( MemError()) |
| 554 | return -1; |
| 555 | HLock(self->ob_itself); |
| 556 | memcpy((char *)*self->ob_itself, data, size); |
| 557 | HUnlock(self->ob_itself); |
| 558 | /* XXXX Should I do the Changed call immedeately? */ |
| 559 | return 0; |
| 560 | } |
| 561 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 562 | |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 563 | #define ResObj_compare NULL |
| 564 | |
| 565 | #define ResObj_repr NULL |
| 566 | |
| 567 | #define ResObj_hash NULL |
| 568 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 569 | PyTypeObject Resource_Type = { |
| 570 | PyObject_HEAD_INIT(&PyType_Type) |
| 571 | 0, /*ob_size*/ |
| 572 | "Resource", /*tp_name*/ |
| 573 | sizeof(ResourceObject), /*tp_basicsize*/ |
| 574 | 0, /*tp_itemsize*/ |
| 575 | /* methods */ |
| 576 | (destructor) ResObj_dealloc, /*tp_dealloc*/ |
| 577 | 0, /*tp_print*/ |
| 578 | (getattrfunc) ResObj_getattr, /*tp_getattr*/ |
| 579 | (setattrfunc) ResObj_setattr, /*tp_setattr*/ |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 580 | (cmpfunc) ResObj_compare, /*tp_compare*/ |
| 581 | (reprfunc) ResObj_repr, /*tp_repr*/ |
| 582 | (PyNumberMethods *)0, /* tp_as_number */ |
| 583 | (PySequenceMethods *)0, /* tp_as_sequence */ |
| 584 | (PyMappingMethods *)0, /* tp_as_mapping */ |
| 585 | (hashfunc) ResObj_hash, /*tp_hash*/ |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | /* -------------------- End object type Resource -------------------- */ |
| 589 | |
| 590 | |
| 591 | static PyObject *Res_InitResources(_self, _args) |
| 592 | PyObject *_self; |
| 593 | PyObject *_args; |
| 594 | { |
| 595 | PyObject *_res = NULL; |
| 596 | short _rv; |
| 597 | if (!PyArg_ParseTuple(_args, "")) |
| 598 | return NULL; |
| 599 | _rv = InitResources(); |
| 600 | { |
| 601 | OSErr _err = ResError(); |
| 602 | if (_err != noErr) return PyMac_Error(_err); |
| 603 | } |
| 604 | _res = Py_BuildValue("h", |
| 605 | _rv); |
| 606 | return _res; |
| 607 | } |
| 608 | |
| 609 | static PyObject *Res_RsrcZoneInit(_self, _args) |
| 610 | PyObject *_self; |
| 611 | PyObject *_args; |
| 612 | { |
| 613 | PyObject *_res = NULL; |
| 614 | if (!PyArg_ParseTuple(_args, "")) |
| 615 | return NULL; |
| 616 | RsrcZoneInit(); |
| 617 | { |
| 618 | OSErr _err = ResError(); |
| 619 | if (_err != noErr) return PyMac_Error(_err); |
| 620 | } |
| 621 | Py_INCREF(Py_None); |
| 622 | _res = Py_None; |
| 623 | return _res; |
| 624 | } |
| 625 | |
| 626 | static PyObject *Res_CloseResFile(_self, _args) |
| 627 | PyObject *_self; |
| 628 | PyObject *_args; |
| 629 | { |
| 630 | PyObject *_res = NULL; |
| 631 | short refNum; |
| 632 | if (!PyArg_ParseTuple(_args, "h", |
| 633 | &refNum)) |
| 634 | return NULL; |
| 635 | CloseResFile(refNum); |
| 636 | { |
| 637 | OSErr _err = ResError(); |
| 638 | if (_err != noErr) return PyMac_Error(_err); |
| 639 | } |
| 640 | Py_INCREF(Py_None); |
| 641 | _res = Py_None; |
| 642 | return _res; |
| 643 | } |
| 644 | |
| 645 | static PyObject *Res_ResError(_self, _args) |
| 646 | PyObject *_self; |
| 647 | PyObject *_args; |
| 648 | { |
| 649 | PyObject *_res = NULL; |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 650 | OSErr _rv; |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 651 | if (!PyArg_ParseTuple(_args, "")) |
| 652 | return NULL; |
| 653 | _rv = ResError(); |
| 654 | { |
| 655 | OSErr _err = ResError(); |
| 656 | if (_err != noErr) return PyMac_Error(_err); |
| 657 | } |
| 658 | _res = Py_BuildValue("h", |
| 659 | _rv); |
| 660 | return _res; |
| 661 | } |
| 662 | |
| 663 | static PyObject *Res_CurResFile(_self, _args) |
| 664 | PyObject *_self; |
| 665 | PyObject *_args; |
| 666 | { |
| 667 | PyObject *_res = NULL; |
| 668 | short _rv; |
| 669 | if (!PyArg_ParseTuple(_args, "")) |
| 670 | return NULL; |
| 671 | _rv = CurResFile(); |
| 672 | { |
| 673 | OSErr _err = ResError(); |
| 674 | if (_err != noErr) return PyMac_Error(_err); |
| 675 | } |
| 676 | _res = Py_BuildValue("h", |
| 677 | _rv); |
| 678 | return _res; |
| 679 | } |
| 680 | |
| 681 | static PyObject *Res_CreateResFile(_self, _args) |
| 682 | PyObject *_self; |
| 683 | PyObject *_args; |
| 684 | { |
| 685 | PyObject *_res = NULL; |
| 686 | Str255 fileName; |
| 687 | if (!PyArg_ParseTuple(_args, "O&", |
| 688 | PyMac_GetStr255, fileName)) |
| 689 | return NULL; |
| 690 | CreateResFile(fileName); |
| 691 | { |
| 692 | OSErr _err = ResError(); |
| 693 | if (_err != noErr) return PyMac_Error(_err); |
| 694 | } |
| 695 | Py_INCREF(Py_None); |
| 696 | _res = Py_None; |
| 697 | return _res; |
| 698 | } |
| 699 | |
| 700 | static PyObject *Res_OpenResFile(_self, _args) |
| 701 | PyObject *_self; |
| 702 | PyObject *_args; |
| 703 | { |
| 704 | PyObject *_res = NULL; |
| 705 | short _rv; |
| 706 | Str255 fileName; |
| 707 | if (!PyArg_ParseTuple(_args, "O&", |
| 708 | PyMac_GetStr255, fileName)) |
| 709 | return NULL; |
| 710 | _rv = OpenResFile(fileName); |
| 711 | { |
| 712 | OSErr _err = ResError(); |
| 713 | if (_err != noErr) return PyMac_Error(_err); |
| 714 | } |
| 715 | _res = Py_BuildValue("h", |
| 716 | _rv); |
| 717 | return _res; |
| 718 | } |
| 719 | |
| 720 | static PyObject *Res_UseResFile(_self, _args) |
| 721 | PyObject *_self; |
| 722 | PyObject *_args; |
| 723 | { |
| 724 | PyObject *_res = NULL; |
| 725 | short refNum; |
| 726 | if (!PyArg_ParseTuple(_args, "h", |
| 727 | &refNum)) |
| 728 | return NULL; |
| 729 | UseResFile(refNum); |
| 730 | { |
| 731 | OSErr _err = ResError(); |
| 732 | if (_err != noErr) return PyMac_Error(_err); |
| 733 | } |
| 734 | Py_INCREF(Py_None); |
| 735 | _res = Py_None; |
| 736 | return _res; |
| 737 | } |
| 738 | |
| 739 | static PyObject *Res_CountTypes(_self, _args) |
| 740 | PyObject *_self; |
| 741 | PyObject *_args; |
| 742 | { |
| 743 | PyObject *_res = NULL; |
| 744 | short _rv; |
| 745 | if (!PyArg_ParseTuple(_args, "")) |
| 746 | return NULL; |
| 747 | _rv = CountTypes(); |
| 748 | { |
| 749 | OSErr _err = ResError(); |
| 750 | if (_err != noErr) return PyMac_Error(_err); |
| 751 | } |
| 752 | _res = Py_BuildValue("h", |
| 753 | _rv); |
| 754 | return _res; |
| 755 | } |
| 756 | |
| 757 | static PyObject *Res_Count1Types(_self, _args) |
| 758 | PyObject *_self; |
| 759 | PyObject *_args; |
| 760 | { |
| 761 | PyObject *_res = NULL; |
| 762 | short _rv; |
| 763 | if (!PyArg_ParseTuple(_args, "")) |
| 764 | return NULL; |
| 765 | _rv = Count1Types(); |
| 766 | { |
| 767 | OSErr _err = ResError(); |
| 768 | if (_err != noErr) return PyMac_Error(_err); |
| 769 | } |
| 770 | _res = Py_BuildValue("h", |
| 771 | _rv); |
| 772 | return _res; |
| 773 | } |
| 774 | |
| 775 | static PyObject *Res_GetIndType(_self, _args) |
| 776 | PyObject *_self; |
| 777 | PyObject *_args; |
| 778 | { |
| 779 | PyObject *_res = NULL; |
| 780 | ResType theType; |
| 781 | short index; |
| 782 | if (!PyArg_ParseTuple(_args, "h", |
| 783 | &index)) |
| 784 | return NULL; |
| 785 | GetIndType(&theType, |
| 786 | index); |
| 787 | { |
| 788 | OSErr _err = ResError(); |
| 789 | if (_err != noErr) return PyMac_Error(_err); |
| 790 | } |
| 791 | _res = Py_BuildValue("O&", |
| 792 | PyMac_BuildOSType, theType); |
| 793 | return _res; |
| 794 | } |
| 795 | |
| 796 | static PyObject *Res_Get1IndType(_self, _args) |
| 797 | PyObject *_self; |
| 798 | PyObject *_args; |
| 799 | { |
| 800 | PyObject *_res = NULL; |
| 801 | ResType theType; |
| 802 | short index; |
| 803 | if (!PyArg_ParseTuple(_args, "h", |
| 804 | &index)) |
| 805 | return NULL; |
| 806 | Get1IndType(&theType, |
| 807 | index); |
| 808 | { |
| 809 | OSErr _err = ResError(); |
| 810 | if (_err != noErr) return PyMac_Error(_err); |
| 811 | } |
| 812 | _res = Py_BuildValue("O&", |
| 813 | PyMac_BuildOSType, theType); |
| 814 | return _res; |
| 815 | } |
| 816 | |
| 817 | static PyObject *Res_SetResLoad(_self, _args) |
| 818 | PyObject *_self; |
| 819 | PyObject *_args; |
| 820 | { |
| 821 | PyObject *_res = NULL; |
| 822 | Boolean load; |
| 823 | if (!PyArg_ParseTuple(_args, "b", |
| 824 | &load)) |
| 825 | return NULL; |
| 826 | SetResLoad(load); |
| 827 | { |
| 828 | OSErr _err = ResError(); |
| 829 | if (_err != noErr) return PyMac_Error(_err); |
| 830 | } |
| 831 | Py_INCREF(Py_None); |
| 832 | _res = Py_None; |
| 833 | return _res; |
| 834 | } |
| 835 | |
| 836 | static PyObject *Res_CountResources(_self, _args) |
| 837 | PyObject *_self; |
| 838 | PyObject *_args; |
| 839 | { |
| 840 | PyObject *_res = NULL; |
| 841 | short _rv; |
| 842 | ResType theType; |
| 843 | if (!PyArg_ParseTuple(_args, "O&", |
| 844 | PyMac_GetOSType, &theType)) |
| 845 | return NULL; |
| 846 | _rv = CountResources(theType); |
| 847 | { |
| 848 | OSErr _err = ResError(); |
| 849 | if (_err != noErr) return PyMac_Error(_err); |
| 850 | } |
| 851 | _res = Py_BuildValue("h", |
| 852 | _rv); |
| 853 | return _res; |
| 854 | } |
| 855 | |
| 856 | static PyObject *Res_Count1Resources(_self, _args) |
| 857 | PyObject *_self; |
| 858 | PyObject *_args; |
| 859 | { |
| 860 | PyObject *_res = NULL; |
| 861 | short _rv; |
| 862 | ResType theType; |
| 863 | if (!PyArg_ParseTuple(_args, "O&", |
| 864 | PyMac_GetOSType, &theType)) |
| 865 | return NULL; |
| 866 | _rv = Count1Resources(theType); |
| 867 | { |
| 868 | OSErr _err = ResError(); |
| 869 | if (_err != noErr) return PyMac_Error(_err); |
| 870 | } |
| 871 | _res = Py_BuildValue("h", |
| 872 | _rv); |
| 873 | return _res; |
| 874 | } |
| 875 | |
| 876 | static PyObject *Res_GetIndResource(_self, _args) |
| 877 | PyObject *_self; |
| 878 | PyObject *_args; |
| 879 | { |
| 880 | PyObject *_res = NULL; |
| 881 | Handle _rv; |
| 882 | ResType theType; |
| 883 | short index; |
| 884 | if (!PyArg_ParseTuple(_args, "O&h", |
| 885 | PyMac_GetOSType, &theType, |
| 886 | &index)) |
| 887 | return NULL; |
| 888 | _rv = GetIndResource(theType, |
| 889 | index); |
| 890 | { |
| 891 | OSErr _err = ResError(); |
| 892 | if (_err != noErr) return PyMac_Error(_err); |
| 893 | } |
| 894 | _res = Py_BuildValue("O&", |
| 895 | ResObj_New, _rv); |
| 896 | return _res; |
| 897 | } |
| 898 | |
| 899 | static PyObject *Res_Get1IndResource(_self, _args) |
| 900 | PyObject *_self; |
| 901 | PyObject *_args; |
| 902 | { |
| 903 | PyObject *_res = NULL; |
| 904 | Handle _rv; |
| 905 | ResType theType; |
| 906 | short index; |
| 907 | if (!PyArg_ParseTuple(_args, "O&h", |
| 908 | PyMac_GetOSType, &theType, |
| 909 | &index)) |
| 910 | return NULL; |
| 911 | _rv = Get1IndResource(theType, |
| 912 | index); |
| 913 | { |
| 914 | OSErr _err = ResError(); |
| 915 | if (_err != noErr) return PyMac_Error(_err); |
| 916 | } |
| 917 | _res = Py_BuildValue("O&", |
| 918 | ResObj_New, _rv); |
| 919 | return _res; |
| 920 | } |
| 921 | |
| 922 | static PyObject *Res_GetResource(_self, _args) |
| 923 | PyObject *_self; |
| 924 | PyObject *_args; |
| 925 | { |
| 926 | PyObject *_res = NULL; |
| 927 | Handle _rv; |
| 928 | ResType theType; |
| 929 | short theID; |
| 930 | if (!PyArg_ParseTuple(_args, "O&h", |
| 931 | PyMac_GetOSType, &theType, |
| 932 | &theID)) |
| 933 | return NULL; |
| 934 | _rv = GetResource(theType, |
| 935 | theID); |
| 936 | { |
| 937 | OSErr _err = ResError(); |
| 938 | if (_err != noErr) return PyMac_Error(_err); |
| 939 | } |
| 940 | _res = Py_BuildValue("O&", |
| 941 | ResObj_New, _rv); |
| 942 | return _res; |
| 943 | } |
| 944 | |
| 945 | static PyObject *Res_Get1Resource(_self, _args) |
| 946 | PyObject *_self; |
| 947 | PyObject *_args; |
| 948 | { |
| 949 | PyObject *_res = NULL; |
| 950 | Handle _rv; |
| 951 | ResType theType; |
| 952 | short theID; |
| 953 | if (!PyArg_ParseTuple(_args, "O&h", |
| 954 | PyMac_GetOSType, &theType, |
| 955 | &theID)) |
| 956 | return NULL; |
| 957 | _rv = Get1Resource(theType, |
| 958 | theID); |
| 959 | { |
| 960 | OSErr _err = ResError(); |
| 961 | if (_err != noErr) return PyMac_Error(_err); |
| 962 | } |
| 963 | _res = Py_BuildValue("O&", |
| 964 | ResObj_New, _rv); |
| 965 | return _res; |
| 966 | } |
| 967 | |
| 968 | static PyObject *Res_GetNamedResource(_self, _args) |
| 969 | PyObject *_self; |
| 970 | PyObject *_args; |
| 971 | { |
| 972 | PyObject *_res = NULL; |
| 973 | Handle _rv; |
| 974 | ResType theType; |
| 975 | Str255 name; |
| 976 | if (!PyArg_ParseTuple(_args, "O&O&", |
| 977 | PyMac_GetOSType, &theType, |
| 978 | PyMac_GetStr255, name)) |
| 979 | return NULL; |
| 980 | _rv = GetNamedResource(theType, |
| 981 | name); |
| 982 | { |
| 983 | OSErr _err = ResError(); |
| 984 | if (_err != noErr) return PyMac_Error(_err); |
| 985 | } |
| 986 | _res = Py_BuildValue("O&", |
| 987 | ResObj_New, _rv); |
| 988 | return _res; |
| 989 | } |
| 990 | |
| 991 | static PyObject *Res_Get1NamedResource(_self, _args) |
| 992 | PyObject *_self; |
| 993 | PyObject *_args; |
| 994 | { |
| 995 | PyObject *_res = NULL; |
| 996 | Handle _rv; |
| 997 | ResType theType; |
| 998 | Str255 name; |
| 999 | if (!PyArg_ParseTuple(_args, "O&O&", |
| 1000 | PyMac_GetOSType, &theType, |
| 1001 | PyMac_GetStr255, name)) |
| 1002 | return NULL; |
| 1003 | _rv = Get1NamedResource(theType, |
| 1004 | name); |
| 1005 | { |
| 1006 | OSErr _err = ResError(); |
| 1007 | if (_err != noErr) return PyMac_Error(_err); |
| 1008 | } |
| 1009 | _res = Py_BuildValue("O&", |
| 1010 | ResObj_New, _rv); |
| 1011 | return _res; |
| 1012 | } |
| 1013 | |
| 1014 | static PyObject *Res_UniqueID(_self, _args) |
| 1015 | PyObject *_self; |
| 1016 | PyObject *_args; |
| 1017 | { |
| 1018 | PyObject *_res = NULL; |
| 1019 | short _rv; |
| 1020 | ResType theType; |
| 1021 | if (!PyArg_ParseTuple(_args, "O&", |
| 1022 | PyMac_GetOSType, &theType)) |
| 1023 | return NULL; |
| 1024 | _rv = UniqueID(theType); |
| 1025 | { |
| 1026 | OSErr _err = ResError(); |
| 1027 | if (_err != noErr) return PyMac_Error(_err); |
| 1028 | } |
| 1029 | _res = Py_BuildValue("h", |
| 1030 | _rv); |
| 1031 | return _res; |
| 1032 | } |
| 1033 | |
| 1034 | static PyObject *Res_Unique1ID(_self, _args) |
| 1035 | PyObject *_self; |
| 1036 | PyObject *_args; |
| 1037 | { |
| 1038 | PyObject *_res = NULL; |
| 1039 | short _rv; |
| 1040 | ResType theType; |
| 1041 | if (!PyArg_ParseTuple(_args, "O&", |
| 1042 | PyMac_GetOSType, &theType)) |
| 1043 | return NULL; |
| 1044 | _rv = Unique1ID(theType); |
| 1045 | { |
| 1046 | OSErr _err = ResError(); |
| 1047 | if (_err != noErr) return PyMac_Error(_err); |
| 1048 | } |
| 1049 | _res = Py_BuildValue("h", |
| 1050 | _rv); |
| 1051 | return _res; |
| 1052 | } |
| 1053 | |
| 1054 | static PyObject *Res_UpdateResFile(_self, _args) |
| 1055 | PyObject *_self; |
| 1056 | PyObject *_args; |
| 1057 | { |
| 1058 | PyObject *_res = NULL; |
| 1059 | short refNum; |
| 1060 | if (!PyArg_ParseTuple(_args, "h", |
| 1061 | &refNum)) |
| 1062 | return NULL; |
| 1063 | UpdateResFile(refNum); |
| 1064 | { |
| 1065 | OSErr _err = ResError(); |
| 1066 | if (_err != noErr) return PyMac_Error(_err); |
| 1067 | } |
| 1068 | Py_INCREF(Py_None); |
| 1069 | _res = Py_None; |
| 1070 | return _res; |
| 1071 | } |
| 1072 | |
| 1073 | static PyObject *Res_SetResPurge(_self, _args) |
| 1074 | PyObject *_self; |
| 1075 | PyObject *_args; |
| 1076 | { |
| 1077 | PyObject *_res = NULL; |
| 1078 | Boolean install; |
| 1079 | if (!PyArg_ParseTuple(_args, "b", |
| 1080 | &install)) |
| 1081 | return NULL; |
| 1082 | SetResPurge(install); |
| 1083 | { |
| 1084 | OSErr _err = ResError(); |
| 1085 | if (_err != noErr) return PyMac_Error(_err); |
| 1086 | } |
| 1087 | Py_INCREF(Py_None); |
| 1088 | _res = Py_None; |
| 1089 | return _res; |
| 1090 | } |
| 1091 | |
| 1092 | static PyObject *Res_GetResFileAttrs(_self, _args) |
| 1093 | PyObject *_self; |
| 1094 | PyObject *_args; |
| 1095 | { |
| 1096 | PyObject *_res = NULL; |
| 1097 | short _rv; |
| 1098 | short refNum; |
| 1099 | if (!PyArg_ParseTuple(_args, "h", |
| 1100 | &refNum)) |
| 1101 | return NULL; |
| 1102 | _rv = GetResFileAttrs(refNum); |
| 1103 | { |
| 1104 | OSErr _err = ResError(); |
| 1105 | if (_err != noErr) return PyMac_Error(_err); |
| 1106 | } |
| 1107 | _res = Py_BuildValue("h", |
| 1108 | _rv); |
| 1109 | return _res; |
| 1110 | } |
| 1111 | |
| 1112 | static PyObject *Res_SetResFileAttrs(_self, _args) |
| 1113 | PyObject *_self; |
| 1114 | PyObject *_args; |
| 1115 | { |
| 1116 | PyObject *_res = NULL; |
| 1117 | short refNum; |
| 1118 | short attrs; |
| 1119 | if (!PyArg_ParseTuple(_args, "hh", |
| 1120 | &refNum, |
| 1121 | &attrs)) |
| 1122 | return NULL; |
| 1123 | SetResFileAttrs(refNum, |
| 1124 | attrs); |
| 1125 | { |
| 1126 | OSErr _err = ResError(); |
| 1127 | if (_err != noErr) return PyMac_Error(_err); |
| 1128 | } |
| 1129 | Py_INCREF(Py_None); |
| 1130 | _res = Py_None; |
| 1131 | return _res; |
| 1132 | } |
| 1133 | |
| 1134 | static PyObject *Res_OpenRFPerm(_self, _args) |
| 1135 | PyObject *_self; |
| 1136 | PyObject *_args; |
| 1137 | { |
| 1138 | PyObject *_res = NULL; |
| 1139 | short _rv; |
| 1140 | Str255 fileName; |
| 1141 | short vRefNum; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1142 | SignedByte permission; |
| 1143 | if (!PyArg_ParseTuple(_args, "O&hb", |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1144 | PyMac_GetStr255, fileName, |
| 1145 | &vRefNum, |
| 1146 | &permission)) |
| 1147 | return NULL; |
| 1148 | _rv = OpenRFPerm(fileName, |
| 1149 | vRefNum, |
| 1150 | permission); |
| 1151 | { |
| 1152 | OSErr _err = ResError(); |
| 1153 | if (_err != noErr) return PyMac_Error(_err); |
| 1154 | } |
| 1155 | _res = Py_BuildValue("h", |
| 1156 | _rv); |
| 1157 | return _res; |
| 1158 | } |
| 1159 | |
| 1160 | static PyObject *Res_RGetResource(_self, _args) |
| 1161 | PyObject *_self; |
| 1162 | PyObject *_args; |
| 1163 | { |
| 1164 | PyObject *_res = NULL; |
| 1165 | Handle _rv; |
| 1166 | ResType theType; |
| 1167 | short theID; |
| 1168 | if (!PyArg_ParseTuple(_args, "O&h", |
| 1169 | PyMac_GetOSType, &theType, |
| 1170 | &theID)) |
| 1171 | return NULL; |
| 1172 | _rv = RGetResource(theType, |
| 1173 | theID); |
| 1174 | { |
| 1175 | OSErr _err = ResError(); |
| 1176 | if (_err != noErr) return PyMac_Error(_err); |
| 1177 | } |
| 1178 | _res = Py_BuildValue("O&", |
| 1179 | ResObj_New, _rv); |
| 1180 | return _res; |
| 1181 | } |
| 1182 | |
| 1183 | static PyObject *Res_HOpenResFile(_self, _args) |
| 1184 | PyObject *_self; |
| 1185 | PyObject *_args; |
| 1186 | { |
| 1187 | PyObject *_res = NULL; |
| 1188 | short _rv; |
| 1189 | short vRefNum; |
| 1190 | long dirID; |
| 1191 | Str255 fileName; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1192 | SignedByte permission; |
| 1193 | if (!PyArg_ParseTuple(_args, "hlO&b", |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1194 | &vRefNum, |
| 1195 | &dirID, |
| 1196 | PyMac_GetStr255, fileName, |
| 1197 | &permission)) |
| 1198 | return NULL; |
| 1199 | _rv = HOpenResFile(vRefNum, |
| 1200 | dirID, |
| 1201 | fileName, |
| 1202 | permission); |
| 1203 | { |
| 1204 | OSErr _err = ResError(); |
| 1205 | if (_err != noErr) return PyMac_Error(_err); |
| 1206 | } |
| 1207 | _res = Py_BuildValue("h", |
| 1208 | _rv); |
| 1209 | return _res; |
| 1210 | } |
| 1211 | |
| 1212 | static PyObject *Res_HCreateResFile(_self, _args) |
| 1213 | PyObject *_self; |
| 1214 | PyObject *_args; |
| 1215 | { |
| 1216 | PyObject *_res = NULL; |
| 1217 | short vRefNum; |
| 1218 | long dirID; |
| 1219 | Str255 fileName; |
| 1220 | if (!PyArg_ParseTuple(_args, "hlO&", |
| 1221 | &vRefNum, |
| 1222 | &dirID, |
| 1223 | PyMac_GetStr255, fileName)) |
| 1224 | return NULL; |
| 1225 | HCreateResFile(vRefNum, |
| 1226 | dirID, |
| 1227 | fileName); |
| 1228 | { |
| 1229 | OSErr _err = ResError(); |
| 1230 | if (_err != noErr) return PyMac_Error(_err); |
| 1231 | } |
| 1232 | Py_INCREF(Py_None); |
| 1233 | _res = Py_None; |
| 1234 | return _res; |
| 1235 | } |
| 1236 | |
| 1237 | static PyObject *Res_FSpOpenResFile(_self, _args) |
| 1238 | PyObject *_self; |
| 1239 | PyObject *_args; |
| 1240 | { |
| 1241 | PyObject *_res = NULL; |
| 1242 | short _rv; |
| 1243 | FSSpec spec; |
| 1244 | SignedByte permission; |
| 1245 | if (!PyArg_ParseTuple(_args, "O&b", |
| 1246 | PyMac_GetFSSpec, &spec, |
| 1247 | &permission)) |
| 1248 | return NULL; |
| 1249 | _rv = FSpOpenResFile(&spec, |
| 1250 | permission); |
| 1251 | { |
| 1252 | OSErr _err = ResError(); |
| 1253 | if (_err != noErr) return PyMac_Error(_err); |
| 1254 | } |
| 1255 | _res = Py_BuildValue("h", |
| 1256 | _rv); |
| 1257 | return _res; |
| 1258 | } |
| 1259 | |
| 1260 | static PyObject *Res_FSpCreateResFile(_self, _args) |
| 1261 | PyObject *_self; |
| 1262 | PyObject *_args; |
| 1263 | { |
| 1264 | PyObject *_res = NULL; |
| 1265 | FSSpec spec; |
| 1266 | OSType creator; |
| 1267 | OSType fileType; |
| 1268 | ScriptCode scriptTag; |
| 1269 | if (!PyArg_ParseTuple(_args, "O&O&O&h", |
| 1270 | PyMac_GetFSSpec, &spec, |
| 1271 | PyMac_GetOSType, &creator, |
| 1272 | PyMac_GetOSType, &fileType, |
| 1273 | &scriptTag)) |
| 1274 | return NULL; |
| 1275 | FSpCreateResFile(&spec, |
| 1276 | creator, |
| 1277 | fileType, |
| 1278 | scriptTag); |
| 1279 | { |
| 1280 | OSErr _err = ResError(); |
| 1281 | if (_err != noErr) return PyMac_Error(_err); |
| 1282 | } |
| 1283 | Py_INCREF(Py_None); |
| 1284 | _res = Py_None; |
| 1285 | return _res; |
| 1286 | } |
| 1287 | |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 1288 | static PyObject *Res_Resource(_self, _args) |
| 1289 | PyObject *_self; |
| 1290 | PyObject *_args; |
| 1291 | { |
| 1292 | PyObject *_res = NULL; |
| 1293 | |
| 1294 | char *buf; |
| 1295 | int len; |
| 1296 | Handle h; |
| 1297 | |
| 1298 | if (!PyArg_ParseTuple(_args, "s#", &buf, &len)) |
| 1299 | return NULL; |
| 1300 | h = NewHandle(len); |
| 1301 | if ( h == NULL ) { |
| 1302 | PyErr_NoMemory(); |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | HLock(h); |
| 1306 | memcpy(*h, buf, len); |
| 1307 | HUnlock(h); |
| 1308 | return (PyObject *)ResObj_New(h); |
| 1309 | |
| 1310 | } |
| 1311 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1312 | static PyMethodDef Res_methods[] = { |
| 1313 | {"InitResources", (PyCFunction)Res_InitResources, 1, |
| 1314 | "() -> (short _rv)"}, |
| 1315 | {"RsrcZoneInit", (PyCFunction)Res_RsrcZoneInit, 1, |
| 1316 | "() -> None"}, |
| 1317 | {"CloseResFile", (PyCFunction)Res_CloseResFile, 1, |
| 1318 | "(short refNum) -> None"}, |
| 1319 | {"ResError", (PyCFunction)Res_ResError, 1, |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 1320 | "() -> (OSErr _rv)"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1321 | {"CurResFile", (PyCFunction)Res_CurResFile, 1, |
| 1322 | "() -> (short _rv)"}, |
| 1323 | {"CreateResFile", (PyCFunction)Res_CreateResFile, 1, |
| 1324 | "(Str255 fileName) -> None"}, |
| 1325 | {"OpenResFile", (PyCFunction)Res_OpenResFile, 1, |
| 1326 | "(Str255 fileName) -> (short _rv)"}, |
| 1327 | {"UseResFile", (PyCFunction)Res_UseResFile, 1, |
| 1328 | "(short refNum) -> None"}, |
| 1329 | {"CountTypes", (PyCFunction)Res_CountTypes, 1, |
| 1330 | "() -> (short _rv)"}, |
| 1331 | {"Count1Types", (PyCFunction)Res_Count1Types, 1, |
| 1332 | "() -> (short _rv)"}, |
| 1333 | {"GetIndType", (PyCFunction)Res_GetIndType, 1, |
| 1334 | "(short index) -> (ResType theType)"}, |
| 1335 | {"Get1IndType", (PyCFunction)Res_Get1IndType, 1, |
| 1336 | "(short index) -> (ResType theType)"}, |
| 1337 | {"SetResLoad", (PyCFunction)Res_SetResLoad, 1, |
| 1338 | "(Boolean load) -> None"}, |
| 1339 | {"CountResources", (PyCFunction)Res_CountResources, 1, |
| 1340 | "(ResType theType) -> (short _rv)"}, |
| 1341 | {"Count1Resources", (PyCFunction)Res_Count1Resources, 1, |
| 1342 | "(ResType theType) -> (short _rv)"}, |
| 1343 | {"GetIndResource", (PyCFunction)Res_GetIndResource, 1, |
| 1344 | "(ResType theType, short index) -> (Handle _rv)"}, |
| 1345 | {"Get1IndResource", (PyCFunction)Res_Get1IndResource, 1, |
| 1346 | "(ResType theType, short index) -> (Handle _rv)"}, |
| 1347 | {"GetResource", (PyCFunction)Res_GetResource, 1, |
| 1348 | "(ResType theType, short theID) -> (Handle _rv)"}, |
| 1349 | {"Get1Resource", (PyCFunction)Res_Get1Resource, 1, |
| 1350 | "(ResType theType, short theID) -> (Handle _rv)"}, |
| 1351 | {"GetNamedResource", (PyCFunction)Res_GetNamedResource, 1, |
| 1352 | "(ResType theType, Str255 name) -> (Handle _rv)"}, |
| 1353 | {"Get1NamedResource", (PyCFunction)Res_Get1NamedResource, 1, |
| 1354 | "(ResType theType, Str255 name) -> (Handle _rv)"}, |
| 1355 | {"UniqueID", (PyCFunction)Res_UniqueID, 1, |
| 1356 | "(ResType theType) -> (short _rv)"}, |
| 1357 | {"Unique1ID", (PyCFunction)Res_Unique1ID, 1, |
| 1358 | "(ResType theType) -> (short _rv)"}, |
| 1359 | {"UpdateResFile", (PyCFunction)Res_UpdateResFile, 1, |
| 1360 | "(short refNum) -> None"}, |
| 1361 | {"SetResPurge", (PyCFunction)Res_SetResPurge, 1, |
| 1362 | "(Boolean install) -> None"}, |
| 1363 | {"GetResFileAttrs", (PyCFunction)Res_GetResFileAttrs, 1, |
| 1364 | "(short refNum) -> (short _rv)"}, |
| 1365 | {"SetResFileAttrs", (PyCFunction)Res_SetResFileAttrs, 1, |
| 1366 | "(short refNum, short attrs) -> None"}, |
| 1367 | {"OpenRFPerm", (PyCFunction)Res_OpenRFPerm, 1, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1368 | "(Str255 fileName, short vRefNum, SignedByte permission) -> (short _rv)"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1369 | {"RGetResource", (PyCFunction)Res_RGetResource, 1, |
| 1370 | "(ResType theType, short theID) -> (Handle _rv)"}, |
| 1371 | {"HOpenResFile", (PyCFunction)Res_HOpenResFile, 1, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1372 | "(short vRefNum, long dirID, Str255 fileName, SignedByte permission) -> (short _rv)"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1373 | {"HCreateResFile", (PyCFunction)Res_HCreateResFile, 1, |
| 1374 | "(short vRefNum, long dirID, Str255 fileName) -> None"}, |
| 1375 | {"FSpOpenResFile", (PyCFunction)Res_FSpOpenResFile, 1, |
| 1376 | "(FSSpec spec, SignedByte permission) -> (short _rv)"}, |
| 1377 | {"FSpCreateResFile", (PyCFunction)Res_FSpCreateResFile, 1, |
| 1378 | "(FSSpec spec, OSType creator, OSType fileType, ScriptCode scriptTag) -> None"}, |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 1379 | {"Resource", (PyCFunction)Res_Resource, 1, |
| 1380 | "Convert a string to a resource object.\n\nThe created resource object is actually just a handle.\nApply AddResource() to write it to a resource file.\n"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1381 | {NULL, NULL, 0} |
| 1382 | }; |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1387 | /* Alternative version of ResObj_New, which returns None for null argument */ |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1388 | PyObject *OptResObj_New(itself) |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1389 | Handle itself; |
| 1390 | { |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1391 | if (itself == NULL) { |
| 1392 | Py_INCREF(Py_None); |
| 1393 | return Py_None; |
| 1394 | } |
| 1395 | return ResObj_New(itself); |
| 1396 | } |
| 1397 | |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1398 | OptResObj_Convert(v, p_itself) |
| 1399 | PyObject *v; |
| 1400 | Handle *p_itself; |
| 1401 | { |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1402 | PyObject *tmp; |
| 1403 | |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1404 | if ( v == Py_None ) { |
| 1405 | *p_itself = NULL; |
| 1406 | return 1; |
| 1407 | } |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1408 | if (ResObj_Check(v)) |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1409 | { |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1410 | *p_itself = ((ResourceObject *)v)->ob_itself; |
| 1411 | return 1; |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1412 | } |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1413 | /* If it isn't a resource yet see whether it is convertible */ |
| 1414 | if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) { |
| 1415 | *p_itself = ((ResourceObject *)tmp)->ob_itself; |
| 1416 | Py_DECREF(tmp); |
| 1417 | return 1; |
| 1418 | } |
| 1419 | PyErr_Clear(); |
| 1420 | PyErr_SetString(PyExc_TypeError, "Resource required"); |
| 1421 | return 0; |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1424 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1425 | |
| 1426 | void initRes() |
| 1427 | { |
| 1428 | PyObject *m; |
| 1429 | PyObject *d; |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | m = Py_InitModule("Res", Res_methods); |
| 1436 | d = PyModule_GetDict(m); |
| 1437 | Res_Error = PyMac_GetOSErrException(); |
| 1438 | if (Res_Error == NULL || |
| 1439 | PyDict_SetItemString(d, "Error", Res_Error) != 0) |
| 1440 | Py_FatalError("can't initialize Res.Error"); |
Jack Jansen | a755e68 | 1997-09-20 17:40:22 +0000 | [diff] [blame] | 1441 | Resource_Type.ob_type = &PyType_Type; |
| 1442 | Py_INCREF(&Resource_Type); |
| 1443 | if (PyDict_SetItemString(d, "ResourceType", (PyObject *)&Resource_Type) != 0) |
| 1444 | Py_FatalError("can't initialize ResourceType"); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /* ========================= End module Res ========================= */ |
| 1448 | |