1# Scan an Apple header file, generating a Python file of generator calls.
2
3import sys
4from bgenlocations import TOOLBOXDIR, BGENDIR
5sys.path.append(BGENDIR)
6
7from scantools import Scanner
8
9def main():
10    input = "QuickDraw.h"
11    output = "qdgen.py"
12    defsoutput = TOOLBOXDIR + "QuickDraw.py"
13    scanner = MyScanner(input, output, defsoutput)
14    scanner.scan()
15    scanner.close()
16
17    # Grmpf. Universal Headers have Text-stuff in a different include file...
18    input = "QuickDrawText.h"
19    output = "@qdgentext.py"
20    defsoutput = "@QuickDrawText.py"
21    have_extra = 0
22    try:
23        scanner = MyScanner(input, output, defsoutput)
24        scanner.scan()
25        scanner.close()
26        have_extra = 1
27    except IOError:
28        pass
29    if have_extra:
30        print "=== Copying QuickDrawText stuff into main files... ==="
31        ifp = open("@qdgentext.py")
32        ofp = open("qdgen.py", "a")
33        ofp.write(ifp.read())
34        ifp.close()
35        ofp.close()
36        ifp = open("@QuickDrawText.py")
37        ofp = open(TOOLBOXDIR + "QuickDraw.py", "a")
38        ofp.write(ifp.read())
39        ifp.close()
40        ofp.close()
41
42    print "=== Testing definitions output code ==="
43    execfile(defsoutput, {}, {})
44    print "=== Done scanning and generating, now importing the generated code... ==="
45    import qdsupport
46    print "=== Done.  It's up to you to compile it now! ==="
47
48class MyScanner(Scanner):
49
50    def destination(self, type, name, arglist):
51        classname = "Function"
52        listname = "functions"
53        if arglist:
54            t, n, m = arglist[0]
55            if t in ('GrafPtr', 'CGrafPtr') and m == 'InMode':
56                classname = "Method"
57                listname = "gr_methods"
58            elif t == 'BitMapPtr' and m == 'InMode':
59                classname = "Method"
60                listname = "bm_methods"
61##                      elif t == "PolyHandle" and m == "InMode":
62##                              classname = "Method"
63##                              listname = "p_methods"
64##                      elif t == "RgnHandle" and m == "InMode":
65##                              classname = "Method"
66##                              listname = "r_methods"
67        return classname, listname
68
69
70    def writeinitialdefs(self):
71        self.defsfile.write("""
72def FOUR_CHAR_CODE(x): return x
73normal                                          = 0
74bold                                            = 1
75italic                                          = 2
76underline                                       = 4
77outline                                         = 8
78shadow                                          = 0x10
79condense                                        = 0x20
80extend                                          = 0x40
81""")
82
83    def makeblacklistnames(self):
84        return [
85                'InitGraf',
86                'StuffHex',
87                'StdLine',
88                'StdComment',
89                'StdGetPic',
90                'OpenPort',
91                'InitPort',
92                'ClosePort',
93                'OpenCPort',
94                'InitCPort',
95                'CloseCPort',
96                'BitMapToRegionGlue',
97                'StdOpcode',    # XXXX Missing from library...
98                # The following are for non-macos use:
99                'LockPortBits',
100                'UnlockPortBits',
101                'UpdatePort',
102                'GetPortNativeWindow',
103                'GetNativeWindowPort',
104                'NativeRegionToMacRegion',
105                'MacRegionToNativeRegion',
106                'GetPortHWND',
107                'GetHWNDPort',
108                'GetPICTFromDIB',
109
110                'HandleToRgn', # Funny signature
111
112                # Need Cm, which we don't want to drag in just yet
113                'OpenCursorComponent',
114                'CloseCursorComponent',
115                'SetCursorComponent',
116                'CursorComponentChanged',
117                'CursorComponentSetData',
118                ]
119
120    def makeblacklisttypes(self):
121        return [
122                "QDRegionBitsRef", # Should do this, but too lazy now.
123                'CIconHandle', # Obsolete
124                'CQDProcs',
125                'CQDProcsPtr',
126                'CSpecArray',
127                'ColorComplementProcPtr',
128                'ColorComplementUPP',
129                'ColorSearchProcPtr',
130                'ColorSearchUPP',
131                'ConstPatternParam',
132                'DeviceLoopDrawingProcPtr',
133                'DeviceLoopFlags',
134                'GrafVerb',
135                'OpenCPicParams_ptr',
136                'Ptr',
137                'QDProcs',
138                'ReqListRec',
139                'void_ptr',
140                'CustomXFerProcPtr',
141                ]
142
143    def makerepairinstructions(self):
144        return [
145                ([('void_ptr', 'textBuf', 'InMode'),
146                  ('short', 'firstByte', 'InMode'),
147                  ('short', 'byteCount', 'InMode')],
148                 [('TextThingie', '*', '*'), ('*', '*', '*'), ('*', '*', '*')]),
149
150                # GetPen and SetPt use a point-pointer as output-only:
151                ('GetPen', [('Point', '*', 'OutMode')], [('*', '*', 'OutMode')]),
152                ('SetPt', [('Point', '*', 'OutMode')], [('*', '*', 'OutMode')]),
153
154                # All others use it as input/output:
155                ([('Point', '*', 'OutMode')],
156                 [('*', '*', 'InOutMode')]),
157
158                 # InsetRect, OffsetRect
159                 ([('Rect', 'r', 'OutMode'),
160                        ('short', 'dh', 'InMode'),
161                        ('short', 'dv', 'InMode')],
162                  [('Rect', 'r', 'InOutMode'),
163                        ('short', 'dh', 'InMode'),
164                        ('short', 'dv', 'InMode')]),
165
166                 # MapRect
167                 ([('Rect', 'r', 'OutMode'),
168                        ('Rect_ptr', 'srcRect', 'InMode'),
169                        ('Rect_ptr', 'dstRect', 'InMode')],
170                  [('Rect', 'r', 'InOutMode'),
171                        ('Rect_ptr', 'srcRect', 'InMode'),
172                        ('Rect_ptr', 'dstRect', 'InMode')]),
173
174                 # CopyBits and friends
175                 ([('RgnHandle', 'maskRgn', 'InMode')],
176                  [('OptRgnHandle', 'maskRgn', 'InMode')]),
177
178                 ('QDFlushPortBuffer',
179                  [('RgnHandle', '*', 'InMode')],
180                  [('OptRgnHandle', '*', 'InMode')]),
181
182                 # Accessors with reference argument also returned.
183                 ([('Rect_ptr', 'GetPortBounds', 'ReturnMode')],
184                  [('void', '*', 'ReturnMode')]),
185
186                 ([('RGBColor_ptr', 'GetPortForeColor', 'ReturnMode')],
187                  [('void', '*', 'ReturnMode')]),
188
189                 ([('RGBColor_ptr', 'GetPortBackColor', 'ReturnMode')],
190                  [('void', '*', 'ReturnMode')]),
191
192                 ([('RGBColor_ptr', 'GetPortOpColor', 'ReturnMode')],
193                  [('void', '*', 'ReturnMode')]),
194
195                 ([('RGBColor_ptr', 'GetPortHiliteColor', 'ReturnMode')],
196                  [('void', '*', 'ReturnMode')]),
197
198                 ([('Point_ptr', 'GetPortPenSize', 'ReturnMode')],
199                  [('void', '*', 'ReturnMode')]),
200
201                 ([('Point_ptr', 'GetPortPenLocation', 'ReturnMode')],
202                  [('void', '*', 'ReturnMode')]),
203
204                 ([('Rect_ptr', 'GetPixBounds', 'ReturnMode')],
205                  [('void', '*', 'ReturnMode')]),
206
207                 ([('BitMap_ptr', 'GetQDGlobalsScreenBits', 'ReturnMode')],
208                  [('void', '*', 'ReturnMode')]),
209
210                 ([('Cursor_ptr', 'GetQDGlobalsArrow', 'ReturnMode')],
211                  [('void', '*', 'ReturnMode')]),
212
213                 ([('Rect_ptr', 'GetRegionBounds', 'ReturnMode')],
214                  [('void', '*', 'ReturnMode')]),
215
216                 ([('Pattern_ptr', '*', 'ReturnMode')],
217                  [('void', '*', 'ReturnMode')]),
218
219                 ([('Point_ptr', 'QDLocalToGlobalPoint', 'ReturnMode')],
220                  [('void', '*', 'ReturnMode')]),
221
222                 ([('Rect_ptr', 'QDLocalToGlobalRect', 'ReturnMode')],
223                  [('void', '*', 'ReturnMode')]),
224
225                 ([('Point_ptr', 'QDGlobalToLocalPoint', 'ReturnMode')],
226                  [('void', '*', 'ReturnMode')]),
227
228                 ([('Rect_ptr', 'QDGlobalToLocalRect', 'ReturnMode')],
229                  [('void', '*', 'ReturnMode')]),
230
231                ]
232
233if __name__ == "__main__":
234    main()
235