]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
1) added "place" operation
authorewt <ewt>
Thu, 11 Sep 1997 03:22:22 +0000 (03:22 +0000)
committerewt <ewt>
Thu, 11 Sep 1997 03:22:22 +0000 (03:22 +0000)
2) first pass at grid auto-placement implementation

Makefile
button.c
checkbox.c
entry.c
grid.c [new file with mode: 0644]
label.c
newt.h
newt_pr.h
scale.c
scrollbar.c
textbox.c

index d5a0c9f9c2fb1c3a6367dd1b5347b0e0e396a0ff..e12a321e9172aca88c95fe40925a3b7c0777d5f0 100644 (file)
--- 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)
 
index ee3543dae4f1753d7c638d2d089df53352c2b2d2..0846cf04c96fc033dc3a73bdd92e2fe560a5f4d3 100644 (file)
--- 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);
 }
index c4f24b5b4e1c441a742714c1e3bc50ae7fafbcfd..ba3e19034d46945d64be0fbece26ee3c9a332361 100644 (file)
@@ -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 005f9a549c895c18fd46417de4614ab0b40a9d76..43e598ee47c3e4cf8ad7a36a27dbc4f3aedbcbc5 100644 (file)
--- 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 (file)
index 0000000..421604c
--- /dev/null
+++ b/grid.c
@@ -0,0 +1,167 @@
+#include <alloca.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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 2cc4348938cb318f083c7f955daffeec2915dc06..c945d5a752d93be2a2cfa0506e29a39e7abb94c6 100644 (file)
--- 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 efc3cb95816277a79e7e10a9d9b301bd46ffded2..cb844d95f87aad04ff6228a5f3d5f5fc6ac9609f 100644 (file)
--- 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
index 7efa55b8ce6f112e4cb331715d5ee15e258eb0f7..61366dba33a985135f86b1d829029599b4de1419 100644 (file)
--- 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 252314450fe6716b69c8866ded410dc9b74d006e..fc533706075c29379fa6272819faa99f85d76748 100644 (file)
--- 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) {
index 18e3bf8ec53f53a487aa262ba3fa7725baf4738a..57e7e634d9248a8613774977040ebd904e765f7d 100644 (file)
@@ -18,6 +18,7 @@ static struct componentOps sbOps = {
     sbDraw,
     newtDefaultEventHandler,
     sbDestroy,
+    NULL,
 } ;
 
 void newtScrollbarSet(newtComponent co, int where, int total) {
index 3238c25ce823d5922a3c86b27892a3b9d8c2c11a..e006ac5a2dd585581eef3b15b91fab5ed6853d37 100644 (file)
--- a/textbox.c
+++ b/textbox.c
@@ -26,6 +26,7 @@ static struct componentOps textboxOps = {
     textboxDraw,
     textboxEvent,
     textboxDestroy,
+    NULL,
 } ;
 
 void newtTextboxSetHeight(newtComponent co, int height) {