introduce box widget

This commit is contained in:
NishiOwO
2025-12-15 14:09:30 +09:00
parent 5003d29246
commit 50f11b1c69
31 changed files with 353 additions and 19 deletions

View File

@@ -50,6 +50,7 @@ static void llresizehandler(MwLL handle, void* data) {
for(i = 0; i < arrlen(h->children); i++) {
MwDispatch(h->children[i], parent_resize);
}
MwDispatch(h, resize);
}
static void llclosehandler(MwLL handle, void* data) {
@@ -176,6 +177,8 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent,
MwAddTickList(h);
}
if(h->parent != NULL) MwDispatch(h->parent, children_update);
return h;
}
@@ -378,7 +381,10 @@ void MwSetInteger(MwWidget handle, const char* key, int n) {
} else {
shput(handle->integer, key, n);
}
if(handle->prop_event) MwDispatch3(handle, prop_change, key);
if(handle->prop_event) {
MwDispatch3(handle, prop_change, key);
if(handle->parent != NULL) MwDispatch4(handle->parent, children_prop_change, handle, key);
}
}
void MwSetText(MwWidget handle, const char* key, const char* value) {
@@ -395,7 +401,10 @@ void MwSetText(MwWidget handle, const char* key, const char* value) {
shdel(handle->text, key);
}
}
if(handle->prop_event) MwDispatch3(handle, prop_change, key);
if(handle->prop_event) {
MwDispatch3(handle, prop_change, key);
if(handle->parent != NULL) MwDispatch4(handle->parent, children_prop_change, handle, key);
}
if(strcmp(key, MwNbackground) == 0 || strcmp(key, MwNforeground) == 0 || strcmp(key, MwNsubBackground) == 0 || strcmp(key, MwNsubForeground) == 0) {
MwForceRender(handle);
}
@@ -413,7 +422,10 @@ void MwSetVoid(MwWidget handle, const char* key, void* value) {
} else {
shput(handle->data, key, value);
}
if(handle->prop_event) MwDispatch3(handle, prop_change, key);
if(handle->prop_event) {
MwDispatch3(handle, prop_change, key);
if(handle->parent != NULL) MwDispatch4(handle->parent, children_prop_change, handle, key);
}
}
int MwGetInteger(MwWidget handle, const char* key) {
@@ -732,10 +744,14 @@ void MwReparent(MwWidget handle, MwWidget new_parent) {
break;
}
}
MwDispatch(handle->parent, children_update);
}
handle->parent = new_parent;
arrput(new_parent->children, handle);
MwDispatch(handle->parent, children_update);
}
MwClass MwGetClass(MwWidget handle) {

100
src/widget/box.c Normal file
View File

@@ -0,0 +1,100 @@
#include <Mw/Milsko.h>
#include "../../external/stb_ds.h"
static int create(MwWidget handle) {
MwSetDefault(handle);
MwSetInteger(handle, MwNorientation, MwHORIZONTAL);
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);
}
static void layout(MwWidget handle) {
int i;
int sum = 0;
int horiz = MwGetInteger(handle, MwNorientation) == MwHORIZONTAL ? 1 : 0;
int sz = MwGetInteger(handle, horiz ? MwNwidth : MwNheight) - MwGetInteger(handle, MwNpadding);
int fsz = MwGetInteger(handle, horiz ? MwNheight : MwNwidth) - MwGetInteger(handle, MwNpadding) * 2;
int sk = 0;
if(arrlen(handle->children) == 0) return;
for(i = 0; i < arrlen(handle->children); i++) {
int n = MwGetInteger(handle->children[i], MwNboxRatio);
if(n == MwDEFAULT) n = 1;
sum += n;
}
for(i = 0; i < arrlen(handle->children); i++) {
int n = MwGetInteger(handle->children[i], MwNboxRatio);
int wsz;
if(n == MwDEFAULT) n = 1;
wsz = sz * n / sum - MwGetInteger(handle, MwNpadding);
sk += MwGetInteger(handle, MwNpadding);
MwVaApply(handle->children[i],
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 */
NULL);
sk += wsz;
}
}
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;
if(strcmp(key, MwNboxRatio) == 0) layout(handle);
}
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;

View File

@@ -101,6 +101,11 @@ MwClassRec MwButtonClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -50,6 +50,11 @@ MwClassRec MwCheckBoxClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -201,6 +201,11 @@ MwClassRec MwComboBoxClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -170,6 +170,11 @@ MwClassRec MwEntryClassRec = {
key, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -59,6 +59,11 @@ MwClassRec MwFrameClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -46,6 +46,11 @@ MwClassRec MwImageClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -59,6 +59,11 @@ MwClassRec MwLabelClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -606,6 +606,11 @@ MwClassRec MwListBoxClassRec = {
NULL, /* key */
func_handler, /* execute */
tick, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -201,6 +201,11 @@ MwClassRec MwMenuClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -140,6 +140,11 @@ MwClassRec MwNumberEntryClassRec = {
key, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -249,6 +249,11 @@ MwClassRec MwOpenGLClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -58,6 +58,11 @@ MwClassRec MwProgressBarClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -59,6 +59,11 @@ MwClassRec MwRadioBoxClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -275,6 +275,11 @@ MwClassRec MwScrollBarClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -47,6 +47,11 @@ MwClassRec MwSeparatorClassRec = {
NULL, /* key */
NULL, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -212,6 +212,11 @@ MwClassRec MwSubMenuClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -493,6 +493,11 @@ MwClassRec MwTreeViewClassRec = {
NULL, /* key */
func_handler, /* execute */
tick, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -188,6 +188,11 @@ MwClassRec MwViewportClassRec = {
NULL, /* key */
func_handler, /* execute */
tick, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -521,6 +521,11 @@ MwClassRec MwVulkanClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};

View File

@@ -56,6 +56,11 @@ MwClassRec MwWindowClassRec = {
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL,
NULL,
NULL,
NULL,
NULL};