Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Mac / Modules / scrap / _Scrapmodule.c
blob4b3d86584bcf2ca7276c80a7606c9a475fa79a45
2 /* ========================= Module _Scrap ========================== */
4 #include "Python.h"
8 #ifdef _WIN32
9 #include "pywintoolbox.h"
10 #else
11 #include "macglue.h"
12 #include "pymactoolbox.h"
13 #endif
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"); \
19 return NULL; \
20 }} while(0)
23 #ifdef WITHOUT_FRAMEWORKS
24 #include <Scrap.h>
25 #else
26 #include <Carbon/Carbon.h>
27 #endif
29 #if TARGET_API_MAC_OS8
32 ** Generate ScrapInfo records
34 static PyObject *
35 SCRRec_New(itself)
36 ScrapStuff *itself;
39 return Py_BuildValue("lO&hhO&", itself->scrapSize,
40 ResObj_New, itself->scrapHandle, itself->scrapCount, itself->scrapState,
41 PyMac_BuildStr255, itself->scrapName);
43 #endif
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 {
55 PyObject_HEAD
56 ScrapRef ob_itself;
57 } ScrapObject;
59 PyObject *ScrapObj_New(ScrapRef itself)
61 ScrapObject *it;
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");
72 return 0;
74 *p_itself = ((ScrapObject *)v)->ob_itself;
75 return 1;
78 static void ScrapObj_dealloc(ScrapObject *self)
80 /* Cleanup of self->ob_itself goes here */
81 PyObject_DEL(self);
84 static PyObject *ScrapObj_GetScrapFlavorFlags(ScrapObject *_self, PyObject *_args)
86 PyObject *_res = NULL;
87 OSStatus _err;
88 ScrapFlavorType flavorType;
89 ScrapFlavorFlags flavorFlags;
90 if (!PyArg_ParseTuple(_args, "O&",
91 PyMac_GetOSType, &flavorType))
92 return NULL;
93 _err = GetScrapFlavorFlags(_self->ob_itself,
94 flavorType,
95 &flavorFlags);
96 if (_err != noErr) return PyMac_Error(_err);
97 _res = Py_BuildValue("l",
98 flavorFlags);
99 return _res;
102 static PyObject *ScrapObj_GetScrapFlavorSize(ScrapObject *_self, PyObject *_args)
104 PyObject *_res = NULL;
105 OSStatus _err;
106 ScrapFlavorType flavorType;
107 Size byteCount;
108 if (!PyArg_ParseTuple(_args, "O&",
109 PyMac_GetOSType, &flavorType))
110 return NULL;
111 _err = GetScrapFlavorSize(_self->ob_itself,
112 flavorType,
113 &byteCount);
114 if (_err != noErr) return PyMac_Error(_err);
115 _res = Py_BuildValue("l",
116 byteCount);
117 return _res;
120 static PyObject *ScrapObj_GetScrapFlavorData(ScrapObject *_self, PyObject *_args)
122 PyObject *_res = NULL;
123 OSStatus _err;
124 ScrapFlavorType flavorType;
125 Size byteCount;
127 if (!PyArg_ParseTuple(_args, "O&",
128 PyMac_GetOSType, &flavorType))
129 return NULL;
130 _err = GetScrapFlavorSize(_self->ob_itself,
131 flavorType,
132 &byteCount);
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,
137 flavorType,
138 &byteCount,
139 PyString_AS_STRING(_res));
140 if (_err != noErr) {
141 Py_XDECREF(_res);
142 return PyMac_Error(_err);
144 destination__error__: ;
145 return _res;
148 static PyObject *ScrapObj_PutScrapFlavor(ScrapObject *_self, PyObject *_args)
150 PyObject *_res = NULL;
151 OSStatus _err;
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,
158 &flavorFlags,
159 &flavorData__in__, &flavorData__in_len__))
160 return NULL;
161 _err = PutScrapFlavor(_self->ob_itself,
162 flavorType,
163 flavorFlags,
164 (Size)flavorData__in_len__,
165 flavorData__in__);
166 if (_err != noErr) return PyMac_Error(_err);
167 Py_INCREF(Py_None);
168 _res = Py_None;
169 flavorData__error__: ;
170 return _res;
173 static PyObject *ScrapObj_GetScrapFlavorCount(ScrapObject *_self, PyObject *_args)
175 PyObject *_res = NULL;
176 OSStatus _err;
177 UInt32 infoCount;
178 if (!PyArg_ParseTuple(_args, ""))
179 return NULL;
180 _err = GetScrapFlavorCount(_self->ob_itself,
181 &infoCount);
182 if (_err != noErr) return PyMac_Error(_err);
183 _res = Py_BuildValue("l",
184 infoCount);
185 return _res;
188 static PyObject *ScrapObj_GetScrapFlavorInfoList(ScrapObject *_self, PyObject *_args)
190 PyObject *_res = NULL;
191 PyObject *item;
192 OSStatus _err;
193 UInt32 infoCount;
194 ScrapFlavorInfo *infolist = NULL;
195 int i;
197 if (!PyArg_ParseTuple(_args, ""))
198 return NULL;
199 _err = GetScrapFlavorCount(_self->ob_itself,
200 &infoCount);
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);
208 if (_err != noErr) {
209 free(infolist);
210 return NULL;
212 if ((_res = PyList_New(infoCount)) == NULL ) {
213 free(infolist);
214 return 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 ) {
220 Py_DECREF(_res);
221 free(infolist);
222 return NULL;
225 free(infolist);
226 return _res;
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), ...])"},
242 {NULL, NULL, 0}
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)
262 0, /*ob_size*/
263 "_Scrap.Scrap", /*tp_name*/
264 sizeof(ScrapObject), /*tp_basicsize*/
265 0, /*tp_itemsize*/
266 /* methods */
267 (destructor) ScrapObj_dealloc, /*tp_dealloc*/
268 0, /*tp_print*/
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;
285 OSStatus _err;
286 if (!PyArg_ParseTuple(_args, ""))
287 return NULL;
288 _err = LoadScrap();
289 if (_err != noErr) return PyMac_Error(_err);
290 Py_INCREF(Py_None);
291 _res = Py_None;
292 return _res;
295 static PyObject *Scrap_UnloadScrap(PyObject *_self, PyObject *_args)
297 PyObject *_res = NULL;
298 OSStatus _err;
299 if (!PyArg_ParseTuple(_args, ""))
300 return NULL;
301 _err = UnloadScrap();
302 if (_err != noErr) return PyMac_Error(_err);
303 Py_INCREF(Py_None);
304 _res = Py_None;
305 return _res;
308 #if TARGET_API_MAC_OS8
310 static PyObject *Scrap_InfoScrap(PyObject *_self, PyObject *_args)
312 PyObject *_res = NULL;
313 ScrapStuffPtr _rv;
314 if (!PyArg_ParseTuple(_args, ""))
315 return NULL;
316 _rv = InfoScrap();
317 _res = Py_BuildValue("O&",
318 SCRRec_New, _rv);
319 return _res;
322 static PyObject *Scrap_GetScrap(PyObject *_self, PyObject *_args)
324 PyObject *_res = NULL;
325 long _rv;
326 Handle destination;
327 ScrapFlavorType flavorType;
328 SInt32 offset;
329 if (!PyArg_ParseTuple(_args, "O&O&",
330 ResObj_Convert, &destination,
331 PyMac_GetOSType, &flavorType))
332 return NULL;
333 _rv = GetScrap(destination,
334 flavorType,
335 &offset);
336 _res = Py_BuildValue("ll",
337 _rv,
338 offset);
339 return _res;
342 static PyObject *Scrap_ZeroScrap(PyObject *_self, PyObject *_args)
344 PyObject *_res = NULL;
345 OSStatus _err;
346 if (!PyArg_ParseTuple(_args, ""))
347 return NULL;
348 _err = ZeroScrap();
349 if (_err != noErr) return PyMac_Error(_err);
350 Py_INCREF(Py_None);
351 _res = Py_None;
352 return _res;
355 static PyObject *Scrap_PutScrap(PyObject *_self, PyObject *_args)
357 PyObject *_res = NULL;
358 OSStatus _err;
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__))
368 return NULL;
369 _err = PutScrap(sourceBufferByteCount,
370 flavorType,
371 sourceBuffer__in__);
372 if (_err != noErr) return PyMac_Error(_err);
373 Py_INCREF(Py_None);
374 _res = Py_None;
375 sourceBuffer__error__: ;
376 return _res;
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;
384 OSStatus _err;
385 ScrapRef scrap;
386 if (!PyArg_ParseTuple(_args, ""))
387 return NULL;
388 _err = GetCurrentScrap(&scrap);
389 if (_err != noErr) return PyMac_Error(_err);
390 _res = Py_BuildValue("O&",
391 ScrapObj_New, scrap);
392 return _res;
395 static PyObject *Scrap_ClearCurrentScrap(PyObject *_self, PyObject *_args)
397 PyObject *_res = NULL;
398 OSStatus _err;
399 if (!PyArg_ParseTuple(_args, ""))
400 return NULL;
401 _err = ClearCurrentScrap();
402 if (_err != noErr) return PyMac_Error(_err);
403 Py_INCREF(Py_None);
404 _res = Py_None;
405 return _res;
408 static PyObject *Scrap_CallInScrapPromises(PyObject *_self, PyObject *_args)
410 PyObject *_res = NULL;
411 OSStatus _err;
412 if (!PyArg_ParseTuple(_args, ""))
413 return NULL;
414 _err = CallInScrapPromises();
415 if (_err != noErr) return PyMac_Error(_err);
416 Py_INCREF(Py_None);
417 _res = Py_None;
418 return _res;
420 #endif
422 static PyMethodDef Scrap_methods[] = {
423 {"LoadScrap", (PyCFunction)Scrap_LoadScrap, 1,
424 "() -> None"},
425 {"UnloadScrap", (PyCFunction)Scrap_UnloadScrap, 1,
426 "() -> None"},
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,
434 "() -> None"},
435 {"PutScrap", (PyCFunction)Scrap_PutScrap, 1,
436 "(SInt32 sourceBufferByteCount, ScrapFlavorType flavorType, Buffer sourceBuffer) -> None"},
437 #endif
439 #if !TARGET_API_MAC_OS8
440 {"GetCurrentScrap", (PyCFunction)Scrap_GetCurrentScrap, 1,
441 "() -> (ScrapRef scrap)"},
442 {"ClearCurrentScrap", (PyCFunction)Scrap_ClearCurrentScrap, 1,
443 "() -> None"},
444 {"CallInScrapPromises", (PyCFunction)Scrap_CallInScrapPromises, 1,
445 "() -> None"},
446 #endif
447 {NULL, NULL, 0}
453 void init_Scrap(void)
455 PyObject *m;
456 PyObject *d;
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)
466 return;
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");
472 #endif
475 /* ======================= End module _Scrap ======================== */