Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Mac / Modules / dlg / dlgsupport.py
blob4c447229d9665fdedc249b9ceaf0ce0fa100d431
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")
11 DialogRef = DialogPtr
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: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name
19 ModalFilterUPP = ModalFilterProcPtr
21 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
23 DITLMethod = Type("DITLMethod", "h")
24 DialogItemIndex = Type("DialogItemIndex", "h")
25 DialogItemType = Type("DialogItemType", "h")
26 DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h")
27 AlertType = Type("AlertType", "h")
28 StringPtr = Str255
29 EventMask = Type("EventMask", "h")
31 includestuff = includestuff + """
32 #include <Dialogs.h>
34 #ifndef HAVE_UNIVERSAL_HEADERS
35 #define NewModalFilterProc(x) (x)
36 #endif
38 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
40 /* XXX Shouldn't this be a stack? */
41 static PyObject *Dlg_FilterProc_callback = NULL;
43 static PyObject *DlgObj_New(DialogPtr); /* Forward */
45 static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
46 EventRecord *event,
47 short *itemHit)
49 Boolean rv;
50 PyObject *args, *res;
51 PyObject *callback = Dlg_FilterProc_callback;
52 if (callback == NULL)
53 return 0; /* Default behavior */
54 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
55 args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
56 if (args == NULL)
57 res = NULL;
58 else {
59 res = PyEval_CallObject(callback, args);
60 Py_DECREF(args);
62 if (res == NULL) {
63 PySys_WriteStderr("Exception in Dialog Filter\\n");
64 PyErr_Print();
65 *itemHit = -1; /* Fake return item */
66 return 1; /* We handled it */
68 else {
69 Dlg_FilterProc_callback = callback;
70 if (PyInt_Check(res)) {
71 *itemHit = PyInt_AsLong(res);
72 rv = 1;
74 else
75 rv = PyObject_IsTrue(res);
77 Py_DECREF(res);
78 return rv;
81 static ModalFilterProcPtr
82 Dlg_PassFilterProc(PyObject *callback)
84 PyObject *tmp = Dlg_FilterProc_callback;
85 Dlg_FilterProc_callback = NULL;
86 if (callback == Py_None) {
87 Py_XDECREF(tmp);
88 return NULL;
90 Py_INCREF(callback);
91 Dlg_FilterProc_callback = callback;
92 Py_XDECREF(tmp);
93 return &Dlg_UnivFilterProc;
96 static PyObject *Dlg_UserItemProc_callback = NULL;
98 static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
99 short item)
101 PyObject *args, *res;
103 if (Dlg_UserItemProc_callback == NULL)
104 return; /* Default behavior */
105 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
106 args = Py_BuildValue("O&h", WinObj_WhichWindow, dialog, item);
107 if (args == NULL)
108 res = NULL;
109 else {
110 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
111 Py_DECREF(args);
113 if (res == NULL) {
114 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
115 PyErr_Print();
117 Py_XDECREF(res);
118 return;
121 extern PyMethodChain WinObj_chain;
125 # Define a class which specializes our object definition
126 class MyObjectDefinition(GlobalObjectDefinition):
127 def __init__(self, name, prefix = None, itselftype = None):
128 GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
129 self.basechain = "&WinObj_chain"
130 def outputInitStructMembers(self):
131 GlobalObjectDefinition.outputInitStructMembers(self)
132 Output("SetWRefCon(itself, (long)it);")
133 def outputCheckNewArg(self):
134 Output("if (itself == NULL) return Py_None;")
135 def outputCheckConvertArg(self):
136 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
137 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
138 Output(" return 1; }")
139 def outputFreeIt(self, itselfname):
140 Output("DisposeDialog(%s);", itselfname)
142 # Create the generator groups and link them
143 module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff)
144 object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
145 module.addobject(object)
147 # Create the generator classes used to populate the lists
148 Function = OSErrFunctionGenerator
149 Method = OSErrMethodGenerator
151 # Create and populate the lists
152 functions = []
153 methods = []
154 execfile("dlggen.py")
156 # add the populated lists to the generator groups
157 for f in functions: module.add(f)
158 for f in methods: object.add(f)
160 # Some methods that are currently macro's in C, but will be real routines
161 # in MacOS 8.
163 f = Method(ExistingDialogPtr, 'GetDialogWindow', (DialogRef, 'dialog', InMode))
164 object.add(f)
165 f = Method(SInt16, 'GetDialogDefaultItem', (DialogRef, 'dialog', InMode))
166 object.add(f)
167 f = Method(SInt16, 'GetDialogCancelItem', (DialogRef, 'dialog', InMode))
168 object.add(f)
169 f = Method(SInt16, 'GetDialogKeyboardFocusItem', (DialogRef, 'dialog', InMode))
170 object.add(f)
171 f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode))
172 object.add(f)
174 setuseritembody = """
175 PyObject *new = NULL;
178 if (!PyArg_ParseTuple(_args, "|O", &new))
179 return NULL;
181 if (Dlg_UserItemProc_callback && new && new != Py_None) {
182 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
183 return NULL;
186 if (new == Py_None) {
187 new = NULL;
188 _res = Py_None;
189 Py_INCREF(Py_None);
190 } else {
191 Py_INCREF(new);
192 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemProc(Dlg_UnivUserItemProc));
195 Dlg_UserItemProc_callback = new;
196 return _res;
198 f = ManualGenerator("SetUserItemHandler", setuseritembody)
199 module.add(f)
201 # generate output
202 SetOutputFileName('Dlgmodule.c')
203 module.generate()