Bump version to 1.0.
[python/dscho.git] / Mac / Modules / win / winsupport.py
blob65d70d07a95f079fa4b32f8232e0a60930d75633
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 /* Function to dispose a window, with a "normal" calling sequence */
53 static void
54 PyMac_AutoDisposeWindow(WindowPtr w)
56 DisposeWindow(w);
58 """
60 finalstuff = finalstuff + """
61 /* Return the object corresponding to the window, or NULL */
63 PyObject *
64 WinObj_WhichWindow(w)
65 WindowPtr w;
67 PyObject *it;
69 if (w == NULL) {
70 it = Py_None;
71 Py_INCREF(it);
72 } else {
73 it = (PyObject *) GetWRefCon(w);
74 if (it == NULL || ((WindowObject *)it)->ob_itself != w) {
75 it = WinObj_New(w);
76 ((WindowObject *)it)->ob_freeit = NULL;
77 } else {
78 Py_INCREF(it);
81 return it;
83 """
85 class MyObjectDefinition(GlobalObjectDefinition):
86 def outputCheckNewArg(self):
87 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
88 def outputStructMembers(self):
89 GlobalObjectDefinition.outputStructMembers(self)
90 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
91 def outputInitStructMembers(self):
92 GlobalObjectDefinition.outputInitStructMembers(self)
93 Output("SetWRefCon(itself, (long)it);")
94 Output("it->ob_freeit = PyMac_AutoDisposeWindow;")
95 def outputCheckConvertArg(self):
96 OutLbrace("if (DlgObj_Check(v))")
97 Output("*p_itself = DlgObj_ConvertToWindow(v);")
98 Output("return 1;")
99 OutRbrace()
100 Out("""
101 if (v == Py_None) { *p_itself = NULL; return 1; }
102 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
103 """)
104 def outputCleanupStructMembers(self):
105 Output("if (self->ob_itself) SetWRefCon(self->ob_itself, 0);")
106 Output("if (self->ob_freeit && self->ob_itself)")
107 OutLbrace()
108 Output("self->ob_freeit(self->ob_itself);")
109 OutRbrace()
110 Output("self->ob_itself = NULL;")
111 ## def outputFreeIt(self, itselfname):
112 ## Output("DisposeWindow(%s);", itselfname)
113 # From here on it's basically all boiler plate...
115 # Create the generator groups and link them
116 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
117 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
118 module.addobject(object)
120 # Create the generator classes used to populate the lists
121 Function = OSErrFunctionGenerator
122 Method = OSErrMethodGenerator
124 # Create and populate the lists
125 functions = []
126 methods = []
127 execfile(INPUTFILE)
129 # Add manual routines for converting integer WindowPtr's (as returned by
130 # various event routines) and Dialog objects to a WindowObject.
131 whichwin_body = """
132 long ptr;
134 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
135 return NULL;
136 return WinObj_WhichWindow((WindowPtr)ptr);
139 f = ManualGenerator("WhichWindow", whichwin_body)
140 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
142 functions.append(f)
144 # And add the routines that access the internal bits of a window struct. They
145 # are currently #defined in Windows.h, they will be real routines in Copland
146 # (at which time this execfile can go)
147 execfile(EDITFILE)
149 # add the populated lists to the generator groups
150 # (in a different wordl the scan program would generate this)
151 for f in functions: module.add(f)
152 for f in methods: object.add(f)
156 # generate output (open the output file as late as possible)
157 SetOutputFileName(OUTPUTFILE)
158 module.generate()