summaryrefslogtreecommitdiffstats
path: root/Mac/Modules/ibcarbon/_IBCarbon.c
blob: 1d50db2ba4280ca71b2f9c186c1dcc2ce9366960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

/* ======================== Module _IBCarbon ======================== */

#include "Python.h"



#include <Carbon/Carbon.h>
#include "macglue.h"

#ifdef USE_TOOLBOX_OBJECT_GLUE
extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
//#define CFStringRefObj_Convert _CFStringRefObj_Convert
#endif

//extern int CFBundleRefObj_Convert(PyObject *, CFBundleRef *);  // need to wrap CFBundle


static PyObject *IBCarbon_Error;

/* ---------------------- Object type IBNibRef ---------------------- */

PyTypeObject IBNibRef_Type;

#define IBNibRefObj_Check(x) ((x)->ob_type == &IBNibRef_Type || PyObject_TypeCheck((x), &IBNibRef_Type))

typedef struct IBNibRefObject {
	PyObject_HEAD
	IBNibRef ob_itself;
} IBNibRefObject;

PyObject *IBNibRefObj_New(IBNibRef itself)
{
	IBNibRefObject *it;
	it = PyObject_NEW(IBNibRefObject, &IBNibRef_Type);
	if (it == NULL) return NULL;
	it->ob_itself = itself;
	return (PyObject *)it;
}
int IBNibRefObj_Convert(PyObject *v, IBNibRef *p_itself)
{
	if (!IBNibRefObj_Check(v))
	{
		PyErr_SetString(PyExc_TypeError, "IBNibRef required");
		return 0;
	}
	*p_itself = ((IBNibRefObject *)v)->ob_itself;
	return 1;
}

static void IBNibRefObj_dealloc(IBNibRefObject *self)
{
	DisposeNibReference(self->ob_itself);
	self->ob_type->tp_free((PyObject *)self);
}

static PyObject *IBNibRefObj_CreateWindowFromNib(IBNibRefObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSStatus _err;
	CFStringRef inName;
	WindowPtr outWindow;
	if (!PyArg_ParseTuple(_args, "O&",
	                      CFStringRefObj_Convert, &inName))
		return NULL;
	_err = CreateWindowFromNib(_self->ob_itself,
	                           inName,
	                           &outWindow);
	if (_err != noErr) return PyMac_Error(_err);
	_res = Py_BuildValue("O&",
	                     WinObj_New, outWindow);
	return _res;
}

static PyObject *IBNibRefObj_CreateMenuFromNib(IBNibRefObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSStatus _err;
	CFStringRef inName;
	MenuHandle outMenuRef;
	if (!PyArg_ParseTuple(_args, "O&",
	                      CFStringRefObj_Convert, &inName))
		return NULL;
	_err = CreateMenuFromNib(_self->ob_itself,
	                         inName,
	                         &outMenuRef);
	if (_err != noErr) return PyMac_Error(_err);
	_res = Py_BuildValue("O&",
	                     MenuObj_New, outMenuRef);
	return _res;
}

static PyObject *IBNibRefObj_CreateMenuBarFromNib(IBNibRefObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSStatus _err;
	CFStringRef inName;
	Handle outMenuBar;
	if (!PyArg_ParseTuple(_args, "O&",
	                      CFStringRefObj_Convert, &inName))
		return NULL;
	_err = CreateMenuBarFromNib(_self->ob_itself,
	                            inName,
	                            &outMenuBar);
	if (_err != noErr) return PyMac_Error(_err);
	_res = Py_BuildValue("O&",
	                     ResObj_New, outMenuBar);
	return _res;
}

static PyObject *IBNibRefObj_SetMenuBarFromNib(IBNibRefObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSStatus _err;
	CFStringRef inName;
	if (!PyArg_ParseTuple(_args, "O&",
	                      CFStringRefObj_Convert, &inName))
		return NULL;
	_err = SetMenuBarFromNib(_self->ob_itself,
	                         inName);
	if (_err != noErr) return PyMac_Error(_err);
	Py_INCREF(Py_None);
	_res = Py_None;
	return _res;
}

static PyMethodDef IBNibRefObj_methods[] = {
	{"CreateWindowFromNib", (PyCFunction)IBNibRefObj_CreateWindowFromNib, 1,
	 PyDoc_STR("(CFStringRef inName) -> (WindowPtr outWindow)")},
	{"CreateMenuFromNib", (PyCFunction)IBNibRefObj_CreateMenuFromNib, 1,
	 PyDoc_STR("(CFStringRef inName) -> (MenuHandle outMenuRef)")},
	{"CreateMenuBarFromNib", (PyCFunction)IBNibRefObj_CreateMenuBarFromNib, 1,
	 PyDoc_STR("(CFStringRef inName) -> (Handle outMenuBar)")},
	{"SetMenuBarFromNib", (PyCFunction)IBNibRefObj_SetMenuBarFromNib, 1,
	 PyDoc_STR("(CFStringRef inName) -> None")},
	{NULL, NULL, 0}
};

