imtk
changeset 23:4c2b3e281409
added a half-assed automatic layout thing
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Sat, 28 May 2011 22:31:51 +0300 |
parents | 17f5ff624da3 |
children | f416d8def7ef |
files | src/imtk.c src/imtk.h src/layout.c |
diffstat | 3 files changed, 101 insertions(+), 67 deletions(-) [+] |
line diff
1.1 --- a/src/imtk.c Sat Apr 30 06:12:51 2011 +0300 1.2 +++ b/src/imtk.c Sat May 28 22:31:51 2011 +0300 1.3 @@ -12,7 +12,6 @@ 1.4 #include "state.h" 1.5 #include "draw.h" 1.6 1.7 - 1.8 void imtk_post_redisplay(void) 1.9 { 1.10 glutPostRedisplay(); 1.11 @@ -69,68 +68,3 @@ 1.12 imtk_draw_string(x, y + 14, str); 1.13 imtk_layout_advance(imtk_string_size(str), 12); 1.14 } 1.15 - 1.16 - 1.17 -static int box[4], span[4]; 1.18 -static int spacing; 1.19 -static int layout; 1.20 - 1.21 -void imtk_layout_start(int x, int y, int sp, int dir) 1.22 -{ 1.23 - box[0] = span[0] = x; 1.24 - box[1] = span[1] = y; 1.25 - box[2] = box[3] = span[2] = span[3] = 0; 1.26 - spacing = sp; 1.27 - layout = dir; 1.28 -} 1.29 - 1.30 -void imtk_layout_dir(int dir) 1.31 -{ 1.32 - layout = dir; 1.33 - if(dir == IMTK_VERTICAL) { 1.34 - imtk_layout_newline(); 1.35 - } 1.36 -} 1.37 - 1.38 -void imtk_layout_advance(int width, int height) 1.39 -{ 1.40 - int max_span_y, max_box_y; 1.41 - 1.42 - span[2] += width + spacing; 1.43 - 1.44 - if(height > span[3]) { 1.45 - span[3] = height; 1.46 - } 1.47 - 1.48 - max_span_y = span[1] + span[3]; 1.49 - max_box_y = box[1] + box[3]; 1.50 - 1.51 - if(max_span_y > max_box_y) { 1.52 - box[3] = max_span_y - box[1]; 1.53 - } 1.54 - if(span[2] > box[2]) { 1.55 - box[2] = span[2]; 1.56 - } 1.57 - 1.58 - if(layout == IMTK_VERTICAL) { 1.59 - imtk_layout_newline(); 1.60 - } 1.61 -} 1.62 - 1.63 -void imtk_layout_newline(void) 1.64 -{ 1.65 - span[0] = box[0]; 1.66 - span[1] = box[1] + box[3] + spacing; 1.67 - span[2] = span[3] = 0; 1.68 -} 1.69 - 1.70 -void imtk_layout_get_pos(int *x, int *y) 1.71 -{ 1.72 - *x = span[0] + span[2]; 1.73 - *y = span[1]; 1.74 -} 1.75 - 1.76 -void imtk_layout_get_bounds(int *bbox) 1.77 -{ 1.78 - memcpy(bbox, box, sizeof box); 1.79 -}
2.1 --- a/src/imtk.h Sat Apr 30 06:12:51 2011 +0300 2.2 +++ b/src/imtk.h Sat May 28 22:31:51 2011 +0300 2.3 @@ -68,13 +68,19 @@ 2.4 int imtk_listbox(int id, const char *list, int sel, int x, int y); 2.5 int imtk_radiogroup(int id, const char *list, int sel, int x, int y); 2.6 2.7 +int imtk_begin_frame(int id, const char *label, int x, int y); 2.8 +void imtk_end_frame(void); 2.9 + 2.10 /* helper functions to create and destroy item lists for listboxes */ 2.11 char *imtk_create_list(const char *first, ...); 2.12 void imtk_free_list(char *list); 2.13 2.14 /* automatic layout */ 2.15 -void imtk_layout_start(int x, int y, int spacing, int dir); 2.16 +int imtk_layout_push(void); 2.17 +int imtk_layout_pop(void); 2.18 +void imtk_layout_start(int x, int y); 2.19 void imtk_layout_dir(int dir); 2.20 +void imtk_layout_spacing(int spacing); 2.21 void imtk_layout_advance(int width, int height); 2.22 void imtk_layout_newline(void); 2.23 void imtk_layout_get_pos(int *x, int *y);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/layout.c Sat May 28 22:31:51 2011 +0300 3.3 @@ -0,0 +1,94 @@ 3.4 +#include <string.h> 3.5 +#include "imtk.h" 3.6 + 3.7 +struct layout { 3.8 + int box[4], span[4]; 3.9 + int spacing; 3.10 + int dir; 3.11 +}; 3.12 + 3.13 +#define MAX_STACK_DEPTH 4 3.14 +static struct layout st[MAX_STACK_DEPTH]; 3.15 +static int top = 0; 3.16 + 3.17 +int imtk_layout_push(void) 3.18 +{ 3.19 + int newtop = top + 1; 3.20 + 3.21 + assert(newtop < MAX_STACK_DEPTH); 3.22 + if(newtop >= MAX_STACK_DEPTH) { 3.23 + return -1; 3.24 + } 3.25 + 3.26 + st[newtop] = st[top++]; 3.27 + return 0; 3.28 +} 3.29 + 3.30 +int imtk_layout_pop(void) 3.31 +{ 3.32 + assert(top > 0); 3.33 + if(top <= 0) { 3.34 + return -1; 3.35 + } 3.36 + top--; 3.37 +} 3.38 + 3.39 +void imtk_layout_start(int x, int y, int sp, int dir) 3.40 +{ 3.41 + st[top].box[0] = st[top].span[0] = x; 3.42 + st[top].box[1] = st[top].span[1] = y; 3.43 + st[top].box[2] = st[top].box[3] = st[top].span[2] = st[top].span[3] = 0; 3.44 + st[top].spacing = sp; 3.45 + st[top].dir = dir; 3.46 +} 3.47 + 3.48 +void imtk_layout_dir(int dir) 3.49 +{ 3.50 + st[top].dir = dir; 3.51 + if(dir == IMTK_VERTICAL) { 3.52 + imtk_layout_newline(); 3.53 + } 3.54 +} 3.55 + 3.56 +void imtk_layout_advance(int width, int height) 3.57 +{ 3.58 + int max_span_y, max_box_y; 3.59 + 3.60 + st[top].span[2] += width + st[top].spacing; 3.61 + 3.62 + if(height > st[top].span[3]) { 3.63 + st[top].span[3] = height; 3.64 + } 3.65 + 3.66 + max_span_y = st[top].span[1] + st[top].span[3]; 3.67 + max_box_y = st[top].box[1] + st[top].box[3]; 3.68 + 3.69 + if(max_span_y > max_box_y) { 3.70 + st[top].box[3] = max_span_y - st[top].box[1]; 3.71 + } 3.72 + if(st[top].span[2] > st[top].box[2]) { 3.73 + st[top].box[2] = st[top].span[2]; 3.74 + } 3.75 + 3.76 + if(st[top].dir == IMTK_VERTICAL) { 3.77 + imtk_layout_newline(); 3.78 + } 3.79 +} 3.80 + 3.81 +void imtk_layout_newline(void) 3.82 +{ 3.83 + st[top].span[0] = st[top].box[0]; 3.84 + st[top].span[1] = st[top].box[1] + st[top].box[3] + spacing; 3.85 + st[top].span[2] = st[top].span[3] = 0; 3.86 +} 3.87 + 3.88 +void imtk_layout_get_pos(int *x, int *y) 3.89 +{ 3.90 + *x = st[top].span[0] + st[top].span[2]; 3.91 + *y = st[top].span[1]; 3.92 +} 3.93 + 3.94 +void imtk_layout_get_bounds(int *bbox) 3.95 +{ 3.96 + memcpy(bbox, st[top].box, sizeof st[top].box); 3.97 +}