static void buttonDestroy(newtComponent co);
static struct eventResult buttonEvent(newtComponent c,
struct event ev);
-static void buttonPlace(newtComponent co);
+static void buttonPlace(newtComponent co, int newLeft, int newTop);
static struct componentOps buttonOps = {
buttonDraw,
buttonEvent,
buttonDestroy,
buttonPlace,
+ newtDefaultMappedHandler,
} ;
static newtComponent createButton(int left, int row, const char * text, int compact) {
free(co);
}
-static void buttonPlace(newtComponent co) {
+static void buttonPlace(newtComponent co, int newLeft, int newTop) {
struct button * bu = co->data;
+ co->top = newTop;
+ co->left = newLeft;
+
newtGotorc(co->top, co->left);
bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF;
}
cbDraw,
cbEvent,
cbDestroy,
- NULL,
+ newtDefaultPlaceHandler,
+ newtDefaultMappedHandler,
} ;
newtComponent newtListitem(int left, int top, const char * text, int isDefault,
entryDraw,
entryEvent,
entryDestroy,
- NULL,
+ newtDefaultPlaceHandler,
+ newtDefaultMappedHandler,
} ;
void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd) {
co->left = left;
co->height = 1;
co->width = width;
+ co->isMapped = 0;
co->callback = NULL;
co->ops = &entryOps;
char * chptr;
int len;
- if (co->top == -1) return;
+ if (!co->isMapped) return;
if (en->flags & NEWT_FLAG_DISABLED)
SLsmg_set_color(NEWT_COLORSET_DISENTRY);
static void gotoComponent(struct form * form, int newComp);
static struct eventResult formEvent(newtComponent co, struct event ev);
static struct eventResult sendEvent(newtComponent comp, struct event ev);
+static void formPlace(newtComponent co, int left, int top);
-static struct componentOps formOps = {
+/* this isn't static as grid.c tests against it to find forms */
+struct componentOps formOps = {
newtDrawForm,
formEvent,
newtFormDestroy,
+ formPlace,
+ newtDefaultMappedHandler,
} ;
static inline int componentFits(newtComponent co, int compNum) {
co->left = -1;
co->isMapped = 0;
- co->takesFocus = 1;
+ co->takesFocus = 0; /* we may have 0 components */
co->ops = &formOps;
form->help = help;
void newtFormAddComponent(newtComponent co, newtComponent newco) {
struct form * form = co->data;
+ co->takesFocus = 1;
+
if (form->numCompsAlloced == form->numComps) {
form->numCompsAlloced += 5;
form->elements = realloc(form->elements,
va_end(ap);
}
+static void formPlace(newtComponent co, int left, int top) {
+ struct form * form = co->data;
+ int vertDelta, horizDelta;
+ struct element * el;
+ int i;
+
+ newtFormSetSize(co);
+
+ vertDelta = top - co->top;
+ horizDelta = left - co->left;
+ co->top = top;
+ co->left = left;
+
+ for (i = 0, el = form->elements; i < form->numComps; i++, el++) {
+ el->co->top += vertDelta;
+ el->top += vertDelta;
+ el->co->left += horizDelta;
+ el->left += horizDelta;
+ }
+}
+
void newtDrawForm(newtComponent co) {
struct form * form = co->data;
struct element * el;
SLsmg_set_color(form->background);
newtClearBox(co->left, co->top, co->width, co->height);
for (i = 0, el = form->elements; i < form->numComps; i++, el++) {
- /* the scrollbar *always* fits */
- if (el->co == form->vertBar)
+ /* the scrollbar *always* fits somewhere */
+ if (el->co == form->vertBar) {
+ el->co->ops->mapped(el->co, 1);
el->co->ops->draw(el->co);
- else {
+ } else {
/* 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->mapped(el->co, 1);
el->co->ops->draw(el->co);
} else {
- el->co->top = -1; /* tell it not to draw itself */
+ el->co->ops->mapped(el->co, 0);
}
}
}
int i, num;
er.result = ER_IGNORED;
+ if (!form->numComps) return er;
+
+ subco = form->elements[form->currComp].co;
switch (ev.when) {
case EV_EARLY:
}
}
- i = form->currComp;
- num = 0;
- while (er.result == ER_IGNORED && num != form->numComps ) {
- er = form->elements[i].co->ops->event(form->elements[i].co, ev);
+ if (form->numComps) {
+ i = form->currComp;
+ num = 0;
+ while (er.result == ER_IGNORED && num != form->numComps ) {
+ er = form->elements[i].co->ops->event(form->elements[i].co, ev);
- num++;
- i++;
- if (i == form->numComps) i = 0;
+ num++;
+ i++;
+ if (i == form->numComps) i = 0;
+ }
}
break;
form->beenSet = 1;
- /* figure out how big we are -- we do this as late as possible (i.e. here)
- to let grids delay there decisions as long as possible */
+ if (!form->numComps) return;
+
co->width = 0;
if (!form->fixedHeight) co->height = 0;
struct gridField ** fields;
};
+/* this is a bit of a hack */
+extern struct componentOps formOps[];
+
newtGrid newtCreateGrid(int cols, int rows) {
newtGrid grid;
shuffleGrid(field->u.grid, left, top, 0);
j = field->u.grid->width;
} else if (field->type == NEWT_GRID_COMPONENT) {
+ if (field->u.co->ops == formOps)
+ newtFormSetSize(field->u.co);
j = field->u.co->width;
} else
j = 0;
shuffleGrid(field->u.grid, x, y, 1);
} else if (field->type == NEWT_GRID_COMPONENT) {
- field->u.co->left = x;
- field->u.co->top = y;
- if (field->u.co->ops->place)
- field->u.co->ops->place(field->u.co);
+ field->u.co->ops->place(field->u.co, x, y);
}
thisLeft += widths[col];
int width, height;
newtGridGetSize(grid, &width, &height);
- newtOpenWindow(left, top, left + width, top + height, title);
+ newtOpenWindow(left, top, width + 2, height + 2, title);
newtGridPlace(grid, 1, 1);
}
/* this handles up to 50 items */
static newtGrid stackem(int isVert, enum newtGridElement type1, void * what1,
- va_list args) {
+ va_list args, int close) {
struct item {
enum newtGridElement type;
void * what;
for (i = 0; i < num; i++) {
newtGridSetField(grid, isVert ? 0 : i, isVert ? i : 0,
items[i].type, items[i].what,
- i ? (isVert ? 0 : 1) : 0,
- i ? (isVert ? 1 : 0) : 0, 0, 0, 0,
- items[i].type == NEWT_GRID_SUBGRID ?
- NEWT_GRID_FLAG_GROWX : 0);
+ close ? 0 : (i ? (isVert ? 0 : 1) : 0),
+ close ? 0 : (i ? (isVert ? 1 : 0) : 0), 0, 0, 0, 0);
}
return grid;
}
+newtGrid newtGridHCloseStacked(enum newtGridElement type1, void * what1, ...) {
+ va_list args;
+ newtGrid grid;
+
+ va_start(args, what1);
+
+ grid = stackem(0, type1, what1, args, 1);
+
+ va_start(args, what1);
+
+ return grid;
+}
+
+newtGrid newtGridVCloseStacked(enum newtGridElement type1, void * what1, ...) {
+ va_list args;
+ newtGrid grid;
+
+ va_start(args, what1);
+
+ grid = stackem(1, type1, what1, args, 1);
+
+ va_start(args, what1);
+
+ return grid;
+}
+
newtGrid newtGridVStacked(enum newtGridElement type1, void * what1, ...) {
va_list args;
newtGrid grid;
va_start(args, what1);
- grid = stackem(1, type1, what1, args);
+ grid = stackem(1, type1, what1, args, 0);
va_start(args, what1);
va_start(args, what1);
- grid = stackem(0, type1, what1, args);
+ grid = stackem(0, type1, what1, args, 0);
va_start(args, what1);
return grid;
}
+
+newtGrid newtGridBasicWindow(newtComponent text, newtGrid middle,
+ newtGrid buttons) {
+ newtGrid grid;
+
+ grid = newtCreateGrid(1, 3);
+ newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
+ 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
+ newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, middle,
+ 0, 1, 0, 0, 0, 0);
+ newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttons,
+ 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);
+
+ return grid;
+}
+
+newtGrid newtGridSimpleWindow(newtComponent text, newtComponent middle,
+ newtGrid buttons) {
+ newtGrid grid;
+
+ grid = newtCreateGrid(1, 3);
+ newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
+ 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
+ newtGridSetField(grid, 0, 1, NEWT_GRID_COMPONENT, middle,
+ 0, 1, 0, 0, 0, 0);
+ newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttons,
+ 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);
+
+ return grid;
+}
labelDraw,
newtDefaultEventHandler,
labelDestroy,
- NULL,
+ newtDefaultPlaceHandler,
+ newtDefaultMappedHandler,
} ;
newtComponent newtLabel(int left, int top, const char * text) {
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 void listboxPlace(newtComponent co, int newLeft, int newTop);
static inline void updateWidth(newtComponent co, struct listbox * li,
int maxField);
+static void listboxMapped(newtComponent co, int isMapped);
static struct componentOps listboxOps = {
listboxDraw,
listboxEvent,
listboxDestroy,
listboxPlace,
+ listboxMapped,
};
-static void listboxPlace(newtComponent co) {
+static void listboxMapped(newtComponent co, int isMapped) {
struct listbox * li = co->data;
- if (li->sb) {
- li->sb->top = co->top;
- li->sb->left = co->left + co->width - 1;
- if (li->sb->ops->place) li->sb->ops->place(li->sb);
- }
+ co->isMapped = isMapped;
+ if (li->sb)
+ li->sb->ops->mapped(li->sb, isMapped);
+}
+
+static void listboxPlace(newtComponent co, int newLeft, int newTop) {
+ struct listbox * li = co->data;
+
+ co->top = newTop;
+ co->left = newLeft;
+
+ if (li->sb)
+ li->sb->ops->place(li->sb, co->left + co->width - 1, co->top);
}
newtComponent newtListbox(int left, int top, int height, int flags) {
if (rows) *rows = SLtt_Screen_Rows;
if (cols) *cols = SLtt_Screen_Cols;
}
+
+void newtDefaultPlaceHandler(newtComponent c, int newLeft, int newTop) {
+ c->left = newLeft;
+ c->top = newTop;
+}
+
+void newtDefaultMappedHandler(newtComponent c, int isMapped) {
+ c->isMapped = isMapped;
+}
newtGrid newtCreateGrid(int cols, int rows);
/* TYPE, what, TYPE, what, ..., NULL */
newtGrid newtGridVStacked(enum newtGridElement type, void * what, ...);
+newtGrid newtGridVCloseStacked(enum newtGridElement type, void * what, ...);
newtGrid newtGridHStacked(enum newtGridElement type1, void * what1, ...);
+newtGrid newtGridHCloseStacked(enum newtGridElement type1, void * what1, ...);
+newtGrid newtGridBasicWindow(newtComponent text, newtGrid middle,
+ newtGrid buttons);
+newtGrid newtGridSimpleWindow(newtComponent text, newtComponent middle,
+ newtGrid buttons);
void newtGridSetField(newtGrid grid, int col, int row,
enum newtGridElement type, void * val, int padLeft,
int padTop, int padRight, int padBottom, int anchor,
void (* draw)(newtComponent c);
struct eventResult (* event)(newtComponent c, struct event ev);
void (* destroy)(newtComponent c);
- void (* place)(newtComponent c);
+ void (* place)(newtComponent c, int newLeft, int newTop);
+ void (* mapped)(newtComponent c, int isMapped);
} ;
+void newtDefaultPlaceHandler(newtComponent c, int newLeft, int newTop);
+void newtDefaultMappedHandler(newtComponent c, int isMapped);
struct eventResult newtDefaultEventHandler(newtComponent c,
struct event ev);
scaleDraw,
newtDefaultEventHandler,
NULL,
- NULL,
+ newtDefaultPlaceHandler,
+ newtDefaultMappedHandler,
} ;
newtComponent newtScale(int left, int top, int width, long long fullValue) {
sbDraw,
newtDefaultEventHandler,
sbDestroy,
- NULL,
+ newtDefaultPlaceHandler,
+ newtDefaultMappedHandler,
} ;
void newtScrollbarSet(newtComponent co, int where, int total) {
int doWrap;
newtComponent sb;
int topLine;
+ int textWidth;
};
static void addLine(newtComponent co, const char * s, int len);
static struct eventResult textboxEvent(newtComponent c,
struct event ev);
static void textboxDestroy(newtComponent co);
+static void textboxPlace(newtComponent co, int newLeft, int newTop);
+static void textboxMapped(newtComponent co, int isMapped);
static struct componentOps textboxOps = {
textboxDraw,
textboxEvent,
textboxDestroy,
- NULL,
+ textboxPlace,
+ textboxMapped,
} ;
+static void textboxMapped(newtComponent co, int isMapped) {
+ struct textbox * tb = co->data;
+
+ co->isMapped = isMapped;
+ if (tb->sb)
+ tb->sb->ops->mapped(tb->sb, isMapped);
+}
+
+static void textboxPlace(newtComponent co, int newLeft, int newTop) {
+ struct textbox * tb = co->data;
+
+ co->top = newTop;
+ co->left = newLeft;
+
+ if (tb->sb)
+ tb->sb->ops->place(tb->sb, co->left + co->width - 1, co->top);
+}
+
void newtTextboxSetHeight(newtComponent co, int height) {
co->height = height;
}
co->top = top;
co->left = left;
co->takesFocus = 0;
+ co->width = width;
tb->doWrap = flags & NEWT_FLAG_WRAP;
tb->numLines = 0;
tb->linesAlloced = 0;
tb->lines = NULL;
tb->topLine = 0;
+ tb->textWidth = width;
if (flags & NEWT_FLAG_SCROLL) {
- co->width = width - 2;
- tb->sb = newtVerticalScrollbar(co->left + co->width + 1, co->top,
+ co->width += 2;
+ tb->sb = newtVerticalScrollbar(co->left + co->width - 1, co->top,
co->height, COLORSET_TEXTBOX, COLORSET_TEXTBOX);
} else {
- co->width = width;
tb->sb = NULL;
}
if (origlen < 0) origlen = strlen(s);
len = origlen;
- if (!tb->doWrap || len <= co->width) {
+ if (!tb->doWrap || len <= tb->textWidth) {
addShortLine(co, s, len);
} else {
/* word wrap */
start = s;
- while (len > co->width) {
- end = start + co->width - 1;
+ while (len > tb->textWidth) {
+ end = start + tb->textWidth - 1;
while (end > start && !isspace(*end)) end--;
if (end == start)
- end = start + co->width - 1;
+ end = start + tb->textWidth - 1;
addShortLine(co, start, end - start);
tb->lines = realloc(tb->lines, (sizeof(char *) * tb->linesAlloced));
}
- if (len > co->width) len = co->width;
+ if (len > tb->textWidth) len = tb->textWidth;
- tb->lines[tb->numLines] = malloc(co->width + 1);
+ tb->lines[tb->numLines] = malloc(tb->textWidth + 1);
strncpy(tb->lines[tb->numLines], s, len);
- while (len < co->width) tb->lines[tb->numLines][len++] = ' ';
+ while (len < tb->textWidth) tb->lines[tb->numLines][len++] = ' ';
tb->lines[tb->numLines++][len] = '\0';
}
newtGridSetField(buttonGrid, 0, 0, NEWT_GRID_COMPONENT, b1,
0, 0, button2 ? 1 : 0, 0, 0, 0);
- grid = newtGridVStacked(NEWT_GRID_COMPONENT, t,
- NEWT_GRID_SUBGRID, buttonGrid,
- NEWT_GRID_EMPTY);
+ grid = newtCreateGrid(1, 2);
+ newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 0, 0, 0);
+ newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, buttonGrid,
+ 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);
newtGridWrappedWindow(grid, title);
f = newtForm(NULL, NULL, 0);
i ? 1 : 0, 0, 0, 0, 0, 0);
}
- grid = newtGridVStacked(NEWT_GRID_COMPONENT, textbox,
- NEWT_GRID_COMPONENT, listbox,
- NEWT_GRID_SUBGRID, buttonBar,
- NEWT_GRID_EMPTY);
-
+ grid = newtGridSimpleWindow(textbox, listbox, buttonBar);
newtGridWrappedWindow(grid, title);
form = newtForm(NULL, 0, 0);