dbg gui, imtk text drawing
[demo_prior] / src / imtk / layout.c
1 #include <string.h>
2 #include <assert.h>
3 #include "imtk.h"
4
5 struct layout {
6         int box[4], span[4];
7         int spacing;
8         int dir;
9 };
10
11 #define MAX_STACK_DEPTH         4
12 static struct layout st[MAX_STACK_DEPTH];
13 static int top = 0;
14
15 int imtk_layout_push(void)
16 {
17         int newtop = top + 1;
18
19         assert(newtop < MAX_STACK_DEPTH);
20         if(newtop >= MAX_STACK_DEPTH) {
21                 return -1;
22         }
23
24         st[newtop] = st[top++];
25         return 0;
26 }
27
28 int imtk_layout_pop(void)
29 {
30         assert(top > 0);
31         if(top <= 0) {
32                 return -1;
33         }
34         top--;
35         return 0;
36 }
37
38 void imtk_layout_start(int x, int y)
39 {
40         st[top].box[0] = st[top].span[0] = x;
41         st[top].box[1] = st[top].span[1] = y;
42         st[top].box[2] = st[top].box[3] = st[top].span[2] = st[top].span[3] = 0;
43 /*      st[top].spacing = sp;
44         st[top].dir = dir;*/
45 }
46
47 void imtk_layout_dir(int dir)
48 {
49         st[top].dir = dir;
50         if(dir == IMTK_VERTICAL) {
51                 imtk_layout_newline();
52         }
53 }
54
55 void imtk_layout_spacing(int spacing)
56 {
57         st[top].spacing = spacing;
58 }
59
60 void imtk_layout_advance(int width, int height)
61 {
62         int max_span_y, max_box_y;
63
64         st[top].span[2] += width + st[top].spacing;
65
66         if(height > st[top].span[3]) {
67                 st[top].span[3] = height;
68         }
69
70         max_span_y = st[top].span[1] + st[top].span[3];
71         max_box_y = st[top].box[1] + st[top].box[3];
72
73         if(max_span_y > max_box_y) {
74                 st[top].box[3] = max_span_y - st[top].box[1];
75         }
76         if(st[top].span[2] > st[top].box[2]) {
77                 st[top].box[2] = st[top].span[2];
78         }
79
80         if(st[top].dir == IMTK_VERTICAL) {
81                 imtk_layout_newline();
82         }
83 }
84
85 void imtk_layout_newline(void)
86 {
87         st[top].span[0] = st[top].box[0];
88         st[top].span[1] = st[top].box[1] + st[top].box[3] + st[top].spacing;
89         st[top].span[2] = st[top].span[3] = 0;
90 }
91
92 void imtk_layout_get_pos(int *x, int *y)
93 {
94         *x = st[top].span[0] + st[top].span[2];
95         *y = st[top].span[1];
96 }
97
98 void imtk_layout_get_bounds(int *bbox)
99 {
100         memcpy(bbox, st[top].box, sizeof st[top].box);
101 }
102
103 int imtk_layout_contains(int x, int y)
104 {
105         if(x < st[top].box[0] || x >= st[top].box[0] + st[top].box[2]) {
106                 return 0;
107         }
108         if(y < st[top].box[1] || y >= st[top].box[1] + st[top].box[3]) {
109                 return 0;
110         }
111         return 1;
112 }