Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Mac / Modules / dlg / dlgsupport.py
blob526bfa441cdd22c502a45c06f3eb7699a0ab2e65
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 from macsupport import *
8 # Create the type objects
10 DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
11 DialogRef = DialogPtr
13 # An OptHandle is either a handle or None (in case NULL is passed in).
14 # This is needed for GetDialogItem().
15 OptHandle = OpaqueByValueType("Handle", "OptResObj")
17 ModalFilterProcPtr = InputOnlyType("PyObject*", "O")
18 ModalFilterProcPtr.passInput = lambda name: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name
19 ModalFilterUPP = ModalFilterProcPtr
21 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
23 DITLMethod = Type("DITLMethod", "h")
24 DialogItemIndex = Type("DialogItemIndex", "h")
25 DialogItemType = Type("DialogItemType", "h")
26 DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h")
27 AlertType = Type("AlertType", "h")
28 StringPtr = Str255
30 includestuff = includestuff + """
31 #include <Dialogs.h>
33 #ifndef HAVE_UNIVERSAL_HEADERS
34 #define NewModalFilterProc(x) (x)
35 #endif
37 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
39 /* XXX Shouldn't this be a stack? */
40 static PyObject *Dlg_FilterProc_callback = NULL;
42 static PyObject *DlgObj_New(DialogPtr); /* Forward */
44 static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
45 EventRecord *event,
46 short *itemHit)
48 Boolean rv;
49 PyObject *args, *res;
50 PyObject *callback = Dlg_FilterProc_callback;
51 if (callback == NULL)
52 return 0; /* Default behavior */
53 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
54 args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
55 if (args == NULL)
56 res = NULL;
57 else {
58 res = PyEval_CallObject(callback, args);
59 Py_DECREF(args);
61 if (res == NULL) {
62 PySys_WriteStderr("Exception in Dialog Filter\\n");
63 PyErr_Print();
64 *itemHit = -1; /* Fake return item */
65 return 1; /* We handled it */
67 else {
68 Dlg_FilterProc_callback = callback;
69 if (PyInt_Check(res)) {
70 *itemHit = PyInt_AsLong(res);
71 rv = 1;
73 else
74 rv = PyObject_IsTrue(res);
76 Py_DECREF(res);
77 return rv;
80 static ModalFilterProcPtr
81 Dlg_PassFilterProc(PyObject *callback)
83 PyObject *tmp = Dlg_FilterProc_callback;
84 Dlg_FilterProc_callback = NULL;
85 if (callback == Py_None) {
86 Py_XDECREF(tmp);
87 return NULL;
89 Py_INCREF(callback);
90 Dlg_FilterProc_callback = callback;
91 Py_XDECREF(tmp);
92 return &Dlg_UnivFilterProc;
95 static PyObject *Dlg_UserItemProc_callback = NULL;
97 static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
98 short item)
100 PyObject *args, *res;
102 if (Dlg_UserItemProc_callback == NULL)
103 return; /* Default behavior */
104 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
105 args = Py_BuildValue("O&h", WinObj_WhichWindow, dialog, item);
106 if (args == NULL)
107 res = NULL;
108 else {
109 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
110 Py_DECREF(args);
112 if (res == NULL) {
113 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
114 PyErr_Print();
116 Py_XDECREF(res);
117 return;
120 extern PyMethodChain WinObj_chain;
124 # Define a class which specializes our object definition
125 class MyObjectDefinition(GlobalObjectDefinition):
126 def __init__(self, name, prefix = None, itselftype = None):
127 GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
128 self.basechain = "&WinObj_chain"
129 def outputInitStructMembers(self):
130 GlobalObjectDefinition.outputInitStructMembers(self)
131 Output("SetWRefCon(itself, (long)it);")
132 def outputCheckNewArg(self):
133 Output("if (itself == NULL) return Py_None;")
134 def outputCheckConvertArg(self):
135 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
136 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
137 Output(" return 1; }")
138 def outputFreeIt(self, itselfname):
139 Output("DisposeDialog(%s);", itselfname)
141 # Create the generator groups and link them
142 module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff)
143 object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
144 module.addobject(object)
146 # Create the generator classes used to populate the lists
147 Function = OSErrFunctionGenerator
148 Method = OSErrMethodGenerator
150 # Create and populate the lists
151 functions = []
152 methods = []
153 execfile("dlggen.py")
155 # add the populated lists to the generator groups
156 for f in functions: module.add(f)
157 for f in methods: object.add(f)
159 # Some methods that are currently macro's in C, but will be real routines
160 # in MacOS 8.
162 f = Method(ExistingDialogPtr, 'GetDialogWindow', (DialogRef, 'dialog', InMode))
163 object.add(f)
164 f = Method(SInt16, 'GetDialogDefaultItem', (DialogRef, 'dialog', InMode))
165 object.add(f)
166 f = Method(SInt16, 'GetDialogCancelItem', (DialogRef, 'dialog', InMode))
167 object.add(f)
168 f = Method(SInt16, 'GetDialogKeyboardFocusItem', (DialogRef, 'dialog', InMode))
169 object.add(f)
170 f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode))
171 object.add(f)
173 setuseritembody = """
174 PyObject *new = NULL;
177 if (!PyArg_ParseTuple(_args, "|O", &new))
178 return NULL;
180 if (Dlg_UserItemProc_callback && new && new != Py_None) {
181 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
182 return NULL;
185 if (new == Py_None) {
186 new = NULL;
187 _res = Py_None;
188 Py_INCREF(Py_None);
189 } else {
190 Py_INCREF(new);
191 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemProc(Dlg_UnivUserItemProc));
194 Dlg_UserItemProc_callback = new;
195 return _res;
197 f = ManualGenerator("SetUserItemHandler", setuseritembody)
198 module.add(f)
200 # generate output
201 SetOutputFileName('Dlgmodule.c')
202 module.generate()