| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
handle_body = """ |
| 30 |
char *buf; |
| 31 |
int len; |
| 32 |
Handle h; |
| 33 |
ResourceObject *rv; |
| 34 |
|
| 35 |
if (!PyArg_ParseTuple(_args, "s#", &buf, &len)) |
| 36 |
return NULL; |
| 37 |
h = NewHandle(len); |
| 38 |
if ( h == NULL ) { |
| 39 |
PyErr_NoMemory(); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
HLock(h); |
| 43 |
memcpy(*h, buf, len); |
| 44 |
HUnlock(h); |
| 45 |
rv = (ResourceObject *)ResObj_New(h); |
| 46 |
rv->ob_freeit = PyMac_AutoDisposeHandle; |
| 47 |
_res = (PyObject *)rv; |
| 48 |
return _res; |
| 49 |
""" |
| 50 |
|
| 51 |
f = ManualGenerator("Handle", handle_body) |
| 52 |
f.docstring = lambda: """Convert a string to a Handle object. |
| 53 |
|
| 54 |
Resource() and Handle() are very similar, but objects created with Handle() are |
| 55 |
by default automatically DisposeHandle()d upon object cleanup. Use AutoDispose() |
| 56 |
to change this. |
| 57 |
""" |
| 58 |
functions.append(f) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
as_xxx_body = """ |
| 63 |
_res = %sObj_New((%sHandle)_self->ob_itself); |
| 64 |
return _res; |
| 65 |
""" |
| 66 |
|
| 67 |
def genresconverter(longname, shortname): |
| 68 |
|
| 69 |
f = ManualGenerator("as_%s"%longname, as_xxx_body%(shortname, longname)) |
| 70 |
docstring = "Return this resource/handle as a %s"%longname |
| 71 |
f.docstring = lambda docstring=docstring: docstring |
| 72 |
return f |
| 73 |
|
| 74 |
resmethods.append(genresconverter("Control", "Ctl")) |
| 75 |
resmethods.append(genresconverter("Menu", "Menu")) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
f = ResMethod(void, 'LoadResource', |
| 80 |
(Handle, 'theResource', InMode), |
| 81 |
) |
| 82 |
resmethods.append(f) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
AutoDispose_body = """ |
| 88 |
int onoff, old = 0; |
| 89 |
if (!PyArg_ParseTuple(_args, "i", &onoff)) |
| 90 |
return NULL; |
| 91 |
if ( _self->ob_freeit ) |
| 92 |
old = 1; |
| 93 |
if ( onoff ) |
| 94 |
_self->ob_freeit = PyMac_AutoDisposeHandle; |
| 95 |
else |
| 96 |
_self->ob_freeit = NULL; |
| 97 |
_res = Py_BuildValue("i", old); |
| 98 |
return _res; |
| 99 |
""" |
| 100 |
f = ManualGenerator("AutoDispose", AutoDispose_body) |
| 101 |
f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup" |
| 102 |
resmethods.append(f) |