shithub: microui

Download patch

ref: c81a6c7f11ceecb213cc947b382e97bbbadce81d
parent: dd374fc9c9980c6440845e4f4df7792ec4b4aaf0
author: Sigrid Haflínudóttir <[email protected]>
date: Tue Jan 7 05:21:27 EST 2020

remove -p from CFLAGS, fix up a few things

--- a/demo/frame.c
+++ b/demo/frame.c
@@ -1,7 +1,7 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
-#include "microui.h"
+#include <microui.h>
 
 static char logbuf[64000];
 static int logbuf_updated = 0;
--- a/demo/plan9.c
+++ b/demo/plan9.c
@@ -5,7 +5,7 @@
 #include <keyboard.h>
 #include <thread.h>
 #include <bio.h>
-#include "microui.h"
+#include <microui.h>
 
 void process_frame(mu_Context *ctx);
 
--- a/microui.c
+++ b/microui.c
@@ -1,11 +1,11 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
-#include "microui.h"
+#include <microui.h>
 
-#define min(a, b) ((a) < (b) ? (a) : (b))
-#define max(a, b) ((a) > (b) ? (a) : (b))
-#define clamp(x, a, b) min(b, max(a, x))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define CLAMP(x, a, b) MIN(b, MAX(a, x))
 
 static mu_Rect unclipped_rect = { 0, 0, 0x1000000, 0x1000000 };
 
@@ -91,7 +91,7 @@
 buffer_grow(void **buf, int onesz, int *bufmax, int bufnum)
 {
 	while (*bufmax <= bufnum) {
-		*bufmax = max(16, *bufmax) * 2;
+		*bufmax = MAX(16, *bufmax) * 2;
 		if ((*buf = realloc(*buf, *bufmax*onesz)) == nil)
 			sysfatal("not enough memory for %d items (%d bytes)", *bufmax, *bufmax*onesz);
 	}
@@ -136,12 +136,10 @@
 static mu_Rect
 clip_rect(mu_Rect r1, mu_Rect r2)
 {
-	int x1 = max(r1.x, r2.x);
-	int y1 = max(r1.y, r2.y);
-	int x2 = min(r1.x + r1.w, r2.x + r2.w);
-	int y2 = min(r1.y + r1.h, r2.y + r2.h);
-	if (x2 < x1) { x2 = x1; }
-	if (y2 < y1) { y2 = y1; }
+	int x1 = MAX(r1.x, r2.x);
+	int y1 = MAX(r1.y, r2.y);
+	int x2 = MAX(MIN(r1.x + r1.w, r2.x + r2.w), x1);
+	int y2 = MAX(MIN(r1.y + r1.h, r2.y + r2.h), y1);
 	return mu_rect(x1, y1, x2 - x1, y2 - y1);
 }
 
@@ -156,9 +154,8 @@
 draw_frame(mu_Context *ctx, mu_Rect rect, int colorid)
 {
 	mu_draw_rect(ctx, rect, ctx->style->colors[colorid]);
-	if (colorid == MU_COLOR_SCROLLBASE	||
-			colorid == MU_COLOR_SCROLLTHUMB ||
-			colorid == MU_COLOR_TITLEBG) { return; }
+	if (colorid == MU_COLOR_SCROLLBASE	|| colorid == MU_COLOR_SCROLLTHUMB || colorid == MU_COLOR_TITLEBG)
+		return;
 	/* draw border */
 	mu_draw_box(ctx, expand_rect(rect, 1), ctx->style->colors[MU_COLOR_BORDER]);
 }
@@ -237,7 +234,8 @@
 	}
 
 	/* unset focus if focus id was not touched this frame */
-	if (!ctx->updated_focus) { ctx->focus = 0; }
+	if (!ctx->updated_focus)
+		ctx->focus = 0;
 	ctx->updated_focus = 0;
 
 	/* bring hover root to front if mouse was pressed */
