py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Mac / Modules / list / listsupport.py
blobd5ddd5b770b57f90442a89759db1043f472f5668
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 = 'Lists.h' # The Apple header file
10 MODNAME = 'List' # The name of the module
11 OBJECTNAME = 'List' # The basic name of the objects used here
12 KIND = 'Handle' # Usually 'Ptr' or 'Handle'
14 # The following is *usually* unchanged but may still require tuning
15 MODPREFIX = MODNAME # The prefix for module-wide routines
16 OBJECTTYPE = "ListHandle" # The C type used to represent them
17 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
18 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
19 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
21 from macsupport import *
23 # Create the type objects
24 ListHandle = OpaqueByValueType("ListHandle", "ListObj")
25 ListRef = ListHandle # Obsolete, but used in Lists.h
26 Cell = Point
27 ListBounds = Rect
28 ListBounds_ptr = Rect_ptr
29 VarOutBufferShortsize = VarHeapOutputBufferType('char', 'short', 's') # (buf, &len)
30 InBufferShortsize = VarInputBufferType('char', 'short', 's') # (buf, len)
32 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
33 DataHandle = OpaqueByValueType("DataHandle", "ResObj")
34 Handle = OpaqueByValueType("Handle", "ResObj")
35 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
37 includestuff = includestuff + """
38 #ifdef WITHOUT_FRAMEWORKS
39 #include <Lists.h>
40 #else
41 #include <Carbon/Carbon.h>
42 #endif
44 #ifdef USE_TOOLBOX_OBJECT_GLUE
45 extern PyObject *_ListObj_New(ListHandle);
46 extern int _ListObj_Convert(PyObject *, ListHandle *);
48 #define ListObj_New _ListObj_New
49 #define ListObj_Convert _ListObj_Convert
50 #endif
52 #if !ACCESSOR_CALLS_ARE_FUNCTIONS
53 #define GetListPort(list) ((CGrafPtr)(*(list))->port)
54 #define GetListVerticalScrollBar(list) ((*(list))->vScroll)
55 #define GetListHorizontalScrollBar(list) ((*(list))->hScroll)
56 #define GetListActive(list) ((*(list))->lActive)
57 #define GetListClickTime(list) ((*(list))->clikTime)
58 #define GetListRefCon(list) ((*(list))->refCon)
59 #define GetListDefinition(list) ((*(list))->listDefProc) /* XXX Is this indeed the same? */
60 #define GetListUserHandle(list) ((*(list))->userHandle)
61 #define GetListDataHandle(list) ((*(list))->cells)
62 #define GetListFlags(list) ((*(list))->listFlags)
63 #define GetListSelectionFlags(list) ((*(list))->selFlags)
64 #define SetListViewBounds(list, bounds) (((*(list))->rView) = *(bounds))
66 #define SetListPort(list, port) (((*(list))->port) = (GrafPtr)(port))
67 #define SetListCellIndent(list, ind) (((*(list))->indent) = *(ind))
68 #define SetListClickTime(list, time) (((*(list))->clikTime) = (time))
69 #define SetListLastClick(list, click) (((*(list)->lastClick) = *(click))
70 #define SetListRefCon(list, refcon) (((*(list))->refCon) = (refcon))
71 #define SetListUserHandle(list, handle) (((*(list))->userHandle) = (handle))
72 #define SetListFlags(list, flags) (((*(list))->listFlags) = (flags))
73 #define SetListSelectionFlags(list, flags) (((*(list))->selFlags) = (flags))
75 #endif
77 #define as_List(x) ((ListHandle)x)
78 #define as_Resource(lh) ((Handle)lh)
79 """
81 initstuff = initstuff + """
82 PyMac_INIT_TOOLBOX_OBJECT_NEW(ListHandle, ListObj_New);
83 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ListHandle, ListObj_Convert);
84 """
86 class ListMethodGenerator(MethodGenerator):
87 """Similar to MethodGenerator, but has self as last argument"""
89 def parseArgumentList(self, args):
90 args, a0 = args[:-1], args[-1]
91 t0, n0, m0 = a0
92 if m0 != InMode:
93 raise ValueError, "method's 'self' must be 'InMode'"
94 self.itself = Variable(t0, "_self->ob_itself", SelfMode)
95 FunctionGenerator.parseArgumentList(self, args)
96 self.argumentList.append(self.itself)
98 getattrHookCode = """{
99 /* XXXX Should we HLock() here?? */
100 if ( strcmp(name, "listFlags") == 0 )
101 return Py_BuildValue("l", (long)(*self->ob_itself)->listFlags & 0xff);
102 if ( strcmp(name, "selFlags") == 0 )
103 return Py_BuildValue("l", (long)(*self->ob_itself)->selFlags & 0xff);
104 }"""
106 setattrCode = """
107 static int
108 ListObj_setattr(ListObject *self, char *name, PyObject *value)
110 long intval;
112 if ( value == NULL || !PyInt_Check(value) )
113 return -1;
114 intval = PyInt_AsLong(value);
115 if (strcmp(name, "listFlags") == 0 ) {
116 /* XXXX Should we HLock the handle here?? */
117 (*self->ob_itself)->listFlags = intval;
118 return 0;
120 if (strcmp(name, "selFlags") == 0 ) {
121 (*self->ob_itself)->selFlags = intval;
122 return 0;
124 return -1;
129 class MyObjectDefinition(GlobalObjectDefinition):
131 def outputStructMembers(self):
132 ObjectDefinition.outputStructMembers(self)
133 Output("int ob_must_be_disposed;")
135 def outputCheckNewArg(self):
136 Output("""if (itself == NULL) {
137 PyErr_SetString(List_Error,"Cannot create null List");
138 return NULL;
139 }""")
141 def outputInitStructMembers(self):
142 ObjectDefinition.outputInitStructMembers(self)
143 Output("it->ob_must_be_disposed = 1;")
145 def outputFreeIt(self, itselfname):
146 Output("if (self->ob_must_be_disposed && %s) LDispose(%s);", itselfname, itselfname)
148 def outputGetattrHook(self):
149 Output(getattrHookCode)
151 def outputSetattr(self):
152 Output(setattrCode)
154 # From here on it's basically all boiler plate...
156 # Create the generator groups and link them
157 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
158 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
159 module.addobject(object)
161 # Create the generator classes used to populate the lists
162 Function = FunctionGenerator
163 Method = ListMethodGenerator
165 # Create and populate the lists
166 functions = []
167 methods = []
168 execfile(INPUTFILE)
170 # Function to convert any handle to a list and vv.
171 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
172 as_List_body = """
173 Handle h;
174 ListObject *l;
175 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
176 return NULL;
177 l = (ListObject *)ListObj_New(as_List(h));
178 l->ob_must_be_disposed = 0;
179 return Py_BuildValue("O", l);
181 f = ManualGenerator("as_List", as_List_body)
182 f.docstring = lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
183 functions.append(f)
185 f = Method(Handle, 'as_Resource', (ListHandle, 'lh', InMode))
186 methods.append(f)
188 # add the populated lists to the generator groups
189 # (in a different wordl the scan program would generate this)
190 for f in functions: module.add(f)
191 for f in methods: object.add(f)
193 # generate output (open the output file as late as possible)
194 SetOutputFileName(OUTPUTFILE)
195 module.generate()