From: ewt Date: Thu, 11 Sep 1997 03:22:22 +0000 (+0000) Subject: 1) added "place" operation X-Git-Tag: r0-12~40 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e69227403085f06a325d2c29a9c525fa5d1113d7;p=thirdparty%2Fnewt.git 1) added "place" operation 2) first pass at grid auto-placement implementation --- diff --git a/Makefile b/Makefile index d5a0c9f..e12a321 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ endif VERSION = 0.10 SONAME = 0.10 -PROGS = test whiptail whiptcl.so +PROGS = test whiptail whiptcl.so testgrid TESTOBJS = test.o NDIALOGOBJS = whiptail.o dialogboxes.o popt.o WHIPTCLOBJS = whiptcl.o dialogboxes.o popt.o @@ -16,7 +16,7 @@ LIBNEWT = libnewt.a LIBNEWTSH = libnewt.so.$(VERSION) LIBNEWTSONAME = libnewt.so.$(SONAME) LIBOBJS = newt.o button.o form.o checkbox.o entry.o label.o listbox.o \ - scrollbar.o textbox.o scale.o + scrollbar.o textbox.o scale.o grid.o SHCFLAGS = -fPIC @@ -43,6 +43,9 @@ all: $(TARGET) test: $(TESTOBJS) $(LIBNEWT) gcc -g -o test $(TESTOBJS) $(LIBNEWT) $(LIBS) +testgrid: testgrid.o $(LIBNEWT) + gcc -g -o testgrid testgrid.o $(LIBNEWT) $(LIBS) + whiptail: $(NDIALOGOBJS) $(LIBNEWTSH) gcc -g -o whiptail $(NDIALOGOBJS) $(LIBNEWTSH) $(LIBS) diff --git a/button.c b/button.c index ee3543d..0846cf0 100644 --- a/button.c +++ b/button.c @@ -18,11 +18,13 @@ static void buttonDraw(newtComponent c); static void buttonDestroy(newtComponent co); static struct eventResult buttonEvent(newtComponent c, struct event ev); +static void buttonPlace(newtComponent co); static struct componentOps buttonOps = { buttonDraw, buttonEvent, buttonDestroy, + buttonPlace, } ; static newtComponent createButton(int left, int row, const char * text, int compact) { @@ -71,6 +73,13 @@ static void buttonDestroy(newtComponent co) { free(co); } +static void buttonPlace(newtComponent co) { + struct button * bu = co->data; + + newtGotorc(co->top, co->left); + bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF; +} + static void buttonDraw(newtComponent co) { buttonDrawIt(co, 0, 0); } diff --git a/checkbox.c b/checkbox.c index c4f24b5..ba3e190 100644 --- a/checkbox.c +++ b/checkbox.c @@ -30,6 +30,7 @@ static struct componentOps cbOps = { cbDraw, cbEvent, cbDestroy, + NULL, } ; newtComponent newtListitem(int left, int top, const char * text, int isDefault, diff --git a/entry.c b/entry.c index 005f9a5..43e598e 100644 --- a/entry.c +++ b/entry.c @@ -27,6 +27,7 @@ static struct componentOps entryOps = { entryDraw, entryEvent, entryDestroy, + NULL, } ; void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd) { diff --git a/grid.c b/grid.c new file mode 100644 index 0000000..421604c --- /dev/null +++ b/grid.c @@ -0,0 +1,167 @@ +#include +#include +#include + +#include "grid.h" +#include "newt.h" +#include "newt_pr.h" + +struct gridField { + enum newtGridElement type; + union { + newtGrid grid; + newtComponent co; + } u; +}; + +struct grid_s { + int rows, cols; + int width, height; /* totals, -1 means unknown */ + struct gridField ** fields; +}; + +newtGrid newtCreateGrid(int cols, int rows) { + newtGrid grid; + + grid = malloc(sizeof(*grid)); + grid->rows = rows; + grid->cols = cols; + + grid->fields = malloc(sizeof(*grid->fields) * cols); + while (cols--) { + grid->fields[cols] = malloc(sizeof(**(grid->fields)) * rows); + memset(grid->fields[cols], 0, sizeof(**(grid->fields)) * rows); + } + + grid->width = grid->height = -1; + + return grid; +} + +void newtGridSetField(newtGrid grid, int col, int row, + enum newtGridElement type, void * val) { + struct gridField * field = &grid->fields[col][row]; + + if (field->type == NEWT_GRID_SUBGRID) + newtGridFree(field->u.grid, 1); + + field->type = type; + field->u.co = (void *) val; + + grid->width = grid->height = -1; +} + +static void shuffleGrid(newtGrid grid, int left, int top, int set) { + struct gridField * field; + int row, col; + int i, j; + int minWidth, minHeight; + int colSpacing; + int * widths, * heights; + int thisLeft, thisTop; + + widths = alloca(sizeof(*widths) * grid->cols); + memset(widths, 0, sizeof(*widths) * grid->cols); + heights = alloca(sizeof(*heights) * grid->rows); + memset(heights, 0, sizeof(*heights) * grid->rows); + + minWidth = 0; + for (row = 0; row < grid->rows; row++) { + i = 0; + for (col = 0; col < grid->cols; col++) { + field = &grid->fields[col][row]; + if (field->type == NEWT_GRID_SUBGRID) { + /* we'll have to redo this later */ + if (field->u.grid->width == -1) + shuffleGrid(field->u.grid, left, top, 0); + j = field->u.grid->width; + } else { + j = field->u.co->width; + } + + i += j; + if (j > widths[col]) widths[col] = j; + } + + if (i > minWidth) minWidth = i; + } + + minHeight = 0; + for (col = 0; col < grid->cols; col++) { + i = 0; + for (row = 0; row < grid->rows; row++) { + field = &grid->fields[col][row]; + 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); + j = field->u.grid->height; + } else { + j = field->u.co->height; + } + + i += j; + if (j > heights[col]) heights[col] = j; + } + + if (i > minHeight) minHeight = i; + } + + if (grid->width == -1) { + colSpacing = 1; + } else { + colSpacing = (grid->width - minWidth) / (grid->cols - 1); + } + minWidth += colSpacing * (grid->cols - 1); + + if (minWidth < grid->width) grid->width = minWidth; /* ack! */ + if (minHeight < grid->height) grid->height = minHeight; /* ditto! */ + + if (!set) return; + + thisTop = top; + for (row = 0; row < grid->rows; row++) { + i = 0; + thisLeft = left; + for (col = 0; col < grid->cols; col++) { + field = &grid->fields[col][row]; + if (field->type == NEWT_GRID_SUBGRID) { + shuffleGrid(field->u.grid, thisLeft, thisTop, 1); + } else { + field->u.co->top = thisTop; + field->u.co->left = thisLeft; + if (field->u.co->ops->place) + field->u.co->ops->place(field->u.co); + } + + thisLeft += widths[col] + colSpacing; + } + + thisTop += heights[row]; + } +} + +void newtGridPlace(newtGrid grid, int left, int top) { + grid->width = grid->height = -1; + + shuffleGrid(grid, left, top, 1); +} + +void newtGridFree(newtGrid grid, int recurse) { + int row, col; + + for (col = 0; col < grid->cols; col++) { + if (recurse) { + for (row = 0; row < grid->rows; row++) { + if (grid->fields[col][row].type == NEWT_GRID_SUBGRID) + newtGridFree(grid->fields[col][row].u.grid, 1); + } + } + + free(grid->fields[col]); + } + + free(grid->fields); + free(grid); +} + diff --git a/label.c b/label.c index 2cc4348..c945d5a 100644 --- a/label.c +++ b/label.c @@ -17,6 +17,7 @@ static struct componentOps labelOps = { labelDraw, newtDefaultEventHandler, labelDestroy, + NULL, } ; newtComponent newtLabel(int left, int top, const char * text) { diff --git a/newt.h b/newt.h index efc3cb9..cb844d9 100644 --- a/newt.h +++ b/newt.h @@ -219,6 +219,16 @@ void newtFormDestroy(newtComponent form); #define NEWT_KEY_F11 NEWT_KEY_EXTRA_BASE + 111 #define NEWT_KEY_F12 NEWT_KEY_EXTRA_BASE + 112 +typedef struct grid_s * newtGrid; +enum newtGridElement { NEWT_GRID_EMPTY = 0, + NEWT_GRID_COMPONENT, NEWT_GRID_SUBGRID }; + +newtGrid newtCreateGrid(int cols, int rows); +void newtGridSetField(newtGrid grid, int col, int row, + enum newtGridElement type, void * val); +void newtGridPlace(newtGrid grid, int left, int top); +void newtGridFree(newtGrid grid, int recurse); + #ifdef __cplusplus } /* End of extern "C" { */ #endif diff --git a/newt_pr.h b/newt_pr.h index 7efa55b..61366db 100644 --- a/newt_pr.h +++ b/newt_pr.h @@ -64,6 +64,7 @@ struct componentOps { void (* draw)(newtComponent c); struct eventResult (* event)(newtComponent c, struct event ev); void (* destroy)(newtComponent c); + void (* place)(newtComponent c); } ; struct eventResult newtDefaultEventHandler(newtComponent c, diff --git a/scale.c b/scale.c index 2523144..fc53370 100644 --- a/scale.c +++ b/scale.c @@ -16,6 +16,7 @@ static struct componentOps scaleOps = { scaleDraw, newtDefaultEventHandler, NULL, + NULL, } ; newtComponent newtScale(int left, int top, int width, long long fullValue) { diff --git a/scrollbar.c b/scrollbar.c index 18e3bf8..57e7e63 100644 --- a/scrollbar.c +++ b/scrollbar.c @@ -18,6 +18,7 @@ static struct componentOps sbOps = { sbDraw, newtDefaultEventHandler, sbDestroy, + NULL, } ; void newtScrollbarSet(newtComponent co, int where, int total) { diff --git a/textbox.c b/textbox.c index 3238c25..e006ac5 100644 --- a/textbox.c +++ b/textbox.c @@ -26,6 +26,7 @@ static struct componentOps textboxOps = { textboxDraw, textboxEvent, textboxDestroy, + NULL, } ; void newtTextboxSetHeight(newtComponent co, int height) {