imtk

diff src/layout.c @ 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
children f416d8def7ef
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/layout.c	Sat May 28 22:31:51 2011 +0300
     1.3 @@ -0,0 +1,94 @@
     1.4 +#include <string.h>
     1.5 +#include "imtk.h"
     1.6 +
     1.7 +struct layout {
     1.8 +	int box[4], span[4];
     1.9 +	int spacing;
    1.10 +	int dir;
    1.11 +};
    1.12 +
    1.13 +#define MAX_STACK_DEPTH		4
    1.14 +static struct layout st[MAX_STACK_DEPTH];
    1.15 +static int top = 0;
    1.16 +
    1.17 +int imtk_layout_push(void)
    1.18 +{
    1.19 +	int newtop = top + 1;
    1.20 +
    1.21 +	assert(newtop < MAX_STACK_DEPTH);
    1.22 +	if(newtop >= MAX_STACK_DEPTH) {
    1.23 +		return -1;
    1.24 +	}
    1.25 +
    1.26 +	st[newtop] = st[top++];
    1.27 +	return 0;
    1.28 +}
    1.29 +
    1.30 +int imtk_layout_pop(void)
    1.31 +{
    1.32 +	assert(top > 0);
    1.33 +	if(top <= 0) {
    1.34 +		return -1;
    1.35 +	}
    1.36 +	top--;
    1.37 +}
    1.38 +
    1.39 +void imtk_layout_start(int x, int y, int sp, int dir)
    1.40 +{
    1.41 +	st[top].box[0] = st[top].span[0] = x;
    1.42 +	st[top].box[1] = st[top].span[1] = y;
    1.43 +	st[top].box[2] = st[top].box[3] = st[top].span[2] = st[top].span[3] = 0;
    1.44 +	st[top].spacing = sp;
    1.45 +	st[top].dir = dir;
    1.46 +}
    1.47 +
    1.48 +void imtk_layout_dir(int dir)
    1.49 +{
    1.50 +	st[top].dir = dir;
    1.51 +	if(dir == IMTK_VERTICAL) {
    1.52 +		imtk_layout_newline();
    1.53 +	}
    1.54 +}
    1.55 +
    1.56 +void imtk_layout_advance(int width, int height)
    1.57 +{
    1.58 +	int max_span_y, max_box_y;
    1.59 +
    1.60 +	st[top].span[2] += width + st[top].spacing;
    1.61 +
    1.62 +	if(height > st[top].span[3]) {
    1.63 +		st[top].span[3] = height;
    1.64 +	}
    1.65 +
    1.66 +	max_span_y = st[top].span[1] + st[top].span[3];
    1.67 +	max_box_y = st[top].box[1] + st[top].box[3];
    1.68 +
    1.69 +	if(max_span_y > max_box_y) {
    1.70 +		st[top].box[3] = max_span_y - st[top].box[1];
    1.71 +	}
    1.72 +	if(st[top].span[2] > st[top].box[2]) {
    1.73 +		st[top].box[2] = st[top].span[2];
    1.74 +	}
    1.75 +
    1.76 +	if(st[top].dir == IMTK_VERTICAL) {
    1.77 +		imtk_layout_newline();
    1.78 +	}
    1.79 +}
    1.80 +
    1.81 +void imtk_layout_newline(void)
    1.82 +{
    1.83 +	st[top].span[0] = st[top].box[0];
    1.84 +	st[top].span[1] = st[top].box[1] + st[top].box[3] + spacing;
    1.85 +	st[top].span[2] = st[top].span[3] = 0;
    1.86 +}
    1.87 +
    1.88 +void imtk_layout_get_pos(int *x, int *y)
    1.89 +{
    1.90 +	*x = st[top].span[0] + st[top].span[2];
    1.91 +	*y = st[top].span[1];
    1.92 +}
    1.93 +
    1.94 +void imtk_layout_get_bounds(int *bbox)
    1.95 +{
    1.96 +	memcpy(bbox, st[top].box, sizeof st[top].box);
    1.97 +}