winsupport.py 8.21 KB
Newer Older
1 2 3 4 5 6 7 8
# This script generates a Python interface for an Apple Macintosh Manager.
# It uses the "bgen" package to generate C code.
# The function specifications are generated by scanning the mamager's header file,
# using the "scantools" package (customized for this particular manager).

import string

# Declarations that change for each manager
9 10 11
MACHEADERFILE = 'Windows.h'             # The Apple header file
MODNAME = '_Win'                                # The name of the module
OBJECTNAME = 'Window'                   # The basic name of the objects used here
12 13

# The following is *usually* unchanged but may still require tuning
14 15 16
MODPREFIX = 'Win'                       # The prefix for module-wide routines
OBJECTTYPE = OBJECTNAME + 'Ptr'         # The C type used to represent them
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
20 21 22 23 24 25

from macsupport import *

# Create the type objects

WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
26
WindowRef = WindowPtr
27 28
WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name
29 30
CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
31

32 33
DragReference = OpaqueByValueType("DragReference", "DragObj")

34
RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
35
PicHandle = OpaqueByValueType("PicHandle", "ResObj")
36 37 38
WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj")
AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj")
PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
39 40
AliasHandle = OpaqueByValueType("AliasHandle", "ResObj")
IconRef = OpaqueByValueType("IconRef", "ResObj")
41

42
WindowRegionCode = Type("WindowRegionCode", "H")
43 44 45 46 47 48
WindowClass = Type("WindowClass", "l")
WindowAttributes = Type("WindowAttributes", "l")
WindowPositionMethod = Type("WindowPositionMethod", "l")
WindowTransitionEffect = Type("WindowTransitionEffect", "l")
WindowTransitionAction = Type("WindowTransitionAction", "l")
RGBColor = OpaqueType("RGBColor", "QdRGB")
49 50 51
RGBColor_ptr = RGBColor
ScrollWindowOptions = Type("ScrollWindowOptions", "l")
WindowPartCode = Type("WindowPartCode", "h")
52 53 54 55
WindowDefPartCode = Type("WindowDefPartCode", "h")
WindowModality = Type("WindowModality", "l")
GDHandle = OpaqueByValueType("GDHandle", "ResObj")
WindowConstrainOptions = Type("WindowConstrainOptions", "l")
56

57 58
PropertyCreator = OSTypeType("PropertyCreator")
PropertyTag = OSTypeType("PropertyTag")
59

60
includestuff = includestuff + """
61
#include <Carbon/Carbon.h>
62

63 64 65 66 67 68 69 70 71 72
#ifdef USE_TOOLBOX_OBJECT_GLUE
extern PyObject *_WinObj_New(WindowRef);
extern PyObject *_WinObj_WhichWindow(WindowRef);
extern int _WinObj_Convert(PyObject *, WindowRef *);

#define WinObj_New _WinObj_New
#define WinObj_WhichWindow _WinObj_WhichWindow
#define WinObj_Convert _WinObj_Convert
#endif

73 74 75 76
/* Classic calls that we emulate in carbon mode */
#define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn))
#define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn))
#define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn))
77

78 79 80 81
/* Function to dispose a window, with a "normal" calling sequence */
static void
PyMac_AutoDisposeWindow(WindowPtr w)
{
82
        DisposeWindow(w);
83
}
84 85 86 87 88 89
"""

finalstuff = finalstuff + """
/* Return the object corresponding to the window, or NULL */

PyObject *
90
WinObj_WhichWindow(WindowPtr w)
91
{
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        PyObject *it;

        if (w == NULL) {
                it = Py_None;
                Py_INCREF(it);
        } else {
                it = (PyObject *) GetWRefCon(w);
                if (it == NULL || !IsPointerValid((Ptr)it) || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) {
                        it = WinObj_New(w);
                        ((WindowObject *)it)->ob_freeit = NULL;
                } else {
                        Py_INCREF(it);
                }
        }
        return it;
107 108 109
}
"""

