rayfract
diff src/imtk/layout.c @ 10:1496aae2e7d4
- simplified build by including dependences in the source tree
- added make dep tracking
- added mingw cross-build rules
- added readme & licence
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 31 Jul 2023 18:58:56 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/imtk/layout.c Mon Jul 31 18:58:56 2023 +0300 1.3 @@ -0,0 +1,112 @@ 1.4 +#include <string.h> 1.5 +#include <assert.h> 1.6 +#include "imtk.h" 1.7 + 1.8 +struct layout { 1.9 + int box[4], span[4]; 1.10 + int spacing; 1.11 + int dir; 1.12 +}; 1.13 + 1.14 +#define MAX_STACK_DEPTH 4 1.15 +static struct layout st[MAX_STACK_DEPTH]; 1.16 +static int top = 0; 1.17 + 1.18 +int imtk_layout_push(void) 1.19 +{ 1.20 + int newtop = top + 1; 1.21 + 1.22 + assert(newtop < MAX_STACK_DEPTH); 1.23 + if(newtop >= MAX_STACK_DEPTH) { 1.24 + return -1; 1.25 + } 1.26 + 1.27 + st[newtop] = st[top++]; 1.28 + return 0; 1.29 +} 1.30 + 1.31 +int imtk_layout_pop(void) 1.32 +{ 1.33 + assert(top > 0); 1.34 + if(top <= 0) { 1.35 + return -1; 1.36 + } 1.37 + top--; 1.38 + return 0; 1.39 +} 1.40 + 1.41 +void imtk_layout_start(int x, int y) 1.42 +{ 1.43 + st[top].box[0] = st[top].span[0] = x; 1.44 + st[top].box[1] = st[top].span[1] = y; 1.45 + st[top].box[2] = st[top].box[3] = st[top].span[2] = st[top].span[3] = 0; 1.46 +/* st[top].spacing = sp; 1.47 + st[top].dir = dir;*/ 1.48 +} 1.49 + 1.50 +void imtk_layout_dir(int dir) 1.51 +{ 1.52 + st[top].dir = dir; 1.53 + if(dir == IMTK_VERTICAL) { 1.54 + imtk_layout_newline(); 1.55 + } 1.56 +} 1.57 + 1.58 +void imtk_layout_spacing(int spacing) 1.59 +{ 1.60 + st[top].spacing = spacing; 1.61 +} 1.62 + 1.63 +void imtk_layout_advance(int width, int height) 1.64 +{ 1.65 + int max_span_y, max_box_y; 1.66 + 1.67 + st[top].span[2] += width + st[top].spacing; 1.68 + 1.69 + if(height > st[top].span[3]) { 1.70 + st[top].span[3] = height; 1.71 + } 1.72 + 1.73 + max_span_y = st[top].span[1] + st[top].span[3]; 1.74 + max_box_y = st[top].box[1] + st[top].box[3]; 1.75 + 1.76 + if(max_span_y > max_box_y) { 1.77 + st[top].box[3] = max_span_y - st[top].box[1]; 1.78 + } 1.79 + if(st[top].span[2] > st[top].box[2]) { 1.80 + st[top].box[2] = st[top].span[2]; 1.81 + } 1.82 + 1.83 + if(st[top].dir == IMTK_VERTICAL) { 1.84 + imtk_layout_newline(); 1.85 + } 1.86 +} 1.87 + 1.88 +void imtk_layout_newline(void) 1.89 +{ 1.90 + st[top].span[0] = st[top].box[0]; 1.91 + st[top].span[1] = st[top].box[1] + st[top].box[3] + st[top].spacing; 1.92 + st[top].span[2] = st[top].span[3] = 0; 1.93 +} 1.94 + 1.95 +void imtk_layout_get_pos(int *x, int *y) 1.96 +{ 1.97 + *x = st[top].span[0] + st[top].span[2]; 1.98 + *y = st[top].span[1]; 1.99 +} 1.100 + 1.101 +void imtk_layout_get_bounds(int *bbox) 1.102 +{ 1.103 + memcpy(bbox, st[top].box, sizeof st[top].box); 1.104 +} 1.105 + 1.106 +int imtk_layout_contains(int x, int y) 1.107 +{ 1.108 + if(x < st[top].box[0] || x >= st[top].box[0] + st[top].box[2]) { 1.109 + return 0; 1.110 + } 1.111 + if(y < st[top].box[1] || y >= st[top].box[1] + st[top].box[3]) { 1.112 + return 0; 1.113 + } 1.114 + return 1; 1.115 +}