_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / Mac / Modules / dlg / dlgsupport.py
blob1b561d97b76fa0d0a21b9a64f86ce26be19045c2
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: "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")
30 StringPtr = Str255
31 EventMask = Type("EventMask", "H")
33 includestuff = includestuff + """
34 #ifdef WITHOUT_FRAMEWORKS
35 #include <Dialogs.h>
36 #else
37 #include <Carbon/Carbon.h>
38 #endif
40 #ifdef USE_TOOLBOX_OBJECT_GLUE
41 extern PyObject *_DlgObj_New(DialogRef);
42 extern PyObject *_DlgObj_WhichDialog(DialogRef);
43 extern int _DlgObj_Convert(PyObject *, DialogRef *);
45 #define DlgObj_New _DlgObj_New
46 #define DlgObj_WhichDialog _DlgObj_WhichDialog
47 #define DlgObj_Convert _DlgObj_Convert
48 #endif
50 /* XXX Shouldn't this be a stack? */
51 static PyObject *Dlg_FilterProc_callback = NULL;
53 static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
54 EventRecord *event,
55 short *itemHit)
57 Boolean rv;
58 PyObject *args, *res;
59 PyObject *callback = Dlg_FilterProc_callback;
60 if (callback == NULL)
61 return 0; /* Default behavior */
62 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
63 args = Py_BuildValue("O&O&", DlgObj_WhichDialog, dialog, PyMac_BuildEventRecord, event);
64 if (args == NULL)
65 res = NULL;
66 else {
67 res = PyEval_CallObject(callback, args);
68 Py_DECREF(args);
70 if (res == NULL) {
71 PySys_WriteStderr("Exception in Dialog Filter\\n");
72 PyErr_Print();
73 *itemHit = -1; /* Fake return item */
74 return 1; /* We handled it */
76 else {
77 Dlg_FilterProc_callback = callback;
78 if (PyInt_Check(res)) {
79 *itemHit = PyInt_AsLong(res);
80 rv = 1;
82 else
83 rv = PyObject_IsTrue(res);
85 Py_DECREF(res);
86 return rv;
89 static ModalFilterUPP
90 Dlg_PassFilterProc(PyObject *callback)
92 PyObject *tmp = Dlg_FilterProc_callback;
93 static ModalFilterUPP UnivFilterUpp = NULL;
95 Dlg_FilterProc_callback = NULL;
96 if (callback == Py_None) {
97 Py_XDECREF(tmp);
98 return NULL;
100 Py_INCREF(callback);
101 Dlg_FilterProc_callback = callback;
102 Py_XDECREF(tmp);
103 if ( UnivFilterUpp == NULL )
104 UnivFilterUpp = NewModalFilterUPP(&Dlg_UnivFilterProc);
105 return UnivFilterUpp;
108 static PyObject *Dlg_UserItemProc_callback = NULL;
110 static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
111 short item)
113 PyObject *args, *res;
115 if (Dlg_UserItemProc_callback == NULL)
116 return; /* Default behavior */
117 Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
118 args = Py_BuildValue("O&h", DlgObj_WhichDialog, dialog, item);
119 if (args == NULL)
120 res = NULL;
121 else {
122 res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
123 Py_DECREF(args);
125 if (res == NULL) {
126 PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
127 PyErr_Print();
129 Py_XDECREF(res);
130 return;
133 #if 0
135 ** Treating DialogObjects as WindowObjects is (I think) illegal under Carbon.
136 ** However, as they are still identical under MacOS9 Carbon this is a problem, even
137 ** if we neatly call GetDialogWindow() at the right places: there's one refcon field
138 ** and it points to the DialogObject, so WinObj_WhichWindow will smartly return the
139 ** dialog object, and therefore we still don't have a WindowObject.
140 ** I'll leave the chaining code in place for now, with this comment to warn the
141 ** unsuspecting victims (i.e. me, probably, in a few weeks:-)
143 extern PyMethodChain WinObj_chain;
144 #endif
147 finalstuff = finalstuff + """
148 /* Return the WindowPtr corresponding to a DialogObject */
149 #if 0
150 WindowPtr
151 DlgObj_ConvertToWindow(PyObject *self)
153 if ( DlgObj_Check(self) )
154 return GetDialogWindow(((DialogObject *)self)->ob_itself);
155 return NULL;
157 #endif
158 /* Return the object corresponding to the dialog, or None */
160 PyObject *
161 DlgObj_WhichDialog(DialogPtr d)
163 PyObject *it;
165 if (d == NULL) {
166 it = Py_None;
167 Py_INCREF(it);
168 } else {
169 WindowPtr w = GetDialogWindow(d);
171 it = (PyObject *) GetWRefCon(w);
172 if (it == NULL || ((DialogObject *)it)->ob_itself != d || !DlgObj_Check(it)) {
173 #if 0
174 /* Should do this, but we don't have an ob_freeit for dialogs yet. */
175 it = WinObj_New(w);
176 ((WindowObject *)it)->ob_freeit = NULL;
177 #else
178 it = Py_None;
179 Py_INCREF(it);
180 #endif
181 } else {
182 Py_INCREF(it);
185 return it;
189 initstuff = initstuff + """
190 PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_New);
191 PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_WhichDialog);
192 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DialogPtr, DlgObj_Convert);
196 # Define a class which specializes our object definition
197 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
198 def __init__(self, name, prefix = None, itselftype = None):
199 GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
200 ## This won't work in Carbon, so we disable it for all MacPythons:-(
201 ## But see the comment above:-((
202 ## self.basechain = "&WinObj_chain"
204 def outputInitStructMembers(self):
205 GlobalObjectDefinition.outputInitStructMembers(self)
206 Output("SetWRefCon(GetDialogWindow(itself), (long)it);")
208 def outputCheckNewArg(self):
209 Output("if (itself == NULL) return Py_None;")
211 def outputCheckConvertArg(self):
212 Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
213 Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
214 Output(" return 1; }")
216 def outputCompare(self):
217 Output()
218 Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
219 OutLbrace()
220 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
221 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
222 Output("return 0;")
223 OutRbrace()
225 def outputHash(self):
226 Output()
227 Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
228 OutLbrace()
229 Output("return (int)self->ob_itself;")
230 OutRbrace()
232 def outputFreeIt(self, itselfname):
233 Output("DisposeDialog(%s);", itselfname)
235 # Create the generator groups and link them
236 module = MacModule('_Dlg', 'Dlg', includestuff, finalstuff, initstuff)
237 object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr')
238 module.addobject(object)
240 # Create the generator classes used to populate the lists
241 Function = OSErrWeakLinkFunctionGenerator
242 Method = OSErrWeakLinkMethodGenerator
244 # Create and populate the lists
245 functions = []
246 methods = []
247 execfile("dlggen.py")
249 # add the populated lists to the generator groups
250 for f in functions: module.add(f)
251 for f in methods: object.add(f)
253 setuseritembody = """
254 PyObject *new = NULL;
257 if (!PyArg_ParseTuple(_args, "|O", &new))
258 return NULL;
260 if (Dlg_UserItemProc_callback && new && new != Py_None) {
261 PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
262 return NULL;
265 if (new == NULL || new == Py_None) {
266 new = NULL;
267 _res = Py_None;
268 Py_INCREF(Py_None);
269 } else {
270 Py_INCREF(new);
271 _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemUPP(Dlg_UnivUserItemProc));
274 Dlg_UserItemProc_callback = new;
275 return _res;
277 f = ManualGenerator("SetUserItemHandler", setuseritembody)
278 module.add(f)
280 # generate output
281 SetOutputFileName('_Dlgmodule.c')
282 module.generate()