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")
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")
29 EventMask
= Type("EventMask", "h")
31 includestuff
= includestuff
+ """
34 #ifndef HAVE_UNIVERSAL_HEADERS
35 #define NewModalFilterProc(x) (x)
38 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
40 /* XXX Shouldn't this be a stack? */
41 static PyObject *Dlg_FilterProc_callback = NULL;
43 static PyObject *DlgObj_New(DialogPtr); /* Forward */
45 static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
51 PyObject *callback = Dlg_FilterProc_callback;
53 return 0; /* Default behavior */
54 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
55 args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
59 res = PyEval_CallObject(callback, args);
63 PySys_WriteStderr("Exception in Dialog Filter\\n");
65 *itemHit = -1; /* Fake return item */
66 return 1; /* We handled it */
69 Dlg_FilterProc_callback = callback;
70 if (PyInt_Check(res)) {
71 *itemHit = PyInt_AsLong(res);
75 rv = PyObject_IsTrue(res);
81 static ModalFilterProcPtr
82 Dlg_PassFilterProc(PyObject *callback)
84 PyObject *tmp = Dlg_FilterProc_callback;
85 Dlg_FilterProc_callback = NULL;
86 if (callback == Py_None) {
91 Dlg_FilterProc_callback = callback;
93 return &Dlg_UnivFilterProc;
96 static PyObject *Dlg_UserItemProc_callback = NULL;
98 static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
101 PyObject *args, *res;
103 if (Dlg_UserItemProc_callback == NULL)
104 return; /* Default behavior */
105 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
106 args = Py_BuildValue("O&h", WinObj_WhichWindow, dialog, item);
110 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
114 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
121 extern PyMethodChain WinObj_chain;
125 # Define a class which specializes our object definition
126 class MyObjectDefinition(GlobalObjectDefinition
):
127 def __init__(self
, name
, prefix
= None, itselftype
= None):
128 GlobalObjectDefinition
.__init
__(self
, name
, prefix
, itselftype
)
129 self
.basechain
= "&WinObj_chain"
130 def outputInitStructMembers(self
):
131 GlobalObjectDefinition
.outputInitStructMembers(self
)
132 Output("SetWRefCon(itself, (long)it);")
133 def outputCheckNewArg(self
):
134 Output("if (itself == NULL) return Py_None;")
135 def outputCheckConvertArg(self
):
136 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
137 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
138 Output(" return 1; }")
139 def outputFreeIt(self
, itselfname
):
140 Output("DisposeDialog(%s);", itselfname
)
142 # Create the generator groups and link them
143 module
= MacModule('Dlg', 'Dlg', includestuff
, finalstuff
, initstuff
)
144 object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
145 module
.addobject(object)
147 # Create the generator classes used to populate the lists
148 Function
= OSErrFunctionGenerator
149 Method
= OSErrMethodGenerator
151 # Create and populate the lists
154 execfile("dlggen.py")
156 # add the populated lists to the generator groups
157 for f
in functions
: module
.add(f
)
158 for f
in methods
: object.add(f
)
160 # Some methods that are currently macro's in C, but will be real routines
163 f
= Method(ExistingDialogPtr
, 'GetDialogWindow', (DialogRef
, 'dialog', InMode
))
165 f
= Method(SInt16
, 'GetDialogDefaultItem', (DialogRef
, 'dialog', InMode
))
167 f
= Method(SInt16
, 'GetDialogCancelItem', (DialogRef
, 'dialog', InMode
))
169 f
= Method(SInt16
, 'GetDialogKeyboardFocusItem', (DialogRef
, 'dialog', InMode
))
171 f
= Method(void
, 'SetGrafPortOfDialog', (DialogRef
, 'dialog', InMode
))
174 setuseritembody
= """
175 PyObject *new = NULL;
178 if (!PyArg_ParseTuple(_args, "|O", &new))
181 if (Dlg_UserItemProc_callback && new && new != Py_None) {
182 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
186 if (new == Py_None) {
192 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemProc(Dlg_UnivUserItemProc));
195 Dlg_UserItemProc_callback = new;
198 f
= ManualGenerator("SetUserItemHandler", setuseritembody
)
202 SetOutputFileName('Dlgmodule.c')