append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Mac / Modules / list / _Listmodule.c
blob99805b6b6bd4064e8c9fc79c08cb5865f439faa5
2 /* ========================== Module _List ========================== */
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 <Lists.h>
25 #else
26 #include <Carbon/Carbon.h>
27 #endif
29 #ifdef USE_TOOLBOX_OBJECT_GLUE
30 extern PyObject *_ListObj_New(ListHandle);
31 extern int _ListObj_Convert(PyObject *, ListHandle *);
33 #define ListObj_New _ListObj_New
34 #define ListObj_Convert _ListObj_Convert
35 #endif
37 #if !ACCESSOR_CALLS_ARE_FUNCTIONS
38 #define GetListPort(list) ((CGrafPtr)(*(list))->port)
39 #define GetListVerticalScrollBar(list) ((*(list))->vScroll)
40 #define GetListHorizontalScrollBar(list) ((*(list))->hScroll)
41 #define GetListActive(list) ((*(list))->lActive)
42 #define GetListClickTime(list) ((*(list))->clikTime)
43 #define GetListRefCon(list) ((*(list))->refCon)
44 #define GetListDefinition(list) ((*(list))->listDefProc) /* XXX Is this indeed the same? */
45 #define GetListUserHandle(list) ((*(list))->userHandle)
46 #define GetListDataHandle(list) ((*(list))->cells)
47 #define GetListFlags(list) ((*(list))->listFlags)
48 #define GetListSelectionFlags(list) ((*(list))->selFlags)
49 #define SetListViewBounds(list, bounds) (((*(list))->rView) = *(bounds))
51 #define SetListPort(list, port) (((*(list))->port) = (GrafPtr)(port))
52 #define SetListCellIndent(list, ind) (((*(list))->indent) = *(ind))
53 #define SetListClickTime(list, time) (((*(list))->clikTime) = (time))
54 #define SetListLastClick(list, click) (((*(list)->lastClick) = *(click))
55 #define SetListRefCon(list, refcon) (((*(list))->refCon) = (refcon))
56 #define SetListUserHandle(list, handle) (((*(list))->userHandle) = (handle))
57 #define SetListFlags(list, flags) (((*(list))->listFlags) = (flags))
58 #define SetListSelectionFlags(list, flags) (((*(list))->selFlags) = (flags))
60 #endif
62 #define as_List(x) ((ListHandle)x)
63 #define as_Resource(lh) ((Handle)lh)
65 static ListDefUPP myListDefFunctionUPP;
67 #if !TARGET_API_MAC_CARBON
69 #define kJumpAbs 0x4EF9
71 #pragma options align=mac68k
72 typedef struct {
73 short jmpabs; /* 4EF9 */
74 ListDefUPP theUPP; /* 00000000 */
75 } LDEFStub, **LDEFStubHandle;
76 #pragma options align=reset
78 static OSErr installLDEFStub(ListHandle list) {
79 LDEFStubHandle stubH;
81 stubH = (LDEFStubHandle)NewHandleClear(sizeof(LDEFStub));
82 if (stubH == NULL)
83 return MemError();
85 (*stubH)->jmpabs = kJumpAbs;
86 (*stubH)->theUPP = myListDefFunctionUPP;
87 HLock((Handle) stubH);
89 (*list)->listDefProc = (Handle)stubH;
90 return noErr;
93 static void removeLDEFStub(ListHandle list) {
94 if ((*list)->listDefProc)
95 DisposeHandle((Handle)(*list)->listDefProc);
96 (*list)->listDefProc = NULL;
99 #endif
101 static PyObject *List_Error;
103 /* ------------------------ Object type List ------------------------ */
105 PyTypeObject List_Type;
107 #define ListObj_Check(x) ((x)->ob_type == &List_Type)
109 typedef struct ListObject {
110 PyObject_HEAD
111 ListHandle ob_itself;
112 PyObject *ob_ldef_func;
113 int ob_have_ldef_stub;
114 int ob_must_be_disposed;
115 } ListObject;
117 PyObject *ListObj_New(ListHandle itself)
119 ListObject *it;
120 if (itself == NULL) {
121 PyErr_SetString(List_Error,"Cannot create null List");
122 return NULL;
124 it = PyObject_NEW(ListObject, &List_Type);
125 if (it == NULL) return NULL;
126 it->ob_itself = itself;
127 it->ob_ldef_func = NULL;
128 it->ob_have_ldef_stub = 0;
129 it->ob_must_be_disposed = 1;
130 SetListRefCon(itself, (long)it);
131 return (PyObject *)it;
133 int ListObj_Convert(PyObject *v, ListHandle *p_itself)
135 if (!ListObj_Check(v))
137 PyErr_SetString(PyExc_TypeError, "List required");
138 return 0;
140 *p_itself = ((ListObject *)v)->ob_itself;
141 return 1;
144 static void ListObj_dealloc(ListObject *self)
146 Py_XDECREF(self->ob_ldef_func);
147 self->ob_ldef_func = NULL;
148 #if !TARGET_API_MAC_CARBON
149 if (self->ob_have_ldef_stub) removeLDEFStub(self->ob_itself);
150 #endif
151 SetListRefCon(self->ob_itself, (long)0);
152 if (self->ob_must_be_disposed && self->ob_itself) LDispose(self->ob_itself);
153 PyObject_Del(self);
156 static PyObject *ListObj_LAddColumn(ListObject *_self, PyObject *_args)
158 PyObject *_res = NULL;
159 short _rv;
160 short count;
161 short colNum;
162 if (!PyArg_ParseTuple(_args, "hh",
163 &count,
164 &colNum))
165 return NULL;
166 _rv = LAddColumn(count,
167 colNum,
168 _self->ob_itself);
169 _res = Py_BuildValue("h",
170 _rv);
171 return _res;
174 static PyObject *ListObj_LAddRow(ListObject *_self, PyObject *_args)
176 PyObject *_res = NULL;
177 short _rv;
178 short count;
179 short rowNum;
180 if (!PyArg_ParseTuple(_args, "hh",
181 &count,
182 &rowNum))
183 return NULL;
184 _rv = LAddRow(count,
185 rowNum,
186 _self->ob_itself);
187 _res = Py_BuildValue("h",
188 _rv);
189 return _res;
192 static PyObject *ListObj_LDelColumn(ListObject *_self, PyObject *_args)
194 PyObject *_res = NULL;
195 short count;
196 short colNum;
197 if (!PyArg_ParseTuple(_args, "hh",
198 &count,
199 &colNum))
200 return NULL;
201 LDelColumn(count,
202 colNum,
203 _self->ob_itself);
204 Py_INCREF(Py_None);
205 _res = Py_None;
206 return _res;
209 static PyObject *ListObj_LDelRow(ListObject *_self, PyObject *_args)
211 PyObject *_res = NULL;
212 short count;
213 short rowNum;
214 if (!PyArg_ParseTuple(_args, "hh",
215 &count,
216 &rowNum))
217 return NULL;
218 LDelRow(count,
219 rowNum,
220 _self->ob_itself);
221 Py_INCREF(Py_None);
222 _res = Py_None;
223 return _res;
226 static PyObject *ListObj_LGetSelect(ListObject *_self, PyObject *_args)
228 PyObject *_res = NULL;
229 Boolean _rv;
230 Boolean next;
231 Point theCell;
232 if (!PyArg_ParseTuple(_args, "bO&",
233 &next,
234 PyMac_GetPoint, &theCell))
235 return NULL;
236 _rv = LGetSelect(next,
237 &theCell,
238 _self->ob_itself);
239 _res = Py_BuildValue("bO&",
240 _rv,
241 PyMac_BuildPoint, theCell);
242 return _res;
245 static PyObject *ListObj_LLastClick(ListObject *_self, PyObject *_args)
247 PyObject *_res = NULL;
248 Point _rv;
249 if (!PyArg_ParseTuple(_args, ""))
250 return NULL;
251 _rv = LLastClick(_self->ob_itself);
252 _res = Py_BuildValue("O&",
253 PyMac_BuildPoint, _rv);
254 return _res;
257 static PyObject *ListObj_LNextCell(ListObject *_self, PyObject *_args)
259 PyObject *_res = NULL;
260 Boolean _rv;
261 Boolean hNext;
262 Boolean vNext;
263 Point theCell;
264 if (!PyArg_ParseTuple(_args, "bbO&",
265 &hNext,
266 &vNext,
267 PyMac_GetPoint, &theCell))
268 return NULL;
269 _rv = LNextCell(hNext,
270 vNext,
271 &theCell,
272 _self->ob_itself);
273 _res = Py_BuildValue("bO&",
274 _rv,
275 PyMac_BuildPoint, theCell);
276 return _res;
279 static PyObject *ListObj_LSize(ListObject *_self, PyObject *_args)
281 PyObject *_res = NULL;
282 short listWidth;
283 short listHeight;
284 if (!PyArg_ParseTuple(_args, "hh",
285 &listWidth,
286 &listHeight))
287 return NULL;
288 LSize(listWidth,
289 listHeight,
290 _self->ob_itself);
291 Py_INCREF(Py_None);
292 _res = Py_None;
293 return _res;
296 static PyObject *ListObj_LSetDrawingMode(ListObject *_self, PyObject *_args)
298 PyObject *_res = NULL;
299 Boolean drawIt;
300 if (!PyArg_ParseTuple(_args, "b",
301 &drawIt))
302 return NULL;
303 LSetDrawingMode(drawIt,
304 _self->ob_itself);
305 Py_INCREF(Py_None);
306 _res = Py_None;
307 return _res;
310 static PyObject *ListObj_LScroll(ListObject *_self, PyObject *_args)
312 PyObject *_res = NULL;
313 short dCols;
314 short dRows;
315 if (!PyArg_ParseTuple(_args, "hh",
316 &dCols,
317 &dRows))
318 return NULL;
319 LScroll(dCols,
320 dRows,
321 _self->ob_itself);
322 Py_INCREF(Py_None);
323 _res = Py_None;
324 return _res;
327 static PyObject *ListObj_LAutoScroll(ListObject *_self, PyObject *_args)
329 PyObject *_res = NULL;
330 if (!PyArg_ParseTuple(_args, ""))
331 return NULL;
332 LAutoScroll(_self->ob_itself);
333 Py_INCREF(Py_None);
334 _res = Py_None;
335 return _res;
338 static PyObject *ListObj_LUpdate(ListObject *_self, PyObject *_args)
340 PyObject *_res = NULL;
341 RgnHandle theRgn;
342 if (!PyArg_ParseTuple(_args, "O&",
343 ResObj_Convert, &theRgn))
344 return NULL;
345 LUpdate(theRgn,
346 _self->ob_itself);
347 Py_INCREF(Py_None);
348 _res = Py_None;
349 return _res;
352 static PyObject *ListObj_LActivate(ListObject *_self, PyObject *_args)
354 PyObject *_res = NULL;
355 Boolean act;
356 if (!PyArg_ParseTuple(_args, "b",
357 &act))
358 return NULL;
359 LActivate(act,
360 _self->ob_itself);
361 Py_INCREF(Py_None);
362 _res = Py_None;
363 return _res;
366 static PyObject *ListObj_LCellSize(ListObject *_self, PyObject *_args)
368 PyObject *_res = NULL;
369 Point cSize;
370 if (!PyArg_ParseTuple(_args, "O&",
371 PyMac_GetPoint, &cSize))
372 return NULL;
373 LCellSize(cSize,
374 _self->ob_itself);
375 Py_INCREF(Py_None);
376 _res = Py_None;
377 return _res;
380 static PyObject *ListObj_LClick(ListObject *_self, PyObject *_args)
382 PyObject *_res = NULL;
383 Boolean _rv;
384 Point pt;
385 EventModifiers modifiers;
386 if (!PyArg_ParseTuple(_args, "O&H",
387 PyMac_GetPoint, &pt,
388 &modifiers))
389 return NULL;
390 _rv = LClick(pt,
391 modifiers,
392 _self->ob_itself);
393 _res = Py_BuildValue("b",
394 _rv);
395 return _res;
398 static PyObject *ListObj_LAddToCell(ListObject *_self, PyObject *_args)
400 PyObject *_res = NULL;
401 char *dataPtr__in__;
402 short dataPtr__len__;
403 int dataPtr__in_len__;
404 Point theCell;
405 if (!PyArg_ParseTuple(_args, "s#O&",
406 &dataPtr__in__, &dataPtr__in_len__,
407 PyMac_GetPoint, &theCell))
408 return NULL;
409 dataPtr__len__ = dataPtr__in_len__;
410 LAddToCell(dataPtr__in__, dataPtr__len__,
411 theCell,
412 _self->ob_itself);
413 Py_INCREF(Py_None);
414 _res = Py_None;
415 return _res;
418 static PyObject *ListObj_LClrCell(ListObject *_self, PyObject *_args)
420 PyObject *_res = NULL;
421 Point theCell;
422 if (!PyArg_ParseTuple(_args, "O&",
423 PyMac_GetPoint, &theCell))
424 return NULL;
425 LClrCell(theCell,
426 _self->ob_itself);
427 Py_INCREF(Py_None);
428 _res = Py_None;
429 return _res;
432 static PyObject *ListObj_LGetCell(ListObject *_self, PyObject *_args)
434 PyObject *_res = NULL;
435 char *dataPtr__out__;
436 short dataPtr__len__;
437 int dataPtr__in_len__;
438 Point theCell;
439 if (!PyArg_ParseTuple(_args, "iO&",
440 &dataPtr__in_len__,
441 PyMac_GetPoint, &theCell))
442 return NULL;
443 if ((dataPtr__out__ = malloc(dataPtr__in_len__)) == NULL)
445 PyErr_NoMemory();
446 goto dataPtr__error__;
448 dataPtr__len__ = dataPtr__in_len__;
449 LGetCell(dataPtr__out__, &dataPtr__len__,
450 theCell,
451 _self->ob_itself);
452 _res = Py_BuildValue("s#",
453 dataPtr__out__, (int)dataPtr__len__);
454 free(dataPtr__out__);
455 dataPtr__error__: ;
456 return _res;
459 static PyObject *ListObj_LRect(ListObject *_self, PyObject *_args)
461 PyObject *_res = NULL;
462 Rect cellRect;
463 Point theCell;
464 if (!PyArg_ParseTuple(_args, "O&",
465 PyMac_GetPoint, &theCell))
466 return NULL;
467 LRect(&cellRect,
468 theCell,
469 _self->ob_itself);
470 _res = Py_BuildValue("O&",
471 PyMac_BuildRect, &cellRect);
472 return _res;
475 static PyObject *ListObj_LSetCell(ListObject *_self, PyObject *_args)
477 PyObject *_res = NULL;
478 char *dataPtr__in__;
479 short dataPtr__len__;
480 int dataPtr__in_len__;
481 Point theCell;
482 if (!PyArg_ParseTuple(_args, "s#O&",
483 &dataPtr__in__, &dataPtr__in_len__,
484 PyMac_GetPoint, &theCell))
485 return NULL;
486 dataPtr__len__ = dataPtr__in_len__;
487 LSetCell(dataPtr__in__, dataPtr__len__,
488 theCell,
489 _self->ob_itself);
490 Py_INCREF(Py_None);
491 _res = Py_None;
492 return _res;
495 static PyObject *ListObj_LSetSelect(ListObject *_self, PyObject *_args)
497 PyObject *_res = NULL;
498 Boolean setIt;
499 Point theCell;
500 if (!PyArg_ParseTuple(_args, "bO&",
501 &setIt,
502 PyMac_GetPoint, &theCell))
503 return NULL;
504 LSetSelect(setIt,
505 theCell,
506 _self->ob_itself);
507 Py_INCREF(Py_None);
508 _res = Py_None;
509 return _res;
512 static PyObject *ListObj_LDraw(ListObject *_self, PyObject *_args)
514 PyObject *_res = NULL;
515 Point theCell;
516 if (!PyArg_ParseTuple(_args, "O&",
517 PyMac_GetPoint, &theCell))
518 return NULL;
519 LDraw(theCell,
520 _self->ob_itself);
521 Py_INCREF(Py_None);
522 _res = Py_None;
523 return _res;
526 static PyObject *ListObj_LGetCellDataLocation(ListObject *_self, PyObject *_args)
528 PyObject *_res = NULL;
529 short offset;
530 short len;
531 Point theCell;
532 if (!PyArg_ParseTuple(_args, "O&",
533 PyMac_GetPoint, &theCell))
534 return NULL;
535 LGetCellDataLocation(&offset,
536 &len,
537 theCell,
538 _self->ob_itself);
539 _res = Py_BuildValue("hh",
540 offset,
541 len);
542 return _res;
545 static PyObject *ListObj_as_Resource(ListObject *_self, PyObject *_args)
547 PyObject *_res = NULL;
548 Handle _rv;
549 if (!PyArg_ParseTuple(_args, ""))
550 return NULL;
551 _rv = as_Resource(_self->ob_itself);
552 _res = Py_BuildValue("O&",
553 ResObj_New, _rv);
554 return _res;
557 static PyMethodDef ListObj_methods[] = {
558 {"LAddColumn", (PyCFunction)ListObj_LAddColumn, 1,
559 PyDoc_STR("(short count, short colNum) -> (short _rv)")},
560 {"LAddRow", (PyCFunction)ListObj_LAddRow, 1,
561 PyDoc_STR("(short count, short rowNum) -> (short _rv)")},
562 {"LDelColumn", (PyCFunction)ListObj_LDelColumn, 1,
563 PyDoc_STR("(short count, short colNum) -> None")},
564 {"LDelRow", (PyCFunction)ListObj_LDelRow, 1,
565 PyDoc_STR("(short count, short rowNum) -> None")},
566 {"LGetSelect", (PyCFunction)ListObj_LGetSelect, 1,
567 PyDoc_STR("(Boolean next, Point theCell) -> (Boolean _rv, Point theCell)")},
568 {"LLastClick", (PyCFunction)ListObj_LLastClick, 1,
569 PyDoc_STR("() -> (Point _rv)")},
570 {"LNextCell", (PyCFunction)ListObj_LNextCell, 1,
571 PyDoc_STR("(Boolean hNext, Boolean vNext, Point theCell) -> (Boolean _rv, Point theCell)")},
572 {"LSize", (PyCFunction)ListObj_LSize, 1,
573 PyDoc_STR("(short listWidth, short listHeight) -> None")},
574 {"LSetDrawingMode", (PyCFunction)ListObj_LSetDrawingMode, 1,
575 PyDoc_STR("(Boolean drawIt) -> None")},
576 {"LScroll", (PyCFunction)ListObj_LScroll, 1,
577 PyDoc_STR("(short dCols, short dRows) -> None")},
578 {"LAutoScroll", (PyCFunction)ListObj_LAutoScroll, 1,
579 PyDoc_STR("() -> None")},
580 {"LUpdate", (PyCFunction)ListObj_LUpdate, 1,
581 PyDoc_STR("(RgnHandle theRgn) -> None")},
582 {"LActivate", (PyCFunction)ListObj_LActivate, 1,
583 PyDoc_STR("(Boolean act) -> None")},
584 {"LCellSize", (PyCFunction)ListObj_LCellSize, 1,
585 PyDoc_STR("(Point cSize) -> None")},
586 {"LClick", (PyCFunction)ListObj_LClick, 1,
587 PyDoc_STR("(Point pt, EventModifiers modifiers) -> (Boolean _rv)")},
588 {"LAddToCell", (PyCFunction)ListObj_LAddToCell, 1,
589 PyDoc_STR("(Buffer dataPtr, Point theCell) -> None")},
590 {"LClrCell", (PyCFunction)ListObj_LClrCell, 1,
591 PyDoc_STR("(Point theCell) -> None")},
592 {"LGetCell", (PyCFunction)ListObj_LGetCell, 1,
593 PyDoc_STR("(Buffer dataPtr, Point theCell) -> (Buffer dataPtr)")},
594 {"LRect", (PyCFunction)ListObj_LRect, 1,
595 PyDoc_STR("(Point theCell) -> (Rect cellRect)")},
596 {"LSetCell", (PyCFunction)ListObj_LSetCell, 1,
597 PyDoc_STR("(Buffer dataPtr, Point theCell) -> None")},
598 {"LSetSelect", (PyCFunction)ListObj_LSetSelect, 1,
599 PyDoc_STR("(Boolean setIt, Point theCell) -> None")},
600 {"LDraw", (PyCFunction)ListObj_LDraw, 1,
601 PyDoc_STR("(Point theCell) -> None")},
602 {"LGetCellDataLocation", (PyCFunction)ListObj_LGetCellDataLocation, 1,
603 PyDoc_STR("(Point theCell) -> (short offset, short len)")},
604 {"as_Resource", (PyCFunction)ListObj_as_Resource, 1,
605 PyDoc_STR("() -> (Handle _rv)")},
606 {NULL, NULL, 0}
609 PyMethodChain ListObj_chain = { ListObj_methods, NULL };
611 static PyObject *ListObj_getattr(ListObject *self, char *name)
614 if ( strcmp(name, "listFlags") == 0 )
615 return Py_BuildValue("l", (long)GetListFlags(self->ob_itself) & 0xff);
616 if ( strcmp(name, "selFlags") == 0 )
617 return Py_BuildValue("l", (long)GetListSelectionFlags(self->ob_itself) & 0xff);
618 if ( strcmp(name, "cellSize") == 0 )
619 return Py_BuildValue("O&", PyMac_BuildPoint, (*self->ob_itself)->cellSize);
621 return Py_FindMethodInChain(&ListObj_chain, (PyObject *)self, name);
624 static int
625 ListObj_setattr(ListObject *self, char *name, PyObject *value)
627 long intval;
628 int err = 0;
630 if ( value == NULL ) {
631 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
632 return -1;
634 if (strcmp(name, "listFlags") == 0 )
635 err = PyArg_Parse(value, "B", &(*self->ob_itself)->listFlags);
636 else if (strcmp(name, "selFlags") == 0 )
637 err = PyArg_Parse(value, "B", &(*self->ob_itself)->selFlags);
638 else if (strcmp(name, "cellSize") == 0 )
639 err = PyArg_Parse(value, "O&", PyMac_GetPoint, &(*self->ob_itself)->cellSize);
640 else
641 PyErr_SetString(PyExc_AttributeError, "No such attribute");
642 if (err) return 0;
643 else return -1;
647 #define ListObj_compare NULL
649 #define ListObj_repr NULL
651 #define ListObj_hash NULL
653 PyTypeObject List_Type = {
654 PyObject_HEAD_INIT(NULL)
655 0, /*ob_size*/
656 "_List.List", /*tp_name*/
657 sizeof(ListObject), /*tp_basicsize*/
658 0, /*tp_itemsize*/
659 /* methods */
660 (destructor) ListObj_dealloc, /*tp_dealloc*/
661 0, /*tp_print*/
662 (getattrfunc) ListObj_getattr, /*tp_getattr*/
663 (setattrfunc) ListObj_setattr, /*tp_setattr*/
664 (cmpfunc) ListObj_compare, /*tp_compare*/
665 (reprfunc) ListObj_repr, /*tp_repr*/
666 (PyNumberMethods *)0, /* tp_as_number */
667 (PySequenceMethods *)0, /* tp_as_sequence */
668 (PyMappingMethods *)0, /* tp_as_mapping */
669 (hashfunc) ListObj_hash, /*tp_hash*/
672 /* ---------------------- End object type List ---------------------- */
675 static PyObject *List_CreateCustomList(PyObject *_self, PyObject *_args)
677 PyObject *_res = NULL;
678 Rect rView;
679 Rect dataBounds;
680 Point cellSize;
682 PyObject *listDefFunc;
683 ListDefSpec theSpec;
684 WindowPtr theWindow;
685 Boolean drawIt;
686 Boolean hasGrow;
687 Boolean scrollHoriz;
688 Boolean scrollVert;
689 ListHandle outList;
691 if (!PyArg_ParseTuple(_args, "O&O&O&(iO)O&bbbb",
692 PyMac_GetRect, &rView,
693 PyMac_GetRect, &dataBounds,
694 PyMac_GetPoint, &cellSize,
695 &theSpec.defType, &listDefFunc,
696 WinObj_Convert, &theWindow,
697 &drawIt,
698 &hasGrow,
699 &scrollHoriz,
700 &scrollVert))
701 return NULL;
704 #if TARGET_API_MAC_CARBON
705 /* Carbon applications use the CreateCustomList API */
706 theSpec.u.userProc = myListDefFunctionUPP;
707 CreateCustomList(&rView,
708 &dataBounds,
709 cellSize,
710 &theSpec,
711 theWindow,
712 drawIt,
713 hasGrow,
714 scrollHoriz,
715 scrollVert,
716 &outList);
718 #else
719 /* pre-Carbon applications set the address in the LDEF
720 to a routine descriptor referring to their list
721 definition routine. */
722 outList = LNew(&rView,
723 &dataBounds,
724 cellSize,
726 theWindow,
727 drawIt, /* XXX must be false */
728 hasGrow,
729 scrollHoriz,
730 scrollVert);
731 if (installLDEFStub(outList) != noErr) {
732 PyErr_SetString(PyExc_MemoryError, "can't create LDEF stub");
733 return NULL;
735 #endif
737 _res = ListObj_New(outList);
738 if (_res == NULL)
739 return NULL;
740 Py_INCREF(listDefFunc);
741 ((ListObject*)_res)->ob_ldef_func = listDefFunc;
742 #if !TARGET_API_MAC_CARBON
743 ((ListObject*)_res)->ob_have_ldef_stub = 1;
744 #endif
745 return _res;
748 static PyObject *List_LNew(PyObject *_self, PyObject *_args)
750 PyObject *_res = NULL;
751 ListHandle _rv;
752 Rect rView;
753 Rect dataBounds;
754 Point cSize;
755 short theProc;
756 WindowPtr theWindow;
757 Boolean drawIt;
758 Boolean hasGrow;
759 Boolean scrollHoriz;
760 Boolean scrollVert;
761 if (!PyArg_ParseTuple(_args, "O&O&O&hO&bbbb",
762 PyMac_GetRect, &rView,
763 PyMac_GetRect, &dataBounds,
764 PyMac_GetPoint, &cSize,
765 &theProc,
766 WinObj_Convert, &theWindow,
767 &drawIt,
768 &hasGrow,
769 &scrollHoriz,
770 &scrollVert))
771 return NULL;
772 _rv = LNew(&rView,
773 &dataBounds,
774 cSize,
775 theProc,
776 theWindow,
777 drawIt,
778 hasGrow,
779 scrollHoriz,
780 scrollVert);
781 _res = Py_BuildValue("O&",
782 ListObj_New, _rv);
783 return _res;
786 static PyObject *List_GetListPort(PyObject *_self, PyObject *_args)
788 PyObject *_res = NULL;
789 CGrafPtr _rv;
790 ListHandle list;
791 if (!PyArg_ParseTuple(_args, "O&",
792 ListObj_Convert, &list))
793 return NULL;
794 _rv = GetListPort(list);
795 _res = Py_BuildValue("O&",
796 GrafObj_New, _rv);
797 return _res;
800 static PyObject *List_GetListVerticalScrollBar(PyObject *_self, PyObject *_args)
802 PyObject *_res = NULL;
803 ControlHandle _rv;
804 ListHandle list;
805 if (!PyArg_ParseTuple(_args, "O&",
806 ListObj_Convert, &list))
807 return NULL;
808 _rv = GetListVerticalScrollBar(list);
809 _res = Py_BuildValue("O&",
810 CtlObj_New, _rv);
811 return _res;
814 static PyObject *List_GetListHorizontalScrollBar(PyObject *_self, PyObject *_args)
816 PyObject *_res = NULL;
817 ControlHandle _rv;
818 ListHandle list;
819 if (!PyArg_ParseTuple(_args, "O&",
820 ListObj_Convert, &list))
821 return NULL;
822 _rv = GetListHorizontalScrollBar(list);
823 _res = Py_BuildValue("O&",
824 CtlObj_New, _rv);
825 return _res;
828 static PyObject *List_GetListActive(PyObject *_self, PyObject *_args)
830 PyObject *_res = NULL;
831 Boolean _rv;
832 ListHandle list;
833 if (!PyArg_ParseTuple(_args, "O&",
834 ListObj_Convert, &list))
835 return NULL;
836 _rv = GetListActive(list);
837 _res = Py_BuildValue("b",
838 _rv);
839 return _res;
842 static PyObject *List_GetListClickTime(PyObject *_self, PyObject *_args)
844 PyObject *_res = NULL;
845 SInt32 _rv;
846 ListHandle list;
847 if (!PyArg_ParseTuple(_args, "O&",
848 ListObj_Convert, &list))
849 return NULL;
850 _rv = GetListClickTime(list);
851 _res = Py_BuildValue("l",
852 _rv);
853 return _res;
856 static PyObject *List_GetListRefCon(PyObject *_self, PyObject *_args)
858 PyObject *_res = NULL;
859 SInt32 _rv;
860 ListHandle list;
861 if (!PyArg_ParseTuple(_args, "O&",
862 ListObj_Convert, &list))
863 return NULL;
864 _rv = GetListRefCon(list);
865 _res = Py_BuildValue("l",
866 _rv);
867 return _res;
870 static PyObject *List_GetListDefinition(PyObject *_self, PyObject *_args)
872 PyObject *_res = NULL;
873 Handle _rv;
874 ListHandle list;
875 if (!PyArg_ParseTuple(_args, "O&",
876 ListObj_Convert, &list))
877 return NULL;
878 _rv = GetListDefinition(list);
879 _res = Py_BuildValue("O&",
880 ResObj_New, _rv);
881 return _res;
884 static PyObject *List_GetListUserHandle(PyObject *_self, PyObject *_args)
886 PyObject *_res = NULL;
887 Handle _rv;
888 ListHandle list;
889 if (!PyArg_ParseTuple(_args, "O&",
890 ListObj_Convert, &list))
891 return NULL;
892 _rv = GetListUserHandle(list);
893 _res = Py_BuildValue("O&",
894 ResObj_New, _rv);
895 return _res;
898 static PyObject *List_GetListDataHandle(PyObject *_self, PyObject *_args)
900 PyObject *_res = NULL;
901 DataHandle _rv;
902 ListHandle list;
903 if (!PyArg_ParseTuple(_args, "O&",
904 ListObj_Convert, &list))
905 return NULL;
906 _rv = GetListDataHandle(list);
907 _res = Py_BuildValue("O&",
908 ResObj_New, _rv);
909 return _res;
912 static PyObject *List_GetListFlags(PyObject *_self, PyObject *_args)
914 PyObject *_res = NULL;
915 OptionBits _rv;
916 ListHandle list;
917 if (!PyArg_ParseTuple(_args, "O&",
918 ListObj_Convert, &list))
919 return NULL;
920 _rv = GetListFlags(list);
921 _res = Py_BuildValue("l",
922 _rv);
923 return _res;
926 static PyObject *List_GetListSelectionFlags(PyObject *_self, PyObject *_args)
928 PyObject *_res = NULL;
929 OptionBits _rv;
930 ListHandle list;
931 if (!PyArg_ParseTuple(_args, "O&",
932 ListObj_Convert, &list))
933 return NULL;
934 _rv = GetListSelectionFlags(list);
935 _res = Py_BuildValue("l",
936 _rv);
937 return _res;
940 static PyObject *List_SetListViewBounds(PyObject *_self, PyObject *_args)
942 PyObject *_res = NULL;
943 ListHandle list;
944 Rect view;
945 if (!PyArg_ParseTuple(_args, "O&O&",
946 ListObj_Convert, &list,
947 PyMac_GetRect, &view))
948 return NULL;
949 SetListViewBounds(list,
950 &view);
951 Py_INCREF(Py_None);
952 _res = Py_None;
953 return _res;
956 static PyObject *List_SetListPort(PyObject *_self, PyObject *_args)
958 PyObject *_res = NULL;
959 ListHandle list;
960 CGrafPtr port;
961 if (!PyArg_ParseTuple(_args, "O&O&",
962 ListObj_Convert, &list,
963 GrafObj_Convert, &port))
964 return NULL;
965 SetListPort(list,
966 port);
967 Py_INCREF(Py_None);
968 _res = Py_None;
969 return _res;
972 static PyObject *List_SetListCellIndent(PyObject *_self, PyObject *_args)
974 PyObject *_res = NULL;
975 ListHandle list;
976 Point indent;
977 if (!PyArg_ParseTuple(_args, "O&O&",
978 ListObj_Convert, &list,
979 PyMac_GetPoint, &indent))
980 return NULL;
981 SetListCellIndent(list,
982 &indent);
983 Py_INCREF(Py_None);
984 _res = Py_None;
985 return _res;
988 static PyObject *List_SetListClickTime(PyObject *_self, PyObject *_args)
990 PyObject *_res = NULL;
991 ListHandle list;
992 SInt32 time;
993 if (!PyArg_ParseTuple(_args, "O&l",
994 ListObj_Convert, &list,
995 &time))
996 return NULL;
997 SetListClickTime(list,
998 time);
999 Py_INCREF(Py_None);
1000 _res = Py_None;
1001 return _res;
1004 static PyObject *List_SetListRefCon(PyObject *_self, PyObject *_args)
1006 PyObject *_res = NULL;
1007 ListHandle list;
1008 SInt32 refCon;
1009 if (!PyArg_ParseTuple(_args, "O&l",
1010 ListObj_Convert, &list,
1011 &refCon))
1012 return NULL;
1013 SetListRefCon(list,
1014 refCon);
1015 Py_INCREF(Py_None);
1016 _res = Py_None;
1017 return _res;
1020 static PyObject *List_SetListUserHandle(PyObject *_self, PyObject *_args)
1022 PyObject *_res = NULL;
1023 ListHandle list;
1024 Handle userHandle;
1025 if (!PyArg_ParseTuple(_args, "O&O&",
1026 ListObj_Convert, &list,
1027 ResObj_Convert, &userHandle))
1028 return NULL;
1029 SetListUserHandle(list,
1030 userHandle);
1031 Py_INCREF(Py_None);
1032 _res = Py_None;
1033 return _res;
1036 static PyObject *List_SetListFlags(PyObject *_self, PyObject *_args)
1038 PyObject *_res = NULL;
1039 ListHandle list;
1040 OptionBits listFlags;
1041 if (!PyArg_ParseTuple(_args, "O&l",
1042 ListObj_Convert, &list,
1043 &listFlags))
1044 return NULL;
1045 SetListFlags(list,
1046 listFlags);
1047 Py_INCREF(Py_None);
1048 _res = Py_None;
1049 return _res;
1052 static PyObject *List_SetListSelectionFlags(PyObject *_self, PyObject *_args)
1054 PyObject *_res = NULL;
1055 ListHandle list;
1056 OptionBits selectionFlags;
1057 if (!PyArg_ParseTuple(_args, "O&l",
1058 ListObj_Convert, &list,
1059 &selectionFlags))
1060 return NULL;
1061 SetListSelectionFlags(list,
1062 selectionFlags);
1063 Py_INCREF(Py_None);
1064 _res = Py_None;
1065 return _res;
1068 static PyObject *List_as_List(PyObject *_self, PyObject *_args)
1070 PyObject *_res = NULL;
1072 Handle h;
1073 ListObject *l;
1074 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
1075 return NULL;
1076 l = (ListObject *)ListObj_New(as_List(h));
1077 l->ob_must_be_disposed = 0;
1078 _res = Py_BuildValue("O", l);
1079 return _res;
1083 static PyMethodDef List_methods[] = {
1084 {"CreateCustomList", (PyCFunction)List_CreateCustomList, 1,
1085 PyDoc_STR("(Rect rView, Rect dataBounds, Point cellSize, ListDefSpec theSpec, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle outList)")},
1086 {"LNew", (PyCFunction)List_LNew, 1,
1087 PyDoc_STR("(Rect rView, Rect dataBounds, Point cSize, short theProc, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle _rv)")},
1088 {"GetListPort", (PyCFunction)List_GetListPort, 1,
1089 PyDoc_STR("(ListHandle list) -> (CGrafPtr _rv)")},
1090 {"GetListVerticalScrollBar", (PyCFunction)List_GetListVerticalScrollBar, 1,
1091 PyDoc_STR("(ListHandle list) -> (ControlHandle _rv)")},
1092 {"GetListHorizontalScrollBar", (PyCFunction)List_GetListHorizontalScrollBar, 1,
1093 PyDoc_STR("(ListHandle list) -> (ControlHandle _rv)")},
1094 {"GetListActive", (PyCFunction)List_GetListActive, 1,
1095 PyDoc_STR("(ListHandle list) -> (Boolean _rv)")},
1096 {"GetListClickTime", (PyCFunction)List_GetListClickTime, 1,
1097 PyDoc_STR("(ListHandle list) -> (SInt32 _rv)")},
1098 {"GetListRefCon", (PyCFunction)List_GetListRefCon, 1,
1099 PyDoc_STR("(ListHandle list) -> (SInt32 _rv)")},
1100 {"GetListDefinition", (PyCFunction)List_GetListDefinition, 1,
1101 PyDoc_STR("(ListHandle list) -> (Handle _rv)")},
1102 {"GetListUserHandle", (PyCFunction)List_GetListUserHandle, 1,
1103 PyDoc_STR("(ListHandle list) -> (Handle _rv)")},
1104 {"GetListDataHandle", (PyCFunction)List_GetListDataHandle, 1,
1105 PyDoc_STR("(ListHandle list) -> (DataHandle _rv)")},
1106 {"GetListFlags", (PyCFunction)List_GetListFlags, 1,
1107 PyDoc_STR("(ListHandle list) -> (OptionBits _rv)")},
1108 {"GetListSelectionFlags", (PyCFunction)List_GetListSelectionFlags, 1,
1109 PyDoc_STR("(ListHandle list) -> (OptionBits _rv)")},
1110 {"SetListViewBounds", (PyCFunction)List_SetListViewBounds, 1,
1111 PyDoc_STR("(ListHandle list, Rect view) -> None")},
1112 {"SetListPort", (PyCFunction)List_SetListPort, 1,
1113 PyDoc_STR("(ListHandle list, CGrafPtr port) -> None")},
1114 {"SetListCellIndent", (PyCFunction)List_SetListCellIndent, 1,
1115 PyDoc_STR("(ListHandle list, Point indent) -> None")},
1116 {"SetListClickTime", (PyCFunction)List_SetListClickTime, 1,
1117 PyDoc_STR("(ListHandle list, SInt32 time) -> None")},
1118 {"SetListRefCon", (PyCFunction)List_SetListRefCon, 1,
1119 PyDoc_STR("(ListHandle list, SInt32 refCon) -> None")},
1120 {"SetListUserHandle", (PyCFunction)List_SetListUserHandle, 1,
1121 PyDoc_STR("(ListHandle list, Handle userHandle) -> None")},
1122 {"SetListFlags", (PyCFunction)List_SetListFlags, 1,
1123 PyDoc_STR("(ListHandle list, OptionBits listFlags) -> None")},
1124 {"SetListSelectionFlags", (PyCFunction)List_SetListSelectionFlags, 1,
1125 PyDoc_STR("(ListHandle list, OptionBits selectionFlags) -> None")},
1126 {"as_List", (PyCFunction)List_as_List, 1,
1127 PyDoc_STR("(Resource)->List.\nReturns List object (which is not auto-freed!)")},
1128 {NULL, NULL, 0}
1133 static void myListDefFunction(SInt16 message,
1134 Boolean selected,
1135 Rect *cellRect,
1136 Cell theCell,
1137 SInt16 dataOffset,
1138 SInt16 dataLen,
1139 ListHandle theList)
1141 PyObject *listDefFunc, *args, *rv=NULL;
1142 ListObject *self;
1144 self = (ListObject*)GetListRefCon(theList);
1145 if (self == NULL || self->ob_itself != theList)
1146 return; /* nothing we can do */
1147 listDefFunc = self->ob_ldef_func;
1148 if (listDefFunc == NULL)
1149 return; /* nothing we can do */
1150 args = Py_BuildValue("hbO&O&hhO", message,
1151 selected,
1152 PyMac_BuildRect, cellRect,
1153 PyMac_BuildPoint, theCell,
1154 dataOffset,
1155 dataLen,
1156 self);
1157 if (args != NULL) {
1158 rv = PyEval_CallObject(listDefFunc, args);
1159 Py_DECREF(args);
1161 if (rv == NULL) {
1162 PySys_WriteStderr("error in list definition callback:\n");
1163 PyErr_Print();
1164 } else {
1165 Py_DECREF(rv);
1170 void init_List(void)
1172 PyObject *m;
1173 PyObject *d;
1177 myListDefFunctionUPP = NewListDefUPP((ListDefProcPtr)myListDefFunction);
1179 PyMac_INIT_TOOLBOX_OBJECT_NEW(ListHandle, ListObj_New);
1180 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ListHandle, ListObj_Convert);
1183 m = Py_InitModule("_List", List_methods);
1184 d = PyModule_GetDict(m);
1185 List_Error = PyMac_GetOSErrException();
1186 if (List_Error == NULL ||
1187 PyDict_SetItemString(d, "Error", List_Error) != 0)
1188 return;
1189 List_Type.ob_type = &PyType_Type;
1190 Py_INCREF(&List_Type);
1191 if (PyDict_SetItemString(d, "ListType", (PyObject *)&List_Type) != 0)
1192 Py_FatalError("can't initialize ListType");
1195 /* ======================== End module _List ======================== */