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
: "Dlg_PassFilterProc(%s)" % name
19 ModalFilterUPP
= ModalFilterProcPtr
21 RgnHandle
= OpaqueByValueType("RgnHandle", "ResObj")
22 TEHandle
= OpaqueByValueType("TEHandle", "ResObj")
23 CGrafPtr
= OpaqueByValueType("CGrafPtr", "GrafObj")
25 DITLMethod
= Type("DITLMethod", "h")
26 DialogItemIndex
= Type("DialogItemIndex", "h")
27 DialogItemType
= Type("DialogItemType", "h")
28 DialogItemIndexZeroBased
= Type("DialogItemIndexZeroBased", "h")
29 AlertType
= Type("AlertType", "h")
31 EventMask
= Type("EventMask", "H")
33 includestuff
= includestuff
+ """
34 #ifdef WITHOUT_FRAMEWORKS
37 #include <Carbon/Carbon.h>
40 #ifdef USE_TOOLBOX_OBJECT_GLUE
41 extern PyObject *_DlgObj_New(DialogRef);
42 extern PyObject *_DlgObj_WhichDialog(DialogRef);
43 extern int _DlgObj_Convert(PyObject *, DialogRef *);
45 #define DlgObj_New _DlgObj_New
46 #define DlgObj_WhichDialog _DlgObj_WhichDialog
47 #define DlgObj_Convert _DlgObj_Convert
50 /* XXX Shouldn't this be a stack? */
51 static PyObject *Dlg_FilterProc_callback = NULL;
53 static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
59 PyObject *callback = Dlg_FilterProc_callback;
61 return 0; /* Default behavior */
62 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
63 args = Py_BuildValue("O&O&", DlgObj_WhichDialog, dialog, PyMac_BuildEventRecord, event);
67 res = PyEval_CallObject(callback, args);
71 PySys_WriteStderr("Exception in Dialog Filter\\n");
73 *itemHit = -1; /* Fake return item */
74 return 1; /* We handled it */
77 Dlg_FilterProc_callback = callback;
78 if (PyInt_Check(res)) {
79 *itemHit = PyInt_AsLong(res);
83 rv = PyObject_IsTrue(res);
90 Dlg_PassFilterProc(PyObject *callback)
92 PyObject *tmp = Dlg_FilterProc_callback;
93 static ModalFilterUPP UnivFilterUpp = NULL;
95 Dlg_FilterProc_callback = NULL;
96 if (callback == Py_None) {
101 Dlg_FilterProc_callback = callback;
103 if ( UnivFilterUpp == NULL )
104 UnivFilterUpp = NewModalFilterUPP(&Dlg_UnivFilterProc);
105 return UnivFilterUpp;
108 static PyObject *Dlg_UserItemProc_callback = NULL;
110 static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
113 PyObject *args, *res;
115 if (Dlg_UserItemProc_callback == NULL)
116 return; /* Default behavior */
117 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
118 args = Py_BuildValue("O&h", DlgObj_WhichDialog, dialog, item);
122 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
126 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
135 ** Treating DialogObjects as WindowObjects is (I think) illegal under Carbon.
136 ** However, as they are still identical under MacOS9 Carbon this is a problem, even
137 ** if we neatly call GetDialogWindow() at the right places: there's one refcon field
138 ** and it points to the DialogObject, so WinObj_WhichWindow will smartly return the
139 ** dialog object, and therefore we still don't have a WindowObject.
140 ** I'll leave the chaining code in place for now, with this comment to warn the
141 ** unsuspecting victims (i.e. me, probably, in a few weeks:-)
143 extern PyMethodChain WinObj_chain;
147 finalstuff
= finalstuff
+ """
148 /* Return the WindowPtr corresponding to a DialogObject */
151 DlgObj_ConvertToWindow(PyObject *self)
153 if ( DlgObj_Check(self) )
154 return GetDialogWindow(((DialogObject *)self)->ob_itself);
158 /* Return the object corresponding to the dialog, or None */
161 DlgObj_WhichDialog(DialogPtr d)
169 WindowPtr w = GetDialogWindow(d);
171 it = (PyObject *) GetWRefCon(w);
172 if (it == NULL || ((DialogObject *)it)->ob_itself != d || !DlgObj_Check(it)) {
174 /* Should do this, but we don't have an ob_freeit for dialogs yet. */
176 ((WindowObject *)it)->ob_freeit = NULL;
189 initstuff
= initstuff
+ """
190 PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_New);
191 PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_WhichDialog);
192 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DialogPtr, DlgObj_Convert);
196 # Define a class which specializes our object definition
197 class MyObjectDefinition(PEP253Mixin
, GlobalObjectDefinition
):
198 def __init__(self
, name
, prefix
= None, itselftype
= None):
199 GlobalObjectDefinition
.__init
__(self
, name
, prefix
, itselftype
)
200 ## This won't work in Carbon, so we disable it for all MacPythons:-(
201 ## But see the comment above:-((
202 ## self.basechain = "&WinObj_chain"
204 def outputInitStructMembers(self
):
205 GlobalObjectDefinition
.outputInitStructMembers(self
)
206 Output("SetWRefCon(GetDialogWindow(itself), (long)it);")
208 def outputCheckNewArg(self
):
209 Output("if (itself == NULL) return Py_None;")
211 def outputCheckConvertArg(self
):
212 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
213 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
214 Output(" return 1; }")
216 def outputCompare(self
):
218 Output("static int %s_compare(%s *self, %s *other)", self
.prefix
, self
.objecttype
, self
.objecttype
)
220 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
221 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
225 def outputHash(self
):
227 Output("static int %s_hash(%s *self)", self
.prefix
, self
.objecttype
)
229 Output("return (int)self->ob_itself;")
232 def outputFreeIt(self
, itselfname
):
233 Output("DisposeDialog(%s);", itselfname
)
235 # Create the generator groups and link them
236 module
= MacModule('_Dlg', 'Dlg', includestuff
, finalstuff
, initstuff
)
237 object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
238 module
.addobject(object)
240 # Create the generator classes used to populate the lists
241 Function
= OSErrWeakLinkFunctionGenerator
242 Method
= OSErrWeakLinkMethodGenerator
244 # Create and populate the lists
247 execfile("dlggen.py")
249 # add the populated lists to the generator groups
250 for f
in functions
: module
.add(f
)
251 for f
in methods
: object.add(f
)
253 setuseritembody
= """
254 PyObject *new = NULL;
257 if (!PyArg_ParseTuple(_args, "|O", &new))
260 if (Dlg_UserItemProc_callback && new && new != Py_None) {
261 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
265 if (new == NULL || new == Py_None) {
271 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemUPP(Dlg_UnivUserItemProc));
274 Dlg_UserItemProc_callback = new;
277 f
= ManualGenerator("SetUserItemHandler", setuseritembody
)
281 SetOutputFileName('_Dlgmodule.c')