/Mac/Modules/drag/dragsupport.py

http://unladen-swallow.googlecode.com/ · Python · 293 lines · 246 code · 26 blank · 21 comment · 2 complexity · 21e770a2b786eaf7d71fe9d48c77b496 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. import string
  6. # Declarations that change for each manager
  7. MACHEADERFILE = 'Drag.h' # The Apple header file
  8. MODNAME = '_Drag' # The name of the module
  9. OBJECTNAME = 'DragObj' # The basic name of the objects used here
  10. # The following is *usually* unchanged but may still require tuning
  11. MODPREFIX = 'Drag' # The prefix for module-wide routines
  12. OBJECTTYPE = 'DragRef' # The C type used to represent them
  13. OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
  14. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  15. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  16. from macsupport import *
  17. # Create the type objects
  18. DragRef = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
  19. DragItemRef = Type("ItemReference", "l")
  20. # Old names
  21. DragReference = DragRef
  22. ItemReference = DragItemRef
  23. PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
  24. RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
  25. AEDesc = OpaqueType('AEDesc')
  26. AEDesc_ptr = AEDesc
  27. RGBColor = OpaqueType("RGBColor", "QdRGB")
  28. FlavorType = OSTypeType("FlavorType")
  29. DragAttributes = Type("DragAttributes", "l")
  30. DragBehaviors = Type("DragBehaviors", "l")
  31. DragImageFlags = Type("DragImageFlags", "l")
  32. DragImageTranslucency = Type("DragImageTranslucency", "l")
  33. DragRegionMessage = Type("DragRegionMessage", "h")
  34. ZoomAcceleration = Type("ZoomAcceleration", "h")
  35. FlavorFlags = Type("FlavorFlags", "l")
  36. DragTrackingMessage = Type("DragTrackingMessage", "h")
  37. includestuff = includestuff + """
  38. #include <Carbon/Carbon.h>
  39. /* Callback glue routines */
  40. DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
  41. DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
  42. DragSendDataUPP dragglue_SendDataUPP;
  43. #if 0
  44. DragInputUPP dragglue_InputUPP;
  45. DragDrawingUPP dragglue_DrawingUPP;
  46. #endif
  47. #ifdef USE_TOOLBOX_OBJECT_GLUE
  48. extern PyObject *_DragObj_New(DragRef);
  49. extern int _DragObj_Convert(PyObject *, DragRef *);
  50. #define DragObj_New _DragObj_New
  51. #define DragObj_Convert _DragObj_Convert
  52. #endif
  53. """
  54. finalstuff = finalstuff + """
  55. static pascal OSErr
  56. dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
  57. void *handlerRefCon, DragReference theDrag)
  58. {
  59. PyObject *args, *rv;
  60. int i;
  61. args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
  62. if ( args == NULL )
  63. return -1;
  64. rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
  65. Py_DECREF(args);
  66. if ( rv == NULL ) {
  67. PySys_WriteStderr("Drag: Exception in TrackingHandler\\n");
  68. PyErr_Print();
  69. return -1;
  70. }
  71. i = -1;
  72. if ( rv == Py_None )
  73. i = 0;
  74. else
  75. PyArg_Parse(rv, "l", &i);
  76. Py_DECREF(rv);
  77. return i;
  78. }
  79. static pascal OSErr
  80. dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
  81. DragReference theDrag)
  82. {
  83. PyObject *args, *rv;
  84. int i;
  85. args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
  86. if ( args == NULL )
  87. return -1;
  88. rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
  89. Py_DECREF(args);
  90. if ( rv == NULL ) {
  91. PySys_WriteStderr("Drag: Exception in ReceiveHandler\\n");
  92. PyErr_Print();
  93. return -1;
  94. }
  95. i = -1;
  96. if ( rv == Py_None )
  97. i = 0;
  98. else
  99. PyArg_Parse(rv, "l", &i);
  100. Py_DECREF(rv);
  101. return i;
  102. }
  103. static pascal OSErr
  104. dragglue_SendData(FlavorType theType, void *dragSendRefCon,
  105. ItemReference theItem, DragReference theDrag)
  106. {
  107. DragObjObject *self = (DragObjObject *)dragSendRefCon;
  108. PyObject *args, *rv;
  109. int i;
  110. if ( self->sendproc == NULL )
  111. return -1;
  112. args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
  113. if ( args == NULL )
  114. return -1;
  115. rv = PyEval_CallObject(self->sendproc, args);
  116. Py_DECREF(args);
  117. if ( rv == NULL ) {
  118. PySys_WriteStderr("Drag: Exception in SendDataHandler\\n");
  119. PyErr_Print();
  120. return -1;
  121. }
  122. i = -1;
  123. if ( rv == Py_None )
  124. i = 0;
  125. else
  126. PyArg_Parse(rv, "l", &i);
  127. Py_DECREF(rv);
  128. return i;
  129. }
  130. #if 0
  131. static pascal OSErr
  132. dragglue_Input(Point *mouse, short *modifiers,
  133. void *dragSendRefCon, DragReference theDrag)
  134. {
  135. return 0;
  136. }
  137. static pascal OSErr
  138. dragglue_Drawing(xxxx
  139. void *dragSendRefCon, DragReference theDrag)
  140. {
  141. return 0;
  142. }
  143. #endif
  144. """
  145. initstuff = initstuff + """
  146. PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New);
  147. PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert);
  148. """
  149. variablestuff = """
  150. dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler);
  151. dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler);
  152. dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData);
  153. #if 0
  154. dragglue_InputUPP = NewDragInputUPP(dragglue_Input);
  155. dragglue_DrawingUPP = NewDragDrawingUPP(dragglue_Drawing);
  156. #endif
  157. """
  158. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  159. def outputCheckNewArg(self):
  160. Output("""if (itself == NULL) {
  161. PyErr_SetString(Drag_Error,"Cannot create null Drag");
  162. return NULL;
  163. }""")
  164. def outputFreeIt(self, itselfname):
  165. ## Output("DisposeDrag(%s);", itselfname)
  166. Output("Py_XDECREF(self->sendproc);")
  167. ## Output("Py_XDECREF(self->inputproc);")
  168. ## Output("Py_XDECREF(self->drawingproc);")
  169. def outputStructMembers(self):
  170. GlobalObjectDefinition.outputStructMembers(self)
  171. Output("PyObject *sendproc;")
  172. ## Output("PyObject *inputproc;")
  173. ## Output("PyObject *drawingproc;")
  174. def outputInitStructMembers(self):
  175. GlobalObjectDefinition.outputInitStructMembers(self)
  176. Output("it->sendproc = NULL;")
  177. ## Output("it->inputproc = NULL;")
  178. ## Output("it->drawingproc = NULL;")
  179. # Create the generator groups and link them
  180. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
  181. object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  182. module.addobject(object)
  183. # Create the generator classes used to populate the lists
  184. Function = OSErrWeakLinkFunctionGenerator
  185. Method = OSErrWeakLinkMethodGenerator
  186. # Create and populate the lists
  187. functions = []
  188. methods = []
  189. execfile(INPUTFILE)
  190. # add the populated lists to the generator groups
  191. for f in functions: module.add(f)
  192. for f in methods: object.add(f)
  193. # Manual generators for the callbacks
  194. installtracking_body = """
  195. PyObject *callback;
  196. WindowPtr theWindow = NULL;
  197. OSErr _err;
  198. if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
  199. return NULL;
  200. Py_INCREF(callback); /* Cannot decref later, too bad */
  201. _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback);
  202. if (_err != noErr) return PyMac_Error(_err);
  203. Py_INCREF(Py_None);
  204. _res = Py_None;
  205. return _res;
  206. """
  207. installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body)
  208. module.add(installtracking)
  209. installreceive_body = """
  210. PyObject *callback;
  211. WindowPtr theWindow = NULL;
  212. OSErr _err;
  213. if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
  214. return NULL;
  215. Py_INCREF(callback); /* Cannot decref later, too bad */
  216. _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback);
  217. if (_err != noErr) return PyMac_Error(_err);
  218. Py_INCREF(Py_None);
  219. _res = Py_None;
  220. return _res;
  221. """
  222. installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body)
  223. module.add(installreceive)
  224. removetracking_body = """
  225. WindowPtr theWindow = NULL;
  226. OSErr _err;
  227. if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
  228. return NULL;
  229. _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
  230. if (_err != noErr) return PyMac_Error(_err);
  231. Py_INCREF(Py_None);
  232. _res = Py_None;
  233. return _res;
  234. """
  235. removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body)
  236. module.add(removetracking)
  237. removereceive_body = """
  238. WindowPtr theWindow = NULL;
  239. OSErr _err;
  240. if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
  241. return NULL;
  242. _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
  243. if (_err != noErr) return PyMac_Error(_err);
  244. Py_INCREF(Py_None);
  245. _res = Py_None;
  246. return _res;
  247. """
  248. removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body)
  249. module.add(removereceive)
  250. # generate output (open the output file as late as possible)
  251. SetOutputFileName(OUTPUTFILE)
  252. module.generate()