From: sopwith Date: Tue, 22 Jul 1997 19:14:27 +0000 (+0000) Subject: Listbox has multiple selection support now (demo included in test.c). X-Git-Tag: r0-12~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46263d9e6b699977ad17f1f3eb70970ab8b9a7fc;p=thirdparty%2Fnewt.git Listbox has multiple selection support now (demo included in test.c). --- diff --git a/listbox.c b/listbox.c index fb0c4d9..46f59db 100644 --- a/listbox.c +++ b/listbox.c @@ -146,8 +146,39 @@ void * newtListboxGetCurrent(newtComponent co) { return NULL; } +void newtListboxSelectItem(newtComponent co, int item) +{ + struct listbox * li = co->data; + int i; + struct items *iitem; + + for(i = 0, iitem = li->boxItems; iitem != NULL && i < item; + i++, iitem = iitem->next); + + if (iitem) { + if(iitem->isSelected) + li->numSelected--; + else + li->numSelected++; + iitem->isSelected = !iitem->isSelected; + } + listboxDraw(co); +} + +void newtListboxClearSelection(newtComponent co) +{ + struct items *item; + struct listbox * li = co->data; + + for(item = li->boxItems; item != NULL; + item = item->next) + item->isSelected = 0; + + listboxDraw(co); +} + /* Free the returned array after use, but NOT the values in the array */ -void ** newtListboxGetSelected(newtComponent co) +void ** newtListboxGetSelection(newtComponent co) { struct listbox * li; int i; @@ -159,11 +190,12 @@ void ** newtListboxGetSelected(newtComponent co) li = co->data; if(!li || !li->numSelected) return NULL; - retval = malloc(li->numSelected * sizeof(void *)); + retval = malloc((li->numSelected + 1) * sizeof(void *)); for(i = 0, item = li->boxItems; item != NULL; item = item->next) if(item->isSelected) retval[i++] = item->data; + retval[i] = NULL; return retval; } @@ -224,7 +256,8 @@ int newtListboxAddEntry(newtComponent co, char * text, void * data) { li->curWidth = strlen(text) ; item->key = strdup(text); item->data = data; item->next = NULL; - + item->isSelected = 0; + if (li->sb) li->sb->left = co->left + li->curWidth + 2; @@ -234,6 +267,8 @@ int newtListboxAddEntry(newtComponent co, char * text, void * data) { co->width = li->curWidth; li->numItems++; + listboxDraw(co); + return li->numItems; } @@ -268,12 +303,14 @@ int newtListboxInsertEntry(newtComponent co, char * text, void * data, li->curWidth = strlen(text); item->key = strdup(text?text:"(null)"); item->data = data; - + item->isSelected = 0; + if (li->sb) li->sb->left = co->left + li->curWidth + 2; if (li->userHasSetWidth == 0) co->width = li->curWidth; li->numItems++; + listboxDraw(co); return li->numItems; @@ -387,13 +424,18 @@ static void listboxDraw(newtComponent co) if (!item->key) continue; newtGotorc(co->top + i, co->left + 1); - if(j + i == li->currItem) - SLsmg_set_color(NEWT_COLORSET_ACTLISTBOX); - + if(j + i == li->currItem) { + if(item->isSelected) + SLsmg_set_color(NEWT_COLORSET_ACTSELLISTBOX); + else + SLsmg_set_color(NEWT_COLORSET_ACTLISTBOX); + } else if(item->isSelected) + SLsmg_set_color(NEWT_COLORSET_SELLISTBOX); + else + SLsmg_set_color(NEWT_COLORSET_LISTBOX); + SLsmg_write_nstring(item->key, li->curWidth); - if(j + i == li->currItem) - SLsmg_set_color(NEWT_COLORSET_LISTBOX); } newtGotorc(co->top + (li->currItem - li->startShowItem), co->left); } @@ -413,6 +455,12 @@ static struct eventResult listboxEvent(newtComponent co, struct event ev) { if (!li->isActive) break; switch(ev.u.key) { + case ' ': + if(!(li->flags & NEWT_FLAG_MULTIPLE)) break; + newtListboxSelectItem(co, li->currItem); + er.result = ER_SWALLOWED; + break; + case NEWT_KEY_ENTER: if(li->numItems <= 0) break; if(li->flags & NEWT_FLAG_RETURNEXIT) diff --git a/newt.c b/newt.c index 112668c..ab0c360 100644 --- a/newt.c +++ b/newt.c @@ -57,6 +57,8 @@ struct newtColors newtDefaultColorPalette = { "red", /* scale empty */ "blue", "lightgray", /* disabled entry fg, bg */ "white", "blue", /* compact button fg, bg */ + "yellow", "red", /* active & sel listbox */ + "black", "brown" /* selected listbox */ }; static struct keymap keymap[] = { @@ -222,6 +224,11 @@ void newtSetColors(struct newtColors colors) { SLtt_set_color(NEWT_COLORSET_COMPACTBUTTON, "", colors.compactButtonFg, colors.compactButtonBg); + + SLtt_set_color(NEWT_COLORSET_ACTSELLISTBOX, "", colors.actSelListboxFg, + colors.actSelListboxBg); + SLtt_set_color(NEWT_COLORSET_SELLISTBOX, "", colors.selListboxFg, + colors.selListboxBg); } int newtGetKey(void) { @@ -304,7 +311,7 @@ void newtClearKeyBuffer(void) { int newtOpenWindow(int left, int top, int width, int height, char * title) { - int i, j, row, col; + int j, row, col; int n; newtFlushInput(); diff --git a/newt.h b/newt.h index aab64d4..4beb2e3 100644 --- a/newt.h +++ b/newt.h @@ -26,6 +26,8 @@ extern "C" { #define NEWT_COLORSET_FULLSCALE 20 #define NEWT_COLORSET_DISENTRY 21 #define NEWT_COLORSET_COMPACTBUTTON 22 +#define NEWT_COLORSET_ACTSELLISTBOX 23 +#define NEWT_COLORSET_SELLISTBOX 24 struct newtColors { char * rootFg, * rootBg; @@ -48,6 +50,8 @@ struct newtColors { char * emptyScale, * fullScale; char * disabledEntryFg, * disabledEntryBg; char * compactButtonFg, * compactButtonBg; + char * actSelListboxFg, * actSelListboxBg; + char * selListboxFg, * selListboxBg; }; enum newtFlagsSense { NEWT_FLAGS_SET, NEWT_FLAGS_RESET }; @@ -132,7 +136,9 @@ int newtListboxAddEntry(newtComponent co, char * text, void * data); int newtListboxInsertEntry(newtComponent co, char * text, void * data, int num); int newtListboxDeleteEntry(newtComponent co, int num); void newtListboxGetEntry(newtComponent co, int num, char **text, void **data); - +void **newtListboxGetSelection(newtComponent co); +void newtListboxClearSelection(newtComponent co); + newtComponent newtTextbox(int left, int top, int with, int height, int flags); void newtTextboxSetText(newtComponent co, const char * text); void newtTextboxSetHeight(newtComponent co, int height); diff --git a/test.c b/test.c index 66b4ec7..8cb7f29 100644 --- a/test.c +++ b/test.c @@ -36,6 +36,7 @@ void main(void) { struct callbackInfo cbis[3]; char results[10]; char * enr2, * enr3, * scaleVal; + void ** selectedList; int i; char buf[20]; @@ -86,17 +87,17 @@ void main(void) { newtFormAddComponents(f, b1, b2, l1, l2, l3, e1, e2, e3, chklist, NULL); newtFormAddComponents(f, rsf, scale, NULL); - lb = newtListbox(45, 3, 4, 0); - newtListboxAddEntry(lb, "First", NULL); - newtListboxAddEntry(lb, "Second", NULL); - newtListboxAddEntry(lb, "Third", NULL); - newtListboxAddEntry(lb, "Fourth", NULL); - newtListboxAddEntry(lb, "Fifth", NULL); - newtListboxAddEntry(lb, "Sixth", NULL); - newtListboxAddEntry(lb, "Seventh", NULL); - newtListboxAddEntry(lb, "Eighth", NULL); - newtListboxAddEntry(lb, "Ninth", NULL); - newtListboxAddEntry(lb, "Tenth", NULL); + lb = newtListbox(45, 3, 4, NEWT_FLAG_MULTIPLE); + 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"); 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"); @@ -117,6 +118,8 @@ void main(void) { enr2 = strdup(enr2); enr3 = strdup(enr3); + selectedList = newtListboxGetSelection(lb); + newtFormDestroy(f); newtPopWindow(); @@ -126,4 +129,10 @@ void main(void) { printf("got string 1: %s\n", scaleVal); printf("got string 2: %s\n", enr2); printf("got string 3: %s\n", enr3); + + if(selectedList) { + printf("\nSelected listbox items:\n"); + for(i = 0; selectedList[i]; i++) + puts(selectedList[i]); + } }