From d03ccea51789415b49b56493f2cb6ab455dc640b Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sat, 11 Oct 2025 13:01:41 +0000 Subject: [PATCH] better api for mouse git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@269 b9cfdab3-6d41-4d17-bbe4-086880011989 --- include/Mw/Core.h | 13 ++++++++++--- include/Mw/LowLevel.h | 12 ++++++++++++ include/Mw/TypeDefs.h | 5 +++-- src/backend/x11.c | 34 ++++++++++++++++++++++------------ src/core.c | 34 +++++++++++++++++++--------------- src/widget/button.c | 20 ++++++++++---------- src/widget/checkbox.c | 20 ++++++++++---------- src/widget/entry.c | 20 ++++++++++---------- src/widget/menu.c | 10 ++++++++-- src/widget/numberentry.c | 8 ++++++-- src/widget/scrollbar.c | 24 +++++++++++++----------- src/widget/submenu.c | 20 ++++++++++---------- 12 files changed, 133 insertions(+), 87 deletions(-) diff --git a/include/Mw/Core.h b/include/Mw/Core.h index 0b07c78..9a23821 100644 --- a/include/Mw/Core.h +++ b/include/Mw/Core.h @@ -33,10 +33,10 @@ * %brief Dispatches the handler of widget class * %param x Widget * %param y Handler name - * %param name Property name + * %param z Argument */ -#define MwDispatch3(x, y, name) \ - if(x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x, name) +#define MwDispatch3(x, y, z) \ + if(x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x, z) #define MwWaitMS 5 @@ -224,6 +224,13 @@ MWDECL void MwGetBeforeStep(MwWidget handle, jmp_buf* jmpbuf); */ MWDECL void MwForceRender(MwWidget handle); +/*! + * %brief Forcefully makes widget render + * %param handle Widget + * %param ptr Ignored + */ +MWDECL void MwForceRender2(MwWidget handle, void* ptr); + #ifdef __cplusplus } #endif diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 6dbd171..5c2191c 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -10,6 +10,7 @@ #include typedef struct _MwLLHandler* MwLLHandler; +typedef struct _MwLLMouse MwLLMouse; #ifdef _MILSKO typedef struct _MwLL* MwLL; typedef struct _MwLLColor* MwLLColor; @@ -44,6 +45,17 @@ enum MwLLKey { MwLLKeyDown }; +enum MwLLMouse { + MwLLMouseLeft = 1, + MwLLMouseMiddle = 2, + MwLLMouseRight = 3, +}; + +struct _MwLLMouse { + MwPoint point; + int button; +}; + struct _MwLLHandler { void (*draw)(MwLL handle, void* data); void (*up)(MwLL handle, void* data); diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index 68ec6ab..144db72 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -29,6 +29,7 @@ typedef void (*MwHandler)(MwWidget handle); typedef int (*MwHandler2)(MwWidget handle); typedef void (*MwHandler3)(MwWidget handle, const char* key); typedef void (*MwHandler4)(MwWidget handle, int key); +typedef void (*MwHandler5)(MwWidget handle, void* ptr); typedef void (*MwUserHandler)(MwWidget handle, void* user_data, void* call_data); typedef void (*MwErrorHandler)(int code, const char* message, void* user_data); @@ -133,8 +134,8 @@ struct _MwClass { MwHandler parent_resize; MwHandler3 prop_change; MwHandler mouse_move; - MwHandler mouse_up; - MwHandler mouse_down; + MwHandler5 mouse_up; + MwHandler5 mouse_down; MwHandler4 key; void* reserved1; void* reserved2; diff --git a/src/backend/x11.c b/src/backend/x11.c index 23eff8c..d685a12 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -163,23 +163,33 @@ void MwLLNextEvent(MwLL handle) { if(ev.type == Expose) { render = 1; } else if(ev.type == ButtonPress) { + MwLLMouse p; + p.point.x = ev.xbutton.x; + p.point.y = ev.xbutton.y; if(ev.xbutton.button == Button1) { - MwPoint p; - p.x = ev.xbutton.x; - p.y = ev.xbutton.y; - - MwLLDispatch(handle, down, &p); - - XSetInputFocus(handle->display, handle->window, RevertToNone, CurrentTime); + p.button = MwLLMouseLeft; + } else if(ev.xbutton.button == Button2) { + p.button = MwLLMouseMiddle; + } else if(ev.xbutton.button == Button3) { + p.button = MwLLMouseRight; } + + MwLLDispatch(handle, down, &p); + + XSetInputFocus(handle->display, handle->window, RevertToNone, CurrentTime); } else if(ev.type == ButtonRelease) { + MwLLMouse p; + p.point.x = ev.xbutton.x; + p.point.y = ev.xbutton.y; if(ev.xbutton.button == Button1) { - MwPoint p; - p.x = ev.xbutton.x; - p.y = ev.xbutton.y; - - MwLLDispatch(handle, up, &p); + p.button = MwLLMouseLeft; + } else if(ev.xbutton.button == Button2) { + p.button = MwLLMouseMiddle; + } else if(ev.xbutton.button == Button3) { + p.button = MwLLMouseRight; } + + MwLLDispatch(handle, up, &p); } else if(ev.type == ConfigureNotify) { MwLLDispatch(handle, resize, NULL); diff --git a/src/core.c b/src/core.c index ec39cd3..b263a03 100644 --- a/src/core.c +++ b/src/core.c @@ -12,28 +12,26 @@ static void lldrawhandler(MwLL handle, void* data) { } static void lluphandler(MwLL handle, void* data) { - MwWidget h = (MwWidget)handle->user; - MwPoint* p = data; + MwWidget h = (MwWidget)handle->user; + MwLLMouse* p = data; - (void)data; + if(p->button == MwLLMouseLeft) h->pressed = 0; + h->mouse_point.x = p->point.x; + h->mouse_point.y = p->point.y; - h->pressed = 0; - h->mouse_point.x = p->x; - h->mouse_point.y = p->y; - - MwDispatch(h, click); - MwDispatch(h, mouse_up); + if(p->button == MwLLMouseLeft) MwDispatch(h, click); + MwDispatch3(h, mouse_up, data); MwDispatchUserHandler(h, MwNmouseUpHandler, data); } static void lldownhandler(MwLL handle, void* data) { - MwWidget h = (MwWidget)handle->user; - MwPoint* p = data; - h->pressed = 1; - h->mouse_point.x = p->x; - h->mouse_point.y = p->y; + MwWidget h = (MwWidget)handle->user; + MwLLMouse* p = data; + if(p->button == MwLLMouseLeft) h->pressed = 1; + h->mouse_point.x = p->point.x; + h->mouse_point.y = p->point.y; - MwDispatch(h, mouse_down); + MwDispatch3(h, mouse_down, data); MwDispatchUserHandler(h, MwNmouseDownHandler, data); } @@ -384,3 +382,9 @@ void MwGetBeforeStep(MwWidget handle, jmp_buf* jmpbuf) { void MwForceRender(MwWidget handle) { MwLLForceRender(handle->lowlevel); } + +void MwForceRender2(MwWidget handle, void* ptr) { + (void)ptr; + + MwForceRender(handle); +} diff --git a/src/widget/button.c b/src/widget/button.c index f3d343f..405fec7 100644 --- a/src/widget/button.c +++ b/src/widget/button.c @@ -66,16 +66,16 @@ static void prop_change(MwWidget handle, const char* key) { } MwClassRec MwButtonClassRec = { - create, /* create */ - NULL, /* destroy */ - draw, /* draw */ - click, /* click */ - NULL, /* parent_resize */ - prop_change, /* prop_change */ - NULL, /* mouse_move */ - MwForceRender, /* mouse_up */ - MwForceRender, /* mouse_down */ - NULL, /* key */ + create, /* create */ + NULL, /* destroy */ + draw, /* draw */ + click, /* click */ + NULL, /* parent_resize */ + prop_change, /* prop_change */ + NULL, /* mouse_move */ + MwForceRender2, /* mouse_up */ + MwForceRender2, /* mouse_down */ + NULL, /* key */ NULL, NULL, NULL, diff --git a/src/widget/checkbox.c b/src/widget/checkbox.c index 222d04e..57fc250 100644 --- a/src/widget/checkbox.c +++ b/src/widget/checkbox.c @@ -38,16 +38,16 @@ static void prop_change(MwWidget handle, const char* key) { } MwClassRec MwCheckBoxClassRec = { - create, /* create */ - NULL, /* destroy */ - draw, /* draw */ - click, /* click */ - NULL, /* parent_resize */ - prop_change, /* prop_change */ - NULL, /* mouse_move */ - MwForceRender, /* mouse_up */ - MwForceRender, /* mouse_down */ - NULL, /* key */ + create, /* create */ + NULL, /* destroy */ + draw, /* draw */ + click, /* click */ + NULL, /* parent_resize */ + prop_change, /* prop_change */ + NULL, /* mouse_move */ + MwForceRender2, /* mouse_up */ + MwForceRender2, /* mouse_down */ + NULL, /* key */ NULL, NULL, NULL, diff --git a/src/widget/entry.c b/src/widget/entry.c index d0b6a20..3688cd6 100644 --- a/src/widget/entry.c +++ b/src/widget/entry.c @@ -118,16 +118,16 @@ static void key(MwWidget handle, int code) { } MwClassRec MwEntryClassRec = { - create, /* create */ - destroy, /* destroy */ - draw, /* draw */ - NULL, /* click */ - NULL, /* parent_resize */ - NULL, /* prop_change */ - NULL, /* mouse_move */ - MwForceRender, /* mouse_up */ - MwForceRender, /* mouse_down */ - key, /* key */ + create, /* create */ + destroy, /* destroy */ + draw, /* draw */ + NULL, /* click */ + NULL, /* parent_resize */ + NULL, /* prop_change */ + NULL, /* mouse_move */ + MwForceRender2, /* mouse_up */ + MwForceRender2, /* mouse_down */ + key, /* key */ NULL, NULL, NULL, diff --git a/src/widget/menu.c b/src/widget/menu.c index 300e495..97c51cf 100644 --- a/src/widget/menu.c +++ b/src/widget/menu.c @@ -122,8 +122,11 @@ static void parent_resize(MwWidget handle) { set_xywh(handle); } -static void mouse_down(MwWidget handle) { +static void mouse_down(MwWidget handle, void* ptr) { MENU_LOOP_DECL; + + if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return; + BEGIN_MENU_LOOP; if(in_area) { if(m->sub[i]->wsub == NULL && arrlen(m->sub[i]->sub) > 0) { @@ -151,8 +154,11 @@ static void mouse_down(MwWidget handle) { MwForceRender(handle); } -static void mouse_up(MwWidget handle) { +static void mouse_up(MwWidget handle, void* ptr) { MENU_LOOP_DECL; + + if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return; + BEGIN_MENU_LOOP; if(in_area && m->sub[i]->wsub != NULL) { m->sub[i]->keep = 1; diff --git a/src/widget/numberentry.c b/src/widget/numberentry.c index c0d08f9..4158b47 100644 --- a/src/widget/numberentry.c +++ b/src/widget/numberentry.c @@ -95,12 +95,14 @@ static void mouse_move(MwWidget handle) { } } -static void mouse_up(MwWidget handle) { +static void mouse_up(MwWidget handle, void* ptr) { MwEntry e = handle->internal; int w = MwGetInteger(handle, MwNwidth); int h = MwGetInteger(handle, MwNheight); const char* str = MwGetText(handle, MwNtext); + if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return; + if(e->mouse.x >= (w - e->right)) { char s[512]; if(e->mouse.y >= (h / 2)) { @@ -114,9 +116,11 @@ static void mouse_up(MwWidget handle) { MwForceRender(handle); } -static void mouse_down(MwWidget handle) { +static void mouse_down(MwWidget handle, void* ptr) { MwEntry e = handle->internal; + if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return; + e->mouse = handle->mouse_point; MwForceRender(handle); diff --git a/src/widget/scrollbar.c b/src/widget/scrollbar.c index 9af89d8..e0596f4 100644 --- a/src/widget/scrollbar.c +++ b/src/widget/scrollbar.c @@ -156,12 +156,14 @@ static void mouse_move(MwWidget handle) { } } -static void mouse_down(MwWidget handle) { +static void mouse_down(MwWidget handle, void* ptr) { int ww = MwGetInteger(handle, MwNwidth); int wh = MwGetInteger(handle, MwNheight); int or = MwGetInteger(handle, MwNorientation); scrollbar_t* scr = handle->internal; + if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return; + scr->point = handle->mouse_point; scr->drag = 0; if(or == MwVERTICAL) { @@ -186,16 +188,16 @@ static void prop_change(MwWidget handle, const char* key) { } MwClassRec MwScrollBarClassRec = { - create, /* create */ - destroy, /* destroy */ - draw, /* draw */ - NULL, /* click */ - NULL, /* parent_resize */ - prop_change, /* prop_change */ - mouse_move, /* mouse_move */ - MwForceRender, /* mouse_up */ - mouse_down, /* mouse_down */ - NULL, /* key */ + create, /* create */ + destroy, /* destroy */ + draw, /* draw */ + NULL, /* click */ + NULL, /* parent_resize */ + prop_change, /* prop_change */ + mouse_move, /* mouse_move */ + MwForceRender2, /* mouse_up */ + mouse_down, /* mouse_down */ + NULL, /* key */ NULL, NULL, NULL, diff --git a/src/widget/submenu.c b/src/widget/submenu.c index 464cc30..d6fa2aa 100644 --- a/src/widget/submenu.c +++ b/src/widget/submenu.c @@ -147,16 +147,16 @@ static void click(MwWidget handle) { } MwClassRec MwSubMenuClassRec = { - create, /* create */ - destroy, /* destroy */ - draw, /* draw */ - click, /* click */ - NULL, /* parent_resize */ - NULL, /* prop_change */ - NULL, /* mouse_move */ - MwForceRender, /* mouse_up */ - MwForceRender, /* mouse_down */ - NULL, /* key */ + create, /* create */ + destroy, /* destroy */ + draw, /* draw */ + click, /* click */ + NULL, /* parent_resize */ + NULL, /* prop_change */ + NULL, /* mouse_move */ + MwForceRender2, /* mouse_up */ + MwForceRender2, /* mouse_down */ + NULL, /* key */ NULL, NULL, NULL,