From: ewt Date: Mon, 27 Oct 1997 23:54:16 +0000 (+0000) Subject: 1) added insert, remove to snack listbox X-Git-Tag: r0-20~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a507b3ec200eaaa17d44d9060f7bfd297caf75b8;p=thirdparty%2Fnewt.git 1) added insert, remove to snack listbox 2) fixed listboxes to be more consistent in not using indices --- diff --git a/Makefile b/Makefile index 8e7c3a0..60f0267 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,9 @@ ifeq ($(RPM_OPT_FLAGS),) 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 diff --git a/listbox.c b/listbox.c index 2813cd7..f15c5dc 100644 --- a/listbox.c +++ b/listbox.c @@ -14,7 +14,7 @@ /* Linked list of items in the listbox */ struct items { - void *key; + char * text; const void *data; unsigned char isSelected; struct items *next; @@ -262,8 +262,8 @@ void newtListboxSetText(newtComponent co, int num, const char * text) { 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)); @@ -289,7 +289,7 @@ void newtListboxSetData(newtComponent co, int num, void * data) { } int newtListboxAddEntry(newtComponent co, const char * text, - const void * data) { + const void * data) { struct listbox * li = co->data; struct items *item; @@ -304,29 +304,28 @@ int newtListboxAddEntry(newtComponent co, const char * text, 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; @@ -335,6 +334,8 @@ int newtListboxInsertEntry(newtComponent co, const char * text, 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; @@ -343,7 +344,7 @@ int newtListboxInsertEntry(newtComponent co, const char * text, 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) @@ -352,55 +353,55 @@ int newtListboxInsertEntry(newtComponent co, const char * text, 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) @@ -411,7 +412,7 @@ 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; @@ -443,7 +444,7 @@ void newtListboxGetEntry(newtComponent co, int num, char **text, void **data) { if (item) { if (text) - *text = item->key; + *text = item->text; if (data) *data = (void *)item->data; } @@ -477,7 +478,7 @@ static void listboxDraw(newtComponent co) 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) { @@ -490,7 +491,7 @@ static void listboxDraw(newtComponent co) 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); @@ -625,7 +626,7 @@ static void listboxDestroy(newtComponent co) { while (item != NULL) { nextitem = item->next; - free(item->key); + free(item->text); free(item); item = nextitem; } diff --git a/newt.h b/newt.h index 98f594a..675f587 100644 --- a/newt.h +++ b/newt.h @@ -139,8 +139,9 @@ void newtListboxSetWidth(newtComponent co, int width); /* 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 */ diff --git a/popcorn.py b/popcorn.py index fa755a9..88e89c3 100755 --- a/popcorn.py +++ b/popcorn.py @@ -2,12 +2,12 @@ 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") @@ -15,6 +15,8 @@ cb = Checkbox("checkbox") 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) diff --git a/snack.py b/snack.py index 42697b5..c25870d 100644 --- a/snack.py +++ b/snack.py @@ -38,14 +38,32 @@ class SingleRadioButton(Widget): 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) diff --git a/snackmodule.c b/snackmodule.c index 980a6e6..a342407 100644 --- a/snackmodule.c +++ b/snackmodule.c @@ -136,13 +136,17 @@ static PyObject * widgetGetAttr(PyObject * s, char * name); 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 } }; @@ -327,7 +331,7 @@ static snackWidget * listboxWidget(PyObject * s, PyObject * args) { widget->co = newtListbox(-1, -1, height, (doScroll ? 0 : NEWT_FLAG_NOSCROLL) | (returnExit ? NEWT_FLAG_RETURNEXIT : 0)); - widget->anint = 0; + widget->anint = 1; return widget; } @@ -546,6 +550,30 @@ static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) { 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; diff --git a/test.c b/test.c index 25abfd7..e4c3d3b 100644 --- a/test.c +++ b/test.c @@ -88,16 +88,19 @@ int main(void) { 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");