2025-10-01 15:55:53 +00:00
|
|
|
#include <Mw/Milsko.h>
|
|
|
|
|
|
2025-10-13 07:00:00 +00:00
|
|
|
#include "../../external/stb_ds.h"
|
2025-10-23 00:26:36 +00:00
|
|
|
#include "Mw/TypeDefs.h"
|
2025-10-01 16:28:48 +00:00
|
|
|
|
2025-10-01 15:55:53 +00:00
|
|
|
static void set_xywh(MwWidget handle) {
|
2025-11-21 15:07:12 +00:00
|
|
|
int height = 0;
|
2025-11-22 16:30:57 +00:00
|
|
|
int diff = MwDefaultBorderWidth(handle);
|
|
|
|
|
diff = 0;
|
2025-10-01 15:55:53 +00:00
|
|
|
|
2025-11-21 09:59:28 +00:00
|
|
|
height = MwTextHeight(handle, "M") + 10;
|
2025-10-01 15:55:53 +00:00
|
|
|
|
|
|
|
|
MwVaApply(handle,
|
2025-11-22 15:44:01 +00:00
|
|
|
MwNx, -diff,
|
|
|
|
|
MwNy, -diff,
|
|
|
|
|
MwNwidth, MwGetInteger(handle->parent, MwNwidth) + diff * 2,
|
|
|
|
|
MwNheight, height + diff,
|
2025-10-01 15:55:53 +00:00
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 13:57:28 +00:00
|
|
|
static int create(MwWidget handle) {
|
2025-10-02 02:17:27 +00:00
|
|
|
MwMenu m = malloc(sizeof(*m));
|
2025-10-01 16:28:48 +00:00
|
|
|
|
|
|
|
|
m->name = NULL;
|
2025-10-02 02:17:27 +00:00
|
|
|
m->wsub = NULL;
|
2025-10-01 16:28:48 +00:00
|
|
|
m->sub = NULL;
|
|
|
|
|
handle->internal = m;
|
|
|
|
|
|
2025-10-01 15:55:53 +00:00
|
|
|
MwSetDefault(handle);
|
|
|
|
|
|
|
|
|
|
set_xywh(handle);
|
2025-10-04 13:57:28 +00:00
|
|
|
|
|
|
|
|
return 0;
|
2025-10-01 15:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 02:17:27 +00:00
|
|
|
static void recursive_free(MwMenu m) {
|
2025-10-01 16:28:48 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < arrlen(m->sub); i++) {
|
|
|
|
|
recursive_free(m->sub[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m->sub != NULL) arrfree(m->sub);
|
|
|
|
|
if(m->name != NULL) free(m->name);
|
|
|
|
|
free(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void destroy(MwWidget handle) {
|
2025-10-02 02:17:27 +00:00
|
|
|
MwMenu m = handle->internal;
|
2025-10-01 16:28:48 +00:00
|
|
|
|
|
|
|
|
recursive_free(m);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 23:03:49 +00:00
|
|
|
#define MENU_LOOP_DECL \
|
|
|
|
|
int i; \
|
|
|
|
|
MwMenu m = handle->internal; \
|
|
|
|
|
MwPoint p; \
|
|
|
|
|
MwRect r; \
|
2025-11-11 12:27:04 +00:00
|
|
|
int rx; \
|
2025-10-04 23:03:49 +00:00
|
|
|
\
|
2025-11-21 09:59:28 +00:00
|
|
|
p.x = 5; \
|
|
|
|
|
p.y = (MwGetInteger(handle, MwNheight) - MwDefaultBorderWidth(handle)) / 2 + MwDefaultBorderWidth(handle); \
|
2025-10-04 23:03:49 +00:00
|
|
|
\
|
|
|
|
|
r.x = 0; \
|
|
|
|
|
r.y = 0; \
|
|
|
|
|
r.width = MwGetInteger(handle, MwNwidth); \
|
2025-11-11 12:27:04 +00:00
|
|
|
r.height = MwGetInteger(handle, MwNheight); \
|
|
|
|
|
\
|
2025-11-21 09:59:28 +00:00
|
|
|
rx = r.width - 5;
|
2025-10-04 23:03:49 +00:00
|
|
|
|
|
|
|
|
#define BEGIN_MENU_LOOP \
|
|
|
|
|
for(i = 0; i < arrlen(m->sub); i++) { \
|
|
|
|
|
int incr = m->sub[i]->name[0] == '?' ? 1 : 0; \
|
|
|
|
|
int tw = MwTextWidth(handle, m->sub[i]->name + incr); \
|
|
|
|
|
int th = MwTextHeight(handle, m->sub[i]->name + incr); \
|
|
|
|
|
int in_area; \
|
|
|
|
|
\
|
|
|
|
|
if(incr) { \
|
2025-11-11 12:27:04 +00:00
|
|
|
p.x = rx -= tw; \
|
2025-11-21 09:59:28 +00:00
|
|
|
rx -= 10; \
|
2025-10-04 23:03:49 +00:00
|
|
|
} \
|
|
|
|
|
\
|
2025-10-08 17:24:42 +00:00
|
|
|
r.x = p.x - 5; \
|
2025-11-24 15:33:19 +00:00
|
|
|
r.y = p.y + ((MwGetInteger(handle, MwNheight) - p.y * 2) - (th + 10)) / 2; \
|
2025-10-04 23:03:49 +00:00
|
|
|
r.width = tw + 10; \
|
|
|
|
|
r.height = th + 10; \
|
|
|
|
|
\
|
|
|
|
|
in_area = (r.x <= handle->mouse_point.x && r.y <= handle->mouse_point.y && handle->mouse_point.x <= (int)(r.x + r.width) && handle->mouse_point.y <= (int)(r.y + r.height)) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
#define END_MENU_LOOP \
|
2025-11-11 12:27:04 +00:00
|
|
|
p.x += tw + 20; \
|
2025-10-04 23:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 15:55:53 +00:00
|
|
|
static void draw(MwWidget handle) {
|
|
|
|
|
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
|
2025-10-01 16:28:48 +00:00
|
|
|
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
|
2025-10-04 23:03:49 +00:00
|
|
|
MENU_LOOP_DECL;
|
2025-10-01 15:55:53 +00:00
|
|
|
|
2025-10-23 00:26:36 +00:00
|
|
|
MwDrawWidgetBack(handle, &r, base, 0, MwTRUE);
|
2025-10-01 16:28:48 +00:00
|
|
|
|
2025-10-04 23:03:49 +00:00
|
|
|
BEGIN_MENU_LOOP;
|
2025-11-24 15:33:19 +00:00
|
|
|
if(m->sub[i]->wsub != NULL || (in_area && handle->pressed)) {
|
2025-10-23 07:33:54 +00:00
|
|
|
MwDrawFrame(handle, &r, base, MwFALSE);
|
2025-10-23 00:26:36 +00:00
|
|
|
MwDrawWidgetBack(handle, &r, base, 0, MwFALSE);
|
2025-10-04 23:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-21 09:59:28 +00:00
|
|
|
MwDrawText(handle, &p, m->sub[i]->name + incr, m->sub[i]->wsub != NULL || (in_area && handle->pressed) ? 1 : 0, MwALIGNMENT_BEGINNING, text);
|
2025-10-04 23:03:49 +00:00
|
|
|
END_MENU_LOOP;
|
2025-10-01 16:47:48 +00:00
|
|
|
|
2025-10-04 23:03:49 +00:00
|
|
|
MwLLFreeColor(text);
|
|
|
|
|
MwLLFreeColor(base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void parent_resize(MwWidget handle) {
|
|
|
|
|
set_xywh(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 13:01:41 +00:00
|
|
|
static void mouse_down(MwWidget handle, void* ptr) {
|
2025-10-04 23:03:49 +00:00
|
|
|
MENU_LOOP_DECL;
|
2025-10-11 13:01:41 +00:00
|
|
|
|
|
|
|
|
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
|
|
|
|
|
|
2025-10-04 23:03:49 +00:00
|
|
|
BEGIN_MENU_LOOP;
|
|
|
|
|
if(in_area) {
|
|
|
|
|
if(m->sub[i]->wsub == NULL && arrlen(m->sub[i]->sub) > 0) {
|
|
|
|
|
MwPoint p2;
|
|
|
|
|
|
2025-10-10 12:39:07 +00:00
|
|
|
p2.x = p.x - 5;
|
2025-10-04 23:03:49 +00:00
|
|
|
p2.y = p.y + th / 2 + 5;
|
|
|
|
|
|
|
|
|
|
m->sub[i]->wsub = MwCreateWidget(MwSubMenuClass, "submenu", handle, 0, 0, 0, 0);
|
|
|
|
|
MwSubMenuAppear(m->sub[i]->wsub, m->sub[i], &p2);
|
|
|
|
|
} else if(m->sub[i]->wsub != NULL && m->sub[i]->keep) {
|
2025-10-02 02:17:27 +00:00
|
|
|
MwDestroyWidget(m->sub[i]->wsub);
|
|
|
|
|
m->sub[i]->wsub = NULL;
|
2025-10-02 02:50:28 +00:00
|
|
|
m->sub[i]->keep = 0;
|
2025-10-04 23:03:49 +00:00
|
|
|
} else if(arrlen(m->sub[i]->sub) == 0) {
|
|
|
|
|
MwDispatchUserHandler(handle, MwNmenuHandler, m->sub[i]);
|
2025-10-01 16:47:48 +00:00
|
|
|
}
|
2025-10-04 23:03:49 +00:00
|
|
|
} else if(m->sub[i]->keep && m->sub[i]->wsub != NULL) {
|
|
|
|
|
MwDestroyWidget(m->sub[i]->wsub);
|
|
|
|
|
m->sub[i]->wsub = NULL;
|
|
|
|
|
m->sub[i]->keep = 0;
|
2025-10-01 16:28:48 +00:00
|
|
|
}
|
2025-10-04 23:03:49 +00:00
|
|
|
END_MENU_LOOP;
|
2025-10-01 16:28:48 +00:00
|
|
|
|
2025-10-04 23:03:49 +00:00
|
|
|
MwForceRender(handle);
|
2025-10-01 16:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-11 13:01:41 +00:00
|
|
|
static void mouse_up(MwWidget handle, void* ptr) {
|
2025-10-04 23:03:49 +00:00
|
|
|
MENU_LOOP_DECL;
|
2025-10-11 13:01:41 +00:00
|
|
|
|
|
|
|
|
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
|
|
|
|
|
|
2025-10-04 23:03:49 +00:00
|
|
|
BEGIN_MENU_LOOP;
|
|
|
|
|
if(in_area && m->sub[i]->wsub != NULL) {
|
|
|
|
|
m->sub[i]->keep = 1;
|
|
|
|
|
} else if(m->sub[i]->wsub != NULL) {
|
|
|
|
|
MwDestroyWidget(m->sub[i]->wsub);
|
|
|
|
|
m->sub[i]->wsub = NULL;
|
|
|
|
|
m->sub[i]->keep = 0;
|
|
|
|
|
}
|
|
|
|
|
END_MENU_LOOP;
|
|
|
|
|
|
|
|
|
|
MwForceRender(handle);
|
2025-10-01 15:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-20 21:55:30 +00:00
|
|
|
static MwMenu mwMenuAddImpl(MwWidget handle, MwMenu menu, const char* name) {
|
|
|
|
|
MwMenu m = menu == NULL ? handle->internal : menu;
|
|
|
|
|
MwMenu new = malloc(sizeof(*new));
|
2025-11-20 09:26:18 +00:00
|
|
|
new->name = MwStringDuplicate(name);
|
2025-10-20 21:55:30 +00:00
|
|
|
new->sub = NULL;
|
|
|
|
|
new->wsub = NULL;
|
|
|
|
|
new->keep = 0;
|
|
|
|
|
|
|
|
|
|
arrput(m->sub, new);
|
|
|
|
|
|
|
|
|
|
set_xywh(handle);
|
|
|
|
|
|
|
|
|
|
return new;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void func_handler(MwWidget handle, const char* name, void* out, va_list va) {
|
|
|
|
|
if(strcmp(name, "mwMenuAdd") == 0) {
|
|
|
|
|
MwMenu menu = va_arg(va, MwMenu);
|
|
|
|
|
const char* name = va_arg(va, const char*);
|
|
|
|
|
*(MwMenu*)out = mwMenuAddImpl(handle, menu, name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 15:55:53 +00:00
|
|
|
MwClassRec MwMenuClassRec = {
|
2025-10-04 19:22:47 +00:00
|
|
|
create, /* create */
|
|
|
|
|
destroy, /* destroy */
|
|
|
|
|
draw, /* draw */
|
|
|
|
|
NULL, /* click */
|
|
|
|
|
parent_resize, /* parent_resize */
|
2025-10-08 15:24:30 +00:00
|
|
|
NULL, /* prop_change */
|
2025-10-04 19:22:47 +00:00
|
|
|
NULL, /* mouse_move */
|
2025-10-04 23:03:49 +00:00
|
|
|
mouse_up, /* mouse_up */
|
2025-10-08 18:03:46 +00:00
|
|
|
mouse_down, /* mouse_down */
|
|
|
|
|
NULL, /* key */
|
2025-10-30 23:16:03 +00:00
|
|
|
func_handler, /* execute */
|
2025-11-04 02:43:38 +00:00
|
|
|
NULL, /* tick */
|
2025-12-15 14:09:30 +09:00
|
|
|
NULL, /* resize */
|
|
|
|
|
NULL, /* children_update */
|
|
|
|
|
NULL, /* children_prop_change */
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2025-10-08 18:03:46 +00:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL};
|
2025-10-01 15:55:53 +00:00
|
|
|
MwClass MwMenuClass = &MwMenuClassRec;
|