milsko/src/widget/box.c

112 lines
2.9 KiB
C
Raw Normal View History

2025-12-15 14:09:30 +09:00
#include <Mw/Milsko.h>
#include "../../external/stb_ds.h"
static int create(MwWidget handle) {
MwSetDefault(handle);
MwSetInteger(handle, MwNorientation, MwHORIZONTAL);
2025-12-15 15:41:19 +09:00
MwSetInteger(handle, MwNmargin, 0);
2025-12-15 14:09:30 +09:00
MwSetInteger(handle, MwNpadding, 0);
return 0;
}
static void draw(MwWidget handle) {
MwRect r;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
r.x = 0;
r.y = 0;
r.width = MwGetInteger(handle, MwNwidth);
r.height = MwGetInteger(handle, MwNheight);
MwDrawRect(handle, &r, base);
MwLLFreeColor(base);
}
2025-12-15 15:45:53 +09:00
#define Margin ((i != (arrlen(handle->children) - 1)) ? MwGetInteger(handle, MwNmargin) : 0)
2025-12-15 14:09:30 +09:00
static void layout(MwWidget handle) {
int i;
int sum = 0;
int horiz = MwGetInteger(handle, MwNorientation) == MwHORIZONTAL ? 1 : 0;
2025-12-15 15:27:04 +09:00
int sz = MwGetInteger(handle, horiz ? MwNwidth : MwNheight) - MwGetInteger(handle, MwNpadding) * 2;
int fsz = MwGetInteger(handle, horiz ? MwNheight : MwNwidth) - MwGetInteger(handle, MwNpadding) * 2;
int sk = MwGetInteger(handle, MwNpadding);
2025-12-15 14:09:30 +09:00
for(i = 0; i < arrlen(handle->children); i++) {
2025-12-15 14:30:59 +09:00
int n = MwGetInteger(handle->children[i], MwNratio);
int s = MwGetInteger(handle->children[i], MwNfixedSize);
2025-12-15 14:09:30 +09:00
if(n == MwDEFAULT) n = 1;
2025-12-15 14:30:59 +09:00
if(s != MwDEFAULT) {
2025-12-15 15:45:53 +09:00
sz -= s + Margin;
2025-12-15 14:30:59 +09:00
} else {
sum += n;
}
2025-12-15 14:09:30 +09:00
}
for(i = 0; i < arrlen(handle->children); i++) {
2025-12-15 14:30:59 +09:00
int n = MwGetInteger(handle->children[i], MwNratio);
int s = MwGetInteger(handle->children[i], MwNfixedSize);
2025-12-15 14:09:30 +09:00
int wsz;
if(n == MwDEFAULT) n = 1;
2025-12-15 14:30:59 +09:00
if(s != MwDEFAULT) {
wsz = s;
} else {
2025-12-15 15:05:36 +09:00
wsz = sz * n / sum;
2025-12-15 14:30:59 +09:00
}
2025-12-15 15:45:53 +09:00
wsz -= Margin;
2025-12-15 14:09:30 +09:00
MwVaApply(handle->children[i],
2025-12-15 15:27:04 +09:00
horiz ? MwNx : MwNy, sk, /* this is what gets changed */
horiz ? MwNy : MwNx, MwGetInteger(handle, MwNpadding), /* fixed between widgets */
horiz ? MwNwidth : MwNheight, wsz, /* this is what gets changed */
horiz ? MwNheight : MwNwidth, fsz, /* fixed between widgets */
2025-12-15 14:09:30 +09:00
NULL);
2025-12-15 15:45:53 +09:00
sk += wsz + Margin;
2025-12-15 14:09:30 +09:00
}
}
static void prop_change(MwWidget handle, const char* key) {
if(strcmp(key, MwNorientation) == 0) layout(handle);
}
static void children_prop_change(MwWidget handle, MwWidget child, const char* key) {
(void)child;
2025-12-15 14:30:59 +09:00
if(strcmp(key, MwNratio) == 0) layout(handle);
2025-12-15 14:09:30 +09:00
}
static void resize(MwWidget handle) {
layout(handle);
}
static void children_update(MwWidget handle) {
layout(handle);
}
MwClassRec MwBoxClassRec = {
create, /* create */
NULL, /* destroy */
draw, /* draw */
NULL, /* click */
NULL, /* parent_resize */
prop_change, /* prop_change */
NULL, /* mouse_move */
NULL, /* mouse_up */
NULL, /* mouse_down */
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
resize, /* resize */
children_update, /* children_update */
children_prop_change, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};
MwClass MwBoxClass = &MwBoxClassRec;