imtk

view 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 source
1 #include <string.h>
2 #include "imtk.h"
4 struct layout {
5 int box[4], span[4];
6 int spacing;
7 int dir;
8 };
10 #define MAX_STACK_DEPTH 4
11 static struct layout st[MAX_STACK_DEPTH];
12 static int top = 0;
14 int imtk_layout_push(void)
15 {
16 int newtop = top + 1;
18 assert(newtop < MAX_STACK_DEPTH);
19 if(newtop >= MAX_STACK_DEPTH) {
20 return -1;
21 }
23 st[newtop] = st[top++];
24 return 0;
25 }
27 int imtk_layout_pop(void)
28 {
29 assert(top > 0);
30 if(top <= 0) {
31 return -1;
32 }
33 top--;
34 }
36 void imtk_layout_start(int x, int y, int sp, int dir)
37 {
38 st[top].box[0] = st[top].span[0] = x;
39 st[top].box[1] = st[top].span[1] = y;
40 st[top].box[2] = st[top].box[3] = st[top].span[2] = st[top].span[3] = 0;
41 st[top].spacing = sp;
42 st[top].dir = dir;
43 }
45 void imtk_layout_dir(int dir)
46 {
47 st[top].dir = dir;
48 if(dir == IMTK_VERTICAL) {
49 imtk_layout_newline();
50 }
51 }
53 void imtk_layout_advance(int width, int height)
54 {
55 int max_span_y, max_box_y;
57 st[top].span[2] += width + st[top].spacing;
59 if(height > st[top].span[3]) {
60 st[top].span[3] = height;
61 }
63 max_span_y = st[top].span[1] + st[top].span[3];
64 max_box_y = st[top].box[1] + st[top].box[3];
66 if(max_span_y > max_box_y) {
67 st[top].box[3] = max_span_y - st[top].box[1];
68 }
69 if(st[top].span[2] > st[top].box[2]) {
70 st[top].box[2] = st[top].span[2];
71 }
73 if(st[top].dir == IMTK_VERTICAL) {
74 imtk_layout_newline();
75 }
76 }
78 void imtk_layout_newline(void)
79 {
80 st[top].span[0] = st[top].box[0];
81 st[top].span[1] = st[top].box[1] + st[top].box[3] + spacing;
82 st[top].span[2] = st[top].span[3] = 0;
83 }
85 void imtk_layout_get_pos(int *x, int *y)
86 {
87 *x = st[top].span[0] + st[top].span[2];
88 *y = st[top].span[1];
89 }
91 void imtk_layout_get_bounds(int *bbox)
92 {
93 memcpy(bbox, st[top].box, sizeof st[top].box);
94 }