CFLAGS += -g -O2
endif
-VERSION = 0.13
+VERSION = 0.20
CVSTAG = r$(subst .,-,$(VERSION))
-SONAME = 0.11
+SONAME = 0.20
PROGS = test whiptail whiptcl.so testgrid
TESTOBJS = test.o
/* Linked list of items in the listbox */
struct items {
- void *key;
+ char * text;
const void *data;
unsigned char isSelected;
struct items *next;
if(!item)
return;
else {
- free(item->key);
- item->key = strdup(text);
+ free(item->text);
+ item->text = strdup(text);
}
if (li->userHasSetWidth == 0 && strlen(text) > li->curWidth) {
updateWidth(co, li, strlen(text));
}
int newtListboxAddEntry(newtComponent co, const char * text,
- const void * data) {
+ const void * data) {
struct listbox * li = co->data;
struct items *item;
if (!li->userHasSetWidth && text && (strlen(text) > li->curWidth))
updateWidth(co, li, strlen(text));
- item->key = strdup(text); item->data = data; item->next = NULL;
+ item->text = strdup(text); item->data = data; item->next = NULL;
item->isSelected = 0;
if (li->grow)
co->height++, li->curHeight++;
li->numItems++;
- return li->numItems;
+ return 0;
}
-
int newtListboxInsertEntry(newtComponent co, const char * text,
- const void * data, int num) {
+ const void * data, void * key) {
struct listbox * li = co->data;
struct items *item, *t;
- int i;
- if(num > li->numItems)
- num = li->numItems;
if (li->boxItems) {
- if(num > 0) {
- for(i = 0, item = li->boxItems; item->next != NULL && i < num - 1;
- item = item->next, i++);
+ if (key) {
+ item = li->boxItems;
+ while (item && item->data != key) item = item->next;
+
+ if (!item) return 1;
+
t = item->next;
item = item->next = malloc(sizeof(struct items));
item->next = t;
item = li->boxItems = malloc(sizeof(struct items));
item->next = t;
}
+ } else if (key) {
+ return 1;
} else {
item = li->boxItems = malloc(sizeof(struct items));
item->next = NULL;
if (!li->userHasSetWidth && text && (strlen(text) > li->curWidth))
updateWidth(co, li, strlen(text));
- item->key = strdup(text?text:"(null)"); item->data = data;
+ item->text = strdup(text?text:"(null)"); item->data = data;
item->isSelected = 0;
if (li->sb)
listboxDraw(co);
- return li->numItems;
+ return 0;
}
-int newtListboxDeleteEntry(newtComponent co, int num) {
+int newtListboxDeleteEntry(newtComponent co, void * key) {
struct listbox * li = co->data;
- int i, widest = 0, t;
+ int widest = 0, t;
struct items *item, *item2 = NULL;
-
- if(num > li->numItems)
- num = li->numItems;
+ int num;
if (li->boxItems == NULL || li->numItems <= 0)
return 0;
- if (num <= 1) {
- item = li->boxItems;
- li->boxItems = item->next;
+ num = 0;
- /* Fix things up for the width-finding loop near the bottom */
- item2 = li->boxItems;
- widest = strlen(item2?item2->key:"");
- } else {
- for(i = 0, item = li->boxItems; item != NULL && i < num - 1;
- i++, item = item->next) {
- if((t = strlen(item->key)) > widest) widest = t;
- item2 = item;
- }
+ item2 = NULL, item = li->boxItems;
+ while (item && item->data != key) {
+ item2 = item;
+ item = item->next;
+ num++;
+ }
- if (!item)
- return -1;
+ if (!item)
+ return -1;
+ if (item2)
item2->next = item->next;
- }
- free(item->key);
+ else
+ li->boxItems = item->next;
+
+ free(item->text);
free(item);
li->numItems--;
- if(li->currItem >= num)
+
+ if (!li->userHasSetWidth) {
+ widest = 0;
+ for (item = li->boxItems; item != NULL; item = item->next)
+ if ((t = strlen(item->text)) > widest) widest = t;
+ }
+
+ if (li->currItem >= num)
li->currItem--;
- for (item = item2?item2->next:item2; item != NULL; item = item->next)
- if((t = strlen(item->key)) > widest) widest = t;
- /* Adjust the listbox width */
if (!li->userHasSetWidth) {
updateWidth(co, li, widest);
}
listboxDraw(co);
- return li->numItems;
+ return 0;
}
void newtListboxClear(newtComponent co)
return;
for(anitem = li->boxItems; anitem != NULL; anitem = nextitem) {
nextitem = anitem->next;
- free(anitem->key);
+ free(anitem->text);
free(anitem);
}
li->numItems = li->numSelected = li->currItem = li->startShowItem = 0;
if (item) {
if (text)
- *text = item->key;
+ *text = item->text;
if (data)
*data = (void *)item->data;
}
j = i;
for (i = 0; item != NULL && i < li->curHeight; i++, item = item->next) {
- if (!item->key) continue;
+ if (!item->text) continue;
newtGotorc(co->top + i + li->bdyAdjust, co->left + li->bdxAdjust);
if(j + i == li->currItem) {
else
SLsmg_set_color(NEWT_COLORSET_LISTBOX);
- SLsmg_write_nstring(item->key, li->curWidth);
+ SLsmg_write_nstring(item->text, li->curWidth);
}
newtGotorc(co->top + (li->currItem - li->startShowItem), co->left);
while (item != NULL) {
nextitem = item->next;
- free(item->key);
+ free(item->text);
free(item);
item = nextitem;
}
/* return the data passed to AddEntry */
void newtListboxSetData(newtComponent co, int num, void * data);
int newtListboxAddEntry(newtComponent co, const char * text, const void * data);
-int newtListboxInsertEntry(newtComponent co, const char * text, const void * data, int num);
-int newtListboxDeleteEntry(newtComponent co, int num);
+/* Send the key to insert after, or NULL to insert at the top */
+int newtListboxInsertEntry(newtComponent co, const char * text, const void * data, void * key);
+int newtListboxDeleteEntry(newtComponent co, void * data);
void newtListboxClear(newtComponent co); /* removes all entries from listbox */
void newtListboxGetEntry(newtComponent co, int num, char **text, void **data);
/* Returns an array of data pointers from items, last element is NULL */
from snack import *
-screen = SnackScreen()
-
t = Textbox(25, 1, "Some text")
li = Listbox(5, width = 20, returnExit = 1)
-li.append("First")
-li.append("Second")
+li.append("First", "f")
+li.append("Second", "s")
+li.insert("Another", "a", "f")
+li.delete("a")
b = Button("Button")
e = Entry(15, "Entry")
l = Label("label")
r1 = SingleRadioButton("Radio 1", None, 1)
r2 = SingleRadioButton("Radio 2", r1)
+screen = SnackScreen()
+
sg = Grid(2, 3)
sg.setField(b, 0, 0, anchorLeft = 1)
sg.setField(e, 1, 0, (1, 0, 0, 0), anchorLeft = 1, anchorTop = 1)
class Listbox(Widget):
- def append(self, text):
- return self.w.listboxAddItem(text)
+ def append(self, text, item):
+ key = self.w.listboxAddItem(text)
+ self.key2item[key] = item
+ self.item2key[item] = key
+ print "key", key, "item", item
+
+ def insert(self, text, item, before):
+ if (not before):
+ key = self.w.listboxInsertItem(text, 0)
+ else:
+ key = self.w.listboxInsertItem(text, self.item2key[before])
+ self.key2item[key] = item
+ self.item2key[item] = key
+
+ def delete(self, item):
+ self.w.listboxDeleteItem(self.item2key[item])
+ del self.key2item[self.item2key[item]]
+ del self.item2key[item]
def current(self):
- return self.w.listboxGetCurrent()
+ return self.key2item[self.w.listboxGetCurrent()]
def __init__(self, height, scroll = 0, returnExit = 0, width = 0):
self.w = _snack.listbox(height, scroll, returnExit)
+ self.key2item = {}
+ self.item2key = {}
if (width):
self.w.listboxSetWidth(width)
static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args);
static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args);
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 PyMethodDef widgetMethods[] = {
{ "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL },
{ "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL },
+ { "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL },
{ "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL },
{ "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL },
+ { "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL },
{ NULL }
};
widget->co = newtListbox(-1, -1, height,
(doScroll ? 0 : NEWT_FLAG_NOSCROLL) |
(returnExit ? NEWT_FLAG_RETURNEXIT : 0));
- widget->anint = 0;
+ widget->anint = 1;
return widget;
}
return PyInt_FromLong(s->anint++);
}
+static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) {
+ char * text;
+ int key;
+
+ if (!PyArg_ParseTuple(args, "si", &text, &key))
+ return NULL;
+
+ newtListboxInsertEntry(s->co, text, (void *) s->anint, (void *) key);
+
+ return PyInt_FromLong(s->anint++);
+}
+
+static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) {
+ int key;
+
+ if (!PyArg_ParseTuple(args, "i", &key))
+ return NULL;
+
+ newtListboxDeleteEntry(s->co, (void *) key);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) {
if (!PyArg_ParseTuple(args, ""))
return NULL;
newtFormAddComponents(f, rsf, scale, NULL);
lb = newtListbox(45, 1, 4, NEWT_FLAG_MULTIPLE | NEWT_FLAG_DOBORDER);
- newtListboxAddEntry(lb, "First", "First");
- newtListboxAddEntry(lb, "Second", "Second");
- newtListboxAddEntry(lb, "Third", "Third");
- newtListboxAddEntry(lb, "Fourth", "Fourth");
- newtListboxAddEntry(lb, "Fifth", "Fifth");
- newtListboxAddEntry(lb, "Sixth", "Sixth");
- newtListboxAddEntry(lb, "Seventh", "Seventh");
- newtListboxAddEntry(lb, "Eighth", "Eighth");
- newtListboxAddEntry(lb, "Ninth", "Ninth");
- newtListboxAddEntry(lb, "Tenth", "Tenth");
+ newtListboxAddEntry(lb, "First", (void *) 1);
+ newtListboxAddEntry(lb, "Second", (void *) 2);
+ newtListboxAddEntry(lb, "Third", (void *) 3);
+ newtListboxAddEntry(lb, "Fourth", (void *) 4);
+ newtListboxAddEntry(lb, "Sixth", (void *) 6);
+ newtListboxAddEntry(lb, "Seventh", (void *) 7);
+ newtListboxAddEntry(lb, "Eighth", (void *) 8);
+ newtListboxAddEntry(lb, "Ninth", (void *) 9);
+ newtListboxAddEntry(lb, "Tenth", (void *) 10);
+
+ newtListboxInsertEntry(lb, "Fifth", (void *) 5, (void *) 4);
+ newtListboxInsertEntry(lb, "Eleventh", (void *) 11, (void *) 10);
+ newtListboxDeleteEntry(lb, (void *) 11);
t = newtTextbox(45, 10, 17, 5, NEWT_FLAG_WRAP);
newtTextboxSetText(t, "This is some text does it look okay?\nThis should be alone.\nThis shouldn't be printed");