/Mac/Modules/ibcarbon/_IBCarbon.c

http://unladen-swallow.googlecode.com/ · C · 270 lines · 218 code · 46 blank · 6 comment · 27 complexity · 105f1733730ab6347893d77851167c94 MD5 · raw file

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