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 6import string 7 8# Declarations that change for each manager 9MACHEADERFILE = 'Windows.h' # The Apple header file 10MODNAME = '_Win' # The name of the module 11OBJECTNAME = 'Window' # The basic name of the objects used here 12 13# The following is *usually* unchanged but may still require tuning 14MODPREFIX = 'Win' # The prefix for module-wide routines 15OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them 16OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods 17INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner 18EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions 19OUTPUTFILE = MODNAME + "module.c" # The file generated by this program 20 21from macsupport import * 22 23# Create the type objects 24 25WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) 26WindowRef = WindowPtr 27WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX) 28WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name 29CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") 30GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") 31 32DragReference = OpaqueByValueType("DragReference", "DragObj") 33 34RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") 35PicHandle = OpaqueByValueType("PicHandle", "ResObj") 36WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj") 37AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj") 38PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj") 39AliasHandle = OpaqueByValueType("AliasHandle", "ResObj") 40IconRef = OpaqueByValueType("IconRef", "ResObj") 41 42WindowRegionCode = Type("WindowRegionCode", "H") 43WindowClass = Type("WindowClass", "l") 44WindowAttributes = Type("WindowAttributes", "l") 45WindowPositionMethod = Type("WindowPositionMethod", "l") 46WindowTransitionEffect = Type("WindowTransitionEffect", "l") 47WindowTransitionAction = Type("WindowTransitionAction", "l") 48RGBColor = OpaqueType("RGBColor", "QdRGB") 49RGBColor_ptr = RGBColor 50ScrollWindowOptions = Type("ScrollWindowOptions", "l") 51WindowPartCode = Type("WindowPartCode", "h") 52WindowDefPartCode = Type("WindowDefPartCode", "h") 53WindowModality = Type("WindowModality", "l") 54GDHandle = OpaqueByValueType("GDHandle", "ResObj") 55WindowConstrainOptions = Type("WindowConstrainOptions", "l") 56 57PropertyCreator = OSTypeType("PropertyCreator") 58PropertyTag = OSTypeType("PropertyTag") 59 60includestuff = includestuff + """ 61#include <Carbon/Carbon.h> 62 63#ifdef USE_TOOLBOX_OBJECT_GLUE 64extern PyObject *_WinObj_New(WindowRef); 65extern PyObject *_WinObj_WhichWindow(WindowRef); 66extern int _WinObj_Convert(PyObject *, WindowRef *); 67 68#define WinObj_New _WinObj_New 69#define WinObj_WhichWindow _WinObj_WhichWindow 70#define WinObj_Convert _WinObj_Convert 71#endif 72 73/* Classic calls that we emulate in carbon mode */ 74#define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn)) 75#define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn)) 76#define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn)) 77 78/* Function to dispose a window, with a "normal" calling sequence */ 79static void 80PyMac_AutoDisposeWindow(WindowPtr w) 81{ 82 DisposeWindow(w); 83} 84""" 85 86finalstuff = finalstuff + """ 87/* Return the object corresponding to the window, or NULL */ 88 89PyObject * 90WinObj_WhichWindow(WindowPtr w) 91{ 92 PyObject *it; 93 94 if (w == NULL) { 95 it = Py_None; 96 Py_INCREF(it); 97 } else { 98 it = (PyObject *) GetWRefCon(w); 99 if (it == NULL || !IsPointerValid((Ptr)it) || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) { 100 it = WinObj_New(w); 101 ((WindowObject *)it)->ob_freeit = NULL; 102 } else { 103 Py_INCREF(it); 104 } 105 } 106 return it; 107} 108""" 109 110initstuff = initstuff + """ 111 PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_New); 112 PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_WhichWindow); 113 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(WindowPtr, WinObj_Convert); 114""" 115 116class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): 117 def outputCheckNewArg(self): 118 Output("if (itself == NULL) return PyMac_Error(resNotFound);") 119 Output("/* XXXX Or should we use WhichWindow code here? */") 120 def outputStructMembers(self): 121 GlobalObjectDefinition.outputStructMembers(self) 122 Output("void (*ob_freeit)(%s ptr);", self.itselftype) 123 def outputInitStructMembers(self): 124 GlobalObjectDefinition.outputInitStructMembers(self) 125 Output("it->ob_freeit = NULL;") 126 Output("if (GetWRefCon(itself) == 0)") 127 OutLbrace() 128 Output("SetWRefCon(itself, (long)it);") 129 Output("it->ob_freeit = PyMac_AutoDisposeWindow;") 130 OutRbrace() 131 def outputCheckConvertArg(self): 132 Out(""" 133 if (v == Py_None) { *p_itself = NULL; return 1; } 134 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } 135 """) 136 OutLbrace() 137 Output("DialogRef dlg;") 138 OutLbrace("if (DlgObj_Convert(v, &dlg) && dlg)") 139 Output("*p_itself = GetDialogWindow(dlg);") 140 Output("return 1;") 141 OutRbrace() 142 Output("PyErr_Clear();") 143 OutRbrace() 144 def outputCleanupStructMembers(self): 145 Output("if (self->ob_freeit && self->ob_itself)") 146 OutLbrace() 147 Output("SetWRefCon(self->ob_itself, 0);") 148 Output("self->ob_freeit(self->ob_itself);") 149 OutRbrace() 150 Output("self->ob_itself = NULL;") 151 Output("self->ob_freeit = NULL;") 152 153 def outputCompare(self): 154 Output() 155 Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype) 156 OutLbrace() 157 Output("if ( self->ob_itself > other->ob_itself ) return 1;") 158 Output("if ( self->ob_itself < other->ob_itself ) return -1;") 159 Output("return 0;") 160 OutRbrace() 161 162 def outputHash(self): 163 Output() 164 Output("static int %s_hash(%s *self)", self.prefix, self.objecttype) 165 OutLbrace() 166 Output("return (int)self->ob_itself;") 167 OutRbrace() 168 169 def outputRepr(self): 170 Output() 171 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype) 172 OutLbrace() 173 Output("char buf[100];") 174 Output("""sprintf(buf, "<Window object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""") 175 Output("return PyString_FromString(buf);") 176 OutRbrace() 177 178## def outputFreeIt(self, itselfname): 179## Output("DisposeWindow(%s);", itselfname) 180# From here on it's basically all boiler plate... 181 182# Create the generator groups and link them 183module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) 184object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) 185module.addobject(object) 186 187# Create the generator classes used to populate the lists 188Function = OSErrWeakLinkFunctionGenerator 189Method = OSErrWeakLinkMethodGenerator 190 191# Create and populate the lists 192functions = [] 193methods = [] 194execfile(INPUTFILE) 195 196# Add manual routines for converting integer WindowPtr's (as returned by 197# various event routines) and Dialog objects to a WindowObject. 198whichwin_body = """ 199long ptr; 200 201if ( !PyArg_ParseTuple(_args, "i", &ptr) ) 202 return NULL; 203_res = WinObj_WhichWindow((WindowPtr)ptr); 204return _res; 205""" 206 207f = ManualGenerator("WhichWindow", whichwin_body) 208f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object" 209 210functions.append(f) 211 212# And add the routines that access the internal bits of a window struct. They 213# are currently #defined in Windows.h, they will be real routines in Copland 214# (at which time this execfile can go) 215execfile(EDITFILE) 216 217# add the populated lists to the generator groups 218# (in a different wordl the scan program would generate this) 219for f in functions: module.add(f) 220for f in methods: object.add(f) 221 222 223 224# generate output (open the output file as late as possible) 225SetOutputFileName(OUTPUTFILE) 226module.generate() 227