Another batch of updates...
[python/dscho.git] / Mac / Modules / win / winsupport.py
blob9aed179a80a633a85d4d11429b4d4cd043f56f76
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).
6 import string
8 import addpack
9 addpack.addpack(':Tools:bgen:bgen')
11 # Declarations that change for each manager
12 MACHEADERFILE = 'Windows.h' # The Apple header file
13 MODNAME = 'Win' # The name of the module
14 OBJECTNAME = 'Window' # The basic name of the objects used here
16 # The following is *usually* unchanged but may still require tuning
17 MODPREFIX = MODNAME # The prefix for module-wide routines
18 OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them
19 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
20 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
21 EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
22 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
24 from macsupport import *
26 # Create the type objects
28 WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
29 WindowRef = WindowPtr
30 WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
31 WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name
32 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
33 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
35 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
36 PicHandle = OpaqueByValueType("PicHandle", "ResObj")
38 includestuff = includestuff + """
39 #include <%s>""" % MACHEADERFILE + """
41 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
43 #ifdef HAVE_UNIVERSAL_HEADERS
44 #define WindowPeek WindowPtr
45 #endif
46 """
48 finalstuff = finalstuff + """
49 /* Return the object corresponding to the window, or NULL */
51 PyObject *
52 WinObj_WhichWindow(w)
53 WindowPtr w;
55 PyObject *it;
57 /* XXX What if we find a stdwin window or a window belonging
58 to some other package? */
59 if (w == NULL)
60 it = NULL;
61 else
62 it = (PyObject *) GetWRefCon(w);
63 if (it == NULL || ((WindowObject *)it)->ob_itself != w)
64 it = Py_None;
65 Py_INCREF(it);
66 return it;
68 """
70 class MyObjectDefinition(GlobalObjectDefinition):
71 def outputCheckNewArg(self):
72 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
73 def outputInitStructMembers(self):
74 GlobalObjectDefinition.outputInitStructMembers(self)
75 Output("SetWRefCon(itself, (long)it);")
76 def outputCheckConvertArg(self):
77 OutLbrace("if (DlgObj_Check(v))")
78 Output("*p_itself = ((WindowObject *)v)->ob_itself;")
79 Output("return 1;")
80 OutRbrace()
81 Out("""
82 if (v == Py_None) { *p_itself = NULL; return 1; }
83 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
84 """)
85 def outputFreeIt(self, itselfname):
86 Output("DisposeWindow(%s);", itselfname)
87 # From here on it's basically all boiler plate...
89 # Create the generator groups and link them
90 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
91 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
92 module.addobject(object)
94 # Create the generator classes used to populate the lists
95 Function = OSErrFunctionGenerator
96 Method = OSErrMethodGenerator
98 # Create and populate the lists
99 functions = []
100 methods = []
101 execfile(INPUTFILE)
103 # Add a manual routine for converting integer WindowPtr's (as returned by
104 # various event routines) to a WindowObject.
105 whichwin_body = """
106 long ptr;
108 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
109 return NULL;
110 return WinObj_WhichWindow((WindowPtr)ptr);
113 f = ManualGenerator("WhichWindow", whichwin_body)
114 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
116 functions.append(f)
118 # And add the routines that access the internal bits of a window struct. They
119 # are currently #defined in Windows.h, they will be real routines in Copland
120 # (at which time this execfile can go)
121 execfile(EDITFILE)
123 # add the populated lists to the generator groups
124 # (in a different wordl the scan program would generate this)
125 for f in functions: module.add(f)
126 for f in methods: object.add(f)
130 # generate output (open the output file as late as possible)
131 SetOutputFileName(OUTPUTFILE)
132 module.generate()