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