/Mac/Modules/cg/cgsupport.py

http://unladen-swallow.googlecode.com/ · Python · 192 lines · 141 code · 34 blank · 17 comment · 1 complexity · ef9ab44be53abfce4b7d1f44a3805691 MD5 · raw file

  1. # This script generates a Python interface for an Apple Macintosh Manager.
  2. # It uses the "bgen" package to generate C code.
  3. # The function specifications are generated by scanning the mamager's header file,
  4. # using the "scantools" package (customized for this particular manager).
  5. #error missing SetActionFilter
  6. import string
  7. # Declarations that change for each manager
  8. MODNAME = '_CG' # The name of the module
  9. # The following is *usually* unchanged but may still require tuning
  10. MODPREFIX = 'CG' # The prefix for module-wide routines
  11. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  12. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  13. from macsupport import *
  14. CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
  15. RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
  16. # Create the type objects
  17. includestuff = includestuff + """
  18. #include <ApplicationServices/ApplicationServices.h>
  19. extern int GrafObj_Convert(PyObject *, GrafPtr *);
  20. /*
  21. ** Manual converters
  22. */
  23. PyObject *CGPoint_New(CGPoint *itself)
  24. {
  25. return Py_BuildValue("(ff)",
  26. itself->x,
  27. itself->y);
  28. }
  29. int
  30. CGPoint_Convert(PyObject *v, CGPoint *p_itself)
  31. {
  32. if( !PyArg_Parse(v, "(ff)",
  33. &p_itself->x,
  34. &p_itself->y) )
  35. return 0;
  36. return 1;
  37. }
  38. PyObject *CGRect_New(CGRect *itself)
  39. {
  40. return Py_BuildValue("(ffff)",
  41. itself->origin.x,
  42. itself->origin.y,
  43. itself->size.width,
  44. itself->size.height);
  45. }
  46. int
  47. CGRect_Convert(PyObject *v, CGRect *p_itself)
  48. {
  49. if( !PyArg_Parse(v, "(ffff)",
  50. &p_itself->origin.x,
  51. &p_itself->origin.y,
  52. &p_itself->size.width,
  53. &p_itself->size.height) )
  54. return 0;
  55. return 1;
  56. }
  57. PyObject *CGAffineTransform_New(CGAffineTransform *itself)
  58. {
  59. return Py_BuildValue("(ffffff)",
  60. itself->a,
  61. itself->b,
  62. itself->c,
  63. itself->d,
  64. itself->tx,
  65. itself->ty);
  66. }
  67. int
  68. CGAffineTransform_Convert(PyObject *v, CGAffineTransform *p_itself)
  69. {
  70. if( !PyArg_Parse(v, "(ffffff)",
  71. &p_itself->a,
  72. &p_itself->b,
  73. &p_itself->c,
  74. &p_itself->d,
  75. &p_itself->tx,
  76. &p_itself->ty) )
  77. return 0;
  78. return 1;
  79. }
  80. """
  81. class MyOpaqueByValueType(OpaqueByValueType):
  82. """Sort of a mix between OpaqueByValueType and OpaqueType."""
  83. def mkvalueArgs(self, name):
  84. return "%s, &%s" % (self.new, name)
  85. CGPoint = MyOpaqueByValueType('CGPoint', 'CGPoint')
  86. CGRect = MyOpaqueByValueType('CGRect', 'CGRect')
  87. CGAffineTransform = MyOpaqueByValueType('CGAffineTransform', 'CGAffineTransform')
  88. char_ptr = Type("char *", "s")
  89. CGTextEncoding = int
  90. CGLineCap = int
  91. CGLineJoin = int
  92. CGTextDrawingMode = int
  93. CGPathDrawingMode = int
  94. CGInterpolationQuality = int
  95. # The real objects
  96. CGContextRef = OpaqueByValueType("CGContextRef", "CGContextRefObj")
  97. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  98. def outputStructMembers(self):
  99. ObjectDefinition.outputStructMembers(self)
  100. def outputCleanupStructMembers(self):
  101. Output("CGContextRelease(self->ob_itself);")
  102. # Create the generator groups and link them
  103. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  104. CGContextRef_object = MyObjectDefinition('CGContextRef', 'CGContextRefObj', 'CGContextRef')
  105. # ADD object here
  106. module.addobject(CGContextRef_object)
  107. Function = FunctionGenerator
  108. Method = MethodGenerator
  109. CGContextRef_methods = []
  110. # ADD _methods initializer here
  111. execfile(INPUTFILE)
  112. # manual method, lives in Quickdraw.h
  113. f = Method(void, 'SyncCGContextOriginWithPort',
  114. (CGContextRef, 'ctx', InMode),
  115. (CGrafPtr, 'port', InMode),
  116. )
  117. CGContextRef_methods.append(f)
  118. # manual method, lives in Quickdraw.h
  119. f = Method(void, 'ClipCGContextToRegion',
  120. (CGContextRef, 'ctx', InMode),
  121. (Rect, 'portRect', InMode),
  122. (RgnHandle, 'region', InMode),
  123. )
  124. CGContextRef_methods.append(f)
  125. CreateCGContextForPort_body = """\
  126. GrafPtr port;
  127. CGContextRef ctx;
  128. OSStatus _err;
  129. if (!PyArg_ParseTuple(_args, "O&", GrafObj_Convert, &port))
  130. return NULL;
  131. _err = CreateCGContextForPort(port, &ctx);
  132. if (_err != noErr)
  133. if (_err != noErr) return PyMac_Error(_err);
  134. _res = Py_BuildValue("O&", CGContextRefObj_New, ctx);
  135. return _res;
  136. """
  137. f = ManualGenerator("CreateCGContextForPort", CreateCGContextForPort_body);
  138. f.docstring = lambda: "(CGrafPtr) -> CGContextRef"
  139. module.add(f)
  140. # ADD add forloop here
  141. for f in CGContextRef_methods:
  142. CGContextRef_object.add(f)
  143. # generate output (open the output file as late as possible)
  144. SetOutputFileName(OUTPUTFILE)
  145. module.generate()