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

63
examples/basic/box.c Normal file
View File

@@ -0,0 +1,63 @@
#include <Mw/Milsko.h>
MwWidget window, box;
void resize(MwWidget handle, void* user, void* client) {
int ww = MwGetInteger(handle, MwNwidth);
int wh = MwGetInteger(handle, MwNheight);
(void)user;
(void)client;
MwVaApply(box,
MwNwidth, ww,
MwNheight, wh,
NULL);
}
int main() {
MwWidget box2, box3;
MwLibraryInit();
window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, 200, 200,
MwNtitle, "box",
NULL);
box = MwVaCreateWidget(MwBoxClass, "box", window, 0, 0, 0, 0,
MwNpadding, 10,
NULL);
box2 = MwVaCreateWidget(MwBoxClass, "box2", box, 0, 0, 0, 0,
MwNpadding, 10,
MwNorientation, MwVERTICAL,
NULL);
box3 = MwVaCreateWidget(MwBoxClass, "box3", box, 0, 0, 0, 0,
MwNpadding, 10,
MwNorientation, MwVERTICAL,
NULL);
MwVaCreateWidget(MwButtonClass, "btn1", box2, 0, 0, 0, 0,
MwNbackground, "#a00",
NULL);
MwVaCreateWidget(MwButtonClass, "btn2", box2, 0, 0, 0, 0,
MwNbackground, "#0a0",
MwNboxRatio, 2,
NULL);
MwVaCreateWidget(MwButtonClass, "btn3", box2, 0, 0, 0, 0,
MwNbackground, "#00a",
NULL);
MwVaCreateWidget(MwButtonClass, "btn4", box3, 0, 0, 0, 0,
MwNbackground, "#00a",
NULL);
MwAddUserHandler(window, MwNresizeHandler, resize, NULL);
resize(window, NULL, NULL);
MwLoop(window);
}

View File

@@ -37,6 +37,17 @@
#define MwDispatch3(x, y, z) \
if(!x->destroyed && x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x, z)
/*!
* @warning Used internally
* @brief Dispatches the handler of widget class
* @param x Widget
* @param y Handler name
* @param z Argument
* @param w Argument
*/
#define MwDispatch4(x, y, z, w) \
if(!x->destroyed && x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x, z, w)
#define MwWaitMS 30
#define MwDoubleClickTimeout 250

View File

@@ -50,5 +50,6 @@
#include <Mw/Widget/Separator.h>
#include <Mw/Widget/ComboBox.h>
#include <Mw/Widget/TreeView.h>
#include <Mw/Widget/Box.h>
#endif

View File

@@ -32,6 +32,7 @@
#define MwNpadding "Ipadding"
#define MwNborderWidth "IborderWidth"
#define MwNfillArea "IfillArea"
#define MwNboxRatio "IboxRatio"
#define MwNtitle "Stitle"
#define MwNtext "Stext"

View File

@@ -32,11 +32,13 @@ typedef void* MwWidget;
typedef void (*MwHandler)(MwWidget handle);
typedef int (*MwHandlerWithStatus)(MwWidget handle);
typedef void (*MwHandlerProp)(MwWidget handle, const char* key);
typedef void (*MwHandlerChildrenProp)(MwWidget handle, MwWidget child, const char* key);
typedef void (*MwHandlerKey)(MwWidget handle, int key);
typedef void (*MwHandlerMouse)(MwWidget handle, void* ptr);
typedef void (*MwHandlerExecute)(MwWidget handle, const char* name, void* out, va_list args);
typedef void (*MwUserHandler)(MwWidget handle, void* user_data, void* call_data);
typedef void (*MwErrorHandler)(int code, const char* message, void* user_data);
typedef void (*MwHandlerExecute)(MwWidget handle, const char* name, void* out, va_list args);
struct _MwTextKeyValue {
char* key;
@@ -175,21 +177,26 @@ struct _MwListBoxPacket {
};
struct _MwClass {
MwHandlerWithStatus create;
MwHandler destroy;
MwHandler draw;
MwHandler click;
MwHandler parent_resize;
MwHandlerProp prop_change;
MwHandler mouse_move;
MwHandlerMouse mouse_up;
MwHandlerMouse mouse_down;
MwHandlerKey key;
MwHandlerExecute execute;
MwHandler tick;
void* reserved3;
void* reserved4;
void* reserved5;
MwHandlerWithStatus create;
MwHandler destroy;
MwHandler draw;
MwHandler click;
MwHandler parent_resize;
MwHandlerProp prop_change;
MwHandler mouse_move;
MwHandlerMouse mouse_up;
MwHandlerMouse mouse_down;
MwHandlerKey key;
MwHandlerExecute execute;
MwHandler tick;
MwHandler resize;
MwHandler children_update;
MwHandlerChildrenProp children_prop_change;
void* reserved1;
void* reserved2;
void* reserved3;
void* reserved4;
void* reserved5;
};
#endif

24
include/Mw/Widget/Box.h Normal file
View File

@@ -0,0 +1,24 @@
/*!
* @file Mw/Widget/Box.h
* @brief Box widget
*/
#ifndef __MW_WIDGET_BOX_H__
#define __MW_WIDGET_BOX_H__
#include <Mw/MachDep.h>
#include <Mw/TypeDefs.h>
#ifdef __cplusplus
extern "C" {
#endif
/*!
* @brief Box widget class
*/
MWDECL MwClass MwBoxClass;
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -49,6 +49,7 @@
- MwNheight
- MwNborderWidth
- MwNbackgroundPixmap
- MwNboxRatio
Integer properties must be prefixed with I.
String properties must be prefixed with S.
@@ -82,6 +83,7 @@
<integer name="showArrows" />
<integer name="padding" />
<integer name="borderWidth" />
<integer name="boxRatio" />
<string name="title" />
<string name="text" />
@@ -483,6 +485,12 @@
</header>
</headers>
<widgets>
<widget name="Box">
<properties>
<property name="orientation" />
<property name="padding" />
</properties>
</widget>
<widget name="Button">
<properties>
<property name="pixmap" />

View File

@@ -71,6 +71,7 @@ new_object("src/icon/*.c");
new_object("src/font/*.c");
new_object("src/cursor/*.c");
new_object("src/widget/box.c");
new_object("src/widget/button.c");
new_object("src/widget/checkbox.c");
new_object("src/widget/combobox.c");
@@ -116,6 +117,7 @@ new_example("examples/basic/progressbar");
new_example("examples/basic/colorpicker");
new_example("examples/basic/combobox");
new_example("examples/basic/treeview");
new_example("examples/basic/box");
if (param_get("opengl")) {
new_example("examples/gldemos/boing", $gl_libs);

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};

View File

@@ -18,5 +18,6 @@ for i in *.c; do
done
cd ../..
rm -f milsko-examples.zip
cp resource/logo/logo.png examples/picture.jpg examples/picture.png milsko-examples/
zip -rv milsko-examples.zip milsko-examples
rm -rf milsko-examples