Added methods as_Menu and as_Control to convert a resource

to those object types

You can now set the data attribute of a resource with the expected
semantics (but you have to call ChangedResource yourself)
This commit is contained in:
Jack Jansen 1995-06-18 20:20:27 +00:00
parent a177228ff8
commit 1e054024c1
3 changed files with 97 additions and 1 deletions

View File

@ -403,6 +403,26 @@ static PyObject *ResObj_GetNextFOND(_self, _args)
return _res; return _res;
} }
static PyObject *ResObj_as_Control(_self, _args)
ResourceObject *_self;
PyObject *_args;
{
PyObject *_res = NULL;
return CtlObj_New((ControlHandle)_self->ob_itself);
}
static PyObject *ResObj_as_Menu(_self, _args)
ResourceObject *_self;
PyObject *_args;
{
PyObject *_res = NULL;
return MenuObj_New((MenuHandle)_self->ob_itself);
}
static PyMethodDef ResObj_methods[] = { static PyMethodDef ResObj_methods[] = {
{"HomeResFile", (PyCFunction)ResObj_HomeResFile, 1, {"HomeResFile", (PyCFunction)ResObj_HomeResFile, 1,
"() -> (short _rv)"}, "() -> (short _rv)"},
@ -438,6 +458,10 @@ static PyMethodDef ResObj_methods[] = {
"(long newSize) -> None"}, "(long newSize) -> None"},
{"GetNextFOND", (PyCFunction)ResObj_GetNextFOND, 1, {"GetNextFOND", (PyCFunction)ResObj_GetNextFOND, 1,
"() -> (Handle _rv)"}, "() -> (Handle _rv)"},
{"as_Control", (PyCFunction)ResObj_as_Control, 1,
"Return this resource/handle as a Control"},
{"as_Menu", (PyCFunction)ResObj_as_Menu, 1,
"Return this resource/handle as a Menu"},
{NULL, NULL, 0} {NULL, NULL, 0}
}; };
@ -468,7 +492,32 @@ static PyObject *ResObj_getattr(self, name)
return Py_FindMethodInChain(&ResObj_chain, (PyObject *)self, name); return Py_FindMethodInChain(&ResObj_chain, (PyObject *)self, name);
} }
#define ResObj_setattr NULL static int
ResObj_setattr(self, name, value)
ResourceObject *self;
char *name;
PyObject *value;
{
char *data;
long size;
if (strcmp(name, "data") != 0 || value == NULL )
return -1;
if ( !PyString_Check(value) )
return -1;
size = PyString_Size(value);
data = PyString_AsString(value);
/* XXXX Do I need the GetState/SetState calls? */
SetHandleSize(self->ob_itself, size);
if ( MemError())
return -1;
HLock(self->ob_itself);
memcpy((char *)*self->ob_itself, data, size);
HUnlock(self->ob_itself);
/* XXXX Should I do the Changed call immedeately? */
return 0;
}
PyTypeObject Resource_Type = { PyTypeObject Resource_Type = {
PyObject_HEAD_INIT(&PyType_Type) PyObject_HEAD_INIT(&PyType_Type)

View File

@ -23,3 +23,19 @@ The created resource object is actually just a handle.
Apply AddResource() to write it to a resource file. Apply AddResource() to write it to a resource file.
""" """
functions.append(f) functions.append(f)
# Convert resources to other things.
as_xxx_body = """
return %sObj_New((%sHandle)_self->ob_itself);
"""
def genresconverter(longname, shortname):
f = ManualGenerator("as_%s"%longname, as_xxx_body%(shortname, longname))
docstring = "Return this resource/handle as a %s"%longname
f.docstring = lambda docstring=docstring: docstring
return f
resmethods.append(genresconverter("Control", "Ctl"))
resmethods.append(genresconverter("Menu", "Menu"))

View File

@ -56,6 +56,34 @@ if (strcmp(name, "__members__") == 0)
return Py_BuildValue("[ss]", "data", "size"); return Py_BuildValue("[ss]", "data", "size");
""" """
setattrCode = """
static int
ResObj_setattr(self, name, value)
ResourceObject *self;
char *name;
PyObject *value;
{
char *data;
long size;
if (strcmp(name, "data") != 0 || value == NULL )
return -1;
if ( !PyString_Check(value) )
return -1;
size = PyString_Size(value);
data = PyString_AsString(value);
/* XXXX Do I need the GetState/SetState calls? */
SetHandleSize(self->ob_itself, size);
if ( MemError())
return -1;
HLock(self->ob_itself);
memcpy((char *)*self->ob_itself, data, size);
HUnlock(self->ob_itself);
/* XXXX Should I do the Changed call immedeately? */
return 0;
}
"""
class ResDefiniton(GlobalObjectDefinition): class ResDefiniton(GlobalObjectDefinition):
def outputCheckNewArg(self): def outputCheckNewArg(self):
@ -63,6 +91,9 @@ class ResDefiniton(GlobalObjectDefinition):
def outputGetattrHook(self): def outputGetattrHook(self):
Output(getattrHookCode) Output(getattrHookCode)
def outputSetattr(self):
Output(setattrCode)
resobject = ResDefiniton('Resource', 'ResObj', 'Handle') resobject = ResDefiniton('Resource', 'ResObj', 'Handle')