mirror of
https://gitea.nishi.boats/pyrite-dev/milsko
synced 2026-01-03 08:00:50 +00:00
menu buttons
git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@106 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
@@ -72,5 +72,11 @@ int main() {
|
|||||||
MwAddUserHandler(button3, MwNactivateHandler, handler, NULL);
|
MwAddUserHandler(button3, MwNactivateHandler, handler, NULL);
|
||||||
MwAddUserHandler(button4, 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);
|
MwLoop(window);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,15 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
MWDECL MwClass MwMenuClass;
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ struct _MwClass {
|
|||||||
MwHandler destroy;
|
MwHandler destroy;
|
||||||
MwHandler draw;
|
MwHandler draw;
|
||||||
MwHandler click;
|
MwHandler click;
|
||||||
|
MwHandler parent_resize;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _MwFont {
|
struct _MwFont {
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ MwClassRec MwButtonClassRec = {
|
|||||||
create, /* create */
|
create, /* create */
|
||||||
NULL, /* destroy */
|
NULL, /* destroy */
|
||||||
draw, /* draw */
|
draw, /* draw */
|
||||||
click /* click */
|
click, /* click */
|
||||||
|
NULL /* parent_resize */
|
||||||
};
|
};
|
||||||
MwClass MwButtonClass = &MwButtonClassRec;
|
MwClass MwButtonClass = &MwButtonClassRec;
|
||||||
|
|||||||
@@ -22,8 +22,12 @@ static void lldownhandler(MwLL handle) {
|
|||||||
|
|
||||||
static void llresizehandler(MwLL handle) {
|
static void llresizehandler(MwLL handle) {
|
||||||
MwWidget h = (MwWidget)handle->user;
|
MwWidget h = (MwWidget)handle->user;
|
||||||
|
int i;
|
||||||
|
|
||||||
MwDispatchUserHandler(h, MwNresizeHandler, NULL);
|
MwDispatchUserHandler(h, MwNresizeHandler, NULL);
|
||||||
|
for(i = 0; i < arrlen(h->children); i++) {
|
||||||
|
MwDispatch(h->children[i], parent_resize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void llclosehandler(MwLL handle) {
|
static void llclosehandler(MwLL handle) {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ MwClassRec MwFrameClassRec = {
|
|||||||
create, /* create */
|
create, /* create */
|
||||||
NULL, /* destroy */
|
NULL, /* destroy */
|
||||||
draw, /* draw */
|
draw, /* draw */
|
||||||
NULL /* click */
|
NULL, /* click */
|
||||||
|
NULL /* parent_resize */
|
||||||
};
|
};
|
||||||
MwClass MwFrameClass = &MwFrameClassRec;
|
MwClass MwFrameClass = &MwFrameClassRec;
|
||||||
|
|||||||
91
src/menu.c
91
src/menu.c
@@ -1,8 +1,26 @@
|
|||||||
/* $Id$ */
|
/* $Id$ */
|
||||||
#include <Mw/Milsko.h>
|
#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) {
|
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;
|
height += 20;
|
||||||
|
|
||||||
@@ -15,14 +33,45 @@ static void set_xywh(MwWidget handle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void create(MwWidget handle) {
|
static void create(MwWidget handle) {
|
||||||
|
menu_t* m = malloc(sizeof(*m));
|
||||||
|
|
||||||
|
m->name = NULL;
|
||||||
|
m->sub = NULL;
|
||||||
|
handle->internal = m;
|
||||||
|
|
||||||
MwSetDefault(handle);
|
MwSetDefault(handle);
|
||||||
|
|
||||||
set_xywh(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) {
|
static void draw(MwWidget handle) {
|
||||||
MwRect r;
|
MwRect r;
|
||||||
|
MwPoint p;
|
||||||
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
|
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.x = 0;
|
||||||
r.y = 0;
|
r.y = 0;
|
||||||
@@ -31,12 +80,44 @@ static void draw(MwWidget handle) {
|
|||||||
|
|
||||||
MwDrawFrame(handle, &r, base, 0);
|
MwDrawFrame(handle, &r, base, 0);
|
||||||
MwDrawRect(handle, &r, base);
|
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 = {
|
MwClassRec MwMenuClassRec = {
|
||||||
create, /* create */
|
create, /* create */
|
||||||
NULL, /* destroy */
|
destroy, /* destroy */
|
||||||
draw, /* draw */
|
draw, /* draw */
|
||||||
NULL /* click */
|
NULL, /* click */
|
||||||
|
parent_resize /* parent_resize */
|
||||||
};
|
};
|
||||||
MwClass MwMenuClass = &MwMenuClassRec;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -130,7 +130,8 @@ MwClassRec MwOpenGLClassRec = {
|
|||||||
create, /* create */
|
create, /* create */
|
||||||
destroy, /* destroy */
|
destroy, /* destroy */
|
||||||
NULL, /* draw */
|
NULL, /* draw */
|
||||||
NULL /* click */
|
NULL, /* click */
|
||||||
|
NULL /* parent_resize */
|
||||||
};
|
};
|
||||||
MwClass MwOpenGLClass = &MwOpenGLClassRec;
|
MwClass MwOpenGLClass = &MwOpenGLClassRec;
|
||||||
|
|
||||||
|
|||||||
@@ -379,6 +379,7 @@ MwClassRec MwVulkanClassRec = {
|
|||||||
create, /* create */
|
create, /* create */
|
||||||
destroy, /* destroy */
|
destroy, /* destroy */
|
||||||
NULL, /* draw */
|
NULL, /* draw */
|
||||||
NULL /* click */
|
NULL, /* click */
|
||||||
|
NULL /* parent_resize */
|
||||||
};
|
};
|
||||||
MwClass MwVulkanClass = &MwVulkanClassRec;
|
MwClass MwVulkanClass = &MwVulkanClassRec;
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ MwClassRec MwWindowClassRec = {
|
|||||||
create, /* create */
|
create, /* create */
|
||||||
NULL, /* destroy */
|
NULL, /* destroy */
|
||||||
draw, /* draw */
|
draw, /* draw */
|
||||||
NULL /* click */
|
NULL, /* click */
|
||||||
|
NULL /* parent_resize */
|
||||||
};
|
};
|
||||||
MwClass MwWindowClass = &MwWindowClassRec;
|
MwClass MwWindowClass = &MwWindowClassRec;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user