2 /* ========================= Module _Scrap ========================== */
9 #include "pywintoolbox.h"
12 #include "pymactoolbox.h"
15 /* Macro to test whether a weak-loaded CFM function exists */
16 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
17 PyErr_SetString(PyExc_NotImplementedError, \
18 "Not available in this shared library/OS version"); \
23 #ifdef WITHOUT_FRAMEWORKS
26 #include <Carbon/Carbon.h>
29 #if TARGET_API_MAC_OS8
32 ** Generate ScrapInfo records
39 return Py_BuildValue("lO&hhO&", itself
->scrapSize
,
40 ResObj_New
, itself
->scrapHandle
, itself
->scrapCount
, itself
->scrapState
,
41 PyMac_BuildStr255
, itself
->scrapName
);
45 static PyObject
*Scrap_Error
;
47 #if !TARGET_API_MAC_OS8
48 /* ----------------------- Object type Scrap ------------------------ */
50 PyTypeObject Scrap_Type
;
52 #define ScrapObj_Check(x) ((x)->ob_type == &Scrap_Type)
54 typedef struct ScrapObject
{
59 PyObject
*ScrapObj_New(ScrapRef itself
)
62 it
= PyObject_NEW(ScrapObject
, &Scrap_Type
);
63 if (it
== NULL
) return NULL
;
64 it
->ob_itself
= itself
;
65 return (PyObject
*)it
;
67 int ScrapObj_Convert(PyObject
*v
, ScrapRef
*p_itself
)
69 if (!ScrapObj_Check(v
))
71 PyErr_SetString(PyExc_TypeError
, "Scrap required");
74 *p_itself
= ((ScrapObject
*)v
)->ob_itself
;
78 static void ScrapObj_dealloc(ScrapObject
*self
)
80 /* Cleanup of self->ob_itself goes here */
84 static PyObject
*ScrapObj_GetScrapFlavorFlags(ScrapObject
*_self
, PyObject
*_args
)
86 PyObject
*_res
= NULL
;
88 ScrapFlavorType flavorType
;
89 ScrapFlavorFlags flavorFlags
;
90 if (!PyArg_ParseTuple(_args
, "O&",
91 PyMac_GetOSType
, &flavorType
))
93 _err
= GetScrapFlavorFlags(_self
->ob_itself
,
96 if (_err
!= noErr
) return PyMac_Error(_err
);
97 _res
= Py_BuildValue("l",
102 static PyObject
*ScrapObj_GetScrapFlavorSize(ScrapObject
*_self
, PyObject
*_args
)
104 PyObject
*_res
= NULL
;
106 ScrapFlavorType flavorType
;
108 if (!PyArg_ParseTuple(_args
, "O&",
109 PyMac_GetOSType
, &flavorType
))
111 _err
= GetScrapFlavorSize(_self
->ob_itself
,
114 if (_err
!= noErr
) return PyMac_Error(_err
);
115 _res
= Py_BuildValue("l",
120 static PyObject
*ScrapObj_GetScrapFlavorData(ScrapObject
*_self
, PyObject
*_args
)
122 PyObject
*_res
= NULL
;
124 ScrapFlavorType flavorType
;
127 if (!PyArg_ParseTuple(_args
, "O&",
128 PyMac_GetOSType
, &flavorType
))
130 _err
= GetScrapFlavorSize(_self
->ob_itself
,
133 if (_err
!= noErr
) return PyMac_Error(_err
);
134 _res
= PyString_FromStringAndSize(NULL
, (int)byteCount
);
135 if ( _res
== NULL
) return NULL
;
136 _err
= GetScrapFlavorData(_self
->ob_itself
,
139 PyString_AS_STRING(_res
));
142 return PyMac_Error(_err
);
144 destination__error__
: ;
148 static PyObject
*ScrapObj_PutScrapFlavor(ScrapObject
*_self
, PyObject
*_args
)
150 PyObject
*_res
= NULL
;
152 ScrapFlavorType flavorType
;
153 ScrapFlavorFlags flavorFlags
;
154 char *flavorData__in__
;
155 int flavorData__in_len__
;
156 if (!PyArg_ParseTuple(_args
, "O&ls#",
157 PyMac_GetOSType
, &flavorType
,
159 &flavorData__in__
, &flavorData__in_len__
))
161 _err
= PutScrapFlavor(_self
->ob_itself
,
164 (Size
)flavorData__in_len__
,
166 if (_err
!= noErr
) return PyMac_Error(_err
);
169 flavorData__error__
: ;
173 static PyObject
*ScrapObj_GetScrapFlavorCount(ScrapObject
*_self
, PyObject
*_args
)
175 PyObject
*_res
= NULL
;
178 if (!PyArg_ParseTuple(_args
, ""))
180 _err
= GetScrapFlavorCount(_self
->ob_itself
,
182 if (_err
!= noErr
) return PyMac_Error(_err
);
183 _res
= Py_BuildValue("l",
188 static PyObject
*ScrapObj_GetScrapFlavorInfoList(ScrapObject
*_self
, PyObject
*_args
)
190 PyObject
*_res
= NULL
;
194 ScrapFlavorInfo
*infolist
= NULL
;
197 if (!PyArg_ParseTuple(_args
, ""))
199 _err
= GetScrapFlavorCount(_self
->ob_itself
,
201 if (_err
!= noErr
) return PyMac_Error(_err
);
202 if (infoCount
== 0) return Py_BuildValue("[]");
204 if ((infolist
= (ScrapFlavorInfo
*)malloc(infoCount
*sizeof(ScrapFlavorInfo
))) == NULL
)
205 return PyErr_NoMemory();
207 _err
= GetScrapFlavorInfoList(_self
->ob_itself
, &infoCount
, infolist
);
212 if ((_res
= PyList_New(infoCount
)) == NULL
) {
216 for(i
=0; i
<infoCount
; i
++) {
217 item
= Py_BuildValue("O&l", PyMac_BuildOSType
, infolist
[i
].flavorType
,
218 infolist
[i
].flavorFlags
);
219 if ( !item
|| PyList_SetItem(_res
, i
, item
) < 0 ) {
229 static PyMethodDef ScrapObj_methods
[] = {
230 {"GetScrapFlavorFlags", (PyCFunction
)ScrapObj_GetScrapFlavorFlags
, 1,
231 "(ScrapFlavorType flavorType) -> (ScrapFlavorFlags flavorFlags)"},
232 {"GetScrapFlavorSize", (PyCFunction
)ScrapObj_GetScrapFlavorSize
, 1,
233 "(ScrapFlavorType flavorType) -> (Size byteCount)"},
234 {"GetScrapFlavorData", (PyCFunction
)ScrapObj_GetScrapFlavorData
, 1,
235 "(ScrapFlavorType flavorType) -> string"},
236 {"PutScrapFlavor", (PyCFunction
)ScrapObj_PutScrapFlavor
, 1,
237 "(ScrapFlavorType flavorType, ScrapFlavorFlags flavorFlags, Buffer flavorData) -> None"},
238 {"GetScrapFlavorCount", (PyCFunction
)ScrapObj_GetScrapFlavorCount
, 1,
239 "() -> (UInt32 infoCount)"},
240 {"GetScrapFlavorInfoList", (PyCFunction
)ScrapObj_GetScrapFlavorInfoList
, 1,
241 "() -> ([(ScrapFlavorType, ScrapFlavorInfo), ...])"},
245 PyMethodChain ScrapObj_chain
= { ScrapObj_methods
, NULL
};
247 static PyObject
*ScrapObj_getattr(ScrapObject
*self
, char *name
)
249 return Py_FindMethodInChain(&ScrapObj_chain
, (PyObject
*)self
, name
);
252 #define ScrapObj_setattr NULL
254 #define ScrapObj_compare NULL
256 #define ScrapObj_repr NULL
258 #define ScrapObj_hash NULL
260 PyTypeObject Scrap_Type
= {
261 PyObject_HEAD_INIT(NULL
)
263 "_Scrap.Scrap", /*tp_name*/
264 sizeof(ScrapObject
), /*tp_basicsize*/
267 (destructor
) ScrapObj_dealloc
, /*tp_dealloc*/
269 (getattrfunc
) ScrapObj_getattr
, /*tp_getattr*/
270 (setattrfunc
) ScrapObj_setattr
, /*tp_setattr*/
271 (cmpfunc
) ScrapObj_compare
, /*tp_compare*/
272 (reprfunc
) ScrapObj_repr
, /*tp_repr*/
273 (PyNumberMethods
*)0, /* tp_as_number */
274 (PySequenceMethods
*)0, /* tp_as_sequence */
275 (PyMappingMethods
*)0, /* tp_as_mapping */
276 (hashfunc
) ScrapObj_hash
, /*tp_hash*/
279 /* --------------------- End object type Scrap ---------------------- */
280 #endif /* !TARGET_API_MAC_OS8 */
282 static PyObject
*Scrap_LoadScrap(PyObject
*_self
, PyObject
*_args
)
284 PyObject
*_res
= NULL
;
286 if (!PyArg_ParseTuple(_args
, ""))
289 if (_err
!= noErr
) return PyMac_Error(_err
);
295 static PyObject
*Scrap_UnloadScrap(PyObject
*_self
, PyObject
*_args
)
297 PyObject
*_res
= NULL
;
299 if (!PyArg_ParseTuple(_args
, ""))
301 _err
= UnloadScrap();
302 if (_err
!= noErr
) return PyMac_Error(_err
);
308 #if TARGET_API_MAC_OS8
310 static PyObject
*Scrap_InfoScrap(PyObject
*_self
, PyObject
*_args
)
312 PyObject
*_res
= NULL
;
314 if (!PyArg_ParseTuple(_args
, ""))
317 _res
= Py_BuildValue("O&",
322 static PyObject
*Scrap_GetScrap(PyObject
*_self
, PyObject
*_args
)
324 PyObject
*_res
= NULL
;
327 ScrapFlavorType flavorType
;
329 if (!PyArg_ParseTuple(_args
, "O&O&",
330 ResObj_Convert
, &destination
,
331 PyMac_GetOSType
, &flavorType
))
333 _rv
= GetScrap(destination
,
336 _res
= Py_BuildValue("ll",
342 static PyObject
*Scrap_ZeroScrap(PyObject
*_self
, PyObject
*_args
)
344 PyObject
*_res
= NULL
;
346 if (!PyArg_ParseTuple(_args
, ""))
349 if (_err
!= noErr
) return PyMac_Error(_err
);
355 static PyObject
*Scrap_PutScrap(PyObject
*_self
, PyObject
*_args
)
357 PyObject
*_res
= NULL
;
359 SInt32 sourceBufferByteCount
;
360 ScrapFlavorType flavorType
;
361 char *sourceBuffer__in__
;
362 int sourceBuffer__len__
;
363 int sourceBuffer__in_len__
;
364 if (!PyArg_ParseTuple(_args
, "lO&s#",
365 &sourceBufferByteCount
,
366 PyMac_GetOSType
, &flavorType
,
367 &sourceBuffer__in__
, &sourceBuffer__in_len__
))
369 _err
= PutScrap(sourceBufferByteCount
,
372 if (_err
!= noErr
) return PyMac_Error(_err
);
375 sourceBuffer__error__
: ;
378 #endif /* TARGET_API_MAC_OS8 */
380 #if !TARGET_API_MAC_OS8
381 static PyObject
*Scrap_GetCurrentScrap(PyObject
*_self
, PyObject
*_args
)
383 PyObject
*_res
= NULL
;
386 if (!PyArg_ParseTuple(_args
, ""))
388 _err
= GetCurrentScrap(&scrap
);
389 if (_err
!= noErr
) return PyMac_Error(_err
);
390 _res
= Py_BuildValue("O&",
391 ScrapObj_New
, scrap
);
395 static PyObject
*Scrap_ClearCurrentScrap(PyObject
*_self
, PyObject
*_args
)
397 PyObject
*_res
= NULL
;
399 if (!PyArg_ParseTuple(_args
, ""))
401 _err
= ClearCurrentScrap();
402 if (_err
!= noErr
) return PyMac_Error(_err
);
408 static PyObject
*Scrap_CallInScrapPromises(PyObject
*_self
, PyObject
*_args
)
410 PyObject
*_res
= NULL
;
412 if (!PyArg_ParseTuple(_args
, ""))
414 _err
= CallInScrapPromises();
415 if (_err
!= noErr
) return PyMac_Error(_err
);
422 static PyMethodDef Scrap_methods
[] = {
423 {"LoadScrap", (PyCFunction
)Scrap_LoadScrap
, 1,
425 {"UnloadScrap", (PyCFunction
)Scrap_UnloadScrap
, 1,
428 #if TARGET_API_MAC_OS8
429 {"InfoScrap", (PyCFunction
)Scrap_InfoScrap
, 1,
430 "() -> (ScrapStuffPtr _rv)"},
431 {"GetScrap", (PyCFunction
)Scrap_GetScrap
, 1,
432 "(Handle destination, ScrapFlavorType flavorType) -> (long _rv, SInt32 offset)"},
433 {"ZeroScrap", (PyCFunction
)Scrap_ZeroScrap
, 1,
435 {"PutScrap", (PyCFunction
)Scrap_PutScrap
, 1,
436 "(SInt32 sourceBufferByteCount, ScrapFlavorType flavorType, Buffer sourceBuffer) -> None"},
439 #if !TARGET_API_MAC_OS8
440 {"GetCurrentScrap", (PyCFunction
)Scrap_GetCurrentScrap
, 1,
441 "() -> (ScrapRef scrap)"},
442 {"ClearCurrentScrap", (PyCFunction
)Scrap_ClearCurrentScrap
, 1,
444 {"CallInScrapPromises", (PyCFunction
)Scrap_CallInScrapPromises
, 1,
453 void init_Scrap(void)
461 m
= Py_InitModule("_Scrap", Scrap_methods
);
462 d
= PyModule_GetDict(m
);
463 Scrap_Error
= PyMac_GetOSErrException();
464 if (Scrap_Error
== NULL
||
465 PyDict_SetItemString(d
, "Error", Scrap_Error
) != 0)
467 #if !TARGET_API_MAC_OS8
468 Scrap_Type
.ob_type
= &PyType_Type
;
469 Py_INCREF(&Scrap_Type
);
470 if (PyDict_SetItemString(d
, "ScrapType", (PyObject
*)&Scrap_Type
) != 0)
471 Py_FatalError("can't initialize ScrapType");
475 /* ======================= End module _Scrap ======================== */