From: ewt Date: Fri, 12 Sep 1997 02:10:22 +0000 (+0000) Subject: basic grid implementation fully functional X-Git-Tag: r0-12~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=676cfb933084b99f87023a0b1fbe44ab06566f6d;p=thirdparty%2Fnewt.git basic grid implementation fully functional --- diff --git a/grid.c b/grid.c index 8eadf72..3f84450 100644 --- 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 567353b..f7b4435 100644 --- 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" { */ diff --git a/testgrid.c b/testgrid.c index 07634e1..a36cba3 100644 --- a/testgrid.c +++ b/testgrid.c @@ -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; diff --git a/textbox.c b/textbox.c index e006ac5..c90782d 100644 --- 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);