8 /* -------------------------------------------------*/
24 /* object functions */
26 static PyObject
*PyTXObj_draw(PyTX_Obj
*self
) {
27 tx_obj_draw(&(self
->tx_obj
));
32 static PyObject
*PyTXObj_flag_set(PyTX_Obj
*self
, PyObject
*args
) {
35 PyArg_ParseTuple(args
, "ii", &flag
, &set
);
37 tx_obj_flag_set(&(self
->tx_obj
), flag
, set
);
43 static PyObject
*PyTXObj_flag_toggle(PyTX_Obj
*self
, PyObject
*args
) {
47 PyArg_ParseTuple(args
, "i", &flag
);
49 flag
= tx_obj_flag_toggle(&(self
->tx_obj
), flag
);
50 result
= PyInt_FromLong(flag
);
56 static PyObject
*PyTXObj_flag_get(PyTX_Obj
*self
, PyObject
*args
) {
60 PyArg_ParseTuple(args
, "i", &flag
);
62 flag
= tx_obj_flag_get(&(self
->tx_obj
), flag
);
63 result
= PyInt_FromLong(flag
);
69 static PyObject
*PyTXObj_reset(PyTX_Obj
*self
) {
70 tx_obj_reset(&(self
->tx_obj
));
76 static void PyTXList_free(PyTX_Obj
* self
) {
77 self
->ob_type
->tp_free((PyObject
*)self
);
80 static void *data_cb(TXObj
*obj
, void *o
, void *d
) {
82 Py_DECREF((PyObject
*)o
);
84 Py_INCREF((PyObject
*)d
);
88 char *text_cb(TXObj
*obj
, char *o
, void *d
) {
89 PyObject
*result
, *params
;
90 PyTXList_Funcs
*funcs
= (PyTXList_Funcs
*)obj
->data
;
95 params
= Py_BuildValue("(OO)", obj
->bind
, d
);
96 result
= PyEval_CallObject(funcs
->text_cb
, params
);
97 text
= PyString_AsString(result
);
104 int cmpr_cb(TXObj
*obj
, const struct list_head
*a
, const struct list_head
*b
) {
105 TX_EXT(TX_List
, list
);
108 struct Row
*ra
= list_entry(a
, struct Row
, node
);
109 struct Row
*rb
= list_entry(b
, struct Row
, node
);
111 o1
= PyList_GetItem(ra
->data
, list
->cols
.sort
);
112 o2
= PyList_GetItem(rb
->data
, list
->cols
.sort
);
114 return list
->asc
? PyObject_Compare(o1
, o2
) : PyObject_Compare(o2
, o1
);
117 void idle_cb(TXObj
*obj
) {
118 PyObject
*params
, *result
;
119 PyTXList_Funcs
*funcs
= (PyTXList_Funcs
*)obj
->data
;
121 params
= Py_BuildValue("(O)", obj
->bind
);
122 result
= PyEval_CallObject(funcs
->idle_cb
, params
);
128 void bind_cb(TXObj
*obj
, int ch
, int key
) {
129 PyObject
*params
, *result
;
130 PyTXList_Funcs
*funcs
= (PyTXList_Funcs
*)obj
->data
;
132 params
= Py_BuildValue("(Oii)", obj
->bind
, ch
, key
);
133 result
= PyEval_CallObject(funcs
->bind_cb
, params
);
139 void rsze_cb(TXObj
*obj
) {
140 PyObject
*params
, *result
;
142 PyTXList_Funcs
*funcs
= (PyTXList_Funcs
*)obj
->data
;
144 params
= Py_BuildValue("(O)", obj
->bind
);
145 result
= PyEval_CallObject(funcs
->rsze_cb
, params
);
151 int size_cb(TXObj
*obj
, int s
, int d
) {
154 PyObject
*result
, *params
;
155 PyTXList_Funcs
*funcs
= (PyTXList_Funcs
*)obj
->data
;
158 params
= Py_BuildValue("(Oii)", obj
->bind
, s
, d
);
159 result
= PyEval_CallObject(funcs
->size_cb
, params
);
160 size
= (int)PyInt_AsLong(result
);
168 int getc_cb(TXObj
*obj
) {
171 Py_BEGIN_ALLOW_THREADS
178 static int PyTXList_init(PyTX_Obj
*self
, PyObject
*args
, PyObject
*kwds
) {
181 PyTXList_Funcs
*funcs
;
182 PyArg_ParseTuple(args
, "|i", &flags
);
183 tx_obj_init(&(self
->tx_obj
), TX_LIST
, PyTuple_Size(args
) ? flags
: 0);
185 tx_obj_getc_cb_set(&(self
->tx_obj
), getc_cb
);
187 self
->tx_obj
.bind
= self
;
188 self
->tx_obj
.data
= calloc(1, sizeof(PyTXList_Funcs
));
189 assert(self
->tx_obj
.data
);
190 funcs
= self
->tx_obj
.data
;
192 cb
= PyDict_GetItemString(self
->ob_type
->tp_dict
, "text_cb");
193 if (cb
&& PyFunction_Check(cb
)) {
194 tx_list_data_cb_set(&(self
->tx_obj
), data_cb
);
198 cb
= PyDict_GetItemString(self
->ob_type
->tp_dict
, "idle_cb");
199 if (cb
&& PyFunction_Check(cb
)) {
200 tx_obj_idle_cb_set(&(self
->tx_obj
), idle_cb
);
204 cb
= PyDict_GetItemString(self
->ob_type
->tp_dict
, "bind_cb");
205 if (cb
&& PyFunction_Check(cb
)) {
206 tx_list_bind_cb_set(&(self
->tx_obj
), bind_cb
);
210 tx_list_cmpr_cb_set(&(self
->tx_obj
), cmpr_cb
);
212 /* FIXME: restore previous cmpr_cb for custom compare */
213 cb
= PyDict_GetItemString(self
->ob_type
->tp_dict
, "cmpr_cb");
214 if (cb
&& PyFunction_Check(cb
)) {
215 tx_list_cmpr_cb_set(&(self
->tx_obj
), cmpr_cb
);
219 cb
= PyDict_GetItemString(self
->ob_type
->tp_dict
, "size_cb");
220 if (cb
&& PyFunction_Check(cb
)) {
221 tx_obj_size_cb_set(&(self
->tx_obj
), size_cb
);
225 cb
= PyDict_GetItemString(self
->ob_type
->tp_dict
, "rsze_cb");
226 if (cb
&& PyFunction_Check(cb
)) {
227 tx_obj_rsze_cb_set(&(self
->tx_obj
), rsze_cb
);
234 char **cols_cb(TXObj
*obj
, char *o
, void *d
) {
235 PyObject
*result
, *params
, *item
;
239 PyTXList_Funcs
*funcs
= (PyTXList_Funcs
*)obj
->data
;
243 params
= Py_BuildValue("(OO)", obj
->bind
, d
);
244 result
= PyEval_CallObject(funcs
->text_cb
, params
);
246 ncols
= PyList_Size(result
);
247 cols
= malloc(sizeof(char *) * ncols
);
250 for (i
=0; i
< ncols
; ++i
) {
251 item
= PyList_GetItem(result
, i
);
252 cols
[i
] = PyString_AsString(item
);
260 static PyObject
*PyTXList_columns_width_set(PyTX_Obj
*self
, PyObject
*args
) {
262 int i
, ncols
, *widths
;
264 PyArg_ParseTuple(args
, "O", &py_widths
);
266 ncols
= PyList_Size(py_widths
);
267 widths
= malloc(sizeof(int) * ncols
);
270 for (i
= 0; i
< ncols
; ++i
)
271 widths
[i
] = PyInt_AsLong(PyList_GetItem(py_widths
, i
));
273 tx_list_columns_width_set(&(self
->tx_obj
), ncols
, widths
);
276 widths
= tx_list_columns_width_get(&(self
->tx_obj
));
277 for (i
= 0; i
< ncols
; ++i
)
278 PyList_SetItem(py_widths
, i
, PyInt_FromLong(widths
[i
]));
280 /* set cols_cb upon setting widths */
281 tx_list_cols_cb_set(&(self
->tx_obj
), cols_cb
);
283 /* give object access to list array */
284 PyDict_SetItemString(self
->ob_type
->tp_dict
, "widths", py_widths
);
286 Py_INCREF(py_widths
);
292 static PyObject
*PyTXList_columns_title_set(PyTX_Obj
*self
, PyObject
*args
) {
293 PyObject
*py_titles
, *title
;
297 PyArg_ParseTuple(args
, "O", &py_titles
);
299 ncols
= PyList_Size(py_titles
);
300 titles
= malloc(sizeof(char*) * ncols
);
303 for (i
= 0; i
< ncols
; ++i
) {
304 title
= PyList_GetItem(py_titles
, i
);
305 titles
[i
] = strdup(PyString_AsString(title
));
308 tx_list_columns_title_set(&(self
->tx_obj
), ncols
, titles
);
312 Py_INCREF(py_titles
);
316 static PyObject
*PyTXList_scroll(PyTX_Obj
*self
, PyObject
*args
) {
319 PyArg_ParseTuple(args
, "i", &scroll
);
320 tx_list_scroll(&(self
->tx_obj
), scroll
);
326 static PyObject
*PyTXList_add(PyTX_Obj
*self
, PyObject
*args
) {
329 PyTXList_Funcs
*funcs
= self
->tx_obj
.data
;
331 PyArg_ParseTuple(args
, funcs
->text_cb
?"iO":"is", &key
, &obj
);
333 tx_list_row_sync(&(self
->tx_obj
), key
, obj
);
339 static PyObject
*PyTXList_remove(PyTX_Obj
*self
, PyObject
*args
) {
342 PyArg_ParseTuple(args
, "i", &key
);
344 tx_list_row_remove(&(self
->tx_obj
), key
);
350 static PyObject
*PyTXList_sort_set(PyTX_Obj
*self
, PyObject
*args
, PyObject
*kwargs
) {
353 char *kwlist
[] = {"col", "asc", NULL
};
355 PyArg_ParseTupleAndKeywords(args
, kwargs
,"|ii", kwlist
, &col
, &asc
);
356 if (col
!= -1) tx_list_column_sort_set(&(self
->tx_obj
), col
);
357 if (asc
!= -1) tx_list_sort_asc_set(&(self
->tx_obj
), asc
);
363 static PyObject
*PyTXList_sort_get(PyTX_Obj
*self
) {
367 col
= tx_list_column_sort_get(&(self
->tx_obj
));
368 asc
= tx_list_sort_asc_get(&(self
->tx_obj
));
370 result
= PyTuple_Pack(2, PyInt_FromLong(col
), PyInt_FromLong(asc
));
376 static PyObject
*PyTXList_sort(PyTX_Obj
*self
, PyObject
*args
) {
377 tx_list_sort(&(self
->tx_obj
));
383 static PyObject
*PyTXList_clear(PyTX_Obj
*self
) {
384 tx_list_clear(&(self
->tx_obj
));
390 static PyObject
*PyTXList_sync(PyTX_Obj
*self
) {
391 tx_list_sync(&(self
->tx_obj
));
397 static PyObject
*PyTXList_selected(PyTX_Obj
*self
, PyObject
*args
) {
401 if (PyTuple_Size(args
)) {
402 PyArg_ParseTuple(args
, "i", &key
);
403 tx_list_selected_set(&(self
->tx_obj
), key
);
408 key
= tx_list_selected_get(&(self
->tx_obj
));
409 result
= PyInt_FromLong(key
);
415 static PyMethodDef PyTXList_methods
[] = {
416 {"flag_set", (PyCFunction
)(PyTXObj_flag_set
), METH_VARARGS
, NULL
},
417 {"flag_get", (PyCFunction
)(PyTXObj_flag_get
), METH_VARARGS
, NULL
},
418 {"flag_toggle", (PyCFunction
)(PyTXObj_flag_toggle
), METH_VARARGS
, NULL
},
419 {"draw", (PyCFunction
)(PyTXObj_draw
), METH_NOARGS
, NULL
},
420 {"reset", (PyCFunction
)(PyTXObj_reset
), METH_NOARGS
, NULL
},
421 {"add", (PyCFunction
)(PyTXList_add
), METH_VARARGS
, NULL
},
422 {"remove", (PyCFunction
)(PyTXList_remove
), METH_VARARGS
, NULL
},
423 {"scroll", (PyCFunction
)(PyTXList_scroll
), METH_VARARGS
, NULL
},
424 {"selected", (PyCFunction
)(PyTXList_selected
), METH_VARARGS
, NULL
},
425 {"sort", (PyCFunction
)(PyTXList_sort
), METH_NOARGS
, NULL
},
426 {"sort_get", (PyCFunction
)(PyTXList_sort_get
), METH_NOARGS
, NULL
},
427 {"sort_set", (PyCFunction
)(PyTXList_sort_set
), METH_VARARGS
|METH_KEYWORDS
, NULL
},
428 {"clear", (PyCFunction
)(PyTXList_clear
), METH_NOARGS
, NULL
},
429 {"sync", (PyCFunction
)(PyTXList_sync
), METH_NOARGS
, NULL
},
430 {"widths_set", (PyCFunction
)(PyTXList_columns_width_set
), METH_KEYWORDS
, NULL
},
431 {"titles_set", (PyCFunction
)(PyTXList_columns_title_set
), METH_KEYWORDS
, NULL
},
432 {NULL
} /* Sentinel */
435 static PyTypeObject PyTXList_Type
= {
436 PyObject_HEAD_INIT(NULL
)
438 "pytx.List", /*tp_name*/
439 sizeof(PyTX_Obj
), /*tp_basicsize*/
441 (destructor
)PyTXList_free
, /*tp_dealloc*/
448 0, /*tp_as_sequence*/
456 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
460 0, /* tp_richcompare */
461 0, /* tp_weaklistoffset */
464 PyTXList_methods
, /* tp_methods */
469 0, /* tp_descr_get */
470 0, /* tp_descr_set */
471 0, /* tp_dictoffset */
472 (initproc
)PyTXList_init
, /* tp_init */
477 /* ---------------------------------------------------------- */
479 static void PyTXBox_free(PyTX_Obj
* self
) {
480 self
->ob_type
->tp_free((PyObject
*)self
);
483 static int PyTXBox_init(PyTX_Obj
*self
, PyObject
*args
, PyObject
*kwds
) {
485 /* FIXME: ensure we have selected VERT or HORZ */
486 PyArg_ParseTuple(args
, "i|i", &align
, &flags
);
487 tx_obj_init(&(self
->tx_obj
), TX_BOX
, PyTuple_Size(args
) == 1 ? align
: align
|flags
);
491 static PyObject
*PyTXBox_add(PyTX_Obj
*self
, PyObject
*args
) {
493 PyArg_ParseTuple(args
, "O", &add
);
495 tx_box_add(&(self
->tx_obj
), &(add
->tx_obj
));
497 self
->tx_obj
.bind
= self
;
498 tx_obj_getc_cb_set(&(self
->tx_obj
), getc_cb
);
504 static PyObject
*PyTXBox_adjust(PyTX_Obj
*self
) {
505 tx_box_adjust(&(self
->tx_obj
));
513 static PyMethodDef PyTXBox_methods
[] = {
514 {"add", (PyCFunction
)(PyTXBox_add
), METH_VARARGS
, NULL
},
515 {"draw", (PyCFunction
)(PyTXObj_draw
), METH_NOARGS
, NULL
},
516 {"adjust", (PyCFunction
)(PyTXBox_adjust
), METH_NOARGS
, NULL
},
517 {NULL
} /* Sentinel */
521 static PyTypeObject PyTXBox_Type
= {
522 PyObject_HEAD_INIT(NULL
)
524 "pytx.Box", /*tp_name*/
525 sizeof(PyTX_Obj
), /*tp_basicsize*/
527 (destructor
)PyTXBox_free
, /*tp_dealloc*/
534 0, /*tp_as_sequence*/
542 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
546 0, /* tp_richcompare */
547 0, /* tp_weaklistoffset */
550 PyTXBox_methods
, /* tp_methods */
555 0, /* tp_descr_get */
556 0, /* tp_descr_set */
557 0, /* tp_dictoffset */
558 (initproc
)PyTXBox_init
, /* tp_init */
563 /* ---------------------------------------------------------*/
565 static PyObject
*PyTX_init(PyObject
*self
) {
572 static PyObject
*PyTX_emergency(PyObject
*self
) {
573 tx_ungetc(EMERGENCY
);
579 static PyObject
*PyTX_free(PyObject
*self
) {
586 static PyObject
*PyTX_adjust(PyTX_Obj
*self
) {
587 tx_obj_map(tx_obj_root
, tx_obj_resize
);
593 static PyObject
*PyTX_operate(PyTX_Obj
*self
) {
594 tx_obj_operate(tx_obj_focus
);
600 static PyMethodDef PyTX_functions
[] = {
601 {"init", (PyCFunction
)(PyTX_init
), METH_NOARGS
, NULL
},
602 {"free", (PyCFunction
)(PyTX_free
), METH_NOARGS
, NULL
},
603 {"operate", (PyCFunction
)(PyTX_operate
), METH_VARARGS
, NULL
},
604 {"adjust", (PyCFunction
)(PyTX_adjust
), METH_VARARGS
, NULL
},
605 {"emergency", (PyCFunction
)(PyTX_emergency
), METH_NOARGS
, NULL
},
606 {NULL
, NULL
, 0, NULL
}
609 DL_EXPORT(void) initpytx(void) {
610 Py_InitModule("pytx", PyTX_functions
);
612 m
= Py_InitModule3("pytx", PyTX_functions
, NULL
);
614 PyTXList_Type
.tp_new
= PyType_GenericNew
;
615 if (PyType_Ready(&PyTXList_Type
) < 0)return;
616 Py_INCREF(&PyTXList_Type
);
617 PyModule_AddObject(m
, "List", (PyObject
*)&PyTXList_Type
);
619 PyTXBox_Type
.tp_new
= PyType_GenericNew
;
620 if (PyType_Ready(&PyTXBox_Type
) < 0) return;
621 Py_INCREF(&PyTXBox_Type
);
622 PyModule_AddObject(m
, "Box", (PyObject
*)&PyTXBox_Type
);
624 d
= PyModule_GetDict(m
);
625 PyDict_SetItemString(d
, "BORDER", PyInt_FromLong(TX_OBJ_BORDER
));
626 PyDict_SetItemString(d
, "FOCUS", PyInt_FromLong(TX_OBJ_FOCUS
));
627 PyDict_SetItemString(d
, "ROOT", PyInt_FromLong(TX_OBJ_ROOT
));
629 PyDict_SetItemString(d
, "SCROLL", PyInt_FromLong(TX_LIST_SCROLL
));
630 PyDict_SetItemString(d
, "SEPARATOR", PyInt_FromLong(TX_LIST_SEPARATOR
));
631 PyDict_SetItemString(d
, "TITLES", PyInt_FromLong(TX_LIST_TITLE
));
632 PyDict_SetItemString(d
, "HIGHLIGHT", PyInt_FromLong(TX_LIST_HLIGHT
));
634 PyDict_SetItemString(d
, "VERT", PyInt_FromLong(TX_BOX_VERT
));
635 PyDict_SetItemString(d
, "HORZ", PyInt_FromLong(TX_BOX_HORZ
));