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")
30 includestuff
= includestuff
+ """
33 #ifndef HAVE_UNIVERSAL_HEADERS
34 #define NewModalFilterProc(x) (x)
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,
50 PyObject *callback = Dlg_FilterProc_callback;
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);
58 res = PyEval_CallObject(callback, args);
62 PySys_WriteStderr("Exception in Dialog Filter\\n");
64 *itemHit = -1; /* Fake return item */
65 return 1; /* We handled it */
68 Dlg_FilterProc_callback = callback;
69 if (PyInt_Check(res)) {
70 *itemHit = PyInt_AsLong(res);
74 rv = PyObject_IsTrue(res);
80 static ModalFilterProcPtr
81 Dlg_PassFilterProc(PyObject *callback)
83 PyObject *tmp = Dlg_FilterProc_callback;
84 Dlg_FilterProc_callback = NULL;
85 if (callback == Py_None) {
90 Dlg_FilterProc_callback = callback;
92 return &Dlg_UnivFilterProc;
95 static PyObject *Dlg_UserItemProc_callback = NULL;
97 static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
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);
109 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
113 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
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
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
162 f
= Method(ExistingDialogPtr
, 'GetDialogWindow', (DialogRef
, 'dialog', InMode
))
164 f
= Method(SInt16
, 'GetDialogDefaultItem', (DialogRef
, 'dialog', InMode
))
166 f
= Method(SInt16
, 'GetDialogCancelItem', (DialogRef
, 'dialog', InMode
))
168 f
= Method(SInt16
, 'GetDialogKeyboardFocusItem', (DialogRef
, 'dialog', InMode
))
170 f
= Method(void
, 'SetGrafPortOfDialog', (DialogRef
, 'dialog', InMode
))
173 setuseritembody
= """
174 PyObject *new = NULL;
177 if (!PyArg_ParseTuple(_args, "|O", &new))
180 if (Dlg_UserItemProc_callback && new && new != Py_None) {
181 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
185 if (new == Py_None) {
191 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemProc(Dlg_UnivUserItemProc));
194 Dlg_UserItemProc_callback = new;
197 f
= ManualGenerator("SetUserItemHandler", setuseritembody
)
201 SetOutputFileName('Dlgmodule.c')