2001-08-23 11:02:09 -03:00
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
/* ========================= Module _Scrap ========================== */
|
2001-08-23 11:02:09 -03:00
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
|
2008-06-05 09:58:24 -03:00
|
|
|
#ifndef __LP64__
|
2001-08-23 11:02:09 -03:00
|
|
|
|
|
|
|
#include "pymactoolbox.h"
|
2001-12-31 10:52:03 -04:00
|
|
|
|
|
|
|
/* Macro to test whether a weak-loaded CFM function exists */
|
|
|
|
#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_NotImplementedError, \
|
|
|
|
"Not available in this shared library/OS version"); \
|
|
|
|
return NULL; \
|
2001-12-31 10:52:03 -04:00
|
|
|
}} while(0)
|
|
|
|
|
2001-08-23 11:02:09 -03:00
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
|
|
|
|
static PyObject *Scrap_Error;
|
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
/* ----------------------- Object type Scrap ------------------------ */
|
|
|
|
|
|
|
|
PyTypeObject Scrap_Type;
|
|
|
|
|
2003-11-20 09:31:00 -04:00
|
|
|
#define ScrapObj_Check(x) ((x)->ob_type == &Scrap_Type || PyObject_TypeCheck((x), &Scrap_Type))
|
2001-12-31 10:52:03 -04:00
|
|
|
|
|
|
|
typedef struct ScrapObject {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
ScrapRef ob_itself;
|
2001-12-31 10:52:03 -04:00
|
|
|
} ScrapObject;
|
|
|
|
|
|
|
|
PyObject *ScrapObj_New(ScrapRef itself)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
ScrapObject *it;
|
|
|
|
it = PyObject_NEW(ScrapObject, &Scrap_Type);
|
|
|
|
if (it == NULL) return NULL;
|
|
|
|
it->ob_itself = itself;
|
|
|
|
return (PyObject *)it;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
int ScrapObj_Convert(PyObject *v, ScrapRef *p_itself)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!ScrapObj_Check(v))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Scrap required");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*p_itself = ((ScrapObject *)v)->ob_itself;
|
|
|
|
return 1;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ScrapObj_dealloc(ScrapObject *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Cleanup of self->ob_itself goes here */
|
|
|
|
self->ob_type->tp_free((PyObject *)self);
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_GetScrapFlavorFlags(ScrapObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
ScrapFlavorType flavorType;
|
|
|
|
ScrapFlavorFlags flavorFlags;
|
|
|
|
if (!PyArg_ParseTuple(_args, "O&",
|
|
|
|
PyMac_GetOSType, &flavorType))
|
|
|
|
return NULL;
|
|
|
|
_err = GetScrapFlavorFlags(_self->ob_itself,
|
|
|
|
flavorType,
|
|
|
|
&flavorFlags);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
_res = Py_BuildValue("l",
|
|
|
|
flavorFlags);
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_GetScrapFlavorSize(ScrapObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
ScrapFlavorType flavorType;
|
|
|
|
Size byteCount;
|
|
|
|
if (!PyArg_ParseTuple(_args, "O&",
|
|
|
|
PyMac_GetOSType, &flavorType))
|
|
|
|
return NULL;
|
|
|
|
_err = GetScrapFlavorSize(_self->ob_itself,
|
|
|
|
flavorType,
|
|
|
|
&byteCount);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
_res = Py_BuildValue("l",
|
|
|
|
byteCount);
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_GetScrapFlavorData(ScrapObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
ScrapFlavorType flavorType;
|
|
|
|
Size byteCount;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(_args, "O&",
|
|
|
|
PyMac_GetOSType, &flavorType))
|
|
|
|
return NULL;
|
|
|
|
_err = GetScrapFlavorSize(_self->ob_itself,
|
|
|
|
flavorType,
|
|
|
|
&byteCount);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
_res = PyString_FromStringAndSize(NULL, (int)byteCount);
|
|
|
|
if ( _res == NULL ) return NULL;
|
|
|
|
_err = GetScrapFlavorData(_self->ob_itself,
|
|
|
|
flavorType,
|
|
|
|
&byteCount,
|
|
|
|
PyString_AS_STRING(_res));
|
|
|
|
if (_err != noErr) {
|
|
|
|
Py_XDECREF(_res);
|
|
|
|
return PyMac_Error(_err);
|
|
|
|
}
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_PutScrapFlavor(ScrapObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
ScrapFlavorType flavorType;
|
|
|
|
ScrapFlavorFlags flavorFlags;
|
|
|
|
char *flavorData__in__;
|
|
|
|
int flavorData__in_len__;
|
|
|
|
if (!PyArg_ParseTuple(_args, "O&Ks#",
|
|
|
|
PyMac_GetOSType, &flavorType,
|
|
|
|
&flavorFlags,
|
|
|
|
&flavorData__in__, &flavorData__in_len__))
|
|
|
|
return NULL;
|
|
|
|
_err = PutScrapFlavor(_self->ob_itself,
|
|
|
|
flavorType,
|
|
|
|
flavorFlags,
|
|
|
|
(Size)flavorData__in_len__,
|
|
|
|
flavorData__in__);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
_res = Py_None;
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_GetScrapFlavorCount(ScrapObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
UInt32 infoCount;
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = GetScrapFlavorCount(_self->ob_itself,
|
|
|
|
&infoCount);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
_res = Py_BuildValue("l",
|
|
|
|
infoCount);
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_GetScrapFlavorInfoList(ScrapObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
PyObject *item;
|
|
|
|
OSStatus _err;
|
|
|
|
UInt32 infoCount;
|
|
|
|
ScrapFlavorInfo *infolist = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = GetScrapFlavorCount(_self->ob_itself,
|
|
|
|
&infoCount);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
if (infoCount == 0) return Py_BuildValue("[]");
|
|
|
|
|
|
|
|
if ((infolist = (ScrapFlavorInfo *)malloc(infoCount*sizeof(ScrapFlavorInfo))) == NULL )
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
|
|
|
|
_err = GetScrapFlavorInfoList(_self->ob_itself, &infoCount, infolist);
|
|
|
|
if (_err != noErr) {
|
|
|
|
free(infolist);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((_res = PyList_New(infoCount)) == NULL ) {
|
|
|
|
free(infolist);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for(i=0; i<infoCount; i++) {
|
|
|
|
item = Py_BuildValue("O&l", PyMac_BuildOSType, infolist[i].flavorType,
|
|
|
|
infolist[i].flavorFlags);
|
|
|
|
if ( !item || PyList_SetItem(_res, i, item) < 0 ) {
|
|
|
|
Py_DECREF(_res);
|
|
|
|
free(infolist);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(infolist);
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef ScrapObj_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"GetScrapFlavorFlags", (PyCFunction)ScrapObj_GetScrapFlavorFlags, 1,
|
|
|
|
PyDoc_STR("(ScrapFlavorType flavorType) -> (ScrapFlavorFlags flavorFlags)")},
|
|
|
|
{"GetScrapFlavorSize", (PyCFunction)ScrapObj_GetScrapFlavorSize, 1,
|
|
|
|
PyDoc_STR("(ScrapFlavorType flavorType) -> (Size byteCount)")},
|
|
|
|
{"GetScrapFlavorData", (PyCFunction)ScrapObj_GetScrapFlavorData, 1,
|
|
|
|
PyDoc_STR("(ScrapFlavorType flavorType, Buffer destination) -> (Size byteCount)")},
|
|
|
|
{"PutScrapFlavor", (PyCFunction)ScrapObj_PutScrapFlavor, 1,
|
|
|
|
PyDoc_STR("(ScrapFlavorType flavorType, ScrapFlavorFlags flavorFlags, Size flavorSize, Buffer flavorData) -> None")},
|
|
|
|
{"GetScrapFlavorCount", (PyCFunction)ScrapObj_GetScrapFlavorCount, 1,
|
|
|
|
PyDoc_STR("() -> (UInt32 infoCount)")},
|
|
|
|
{"GetScrapFlavorInfoList", (PyCFunction)ScrapObj_GetScrapFlavorInfoList, 1,
|
|
|
|
PyDoc_STR("() -> ([(ScrapFlavorType, ScrapFlavorInfo), ...])")},
|
|
|
|
{NULL, NULL, 0}
|
2001-12-31 10:52:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMethodChain ScrapObj_chain = { ScrapObj_methods, NULL };
|
|
|
|
|
|
|
|
static PyObject *ScrapObj_getattr(ScrapObject *self, char *name)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return Py_FindMethodInChain(&ScrapObj_chain, (PyObject *)self, name);
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ScrapObj_setattr NULL
|
|
|
|
|
|
|
|
#define ScrapObj_compare NULL
|
|
|
|
|
|
|
|
#define ScrapObj_repr NULL
|
|
|
|
|
|
|
|
#define ScrapObj_hash NULL
|
|
|
|
|
|
|
|
PyTypeObject Scrap_Type = {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
|
|
|
0, /*ob_size*/
|
|
|
|
"_Scrap.Scrap", /*tp_name*/
|
|
|
|
sizeof(ScrapObject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor) ScrapObj_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc) ScrapObj_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc) ScrapObj_setattr, /*tp_setattr*/
|
|
|
|
(cmpfunc) ScrapObj_compare, /*tp_compare*/
|
|
|
|
(reprfunc) ScrapObj_repr, /*tp_repr*/
|
|
|
|
(PyNumberMethods *)0, /* tp_as_number */
|
|
|
|
(PySequenceMethods *)0, /* tp_as_sequence */
|
|
|
|
(PyMappingMethods *)0, /* tp_as_mapping */
|
|
|
|
(hashfunc) ScrapObj_hash, /*tp_hash*/
|
2001-12-31 10:52:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* --------------------- End object type Scrap ---------------------- */
|
|
|
|
|
|
|
|
static PyObject *Scrap_LoadScrap(PyObject *_self, PyObject *_args)
|
2001-08-23 11:02:09 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = LoadScrap();
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
_res = Py_None;
|
|
|
|
return _res;
|
2001-08-23 11:02:09 -03:00
|
|
|
}
|
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
static PyObject *Scrap_UnloadScrap(PyObject *_self, PyObject *_args)
|
2001-08-23 11:02:09 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = UnloadScrap();
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
_res = Py_None;
|
|
|
|
return _res;
|
2001-08-23 11:02:09 -03:00
|
|
|
}
|
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
static PyObject *Scrap_GetCurrentScrap(PyObject *_self, PyObject *_args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
ScrapRef scrap;
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = GetCurrentScrap(&scrap);
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
_res = Py_BuildValue("O&",
|
|
|
|
ScrapObj_New, scrap);
|
|
|
|
return _res;
|
2001-12-31 10:52:03 -04:00
|
|
|
}
|
2001-08-23 11:02:09 -03:00
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
static PyObject *Scrap_ClearCurrentScrap(PyObject *_self, PyObject *_args)
|
2001-08-23 11:02:09 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = ClearCurrentScrap();
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
_res = Py_None;
|
|
|
|
return _res;
|
2001-08-23 11:02:09 -03:00
|
|
|
}
|
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
static PyObject *Scrap_CallInScrapPromises(PyObject *_self, PyObject *_args)
|
2001-08-23 11:02:09 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *_res = NULL;
|
|
|
|
OSStatus _err;
|
|
|
|
if (!PyArg_ParseTuple(_args, ""))
|
|
|
|
return NULL;
|
|
|
|
_err = CallInScrapPromises();
|
|
|
|
if (_err != noErr) return PyMac_Error(_err);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
_res = Py_None;
|
|
|
|
return _res;
|
2001-08-23 11:02:09 -03:00
|
|
|
}
|
2008-06-05 09:58:24 -03:00
|
|
|
#endif /* __LP64__ */
|
2001-08-23 11:02:09 -03:00
|
|
|
|
|
|
|
static PyMethodDef Scrap_methods[] = {
|
2008-06-05 09:58:24 -03:00
|
|
|
#ifndef __LP64__
|
2010-05-09 11:46:46 -03:00
|
|
|
{"LoadScrap", (PyCFunction)Scrap_LoadScrap, 1,
|
|
|
|
PyDoc_STR("() -> None")},
|
|
|
|
{"UnloadScrap", (PyCFunction)Scrap_UnloadScrap, 1,
|
|
|
|
PyDoc_STR("() -> None")},
|
|
|
|
{"GetCurrentScrap", (PyCFunction)Scrap_GetCurrentScrap, 1,
|
|
|
|
PyDoc_STR("() -> (ScrapRef scrap)")},
|
|
|
|
{"ClearCurrentScrap", (PyCFunction)Scrap_ClearCurrentScrap, 1,
|
|
|
|
PyDoc_STR("() -> None")},
|
|
|
|
{"CallInScrapPromises", (PyCFunction)Scrap_CallInScrapPromises, 1,
|
|
|
|
PyDoc_STR("() -> None")},
|
2008-06-05 09:58:24 -03:00
|
|
|
#endif /* __LP64__ */
|
2010-05-09 11:46:46 -03:00
|
|
|
{NULL, NULL, 0}
|
2001-08-23 11:02:09 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
void init_Scrap(void)
|
2001-08-23 11:02:09 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m;
|
2008-06-05 09:58:24 -03:00
|
|
|
#ifndef __LP64__
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *d;
|
2008-06-05 09:58:24 -03:00
|
|
|
#endif /* __LP64__ */
|
2001-08-23 11:02:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
m = Py_InitModule("_Scrap", Scrap_methods);
|
2008-06-05 09:58:24 -03:00
|
|
|
#ifndef __LP64__
|
2010-05-09 11:46:46 -03:00
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
Scrap_Error = PyMac_GetOSErrException();
|
|
|
|
if (Scrap_Error == NULL ||
|
|
|
|
PyDict_SetItemString(d, "Error", Scrap_Error) != 0)
|
|
|
|
return;
|
|
|
|
Scrap_Type.ob_type = &PyType_Type;
|
|
|
|
Py_INCREF(&Scrap_Type);
|
|
|
|
if (PyDict_SetItemString(d, "ScrapType", (PyObject *)&Scrap_Type) != 0)
|
|
|
|
Py_FatalError("can't initialize ScrapType");
|
2008-06-05 09:58:24 -03:00
|
|
|
#endif /* __LP64__ */
|
2001-08-23 11:02:09 -03:00
|
|
|
}
|
|
|
|
|
2001-12-31 10:52:03 -04:00
|
|
|
/* ======================= End module _Scrap ======================== */
|
2001-08-23 11:02:09 -03:00
|
|
|
|