blob: fce0bd11a71153b34f53650d5861ef29bdb89cb2 [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 = """
Jack Jansenfd064862001-09-05 10:31:52 +000061_res = %sObj_New((%sHandle)_self->ob_itself);
62return _res;
Jack Jansen1e054021995-06-18 20:20:27 +000063"""
64
65def genresconverter(longname, shortname):
66
67 f = ManualGenerator("as_%s"%longname, as_xxx_body%(shortname, longname))
68 docstring = "Return this resource/handle as a %s"%longname
69 f.docstring = lambda docstring=docstring: docstring
70 return f
71
72resmethods.append(genresconverter("Control", "Ctl"))
73resmethods.append(genresconverter("Menu", "Menu"))
Jack Jansene180d991998-04-24 10:28:20 +000074
75# The definition of this one is MacLoadResource, so we do it by hand...
76
77f = ResMethod(void, 'LoadResource',
78 (Handle, 'theResource', InMode),
79)
80resmethods.append(f)
Jack Jansenadd03b62000-03-08 16:58:15 +000081
82#
83# A method to set the auto-dispose flag
84#
85AutoDispose_body = """
86int onoff, old = 0;
87if (!PyArg_ParseTuple(_args, "i", &onoff))
88 return NULL;
89if ( _self->ob_freeit )
90 old = 1;
91if ( onoff )
92 _self->ob_freeit = PyMac_AutoDisposeHandle;
93else
94 _self->ob_freeit = NULL;
95return Py_BuildValue("i", old);
96"""
97f = ManualGenerator("AutoDispose", AutoDispose_body)
98f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup"
99resmethods.append(f)