menu buttons

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@106 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-01 16:28:48 +00:00
parent 5d7d47907a
commit cb46c5ba34
10 changed files with 116 additions and 10 deletions

View File

@@ -72,5 +72,11 @@ int main() {
MwAddUserHandler(button3, MwNactivateHandler, handler, NULL);
MwAddUserHandler(button4, MwNactivateHandler, handler, NULL);
MwMenuAdd(menu, NULL, "test 1");
MwMenuAdd(menu, NULL, "test 2");
MwMenuAdd(menu, NULL, "test 3");
MwMenuAdd(menu, NULL, "test 4");
MwMenuAdd(menu, NULL, "test 5");
MwLoop(window);
}

View File

@@ -18,6 +18,15 @@ extern "C" {
*/
MWDECL MwClass MwMenuClass;
/*!
* %brief Adds a menu
* %param handle Widget
* %param menu Menu
* %param name Menu name
* %return Menu
*/
MWDECL void* MwMenuAdd(MwWidget handle, void* menu, const char* name);
#ifdef __cplusplus
}
#endif

View File

@@ -89,6 +89,7 @@ struct _MwClass {
MwHandler destroy;
MwHandler draw;
MwHandler click;
MwHandler parent_resize;
};
struct _MwFont {

View File

@@ -63,6 +63,7 @@ MwClassRec MwButtonClassRec = {
create, /* create */
NULL, /* destroy */
draw, /* draw */
click /* click */
click, /* click */
NULL /* parent_resize */
};
MwClass MwButtonClass = &MwButtonClassRec;

View File

@@ -22,8 +22,12 @@ static void lldownhandler(MwLL handle) {
static void llresizehandler(MwLL handle) {
MwWidget h = (MwWidget)handle->user;
int i;
MwDispatchUserHandler(h, MwNresizeHandler, NULL);
for(i = 0; i < arrlen(h->children); i++) {
MwDispatch(h->children[i], parent_resize);
}
}
static void llclosehandler(MwLL handle) {

View File

@@ -25,6 +25,7 @@ MwClassRec MwFrameClassRec = {
create, /* create */
NULL, /* destroy */
draw, /* draw */
NULL /* click */
NULL, /* click */
NULL /* parent_resize */
};
MwClass MwFrameClass = &MwFrameClassRec;

View File

@@ -1,8 +1,26 @@
/* $Id$ */
#include <Mw/Milsko.h>
#include "stb_ds.h"
typedef struct menu menu_t;
struct menu {
char* name;
menu_t** sub;
};
static void set_xywh(MwWidget handle) {
int height = 0;
int height = 0;
int i;
menu_t* m = handle->internal;
for(i = 0; i < arrlen(m->sub); i++) {
int h = MwTextHeight(handle, m->sub[i]->name);
if(height < h) {
height = h;
}
}
height += 20;
@@ -15,14 +33,45 @@ static void set_xywh(MwWidget handle) {
}
static void create(MwWidget handle) {
menu_t* m = malloc(sizeof(*m));
m->name = NULL;
m->sub = NULL;
handle->internal = m;
MwSetDefault(handle);
set_xywh(handle);
}
static void recursive_free(menu_t* m) {
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) {
menu_t* m = handle->internal;
recursive_free(m);
}
static void draw(MwWidget handle) {
MwRect r;
MwPoint p;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
menu_t* m = handle->internal;
int i;
p.x = 10;
p.y = MwGetInteger(handle, MwNheight) / 2;
r.x = 0;
r.y = 0;
@@ -31,12 +80,44 @@ static void draw(MwWidget handle) {
MwDrawFrame(handle, &r, base, 0);
MwDrawRect(handle, &r, base);
for(i = 0; i < arrlen(m->sub); i++) {
int tw = MwTextWidth(handle, m->sub[i]->name);
p.x += tw / 2;
MwDrawText(handle, &p, m->sub[i]->name, text);
p.x += tw / 2 + 20;
}
MwLLFreeColor(text);
MwLLFreeColor(base);
}
static void parent_resize(MwWidget handle) {
set_xywh(handle);
}
MwClassRec MwMenuClassRec = {
create, /* create */
NULL, /* destroy */
draw, /* draw */
NULL /* click */
create, /* create */
destroy, /* destroy */
draw, /* draw */
NULL, /* click */
parent_resize /* parent_resize */
};
MwClass MwMenuClass = &MwMenuClassRec;
void* MwMenuAdd(MwWidget handle, void* menu, const char* name) {
menu_t* m = menu == NULL ? handle->internal : menu;
menu_t* new = malloc(sizeof(*new));
new->name = malloc(strlen(name) + 1);
new->sub = NULL;
strcpy(new->name, name);
arrput(m->sub, new);
set_xywh(handle);
return new;
}

View File

@@ -130,7 +130,8 @@ MwClassRec MwOpenGLClassRec = {
create, /* create */
destroy, /* destroy */
NULL, /* draw */
NULL /* click */
NULL, /* click */
NULL /* parent_resize */
};
MwClass MwOpenGLClass = &MwOpenGLClassRec;

View File

@@ -379,6 +379,7 @@ MwClassRec MwVulkanClassRec = {
create, /* create */
destroy, /* destroy */
NULL, /* draw */
NULL /* click */
NULL, /* click */
NULL /* parent_resize */
};
MwClass MwVulkanClass = &MwVulkanClassRec;

View File

@@ -23,7 +23,8 @@ MwClassRec MwWindowClassRec = {
create, /* create */
NULL, /* destroy */
draw, /* draw */
NULL /* click */
NULL, /* click */
NULL /* parent_resize */
};
MwClass MwWindowClass = &MwWindowClassRec;