@@ -553,8 +551,8 @@
 	if (clipped == MU_CLIP_PART)
 		mu_set_clip(ctx, mu_get_clip_rect(ctx));
 
-	while (ctx->strmax <= ctx->strnum+len+1) {
-		ctx->strmax = max(ctx->strmax, ctx->strnum+len+1) * 2;
+	if (ctx->strmax <= ctx->strnum+len+1) {
+		ctx->strmax = MAX(ctx->strmax, ctx->strnum+len+1) * 2;
 		if ((ctx->str = realloc(ctx->str, ctx->strmax)) == nil)
 			sysfatal("not enough memory for %d chars", ctx->strmax);
 	}
@@ -579,14 +577,17 @@
 	mu_Command *cmd;
 	/* do clip command if the rect isn't fully contained within the cliprect */
 	int clipped = mu_check_clip(ctx, rect);
-	if (clipped == MU_CLIP_ALL ) { return; }
-	if (clipped == MU_CLIP_PART) { mu_set_clip(ctx, mu_get_clip_rect(ctx)); }
+	if (clipped == MU_CLIP_ALL )
+		return;
+	if (clipped == MU_CLIP_PART)
+		mu_set_clip(ctx, mu_get_clip_rect(ctx));
 	/* do icon command */
 	cmd = mu_push_command(ctx, MU_COMMAND_ICON);
 	cmd->icon.id = id;
 	cmd->icon.rect = rect;
 	/* reset clipping if it was set */
-	if (clipped) { mu_set_clip(ctx, unclipped_rect); }
+	if (clipped)
+		mu_set_clip(ctx, unclipped_rect);
 }
 
 
@@ -616,10 +617,10 @@
 	ctx->layoutsnum--;
 	/* inherit position/next_row/max from child layout if they are greater */
 	a = get_layout(ctx);
-	a->position.x = max(a->position.x, b->position.x + b->body.x - a->body.x);
-	a->next_row = max(a->next_row, b->next_row + b->body.y - a->body.y);
-	a->max.x = max(a->max.x, b->max.x);
-	a->max.y = max(a->max.y, b->max.y);
+	a->position.x = MAX(a->position.x, b->position.x + b->body.x - a->body.x);
+	a->next_row = MAX(a->next_row, b->next_row + b->body.y - a->body.y);
+	a->max.x = MAX(a->max.x, b->max.x);
+	a->max.y = MAX(a->max.y, b->max.y);
 }
 
 
@@ -690,10 +691,14 @@
 		/* size */
 		res.w = layout->items > -1 ? layout->widths[layout->row_index] : layout->size.x;
 		res.h = layout->size.y;
-		if (res.w == 0) { res.w = style->size.x + style->padding * 2; }
-		if (res.h == 0) { res.h = style->size.y + style->padding * 2; }
-		if (res.w <	0) { res.w += layout->body.w - res.x + 1; }
-		if (res.h <	0) { res.h += layout->body.h - res.y + 1; }
+		if (res.w == 0)
+			res.w = style->size.x + style->padding * 2;
+		if (res.h == 0)
+			res.h = style->size.y + style->padding * 2;
+		if (res.w <	0)
+			res.w += layout->body.w - res.x + 1;
+		if (res.h <	0)
+			res.h += layout->body.h - res.y + 1;
 
 		layout->row_index++;
 	}
@@ -700,7 +705,7 @@
 
 	/* update position */
 	layout->position.x += res.w + style->spacing;
-	layout->next_row = max(layout->next_row, res.y + res.h + style->spacing);
+	layout->next_row = MAX(layout->next_row, res.y + res.h + style->spacing);
 
 	/* apply body offset */
 	res.x += layout->body.x;
@@ -707,8 +712,8 @@
 	res.y += layout->body.y;
 
 	/* update max position */
