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
#include <Carbon/Carbon.h>
36
#ifdef USE_TOOLBOX_OBJECT_GLUE
37
extern PyObject *_DlgObj_New(DialogRef);
38
extern PyObject *_DlgObj_WhichDialog(DialogRef);
39
extern int _DlgObj_Convert(PyObject *, DialogRef *);
41
#define DlgObj_New _DlgObj_New
42
#define DlgObj_WhichDialog _DlgObj_WhichDialog
43
#define DlgObj_Convert _DlgObj_Convert
46
/* XXX Shouldn't this be a stack? */
47
static PyObject *Dlg_FilterProc_callback = NULL;
49
static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
55
PyObject *callback = Dlg_FilterProc_callback;
57
return 0; /* Default behavior */
58
Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
59
args = Py_BuildValue("O&O&", DlgObj_WhichDialog, dialog, PyMac_BuildEventRecord, event);
63
res = PyEval_CallObject(callback, args);
67
PySys_WriteStderr("Exception in Dialog Filter\\n");
69
*itemHit = -1; /* Fake return item */
70
return 1; /* We handled it */
73
Dlg_FilterProc_callback = callback;
74
if (PyInt_Check(res)) {
75
*itemHit = PyInt_AsLong(res);
79
rv = PyObject_IsTrue(res);
86
Dlg_PassFilterProc(PyObject *callback)
88
PyObject *tmp = Dlg_FilterProc_callback;
89
static ModalFilterUPP UnivFilterUpp = NULL;
91
Dlg_FilterProc_callback = NULL;
92
if (callback == Py_None) {
97
Dlg_FilterProc_callback = callback;
99
if ( UnivFilterUpp == NULL )
100
UnivFilterUpp = NewModalFilterUPP(&Dlg_UnivFilterProc);
101
return UnivFilterUpp;
104
static PyObject *Dlg_UserItemProc_callback = NULL;
106
static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
109
PyObject *args, *res;
111
if (Dlg_UserItemProc_callback == NULL)
112
return; /* Default behavior */
113
Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
114
args = Py_BuildValue("O&h", DlgObj_WhichDialog, dialog, item);
118
res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
122
PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
131
** Treating DialogObjects as WindowObjects is (I think) illegal under Carbon.
132
** However, as they are still identical under MacOS9 Carbon this is a problem, even
133
** if we neatly call GetDialogWindow() at the right places: there's one refcon field
134
** and it points to the DialogObject, so WinObj_WhichWindow will smartly return the
135
** dialog object, and therefore we still don't have a WindowObject.
136
** I'll leave the chaining code in place for now, with this comment to warn the
137
** unsuspecting victims (i.e. me, probably, in a few weeks:-)
139
extern PyMethodChain WinObj_chain;
143
finalstuff = finalstuff + """
144
/* Return the WindowPtr corresponding to a DialogObject */
147
DlgObj_ConvertToWindow(PyObject *self)
149
if ( DlgObj_Check(self) )
150
return GetDialogWindow(((DialogObject *)self)->ob_itself);
154
/* Return the object corresponding to the dialog, or None */
157
DlgObj_WhichDialog(DialogPtr d)
165
WindowPtr w = GetDialogWindow(d);
167
it = (PyObject *) GetWRefCon(w);
168
if (it == NULL || ((DialogObject *)it)->ob_itself != d || !DlgObj_Check(it)) {
170
/* Should do this, but we don't have an ob_freeit for dialogs yet. */
172
((WindowObject *)it)->ob_freeit = NULL;
185
initstuff = initstuff + """
186
PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_New);
187
PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_WhichDialog);
188
PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DialogPtr, DlgObj_Convert);
192
# Define a class which specializes our object definition
193
class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
194
def __init__(self, name, prefix = None, itselftype = None):
195
GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
196
## This won't work in Carbon, so we disable it for all MacPythons:-(
197
## But see the comment above:-((
198
## self.basechain = "&WinObj_chain"
200
def outputInitStructMembers(self):
201
GlobalObjectDefinition.outputInitStructMembers(self)
202
Output("SetWRefCon(GetDialogWindow(itself), (long)it);")
204
def outputCheckNewArg(self):
205
Output("if (itself == NULL) { Py_INCREF(Py_None); return Py_None; }")
207
def outputCheckConvertArg(self):
208
Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
209
Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
210
Output(" return 1; }")
212
def outputCompare(self):
214
Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
216
Output("if ( self->ob_itself > other->ob_itself ) return 1;")
217
Output("if ( self->ob_itself < other->ob_itself ) return -1;")
221
def outputHash(self):
223
Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
225
Output("return (int)self->ob_itself;")
228
def outputFreeIt(self, itselfname):
229
Output("DisposeDialog(%s);", itselfname)
231
# Create the generator groups and link them
232
module = MacModule('_Dlg', 'Dlg', includestuff, finalstuff, initstuff)
233
object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
234
module.addobject(object)
236
# Create the generator classes used to populate the lists
237
Function = OSErrWeakLinkFunctionGenerator
238
Method = OSErrWeakLinkMethodGenerator
240
# Create and populate the lists
243
execfile("dlggen.py")
245
# add the populated lists to the generator groups
246
for f in functions: module.add(f)
247
for f in methods: object.add(f)
249
setuseritembody = """
250
PyObject *new = NULL;
253
if (!PyArg_ParseTuple(_args, "|O", &new))
256
if (Dlg_UserItemProc_callback && new && new != Py_None) {
257
PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
261
if (new == NULL || new == Py_None) {
267
_res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemUPP(Dlg_UnivUserItemProc));
270
Dlg_UserItemProc_callback = new;
273
f = ManualGenerator("SetUserItemHandler", setuseritembody)
277
SetOutputFileName('_Dlgmodule.c')