From 67faee665b26ffb5d61a1922bf189f11104abbc4 Mon Sep 17 00:00:00 2001 From: mlichvar Date: Fri, 8 Jun 2007 12:15:19 +0000 Subject: [PATCH] - add support for listbox multiple selection and border to snack (Shawn Starr) --- newt.spec | 2 ++ snack.py | 14 +++++++++++--- snackmodule.c | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/newt.spec b/newt.spec index 641799a..a88923b 100644 --- a/newt.spec +++ b/newt.spec @@ -88,6 +88,8 @@ rm -rf $RPM_BUILD_ROOT %{_libdir}/libnewt.a %changelog +- add support for listbox multiple selection and border to snack + (patch by Shawn Starr) - fix scrollbar positioning in listbox - cope with backward system time jumps (#240691) - free helplines and windows in newtFinished, check for overflow (#239992) diff --git a/snack.py b/snack.py index e11e430..e40515b 100644 --- a/snack.py +++ b/snack.py @@ -144,11 +144,12 @@ class Listbox(Widget): methods: - - Listbox(self,height, scroll = 0l returnExit = 0, width = 0, showCursor = 0) + - Listbox(self, height, scroll = 0, returnExit = 0, width = 0, showCursor = 0, multiple = 0, border = 0) - insert(self, text, item, before) : insert element; before = key to item to insert before, or None. - delete(self, item) : delete item from list. - replace(self, text,item) : Replace a given item's text - current(self) : returns currently selected item + - getSelection(self) : returns a list of selected items - setCurrent(self,i tem) : select current. - clear(self) : clear listbox """ @@ -181,6 +182,13 @@ class Listbox(Widget): def current(self): return self.key2item[self.w.listboxGetCurrent()] + def getSelection(self): + selection = [] + list = self.w.listboxGetSelection() + for key in list: + selection.append(self.key2item[key]) + return selection + def setCurrent(self, item): self.w.listboxSetCurrent(self.item2key[item]) @@ -189,8 +197,8 @@ class Listbox(Widget): self.item2key = {} self.w.listboxClear() - def __init__(self, height, scroll = 0, returnExit = 0, width = 0, showCursor = 0): - self.w = _snack.listbox(height, scroll, returnExit, showCursor) + def __init__(self, height, scroll = 0, returnExit = 0, width = 0, showCursor = 0, multiple = 0, border = 0): + self.w = _snack.listbox(height, scroll, returnExit, showCursor, multiple, border) self.key2item = {} self.item2key = {} if (width): diff --git a/snackmodule.c b/snackmodule.c index 96ed4aa..3a47a3d 100644 --- a/snackmodule.c +++ b/snackmodule.c @@ -200,6 +200,7 @@ static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args); static PyObject * widgetListboxIns(snackWidget * s, PyObject * args); static PyObject * widgetListboxDel(snackWidget * s, PyObject * args); static PyObject * widgetListboxGet(snackWidget * s, PyObject * args); +static PyObject * widgetListboxGetSel(snackWidget * s, PyObject * args); static PyObject * widgetListboxSet(snackWidget * s, PyObject * args); static PyObject * widgetListboxClear(snackWidget * s, PyObject * args); static PyObject * widgetTextboxText(snackWidget * s, PyObject * args); @@ -223,6 +224,7 @@ static PyMethodDef widgetMethods[] = { { "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL }, { "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL }, { "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL }, + { "listboxGetSelection", (PyCFunction) widgetListboxGetSel, METH_VARARGS, NULL }, { "listboxSetCurrent", (PyCFunction) widgetListboxSet, METH_VARARGS, NULL }, { "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL }, { "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL }, @@ -654,16 +656,19 @@ static PyObject * widgetTextboxText(snackWidget * s, PyObject * args) { static snackWidget * listboxWidget(PyObject * s, PyObject * args) { snackWidget * widget; int height; - int doScroll = 0, returnExit = 0, showCursor = 0 ; + int doScroll = 0, returnExit = 0, showCursor = 0, multiple = 0, border = 0; - if (!PyArg_ParseTuple(args, "i|iii", &height, &doScroll, &returnExit, &showCursor)) + if (!PyArg_ParseTuple(args, "i|iiiii", &height, &doScroll, &returnExit, + &showCursor, &multiple, &border)) return NULL; widget = snackWidgetNew (); widget->co = newtListbox(-1, -1, height, (doScroll ? NEWT_FLAG_SCROLL : 0) | (returnExit ? NEWT_FLAG_RETURNEXIT : 0) | - (showCursor ? NEWT_FLAG_SHOWCURSOR : 0) + (showCursor ? NEWT_FLAG_SHOWCURSOR : 0) | + (multiple ? NEWT_FLAG_MULTIPLE : 0) | + (border ? NEWT_FLAG_BORDER : 0) ); widget->anint = 1; @@ -1032,6 +1037,32 @@ static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) { return PyInt_FromLong((long) newtListboxGetCurrent(s->co)); } +static PyObject * widgetListboxGetSel(snackWidget * s, PyObject * args) { + void ** selection; + int numselected; + int i; + PyObject * sel; + + if (!PyArg_ParseTuple(args, "")) + return NULL; + + selection = (void **) newtListboxGetSelection(s->co, &numselected); + + sel = PyList_New(0); + + if (!selection) { + return sel; + } + + sel = PyList_New(0); + for (i = 0; i < numselected; i++) { + PyList_Append(sel, PyInt_FromLong((long) selection[i])); + } + free(selection); + + return sel; +} + static PyObject * widgetListboxSet(snackWidget * s, PyObject * args) { int index; -- 2.47.2