blob: 526bfa441cdd22c502a45c06f3eb7699a0ab2e65 [file] [log] [blame]
Guido van Rossum17448e21995-01-30 11:53:55 +00001# 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).
5
6from macsupport import *
7
8# Create the type objects
9
10DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
Jack Jansenae8a68f1995-06-06 12:55:40 +000011DialogRef = DialogPtr
Guido van Rossum17448e21995-01-30 11:53:55 +000012
Jack Jansen425e9eb1995-12-12 15:02:03 +000013# An OptHandle is either a handle or None (in case NULL is passed in).
14# This is needed for GetDialogItem().
15OptHandle = OpaqueByValueType("Handle", "OptResObj")
Jack Jansen91a63981995-08-17 14:30:52 +000016
Guido van Rossum17448e21995-01-30 11:53:55 +000017ModalFilterProcPtr = InputOnlyType("PyObject*", "O")
Guido van Rossum97842951995-02-19 15:59:49 +000018ModalFilterProcPtr.passInput = lambda name: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name
Jack Jansenae8a68f1995-06-06 12:55:40 +000019ModalFilterUPP = ModalFilterProcPtr
Guido van Rossum17448e21995-01-30 11:53:55 +000020
Jack Jansen2b724171996-04-10 14:48:19 +000021RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
Guido van Rossum17448e21995-01-30 11:53:55 +000022
23DITLMethod = Type("DITLMethod", "h")
Jack Jansen21f96871998-02-20 16:02:09 +000024DialogItemIndex = Type("DialogItemIndex", "h")
25DialogItemType = Type("DialogItemType", "h")
26DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h")
27AlertType = Type("AlertType", "h")
28StringPtr = Str255
Guido van Rossum17448e21995-01-30 11:53:55 +000029
30includestuff = includestuff + """
31#include <Dialogs.h>
32
Guido van Rossum97842951995-02-19 15:59:49 +000033#ifndef HAVE_UNIVERSAL_HEADERS
34#define NewModalFilterProc(x) (x)
35#endif
36
Guido van Rossum17448e21995-01-30 11:53:55 +000037#define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
38
39/* XXX Shouldn't this be a stack? */
40static PyObject *Dlg_FilterProc_callback = NULL;
41
42static PyObject *DlgObj_New(DialogPtr); /* Forward */
43
44static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
45 EventRecord *event,
46 short *itemHit)
47{
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 */
Guido van Rossum0437e891995-02-21 20:56:21 +000054 args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
Guido van Rossum17448e21995-01-30 11:53:55 +000055 if (args == NULL)
56 res = NULL;
57 else {
58 res = PyEval_CallObject(callback, args);
59 Py_DECREF(args);
60 }
61 if (res == NULL) {
Jack Jansendeff89c1998-10-12 20:53:15 +000062 PySys_WriteStderr("Exception in Dialog Filter\\n");
Guido van Rossum17448e21995-01-30 11:53:55 +000063 PyErr_Print();
64 *itemHit = -1; /* Fake return item */
65 return 1; /* We handled it */
66 }
67 else {
68 Dlg_FilterProc_callback = callback;
69 if (PyInt_Check(res)) {
70 *itemHit = PyInt_AsLong(res);
71 rv = 1;
72 }
73 else
74 rv = PyObject_IsTrue(res);
75 }
76 Py_DECREF(res);
77 return rv;
78}
79
80static ModalFilterProcPtr
81Dlg_PassFilterProc(PyObject *callback)
82{
83 PyObject *tmp = Dlg_FilterProc_callback;
84 Dlg_FilterProc_callback = NULL;
85 if (callback == Py_None) {
86 Py_XDECREF(tmp);
87 return NULL;
88 }
89 Py_INCREF(callback);
90 Dlg_FilterProc_callback = callback;
91 Py_XDECREF(tmp);
92 return &Dlg_UnivFilterProc;
93}
94
Jack Jansendf901df1998-07-10 15:47:48 +000095static PyObject *Dlg_UserItemProc_callback = NULL;
96
97static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
98 short item)
99{
100 PyObject *args, *res;
101
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);
111 }
112 if (res == NULL) {
Jack Jansendeff89c1998-10-12 20:53:15 +0000113 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
Jack Jansendf901df1998-07-10 15:47:48 +0000114 PyErr_Print();
115 }
116 Py_XDECREF(res);
117 return;
118}
119
Guido van Rossum17448e21995-01-30 11:53:55 +0000120extern PyMethodChain WinObj_chain;
121"""
122
123
124# Define a class which specializes our object definition
125class 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)
140
141# Create the generator groups and link them
142module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff)
143object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
144module.addobject(object)
145
146# Create the generator classes used to populate the lists
147Function = OSErrFunctionGenerator
148Method = OSErrMethodGenerator
149
150# Create and populate the lists
151functions = []
152methods = []
153execfile("dlggen.py")
154
155# add the populated lists to the generator groups
156for f in functions: module.add(f)
157for f in methods: object.add(f)
158
Jack Jansend96cb501996-09-23 15:48:46 +0000159# Some methods that are currently macro's in C, but will be real routines
160# in MacOS 8.
161
162f = Method(ExistingDialogPtr, 'GetDialogWindow', (DialogRef, 'dialog', InMode))
163object.add(f)
164f = Method(SInt16, 'GetDialogDefaultItem', (DialogRef, 'dialog', InMode))
165object.add(f)
166f = Method(SInt16, 'GetDialogCancelItem', (DialogRef, 'dialog', InMode))
167object.add(f)
168f = Method(SInt16, 'GetDialogKeyboardFocusItem', (DialogRef, 'dialog', InMode))
169object.add(f)
170f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode))
171object.add(f)
172
Jack Jansendf901df1998-07-10 15:47:48 +0000173setuseritembody = """
174 PyObject *new = NULL;
175
176
177 if (!PyArg_ParseTuple(_args, "|O", &new))
178 return NULL;
179
180 if (Dlg_UserItemProc_callback && new && new != Py_None) {
181 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
182 return NULL;
183 }
184
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));
192 }
193
194 Dlg_UserItemProc_callback = new;
195 return _res;
196"""
197f = ManualGenerator("SetUserItemHandler", setuseritembody)
198module.add(f)
199
Guido van Rossum17448e21995-01-30 11:53:55 +0000200# generate output
201SetOutputFileName('Dlgmodule.c')
202module.generate()