imtk
view src/layout.c @ 27:48e708baa7be
added predicate imtk_layout_contains(x, y)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 12 Dec 2020 17:20:00 +0200 |
parents | f416d8def7ef |
children |
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "imtk.h"
5 struct layout {
6 int box[4], span[4];
7 int spacing;
8 int dir;
9 };
11 #define MAX_STACK_DEPTH 4
12 static struct layout st[MAX_STACK_DEPTH];
13 static int top = 0;
15 int imtk_layout_push(void)
16 {
17 int newtop = top + 1;
19 assert(newtop < MAX_STACK_DEPTH);
20 if(newtop >= MAX_STACK_DEPTH) {
21 return -1;
22 }
24 st[newtop] = st[top++];
25 return 0;
26 }
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 }
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 }
47 void imtk_layout_dir(int dir)
48 {
49 st[top].dir = dir;
50 if(dir == IMTK_VERTICAL) {
51 imtk_layout_newline();
52 }
53 }
55 void imtk_layout_spacing(int spacing)
56 {
57 st[top].spacing = spacing;
58 }
60 void imtk_layout_advance(int width, int height)
61 {
62 int max_span_y, max_box_y;
64 st[top].span[2] += width + st[top].spacing;
66 if(height > st[top].span[3]) {
67 st[top].span[3] = height;
68 }
70 max_span_y = st[top].span[1] + st[top].span[3];
71 max_box_y = st[top].box[1] + st[top].box[3];
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 }
80 if(st[top].dir == IMTK_VERTICAL) {
81 imtk_layout_newline();
82 }
83 }
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 }
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 }
98 void imtk_layout_get_bounds(int *bbox)
99 {
100 memcpy(bbox, st[top].box, sizeof st[top].box);
101 }
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 }