Null commit with -f option to force an uprev and put HEADs firmly on the trunk.
[python/dscho.git] / Mac / Modules / win / winsupport.py
blobcc1c312fa7773e57653434f186e78cdfde42706c
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 DragReference = OpaqueByValueType("DragReference", "DragObj")
34 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
35 PicHandle = OpaqueByValueType("PicHandle", "ResObj")
36 WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj")
37 AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj")
38 PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
39 AliasHandle = OpaqueByValueType("AliasHandle", "ResObj")
40 IconRef = OpaqueByValueType("IconRef", "ResObj")
42 WindowRegionCode = Type("WindowRegionCode", "H")
43 WindowClass = Type("WindowClass", "l")
44 WindowAttributes = Type("WindowAttributes", "l")
45 WindowPositionMethod = Type("WindowPositionMethod", "l")
46 WindowTransitionEffect = Type("WindowTransitionEffect", "l")
47 WindowTransitionAction = Type("WindowTransitionAction", "l")
48 RGBColor = OpaqueType("RGBColor", "QdRGB")
49 RGBColor_ptr = RGBColor
50 ScrollWindowOptions = Type("ScrollWindowOptions", "l")
51 WindowPartCode = Type("WindowPartCode", "h")
53 PropertyCreator = OSTypeType("PropertyCreator")
54 PropertyTag = OSTypeType("PropertyTag")
56 includestuff = includestuff + """
57 #ifdef WITHOUT_FRAMEWORKS
58 #include <Windows.h>
59 #else
60 #include <Carbon/Carbon.h>
61 #endif
63 #ifdef USE_TOOLBOX_OBJECT_GLUE
64 extern PyObject *_WinObj_New(WindowRef);
65 extern PyObject *_WinObj_WhichWindow(WindowRef);
66 extern int _WinObj_Convert(PyObject *, WindowRef *);
68 #define WinObj_New _WinObj_New
69 #define WinObj_WhichWindow _WinObj_WhichWindow
70 #define WinObj_Convert _WinObj_Convert
71 #endif
73 #if !ACCESSOR_CALLS_ARE_FUNCTIONS
74 /* Carbon calls that we emulate in classic mode */
75 #define GetWindowSpareFlag(win) (((CWindowPeek)(win))->spareFlag)
76 #define GetWindowFromPort(port) ((WindowRef)(port))
77 #define GetWindowPortBounds(win, rectp) (*(rectp) = ((CWindowPeek)(win))->port.portRect)
78 #define IsPointerValid(p) (((long)p&3) == 0)
79 #endif
80 #if ACCESSOR_CALLS_ARE_FUNCTIONS
81 /* Classic calls that we emulate in carbon mode */
82 #define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn))
83 #define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn))
84 #define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn))
85 #endif
87 /* Function to dispose a window, with a "normal" calling sequence */
88 static void
89 PyMac_AutoDisposeWindow(WindowPtr w)
91 DisposeWindow(w);
93 """
95 finalstuff = finalstuff + """
96 /* Return the object corresponding to the window, or NULL */
98 PyObject *
99 WinObj_WhichWindow(WindowPtr w)
101 PyObject *it;
103 if (w == NULL) {
104 it = Py_None;
105 Py_INCREF(it);
106 } else {
107 it = (PyObject *) GetWRefCon(w);
108 if (it == NULL || !IsPointerValid((Ptr)it) || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) {
109 it = WinObj_New(w);
110 ((WindowObject *)it)->ob_freeit = NULL;
111 } else {
112 Py_INCREF(it);
115 return it;
119 initstuff = initstuff + """
120 PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_New);
121 PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_WhichWindow);
122 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(WindowPtr, WinObj_Convert);
125 class MyObjectDefinition(GlobalObjectDefinition):
126 def outputCheckNewArg(self):
127 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
128 def outputStructMembers(self):
129 GlobalObjectDefinition.outputStructMembers(self)
130 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
131 def outputInitStructMembers(self):
132 GlobalObjectDefinition.outputInitStructMembers(self)
133 Output("it->ob_freeit = NULL;")
134 Output("if (GetWRefCon(itself) == 0)")
135 OutLbrace()
136 Output("SetWRefCon(itself, (long)it);")
137 Output("it->ob_freeit = PyMac_AutoDisposeWindow;")
138 OutRbrace()
139 def outputCheckConvertArg(self):
140 Out("""
141 if (v == Py_None) { *p_itself = NULL; return 1; }
142 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
143 """)
144 OutLbrace()
145 Output("DialogRef dlg;")
146 OutLbrace("if (DlgObj_Convert(v, &dlg) && dlg)")
147 Output("*p_itself = GetDialogWindow(dlg);")
148 Output("return 1;")
149 OutRbrace()
150 Output("PyErr_Clear();")
151 OutRbrace()
152 def outputCleanupStructMembers(self):
153 Output("if (self->ob_freeit && self->ob_itself)")
154 OutLbrace()
155 Output("SetWRefCon(self->ob_itself, 0);")
156 Output("self->ob_freeit(self->ob_itself);")
157 OutRbrace()
158 Output("self->ob_itself = NULL;")
159 Output("self->ob_freeit = NULL;")
161 def outputCompare(self):
162 Output()
163 Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
164 OutLbrace()
165 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
166 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
167 Output("return 0;")
168 OutRbrace()
170 def outputHash(self):
171 Output()
172 Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
173 OutLbrace()
174 Output("return (int)self->ob_itself;")
175 OutRbrace()
177 def outputRepr(self):
178 Output()
179 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
180 OutLbrace()
181 Output("char buf[100];")
182 Output("""sprintf(buf, "<Window object at 0x%%08.8x for 0x%%08.8x>", self, self->ob_itself);""")
183 Output("return PyString_FromString(buf);")
184 OutRbrace()
186 ## def outputFreeIt(self, itselfname):
187 ## Output("DisposeWindow(%s);", itselfname)
188 # From here on it's basically all boiler plate...
190 # Create the generator groups and link them
191 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
192 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
193 module.addobject(object)
195 # Create the generator classes used to populate the lists
196 Function = OSErrFunctionGenerator
197 Method = OSErrMethodGenerator
199 # Create and populate the lists
200 functions = []
201 methods = []
202 execfile(INPUTFILE)
204 # Add manual routines for converting integer WindowPtr's (as returned by
205 # various event routines) and Dialog objects to a WindowObject.
206 whichwin_body = """
207 long ptr;
209 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
210 return NULL;
211 return WinObj_WhichWindow((WindowPtr)ptr);
214 f = ManualGenerator("WhichWindow", whichwin_body)
215 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
217 functions.append(f)
219 # And add the routines that access the internal bits of a window struct. They
220 # are currently #defined in Windows.h, they will be real routines in Copland
221 # (at which time this execfile can go)
222 execfile(EDITFILE)
224 # add the populated lists to the generator groups
225 # (in a different wordl the scan program would generate this)
226 for f in functions: module.add(f)
227 for f in methods: object.add(f)
231 # generate output (open the output file as late as possible)
232 SetOutputFileName(OUTPUTFILE)
233 module.generate()