imtk

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