110
initstuff = initstuff + """
111 112 113
        PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_New);
        PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_WhichWindow);
        PyMac_INIT_TOOLBOX_OBJECT_CONVERT(WindowPtr, WinObj_Convert);
114 115
"""

116
class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    def outputCheckNewArg(self):
        Output("if (itself == NULL) return PyMac_Error(resNotFound);")
        Output("/* XXXX Or should we use WhichWindow code here? */")
    def outputStructMembers(self):
        GlobalObjectDefinition.outputStructMembers(self)
        Output("void (*ob_freeit)(%s ptr);", self.itselftype)
    def outputInitStructMembers(self):
        GlobalObjectDefinition.outputInitStructMembers(self)
        Output("it->ob_freeit = NULL;")
        Output("if (GetWRefCon(itself) == 0)")
        OutLbrace()
        Output("SetWRefCon(itself, (long)it);")
        Output("it->ob_freeit = PyMac_AutoDisposeWindow;")
        OutRbrace()
    def outputCheckConvertArg(self):
        Out("""
        if (v == Py_None) { *p_itself = NULL; return 1; }
        if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
        """)
        OutLbrace()
        Output("DialogRef dlg;")
        OutLbrace("if (DlgObj_Convert(v, &dlg) && dlg)")
        Output("*p_itself = GetDialogWindow(dlg);")
        Output("return 1;")
        OutRbrace()
        Output("PyErr_Clear();")
        OutRbrace()
    def outputCleanupStructMembers(self):
        Output("if (self->ob_freeit && self->ob_itself)")
        OutLbrace()
        Output("SetWRefCon(self->ob_itself, 0);")
        Output("self->ob_freeit(self->ob_itself);")
        OutRbrace()
        Output("self->ob_itself = NULL;")
        Output("self->ob_freeit = NULL;")

    def outputCompare(self):
        Output()
        Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
        OutLbrace()
        Output("if ( self->ob_itself > other->ob_itself ) return 1;")
        Output("if ( self->ob_itself < other->ob_itself ) return -1;")
        Output("return 0;")
        OutRbrace()

    def outputHash(self):
        Output()
        Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
        OutLbrace()
        Output("return (int)self->ob_itself;")
        OutRbrace()

    def outputRepr(self):
        Output()
        Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
        OutLbrace()
        Output("char buf[100];")
        Output("""sprintf(buf, "<Window object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
        Output("return PyString_FromString(buf);")
        OutRbrace()

##      def outputFreeIt(self, itselfname):
##              Output("DisposeWindow(%s);", itselfname)
180 181 182 183 184 185 186 187
# From here on it's basically all boiler plate...

# Create the generator groups and link them
module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
module.addobject(object)

# Create the generator classes used to populate the lists
188 189
Function = OSErrWeakLinkFunctionGenerator
Method = OSErrWeakLinkMethodGenerator
190 191 192 193 194 195

# Create and populate the lists
functions = []
methods = []
execfile(INPUTFILE)

196 197
# Add manual routines for converting integer WindowPtr's (as returned by
# various event routines)  and Dialog objects to a WindowObject.
198 199 200 201
whichwin_body = """
long ptr;

if ( !PyArg_ParseTuple(_args, "i", &ptr) )
202
        return NULL;
203 204
_res = WinObj_WhichWindow((WindowPtr)ptr);
return _res;
205 206 207 208 209 210 211
"""

f = ManualGenerator("WhichWindow", whichwin_body)
f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"

functions.append(f)

212 213 214 215 216
# And add the routines that access the internal bits of a window struct. They
# are currently #defined in Windows.h, they will be real routines in Copland
# (at which time this execfile can go)
execfile(EDITFILE)

217 218 219 220 221
# add the populated lists to the generator groups
# (in a different wordl the scan program would generate this)
for f in functions: module.add(f)
for f in methods: object.add(f)

222 223


224 225 226
# generate output (open the output file as late as possible)
SetOutputFileName(OUTPUTFILE)
module.generate()