This commit was manufactured by cvs2svn to create tag 'r222'.
[python/dscho.git] / Mac / Modules / te / tesupport.py
bloba77250c6569048dc951efda736fe9a4f6f38ec62
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 = 'TextEdit.h' # The Apple header file
10 MODNAME = '_TE' # The name of the module
11 OBJECTNAME = 'TE' # 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 = 'TE' # The prefix for module-wide routines
16 OBJECTTYPE = "TEHandle" # 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 TEHandle = OpaqueByValueType("TEHandle", "TEObj")
25 CharsHandle = OpaqueByValueType("CharsHandle", "ResObj")
26 Handle = OpaqueByValueType("Handle", "ResObj")
27 StScrpHandle = OpaqueByValueType("StScrpHandle", "ResObj")
28 TEStyleHandle = OpaqueByValueType("TEStyleHandle", "ResObj")
29 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
31 TextStyle = OpaqueType("TextStyle", "TextStyle")
32 TextStyle_ptr = TextStyle
34 includestuff = includestuff + """
35 #ifdef WITHOUT_FRAMEWORKS
36 #include <TextEdit.h>
37 #else
38 #include <Carbon/Carbon.h>
39 #endif
41 #ifdef USE_TOOLBOX_OBJECT_GLUE
42 extern PyObject *_TEObj_New(TEHandle);
43 extern int _TEObj_Convert(PyObject *, TEHandle *);
45 #define TEObj_New _TEObj_New
46 #define TEObj_Convert _TEObj_Convert
47 #endif
49 #define as_TE(h) ((TEHandle)h)
50 #define as_Resource(teh) ((Handle)teh)
53 ** Parse/generate TextStyle records
55 static PyObject *
56 TextStyle_New(TextStylePtr itself)
59 return Py_BuildValue("lllO&", (long)itself->tsFont, (long)itself->tsFace, (long)itself->tsSize, QdRGB_New,
60 &itself->tsColor);
63 static int
64 TextStyle_Convert(PyObject *v, TextStylePtr p_itself)
66 long font, face, size;
68 if( !PyArg_ParseTuple(v, "lllO&", &font, &face, &size, QdRGB_Convert, &p_itself->tsColor) )
69 return 0;
70 p_itself->tsFont = (short)font;
71 p_itself->tsFace = (Style)face;
72 p_itself->tsSize = (short)size;
73 return 1;
75 """
77 initstuff = initstuff + """
78 PyMac_INIT_TOOLBOX_OBJECT_NEW(TEHandle, TEObj_New);
79 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(TEHandle, TEObj_Convert);
80 """
82 class TEMethodGenerator(OSErrWeakLinkMethodGenerator):
83 """Similar to MethodGenerator, but has self as last argument"""
85 def parseArgumentList(self, args):
86 args, a0 = args[:-1], args[-1]
87 t0, n0, m0 = a0
88 if m0 != InMode:
89 raise ValueError, "method's 'self' must be 'InMode'"
90 self.itself = Variable(t0, "_self->ob_itself", SelfMode)
91 FunctionGenerator.parseArgumentList(self, args)
92 self.argumentList.append(self.itself)
96 class MyObjectDefinition(GlobalObjectDefinition):
97 def outputCheckNewArg(self):
98 Output("""if (itself == NULL) {
99 PyErr_SetString(TE_Error,"Cannot create null TE");
100 return NULL;
101 }""")
102 def outputFreeIt(self, itselfname):
103 Output("TEDispose(%s);", itselfname)
105 def outputGetattrHook(self):
106 Output("""
107 if( strcmp(name, "destRect") == 0 )
108 return Py_BuildValue("O&", PyMac_BuildRect,
109 &(*self->ob_itself)->destRect);
110 if( strcmp(name, "viewRect") == 0 )
111 return Py_BuildValue("O&", PyMac_BuildRect,
112 &(*self->ob_itself)->viewRect);
113 if( strcmp(name, "selRect") == 0 )
114 return Py_BuildValue("O&", PyMac_BuildRect,
115 &(*self->ob_itself)->selRect);
116 if( strcmp(name, "lineHeight") == 0 )
117 return Py_BuildValue("h", (*self->ob_itself)->lineHeight);
118 if( strcmp(name, "fontAscent") == 0 )
119 return Py_BuildValue("h", (*self->ob_itself)->fontAscent);
120 if( strcmp(name, "selPoint") == 0 )
121 return Py_BuildValue("O&", PyMac_BuildPoint,
122 (*self->ob_itself)->selPoint);
123 if( strcmp(name, "selStart") == 0 )
124 return Py_BuildValue("h", (*self->ob_itself)->selStart);
125 if( strcmp(name, "selEnd") == 0 )
126 return Py_BuildValue("h", (*self->ob_itself)->selEnd);
127 if( strcmp(name, "active") == 0 )
128 return Py_BuildValue("h", (*self->ob_itself)->active);
129 if( strcmp(name, "just") == 0 )
130 return Py_BuildValue("h", (*self->ob_itself)->just);
131 if( strcmp(name, "teLength") == 0 )
132 return Py_BuildValue("h", (*self->ob_itself)->teLength);
133 if( strcmp(name, "txFont") == 0 )
134 return Py_BuildValue("h", (*self->ob_itself)->txFont);
135 if( strcmp(name, "txFace") == 0 )
136 return Py_BuildValue("h", (*self->ob_itself)->txFace);
137 if( strcmp(name, "txMode") == 0 )
138 return Py_BuildValue("h", (*self->ob_itself)->txMode);
139 if( strcmp(name, "txSize") == 0 )
140 return Py_BuildValue("h", (*self->ob_itself)->txSize);
141 if( strcmp(name, "nLines") == 0 )
142 return Py_BuildValue("h", (*self->ob_itself)->nLines);
143 """)
145 # From here on it's basically all boiler plate...
147 # Create the generator groups and link them
148 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
149 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
150 module.addobject(object)
152 # Create the generator classes used to populate the lists
153 Function = OSErrWeakLinkFunctionGenerator
154 Method = TEMethodGenerator
156 # Create and populate the lists
157 functions = []
158 methods = []
159 execfile(INPUTFILE)
161 # Converter from/to handle
162 f = Function(TEHandle, 'as_TE', (Handle, 'h', InMode))
163 functions.append(f)
164 f = Method(Handle, 'as_Resource', (TEHandle, 'teh', InMode))
165 methods.append(f)
167 # add the populated lists to the generator groups
168 # (in a different wordl the scan program would generate this)
169 for f in functions: module.add(f)
170 for f in methods: object.add(f)
172 # generate output (open the output file as late as possible)
173 SetOutputFileName(OUTPUTFILE)
174 module.generate()