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