/Mac/Modules/res/resedit.py

http://unladen-swallow.googlecode.com/ · Python · 102 lines · 60 code · 10 blank · 32 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. handle_body = """
  29. char *buf;
  30. int len;
  31. Handle h;
  32. ResourceObject *rv;
  33. if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
  34. return NULL;
  35. h = NewHandle(len);
  36. if ( h == NULL ) {
  37. PyErr_NoMemory();
  38. return NULL;
  39. }
  40. HLock(h);
  41. memcpy(*h, buf, len);
  42. HUnlock(h);
  43. rv = (ResourceObject *)ResObj_New(h);
  44. rv->ob_freeit = PyMac_AutoDisposeHandle;
  45. _res = (PyObject *)rv;
  46. return _res;
  47. """
  48. f = ManualGenerator("Handle", handle_body)
  49. f.docstring = lambda: """Convert a string to a Handle object.
  50. Resource() and Handle() are very similar, but objects created with Handle() are
  51. by default automatically DisposeHandle()d upon object cleanup. Use AutoDispose()
  52. to change this.
  53. """
  54. functions.append(f)
  55. # Convert resources to other things.
  56. as_xxx_body = """
  57. _res = %sObj_New((%sHandle)_self->ob_itself);
  58. return _res;
  59. """
  60. def genresconverter(longname, shortname):
  61. f = ManualGenerator("as_%s"%longname, as_xxx_body%(shortname, longname))
  62. docstring = "Return this resource/handle as a %s"%longname
  63. f.docstring = lambda docstring=docstring: docstring
  64. return f
  65. resmethods.append(genresconverter("Control", "Ctl"))
  66. resmethods.append(genresconverter("Menu", "Menu"))
  67. # The definition of this one is MacLoadResource, so we do it by hand...
  68. f = ResMethod(void, 'LoadResource',
  69. (Handle, 'theResource', InMode),
  70. )
  71. resmethods.append(f)
  72. #
  73. # A method to set the auto-dispose flag
  74. #
  75. AutoDispose_body = """
  76. int onoff, old = 0;
  77. if (!PyArg_ParseTuple(_args, "i", &onoff))
  78. return NULL;
  79. if ( _self->ob_freeit )
  80. old = 1;
  81. if ( onoff )
  82. _self->ob_freeit = PyMac_AutoDisposeHandle;
  83. else
  84. _self->ob_freeit = NULL;
  85. _res = Py_BuildValue("i", old);
  86. return _res;
  87. """
  88. f = ManualGenerator("AutoDispose", AutoDispose_body)
  89. f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup"
  90. resmethods.append(f)