Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Mac / Modules / list / Listmodule.c
blob0993ad9c66c04d4532fbe804e596a4e257d4967e
2 /* ========================== Module List =========================== */
4 #include "Python.h"
8 #define SystemSevenOrLater 1
10 #include "macglue.h"
11 #include <Memory.h>
12 #include <Dialogs.h>
13 #include <Menus.h>
14 #include <Controls.h>
16 extern PyObject *ResObj_New(Handle);
17 extern int ResObj_Convert(PyObject *, Handle *);
18 extern PyObject *OptResObj_New(Handle);
19 extern int OptResObj_Convert(PyObject *, Handle *);
21 extern PyObject *WinObj_New(WindowPtr);
22 extern int WinObj_Convert(PyObject *, WindowPtr *);
23 extern PyTypeObject Window_Type;
24 #define WinObj_Check(x) ((x)->ob_type == &Window_Type)
26 extern PyObject *DlgObj_New(DialogPtr);
27 extern int DlgObj_Convert(PyObject *, DialogPtr *);
28 extern PyTypeObject Dialog_Type;
29 #define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type)
31 extern PyObject *MenuObj_New(MenuHandle);
32 extern int MenuObj_Convert(PyObject *, MenuHandle *);
34 extern PyObject *CtlObj_New(ControlHandle);
35 extern int CtlObj_Convert(PyObject *, ControlHandle *);
37 extern PyObject *GrafObj_New(GrafPtr);
38 extern int GrafObj_Convert(PyObject *, GrafPtr *);
40 extern PyObject *BMObj_New(BitMapPtr);
41 extern int BMObj_Convert(PyObject *, BitMapPtr *);
43 extern PyObject *WinObj_WhichWindow(WindowPtr);
45 #include <Lists.h>
47 #define as_List(x) ((ListHandle)x)
48 #define as_Resource(lh) ((Handle)lh)
50 static PyObject *List_Error;
52 /* ------------------------ Object type List ------------------------ */
54 PyTypeObject List_Type;
56 #define ListObj_Check(x) ((x)->ob_type == &List_Type)
58 typedef struct ListObject {
59 PyObject_HEAD
60 ListHandle ob_itself;
61 int ob_must_be_disposed;
62 } ListObject;
64 PyObject *ListObj_New(itself)
65 ListHandle itself;
67 ListObject *it;
68 if (itself == NULL) {
69 PyErr_SetString(List_Error,"Cannot create null List");
70 return NULL;
72 it = PyObject_NEW(ListObject, &List_Type);
73 if (it == NULL) return NULL;
74 it->ob_itself = itself;
75 it->ob_must_be_disposed = 1;
76 return (PyObject *)it;
78 ListObj_Convert(v, p_itself)
79 PyObject *v;
80 ListHandle *p_itself;
82 if (!ListObj_Check(v))
84 PyErr_SetString(PyExc_TypeError, "List required");
85 return 0;
87 *p_itself = ((ListObject *)v)->ob_itself;
88 return 1;
91 static void ListObj_dealloc(self)
92 ListObject *self;
94 if (self->ob_must_be_disposed && self->ob_itself) LDispose(self->ob_itself);
95 PyMem_DEL(self);
98 static PyObject *ListObj_LAddColumn(_self, _args)
99 ListObject *_self;
100 PyObject *_args;
102 PyObject *_res = NULL;
103 short _rv;
104 short count;
105 short colNum;
106 if (!PyArg_ParseTuple(_args, "hh",
107 &count,
108 &colNum))
109 return NULL;
110 _rv = LAddColumn(count,
111 colNum,
112 _self->ob_itself);
113 _res = Py_BuildValue("h",
114 _rv);
115 return _res;
118 static PyObject *ListObj_LAddRow(_self, _args)
119 ListObject *_self;
120 PyObject *_args;
122 PyObject *_res = NULL;
123 short _rv;
124 short count;
125 short rowNum;
126 if (!PyArg_ParseTuple(_args, "hh",
127 &count,
128 &rowNum))
129 return NULL;
130 _rv = LAddRow(count,
131 rowNum,
132 _self->ob_itself);
133 _res = Py_BuildValue("h",
134 _rv);
135 return _res;
138 static PyObject *ListObj_LDelColumn(_self, _args)
139 ListObject *_self;
140 PyObject *_args;
142 PyObject *_res = NULL;
143 short count;
144 short colNum;
145 if (!PyArg_ParseTuple(_args, "hh",
146 &count,
147 &colNum))
148 return NULL;
149 LDelColumn(count,
150 colNum,
151 _self->ob_itself);
152 Py_INCREF(Py_None);
153 _res = Py_None;
154 return _res;
157 static PyObject *ListObj_LDelRow(_self, _args)
158 ListObject *_self;
159 PyObject *_args;
161 PyObject *_res = NULL;
162 short count;
163 short rowNum;
164 if (!PyArg_ParseTuple(_args, "hh",
165 &count,
166 &rowNum))
167 return NULL;
168 LDelRow(count,
169 rowNum,
170 _self->ob_itself);
171 Py_INCREF(Py_None);
172 _res = Py_None;
173 return _res;
176 static PyObject *ListObj_LGetSelect(_self, _args)
177 ListObject *_self;
178 PyObject *_args;
180 PyObject *_res = NULL;
181 Boolean _rv;
182 Boolean next;
183 Point theCell;
184 if (!PyArg_ParseTuple(_args, "bO&",
185 &next,
186 PyMac_GetPoint, &theCell))
187 return NULL;
188 _rv = LGetSelect(next,
189 &theCell,
190 _self->ob_itself);
191 _res = Py_BuildValue("bO&",
192 _rv,
193 PyMac_BuildPoint, theCell);
194 return _res;
197 static PyObject *ListObj_LLastClick(_self, _args)
198 ListObject *_self;
199 PyObject *_args;
201 PyObject *_res = NULL;
202 Point _rv;
203 if (!PyArg_ParseTuple(_args, ""))
204 return NULL;
205 _rv = LLastClick(_self->ob_itself);
206 _res = Py_BuildValue("O&",
207 PyMac_BuildPoint, _rv);
208 return _res;
211 static PyObject *ListObj_LNextCell(_self, _args)
212 ListObject *_self;
213 PyObject *_args;
215 PyObject *_res = NULL;
216 Boolean _rv;
217 Boolean hNext;
218 Boolean vNext;
219 Point theCell;
220 if (!PyArg_ParseTuple(_args, "bbO&",
221 &hNext,
222 &vNext,
223 PyMac_GetPoint, &theCell))
224 return NULL;
225 _rv = LNextCell(hNext,
226 vNext,
227 &theCell,
228 _self->ob_itself);
229 _res = Py_BuildValue("bO&",
230 _rv,
231 PyMac_BuildPoint, theCell);
232 return _res;
235 static PyObject *ListObj_LSize(_self, _args)
236 ListObject *_self;
237 PyObject *_args;
239 PyObject *_res = NULL;
240 short listWidth;
241 short listHeight;
242 if (!PyArg_ParseTuple(_args, "hh",
243 &listWidth,
244 &listHeight))
245 return NULL;
246 LSize(listWidth,
247 listHeight,
248 _self->ob_itself);
249 Py_INCREF(Py_None);
250 _res = Py_None;
251 return _res;
254 static PyObject *ListObj_LSetDrawingMode(_self, _args)
255 ListObject *_self;
256 PyObject *_args;
258 PyObject *_res = NULL;
259 Boolean drawIt;
260 if (!PyArg_ParseTuple(_args, "b",
261 &drawIt))
262 return NULL;
263 LSetDrawingMode(drawIt,
264 _self->ob_itself);
265 Py_INCREF(Py_None);
266 _res = Py_None;
267 return _res;
270 static PyObject *ListObj_LScroll(_self, _args)
271 ListObject *_self;
272 PyObject *_args;
274 PyObject *_res = NULL;
275 short dCols;
276 short dRows;
277 if (!PyArg_ParseTuple(_args, "hh",
278 &dCols,
279 &dRows))
280 return NULL;
281 LScroll(dCols,
282 dRows,
283 _self->ob_itself);
284 Py_INCREF(Py_None);
285 _res = Py_None;
286 return _res;
289 static PyObject *ListObj_LAutoScroll(_self, _args)
290 ListObject *_self;
291 PyObject *_args;
293 PyObject *_res = NULL;
294 if (!PyArg_ParseTuple(_args, ""))
295 return NULL;
296 LAutoScroll(_self->ob_itself);
297 Py_INCREF(Py_None);
298 _res = Py_None;
299 return _res;
302 static PyObject *ListObj_LUpdate(_self, _args)
303 ListObject *_self;
304 PyObject *_args;
306 PyObject *_res = NULL;
307 RgnHandle theRgn;
308 if (!PyArg_ParseTuple(_args, "O&",
309 ResObj_Convert, &theRgn))
310 return NULL;
311 LUpdate(theRgn,
312 _self->ob_itself);
313 Py_INCREF(Py_None);
314 _res = Py_None;
315 return _res;
318 static PyObject *ListObj_LActivate(_self, _args)
319 ListObject *_self;
320 PyObject *_args;
322 PyObject *_res = NULL;
323 Boolean act;
324 if (!PyArg_ParseTuple(_args, "b",
325 &act))
326 return NULL;
327 LActivate(act,
328 _self->ob_itself);
329 Py_INCREF(Py_None);
330 _res = Py_None;
331 return _res;
334 static PyObject *ListObj_LCellSize(_self, _args)
335 ListObject *_self;
336 PyObject *_args;
338 PyObject *_res = NULL;
339 Point cSize;
340 if (!PyArg_ParseTuple(_args, "O&",
341 PyMac_GetPoint, &cSize))
342 return NULL;
343 LCellSize(cSize,
344 _self->ob_itself);
345 Py_INCREF(Py_None);
346 _res = Py_None;
347 return _res;
350 static PyObject *ListObj_LClick(_self, _args)
351 ListObject *_self;
352 PyObject *_args;
354 PyObject *_res = NULL;
355 Boolean _rv;
356 Point pt;
357 short modifiers;
358 if (!PyArg_ParseTuple(_args, "O&h",
359 PyMac_GetPoint, &pt,
360 &modifiers))
361 return NULL;
362 _rv = LClick(pt,
363 modifiers,
364 _self->ob_itself);
365 _res = Py_BuildValue("b",
366 _rv);
367 return _res;
370 static PyObject *ListObj_LAddToCell(_self, _args)
371 ListObject *_self;
372 PyObject *_args;
374 PyObject *_res = NULL;
375 char *dataPtr__in__;
376 short dataPtr__len__;
377 int dataPtr__in_len__;
378 Point theCell;
379 if (!PyArg_ParseTuple(_args, "s#O&",
380 &dataPtr__in__, &dataPtr__in_len__,
381 PyMac_GetPoint, &theCell))
382 return NULL;
383 dataPtr__len__ = dataPtr__in_len__;
384 LAddToCell(dataPtr__in__, dataPtr__len__,
385 theCell,
386 _self->ob_itself);
387 Py_INCREF(Py_None);
388 _res = Py_None;
389 dataPtr__error__: ;
390 return _res;
393 static PyObject *ListObj_LClrCell(_self, _args)
394 ListObject *_self;
395 PyObject *_args;
397 PyObject *_res = NULL;
398 Point theCell;
399 if (!PyArg_ParseTuple(_args, "O&",
400 PyMac_GetPoint, &theCell))
401 return NULL;
402 LClrCell(theCell,
403 _self->ob_itself);
404 Py_INCREF(Py_None);
405 _res = Py_None;
406 return _res;
409 static PyObject *ListObj_LGetCell(_self, _args)
410 ListObject *_self;
411 PyObject *_args;
413 PyObject *_res = NULL;
414 char *dataPtr__out__;
415 short dataPtr__len__;
416 int dataPtr__in_len__;
417 Point theCell;
418 if (!PyArg_ParseTuple(_args, "iO&",
419 &dataPtr__in_len__,
420 PyMac_GetPoint, &theCell))
421 return NULL;
422 if ((dataPtr__out__ = malloc(dataPtr__in_len__)) == NULL)
424 PyErr_NoMemory();
425 goto dataPtr__error__;
427 dataPtr__len__ = dataPtr__in_len__;
428 LGetCell(dataPtr__out__, &dataPtr__len__,
429 theCell,
430 _self->ob_itself);
431 _res = Py_BuildValue("s#",
432 dataPtr__out__, (int)dataPtr__len__);
433 free(dataPtr__out__);
434 dataPtr__error__: ;
435 return _res;
438 static PyObject *ListObj_LRect(_self, _args)
439 ListObject *_self;
440 PyObject *_args;
442 PyObject *_res = NULL;
443 Rect cellRect;
444 Point theCell;
445 if (!PyArg_ParseTuple(_args, "O&",
446 PyMac_GetPoint, &theCell))
447 return NULL;
448 LRect(&cellRect,
449 theCell,
450 _self->ob_itself);
451 _res = Py_BuildValue("O&",
452 PyMac_BuildRect, &cellRect);
453 return _res;
456 static PyObject *ListObj_LSetCell(_self, _args)
457 ListObject *_self;
458 PyObject *_args;
460 PyObject *_res = NULL;
461 char *dataPtr__in__;
462 short dataPtr__len__;
463 int dataPtr__in_len__;
464 Point theCell;
465 if (!PyArg_ParseTuple(_args, "s#O&",
466 &dataPtr__in__, &dataPtr__in_len__,
467 PyMac_GetPoint, &theCell))
468 return NULL;
469 dataPtr__len__ = dataPtr__in_len__;
470 LSetCell(dataPtr__in__, dataPtr__len__,
471 theCell,
472 _self->ob_itself);
473 Py_INCREF(Py_None);
474 _res = Py_None;
475 dataPtr__error__: ;
476 return _res;
479 static PyObject *ListObj_LSetSelect(_self, _args)
480 ListObject *_self;
481 PyObject *_args;
483 PyObject *_res = NULL;
484 Boolean setIt;
485 Point theCell;
486 if (!PyArg_ParseTuple(_args, "bO&",
487 &setIt,
488 PyMac_GetPoint, &theCell))
489 return NULL;
490 LSetSelect(setIt,
491 theCell,
492 _self->ob_itself);
493 Py_INCREF(Py_None);
494 _res = Py_None;
495 return _res;
498 static PyObject *ListObj_LDraw(_self, _args)
499 ListObject *_self;
500 PyObject *_args;
502 PyObject *_res = NULL;
503 Point theCell;
504 if (!PyArg_ParseTuple(_args, "O&",
505 PyMac_GetPoint, &theCell))
506 return NULL;
507 LDraw(theCell,
508 _self->ob_itself);
509 Py_INCREF(Py_None);
510 _res = Py_None;
511 return _res;
514 static PyObject *ListObj_as_Resource(_self, _args)
515 ListObject *_self;
516 PyObject *_args;
518 PyObject *_res = NULL;
519 Handle _rv;
520 if (!PyArg_ParseTuple(_args, ""))
521 return NULL;
522 _rv = as_Resource(_self->ob_itself);
523 _res = Py_BuildValue("O&",
524 ResObj_New, _rv);
525 return _res;
528 static PyMethodDef ListObj_methods[] = {
529 {"LAddColumn", (PyCFunction)ListObj_LAddColumn, 1,
530 "(short count, short colNum) -> (short _rv)"},
531 {"LAddRow", (PyCFunction)ListObj_LAddRow, 1,
532 "(short count, short rowNum) -> (short _rv)"},
533 {"LDelColumn", (PyCFunction)ListObj_LDelColumn, 1,
534 "(short count, short colNum) -> None"},
535 {"LDelRow", (PyCFunction)ListObj_LDelRow, 1,
536 "(short count, short rowNum) -> None"},
537 {"LGetSelect", (PyCFunction)ListObj_LGetSelect, 1,
538 "(Boolean next, Point theCell) -> (Boolean _rv, Point theCell)"},
539 {"LLastClick", (PyCFunction)ListObj_LLastClick, 1,
540 "() -> (Point _rv)"},
541 {"LNextCell", (PyCFunction)ListObj_LNextCell, 1,
542 "(Boolean hNext, Boolean vNext, Point theCell) -> (Boolean _rv, Point theCell)"},
543 {"LSize", (PyCFunction)ListObj_LSize, 1,
544 "(short listWidth, short listHeight) -> None"},
545 {"LSetDrawingMode", (PyCFunction)ListObj_LSetDrawingMode, 1,
546 "(Boolean drawIt) -> None"},
547 {"LScroll", (PyCFunction)ListObj_LScroll, 1,
548 "(short dCols, short dRows) -> None"},
549 {"LAutoScroll", (PyCFunction)ListObj_LAutoScroll, 1,
550 "() -> None"},
551 {"LUpdate", (PyCFunction)ListObj_LUpdate, 1,
552 "(RgnHandle theRgn) -> None"},
553 {"LActivate", (PyCFunction)ListObj_LActivate, 1,
554 "(Boolean act) -> None"},
555 {"LCellSize", (PyCFunction)ListObj_LCellSize, 1,
556 "(Point cSize) -> None"},
557 {"LClick", (PyCFunction)ListObj_LClick, 1,
558 "(Point pt, short modifiers) -> (Boolean _rv)"},
559 {"LAddToCell", (PyCFunction)ListObj_LAddToCell, 1,
560 "(Buffer dataPtr, Point theCell) -> None"},
561 {"LClrCell", (PyCFunction)ListObj_LClrCell, 1,
562 "(Point theCell) -> None"},
563 {"LGetCell", (PyCFunction)ListObj_LGetCell, 1,
564 "(Buffer dataPtr, Point theCell) -> (Buffer dataPtr)"},
565 {"LRect", (PyCFunction)ListObj_LRect, 1,
566 "(Point theCell) -> (Rect cellRect)"},
567 {"LSetCell", (PyCFunction)ListObj_LSetCell, 1,
568 "(Buffer dataPtr, Point theCell) -> None"},
569 {"LSetSelect", (PyCFunction)ListObj_LSetSelect, 1,
570 "(Boolean setIt, Point theCell) -> None"},
571 {"LDraw", (PyCFunction)ListObj_LDraw, 1,
572 "(Point theCell) -> None"},
573 {"as_Resource", (PyCFunction)ListObj_as_Resource, 1,
574 "() -> (Handle _rv)"},
575 {NULL, NULL, 0}
578 PyMethodChain ListObj_chain = { ListObj_methods, NULL };
580 static PyObject *ListObj_getattr(self, name)
581 ListObject *self;
582 char *name;
585 /* XXXX Should we HLock() here?? */
586 if ( strcmp(name, "listFlags") == 0 )
587 return Py_BuildValue("l", (long)(*self->ob_itself)->listFlags & 0xff);
588 if ( strcmp(name, "selFlags") == 0 )
589 return Py_BuildValue("l", (long)(*self->ob_itself)->selFlags & 0xff);
591 return Py_FindMethodInChain(&ListObj_chain, (PyObject *)self, name);
594 static int
595 ListObj_setattr(self, name, value)
596 ListObject *self;
597 char *name;
598 PyObject *value;
600 long intval;
602 if ( value == NULL || !PyInt_Check(value) )
603 return -1;
604 intval = PyInt_AsLong(value);
605 if (strcmp(name, "listFlags") == 0 ) {
606 /* XXXX Should we HLock the handle here?? */
607 (*self->ob_itself)->listFlags = intval;
608 return 0;
610 if (strcmp(name, "selFlags") == 0 ) {
611 (*self->ob_itself)->selFlags = intval;
612 return 0;
614 return -1;
618 #define ListObj_compare NULL
620 #define ListObj_repr NULL
622 #define ListObj_hash NULL
624 PyTypeObject List_Type = {
625 PyObject_HEAD_INIT(&PyType_Type)
626 0, /*ob_size*/
627 "List", /*tp_name*/
628 sizeof(ListObject), /*tp_basicsize*/
629 0, /*tp_itemsize*/
630 /* methods */
631 (destructor) ListObj_dealloc, /*tp_dealloc*/
632 0, /*tp_print*/
633 (getattrfunc) ListObj_getattr, /*tp_getattr*/
634 (setattrfunc) ListObj_setattr, /*tp_setattr*/
635 (cmpfunc) ListObj_compare, /*tp_compare*/
636 (reprfunc) ListObj_repr, /*tp_repr*/
637 (PyNumberMethods *)0, /* tp_as_number */
638 (PySequenceMethods *)0, /* tp_as_sequence */
639 (PyMappingMethods *)0, /* tp_as_mapping */
640 (hashfunc) ListObj_hash, /*tp_hash*/
643 /* ---------------------- End object type List ---------------------- */
646 static PyObject *List_LNew(_self, _args)
647 PyObject *_self;
648 PyObject *_args;
650 PyObject *_res = NULL;
651 ListHandle _rv;
652 Rect rView;
653 Rect dataBounds;
654 Point cSize;
655 short theProc;
656 WindowPtr theWindow;
657 Boolean drawIt;
658 Boolean hasGrow;
659 Boolean scrollHoriz;
660 Boolean scrollVert;
661 if (!PyArg_ParseTuple(_args, "O&O&O&hO&bbbb",
662 PyMac_GetRect, &rView,
663 PyMac_GetRect, &dataBounds,
664 PyMac_GetPoint, &cSize,
665 &theProc,
666 WinObj_Convert, &theWindow,
667 &drawIt,
668 &hasGrow,
669 &scrollHoriz,
670 &scrollVert))
671 return NULL;
672 _rv = LNew(&rView,
673 &dataBounds,
674 cSize,
675 theProc,
676 theWindow,
677 drawIt,
678 hasGrow,
679 scrollHoriz,
680 scrollVert);
681 _res = Py_BuildValue("O&",
682 ListObj_New, _rv);
683 return _res;
686 static PyObject *List_as_List(_self, _args)
687 PyObject *_self;
688 PyObject *_args;
690 PyObject *_res = NULL;
692 Handle h;
693 ListObject *l;
694 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
695 return NULL;
696 l = (ListObject *)ListObj_New(as_List(h));
697 l->ob_must_be_disposed = 0;
698 return Py_BuildValue("O", l);
702 static PyMethodDef List_methods[] = {
703 {"LNew", (PyCFunction)List_LNew, 1,
704 "(Rect rView, Rect dataBounds, Point cSize, short theProc, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle _rv)"},
705 {"as_List", (PyCFunction)List_as_List, 1,
706 "(Resource)->List.\nReturns List object (which is not auto-freed!)"},
707 {NULL, NULL, 0}
713 void initList()
715 PyObject *m;
716 PyObject *d;
721 m = Py_InitModule("List", List_methods);
722 d = PyModule_GetDict(m);
723 List_Error = PyMac_GetOSErrException();
724 if (List_Error == NULL ||
725 PyDict_SetItemString(d, "Error", List_Error) != 0)
726 Py_FatalError("can't initialize List.Error");
727 List_Type.ob_type = &PyType_Type;
728 Py_INCREF(&List_Type);
729 if (PyDict_SetItemString(d, "ListType", (PyObject *)&List_Type) != 0)
730 Py_FatalError("can't initialize ListType");
733 /* ======================== End module List ========================= */