winsupport.py revision 3f49e4d06442a53510af6006145f9fe6d2ed921e
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 = MODNAME # 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") 52 53PropertyCreator = OSTypeType("PropertyCreator") 54PropertyTag = OSTypeType("PropertyTag") 55 56includestuff = includestuff + """ 57#include <%s>""" % MACHEADERFILE + """ 58 59#if !ACCESSOR_CALLS_ARE_FUNCTIONS 60/* Carbon calls that we emulate in classic mode */ 61#define GetWindowSpareFlag(win) (((CWindowPeek)(win))->spareFlag) 62#define GetWindowFromPort(port) ((WindowRef)(port)) 63#define GetWindowPortBounds(win, rectp) (*(rectp) = ((CWindowPeek)(win))->port.portRect) 64#endif 65#if ACCESSOR_CALLS_ARE_FUNCTIONS 66/* Classic calls that we emulate in carbon mode */ 67#define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn)) 68#define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn)) 69#define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn)) 70#endif 71 72/* Function to dispose a window, with a "normal" calling sequence */ 73static void 74PyMac_AutoDisposeWindow(WindowPtr w) 75{ 76 DisposeWindow(w); 77} 78""" 79 80finalstuff = finalstuff + """ 81/* Return the object corresponding to the window, or NULL */ 82 83PyObject * 84WinObj_WhichWindow(w) 85 WindowPtr w; 86{ 87 PyObject *it; 88 89 if (w == NULL) { 90 it = Py_None; 91 Py_INCREF(it); 92 } else { 93 it = (PyObject *) GetWRefCon(w); 94 if (it == NULL || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) { 95 it = WinObj_New(w); 96 ((WindowObject *)it)->ob_freeit = NULL; 97 } else { 98 Py_INCREF(it); 99 } 100 } 101 return it; 102} 103""" 104 105class MyObjectDefinition(GlobalObjectDefinition): 106 def outputCheckNewArg(self): 107 Output("if (itself == NULL) return PyMac_Error(resNotFound);") 108 def outputStructMembers(self): 109 GlobalObjectDefinition.outputStructMembers(self) 110 Output("void (*ob_freeit)(%s ptr);", self.itselftype) 111 def outputInitStructMembers(self): 112 GlobalObjectDefinition.outputInitStructMembers(self) 113 Output("it->ob_freeit = NULL;") 114 Output("if (GetWRefCon(itself) == 0)") 115 OutLbrace() 116 Output("SetWRefCon(itself, (long)it);") 117 Output("it->ob_freeit = PyMac_AutoDisposeWindow;") 118 OutRbrace() 119 def outputCheckConvertArg(self): 120 OutLbrace("if (DlgObj_Check(v))") 121 Output("*p_itself = DlgObj_ConvertToWindow(v);") 122 Output("return 1;") 123 OutRbrace() 124 Out(""" 125 if (v == Py_None) { *p_itself = NULL; return 1; } 126 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } 127 """) 128 def outputCleanupStructMembers(self): 129 Output("if (self->ob_freeit && self->ob_itself)") 130 OutLbrace() 131 Output("SetWRefCon(self->ob_itself, 0);") 132 Output("self->ob_freeit(self->ob_itself);") 133 OutRbrace() 134 Output("self->ob_itself = NULL;") 135 Output("self->ob_freeit = NULL;") 136 137 def outputCompare(self): 138 Output() 139 Output("static int %s_compare(self, other)", self.prefix) 140 IndentLevel() 141 Output("%s *self, *other;", self.objecttype) 142 DedentLevel() 143 OutLbrace() 144 Output("if ( self->ob_itself > other->ob_itself ) return 1;") 145 Output("if ( self->ob_itself < other->ob_itself ) return -1;") 146 Output("return 0;") 147 OutRbrace() 148 149 def outputHash(self): 150 Output() 151 Output("static int %s_hash(self)", self.prefix) 152 IndentLevel() 153 Output("%s *self;", self.objecttype) 154 DedentLevel() 155 OutLbrace() 156 Output("return (int)self->ob_itself;") 157 OutRbrace() 158 159 def outputRepr(self): 160 Output() 161 Output("static PyObject * %s_repr(self)", self.prefix) 162 IndentLevel() 163 Output("%s *self;", self.objecttype) 164 DedentLevel() 165 OutLbrace() 166 Output("char buf[100];") 167 Output("""sprintf(buf, "<Window object at 0x%%08.8x for 0x%%08.8x>", self, self->ob_itself);""") 168 Output("return PyString_FromString(buf);") 169 OutRbrace() 170 171## def outputFreeIt(self, itselfname): 172## Output("DisposeWindow(%s);", itselfname) 173# From here on it's basically all boiler plate... 174 175# Create the generator groups and link them 176module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) 177object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) 178module.addobject(object) 179 180# Create the generator classes used to populate the lists 181Function = OSErrFunctionGenerator 182Method = OSErrMethodGenerator 183 184# Create and populate the lists 185functions = [] 186methods = [] 187execfile(INPUTFILE) 188 189# Add manual routines for converting integer WindowPtr's (as returned by 190# various event routines) and Dialog objects to a WindowObject. 191whichwin_body = """ 192long ptr; 193 194if ( !PyArg_ParseTuple(_args, "i", &ptr) ) 195 return NULL; 196return WinObj_WhichWindow((WindowPtr)ptr); 197""" 198 199f = ManualGenerator("WhichWindow", whichwin_body) 200f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object" 201 202functions.append(f) 203 204# And add the routines that access the internal bits of a window struct. They 205# are currently #defined in Windows.h, they will be real routines in Copland 206# (at which time this execfile can go) 207execfile(EDITFILE) 208 209# add the populated lists to the generator groups 210# (in a different wordl the scan program would generate this) 211for f in functions: module.add(f) 212for f in methods: object.add(f) 213 214 215 216# generate output (open the output file as late as possible) 217SetOutputFileName(OUTPUTFILE) 218module.generate() 219