Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Mac / Modules / list / Listmodule.c
blob9b09341af73738c33237933c0c60e1142f7ed766
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 static PyObject *List_Error;
49 /* ------------------------ Object type List ------------------------ */
51 PyTypeObject List_Type;
53 #define ListObj_Check(x) ((x)->ob_type == &List_Type)
55 typedef struct ListObject {
56 PyObject_HEAD
57 ListHandle ob_itself;
58 } ListObject;
60 PyObject *ListObj_New(itself)
61 ListHandle itself;
63 ListObject *it;
64 if (itself == NULL) {
65 PyErr_SetString(List_Error,"Cannot create null List");
66 return NULL;
68 it = PyObject_NEW(ListObject, &List_Type);
69 if (it == NULL) return NULL;
70 it->ob_itself = itself;
71 return (PyObject *)it;
73 ListObj_Convert(v, p_itself)
74 PyObject *v;
75 ListHandle *p_itself;
77 if (!ListObj_Check(v))
79 PyErr_SetString(PyExc_TypeError, "List required");
80 return 0;
82 *p_itself = ((ListObject *)v)->ob_itself;
83 return 1;
86 static void ListObj_dealloc(self)
87 ListObject *self;
89 LDispose(self->ob_itself);
90 PyMem_DEL(self);
93 static PyObject *ListObj_LAddColumn(_self, _args)
94 ListObject *_self;
95 PyObject *_args;
97 PyObject *_res = NULL;
98 short _rv;
99 short count;
100 short colNum;
101 if (!PyArg_ParseTuple(_args, "hh",
102 &count,
103 &colNum))
104 return NULL;
105 _rv = LAddColumn(count,
106 colNum,
107 _self->ob_itself);
108 _res = Py_BuildValue("h",
109 _rv);
110 return _res;
113 static PyObject *ListObj_LAddRow(_self, _args)
114 ListObject *_self;
115 PyObject *_args;
117 PyObject *_res = NULL;
118 short _rv;
119 short count;
120 short rowNum;
121 if (!PyArg_ParseTuple(_args, "hh",
122 &count,
123 &rowNum))
124 return NULL;
125 _rv = LAddRow(count,
126 rowNum,
127 _self->ob_itself);
128 _res = Py_BuildValue("h",
129 _rv);
130 return _res;
133 static PyObject *ListObj_LDelColumn(_self, _args)
134 ListObject *_self;
135 PyObject *_args;
137 PyObject *_res = NULL;
138 short count;
139 short colNum;
140 if (!PyArg_ParseTuple(_args, "hh",
141 &count,
142 &colNum))
143 return NULL;
144 LDelColumn(count,
145 colNum,
146 _self->ob_itself);
147 Py_INCREF(Py_None);
148 _res = Py_None;
149 return _res;
152 static PyObject *ListObj_LDelRow(_self, _args)
153 ListObject *_self;
154 PyObject *_args;
156 PyObject *_res = NULL;
157 short count;
158 short rowNum;
159 if (!PyArg_ParseTuple(_args, "hh",
160 &count,
161 &rowNum))
162 return NULL;
163 LDelRow(count,
164 rowNum,
165 _self->ob_itself);
166 Py_INCREF(Py_None);
167 _res = Py_None;
168 return _res;
171 static PyObject *ListObj_LGetSelect(_self, _args)
172 ListObject *_self;
173 PyObject *_args;
175 PyObject *_res = NULL;
176 Boolean _rv;
177 Boolean next;
178 Point theCell;
179 if (!PyArg_ParseTuple(_args, "bO&",
180 &next,
181 PyMac_GetPoint, &theCell))
182 return NULL;
183 _rv = LGetSelect(next,
184 &theCell,
185 _self->ob_itself);
186 _res = Py_BuildValue("bO&",
187 _rv,
188 PyMac_BuildPoint, theCell);
189 return _res;
192 static PyObject *ListObj_LLastClick(_self, _args)
193 ListObject *_self;
194 PyObject *_args;
196 PyObject *_res = NULL;
197 Point _rv;
198 if (!PyArg_ParseTuple(_args, ""))
199 return NULL;
200 _rv = LLastClick(_self->ob_itself);
201 _res = Py_BuildValue("O&",
202 PyMac_BuildPoint, _rv);
203 return _res;
206 static PyObject *ListObj_LNextCell(_self, _args)
207 ListObject *_self;
208 PyObject *_args;
210 PyObject *_res = NULL;
211 Boolean _rv;
212 Boolean hNext;
213 Boolean vNext;
214 Point theCell;
215 if (!PyArg_ParseTuple(_args, "bbO&",
216 &hNext,
217 &vNext,
218 PyMac_GetPoint, &theCell))
219 return NULL;
220 _rv = LNextCell(hNext,
221 vNext,
222 &theCell,
223 _self->ob_itself);
224 _res = Py_BuildValue("bO&",
225 _rv,
226 PyMac_BuildPoint, theCell);
227 return _res;
230 static PyObject *ListObj_LSize(_self, _args)
231 ListObject *_self;
232 PyObject *_args;
234 PyObject *_res = NULL;
235 short listWidth;
236 short listHeight;
237 if (!PyArg_ParseTuple(_args, "hh",
238 &listWidth,
239 &listHeight))
240 return NULL;
241 LSize(listWidth,
242 listHeight,
243 _self->ob_itself);
244 Py_INCREF(Py_None);
245 _res = Py_None;
246 return _res;
249 static PyObject *ListObj_LSetDrawingMode(_self, _args)
250 ListObject *_self;
251 PyObject *_args;
253 PyObject *_res = NULL;
254 Boolean drawIt;
255 if (!PyArg_ParseTuple(_args, "b",
256 &drawIt))
257 return NULL;
258 LSetDrawingMode(drawIt,
259 _self->ob_itself);
260 Py_INCREF(Py_None);
261 _res = Py_None;
262 return _res;
265 static PyObject *ListObj_LScroll(_self, _args)
266 ListObject *_self;
267 PyObject *_args;
269 PyObject *_res = NULL;
270 short dCols;
271 short dRows;
272 if (!PyArg_ParseTuple(_args, "hh",
273 &dCols,
274 &dRows))
275 return NULL;
276 LScroll(dCols,
277 dRows,
278 _self->ob_itself);
279 Py_INCREF(Py_None);
280 _res = Py_None;
281 return _res;
284 static PyObject *ListObj_LAutoScroll(_self, _args)
285 ListObject *_self;
286 PyObject *_args;
288 PyObject *_res = NULL;
289 if (!PyArg_ParseTuple(_args, ""))
290 return NULL;
291 LAutoScroll(_self->ob_itself);
292 Py_INCREF(Py_None);
293 _res = Py_None;
294 return _res;
297 static PyObject *ListObj_LUpdate(_self, _args)
298 ListObject *_self;
299 PyObject *_args;
301 PyObject *_res = NULL;
302 RgnHandle theRgn;
303 if (!PyArg_ParseTuple(_args, "O&",
304 ResObj_Convert, &theRgn))
305 return NULL;
306 LUpdate(theRgn,
307 _self->ob_itself);
308 Py_INCREF(Py_None);
309 _res = Py_None;
310 return _res;
313 static PyObject *ListObj_LActivate(_self, _args)
314 ListObject *_self;
315 PyObject *_args;
317 PyObject *_res = NULL;
318 Boolean act;
319 if (!PyArg_ParseTuple(_args, "b",
320 &act))
321 return NULL;
322 LActivate(act,
323 _self->ob_itself);
324 Py_INCREF(Py_None);
325 _res = Py_None;
326 return _res;
329 static PyObject *ListObj_LCellSize(_self, _args)
330 ListObject *_self;
331 PyObject *_args;
333 PyObject *_res = NULL;
334 Point cSize;
335 if (!PyArg_ParseTuple(_args, "O&",
336 PyMac_GetPoint, &cSize))
337 return NULL;
338 LCellSize(cSize,
339 _self->ob_itself);
340 Py_INCREF(Py_None);
341 _res = Py_None;
342 return _res;
345 static PyObject *ListObj_LClick(_self, _args)
346 ListObject *_self;
347 PyObject *_args;
349 PyObject *_res = NULL;
350 Boolean _rv;
351 Point pt;
352 short modifiers;
353 if (!PyArg_ParseTuple(_args, "O&h",
354 PyMac_GetPoint, &pt,
355 &modifiers))
356 return NULL;
357 _rv = LClick(pt,
358 modifiers,
359 _self->ob_itself);
360 _res = Py_BuildValue("b",
361 _rv);
362 return _res;
365 static PyObject *ListObj_LAddToCell(_self, _args)
366 ListObject *_self;
367 PyObject *_args;
369 PyObject *_res = NULL;
370 char *dataPtr__in__;
371 short dataPtr__len__;
372 int dataPtr__in_len__;
373 Point theCell;
374 if (!PyArg_ParseTuple(_args, "s#O&",
375 &dataPtr__in__, &dataPtr__in_len__,
376 PyMac_GetPoint, &theCell))
377 return NULL;
378 dataPtr__len__ = dataPtr__in_len__;
379 LAddToCell(dataPtr__in__, dataPtr__len__,
380 theCell,
381 _self->ob_itself);
382 Py_INCREF(Py_None);
383 _res = Py_None;
384 dataPtr__error__: ;
385 return _res;
388 static PyObject *ListObj_LClrCell(_self, _args)
389 ListObject *_self;
390 PyObject *_args;
392 PyObject *_res = NULL;
393 Point theCell;
394 if (!PyArg_ParseTuple(_args, "O&",
395 PyMac_GetPoint, &theCell))
396 return NULL;
397 LClrCell(theCell,
398 _self->ob_itself);
399 Py_INCREF(Py_None);
400 _res = Py_None;
401 return _res;
404 static PyObject *ListObj_LGetCell(_self, _args)
405 ListObject *_self;
406 PyObject *_args;
408 PyObject *_res = NULL;
409 char *dataPtr__out__;
410 short dataPtr__len__;
411 int dataPtr__in_len__;
412 Point theCell;
413 if (!PyArg_ParseTuple(_args, "iO&",
414 &dataPtr__in_len__,
415 PyMac_GetPoint, &theCell))
416 return NULL;
417 if ((dataPtr__out__ = malloc(dataPtr__in_len__)) == NULL)
419 PyErr_NoMemory();
420 goto dataPtr__error__;
422 dataPtr__len__ = dataPtr__in_len__;
423 LGetCell(dataPtr__out__, &dataPtr__len__,
424 theCell,
425 _self->ob_itself);
426 _res = Py_BuildValue("s#",
427 dataPtr__out__, (int)dataPtr__len__);
428 free(dataPtr__out__);
429 dataPtr__error__: ;
430 return _res;
433 static PyObject *ListObj_LRect(_self, _args)
434 ListObject *_self;
435 PyObject *_args;
437 PyObject *_res = NULL;
438 Rect cellRect;
439 Point theCell;
440 if (!PyArg_ParseTuple(_args, "O&",
441 PyMac_GetPoint, &theCell))
442 return NULL;
443 LRect(&cellRect,
444 theCell,
445 _self->ob_itself);
446 _res = Py_BuildValue("O&",
447 PyMac_BuildRect, &cellRect);
448 return _res;
451 static PyObject *ListObj_LSetCell(_self, _args)
452 ListObject *_self;
453 PyObject *_args;
455 PyObject *_res = NULL;
456 char *dataPtr__in__;
457 short dataPtr__len__;
458 int dataPtr__in_len__;
459 Point theCell;
460 if (!PyArg_ParseTuple(_args, "s#O&",
461 &dataPtr__in__, &dataPtr__in_len__,
462 PyMac_GetPoint, &theCell))
463 return NULL;
464 dataPtr__len__ = dataPtr__in_len__;
465 LSetCell(dataPtr__in__, dataPtr__len__,
466 theCell,
467 _self->ob_itself);
468 Py_INCREF(Py_None);
469 _res = Py_None;
470 dataPtr__error__: ;
471 return _res;
474 static PyObject *ListObj_LSetSelect(_self, _args)
475 ListObject *_self;
476 PyObject *_args;
478 PyObject *_res = NULL;
479 Boolean setIt;
480 Point theCell;
481 if (!PyArg_ParseTuple(_args, "bO&",
482 &setIt,
483 PyMac_GetPoint, &theCell))
484 return NULL;
485 LSetSelect(setIt,
486 theCell,
487 _self->ob_itself);
488 Py_INCREF(Py_None);
489 _res = Py_None;
490 return _res;
493 static PyObject *ListObj_LDraw(_self, _args)
494 ListObject *_self;
495 PyObject *_args;
497 PyObject *_res = NULL;
498 Point theCell;
499 if (!PyArg_ParseTuple(_args, "O&",
500 PyMac_GetPoint, &theCell))
501 return NULL;
502 LDraw(theCell,
503 _self->ob_itself);
504 Py_INCREF(Py_None);
505 _res = Py_None;
506 return _res;
509 static PyMethodDef ListObj_methods[] = {
510 {"LAddColumn", (PyCFunction)ListObj_LAddColumn, 1,
511 "(short count, short colNum) -> (short _rv)"},
512 {"LAddRow", (PyCFunction)ListObj_LAddRow, 1,
513 "(short count, short rowNum) -> (short _rv)"},
514 {"LDelColumn", (PyCFunction)ListObj_LDelColumn, 1,
515 "(short count, short colNum) -> None"},
516 {"LDelRow", (PyCFunction)ListObj_LDelRow, 1,
517 "(short count, short rowNum) -> None"},
518 {"LGetSelect", (PyCFunction)ListObj_LGetSelect, 1,
519 "(Boolean next, Point theCell) -> (Boolean _rv, Point theCell)"},
520 {"LLastClick", (PyCFunction)ListObj_LLastClick, 1,
521 "() -> (Point _rv)"},
522 {"LNextCell", (PyCFunction)ListObj_LNextCell, 1,
523 "(Boolean hNext, Boolean vNext, Point theCell) -> (Boolean _rv, Point theCell)"},
524 {"LSize", (PyCFunction)ListObj_LSize, 1,
525 "(short listWidth, short listHeight) -> None"},
526 {"LSetDrawingMode", (PyCFunction)ListObj_LSetDrawingMode, 1,
527 "(Boolean drawIt) -> None"},
528 {"LScroll", (PyCFunction)ListObj_LScroll, 1,
529 "(short dCols, short dRows) -> None"},
530 {"LAutoScroll", (PyCFunction)ListObj_LAutoScroll, 1,
531 "() -> None"},
532 {"LUpdate", (PyCFunction)ListObj_LUpdate, 1,
533 "(RgnHandle theRgn) -> None"},
534 {"LActivate", (PyCFunction)ListObj_LActivate, 1,
535 "(Boolean act) -> None"},
536 {"LCellSize", (PyCFunction)ListObj_LCellSize, 1,
537 "(Point cSize) -> None"},
538 {"LClick", (PyCFunction)ListObj_LClick, 1,
539 "(Point pt, short modifiers) -> (Boolean _rv)"},
540 {"LAddToCell", (PyCFunction)ListObj_LAddToCell, 1,
541 "(Buffer dataPtr, Point theCell) -> None"},
542 {"LClrCell", (PyCFunction)ListObj_LClrCell, 1,
543 "(Point theCell) -> None"},
544 {"LGetCell", (PyCFunction)ListObj_LGetCell, 1,
545 "(Buffer dataPtr, Point theCell) -> (Buffer dataPtr)"},
546 {"LRect", (PyCFunction)ListObj_LRect, 1,
547 "(Point theCell) -> (Rect cellRect)"},
548 {"LSetCell", (PyCFunction)ListObj_LSetCell, 1,
549 "(Buffer dataPtr, Point theCell) -> None"},
550 {"LSetSelect", (PyCFunction)ListObj_LSetSelect, 1,
551 "(Boolean setIt, Point theCell) -> None"},
552 {"LDraw", (PyCFunction)ListObj_LDraw, 1,
553 "(Point theCell) -> None"},
554 {NULL, NULL, 0}
557 PyMethodChain ListObj_chain = { ListObj_methods, NULL };
559 static PyObject *ListObj_getattr(self, name)
560 ListObject *self;
561 char *name;
564 /* XXXX Should we HLock() here?? */
565 if ( strcmp(name, "listFlags") == 0 )
566 return Py_BuildValue("l", (long)(*self->ob_itself)->listFlags & 0xff);
567 if ( strcmp(name, "selFlags") == 0 )
568 return Py_BuildValue("l", (long)(*self->ob_itself)->selFlags & 0xff);
570 return Py_FindMethodInChain(&ListObj_chain, (PyObject *)self, name);
573 static int
574 ListObj_setattr(self, name, value)
575 ListObject *self;
576 char *name;
577 PyObject *value;
579 long intval;
581 if ( value == NULL || !PyInt_Check(value) )
582 return -1;
583 intval = PyInt_AsLong(value);
584 if (strcmp(name, "listFlags") == 0 ) {
585 /* XXXX Should we HLock the handle here?? */
586 (*self->ob_itself)->listFlags = intval;
587 return 0;
589 if (strcmp(name, "selFlags") == 0 ) {
590 (*self->ob_itself)->selFlags = intval;
591 return 0;
593 return -1;
597 PyTypeObject List_Type = {
598 PyObject_HEAD_INIT(&PyType_Type)
599 0, /*ob_size*/
600 "List", /*tp_name*/
601 sizeof(ListObject), /*tp_basicsize*/
602 0, /*tp_itemsize*/
603 /* methods */
604 (destructor) ListObj_dealloc, /*tp_dealloc*/
605 0, /*tp_print*/
606 (getattrfunc) ListObj_getattr, /*tp_getattr*/
607 (setattrfunc) ListObj_setattr, /*tp_setattr*/
610 /* ---------------------- End object type List ---------------------- */
613 static PyObject *List_LNew(_self, _args)
614 PyObject *_self;
615 PyObject *_args;
617 PyObject *_res = NULL;
618 ListHandle _rv;
619 Rect rView;
620 Rect dataBounds;
621 Point cSize;
622 short theProc;
623 WindowPtr theWindow;
624 Boolean drawIt;
625 Boolean hasGrow;
626 Boolean scrollHoriz;
627 Boolean scrollVert;
628 if (!PyArg_ParseTuple(_args, "O&O&O&hO&bbbb",
629 PyMac_GetRect, &rView,
630 PyMac_GetRect, &dataBounds,
631 PyMac_GetPoint, &cSize,
632 &theProc,
633 WinObj_Convert, &theWindow,
634 &drawIt,
635 &hasGrow,
636 &scrollHoriz,
637 &scrollVert))
638 return NULL;
639 _rv = LNew(&rView,
640 &dataBounds,
641 cSize,
642 theProc,
643 theWindow,
644 drawIt,
645 hasGrow,
646 scrollHoriz,
647 scrollVert);
648 _res = Py_BuildValue("O&",
649 ListObj_New, _rv);
650 return _res;
653 static PyMethodDef List_methods[] = {
654 {"LNew", (PyCFunction)List_LNew, 1,
655 "(Rect rView, Rect dataBounds, Point cSize, short theProc, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle _rv)"},
656 {NULL, NULL, 0}
662 void initList()
664 PyObject *m;
665 PyObject *d;
670 m = Py_InitModule("List", List_methods);
671 d = PyModule_GetDict(m);
672 List_Error = PyMac_GetOSErrException();
673 if (List_Error == NULL ||
674 PyDict_SetItemString(d, "Error", List_Error) != 0)
675 Py_FatalError("can't initialize List.Error");
676 List_Type.ob_type = &PyType_Type;
677 Py_INCREF(&List_Type);
678 if (PyDict_SetItemString(d, "ListType", (PyObject *)&List_Type) != 0)
679 Py_FatalError("can't initialize ListType");
682 /* ======================== End module List ========================= */