#define IBNibRefObj_getsetlist NULL


#define IBNibRefObj_compare NULL

#define IBNibRefObj_repr NULL

#define IBNibRefObj_hash NULL
#define IBNibRefObj_tp_init 0

#define IBNibRefObj_tp_alloc PyType_GenericAlloc

static PyObject *IBNibRefObj_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	PyObject *self;
	IBNibRef itself;
	char *kw[] = {"itself", 0};

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kw, IBNibRefObj_Convert, &itself)) return NULL;
	if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;
	((IBNibRefObject *)self)->ob_itself = itself;
	return self;
}

#define IBNibRefObj_tp_free PyObject_Del


PyTypeObject IBNibRef_Type = {
	PyObject_HEAD_INIT(NULL)
	0, /*ob_size*/
	"_IBCarbon.IBNibRef", /*tp_name*/
	sizeof(IBNibRefObject), /*tp_basicsize*/
	0, /*tp_itemsize*/
	/* methods */
	(destructor) IBNibRefObj_dealloc, /*tp_dealloc*/
	0, /*tp_print*/
	(getattrfunc)0, /*tp_getattr*/
	(setattrfunc)0, /*tp_setattr*/
	(cmpfunc) IBNibRefObj_compare, /*tp_compare*/
	(reprfunc) IBNibRefObj_repr, /*tp_repr*/
	(PyNumberMethods *)0, /* tp_as_number */
	(PySequenceMethods *)0, /* tp_as_sequence */
	(PyMappingMethods *)0, /* tp_as_mapping */
	(hashfunc) IBNibRefObj_hash, /*tp_hash*/
	0, /*tp_call*/
	0, /*tp_str*/
	PyObject_GenericGetAttr, /*tp_getattro*/
	PyObject_GenericSetAttr, /*tp_setattro */
	0, /*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
	0, /*tp_doc*/
	0, /*tp_traverse*/
	0, /*tp_clear*/
	0, /*tp_richcompare*/
	0, /*tp_weaklistoffset*/
	0, /*tp_iter*/
	0, /*tp_iternext*/
	IBNibRefObj_methods, /* tp_methods */
	0, /*tp_members*/
	IBNibRefObj_getsetlist, /*tp_getset*/
	0, /*tp_base*/
	0, /*tp_dict*/
	0, /*tp_descr_get*/
	0, /*tp_descr_set*/
	0, /*tp_dictoffset*/
	IBNibRefObj_tp_init, /* tp_init */
	IBNibRefObj_tp_alloc, /* tp_alloc */
	IBNibRefObj_tp_new, /* tp_new */
	IBNibRefObj_tp_free, /* tp_free */
};

/* -------------------- End object type IBNibRef -------------------- */


static PyObject *IBCarbon_CreateNibReference(PyObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSStatus _err;
	CFStringRef inNibName;
	IBNibRef outNibRef;
	if (!PyArg_ParseTuple(_args, "O&",
	                      CFStringRefObj_Convert, &inNibName))
		return NULL;
	_err = CreateNibReference(inNibName,
	                          &outNibRef);
	if (_err != noErr) return PyMac_Error(_err);
	_res = Py_BuildValue("O&",
	                     IBNibRefObj_New, outNibRef);
	return _res;
}

static PyMethodDef IBCarbon_methods[] = {
	{"CreateNibReference", (PyCFunction)IBCarbon_CreateNibReference, 1,
	 PyDoc_STR("(CFStringRef inNibName) -> (IBNibRef outNibRef)")},
	{NULL, NULL, 0}
};




void init_IBCarbon(void)
{
	PyObject *m;
	PyObject *d;





	m = Py_InitModule("_IBCarbon", IBCarbon_methods);
	d = PyModule_GetDict(m);
	IBCarbon_Error = PyMac_GetOSErrException();
	if (IBCarbon_Error == NULL ||
	    PyDict_SetItemString(d, "Error", IBCarbon_Error) != 0)
		return;
	IBNibRef_Type.ob_type = &PyType_Type;
	if (PyType_Ready(&IBNibRef_Type) < 0) return;
	Py_INCREF(&IBNibRef_Type);
	PyModule_AddObject(m, "IBNibRef", (PyObject *)&IBNibRef_Type);
	/* Backward-compatible name */
	Py_INCREF(&IBNibRef_Type);
	PyModule_AddObject(m, "IBNibRefType", (PyObject *)&IBNibRef_Type);
}

/* ====================== End module _IBCarbon ====================== */