| 1 |
|
|---|
| 2 | /* ======================== Module _IBCarbon ======================== */
|
|---|
| 3 |
|
|---|
| 4 | #include "Python.h"
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | #include <Carbon/Carbon.h>
|
|---|
| 9 | #include "pymactoolbox.h"
|
|---|
| 10 |
|
|---|
| 11 | #ifdef USE_TOOLBOX_OBJECT_GLUE
|
|---|
| 12 | extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
|
|---|
| 13 | #endif
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | static PyObject *IBCarbon_Error;
|
|---|
| 17 |
|
|---|
| 18 | /* ---------------------- Object type IBNibRef ---------------------- */
|
|---|
| 19 |
|
|---|
| 20 | PyTypeObject IBNibRef_Type;
|
|---|
| 21 |
|
|---|
| 22 | #define IBNibRefObj_Check(x) ((x)->ob_type == &IBNibRef_Type || PyObject_TypeCheck((x), &IBNibRef_Type))
|
|---|
| 23 |
|
|---|
| 24 | typedef struct IBNibRefObject {
|
|---|
| 25 | PyObject_HEAD
|
|---|
| 26 | IBNibRef ob_itself;
|
|---|
| 27 | } IBNibRefObject;
|
|---|
| 28 |
|
|---|
| 29 | PyObject *IBNibRefObj_New(IBNibRef itself)
|
|---|
| 30 | {
|
|---|
| 31 | IBNibRefObject *it;
|
|---|
| 32 | it = PyObject_NEW(IBNibRefObject, &IBNibRef_Type);
|
|---|
| 33 | if (it == NULL) return NULL;
|
|---|
| 34 | it->ob_itself = itself;
|
|---|
| 35 | return (PyObject *)it;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | int IBNibRefObj_Convert(PyObject *v, IBNibRef *p_itself)
|
|---|
| 39 | {
|
|---|
| 40 | if (!IBNibRefObj_Check(v))
|
|---|
| 41 | {
|
|---|
| 42 | PyErr_SetString(PyExc_TypeError, "IBNibRef required");
|
|---|
| 43 | return 0;
|
|---|
| 44 | }
|
|---|
| 45 | *p_itself = ((IBNibRefObject *)v)->ob_itself;
|
|---|
| 46 | return 1;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | static void IBNibRefObj_dealloc(IBNibRefObject *self)
|
|---|
| 50 | {
|
|---|
| 51 | DisposeNibReference(self->ob_itself);
|
|---|
| 52 | self->ob_type->tp_free((PyObject *)self);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | static PyObject *IBNibRefObj_CreateWindowFromNib(IBNibRefObject *_self, PyObject *_args)
|
|---|
| 56 | {
|
|---|
| 57 | PyObject *_res = NULL;
|
|---|
| 58 | OSStatus _err;
|
|---|
| 59 | CFStringRef inName;
|
|---|
| 60 | WindowPtr outWindow;
|
|---|
| 61 | if (!PyArg_ParseTuple(_args, "O&",
|
|---|
| 62 | CFStringRefObj_Convert, &inName))
|
|---|
| 63 | return NULL;
|
|---|
| 64 | _err = CreateWindowFromNib(_self->ob_itself,
|
|---|
| 65 | inName,
|
|---|
| 66 | &outWindow);
|
|---|
| 67 | if (_err != noErr) return PyMac_Error(_err);
|
|---|
| 68 | _res = Py_BuildValue("O&",
|
|---|
| 69 | WinObj_New, outWindow);
|
|---|
| 70 | return _res;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static PyObject *IBNibRefObj_CreateMenuFromNib(IBNibRefObject *_self, PyObject *_args)
|
|---|
| 74 | {
|
|---|
| 75 | PyObject *_res = NULL;
|
|---|
| 76 | OSStatus _err;
|
|---|
| 77 | CFStringRef inName;
|
|---|
| 78 | MenuHandle outMenuRef;
|
|---|
| 79 | if (!PyArg_ParseTuple(_args, "O&",
|
|---|
| 80 | CFStringRefObj_Convert, &inName))
|
|---|
| 81 | return NULL;
|
|---|
| 82 | _err = CreateMenuFromNib(_self->ob_itself,
|
|---|
| 83 | inName,
|
|---|
| 84 | &outMenuRef);
|
|---|
| 85 | if (_err != noErr) return PyMac_Error(_err);
|
|---|
| 86 | _res = Py_BuildValue("O&",
|
|---|
| 87 | MenuObj_New, outMenuRef);
|
|---|
| 88 | return _res;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static PyObject *IBNibRefObj_CreateMenuBarFromNib(IBNibRefObject *_self, PyObject *_args)
|
|---|
| 92 | {
|
|---|
| 93 | PyObject *_res = NULL;
|
|---|
| 94 | OSStatus _err;
|
|---|
| 95 | CFStringRef inName;
|
|---|
| 96 | Handle outMenuBar;
|
|---|
| 97 | if (!PyArg_ParseTuple(_args, "O&",
|
|---|
| 98 | CFStringRefObj_Convert, &inName))
|
|---|
| 99 | return NULL;
|
|---|
| 100 | _err = CreateMenuBarFromNib(_self->ob_itself,
|
|---|
| 101 | inName,
|
|---|
| 102 | &outMenuBar);
|
|---|
| 103 | if (_err != noErr) return PyMac_Error(_err);
|
|---|
| 104 | _res = Py_BuildValue("O&",
|
|---|
| 105 | ResObj_New, outMenuBar);
|
|---|
| 106 | return _res;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static PyObject *IBNibRefObj_SetMenuBarFromNib(IBNibRefObject *_self, PyObject *_args)
|
|---|
| 110 | {
|
|---|
| 111 | PyObject *_res = NULL;
|
|---|
| 112 | OSStatus _err;
|
|---|
| 113 | CFStringRef inName;
|
|---|
| 114 | if (!PyArg_ParseTuple(_args, "O&",
|
|---|
| 115 | CFStringRefObj_Convert, &inName))
|
|---|
| 116 | return NULL;
|
|---|
| 117 | _err = SetMenuBarFromNib(_self->ob_itself,
|
|---|
| 118 | inName);
|
|---|
| 119 | if (_err != noErr) return PyMac_Error(_err);
|
|---|
| 120 | Py_INCREF(Py_None);
|
|---|
| 121 | _res = Py_None;
|
|---|
| 122 | return _res;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | static PyMethodDef IBNibRefObj_methods[] = {
|
|---|
| 126 | {"CreateWindowFromNib", (PyCFunction)IBNibRefObj_CreateWindowFromNib, 1,
|
|---|
| 127 | PyDoc_STR("(CFStringRef inName) -> (WindowPtr outWindow)")},
|
|---|
| 128 | {"CreateMenuFromNib", (PyCFunction)IBNibRefObj_CreateMenuFromNib, 1,
|
|---|
| 129 | PyDoc_STR("(CFStringRef inName) -> (MenuHandle outMenuRef)")},
|
|---|
| 130 | {"CreateMenuBarFromNib", (PyCFunction)IBNibRefObj_CreateMenuBarFromNib, 1,
|
|---|
| 131 | PyDoc_STR("(CFStringRef inName) -> (Handle outMenuBar)")},
|
|---|
| 132 | {"SetMenuBarFromNib", (PyCFunction)IBNibRefObj_SetMenuBarFromNib, 1,
|
|---|
| 133 | PyDoc_STR("(CFStringRef inName) -> None")},
|
|---|
| 134 | {NULL, NULL, 0}
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 | #define IBNibRefObj_getsetlist NULL
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | #define IBNibRefObj_compare NULL
|
|---|
| 141 |
|
|---|
| 142 | #define IBNibRefObj_repr NULL
|
|---|
| 143 |
|
|---|
| 144 | #define IBNibRefObj_hash NULL
|
|---|
| 145 | #define IBNibRefObj_tp_init 0
|
|---|
| 146 |
|
|---|
| 147 | #define IBNibRefObj_tp_alloc PyType_GenericAlloc
|
|---|
| 148 |
|
|---|
| 149 | static PyObject *IBNibRefObj_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)
|
|---|
| 150 | {
|
|---|
| 151 | PyObject *_self;
|
|---|
| 152 | IBNibRef itself;
|
|---|
| 153 | char *kw[] = {"itself", 0};
|
|---|
| 154 |
|
|---|
| 155 | if (!PyArg_ParseTupleAndKeywords(_args, _kwds, "O&", kw, IBNibRefObj_Convert, &itself)) return NULL;
|
|---|
| 156 | if ((_self = type->tp_alloc(type, 0)) == NULL) return NULL;
|
|---|
| 157 | ((IBNibRefObject *)_self)->ob_itself = itself;
|
|---|
| 158 | return _self;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | #define IBNibRefObj_tp_free PyObject_Del
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | PyTypeObject IBNibRef_Type = {
|
|---|
| 165 | PyObject_HEAD_INIT(NULL)
|
|---|
| 166 | 0, /*ob_size*/
|
|---|
| 167 | "_IBCarbon.IBNibRef", /*tp_name*/
|
|---|
| 168 | sizeof(IBNibRefObject), /*tp_basicsize*/
|
|---|
| 169 | 0, /*tp_itemsize*/
|
|---|
| 170 | /* methods */
|
|---|
| 171 | (destructor) IBNibRefObj_dealloc, /*tp_dealloc*/
|
|---|
| 172 | 0, /*tp_print*/
|
|---|
| 173 | (getattrfunc)0, /*tp_getattr*/
|
|---|
| 174 | (setattrfunc)0, /*tp_setattr*/
|
|---|
| 175 | (cmpfunc) IBNibRefObj_compare, /*tp_compare*/
|
|---|
| 176 | (reprfunc) IBNibRefObj_repr, /*tp_repr*/
|
|---|
| 177 | (PyNumberMethods *)0, /* tp_as_number */
|
|---|
| 178 | (PySequenceMethods *)0, /* tp_as_sequence */
|
|---|
| 179 | (PyMappingMethods *)0, /* tp_as_mapping */
|
|---|
| 180 | (hashfunc) IBNibRefObj_hash, /*tp_hash*/
|
|---|
| 181 | 0, /*tp_call*/
|
|---|
| 182 | 0, /*tp_str*/
|
|---|
| 183 | PyObject_GenericGetAttr, /*tp_getattro*/
|
|---|
| 184 | PyObject_GenericSetAttr, /*tp_setattro */
|
|---|
| 185 | 0, /*tp_as_buffer*/
|
|---|
| 186 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|---|
| 187 | 0, /*tp_doc*/
|
|---|
| 188 | 0, /*tp_traverse*/
|
|---|
| 189 | 0, /*tp_clear*/
|
|---|
| 190 | 0, /*tp_richcompare*/
|
|---|
| 191 | 0, /*tp_weaklistoffset*/
|
|---|
| 192 | 0, /*tp_iter*/
|
|---|
| 193 | 0, /*tp_iternext*/
|
|---|
| 194 | IBNibRefObj_methods, /* tp_methods */
|
|---|
| 195 | 0, /*tp_members*/
|
|---|
| 196 | IBNibRefObj_getsetlist, /*tp_getset*/
|
|---|
| 197 | 0, /*tp_base*/
|
|---|
| 198 | 0, /*tp_dict*/
|
|---|
| 199 | 0, /*tp_descr_get*/
|
|---|
| 200 | 0, /*tp_descr_set*/
|
|---|
| 201 | 0, /*tp_dictoffset*/
|
|---|
| 202 | IBNibRefObj_tp_init, /* tp_init */
|
|---|
| 203 | IBNibRefObj_tp_alloc, /* tp_alloc */
|
|---|
| 204 | IBNibRefObj_tp_new, /* tp_new */
|
|---|
| 205 | IBNibRefObj_tp_free, /* tp_free */
|
|---|
| 206 | };
|
|---|
| 207 |
|
|---|
| 208 | /* -------------------- End object type IBNibRef -------------------- */
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | static PyObject *IBCarbon_CreateNibReference(PyObject *_self, PyObject *_args)
|
|---|
| 212 | {
|
|---|
| 213 | PyObject *_res = NULL;
|
|---|
| 214 | OSStatus _err;
|
|---|
| 215 | CFStringRef inNibName;
|
|---|
| 216 | IBNibRef outNibRef;
|
|---|
| 217 | if (!PyArg_ParseTuple(_args, "O&",
|
|---|
| 218 | CFStringRefObj_Convert, &inNibName))
|
|---|
| 219 | return NULL;
|
|---|
| 220 | _err = CreateNibReference(inNibName,
|
|---|
| 221 | &outNibRef);
|
|---|
| 222 | if (_err != noErr) return PyMac_Error(_err);
|
|---|
| 223 | _res = Py_BuildValue("O&",
|
|---|
| 224 | IBNibRefObj_New, outNibRef);
|
|---|
| 225 | return _res;
|
|---|
| 226 | }
|
|---|
|
|---|