# HG changeset patch # User John Tsiombikas # Date 1306611111 -10800 # Node ID 4c2b3e281409e7124d901cb5b898e9cd5b3556dc # Parent 17f5ff624da3bff152b8bc60d6933d00cca76a30 added a half-assed automatic layout thing diff -r 17f5ff624da3 -r 4c2b3e281409 src/imtk.c --- a/src/imtk.c Sat Apr 30 06:12:51 2011 +0300 +++ b/src/imtk.c Sat May 28 22:31:51 2011 +0300 @@ -12,7 +12,6 @@ #include "state.h" #include "draw.h" - void imtk_post_redisplay(void) { glutPostRedisplay(); @@ -69,68 +68,3 @@ imtk_draw_string(x, y + 14, str); imtk_layout_advance(imtk_string_size(str), 12); } - - -static int box[4], span[4]; -static int spacing; -static int layout; - -void imtk_layout_start(int x, int y, int sp, int dir) -{ - box[0] = span[0] = x; - box[1] = span[1] = y; - box[2] = box[3] = span[2] = span[3] = 0; - spacing = sp; - layout = dir; -} - -void imtk_layout_dir(int dir) -{ - layout = dir; - if(dir == IMTK_VERTICAL) { - imtk_layout_newline(); - } -} - -void imtk_layout_advance(int width, int height) -{ - int max_span_y, max_box_y; - - span[2] += width + spacing; - - if(height > span[3]) { - span[3] = height; - } - - max_span_y = span[1] + span[3]; - max_box_y = box[1] + box[3]; - - if(max_span_y > max_box_y) { - box[3] = max_span_y - box[1]; - } - if(span[2] > box[2]) { - box[2] = span[2]; - } - - if(layout == IMTK_VERTICAL) { - imtk_layout_newline(); - } -} - -void imtk_layout_newline(void) -{ - span[0] = box[0]; - span[1] = box[1] + box[3] + spacing; - span[2] = span[3] = 0; -} - -void imtk_layout_get_pos(int *x, int *y) -{ - *x = span[0] + span[2]; - *y = span[1]; -} - -void imtk_layout_get_bounds(int *bbox) -{ - memcpy(bbox, box, sizeof box); -} diff -r 17f5ff624da3 -r 4c2b3e281409 src/imtk.h --- a/src/imtk.h Sat Apr 30 06:12:51 2011 +0300 +++ b/src/imtk.h Sat May 28 22:31:51 2011 +0300 @@ -68,13 +68,19 @@ int imtk_listbox(int id, const char *list, int sel, int x, int y); int imtk_radiogroup(int id, const char *list, int sel, int x, int y); +int imtk_begin_frame(int id, const char *label, int x, int y); +void imtk_end_frame(void); + /* helper functions to create and destroy item lists for listboxes */ char *imtk_create_list(const char *first, ...); void imtk_free_list(char *list); /* automatic layout */ -void imtk_layout_start(int x, int y, int spacing, int dir); +int imtk_layout_push(void); +int imtk_layout_pop(void); +void imtk_layout_start(int x, int y); void imtk_layout_dir(int dir); +void imtk_layout_spacing(int spacing); void imtk_layout_advance(int width, int height); void imtk_layout_newline(void); void imtk_layout_get_pos(int *x, int *y); diff -r 17f5ff624da3 -r 4c2b3e281409 src/layout.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/layout.c Sat May 28 22:31:51 2011 +0300 @@ -0,0 +1,94 @@ +#include +#include "imtk.h" + +struct layout { + int box[4], span[4]; + int spacing; + int dir; +}; + +#define MAX_STACK_DEPTH 4 +static struct layout st[MAX_STACK_DEPTH]; +static int top = 0; + +int imtk_layout_push(void) +{ + int newtop = top + 1; + + assert(newtop < MAX_STACK_DEPTH); + if(newtop >= MAX_STACK_DEPTH) { + return -1; + } + + st[newtop] = st[top++]; + return 0; +} + +int imtk_layout_pop(void) +{ + assert(top > 0); + if(top <= 0) { + return -1; + } + top--; +} + +void imtk_layout_start(int x, int y, int sp, int dir) +{ + st[top].box[0] = st[top].span[0] = x; + st[top].box[1] = st[top].span[1] = y; + st[top].box[2] = st[top].box[3] = st[top].span[2] = st[top].span[3] = 0; + st[top].spacing = sp; + st[top].dir = dir; +} + +void imtk_layout_dir(int dir) +{ + st[top].dir = dir; + if(dir == IMTK_VERTICAL) { + imtk_layout_newline(); + } +} + +void imtk_layout_advance(int width, int height) +{ + int max_span_y, max_box_y; + + st[top].span[2] += width + st[top].spacing; + + if(height > st[top].span[3]) { + st[top].span[3] = height; + } + + max_span_y = st[top].span[1] + st[top].span[3]; + max_box_y = st[top].box[1] + st[top].box[3]; + + if(max_span_y > max_box_y) { + st[top].box[3] = max_span_y - st[top].box[1]; + } + if(st[top].span[2] > st[top].box[2]) { + st[top].box[2] = st[top].span[2]; + } + + if(st[top].dir == IMTK_VERTICAL) { + imtk_layout_newline(); + } +} + +void imtk_layout_newline(void) +{ + st[top].span[0] = st[top].box[0]; + st[top].span[1] = st[top].box[1] + st[top].box[3] + spacing; + st[top].span[2] = st[top].span[3] = 0; +} + +void imtk_layout_get_pos(int *x, int *y) +{ + *x = st[top].span[0] + st[top].span[2]; + *y = st[top].span[1]; +} + +void imtk_layout_get_bounds(int *bbox) +{ + memcpy(bbox, st[top].box, sizeof st[top].box); +}