Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 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 | |
| 6 | from macsupport import * |
| 7 | |
| 8 | # Create the type objects |
| 9 | |
| 10 | DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj") |
Jack Jansen | ae8a68f | 1995-06-06 12:55:40 +0000 | [diff] [blame] | 11 | DialogRef = DialogPtr |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 12 | |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 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") |
Jack Jansen | 91a6398 | 1995-08-17 14:30:52 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 17 | ModalFilterProcPtr = InputOnlyType("PyObject*", "O") |
Guido van Rossum | 9784295 | 1995-02-19 15:59:49 +0000 | [diff] [blame] | 18 | ModalFilterProcPtr.passInput = lambda name: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name |
Jack Jansen | ae8a68f | 1995-06-06 12:55:40 +0000 | [diff] [blame] | 19 | ModalFilterUPP = ModalFilterProcPtr |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 20 | |
Jack Jansen | 2b72417 | 1996-04-10 14:48:19 +0000 | [diff] [blame] | 21 | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
Jack Jansen | f7d5aa6 | 2000-12-10 23:43:49 +0000 | [diff] [blame] | 22 | TEHandle = OpaqueByValueType("TEHandle", "ResObj") |
| 23 | CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 24 | |
| 25 | DITLMethod = Type("DITLMethod", "h") |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 26 | DialogItemIndex = Type("DialogItemIndex", "h") |
| 27 | DialogItemType = Type("DialogItemType", "h") |
| 28 | DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h") |
| 29 | AlertType = Type("AlertType", "h") |
| 30 | StringPtr = Str255 |
Jack Jansen | 0b13e7c | 2000-07-07 13:09:35 +0000 | [diff] [blame] | 31 | EventMask = Type("EventMask", "H") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 32 | |
| 33 | includestuff = includestuff + """ |
| 34 | #include <Dialogs.h> |
| 35 | |
Jack Jansen | f7d5aa6 | 2000-12-10 23:43:49 +0000 | [diff] [blame] | 36 | #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 Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 43 | /* XXX Shouldn't this be a stack? */ |
| 44 | static PyObject *Dlg_FilterProc_callback = NULL; |
| 45 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 46 | static 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 Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 56 | args = Py_BuildValue("O&O&", DlgObj_WhichDialog, dialog, PyMac_BuildEventRecord, event); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 57 | if (args == NULL) |
| 58 | res = NULL; |
| 59 | else { |
| 60 | res = PyEval_CallObject(callback, args); |
| 61 | Py_DECREF(args); |
| 62 | } |
| 63 | if (res == NULL) { |
Jack Jansen | deff89c | 1998-10-12 20:53:15 +0000 | [diff] [blame] | 64 | PySys_WriteStderr("Exception in Dialog Filter\\n"); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 65 | 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 | |
| 82 | static ModalFilterProcPtr |
| 83 | Dlg_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 Jansen | df901df | 1998-07-10 15:47:48 +0000 | [diff] [blame] | 97 | static PyObject *Dlg_UserItemProc_callback = NULL; |
| 98 | |
| 99 | static 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 Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 107 | args = Py_BuildValue("O&h", DlgObj_WhichDialog, dialog, item); |
Jack Jansen | df901df | 1998-07-10 15:47:48 +0000 | [diff] [blame] | 108 | 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 Jansen | deff89c | 1998-10-12 20:53:15 +0000 | [diff] [blame] | 115 | PySys_WriteStderr("Exception in Dialog UserItem proc\\n"); |
Jack Jansen | df901df | 1998-07-10 15:47:48 +0000 | [diff] [blame] | 116 | PyErr_Print(); |
| 117 | } |
| 118 | Py_XDECREF(res); |
| 119 | return; |
| 120 | } |
| 121 | |
Jack Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 122 | #if 0 |
Jack Jansen | b8c4c7b | 2000-08-25 22:25:54 +0000 | [diff] [blame] | 123 | /* |
| 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 Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 132 | extern PyMethodChain WinObj_chain; |
Jack Jansen | b8c4c7b | 2000-08-25 22:25:54 +0000 | [diff] [blame] | 133 | #endif |
| 134 | """ |
| 135 | |
| 136 | finalstuff = finalstuff + """ |
| 137 | /* Return the WindowPtr corresponding to a DialogObject */ |
| 138 | |
| 139 | WindowPtr |
| 140 | DlgObj_ConvertToWindow(self) |
| 141 | PyObject *self; |
| 142 | { |
| 143 | if ( DlgObj_Check(self) ) |
| 144 | return GetDialogWindow(((DialogObject *)self)->ob_itself); |
| 145 | return NULL; |
| 146 | } |
Jack Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 147 | /* Return the object corresponding to the dialog, or None */ |
| 148 | |
| 149 | PyObject * |
| 150 | DlgObj_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 Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 177 | """ |
| 178 | |
| 179 | |
| 180 | # Define a class which specializes our object definition |
| 181 | class MyObjectDefinition(GlobalObjectDefinition): |
| 182 | def __init__(self, name, prefix = None, itselftype = None): |
| 183 | GlobalObjectDefinition.__init__(self, name, prefix, itselftype) |
Jack Jansen | b8c4c7b | 2000-08-25 22:25:54 +0000 | [diff] [blame] | 184 | ## This won't work in Carbon, so we disable it for all MacPythons:-( |
| 185 | ## But see the comment above:-(( |
Jack Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 186 | ## self.basechain = "&WinObj_chain" |
| 187 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 188 | def outputInitStructMembers(self): |
| 189 | GlobalObjectDefinition.outputInitStructMembers(self) |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 190 | Output("SetWRefCon(GetDialogWindow(itself), (long)it);") |
Jack Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 191 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 192 | def outputCheckNewArg(self): |
| 193 | Output("if (itself == NULL) return Py_None;") |
Jack Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 194 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 195 | 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 Jansen | 69e7f11 | 2001-02-06 16:14:54 +0000 | [diff] [blame^] | 199 | |
| 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 Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 222 | def outputFreeIt(self, itselfname): |
| 223 | Output("DisposeDialog(%s);", itselfname) |
| 224 | |
| 225 | # Create the generator groups and link them |
| 226 | module = MacModule('Dlg', 'Dlg', includestuff, finalstuff, initstuff) |
| 227 | object = MyObjectDefinition('Dialog', 'DlgObj', 'DialogPtr') |
| 228 | module.addobject(object) |
| 229 | |
| 230 | # Create the generator classes used to populate the lists |
| 231 | Function = OSErrFunctionGenerator |
| 232 | Method = OSErrMethodGenerator |
| 233 | |
| 234 | # Create and populate the lists |
| 235 | functions = [] |
| 236 | methods = [] |
| 237 | execfile("dlggen.py") |
| 238 | |
| 239 | # add the populated lists to the generator groups |
| 240 | for f in functions: module.add(f) |
| 241 | for f in methods: object.add(f) |
| 242 | |
Jack Jansen | d96cb50 | 1996-09-23 15:48:46 +0000 | [diff] [blame] | 243 | # Some methods that are currently macro's in C, but will be real routines |
| 244 | # in MacOS 8. |
| 245 | |
Jack Jansen | f7d5aa6 | 2000-12-10 23:43:49 +0000 | [diff] [blame] | 246 | ##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 Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 254 | f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode), |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 255 | condition='#if !TARGET_API_MAC_CARBON') |
Jack Jansen | d96cb50 | 1996-09-23 15:48:46 +0000 | [diff] [blame] | 256 | object.add(f) |
| 257 | |
Jack Jansen | df901df | 1998-07-10 15:47:48 +0000 | [diff] [blame] | 258 | setuseritembody = """ |
| 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 | """ |
| 282 | f = ManualGenerator("SetUserItemHandler", setuseritembody) |
| 283 | module.add(f) |
| 284 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 285 | # generate output |
| 286 | SetOutputFileName('Dlgmodule.c') |
| 287 | module.generate() |