Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 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). |
| 5 | |
| 6 | import string |
| 7 | |
| 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 |
| 12 | |
| 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 |
Jack Jansen | 330f576 | 1995-11-14 10:48:54 +0000 | [diff] [blame] | 18 | EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 19 | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
| 20 | |
| 21 | from macsupport import * |
| 22 | |
| 23 | # Create the type objects |
| 24 | |
| 25 | WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 26 | WindowRef = WindowPtr |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 27 | WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX) |
| 28 | WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name |
Jack Jansen | b7abb18 | 1995-11-15 15:18:47 +0000 | [diff] [blame] | 29 | CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
| 30 | GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 31 | |
Jack Jansen | 723ad8a | 2000-12-12 22:10:21 +0000 | [diff] [blame] | 32 | DragReference = OpaqueByValueType("DragReference", "DragObj") |
| 33 | |
Jack Jansen | b7abb18 | 1995-11-15 15:18:47 +0000 | [diff] [blame] | 34 | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
Jack Jansen | 330f576 | 1995-11-14 10:48:54 +0000 | [diff] [blame] | 35 | PicHandle = OpaqueByValueType("PicHandle", "ResObj") |
Jack Jansen | 8f0fab7 | 1997-08-15 14:38:05 +0000 | [diff] [blame] | 36 | WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj") |
| 37 | AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj") |
| 38 | PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj") |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 39 | AliasHandle = OpaqueByValueType("AliasHandle", "ResObj") |
| 40 | IconRef = OpaqueByValueType("IconRef", "ResObj") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 41 | |
Jack Jansen | 0b13e7c | 2000-07-07 13:09:35 +0000 | [diff] [blame] | 42 | WindowRegionCode = Type("WindowRegionCode", "H") |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 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") |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 48 | RGBColor = OpaqueType("RGBColor", "QdRGB") |
Jack Jansen | 723ad8a | 2000-12-12 22:10:21 +0000 | [diff] [blame] | 49 | RGBColor_ptr = RGBColor |
| 50 | ScrollWindowOptions = Type("ScrollWindowOptions", "l") |
| 51 | WindowPartCode = Type("WindowPartCode", "h") |
| 52 | |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 53 | PropertyCreator = OSTypeType("PropertyCreator") |
| 54 | PropertyTag = OSTypeType("PropertyTag") |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 56 | includestuff = includestuff + """ |
| 57 | #include <%s>""" % MACHEADERFILE + """ |
Jack Jansen | 723ad8a | 2000-12-12 22:10:21 +0000 | [diff] [blame] | 58 | |
| 59 | #if !ACCESSOR_CALLS_ARE_FUNCTIONS |
Jack Jansen | 3224865 | 2000-12-19 22:23:06 +0000 | [diff] [blame] | 60 | /* Carbon calls that we emulate in classic mode */ |
Jack Jansen | 723ad8a | 2000-12-12 22:10:21 +0000 | [diff] [blame] | 61 | #define GetWindowSpareFlag(win) (((CWindowPeek)(win))->spareFlag) |
| 62 | #define GetWindowFromPort(port) ((WindowRef)(port)) |
| 63 | #define GetWindowPortBounds(win, rectp) (*(rectp) = ((CWindowPeek)(win))->port.portRect) |
| 64 | #endif |
Jack Jansen | 3224865 | 2000-12-19 22:23:06 +0000 | [diff] [blame] | 65 | #if ACCESSOR_CALLS_ARE_FUNCTIONS |
| 66 | /* Classic calls that we emulate in carbon mode */ |
| 67 | #define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn)) |
| 68 | #define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn)) |
| 69 | #define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn)) |
| 70 | #endif |
Jack Jansen | 723ad8a | 2000-12-12 22:10:21 +0000 | [diff] [blame] | 71 | |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 72 | /* Function to dispose a window, with a "normal" calling sequence */ |
| 73 | static void |
| 74 | PyMac_AutoDisposeWindow(WindowPtr w) |
| 75 | { |
| 76 | DisposeWindow(w); |
| 77 | } |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 78 | """ |
| 79 | |
| 80 | finalstuff = finalstuff + """ |
| 81 | /* Return the object corresponding to the window, or NULL */ |
| 82 | |
| 83 | PyObject * |
| 84 | WinObj_WhichWindow(w) |
| 85 | WindowPtr w; |
| 86 | { |
| 87 | PyObject *it; |
| 88 | |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 89 | if (w == NULL) { |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 90 | it = Py_None; |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 91 | Py_INCREF(it); |
| 92 | } else { |
| 93 | it = (PyObject *) GetWRefCon(w); |
Jack Jansen | 80716f0 | 2000-12-14 22:29:00 +0000 | [diff] [blame] | 94 | if (it == NULL || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) { |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 95 | it = WinObj_New(w); |
| 96 | ((WindowObject *)it)->ob_freeit = NULL; |
| 97 | } else { |
| 98 | Py_INCREF(it); |
| 99 | } |
| 100 | } |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 101 | return it; |
| 102 | } |
| 103 | """ |
| 104 | |
| 105 | class MyObjectDefinition(GlobalObjectDefinition): |
| 106 | def outputCheckNewArg(self): |
| 107 | Output("if (itself == NULL) return PyMac_Error(resNotFound);") |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 108 | def outputStructMembers(self): |
| 109 | GlobalObjectDefinition.outputStructMembers(self) |
| 110 | Output("void (*ob_freeit)(%s ptr);", self.itselftype) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 111 | def outputInitStructMembers(self): |
| 112 | GlobalObjectDefinition.outputInitStructMembers(self) |
Jack Jansen | 80716f0 | 2000-12-14 22:29:00 +0000 | [diff] [blame] | 113 | Output("it->ob_freeit = NULL;") |
| 114 | Output("if (GetWRefCon(itself) == 0)") |
| 115 | OutLbrace() |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 116 | Output("SetWRefCon(itself, (long)it);") |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 117 | Output("it->ob_freeit = PyMac_AutoDisposeWindow;") |
Jack Jansen | 80716f0 | 2000-12-14 22:29:00 +0000 | [diff] [blame] | 118 | OutRbrace() |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 119 | def outputCheckConvertArg(self): |
| 120 | OutLbrace("if (DlgObj_Check(v))") |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 121 | Output("*p_itself = DlgObj_ConvertToWindow(v);") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 122 | Output("return 1;") |
| 123 | OutRbrace() |
| 124 | Out(""" |
| 125 | if (v == Py_None) { *p_itself = NULL; return 1; } |
| 126 | if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } |
| 127 | """) |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 128 | def outputCleanupStructMembers(self): |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 129 | Output("if (self->ob_freeit && self->ob_itself)") |
| 130 | OutLbrace() |
Jack Jansen | 80716f0 | 2000-12-14 22:29:00 +0000 | [diff] [blame] | 131 | Output("SetWRefCon(self->ob_itself, 0);") |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 132 | Output("self->ob_freeit(self->ob_itself);") |
| 133 | OutRbrace() |
| 134 | Output("self->ob_itself = NULL;") |
Jack Jansen | 80716f0 | 2000-12-14 22:29:00 +0000 | [diff] [blame] | 135 | Output("self->ob_freeit = NULL;") |
Jack Jansen | 87a3092 | 2000-12-19 21:34:55 +0000 | [diff] [blame] | 136 | |
| 137 | def outputCompare(self): |
| 138 | Output() |
| 139 | Output("static int %s_compare(self, other)", self.prefix) |
| 140 | IndentLevel() |
| 141 | Output("%s *self, *other;", self.objecttype) |
| 142 | DedentLevel() |
| 143 | OutLbrace() |
| 144 | Output("if ( self->ob_itself > other->ob_itself ) return 1;") |
| 145 | Output("if ( self->ob_itself < other->ob_itself ) return -1;") |
| 146 | Output("return 0;") |
| 147 | OutRbrace() |
| 148 | |
| 149 | def outputHash(self): |
| 150 | Output() |
| 151 | Output("static int %s_hash(self)", self.prefix) |
| 152 | IndentLevel() |
| 153 | Output("%s *self;", self.objecttype) |
| 154 | DedentLevel() |
| 155 | OutLbrace() |
| 156 | Output("return (int)self->ob_itself;") |
| 157 | OutRbrace() |
| 158 | |
Jack Jansen | 0aee0e6 | 2000-08-25 22:17:51 +0000 | [diff] [blame] | 159 | ## def outputFreeIt(self, itselfname): |
| 160 | ## Output("DisposeWindow(%s);", itselfname) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 161 | # From here on it's basically all boiler plate... |
| 162 | |
| 163 | # Create the generator groups and link them |
| 164 | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
| 165 | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
| 166 | module.addobject(object) |
| 167 | |
| 168 | # Create the generator classes used to populate the lists |
| 169 | Function = OSErrFunctionGenerator |
| 170 | Method = OSErrMethodGenerator |
| 171 | |
| 172 | # Create and populate the lists |
| 173 | functions = [] |
| 174 | methods = [] |
| 175 | execfile(INPUTFILE) |
| 176 | |
Jack Jansen | 8f0fab7 | 1997-08-15 14:38:05 +0000 | [diff] [blame] | 177 | # Add manual routines for converting integer WindowPtr's (as returned by |
| 178 | # various event routines) and Dialog objects to a WindowObject. |
Jack Jansen | caf7505 | 1995-08-17 14:28:27 +0000 | [diff] [blame] | 179 | whichwin_body = """ |
| 180 | long ptr; |
| 181 | |
| 182 | if ( !PyArg_ParseTuple(_args, "i", &ptr) ) |
| 183 | return NULL; |
| 184 | return WinObj_WhichWindow((WindowPtr)ptr); |
| 185 | """ |
| 186 | |
| 187 | f = ManualGenerator("WhichWindow", whichwin_body) |
| 188 | f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object" |
| 189 | |
| 190 | functions.append(f) |
| 191 | |
Jack Jansen | 330f576 | 1995-11-14 10:48:54 +0000 | [diff] [blame] | 192 | # And add the routines that access the internal bits of a window struct. They |
| 193 | # are currently #defined in Windows.h, they will be real routines in Copland |
| 194 | # (at which time this execfile can go) |
| 195 | execfile(EDITFILE) |
| 196 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 197 | # add the populated lists to the generator groups |
| 198 | # (in a different wordl the scan program would generate this) |
| 199 | for f in functions: module.add(f) |
| 200 | for f in methods: object.add(f) |
| 201 | |
Jack Jansen | caf7505 | 1995-08-17 14:28:27 +0000 | [diff] [blame] | 202 | |
| 203 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 204 | # generate output (open the output file as late as possible) |
| 205 | SetOutputFileName(OUTPUTFILE) |
| 206 | module.generate() |