Bump version to 0.9.1.
[python/dscho.git] / Mac / Modules / win / winsupport.py
blobbfc5f275a67bd280eec6a5aa7b8256a0dbbc6aec
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")
37 AliasHandle = OpaqueByValueType("AliasHandle", "ResObj")
38 IconRef = OpaqueByValueType("IconRef", "ResObj")
40 WindowRegionCode = Type("WindowRegionCode", "H")
41 WindowClass = Type("WindowClass", "l")
42 WindowAttributes = Type("WindowAttributes", "l")
43 WindowPositionMethod = Type("WindowPositionMethod", "l")
44 WindowTransitionEffect = Type("WindowTransitionEffect", "l")
45 WindowTransitionAction = Type("WindowTransitionAction", "l")
46 RGBColor = OpaqueType("RGBColor", "QdRGB")
47 PropertyCreator = OSTypeType("PropertyCreator")
48 PropertyTag = OSTypeType("PropertyTag")
50 includestuff = includestuff + """
51 #include <%s>""" % MACHEADERFILE + """
52 """
54 finalstuff = finalstuff + """
55 /* Return the object corresponding to the window, or NULL */
57 PyObject *
58 WinObj_WhichWindow(w)
59 WindowPtr w;
61 PyObject *it;
63 /* XXX What if we find a stdwin window or a window belonging
64 to some other package? */
65 if (w == NULL)
66 it = NULL;
67 else
68 it = (PyObject *) GetWRefCon(w);
69 if (it == NULL || ((WindowObject *)it)->ob_itself != w)
70 it = Py_None;
71 Py_INCREF(it);
72 return it;
74 """
76 class MyObjectDefinition(GlobalObjectDefinition):
77 def outputCheckNewArg(self):
78 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
79 def outputInitStructMembers(self):
80 GlobalObjectDefinition.outputInitStructMembers(self)
81 Output("SetWRefCon(itself, (long)it);")
82 def outputCheckConvertArg(self):
83 Output("#if !TARGET_API_MAC_CARBON")
84 OutLbrace("if (DlgObj_Check(v))")
85 Output("*p_itself = ((WindowObject *)v)->ob_itself;")
86 Output("return 1;")
87 OutRbrace()
88 Output("#endif")
89 Out("""
90 if (v == Py_None) { *p_itself = NULL; return 1; }
91 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
92 """)
93 def outputFreeIt(self, itselfname):
94 Output("DisposeWindow(%s);", itselfname)
95 # From here on it's basically all boiler plate...
97 # Create the generator groups and link them
98 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
99 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
100 module.addobject(object)
102 # Create the generator classes used to populate the lists
103 Function = OSErrFunctionGenerator
104 Method = OSErrMethodGenerator
106 # Create and populate the lists
107 functions = []
108 methods = []
109 execfile(INPUTFILE)
111 # Add manual routines for converting integer WindowPtr's (as returned by
112 # various event routines) and Dialog objects to a WindowObject.
113 whichwin_body = """
114 long ptr;
116 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
117 return NULL;
118 return WinObj_WhichWindow((WindowPtr)ptr);
121 f = ManualGenerator("WhichWindow", whichwin_body)
122 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
124 functions.append(f)
126 # And add the routines that access the internal bits of a window struct. They
127 # are currently #defined in Windows.h, they will be real routines in Copland
128 # (at which time this execfile can go)
129 execfile(EDITFILE)
131 # add the populated lists to the generator groups
132 # (in a different wordl the scan program would generate this)
133 for f in functions: module.add(f)
134 for f in methods: object.add(f)
138 # generate output (open the output file as late as possible)
139 SetOutputFileName(OUTPUTFILE)
140 module.generate()