Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c06992

Browse files
committed
Donovan Preston's interface to IBCarbon, allowing you to use Interface
Builder carbon NIB files from Python. As-is, I may need to twiddle a few things as he donated this long ago. Donovan is now one of the four people in the world who know how to drive bgen!
1 parent 84a0164 commit 0c06992

3 files changed

Lines changed: 342 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# IBCarbonscan.py
2+
3+
import sys
4+
import os
5+
import string
6+
import MacOS
7+
8+
BGENDIR= '/Users/dp/python/dist/src/Tools/bgen/bgen'
9+
sys.path.append(BGENDIR)
10+
print sys.path, sys.prefix
11+
from bgenlocations import TOOLBOXDIR
12+
13+
from scantools import Scanner_OSX
14+
15+
def main():
16+
print "---Scanning IBCarbonRuntime.h---"
17+
input = ["IBCarbonRuntime.h"]
18+
output = "IBCarbongen.py"
19+
defsoutput = TOOLBOXDIR + "IBCarbonRuntime.py"
20+
scanner = IBCarbon_Scanner(input, output, defsoutput)
21+
scanner.scan()
22+
scanner.close()
23+
print "--done scanning, importing--"
24+
import IBCarbonsupport
25+
print "done"
26+
27+
class IBCarbon_Scanner(Scanner_OSX):
28+
def destination(self, type, name, arglist):
29+
classname = "IBCarbonFunction"
30+
listname = "functions"
31+
if arglist:
32+
t, n, m = arglist[0]
33+
if t == "IBNibRef" and m == "InMode":
34+
classname = "IBCarbonMethod"
35+
listname = "methods"
36+
return classname, listname
37+
38+
def makeblacklistnames(self):
39+
return [
40+
"DisposeNibReference", # taken care of by destructor
41+
"CreateNibReferenceWithCFBundle", ## need to wrap CFBundle.h properly first
42+
]
43+
44+
if __name__ == "__main__":
45+
main()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# IBCarbonsupport.py
2+
3+
from macsupport import *
4+
5+
CFStringRef = OpaqueByValueType('CFStringRef', 'CFStringRefObj')
6+
IBNibRef = OpaqueByValueType('IBNibRef', 'IBNibRefObj')
7+
#CFBundleRef = OpaqueByValueType('CFBundleRef')
8+
9+
IBCarbonFunction = OSErrFunctionGenerator
10+
IBCarbonMethod = OSErrMethodGenerator
11+
12+
includestuff = """
13+
#ifdef WITHOUT_FRAMEWORKS
14+
#include <IBCarbonRuntime.h>
15+
#else
16+
#include <Carbon/Carbon.h>
17+
#endif /* WITHOUT_FRAMEWORKS */
18+
#include "macglue.h"
19+
20+
#ifdef USE_TOOLBOX_OBJECT_GLUE
21+
extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
22+
//#define CFStringRefObj_Convert _CFStringRefObj_Convert
23+
#endif
24+
25+
//extern int CFBundleRefObj_Convert(PyObject *, CFBundleRef *); // need to wrap CFBundle
26+
27+
"""
28+
29+
initstuff = """
30+
31+
"""
32+
33+
module = MacModule('_IBCarbon', 'IBCarbon', includestuff, finalstuff, initstuff)
34+
35+
class CFReleaserObject(GlobalObjectDefinition):
36+
def outputFreeIt(self, name):
37+
Output("CFRelease(%s);" % name)
38+
39+
class CFNibDesc(GlobalObjectDefinition):
40+
def outputFreeIt(self, name):
41+
Output("DisposeNibReference(%s);" % name)
42+
43+
#cfstringobject = CFReleaserObject("CFStringRef")
44+
#module.addobject(cfstringobject)
45+
#cfbundleobject = CFReleaserObject("CFBundleRef")
46+
#module.addobject(cfbundleobject)
47+
ibnibobject = CFNibDesc("IBNibRef", "IBNibRefObj")
48+
module.addobject(ibnibobject)
49+
50+
functions = []
51+
methods = []
52+
53+
execfile('IBCarbongen.py')
54+
55+
for f in functions: module.add(f)
56+
for m in methods: ibnibobject.add(m)
57+
58+
SetOutputFileName('_IBCarbon.c')
59+
module.generate()

Mac/Modules/ibcarbon/_IBCarbon.c

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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

Comments
 (0)