blob: ab60b342f303d7d657992b2b3154ae6338aabb8a [file] [log] [blame]
Jack Jansen5f61a052002-12-05 23:26:38 +00001##resource_body = """
2##char *buf;
3##int len;
4##Handle h;
5##
6##if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
7## return NULL;
8##h = NewHandle(len);
9##if ( h == NULL ) {
10## PyErr_NoMemory();
11## return NULL;
12##}
13##HLock(h);
14##memcpy(*h, buf, len);
15##HUnlock(h);
16##_res = ResObj_New(h);
17##return _res;
18##"""
19##
20##f = ManualGenerator("Resource", resource_body)
21##f.docstring = lambda: """Convert a string to a resource object.
22##
23##The created resource object is actually just a handle,
24##apply AddResource() to write it to a resource file.
25##See also the Handle() docstring.
26##"""
27##functions.append(f)
Jack Jansenadd03b62000-03-08 16:58:15 +000028
29handle_body = """
30char *buf;
31int len;
32Handle h;
33ResourceObject *rv;
34
35if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
36 return NULL;
37h = NewHandle(len);
38if ( h == NULL ) {
39 PyErr_NoMemory();
40 return NULL;
41}
42HLock(h);
43memcpy(*h, buf, len);
44HUnlock(h);
45rv = (ResourceObject *)ResObj_New(h);
46rv->ob_freeit = PyMac_AutoDisposeHandle;
Jack Jansen044d95e2001-09-05 15:44:37 +000047_res = (PyObject *)rv;
48return _res;
Jack Jansenadd03b62000-03-08 16:58:15 +000049"""
50
51f = ManualGenerator("Handle", handle_body)
52f.docstring = lambda: """Convert a string to a Handle object.
53
54Resource() and Handle() are very similar, but objects created with Handle() are
55by default automatically DisposeHandle()d upon object cleanup. Use AutoDispose()
56to change this.
Guido van Rossum81920f11995-02-05 17:01:24 +000057"""
58functions.append(f)
Jack Jansen1e054021995-06-18 20:20:27 +000059
60# Convert resources to other things.
61
62as_xxx_body = """
Jack Jansenfd064862001-09-05 10:31:52 +000063_res = %sObj_New((%sHandle)_self->ob_itself);
64return _res;
Jack Jansen1e054021995-06-18 20:20:27 +000065"""
66
67def 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
74resmethods.append(genresconverter("Control", "Ctl"))
75resmethods.append(genresconverter("Menu", "Menu"))
Jack Jansene180d991998-04-24 10:28:20 +000076
77# The definition of this one is MacLoadResource, so we do it by hand...
78
79f = ResMethod(void, 'LoadResource',
80 (Handle, 'theResource', InMode),
81)
82resmethods.append(f)
Jack Jansenadd03b62000-03-08 16:58:15 +000083
84#
85# A method to set the auto-dispose flag
86#
87AutoDispose_body = """
88int onoff, old = 0;
89if (!PyArg_ParseTuple(_args, "i", &onoff))
90 return NULL;
91if ( _self->ob_freeit )
92 old = 1;
93if ( onoff )
94 _self->ob_freeit = PyMac_AutoDisposeHandle;
95else
96 _self->ob_freeit = NULL;
Jack Jansen044d95e2001-09-05 15:44:37 +000097_res = Py_BuildValue("i", old);
98return _res;
Jack Jansenadd03b62000-03-08 16:58:15 +000099"""
100f = ManualGenerator("AutoDispose", AutoDispose_body)
101f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup"
102resmethods.append(f)