| 1 | n/a | # This script generates a Python interface for an Apple Macintosh Manager. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # The function specifications are generated by scanning the mamager's header file, |
|---|
| 4 | n/a | # using the "scantools" package (customized for this particular manager). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import string |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Declarations that change for each manager |
|---|
| 9 | n/a | MACHEADERFILE = 'Windows.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_Win' # The name of the module |
|---|
| 11 | n/a | OBJECTNAME = 'Window' # The basic name of the objects used here |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 14 | n/a | MODPREFIX = 'Win' # The prefix for module-wide routines |
|---|
| 15 | n/a | OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them |
|---|
| 16 | n/a | OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
|---|
| 17 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 18 | n/a | EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions |
|---|
| 19 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | from macsupport import * |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | # Create the type objects |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
|---|
| 26 | n/a | WindowRef = WindowPtr |
|---|
| 27 | n/a | WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX) |
|---|
| 28 | n/a | WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name |
|---|
| 29 | n/a | CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
|---|
| 30 | n/a | GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | DragReference = OpaqueByValueType("DragReference", "DragObj") |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|---|
| 35 | n/a | PicHandle = OpaqueByValueType("PicHandle", "ResObj") |
|---|
| 36 | n/a | WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj") |
|---|
| 37 | n/a | AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj") |
|---|
| 38 | n/a | PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj") |
|---|
| 39 | n/a | AliasHandle = OpaqueByValueType("AliasHandle", "ResObj") |
|---|
| 40 | n/a | IconRef = OpaqueByValueType("IconRef", "ResObj") |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | WindowRegionCode = Type("WindowRegionCode", "H") |
|---|
| 43 | n/a | WindowClass = Type("WindowClass", "l") |
|---|
| 44 | n/a | WindowAttributes = Type("WindowAttributes", "l") |
|---|
| 45 | n/a | WindowPositionMethod = Type("WindowPositionMethod", "l") |
|---|
| 46 | n/a | WindowTransitionEffect = Type("WindowTransitionEffect", "l") |
|---|
| 47 | n/a | WindowTransitionAction = Type("WindowTransitionAction", "l") |
|---|
| 48 | n/a | RGBColor = OpaqueType("RGBColor", "QdRGB") |
|---|
| 49 | n/a | RGBColor_ptr = RGBColor |
|---|
| 50 | n/a | ScrollWindowOptions = Type("ScrollWindowOptions", "l") |
|---|
| 51 | n/a | WindowPartCode = Type("WindowPartCode", "h") |
|---|
| 52 | n/a | WindowDefPartCode = Type("WindowDefPartCode", "h") |
|---|
| 53 | n/a | WindowModality = Type("WindowModality", "l") |
|---|
| 54 | n/a | GDHandle = OpaqueByValueType("GDHandle", "ResObj") |
|---|
| 55 | n/a | WindowConstrainOptions = Type("WindowConstrainOptions", "l") |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | PropertyCreator = OSTypeType("PropertyCreator") |
|---|
| 58 | n/a | PropertyTag = OSTypeType("PropertyTag") |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | includestuff = includestuff + """ |
|---|
| 61 | n/a | #include <Carbon/Carbon.h> |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 64 | n/a | extern PyObject *_WinObj_New(WindowRef); |
|---|
| 65 | n/a | extern PyObject *_WinObj_WhichWindow(WindowRef); |
|---|
| 66 | n/a | extern int _WinObj_Convert(PyObject *, WindowRef *); |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | #define WinObj_New _WinObj_New |
|---|
| 69 | n/a | #define WinObj_WhichWindow _WinObj_WhichWindow |
|---|
| 70 | n/a | #define WinObj_Convert _WinObj_Convert |
|---|
| 71 | n/a | #endif |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | /* Classic calls that we emulate in carbon mode */ |
|---|
| 74 | n/a | #define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn)) |
|---|
| 75 | n/a | #define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn)) |
|---|
| 76 | n/a | #define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn)) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | /* Function to dispose a window, with a "normal" calling sequence */ |
|---|
| 79 | n/a | static void |
|---|
| 80 | n/a | PyMac_AutoDisposeWindow(WindowPtr w) |
|---|
| 81 | n/a | { |
|---|
| 82 | n/a | DisposeWindow(w); |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | """ |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | finalstuff = finalstuff + """ |
|---|
| 87 | n/a | /* Return the object corresponding to the window, or NULL */ |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | PyObject * |
|---|
| 90 | n/a | WinObj_WhichWindow(WindowPtr w) |
|---|
| 91 | n/a | { |
|---|
| 92 | n/a | PyObject *it; |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | if (w == NULL) { |
|---|
| 95 | n/a | it = Py_None; |
|---|
| 96 | n/a | Py_INCREF(it); |
|---|
| 97 | n/a | } else { |
|---|
| 98 | n/a | it = (PyObject *) GetWRefCon(w); |
|---|
| 99 | n/a | if (it == NULL || !IsPointerValid((Ptr)it) || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) { |
|---|
| 100 | n/a | it = WinObj_New(w); |
|---|
| 101 | n/a | ((WindowObject *)it)->ob_freeit = NULL; |
|---|
| 102 | n/a | } else { |
|---|
| 103 | n/a | Py_INCREF(it); |
|---|
| 104 | n/a | } |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | return it; |
|---|
| 107 | n/a | } |
|---|
| 108 | n/a | """ |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | initstuff = initstuff + """ |
|---|
| 111 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_New); |
|---|
| 112 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_WhichWindow); |
|---|
| 113 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(WindowPtr, WinObj_Convert); |
|---|
| 114 | n/a | """ |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 117 | n/a | def outputCheckNewArg(self): |
|---|
| 118 | n/a | Output("if (itself == NULL) return PyMac_Error(resNotFound);") |
|---|
| 119 | n/a | Output("/* XXXX Or should we use WhichWindow code here? */") |
|---|
| 120 | n/a | def outputStructMembers(self): |
|---|
| 121 | n/a | GlobalObjectDefinition.outputStructMembers(self) |
|---|
| 122 | n/a | Output("void (*ob_freeit)(%s ptr);", self.itselftype) |
|---|
| 123 | n/a | def outputInitStructMembers(self): |
|---|
| 124 | n/a | GlobalObjectDefinition.outputInitStructMembers(self) |
|---|
| 125 | n/a | Output("it->ob_freeit = NULL;") |
|---|
| 126 | n/a | Output("if (GetWRefCon(itself) == 0)") |
|---|
| 127 | n/a | OutLbrace() |
|---|
| 128 | n/a | Output("SetWRefCon(itself, (long)it);") |
|---|
| 129 | n/a | Output("it->ob_freeit = PyMac_AutoDisposeWindow;") |
|---|
| 130 | n/a | OutRbrace() |
|---|
| 131 | n/a | def outputCheckConvertArg(self): |
|---|
| 132 | n/a | Out(""" |
|---|
| 133 | n/a | if (v == Py_None) { *p_itself = NULL; return 1; } |
|---|
| 134 | n/a | if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } |
|---|
| 135 | n/a | """) |
|---|
| 136 | n/a | OutLbrace() |
|---|
| 137 | n/a | Output("DialogRef dlg;") |
|---|
| 138 | n/a | OutLbrace("if (DlgObj_Convert(v, &dlg) && dlg)") |
|---|
| 139 | n/a | Output("*p_itself = GetDialogWindow(dlg);") |
|---|
| 140 | n/a | Output("return 1;") |
|---|
| 141 | n/a | OutRbrace() |
|---|
| 142 | n/a | Output("PyErr_Clear();") |
|---|
| 143 | n/a | OutRbrace() |
|---|
| 144 | n/a | def outputCleanupStructMembers(self): |
|---|
| 145 | n/a | Output("if (self->ob_freeit && self->ob_itself)") |
|---|
| 146 | n/a | OutLbrace() |
|---|
| 147 | n/a | Output("SetWRefCon(self->ob_itself, 0);") |
|---|
| 148 | n/a | Output("self->ob_freeit(self->ob_itself);") |
|---|
| 149 | n/a | OutRbrace() |
|---|
| 150 | n/a | Output("self->ob_itself = NULL;") |
|---|
| 151 | n/a | Output("self->ob_freeit = NULL;") |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def outputCompare(self): |
|---|
| 154 | n/a | Output() |
|---|
| 155 | n/a | Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype) |
|---|
| 156 | n/a | OutLbrace() |
|---|
| 157 | n/a | Output("if ( self->ob_itself > other->ob_itself ) return 1;") |
|---|
| 158 | n/a | Output("if ( self->ob_itself < other->ob_itself ) return -1;") |
|---|
| 159 | n/a | Output("return 0;") |
|---|
| 160 | n/a | OutRbrace() |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | def outputHash(self): |
|---|
| 163 | n/a | Output() |
|---|
| 164 | n/a | Output("static int %s_hash(%s *self)", self.prefix, self.objecttype) |
|---|
| 165 | n/a | OutLbrace() |
|---|
| 166 | n/a | Output("return (int)self->ob_itself;") |
|---|
| 167 | n/a | OutRbrace() |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | def outputRepr(self): |
|---|
| 170 | n/a | Output() |
|---|
| 171 | n/a | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype) |
|---|
| 172 | n/a | OutLbrace() |
|---|
| 173 | n/a | Output("char buf[100];") |
|---|
| 174 | n/a | Output("""sprintf(buf, "<Window object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""") |
|---|
| 175 | n/a | Output("return PyString_FromString(buf);") |
|---|
| 176 | n/a | OutRbrace() |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | ## def outputFreeIt(self, itselfname): |
|---|
| 179 | n/a | ## Output("DisposeWindow(%s);", itselfname) |
|---|
| 180 | n/a | # From here on it's basically all boiler plate... |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | # Create the generator groups and link them |
|---|
| 183 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|---|
| 184 | n/a | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|---|
| 185 | n/a | module.addobject(object) |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | # Create the generator classes used to populate the lists |
|---|
| 188 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 189 | n/a | Method = OSErrWeakLinkMethodGenerator |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | # Create and populate the lists |
|---|
| 192 | n/a | functions = [] |
|---|
| 193 | n/a | methods = [] |
|---|
| 194 | n/a | execfile(INPUTFILE) |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | # Add manual routines for converting integer WindowPtr's (as returned by |
|---|
| 197 | n/a | # various event routines) and Dialog objects to a WindowObject. |
|---|
| 198 | n/a | whichwin_body = """ |
|---|
| 199 | n/a | long ptr; |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | if ( !PyArg_ParseTuple(_args, "i", &ptr) ) |
|---|
| 202 | n/a | return NULL; |
|---|
| 203 | n/a | _res = WinObj_WhichWindow((WindowPtr)ptr); |
|---|
| 204 | n/a | return _res; |
|---|
| 205 | n/a | """ |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | f = ManualGenerator("WhichWindow", whichwin_body) |
|---|
| 208 | n/a | f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object" |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | functions.append(f) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | # And add the routines that access the internal bits of a window struct. They |
|---|
| 213 | n/a | # are currently #defined in Windows.h, they will be real routines in Copland |
|---|
| 214 | n/a | # (at which time this execfile can go) |
|---|
| 215 | n/a | execfile(EDITFILE) |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | # add the populated lists to the generator groups |
|---|
| 218 | n/a | # (in a different wordl the scan program would generate this) |
|---|
| 219 | n/a | for f in functions: module.add(f) |
|---|
| 220 | n/a | for f in methods: object.add(f) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | # generate output (open the output file as late as possible) |
|---|
| 225 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 226 | n/a | module.generate() |
|---|