changes by Barry, e.g. font lock & email addresses
[python/dscho.git] / Mac / Modules / win / winsupport.py
blob1f0782862a00801efa61ae110b8ccf223158792b
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 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
23 from macsupport import *
25 # Create the type objects
27 WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
28 WindowRef = WindowPtr
29 WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
30 WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name
32 #RgnHandle = FakeType("theWindow->updtRgn") # XXX
34 includestuff = includestuff + """
35 #include <%s>""" % MACHEADERFILE + """
37 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
39 #ifdef HAVE_UNIVERSAL_HEADERS
40 #define WindowPeek WindowPtr
41 #endif
42 """
44 finalstuff = finalstuff + """
45 /* Return the object corresponding to the window, or NULL */
47 PyObject *
48 WinObj_WhichWindow(w)
49 WindowPtr w;
51 PyObject *it;
53 /* XXX What if we find a stdwin window or a window belonging
54 to some other package? */
55 if (w == NULL)
56 it = NULL;
57 else
58 it = (PyObject *) GetWRefCon(w);
59 if (it == NULL || ((WindowObject *)it)->ob_itself != w)
60 it = Py_None;
61 Py_INCREF(it);
62 return it;
64 """
66 class MyObjectDefinition(GlobalObjectDefinition):
67 def outputCheckNewArg(self):
68 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
69 def outputInitStructMembers(self):
70 GlobalObjectDefinition.outputInitStructMembers(self)
71 Output("SetWRefCon(itself, (long)it);")
72 def outputCheckConvertArg(self):
73 OutLbrace("if (DlgObj_Check(v))")
74 Output("*p_itself = ((WindowObject *)v)->ob_itself;")
75 Output("return 1;")
76 OutRbrace()
77 Out("""
78 if (v == Py_None) { *p_itself = NULL; return 1; }
79 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
80 """)
81 def outputFreeIt(self, itselfname):
82 Output("DisposeWindow(%s);", itselfname)
84 # From here on it's basically all boiler plate...
86 # Create the generator groups and link them
87 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
88 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
89 module.addobject(object)
91 # Create the generator classes used to populate the lists
92 Function = OSErrFunctionGenerator
93 Method = OSErrMethodGenerator
95 # Create and populate the lists
96 functions = []
97 methods = []
98 execfile(INPUTFILE)
100 # Add a manual routine for converting integer WindowPtr's (as returned by
101 # various event routines) to a WindowObject.
102 whichwin_body = """
103 long ptr;
105 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
106 return NULL;
107 return WinObj_WhichWindow((WindowPtr)ptr);
110 f = ManualGenerator("WhichWindow", whichwin_body)
111 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
113 functions.append(f)
115 # add the populated lists to the generator groups
116 # (in a different wordl the scan program would generate this)
117 for f in functions: module.add(f)
118 for f in methods: object.add(f)
122 # generate output (open the output file as late as possible)
123 SetOutputFileName(OUTPUTFILE)
124 module.generate()