Another batch of updates...
[python/dscho.git] / Mac / Modules / dlg / dlgsupport.py
blobca4e0dfd3d02da18724c6dada64f64c37666bc14
1 # This script generates the Dialogs interface for Python.
2 # It uses the "bgen" package to generate C code.
3 # It execs the file dlggen.py which contain the function definitions
4 # (dlggen.py was generated by dlgscan.py, scanning the <Dialogs.h> header file).
6 import addpack
7 addpack.addpack(':Tools:bgen:bgen')
9 from macsupport import *
11 # Create the type objects
13 DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
14 DialogRef = DialogPtr
16 # An OptHandle is either a handle or None (in case NULL is passed in).
17 # This is needed for GetDialogItem().
18 OptHandle = OpaqueByValueType("Handle", "OptResObj")
20 ModalFilterProcPtr = InputOnlyType("PyObject*", "O")
21 ModalFilterProcPtr.passInput = lambda name: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name
22 ModalFilterUPP = ModalFilterProcPtr
24 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
26 DITLMethod = Type("DITLMethod", "h")
28 includestuff = includestuff + """
29 #include <Dialogs.h>
31 #ifndef HAVE_UNIVERSAL_HEADERS
32 #define NewModalFilterProc(x) (x)
33 #endif
35 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
37 /* XXX Shouldn't this be a stack? */
38 static PyObject *Dlg_FilterProc_callback = NULL;
40 static PyObject *DlgObj_New(DialogPtr); /* Forward */
42 static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
43 EventRecord *event,
44 short *itemHit)
46 Boolean rv;
47 PyObject *args, *res;
48 PyObject *callback = Dlg_FilterProc_callback;
49 if (callback == NULL)
50 return 0; /* Default behavior */
51 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
52 args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
53 if (args == NULL)
54 res = NULL;
55 else {
56 res = PyEval_CallObject(callback, args);
57 Py_DECREF(args);
59 if (res == NULL) {
60 fprintf(stderr, "Exception in Dialog Filter\\n");
61 PyErr_Print();
62 *itemHit = -1; /* Fake return item */
63 return 1; /* We handled it */
65 else {
66 Dlg_FilterProc_callback = callback;
67 if (PyInt_Check(res)) {
68 *itemHit = PyInt_AsLong(res);
69 rv = 1;
71 else
72 rv = PyObject_IsTrue(res);
74 Py_DECREF(res);
75 return rv;
78 static ModalFilterProcPtr
79 Dlg_PassFilterProc(PyObject *callback)
81 PyObject *tmp = Dlg_FilterProc_callback;
82 Dlg_FilterProc_callback = NULL;
83 if (callback == Py_None) {
84 Py_XDECREF(tmp);
85 return NULL;
87 Py_INCREF(callback);
88 Dlg_FilterProc_callback = callback;
89 Py_XDECREF(tmp);
90 return &Dlg_UnivFilterProc;
93 extern PyMethodChain WinObj_chain;
94 """
97 # Define a class which specializes our object definition
98 class MyObjectDefinition(GlobalObjectDefinition):
99 def __init__(self, name, prefix = None, itselftype = None):
100 GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
101 self.basechain = "&WinObj_chain"
102 def outputInitStructMembers(self):
103 GlobalObjectDefinition.outputInitStructMembers(self)
104 Output("SetWRefCon(itself, (long)it);")
105 def outputCheckNewArg(self):
106 Output("if (itself == NULL) return Py_None;")
107 def outputCheckConvertArg(self):
108 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
109 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
110 Output(" return 1; }")
111 def outputFreeIt(self, itselfname):
112 Output("DisposeDialog(%s);", itselfname)
114 # Create the generator groups and link them
115 module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff)
116 object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
117 module.addobject(object)
119 # Create the generator classes used to populate the lists
120 Function = OSErrFunctionGenerator
121 Method = OSErrMethodGenerator
123 # Create and populate the lists
124 functions = []
125 methods = []
126 execfile("dlggen.py")
128 # add the populated lists to the generator groups
129 for f in functions: module.add(f)
130 for f in methods: object.add(f)
132 # generate output
133 SetOutputFileName('Dlgmodule.c')
134 module.generate()