|
| 1 | + |
| 2 | +/* ======================== Module _IBCarbon ======================== */ |
| 3 | + |
| 4 | +#include "Python.h" |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +#ifdef WITHOUT_FRAMEWORKS |
| 9 | +#include <IBCarbonRuntime.h> |
| 10 | +#else |
| 11 | +#include <Carbon/Carbon.h> |
| 12 | +#endif /* WITHOUT_FRAMEWORKS */ |
| 13 | +#include "macglue.h" |
| 14 | + |
| 15 | +#ifdef USE_TOOLBOX_OBJECT_GLUE |
| 16 | +extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *); |
| 17 | +//#define CFStringRefObj_Convert _CFStringRefObj_Convert |
| 18 | +#endif |
| 19 | + |
| 20 | +//extern int CFBundleRefObj_Convert(PyObject *, CFBundleRef *); // need to wrap CFBundle |
| 21 | + |
| 22 | + |
| 23 | +static PyObject *IBCarbon_Error; |
| 24 | + |
| 25 | +/* ---------------------- Object type IBNibRef ---------------------- */ |
| 26 | + |
| 27 | +PyTypeObject IBNibRef_Type; |
| 28 | + |
| 29 | +#define IBNibRefObj_Check(x) ((x)->ob_type == &IBNibRef_Type) |
| 30 | + |
| 31 | +typedef struct IBNibRefObject { |
| 32 | + PyObject_HEAD |
| 33 | + IBNibRef ob_itself; |
| 34 | +} IBNibRefObject; |
| 35 | + |
| 36 | +PyObject *IBNibRefObj_New(IBNibRef itself) |
| 37 | +{ |
| 38 | + IBNibRefObject *it; |
| 39 | + it = PyObject_NEW(IBNibRefObject, &IBNibRef_Type); |
| 40 | + if (it == NULL) return NULL; |
| 41 | + it->ob_itself = itself; |
| 42 | + return (PyObject *)it; |
| 43 | +} |
| 44 | +int IBNibRefObj_Convert(PyObject *v, IBNibRef *p_itself) |
| 45 | +{ |
| 46 | + if (!IBNibRefObj_Check(v)) |
| 47 | + { |
| 48 | + PyErr_SetString(PyExc_TypeError, "IBNibRef required"); |
| 49 | + return 0; |
| 50 | + } |
| 51 | + *p_itself = ((IBNibRefObject *)v)->ob_itself; |
| 52 | + return 1; |
| 53 | +} |
| 54 | + |
| 55 | +static void IBNibRefObj_dealloc(IBNibRefObject *self) |
| 56 | +{ |
| 57 | + DisposeNibReference(self->ob_itself); |
| 58 | + PyMem_DEL(self); |
| 59 | +} |
| 60 | + |
| 61 | +static PyObject *IBNibRefObj_CreateWindowFromNib(IBNibRefObject *_self, PyObject *_args) |
| 62 | +{ |
| 63 | + PyObject *_res = NULL; |
| 64 | + OSStatus _err; |
| 65 | + CFStringRef inName; |
| 66 | + WindowPtr outWindow; |
| 67 | + if (!PyArg_ParseTuple(_args, "O&", |
| 68 | + CFStringRefObj_Convert, &inName)) |
| 69 | + return NULL; |
| 70 | + Py_BEGIN_ALLOW_THREADS |
| 71 | + _err = CreateWindowFromNib(_self->ob_itself, |
| 72 | + inName, |
| 73 | + &outWindow); |
| 74 | + Py_END_ALLOW_THREADS |
| 75 | + if (_err != noErr) return PyMac_Error(_err); |
| 76 | + _res = Py_BuildValue("O&", |
| 77 | + WinObj_New, outWindow); |
| 78 | + return _res; |
| 79 | +} |
| 80 | + |
| 81 | +static PyObject *IBNibRefObj_CreateMenuFromNib(IBNibRefObject *_self, PyObject *_args) |
| 82 | +{ |
| 83 | + PyObject *_res = NULL; |
| 84 | + OSStatus _err; |
| 85 | + CFStringRef inName; |
| 86 | + MenuHandle outMenuRef; |
| 87 | + if (!PyArg_ParseTuple(_args, "O&", |
| 88 | + CFStringRefObj_Convert, &inName)) |
| 89 | + return NULL; |
| 90 | + Py_BEGIN_ALLOW_THREADS |
| 91 | + _err = CreateMenuFromNib(_self->ob_itself, |
| 92 | + inName, |
| 93 | + &outMenuRef); |
| 94 | + Py_END_ALLOW_THREADS |
| 95 | + if (_err != noErr) return PyMac_Error(_err); |
| 96 | + _res = Py_BuildValue("O&", |
| 97 | + MenuObj_New, outMenuRef); |
| 98 | + return _res; |
| 99 | +} |
| 100 | + |
| 101 | +static PyObject *IBNibRefObj_CreateMenuBarFromNib(IBNibRefObject *_self, PyObject *_args) |
| 102 | +{ |
| 103 | + PyObject *_res = NULL; |
| 104 | + OSStatus _err; |
| 105 | + CFStringRef inName; |
| 106 | + Handle outMenuBar; |
| 107 | + if (!PyArg_ParseTuple(_args, "O&", |
| 108 | + CFStringRefObj_Convert, &inName)) |
| 109 | + return NULL; |
| 110 | + Py_BEGIN_ALLOW_THREADS |
| 111 | + _err = CreateMenuBarFromNib(_self->ob_itself, |
| 112 | + inName, |
| 113 | + &outMenuBar); |
| 114 | + Py_END_ALLOW_THREADS |
| 115 | + if (_err != noErr) return PyMac_Error(_err); |
| 116 | + _res = Py_BuildValue("O&", |
| 117 | + ResObj_New, outMenuBar); |
| 118 | + return _res; |
| 119 | +} |
| 120 | + |
| 121 | +static PyObject *IBNibRefObj_SetMenuBarFromNib(IBNibRefObject *_self, PyObject *_args) |
| 122 | +{ |
| 123 | + PyObject *_res = NULL; |
| 124 | + OSStatus _err; |
| 125 | + CFStringRef inName; |
| 126 | + if (!PyArg_ParseTuple(_args, "O&", |
| 127 | + CFStringRefObj_Convert, &inName)) |
| 128 | + return NULL; |
| 129 | + Py_BEGIN_ALLOW_THREADS |
| 130 | + _err = SetMenuBarFromNib(_self->ob_itself, |
| 131 | + inName); |
| 132 | + Py_END_ALLOW_THREADS |
| 133 | + if (_err != noErr) return PyMac_Error(_err); |
| 134 | + Py_INCREF(Py_None); |
| 135 | + _res = Py_None; |
| 136 | + return _res; |
| 137 | +} |
| 138 | + |
| 139 | +static PyMethodDef IBNibRefObj_methods[] = { |
| 140 | + {"CreateWindowFromNib", (PyCFunction)IBNibRefObj_CreateWindowFromNib, 1, |
| 141 | + "(CFStringRef inName) -> (WindowPtr outWindow)"}, |
| 142 | + {"CreateMenuFromNib", (PyCFunction)IBNibRefObj_CreateMenuFromNib, 1, |
| 143 | + "(CFStringRef inName) -> (MenuHandle outMenuRef)"}, |
| 144 | + {"CreateMenuBarFromNib", (PyCFunction)IBNibRefObj_CreateMenuBarFromNib, 1, |
| 145 | + "(CFStringRef inName) -> (Handle outMenuBar)"}, |
| 146 | + {"SetMenuBarFromNib", (PyCFunction)IBNibRefObj_SetMenuBarFromNib, 1, |
| 147 | + "(CFStringRef inName) -> None"}, |
| 148 | + {NULL, NULL, 0} |
| 149 | +}; |
| 150 | + |
| 151 | +PyMethodChain IBNibRefObj_chain = { IBNibRefObj_methods, NULL }; |
| 152 | + |
| 153 | +static PyObject *IBNibRefObj_getattr(IBNibRefObject *self, char *name) |
| 154 | +{ |
| 155 | + return Py_FindMethodInChain(&IBNibRefObj_chain, (PyObject *)self, name); |
| 156 | +} |
| 157 | + |
| 158 | +#define IBNibRefObj_setattr NULL |
| 159 | + |
| 160 | +#define IBNibRefObj_compare NULL |
| 161 | + |
| 162 | +#define IBNibRefObj_repr NULL |
| 163 | + |
| 164 | +#define IBNibRefObj_hash NULL |
| 165 | + |
| 166 | +PyTypeObject IBNibRef_Type = { |
| 167 | + PyObject_HEAD_INIT(NULL) |
| 168 | + 0, /*ob_size*/ |
| 169 | + "_IBCarbon.IBNibRef", /*tp_name*/ |
| 170 | + sizeof(IBNibRefObject), /*tp_basicsize*/ |
| 171 | + 0, /*tp_itemsize*/ |
| 172 | + /* methods */ |
| 173 | + (destructor) IBNibRefObj_dealloc, /*tp_dealloc*/ |
| 174 | + 0, /*tp_print*/ |
| 175 | + (getattrfunc) IBNibRefObj_getattr, /*tp_getattr*/ |
| 176 | + (setattrfunc) IBNibRefObj_setattr, /*tp_setattr*/ |
| 177 | + (cmpfunc) IBNibRefObj_compare, /*tp_compare*/ |
| 178 | + (reprfunc) IBNibRefObj_repr, /*tp_repr*/ |
| 179 | + (PyNumberMethods *)0, /* tp_as_number */ |
| 180 | + (PySequenceMethods *)0, /* tp_as_sequence */ |
| 181 | + (PyMappingMethods *)0, /* tp_as_mapping */ |
| 182 | + (hashfunc) IBNibRefObj_hash, /*tp_hash*/ |
| 183 | +}; |
| 184 | + |
| 185 | +/* -------------------- End object type IBNibRef -------------------- */ |
| 186 | + |
| 187 | + |
| 188 | +static PyObject *IBCarbon_CreateNibReference(PyObject *_self, PyObject *_args) |
| 189 | +{ |
| 190 | + PyObject *_res = NULL; |
| 191 | + OSStatus _err; |
| 192 | + CFStringRef inNibName; |
| 193 | + IBNibRef outNibRef; |
| 194 | + if (!PyArg_ParseTuple(_args, "O&", |
| 195 | + CFStringRefObj_Convert, &inNibName)) |
| 196 | + return NULL; |
| 197 | + Py_BEGIN_ALLOW_THREADS |
| 198 | + _err = CreateNibReference(inNibName, |
| 199 | + &outNibRef); |
| 200 | + Py_END_ALLOW_THREADS |
| 201 | + if (_err != noErr) return PyMac_Error(_err); |
| 202 | + _res = Py_BuildValue("O&", |
| 203 | + IBNibRefObj_New, outNibRef); |
| 204 | + return _res; |
| 205 | +} |
| 206 | + |
| 207 | +static PyMethodDef IBCarbon_methods[] = { |
| 208 | + {"CreateNibReference", (PyCFunction)IBCarbon_CreateNibReference, 1, |
| 209 | + "(CFStringRef inNibName) -> (IBNibRef outNibRef)"}, |
| 210 | + {NULL, NULL, 0} |
| 211 | +}; |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +void init_IBCarbon(void) |
| 217 | +{ |
| 218 | + PyObject *m; |
| 219 | + PyObject *d; |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + m = Py_InitModule("_IBCarbon", IBCarbon_methods); |
| 226 | + d = PyModule_GetDict(m); |
| 227 | + IBCarbon_Error = PyMac_GetOSErrException(); |
| 228 | + if (IBCarbon_Error == NULL || |
| 229 | + PyDict_SetItemString(d, "Error", IBCarbon_Error) != 0) |
| 230 | + return; |
| 231 | + IBNibRef_Type.ob_type = &PyType_Type; |
| 232 | + Py_INCREF(&IBNibRef_Type); |
| 233 | + if (PyDict_SetItemString(d, "IBNibRefType", (PyObject *)&IBNibRef_Type) != 0) |
| 234 | + Py_FatalError("can't initialize IBNibRefType"); |
| 235 | +} |
| 236 | + |
| 237 | +/* ====================== End module _IBCarbon ====================== */ |
| 238 | + |
0 commit comments