dlgsupport.py revision 0c4d947eacfb360515a5ba58f1a6b96dddc6d265
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).
5
6from macsupport import *
7
8# Create the type objects
9
10DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
11DialogRef = DialogPtr
12
13# An OptHandle is either a handle or None (in case NULL is passed in).
14# This is needed for GetDialogItem().
15OptHandle = OpaqueByValueType("Handle", "OptResObj")
16
17ModalFilterProcPtr = InputOnlyType("PyObject*", "O")
18ModalFilterProcPtr.passInput = lambda name: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name
19ModalFilterUPP = ModalFilterProcPtr
20
21RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
22
23DITLMethod = Type("DITLMethod", "h")
24DialogItemIndex = Type("DialogItemIndex", "h")
25DialogItemType = Type("DialogItemType", "h")
26DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h")
27AlertType = Type("AlertType", "h")
28StringPtr = Str255
29
30includestuff = includestuff + """
31#include <Dialogs.h>
32
33#ifndef HAVE_UNIVERSAL_HEADERS
34#define NewModalFilterProc(x) (x)
35#endif
36
37#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 */
54	args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
55	if (args == NULL)
56		res = NULL;
57	else {
58		res = PyEval_CallObject(callback, args);
59		Py_DECREF(args);
60	}
61	if (res == NULL) {
62		fprintf(stderr, "Exception in Dialog Filter\\n");
63		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
95extern PyMethodChain WinObj_chain;
96"""
97
98
99# Define a class which specializes our object definition
100class MyObjectDefinition(GlobalObjectDefinition):
101	def __init__(self, name, prefix = None, itselftype = None):
102		GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
103		self.basechain = "&WinObj_chain"
104	def outputInitStructMembers(self):
105		GlobalObjectDefinition.outputInitStructMembers(self)
106		Output("SetWRefCon(itself, (long)it);")
107	def outputCheckNewArg(self):
108		Output("if (itself == NULL) return Py_None;")
109	def outputCheckConvertArg(self):
110		Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
111		Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
112		Output("                      return 1; }")
113	def outputFreeIt(self, itselfname):
114		Output("DisposeDialog(%s);", itselfname)
115
116# Create the generator groups and link them
117module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff)
118object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
119module.addobject(object)
120
121# Create the generator classes used to populate the lists
122Function = OSErrFunctionGenerator
123Method = OSErrMethodGenerator
124
125# Create and populate the lists
126functions = []
127methods = []
128execfile("dlggen.py")
129
130# add the populated lists to the generator groups
131for f in functions: module.add(f)
132for f in methods: object.add(f)
133
134# Some methods that are currently macro's in C, but will be real routines
135# in MacOS 8.
136
137f = Method(ExistingDialogPtr, 'GetDialogWindow', (DialogRef, 'dialog', InMode))
138object.add(f)
139f = Method(SInt16, 'GetDialogDefaultItem', (DialogRef, 'dialog', InMode))
140object.add(f)
141f = Method(SInt16, 'GetDialogCancelItem', (DialogRef, 'dialog', InMode))
142object.add(f)
143f = Method(SInt16, 'GetDialogKeyboardFocusItem', (DialogRef, 'dialog', InMode))
144object.add(f)
145f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode))
146object.add(f)
147
148# generate output
149SetOutputFileName('Dlgmodule.c')
150module.generate()
151