| 1 | n/a | # This script generates the Dialogs interface for Python. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # It execs the file dlggen.py which contain the function definitions |
|---|
| 4 | n/a | # (dlggen.py was generated by dlgscan.py, scanning the <Dialogs.h> header file). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | from macsupport import * |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Create the type objects |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj") |
|---|
| 11 | n/a | DialogRef = DialogPtr |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # An OptHandle is either a handle or None (in case NULL is passed in). |
|---|
| 14 | n/a | # This is needed for GetDialogItem(). |
|---|
| 15 | n/a | OptHandle = OpaqueByValueType("Handle", "OptResObj") |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | ModalFilterProcPtr = InputOnlyType("PyObject*", "O") |
|---|
| 18 | n/a | ModalFilterProcPtr.passInput = lambda name: "Dlg_PassFilterProc(%s)" % name |
|---|
| 19 | n/a | ModalFilterUPP = ModalFilterProcPtr |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|---|
| 22 | n/a | TEHandle = OpaqueByValueType("TEHandle", "ResObj") |
|---|
| 23 | n/a | CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | DITLMethod = Type("DITLMethod", "h") |
|---|
| 26 | n/a | DialogItemIndex = Type("DialogItemIndex", "h") |
|---|
| 27 | n/a | DialogItemType = Type("DialogItemType", "h") |
|---|
| 28 | n/a | DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h") |
|---|
| 29 | n/a | AlertType = Type("AlertType", "h") |
|---|
| 30 | n/a | StringPtr = Str255 |
|---|
| 31 | n/a | EventMask = Type("EventMask", "H") |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | includestuff = includestuff + """ |
|---|
| 34 | n/a | #include <Carbon/Carbon.h> |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 37 | n/a | extern PyObject *_DlgObj_New(DialogRef); |
|---|
| 38 | n/a | extern PyObject *_DlgObj_WhichDialog(DialogRef); |
|---|
| 39 | n/a | extern int _DlgObj_Convert(PyObject *, DialogRef *); |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | #define DlgObj_New _DlgObj_New |
|---|
| 42 | n/a | #define DlgObj_WhichDialog _DlgObj_WhichDialog |
|---|
| 43 | n/a | #define DlgObj_Convert _DlgObj_Convert |
|---|
| 44 | n/a | #endif |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | /* XXX Shouldn't this be a stack? */ |
|---|
| 47 | n/a | static PyObject *Dlg_FilterProc_callback = NULL; |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog, |
|---|
| 50 | n/a | EventRecord *event, |
|---|
| 51 | n/a | short *itemHit) |
|---|
| 52 | n/a | { |
|---|
| 53 | n/a | Boolean rv; |
|---|
| 54 | n/a | PyObject *args, *res; |
|---|
| 55 | n/a | PyObject *callback = Dlg_FilterProc_callback; |
|---|
| 56 | n/a | if (callback == NULL) |
|---|
| 57 | n/a | return 0; /* Default behavior */ |
|---|
| 58 | n/a | Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */ |
|---|
| 59 | n/a | args = Py_BuildValue("O&O&", DlgObj_WhichDialog, dialog, PyMac_BuildEventRecord, event); |
|---|
| 60 | n/a | if (args == NULL) |
|---|
| 61 | n/a | res = NULL; |
|---|
| 62 | n/a | else { |
|---|
| 63 | n/a | res = PyEval_CallObject(callback, args); |
|---|
| 64 | n/a | Py_DECREF(args); |
|---|
| 65 | n/a | } |
|---|
| 66 | n/a | if (res == NULL) { |
|---|
| 67 | n/a | PySys_WriteStderr("Exception in Dialog Filter\\n"); |
|---|
| 68 | n/a | PyErr_Print(); |
|---|
| 69 | n/a | *itemHit = -1; /* Fake return item */ |
|---|
| 70 | n/a | return 1; /* We handled it */ |
|---|
| 71 | n/a | } |
|---|
| 72 | n/a | else { |
|---|
| 73 | n/a | Dlg_FilterProc_callback = callback; |
|---|
| 74 | n/a | if (PyInt_Check(res)) { |
|---|
| 75 | n/a | *itemHit = PyInt_AsLong(res); |
|---|
| 76 | n/a | rv = 1; |
|---|
| 77 | n/a | } |
|---|
| 78 | n/a | else |
|---|
| 79 | n/a | rv = PyObject_IsTrue(res); |
|---|
| 80 | n/a | } |
|---|
| 81 | n/a | Py_DECREF(res); |
|---|
| 82 | n/a | return rv; |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | static ModalFilterUPP |
|---|
| 86 | n/a | Dlg_PassFilterProc(PyObject *callback) |
|---|
| 87 | n/a | { |
|---|
| 88 | n/a | PyObject *tmp = Dlg_FilterProc_callback; |
|---|
| 89 | n/a | static ModalFilterUPP UnivFilterUpp = NULL; |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | Dlg_FilterProc_callback = NULL; |
|---|
| 92 | n/a | if (callback == Py_None) { |
|---|
| 93 | n/a | Py_XDECREF(tmp); |
|---|
| 94 | n/a | return NULL; |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | Py_INCREF(callback); |
|---|
| 97 | n/a | Dlg_FilterProc_callback = callback; |
|---|
| 98 | n/a | Py_XDECREF(tmp); |
|---|
| 99 | n/a | if ( UnivFilterUpp == NULL ) |
|---|
| 100 | n/a | UnivFilterUpp = NewModalFilterUPP(&Dlg_UnivFilterProc); |
|---|
| 101 | n/a | return UnivFilterUpp; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | static PyObject *Dlg_UserItemProc_callback = NULL; |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | static pascal void Dlg_UnivUserItemProc(DialogPtr dialog, |
|---|
| 107 | n/a | short item) |
|---|
| 108 | n/a | { |
|---|
| 109 | n/a | PyObject *args, *res; |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | if (Dlg_UserItemProc_callback == NULL) |
|---|
| 112 | n/a | return; /* Default behavior */ |
|---|
| 113 | n/a | Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */ |
|---|
| 114 | n/a | args = Py_BuildValue("O&h", DlgObj_WhichDialog, dialog, item); |
|---|
| 115 | n/a | if (args == NULL) |
|---|
| 116 | n/a | res = NULL; |
|---|
| 117 | n/a | else { |
|---|
| 118 | n/a | res = PyEval_CallObject(Dlg_UserItemProc_callback, args); |
|---|
| 119 | n/a | Py_DECREF(args); |
|---|
| 120 | n/a | } |
|---|
| 121 | n/a | if (res == NULL) { |
|---|
| 122 | n/a | PySys_WriteStderr("Exception in Dialog UserItem proc\\n"); |
|---|
| 123 | n/a | PyErr_Print(); |
|---|
| 124 | n/a | } |
|---|
| 125 | n/a | Py_XDECREF(res); |
|---|
| 126 | n/a | return; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | #if 0 |
|---|
| 130 | n/a | /* |
|---|
| 131 | n/a | ** Treating DialogObjects as WindowObjects is (I think) illegal under Carbon. |
|---|
| 132 | n/a | ** However, as they are still identical under MacOS9 Carbon this is a problem, even |
|---|
| 133 | n/a | ** if we neatly call GetDialogWindow() at the right places: there's one refcon field |
|---|
| 134 | n/a | ** and it points to the DialogObject, so WinObj_WhichWindow will smartly return the |
|---|
| 135 | n/a | ** dialog object, and therefore we still don't have a WindowObject. |
|---|
| 136 | n/a | ** I'll leave the chaining code in place for now, with this comment to warn the |
|---|
| 137 | n/a | ** unsuspecting victims (i.e. me, probably, in a few weeks:-) |
|---|
| 138 | n/a | */ |
|---|
| 139 | n/a | extern PyMethodChain WinObj_chain; |
|---|
| 140 | n/a | #endif |
|---|
| 141 | n/a | """ |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | finalstuff = finalstuff + """ |
|---|
| 144 | n/a | /* Return the WindowPtr corresponding to a DialogObject */ |
|---|
| 145 | n/a | #if 0 |
|---|
| 146 | n/a | WindowPtr |
|---|
| 147 | n/a | DlgObj_ConvertToWindow(PyObject *self) |
|---|
| 148 | n/a | { |
|---|
| 149 | n/a | if ( DlgObj_Check(self) ) |
|---|
| 150 | n/a | return GetDialogWindow(((DialogObject *)self)->ob_itself); |
|---|
| 151 | n/a | return NULL; |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | #endif |
|---|
| 154 | n/a | /* Return the object corresponding to the dialog, or None */ |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | PyObject * |
|---|
| 157 | n/a | DlgObj_WhichDialog(DialogPtr d) |
|---|
| 158 | n/a | { |
|---|
| 159 | n/a | PyObject *it; |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | if (d == NULL) { |
|---|
| 162 | n/a | it = Py_None; |
|---|
| 163 | n/a | Py_INCREF(it); |
|---|
| 164 | n/a | } else { |
|---|
| 165 | n/a | WindowPtr w = GetDialogWindow(d); |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | it = (PyObject *) GetWRefCon(w); |
|---|
| 168 | n/a | if (it == NULL || ((DialogObject *)it)->ob_itself != d || !DlgObj_Check(it)) { |
|---|
| 169 | n/a | #if 0 |
|---|
| 170 | n/a | /* Should do this, but we don't have an ob_freeit for dialogs yet. */ |
|---|
| 171 | n/a | it = WinObj_New(w); |
|---|
| 172 | n/a | ((WindowObject *)it)->ob_freeit = NULL; |
|---|
| 173 | n/a | #else |
|---|
| 174 | n/a | it = Py_None; |
|---|
| 175 | n/a | Py_INCREF(it); |
|---|
| 176 | n/a | #endif |
|---|
| 177 | n/a | } else { |
|---|
| 178 | n/a | Py_INCREF(it); |
|---|
| 179 | n/a | } |
|---|
| 180 | n/a | } |
|---|
| 181 | n/a | return it; |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | """ |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | initstuff = initstuff + """ |
|---|
| 186 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_New); |
|---|
| 187 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(DialogPtr, DlgObj_WhichDialog); |
|---|
| 188 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DialogPtr, DlgObj_Convert); |
|---|
| 189 | n/a | """ |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | # Define a class which specializes our object definition |
|---|
| 193 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 194 | n/a | def __init__(self, name, prefix = None, itselftype = None): |
|---|
| 195 | n/a | GlobalObjectDefinition.__init__(self, name, prefix, itselftype) |
|---|
| 196 | n/a | ## This won't work in Carbon, so we disable it for all MacPythons:-( |
|---|
| 197 | n/a | ## But see the comment above:-(( |
|---|
| 198 | n/a | ## self.basechain = "&WinObj_chain" |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | def outputInitStructMembers(self): |
|---|
| 201 | n/a | GlobalObjectDefinition.outputInitStructMembers(self) |
|---|
| 202 | n/a | Output("SetWRefCon(GetDialogWindow(itself), (long)it);") |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | def outputCheckNewArg(self): |
|---|
| 205 | n/a | Output("if (itself == NULL) { Py_INCREF(Py_None); return Py_None; }") |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def outputCheckConvertArg(self): |
|---|
| 208 | n/a | Output("if (v == Py_None) { *p_itself = NULL; return 1; }") |
|---|
| 209 | n/a | Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);") |
|---|
| 210 | n/a | Output(" return 1; }") |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | def outputCompare(self): |
|---|
| 213 | n/a | Output() |
|---|
| 214 | n/a | Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype) |
|---|
| 215 | n/a | OutLbrace() |
|---|
| 216 | n/a | Output("if ( self->ob_itself > other->ob_itself ) return 1;") |
|---|
| 217 | n/a | Output("if ( self->ob_itself < other->ob_itself ) return -1;") |
|---|
| 218 | n/a | Output("return 0;") |
|---|
| 219 | n/a | OutRbrace() |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def outputHash(self): |
|---|
| 222 | n/a | Output() |
|---|
| 223 | n/a | Output("static int %s_hash(%s *self)", self.prefix, self.objecttype) |
|---|
| 224 | n/a | OutLbrace() |
|---|
| 225 | n/a | Output("return (int)self->ob_itself;") |
|---|
| 226 | n/a | OutRbrace() |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def outputFreeIt(self, itselfname): |
|---|
| 229 | n/a | Output("DisposeDialog(%s);", itselfname) |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | # Create the generator groups and link them |
|---|
| 232 | n/a | module = MacModule('_Dlg', 'Dlg', includestuff, finalstuff, initstuff) |
|---|
| 233 | n/a | object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr') |
|---|
| 234 | n/a | module.addobject(object) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | # Create the generator classes used to populate the lists |
|---|
| 237 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 238 | n/a | Method = OSErrWeakLinkMethodGenerator |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | # Create and populate the lists |
|---|
| 241 | n/a | functions = [] |
|---|
| 242 | n/a | methods = [] |
|---|
| 243 | n/a | execfile("dlggen.py") |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | # add the populated lists to the generator groups |
|---|
| 246 | n/a | for f in functions: module.add(f) |
|---|
| 247 | n/a | for f in methods: object.add(f) |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | setuseritembody = """ |
|---|
| 250 | n/a | PyObject *new = NULL; |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | if (!PyArg_ParseTuple(_args, "|O", &new)) |
|---|
| 254 | n/a | return NULL; |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | if (Dlg_UserItemProc_callback && new && new != Py_None) { |
|---|
| 257 | n/a | PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed"); |
|---|
| 258 | n/a | return NULL; |
|---|
| 259 | n/a | } |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | if (new == NULL || new == Py_None) { |
|---|
| 262 | n/a | new = NULL; |
|---|
| 263 | n/a | _res = Py_None; |
|---|
| 264 | n/a | Py_INCREF(Py_None); |
|---|
| 265 | n/a | } else { |
|---|
| 266 | n/a | Py_INCREF(new); |
|---|
| 267 | n/a | _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemUPP(Dlg_UnivUserItemProc)); |
|---|
| 268 | n/a | } |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | Dlg_UserItemProc_callback = new; |
|---|
| 271 | n/a | return _res; |
|---|
| 272 | n/a | """ |
|---|
| 273 | n/a | f = ManualGenerator("SetUserItemHandler", setuseritembody) |
|---|
| 274 | n/a | module.add(f) |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | # generate output |
|---|
| 277 | n/a | SetOutputFileName('_Dlgmodule.c') |
|---|
| 278 | n/a | module.generate() |
|---|