1# Scan an Apple header file, generating a Python file of generator calls.
2import sys
3from bgenlocations import TOOLBOXDIR, BGENDIR
4sys.path.append(BGENDIR)
5
6from scantools import Scanner
7
8def main():
9    input = "MacWindows.h"
10    output = "wingen.py"
11    defsoutput = TOOLBOXDIR + "Windows.py"
12    scanner = MyScanner(input, output, defsoutput)
13    scanner.scan()
14    scanner.close()
15    print "=== Testing definitions output code ==="
16    execfile(defsoutput, {}, {})
17    print "=== Done scanning and generating, now importing the generated code... ==="
18    import winsupport
19    print "=== Done.  It's up to you to compile it now! ==="
20
21class MyScanner(Scanner):
22
23    def destination(self, type, name, arglist):
24        classname = "Function"
25        listname = "functions"
26        if arglist:
27            t, n, m = arglist[0]
28            if t in ("WindowPtr", "WindowPeek", "WindowRef") and m == "InMode":
29                classname = "Method"
30                listname = "methods"
31        return classname, listname
32
33    def writeinitialdefs(self):
34        self.defsfile.write("def FOUR_CHAR_CODE(x): return x\n")
35        self.defsfile.write("false = 0\n")
36        self.defsfile.write("true = 1\n")
37        self.defsfile.write("kWindowNoConstrainAttribute = 0x80000000\n")
38
39    def makeblacklistnames(self):
40        return [
41                'DisposeWindow', # Implied when the object is deleted
42                'CloseWindow',
43                'SetWindowProperty',    # For the moment
44                'GetWindowProperty',
45                'GetWindowPropertySize',
46                'RemoveWindowProperty',
47                'MacCloseWindow',
48                'GetWindowList', # Don't know whether this is safe...
49                # Constants with funny definitions
50                'kMouseUpOutOfSlop',
51                'kAllWindowClasses',
52                'kWindowNoConstrainAttribute',
53                # OS8 only:
54                'GetAuxWin',
55                'GetWindowDataHandle',
56                'SaveOld',
57                'DrawNew',
58                'SetWinColor',
59                'SetDeskCPat',
60                'InitWindows',
61                'InitFloatingWindows',
62                'GetWMgrPort',
63                'GetCWMgrPort',
64                'ValidRgn',             # Use versions with Window in their name
65                'ValidRect',
66                'InvalRgn',
67                'InvalRect',
68                'IsValidWindowPtr', # I think this is useless for Python, but not sure...
69                'GetWindowZoomFlag',    # Not available in Carbon
70                'GetWindowTitleWidth',  # Ditto
71                'GetWindowGoAwayFlag',
72                'GetWindowSpareFlag',
73                ]
74
75    def makeblacklisttypes(self):
76        return [
77                'ProcPtr',
78                'DragGrayRgnUPP',
79                'WindowPaintUPP',
80                'Collection',           # For now, to be done later
81                'WindowDefSpec',        # Too difficult for now
82                'WindowDefSpec_ptr',
83                'EventRef', #TBD
84                ]
85
86    def makerepairinstructions(self):
87        return [
88
89                # GetWTitle
90                ([("Str255", "*", "InMode")],
91                 [("*", "*", "OutMode")]),
92
93                ([("void_ptr", "*", "InMode"), ("long", "*", "InMode")],
94                 [("InBuffer", "*", "*")]),
95
96                ([("void", "*", "OutMode"), ("long", "*", "InMode"),
97                                            ("long", "*", "OutMode")],
98                 [("VarVarOutBuffer", "*", "InOutMode")]),
99
100                ([("void", "wStorage", "OutMode")],
101                 [("NullStorage", "*", "InMode")]),
102
103                # match FindWindowOfClass
104                ([("WindowRef", "outWindow", "OutMode"), ("WindowPartCode", "outWindowPart", "OutMode")],
105                 [("ExistingWindowPtr", "*", "OutMode"), ("WindowPartCode", "outWindowPart", "OutMode")]),
106            # then match CreateNewWindow and CreateWindowFromResource
107                ([("WindowRef", "outWindow", "OutMode")],
108                 [("WindowRef", "*", "*")]),
109
110                ([("WindowPtr", "*", "OutMode")],
111                 [("ExistingWindowPtr", "*", "*")]),
112                ([("WindowRef", "*", "OutMode")],       # Same, but other style headerfiles
113                 [("ExistingWindowPtr", "*", "*")]),
114
115                ([("WindowPtr", "FrontWindow", "ReturnMode")],
116                 [("ExistingWindowPtr", "*", "*")]),
117                ([("WindowRef", "FrontWindow", "ReturnMode")],  # Ditto
118                 [("ExistingWindowPtr", "*", "*")]),
119                ([("WindowPtr", "FrontNonFloatingWindow", "ReturnMode")],
120                 [("ExistingWindowPtr", "*", "*")]),
121                ([("WindowRef", "FrontNonFloatingWindow", "ReturnMode")],       # Ditto
122                 [("ExistingWindowPtr", "*", "*")]),
123
124                ([("Rect_ptr", "*", "ReturnMode")], # GetWindowXXXState accessors
125                 [("void", "*", "ReturnMode")]),
126                ]
127
128if __name__ == "__main__":
129    main()
130