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