-	layout->max.x = max(layout->max.x, res.x + res.w);
-	layout->max.y = max(layout->max.y, res.y + res.h);
+	layout->max.x = MAX(layout->max.x, res.x + res.w);
+	layout->max.y = MAX(layout->max.y, res.y + res.h);
 
 	ctx->last_rect = res;
 	return res;
@@ -899,7 +904,7 @@
 	if (ctx->focus == id) {
 		/* handle text input */
 		int len = strlen(buf);
-		int n = min(bufsz - len - 1, (int) strlen(ctx->text_input));
+		int n = MIN(bufsz - len - 1, (int) strlen(ctx->text_input));
 		if (n > 0) {
 			memcpy(buf + len, ctx->text_input, n);
 			len += n;
@@ -932,7 +937,7 @@
 		int textw = text_width(font, buf, -1);
 		int texth = font->height;
 		int ofx = r.w - ctx->style->padding - textw - 1;
-		int textx = r.x + min(ofx, ctx->style->padding);
+		int textx = r.x + MIN(ofx, ctx->style->padding);
 		int texty = r.y + (r.h - texth) / 2;
 		mu_push_clip_rect(ctx, r);
 		mu_draw_text(ctx, font, buf, -1, Pt(textx, texty), color);
@@ -1016,7 +1021,7 @@
 	if (step)
 		v = ((long) ((v + step/2) / step)) * step;
 	/* clamp and store value, update res */
-	*value = v = clamp(v, low, high);
+	*value = v = CLAMP(v, low, high);
 	if (last != v)
 		res |= MU_RES_CHANGE;
 
@@ -1140,44 +1145,63 @@
 	mu_pop_id(ctx);
 }
 
+static void
+scrollbar(mu_Context *ctx, mu_Container *cnt, mu_Rect *b, Point cs, int v)
+{
+	/* only add scrollbar if content size is larger than body */
+	int maxscroll = v ? cs.y - b->h : cs.x - b->w;
 
-#define scrollbar(ctx, cnt, b, cs, x, y, w, h) \
-	do { \
-		/* only add scrollbar if content size is larger than body */ \
-		int maxscroll = cs.y - b->h; \
- \
-		if (maxscroll > 0 && b->h > 0) { \
-			mu_Rect base, thumb; \
-			mu_Id id = mu_get_id(ctx, "!scrollbar" #y, 11); \
- \
-			/* get sizing / positioning */ \
-			base = *b; \
-			base.x = b->x + b->w; \
-			base.w = ctx->style->scrollbar_size; \
- \
-			/* handle input */ \
-			mu_update_control(ctx, id, base, 0); \
-			if (ctx->focus == id && ctx->mouse_down == MU_MOUSE_LEFT) { \
-				cnt->scroll.y += ctx->mouse_delta.y * cs.y / base.h; \
-			} \
-			/* clamp scroll to limits */ \
-			cnt->scroll.y = clamp(cnt->scroll.y, 0, maxscroll); \
- \
-			/* draw base and thumb */ \
-			draw_frame(ctx, base, MU_COLOR_SCROLLBASE); \
-			thumb = base; \
-			thumb.h = max(ctx->style->thumb_size, base.h * b->h / cs.y); \
-			thumb.y += cnt->scroll.y * (base.h - thumb.h) / maxscroll; \
-			draw_frame(ctx, thumb, MU_COLOR_SCROLLTHUMB); \
- \
-			/* set this as the scroll_target (will get scrolled on mousewheel) */ \
-			/* if the mouse is over it */ \
-			if (mu_mouse_over(ctx, *b)) { ctx->scroll_target = cnt; } \
-		} else { \
-			cnt->scroll.y = 0; \
-		} \
-	} while (0)
+	if (maxscroll > 0 && (v ? b->h : b->x) > 0) {
+		mu_Rect base, thumb;
+		mu_Id id = mu_get_id(ctx, v ? "!scrollbary" : "!scrollbarx", 11);
 
+		/* get sizing / positioning */
+		base = *b;
+		if (v) {
+			base.x = b->x + b->w;
+			base.w = ctx->style->scrollbar_size;
+		} else {
+			base.y = b->y + b->h;
+			base.h = ctx->style->scrollbar_size;
+		}
+
+		/* handle input */
+		mu_update_control(ctx, id, base, 0);
+		if (ctx->focus == id && ctx->mouse_down == MU_MOUSE_LEFT) {
+			if (v)
+				cnt->scroll.y += ctx->mouse_delta.y * cs.y / base.h;
+			else
+				cnt->scroll.x += ctx->mouse_delta.x * cs.x / base.w;
+		}
+		/* clamp scroll to limits */
+		if (v)
+			cnt->scroll.y = CLAMP(cnt->scroll.y, 0, maxscroll);
+		else
+			cnt->scroll.x = CLAMP(cnt->scroll.x, 0, maxscroll);
+
+		/* draw base and thumb */
+		draw_frame(ctx, base, MU_COLOR_SCROLLBASE);
+		thumb = base;
+		if (v) {
+			thumb.h = MAX(ctx->style->thumb_size, base.h * b->h / cs.y);
+			thumb.y += cnt->scroll.y * (base.h - thumb.h) / maxscroll;
+		} else {
+			thumb.w = MAX(ctx->style->thumb_size, base.w * b->w / cs.x);
+			thumb.x += cnt->scroll.x * (base.w - thumb.w) / maxscroll;
+		}
+		draw_frame(ctx, thumb, MU_COLOR_SCROLLTHUMB);
+
+		/* set this as the scroll_target (will get scrolled on mousewheel) */
+		/* if the mouse is over it */
+		if (mu_mouse_over(ctx, *b))
+			ctx->scroll_target = cnt;
+	} else if (v) {
+		cnt->scroll.y = 0;
+	} else {
+		cnt->scroll.x = 0;
+	}
+}
+
 static void
 scrollbars(mu_Context *ctx, mu_Container *cnt, mu_Rect *body)
 {
@@ -1193,12 +1217,11 @@
 		body->h -= sz;
 	/* to create a horizontal or vertical scrollbar almost-identical code is
 	** used; only the references to `x|y` `w|h` need to be switched */
-	scrollbar(ctx, cnt, body, cs, x, y, w, h);
-	scrollbar(ctx, cnt, body, cs, y, x, h, w);
+	scrollbar(ctx, cnt, body, cs, 1);
+	scrollbar(ctx, cnt, body, cs, 0);
 	mu_pop_clip_rect(ctx);
 }
 
-
 static void
 push_container_body(mu_Context *ctx, mu_Container *cnt, mu_Rect body, int opt)
 {
@@ -1208,7 +1231,6 @@
 	cnt->body = body;
 }
 
-
 static void
 begin_root_container(mu_Context *ctx, mu_Container *cnt)
 {
@@ -1307,8 +1329,8 @@
 		mu_Rect r = mu_rect(rect.x + rect.w - sz, rect.y + rect.h - sz, sz, sz);
 		mu_update_control(ctx, id, r, opt);
 		if (id == ctx->focus && ctx->mouse_down == MU_MOUSE_LEFT) {
-			cnt->rect.w = max(96, cnt->rect.w + ctx->mouse_delta.x);
-			cnt->rect.h = max(64, cnt->rect.h + ctx->mouse_delta.y);
+			cnt->rect.w = MAX(96, cnt->rect.w + ctx->mouse_delta.x);
+			cnt->rect.h = MAX(64, cnt->rect.h + ctx->mouse_delta.y);
 		}
 	}
 
--- a/mkfile
+++ b/mkfile
@@ -1,8 +1,6 @@
 </$objtype/mkfile
 LIB=/$objtype/lib/libmicroui.a
 
-CFLAGS=$CFLAGS -p
-
 OFILES=\
 	microui.$O\