blob: ab22e180ff6a4b0f17dcefe74452ebcdaff81977 [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")
Jack Jansenf7d5aa62000-12-10 23:43:49 +000022TEHandle = OpaqueByValueType("TEHandle", "ResObj")
23CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
Guido van Rossum17448e21995-01-30 11:53:55 +000024
25DITLMethod = Type("DITLMethod", "h")
Jack Jansen21f96871998-02-20 16:02:09 +000026DialogItemIndex = Type("DialogItemIndex", "h")
27DialogItemType = Type("DialogItemType", "h")
28DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h")
29AlertType = Type("AlertType", "h")
30StringPtr = Str255
Jack Jansen0b13e7c2000-07-07 13:09:35 +000031EventMask = Type("EventMask", "H")
Guido van Rossum17448e21995-01-30 11:53:55 +000032
33includestuff = includestuff + """
34#include <Dialogs.h>
35
Jack Jansenf7d5aa62000-12-10 23:43:49 +000036#if !ACCESSOR_CALLS_ARE_FUNCTIONS
37#define GetDialogTextEditHandle(dlg) (((DialogPeek)(dlg))->textH)
38#define SetPortDialogPort(dlg) SetPort(dlg)
39#define GetDialogPort(dlg) ((CGrafPtr)(dlg))
40#define GetDialogFromWindow(win) ((DialogRef)(win))
41#endif
42
Guido van Rossum17448e21995-01-30 11:53:55 +000043/* XXX Shouldn't this be a stack? */
44static PyObject *Dlg_FilterProc_callback = NULL;
45
Guido van Rossum17448e21995-01-30 11:53:55 +000046static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
47 EventRecord *event,
48 short *itemHit)
49{
50 Boolean rv;
51 PyObject *args, *res;
52 PyObject *callback = Dlg_FilterProc_callback;
53 if (callback == NULL)
54 return 0; /* Default behavior */
55 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
Jack Jansen69e7f112001-02-06 16:14:54 +000056 args = Py_BuildValue("O&O&", DlgObj_WhichDialog, dialog, PyMac_BuildEventRecord, event);
Guido van Rossum17448e21995-01-30 11:53:55 +000057 if (args == NULL)
58 res = NULL;
59 else {
60 res = PyEval_CallObject(callback, args);
61 Py_DECREF(args);
62 }
63 if (res == NULL) {
Jack Jansendeff89c1998-10-12 20:53:15 +000064 PySys_WriteStderr("Exception in Dialog Filter\\n");
Guido van Rossum17448e21995-01-30 11:53:55 +000065 PyErr_Print();
66 *itemHit = -1; /* Fake return item */
67 return 1; /* We handled it */
68 }
69 else {
70 Dlg_FilterProc_callback = callback;
71 if (PyInt_Check(res)) {
72 *itemHit = PyInt_AsLong(res);
73 rv = 1;
74 }
75 else
76 rv = PyObject_IsTrue(res);
77 }
78 Py_DECREF(res);
79 return rv;
80}
81
82static ModalFilterProcPtr
83Dlg_PassFilterProc(PyObject *callback)
84{
85 PyObject *tmp = Dlg_FilterProc_callback;
86 Dlg_FilterProc_callback = NULL;
87 if (callback == Py_None) {
88 Py_XDECREF(tmp);
89 return NULL;
90 }
91 Py_INCREF(callback);
92 Dlg_FilterProc_callback = callback;
93 Py_XDECREF(tmp);
94 return &Dlg_UnivFilterProc;
95}
96
Jack Jansendf901df1998-07-10 15:47:48 +000097static PyObject *Dlg_UserItemProc_callback = NULL;
98
99static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
100 short item)
101{
102 PyObject *args, *res;
103
104 if (Dlg_UserItemProc_callback == NULL)
105 return; /* Default behavior */
106 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
Jack Jansen69e7f112001-02-06 16:14:54 +0000107 args = Py_BuildValue("O&h", DlgObj_WhichDialog, dialog, item);
Jack Jansendf901df1998-07-10 15:47:48 +0000108 if (args == NULL)
109 res = NULL;
110 else {
111 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
112 Py_DECREF(args);
113 }
114 if (res == NULL) {
Jack Jansendeff89c1998-10-12 20:53:15 +0000115 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
Jack Jansendf901df1998-07-10 15:47:48 +0000116 PyErr_Print();
117 }
118 Py_XDECREF(res);
119 return;
120}
121
Jack Jansen69e7f112001-02-06 16:14:54 +0000122#if 0
Jack Jansenb8c4c7b2000-08-25 22:25:54 +0000123/*
124** Treating DialogObjects as WindowObjects is (I think) illegal under Carbon.
125** However, as they are still identical under MacOS9 Carbon this is a problem, even
126** if we neatly call GetDialogWindow() at the right places: there's one refcon field
127** and it points to the DialogObject, so WinObj_WhichWindow will smartly return the
128** dialog object, and therefore we still don't have a WindowObject.
129** I'll leave the chaining code in place for now, with this comment to warn the
130** unsuspecting victims (i.e. me, probably, in a few weeks:-)
131*/
Guido van Rossum17448e21995-01-30 11:53:55 +0000132extern PyMethodChain WinObj_chain;
Jack Jansenb8c4c7b2000-08-25 22:25:54 +0000133#endif
134"""
135
136finalstuff = finalstuff + """
137/* Return the WindowPtr corresponding to a DialogObject */
138
139WindowPtr
140DlgObj_ConvertToWindow(self)
141 PyObject *self;
142{
143 if ( DlgObj_Check(self) )
144 return GetDialogWindow(((DialogObject *)self)->ob_itself);
145 return NULL;
146}
Jack Jansen69e7f112001-02-06 16:14:54 +0000147/* Return the object corresponding to the dialog, or None */
148
149PyObject *
150DlgObj_WhichDialog(d)
151 DialogPtr d;
152{
153 PyObject *it;
154
155 if (d == NULL) {
156 it = Py_None;
157 Py_INCREF(it);
158 } else {
159 WindowPtr w = GetDialogWindow(d);
160
161 it = (PyObject *) GetWRefCon(w);
162 if (it == NULL || ((DialogObject *)it)->ob_itself != d || !DlgObj_Check(it)) {
163#if 0
164 /* Should do this, but we don't have an ob_freeit for dialogs yet. */
165 it = WinObj_New(w);
166 ((WindowObject *)it)->ob_freeit = NULL;
167#else
168 it = Py_None;
169 Py_INCREF(it);
170#endif
171 } else {
172 Py_INCREF(it);
173 }
174 }
175 return it;
176}
Guido van Rossum17448e21995-01-30 11:53:55 +0000177"""
178
179
180# Define a class which specializes our object definition
181class MyObjectDefinition(GlobalObjectDefinition):
182 def __init__(self, name, prefix = None, itselftype = None):
183 GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
Jack Jansenb8c4c7b2000-08-25 22:25:54 +0000184## This won't work in Carbon, so we disable it for all MacPythons:-(
185## But see the comment above:-((
Jack Jansen69e7f112001-02-06 16:14:54 +0000186## self.basechain = "&WinObj_chain"
187
Guido van Rossum17448e21995-01-30 11:53:55 +0000188 def outputInitStructMembers(self):
189 GlobalObjectDefinition.outputInitStructMembers(self)
Jack Jansene79dc762000-06-02 21:35:07 +0000190 Output("SetWRefCon(GetDialogWindow(itself), (long)it);")
Jack Jansen69e7f112001-02-06 16:14:54 +0000191
Guido van Rossum17448e21995-01-30 11:53:55 +0000192 def outputCheckNewArg(self):
193 Output("if (itself == NULL) return Py_None;")
Jack Jansen69e7f112001-02-06 16:14:54 +0000194
Guido van Rossum17448e21995-01-30 11:53:55 +0000195 def outputCheckConvertArg(self):
196 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
197 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
198 Output(" return 1; }")
Jack Jansen69e7f112001-02-06 16:14:54 +0000199
200 def outputCompare(self):
201 Output()
202 Output("static int %s_compare(self, other)", self.prefix)
203 IndentLevel()
204 Output("%s *self, *other;", self.objecttype)
205 DedentLevel()
206 OutLbrace()
207 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
208 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
209 Output("return 0;")
210 OutRbrace()
211
212 def outputHash(self):
213 Output()
214 Output("static int %s_hash(self)", self.prefix)
215 IndentLevel()
216 Output("%s *self;", self.objecttype)
217 DedentLevel()
218 OutLbrace()
219 Output("return (int)self->ob_itself;")
220 OutRbrace()
221
Guido van Rossum17448e21995-01-30 11:53:55 +0000222 def outputFreeIt(self, itselfname):
223 Output("DisposeDialog(%s);", itselfname)
224
225# Create the generator groups and link them
226module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff)
227object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
228module.addobject(object)
229
230# Create the generator classes used to populate the lists
231Function = OSErrFunctionGenerator
232Method = OSErrMethodGenerator
233
234# Create and populate the lists
235functions = []
236methods = []
237execfile("dlggen.py")
238
239# add the populated lists to the generator groups
240for f in functions: module.add(f)
241for f in methods: object.add(f)
242
Jack Jansend96cb501996-09-23 15:48:46 +0000243# Some methods that are currently macro's in C, but will be real routines
244# in MacOS 8.
245
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000246##f = Method(ExistingWindowPtr, 'GetDialogWindow', (DialogRef, 'dialog', InMode))
247##object.add(f)
248##f = Method(SInt16, 'GetDialogDefaultItem', (DialogRef, 'dialog', InMode))
249##object.add(f)
250##f = Method(SInt16, 'GetDialogCancelItem', (DialogRef, 'dialog', InMode))
251##object.add(f)
252##f = Method(SInt16, 'GetDialogKeyboardFocusItem', (DialogRef, 'dialog', InMode))
253##object.add(f)
Jack Jansene79dc762000-06-02 21:35:07 +0000254f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode),
Jack Jansen74a1e632000-07-14 22:37:27 +0000255 condition='#if !TARGET_API_MAC_CARBON')
Jack Jansend96cb501996-09-23 15:48:46 +0000256object.add(f)
257
Jack Jansendf901df1998-07-10 15:47:48 +0000258setuseritembody = """
259 PyObject *new = NULL;
260
261
262 if (!PyArg_ParseTuple(_args, "|O", &new))
263 return NULL;
264
265 if (Dlg_UserItemProc_callback && new && new != Py_None) {
266 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
267 return NULL;
268 }
269
270 if (new == Py_None) {
271 new = NULL;
272 _res = Py_None;
273 Py_INCREF(Py_None);
274 } else {
275 Py_INCREF(new);
276 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemProc(Dlg_UnivUserItemProc));
277 }
278
279 Dlg_UserItemProc_callback = new;
280 return _res;
281"""
282f = ManualGenerator("SetUserItemHandler", setuseritembody)
283module.add(f)
284
Guido van Rossum17448e21995-01-30 11:53:55 +0000285# generate output
286SetOutputFileName('Dlgmodule.c')
287module.generate()