]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
basic grid implementation fully functional
authorewt <ewt>
Fri, 12 Sep 1997 02:10:22 +0000 (02:10 +0000)
committerewt <ewt>
Fri, 12 Sep 1997 02:10:22 +0000 (02:10 +0000)
grid.c
newt.h
testgrid.c
textbox.c

diff --git a/grid.c b/grid.c
index 8eadf728699e0dd4b3c3f59b620654bbe5023d15..3f844503995ea136aa557a7870b97e661c6de890 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -57,10 +57,25 @@ void newtGridSetField(newtGrid grid, int col, int row,
     field->padTop = padTop;
     field->padBottom = padBottom;
     field->anchor = anchor;
+    field->flags = flags;
 
     grid->width = grid->height = -1;
 }
 
+static void distSpace(int extra, int items, int * list) {
+    int all, some, i;
+
+    all = extra / items;
+    some = extra % items;
+    for (i = 0; i < items; i++) {
+       list[i] += all;
+       if (some) {
+           list[i]++;
+           some--;
+       }
+    }
+}
+
 static void shuffleGrid(newtGrid grid, int left, int top, int set) {
     struct gridField * field;
     int row, col;
@@ -106,7 +121,7 @@ static void shuffleGrid(newtGrid grid, int left, int top, int set) {
            if (field->type == NEWT_GRID_SUBGRID) {
                /* we'll have to redo this later */
                if (field->u.grid->height == -1) 
-                   shuffleGrid(field->u.grid, left, top, 0);
+                   shuffleGrid(field->u.grid, 0, 0, 0);
                j = field->u.grid->height;
            } else {
                j = field->u.co->height;
@@ -115,7 +130,7 @@ static void shuffleGrid(newtGrid grid, int left, int top, int set) {
            j += field->padTop + field->padBottom;
 
            i += j;
-           if (j > heights[col]) heights[col] = j;
+           if (j > heights[row]) heights[row] = j;
        }
 
        if (i > minHeight) minHeight = i;
@@ -127,6 +142,9 @@ static void shuffleGrid(newtGrid grid, int left, int top, int set) {
 
     if (!set) return;
 
+    distSpace(grid->width - minWidth, grid->cols, widths);
+    distSpace(grid->height - minHeight, grid->rows, heights);
+
     thisTop = top;
     for (row = 0; row < grid->rows; row++) {
        i = 0;
@@ -137,7 +155,7 @@ static void shuffleGrid(newtGrid grid, int left, int top, int set) {
            x = thisLeft + field->padLeft;
            remx = widths[col] - field->padLeft - field->padRight;
            y = thisTop + field->padTop;
-           remy = heights[col] - field->padTop - field->padBottom;
+           remy = heights[row] - field->padTop - field->padBottom;
 
            if (field->type == NEWT_GRID_SUBGRID) {
                remx -= field->u.grid->width;
@@ -147,18 +165,28 @@ static void shuffleGrid(newtGrid grid, int left, int top, int set) {
                remy -= field->u.co->height;
            }
 
-           if (field->anchor & NEWT_ANCHOR_RIGHT)
-               x += remx;
-           else if (!(field->anchor & NEWT_ANCHOR_LEFT))
-               x += (remx / 2);
-        
-           if (field->anchor & NEWT_ANCHOR_BOTTOM)
-               y += remx;
-           else if (!(field->anchor & NEWT_ANCHOR_TOP))
-               y += (remy / 2);
+           if (!(field->flags & NEWT_GRID_FLAG_GROWX)) {
+               if (field->anchor & NEWT_ANCHOR_RIGHT)
+                   x += remx;
+               else if (!(field->anchor & NEWT_ANCHOR_LEFT))
+                   x += (remx / 2);
+           }
         
+           if (!(field->flags & NEWT_GRID_FLAG_GROWY)) {
+               if (field->anchor & NEWT_ANCHOR_BOTTOM)
+                   y += remx;
+               else if (!(field->anchor & NEWT_ANCHOR_TOP))
+                   y += (remy / 2);
+           }
 
            if (field->type == NEWT_GRID_SUBGRID) {
+               if (field->flags & NEWT_GRID_FLAG_GROWX)
+                   field->u.grid->width = widths[col] - field->padLeft 
+                                               - field->padRight;
+               if (field->flags & NEWT_GRID_FLAG_GROWY)
+                   field->u.grid->height = heights[col] - field->padTop
+                                               - field->padBottom;
+
                shuffleGrid(field->u.grid, x, y, 1);
            } else {
                field->u.co->left = x;
@@ -175,7 +203,6 @@ static void shuffleGrid(newtGrid grid, int left, int top, int set) {
 }
 
 void newtGridPlace(newtGrid grid, int left, int top) {
-    grid->width = grid->height = -1;
     shuffleGrid(grid, left, top, 1);
 }
 
@@ -197,3 +224,12 @@ void newtGridFree(newtGrid grid, int recurse) {
     free(grid);
 }
 
+void newtGridGetSize(newtGrid grid, int * width, int * height) {
+    if (grid->width == -1 || grid->height == -1) {
+       grid->width = grid->height = -1;
+       shuffleGrid(grid, 0, 0, 1);
+    }
+
+    *width = grid->width;
+    *height = grid->height;
+}
diff --git a/newt.h b/newt.h
index 567353b5b11eac7b314746e1151916fbe85c6601..f7b44350112fb69167ca9515e381d9e2fc963aa1 100644 (file)
--- a/newt.h
+++ b/newt.h
@@ -239,6 +239,7 @@ void newtGridSetField(newtGrid grid, int col, int row,
                      int flags);
 void newtGridPlace(newtGrid grid, int left, int top);
 void newtGridFree(newtGrid grid, int recurse);
+void newtGridGetSize(newtGrid grid, int * width, int * height);
 
 #ifdef __cplusplus
 } /* End of extern "C" { */
index 07634e1cd3781dd1da9c0cfdbf6d06f5e214d5c0..a36cba37f89a510afd83cb067af65c9a8d1eeebd 100644 (file)
@@ -7,8 +7,8 @@
 
 int main(void) {
     newtComponent b1, b2, b3, b4;
-    newtComponent answer, f;
-    newtGrid grid;
+    newtComponent answer, f, t;
+    newtGrid grid, subgrid;
 
     newtInit();
     newtCls();
@@ -37,6 +37,33 @@ int main(void) {
     newtFormDestroy(f);
 
     newtPopWindow();
+
+    newtOpenWindow(10, 5, 45, 15, "another example");
+
+    t = newtTextbox(-1, -1, 40, 5, NEWT_FLAG_WRAP);
+    newtTextboxSetText(t, "This is a quite a bit of text. It is 40 "
+                         "columns long, so some wrapping should be "
+                         "done. Did you know that the quick, brown "
+                         "fox jumped over the lazy dog?");
+    
+    b1 = newtButton(-1, -1, "Okay");
+    b2 = newtButton(-1, -1, "Cancel");
+
+    grid = newtCreateGrid(1, 2);
+    subgrid = newtCreateGrid(2, 1);
+
+    newtGridSetField(subgrid, 0, 0, NEWT_GRID_COMPONENT, b1, 0, 0, 0, 0, 0, 0);
+    newtGridSetField(subgrid, 1, 0, NEWT_GRID_COMPONENT, b2, 0, 0, 0, 0, 0, 0);
+
+    newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 0, 0, 0);
+    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, 0, 0, 0, 0, 0,
+                       NEWT_GRID_FLAG_GROWX);
+    newtGridPlace(grid, 1, 1);
+
+    f = newtForm(NULL, NULL, 0);
+    newtFormAddComponents(f, b1, t, b2, NULL);
+    answer = newtRunForm(f);
+
     newtFinished();
 
     return 0;
index e006ac5a2dd585581eef3b15b91fab5ed6853d37..c90782dfa565323813cc53d63e8c719615a00b65 100644 (file)
--- a/textbox.c
+++ b/textbox.c
@@ -155,6 +155,8 @@ static void textboxDraw(newtComponent c) {
        newtScrollbarSet(tb->sb, tb->topLine, size ? size : 0);
        tb->sb->ops->draw(tb->sb);
     }
+
+    SLsmg_set_color(NEWT_COLORSET_TEXTBOX);
    
     for (i = 0; (i + tb->topLine) < tb->numLines && i < c->height; i++) {
        newtGotorc(c->top + i, c->left);