source: python/trunk/Mac/Modules/ibcarbon/_IBCarbon.c

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

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