PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Mac/Modules/res/resedit.py

http://unladen-swallow.googlecode.com/
Python | 102 lines | 74 code | 1 blank | 27 comment | 0 complexity | 533b1cc73ee059d1be16b645fb4e7fe1 MD5 | raw file
  1##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)
 28
 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;
 47_res = (PyObject *)rv;
 48return _res;
 49"""
 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.
 57"""
 58functions.append(f)
 59
 60# Convert resources to other things.
 61
 62as_xxx_body = """
 63_res = %sObj_New((%sHandle)_self->ob_itself);
 64return _res;
 65"""
 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"))
 76
 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)
 83
 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;
 97_res = Py_BuildValue("i", old);
 98return _res;
 99"""
100f = ManualGenerator("AutoDispose", AutoDispose_body)
101f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup"
102resmethods.append(f)