]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
multi-state checkboxtrees. Whee...
authornotting <notting>
Thu, 12 Aug 1999 18:15:37 +0000 (18:15 +0000)
committernotting <notting>
Thu, 12 Aug 1999 18:15:37 +0000 (18:15 +0000)
checkboxtree.c
newt.h
testtree.c

index 535266e921b9840828ba2f65ab4f006314b0f363..5b7ee50cefe75f56d9d59986701c2183b0d29a85 100644 (file)
@@ -37,7 +37,7 @@ static void ctMapped(newtComponent co, int isMapped);
 static struct items * findItem(struct items * items, const void * data);
 static void buildFlatList(newtComponent co);
 static void doBuildFlatList(struct CheckboxTree * ct, struct items * item);
-enum countWhat { COUNT_EXPOSED, COUNT_SELECTED };
+enum countWhat { COUNT_EXPOSED=0, COUNT_SELECTED=1 };
 static int countItems(struct items * item, enum countWhat justExposed);
 
 static struct componentOps ctOps = {
@@ -52,7 +52,7 @@ static int countItems(struct items * item, enum countWhat what) {
     int count = 0;
 
     while (item) {
-        if ((!item->branch) || (what == COUNT_EXPOSED))
+        if ((!item->branch && item->selected == what) || (what == COUNT_EXPOSED))
            count++;
        if (item->branch || (what == COUNT_EXPOSED && item->selected))
            count += countItems(item->branch, what);
@@ -256,36 +256,52 @@ static struct items * findItem(struct items * items, const void * data) {
     return NULL;
 }
 
-static void listSelected(struct items * items, int * num, void ** list) {
+static void listSelected(struct items * items, int * num, void ** list, int seqindex) {
     while (items) {
-        if (items->selected && !items->branch)
+           if ((seqindex ? items->selected==seqindex : items->selected) && !items->branch)
            list[(*num)++] = items->data;
        if (items->branch)
-           listSelected(items->branch, num, list);
+           listSelected(items->branch, num, list, seqindex);
        items = items->next;
     }
 }
 
 void ** newtCheckboxTreeGetSelection(newtComponent co, int *numitems)
+{
+    return newtCheckboxTreeGetMultiSelection(co, numitems, 0);
+}
+
+void ** newtCheckboxTreeGetMultiSelection(newtComponent co, int *numitems, char seqnum)
 {
     struct CheckboxTree * ct;
     void **retval;
+    int seqindex=0;
 
     if(!co || !numitems) return NULL;
 
     ct = co->data;
+       
+    if (seqnum) {
+           while( ct->seq[seqindex] && ( ct->seq[seqindex] != seqnum )) seqindex++;
+    } else {
+           seqindex = 0;
+    }
 
-    *numitems = countItems(ct->itemlist, COUNT_SELECTED);
+       *numitems = countItems(ct->itemlist, (seqindex ? seqindex : COUNT_SELECTED));
     if (!*numitems) return NULL;
     
     retval = malloc(*numitems * sizeof(void *));
     *numitems = 0;
-    listSelected(ct->itemlist, numitems, retval);
+    listSelected(ct->itemlist, numitems, retval, seqindex);
 
     return retval;
 }
 
 newtComponent newtCheckboxTree(int left, int top, int height, int flags) {
+       return newtCheckboxTreeMulti(left, top, height, NULL, flags);
+}
+
+newtComponent newtCheckboxTreeMulti(int left, int top, int height, char *seq, int flags) {
     newtComponent co;
     struct CheckboxTree * ct;
 
@@ -301,6 +317,10 @@ newtComponent newtCheckboxTree(int left, int top, int height, int flags) {
     ct->firstItem = NULL;
     ct->currItem = NULL;
     ct->flatList = NULL;
+       if (seq)
+         ct->seq = strdup(seq);
+       else
+         ct->seq = strdup(" *");
     if (flags & NEWT_FLAG_SCROLL) {
        ct->sb = newtVerticalScrollbar(left, top, height,
                                       COLORSET_LISTBOX, COLORSET_ACTLISTBOX);
@@ -348,7 +368,13 @@ int ctSetItem(newtComponent co, struct items *item, enum newtFlagsSense sense)
            item->selected = 1;
            break;
        case NEWT_FLAGS_TOGGLE:
-           item->selected = !item->selected;
+           if (item->branch)
+             item->selected = !item->selected;
+           else {
+                   item->selected++;
+                   if (item->selected==strlen(ct->seq))
+                     item->selected = 0;
+           }
            break;
     }
 
@@ -400,8 +426,11 @@ static void ctDraw(newtComponent co) {
            else
                SLsmg_write_string("<+> ");
        } else {
-           if ((*item)->selected) 
-               SLsmg_write_string("[*] ");
+           if ((*item)->selected)  {
+                   char tmp[5];
+                   snprintf(tmp,5,"[%c] ",ct->seq[(*item)->selected]);
+               SLsmg_write_string(tmp);
+           }
            else
                SLsmg_write_string("[ ] ");
        }
@@ -434,6 +463,7 @@ static void ctDestroy(newtComponent co) {
        item = nextitem;
     }
 
+    free(ct->seq);
     free(ct);
     free(co);
 }
diff --git a/newt.h b/newt.h
index f1358ac4339a14fb8d76739c457cd79185d86d2d..b58ba0d736e64b5bfa0a44bd0195b841136747d0 100644 (file)
--- a/newt.h
+++ b/newt.h
@@ -167,7 +167,9 @@ void newtListboxSelectItem(newtComponent co, const void * key,
        enum newtFlagsSense sense);
 
 newtComponent newtCheckboxTree(int left, int top, int height, int flags);
+newtComponent newtCheckboxTreeMulti(int left, int top, int height, char *seq, int flags);
 void ** newtCheckboxTreeGetSelection(newtComponent co, int *numitems);
+void ** newtCheckboxTreeGetMultiSelection(newtComponent co, int *numitems, char seqnum);
 /* last item is NEWT_ARG_LAST for all of these */
 int newtCheckboxTreeAddItem(newtComponent co, 
                            const char * text, const void * data,
index 514f5622245777add71031ae95cdb8344a519ebc..54254e383e668c278b7f3bc9a0238114475924ff 100644 (file)
@@ -18,7 +18,7 @@ int main(void) {
     newtInit();
     newtCls();
 
-    checktree = newtCheckboxTree(-1, -1, 10, NEWT_FLAG_SCROLL);
+    checktree = newtCheckboxTreeMulti(-1, -1, 10, " ab", NEWT_FLAG_SCROLL);
     newtCheckboxTreeAddItem(checktree, "Numbers", (void *) 2, 0,
                            NEWT_ARG_APPEND, NEWT_ARG_LAST);
     newtCheckboxTreeAddItem(checktree, "Really really long thing",
@@ -101,11 +101,22 @@ int main(void) {
     if (!result || !numselected)
        printf("none selected\n");
     else
-       printf("Current selection:\n");
+       printf("Current selection (all) (%d):\n", numselected);
     for (i = 0; i < numselected; i++) {
        j = (int) *ptr++;
        printf("%d\n", j);
     }
+    result = newtCheckboxTreeGetMultiSelection(checktree, &numselected, 'b');
+    ptr = result;
+    if (!result || !numselected)
+       printf("none selected\n");
+    else
+       printf("Current selection (b) (%d):\n",numselected);
+    for (i = 0; i < numselected; i++) {
+       j = (int) *ptr++;
+       printf("%d\n", j);
+    }
+       
     if (result)
        free(result);