]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
1) added ismapped field
authorewt <ewt>
Mon, 29 Sep 1997 19:53:49 +0000 (19:53 +0000)
committerewt <ewt>
Mon, 29 Sep 1997 19:53:49 +0000 (19:53 +0000)
2) reworked listbox to deal with borders properly

form.c
listbox.c
newt_pr.h
test.c

diff --git a/form.c b/form.c
index e0d681eb94f9a5e391ab7add49460d0a16f54f82..f2811e855b2cff21012256519243864cb5047dd1 100644 (file)
--- a/form.c
+++ b/form.c
@@ -216,6 +216,7 @@ void newtDrawForm(newtComponent co) {
            /* only draw it if it'll fit on the screen vertically */
            if (componentFits(co, i)) {
                el->co->top = el->top - form->vertOffset;
+               el->co->isMapped = 1;
                el->co->ops->draw(el->co);
            } else {
                el->co->top = -1;               /* tell it not to draw itself */
index 7e2f36e27d074ec3653148f27192e1188758c145..ebbbd43c006d61b433ec10bd8b3c493659a3eab6 100644 (file)
--- a/listbox.c
+++ b/listbox.c
@@ -23,8 +23,10 @@ struct items {
 /* Holds all the relevant information for this listbox */
 struct listbox {
     newtComponent sb;   /* Scrollbar on right side of listbox */
-    int curWidth;      /* size of text w/o scrollbar */
+    int curWidth;      /* size of text w/o scrollbar or border*/
+    int curHeight;     /* size of text w/o border */
     int sbAdjust;
+    int bdxAdjust, bdyAdjust;
     int numItems, numSelected; 
     int userHasSetWidth;
     int currItem, startShowItem; /* startShowItem is the first item displayed
@@ -36,7 +38,6 @@ struct listbox {
     int grow;
     int flags; /* flags for this listbox, right now just
                  NEWT_FLAG_RETURNEXIT */
-    int beenDisplayed;
 };
 
 static void listboxDraw(newtComponent co);
@@ -44,6 +45,8 @@ static void listboxDestroy(newtComponent co);
 static struct eventResult listboxEvent(newtComponent co, struct event ev);
 static void newtListboxRealSetCurrent(newtComponent co);
 static void listboxPlace(newtComponent co);
+static inline void updateWidth(newtComponent co, struct listbox * li, 
+                               int maxField);
 
 static struct componentOps listboxOps = {
     listboxDraw,
@@ -80,10 +83,17 @@ newtComponent newtListbox(int left, int top, int height, int flags) {
     li->numSelected = 0;
     li->isActive = 0;
     li->userHasSetWidth = 0;
-    li->beenDisplayed = 0;
     li->startShowItem = 0;
     li->sbAdjust = 0;
-    li->flags = flags & (NEWT_FLAG_RETURNEXIT|NEWT_FLAG_DOBORDER|NEWT_FLAG_MULTIPLE);
+    li->bdxAdjust = 0;
+    li->bdyAdjust = 0;
+    li->flags = flags & (NEWT_FLAG_RETURNEXIT | NEWT_FLAG_DOBORDER | 
+                        NEWT_FLAG_MULTIPLE);
+
+    if (li->flags & NEWT_FLAG_DOBORDER) {
+       li->bdxAdjust = 2;
+       li->bdyAdjust = 1;
+    }
 
     if (height) {
        li->grow = 0;
@@ -101,18 +111,29 @@ newtComponent newtListbox(int left, int top, int height, int flags) {
 
     li->sb = sb;
     co->data = li;
+    co->isMapped = 0;
     co->left = left;
     co->top = top;
     co->height = height;
-    li->curWidth = 5;
-    co->width = li->curWidth + li->sbAdjust;
     co->ops = &listboxOps;
     co->takesFocus = 1;
     co->callback = NULL;
 
+    updateWidth(co, li, 5);
+    li->curHeight = height - (2 * li->bdyAdjust);
+
     return co;
 }
 
+static inline void updateWidth(newtComponent co, struct listbox * li, 
+                               int maxField) {
+    li->curWidth = maxField;
+    co->width = li->curWidth + li->sbAdjust + 2 * li->bdxAdjust;
+
+    if (li->sb)
+       li->sb->left = co->left + co->width - 1;
+}
+
 void newtListboxSetCurrent(newtComponent co, int num)
 {
     struct listbox * li = co->data;
@@ -140,18 +161,18 @@ newtListboxRealSetCurrent(newtComponent co)
     struct listbox * li = co->data;
     if(li->sb)
        newtScrollbarSet(li->sb, li->currItem + 1, li->numItems);
-    if (li->beenDisplayed) listboxDraw(co);
+    listboxDraw(co);
     if(co->callback) co->callback(co, co->callbackData);
 }
 
-void newtListboxSetWidth(newtComponent co , int width) {
+void newtListboxSetWidth(newtComponent co, int width) {
     struct listbox * li = co->data;
     
     co->width = width;
-    li->curWidth = co->width - li->sbAdjust;
+    li->curWidth = co->width - li->sbAdjust - 2 * li->bdxAdjust;
     li->userHasSetWidth = 1;
     li->sb->left = co->width + co->left - 1;
-    if (li->beenDisplayed) listboxDraw(co);
+    listboxDraw(co);
 }
 
 void * newtListboxGetCurrent(newtComponent co) {
@@ -192,7 +213,7 @@ void newtListboxSelectItem(newtComponent co, int item,
                        iitem->isSelected = !iitem->isSelected;
        }
     }
-    if (li->beenDisplayed) listboxDraw(co);
+    listboxDraw(co);
 }
 
 void newtListboxClearSelection(newtComponent co)
@@ -204,7 +225,7 @@ void newtListboxClearSelection(newtComponent co)
        item = item->next)
        item->isSelected = 0;
     li->numSelected = 0;
-    if (li->beenDisplayed) listboxDraw(co);
+    listboxDraw(co);
 }
 
 /* Free the returned array after use, but NOT the values in the array */
@@ -243,16 +264,12 @@ void newtListboxSetText(newtComponent co, int num, const char * text) {
        free(item->key);
        item->key = strdup(text);
     }
-    if (li->userHasSetWidth == 0
-       && strlen(text) > (size_t)li->curWidth) {
-       li->curWidth = strlen(text);
-       co->width = li->curWidth + li->sbAdjust;
-       if (li->sb)
-           li->sb->left = co->left + co->width - 1;
+    if (li->userHasSetWidth == 0 && strlen(text) > li->curWidth) {
+       updateWidth(co, li, strlen(text));
     }
 
     if (num >= li->startShowItem && num <= li->startShowItem + co->height)
-       if (li->beenDisplayed) listboxDraw(co);
+       listboxDraw(co);
 }
 
 void newtListboxSetEntry(newtComponent co, int num, const char * text) {
@@ -283,20 +300,14 @@ int newtListboxAddEntry(newtComponent co, const char * text,
        item = li->boxItems = malloc(sizeof(struct items));
     }
 
-    if (li->userHasSetWidth == 0
-       && text && (strlen(text) > (size_t)li->curWidth))
-       li->curWidth = strlen(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->isSelected = 0;
     
-    if (li->sb)
-       li->sb->left = co->left + co->width - 1;
-
     if (li->grow)
        co->height++;
-    if(li->userHasSetWidth == 0)
-       co->width = li->curWidth + li->sbAdjust;
     li->numItems++;
 
     return li->numItems;
@@ -328,20 +339,17 @@ int newtListboxInsertEntry(newtComponent co, const char * text,
        item->next = NULL;
     }
 
-    if (li->userHasSetWidth == 0
-       && text && (strlen(text) > (size_t)li->curWidth))
-       li->curWidth = strlen(text);
+    if (!li->userHasSetWidth && text && (strlen(text) > li->curWidth))
+       updateWidth(co, li, strlen(text));
 
     item->key = strdup(text?text:"(null)"); item->data = data;
     item->isSelected = 0;
     
     if (li->sb)
        li->sb->left = co->left + co->width - 1;
-    if (li->userHasSetWidth == 0)
-       co->width = li->curWidth + li->sbAdjust;
     li->numItems++;
 
-    if (li->beenDisplayed) listboxDraw(co);
+    listboxDraw(co);
 
     return li->numItems;
 }
@@ -385,14 +393,11 @@ int newtListboxDeleteEntry(newtComponent co, int num) {
        if((t = strlen(item->key)) > widest) widest = t;
 
     /* Adjust the listbox width */
-    if (li->userHasSetWidth == 0) {
-       li->curWidth = widest;
-       co->width = li->curWidth + li->sbAdjust;
-       if (li->sb)
-               li->sb->left = co->left + co->width - 1;
+    if (!li->userHasSetWidth) {
+       updateWidth(co, li, widest);
     }
 
-    if (li->beenDisplayed) listboxDraw(co);
+    listboxDraw(co);
 
     return li->numItems;
 }
@@ -410,10 +415,8 @@ void newtListboxClear(newtComponent co)
     }
     li->numItems = li->numSelected = li->currItem = li->startShowItem = 0;
     li->boxItems = NULL;
-    if(li->userHasSetWidth == 0) {
-       li->curWidth = 5;
-       co->width = li->curWidth + li->sbAdjust;
-    }
+    if (!li->userHasSetWidth) 
+       updateWidth(co, li, 5);
 }
 
 /* If you don't want to get back the text, pass in NULL for the ptr-ptr. Same
@@ -451,7 +454,7 @@ static void listboxDraw(newtComponent co)
     struct items *item;
     int i, j;
 
-    li->beenDisplayed = 1;
+    if (!co->isMapped) return ;
 
     if(li->sb)
        li->sb->ops->draw(li->sb);
@@ -462,7 +465,7 @@ static void listboxDraw(newtComponent co)
       else
           SLsmg_set_color(NEWT_COLORSET_LISTBOX);
 
-      newtDrawBox(co->left-1, co->top-1, co->width+5, co->height+2, 0);
+      newtDrawBox(co->left, co->top, co->width, co->height, 0);
     }
 
     SLsmg_set_color(NEWT_COLORSET_LISTBOX);
@@ -472,10 +475,10 @@ static void listboxDraw(newtComponent co)
 
     j = i;
 
-    for (i = 0; item != NULL && i < co->height; i++, item = item->next) {
+    for (i = 0; item != NULL && i < li->curHeight; i++, item = item->next) {
        if (!item->key) continue;
 
-       newtGotorc(co->top + i, co->left + 1);
+       newtGotorc(co->top + i + li->bdyAdjust, co->left + li->bdxAdjust);
        if(j + i == li->currItem) {
            if(item->isSelected)
                SLsmg_set_color(NEWT_COLORSET_ACTSELLISTBOX);
@@ -518,10 +521,10 @@ static struct eventResult listboxEvent(newtComponent co, struct event ev) {
            if(li->numItems <= 0) break;
            if(li->currItem < li->numItems - 1) {
                li->currItem++;
-               if(li->currItem > (li->startShowItem + co->height - 1)) {
-                   li->startShowItem = li->currItem - co->height + 1;
-                   if(li->startShowItem + co->height > li->numItems)
-                       li->startShowItem = li->numItems - co->height;
+               if(li->currItem > (li->startShowItem + li->curHeight - 1)) {
+                   li->startShowItem = li->currItem - li->curHeight + 1;
+                   if(li->startShowItem + li->curHeight > li->numItems)
+                       li->startShowItem = li->numItems - li->curHeight;
                }
                if(li->sb)
                    newtScrollbarSet(li->sb, li->currItem + 1, li->numItems);
@@ -553,10 +556,10 @@ static struct eventResult listboxEvent(newtComponent co, struct event ev) {
 
          case NEWT_KEY_PGUP:
            if(li->numItems <= 0) break;
-           li->startShowItem -= co->height - 1;
+           li->startShowItem -= li->curHeight - 1;
            if(li->startShowItem < 0)
                li->startShowItem = 0;
-           li->currItem -= co->height - 1;
+           li->currItem -= li->curHeight - 1;
            if(li->currItem < 0)
                li->currItem = 0;
            newtListboxRealSetCurrent(co);
@@ -565,11 +568,11 @@ static struct eventResult listboxEvent(newtComponent co, struct event ev) {
 
          case NEWT_KEY_PGDN:
            if(li->numItems <= 0) break;
-           li->startShowItem += co->height;
-           if(li->startShowItem > (li->numItems - co->height)) {
-               li->startShowItem = li->numItems - co->height;
+           li->startShowItem += li->curHeight;
+           if(li->startShowItem > (li->numItems - li->curHeight)) {
+               li->startShowItem = li->numItems - li->curHeight;
            }
-           li->currItem += co->height;
+           li->currItem += li->curHeight;
            if(li->currItem > li->numItems) {
                li->currItem = li->numItems - 1;
            }
index 61366dba33a985135f86b1d829029599b4de1419..9f1f034050c8440a3eccb1f3ad24fb7af226ff22 100644 (file)
--- a/newt_pr.h
+++ b/newt_pr.h
@@ -31,6 +31,7 @@ struct newtComponent_struct {
     int height, width; 
     int top, left;
     int takesFocus;
+    int isMapped;
 
     struct componentOps * ops;
 
diff --git a/test.c b/test.c
index 7ac3524b83aec3eb2ba249f5a2b84e93db523696..25abfd78bef5fa8a3e1b29e259150b4efd0d9079 100644 (file)
--- a/test.c
+++ b/test.c
@@ -87,7 +87,7 @@ int main(void) {
     newtFormAddComponents(f, b1, b2, l1, l2, l3, e1, e2, e3, chklist, NULL);
     newtFormAddComponents(f, rsf, scale, NULL);
 
-    lb = newtListbox(45, 3, 4, NEWT_FLAG_MULTIPLE);
+    lb = newtListbox(45, 1, 4, NEWT_FLAG_MULTIPLE | NEWT_FLAG_DOBORDER);
     newtListboxAddEntry(lb, "First", "First");
     newtListboxAddEntry(lb, "Second", "Second");
     newtListboxAddEntry(lb, "Third", "Third");