better api for mouse

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@269 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
Nishi 2025-10-11 13:01:41 +00:00
commit d03ccea517
12 changed files with 132 additions and 86 deletions

View file

@ -33,10 +33,10 @@
* %brief Dispatches the handler of widget class * %brief Dispatches the handler of widget class
* %param x Widget * %param x Widget
* %param y Handler name * %param y Handler name
* %param name Property name * %param z Argument
*/ */
#define MwDispatch3(x, y, name) \ #define MwDispatch3(x, y, z) \
if(x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x, name) if(x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x, z)
#define MwWaitMS 5 #define MwWaitMS 5
@ -224,6 +224,13 @@ MWDECL void MwGetBeforeStep(MwWidget handle, jmp_buf* jmpbuf);
*/ */
MWDECL void MwForceRender(MwWidget handle); 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 #ifdef __cplusplus
} }
#endif #endif

View file

@ -10,6 +10,7 @@
#include <Mw/MachDep.h> #include <Mw/MachDep.h>
typedef struct _MwLLHandler* MwLLHandler; typedef struct _MwLLHandler* MwLLHandler;
typedef struct _MwLLMouse MwLLMouse;
#ifdef _MILSKO #ifdef _MILSKO
typedef struct _MwLL* MwLL; typedef struct _MwLL* MwLL;
typedef struct _MwLLColor* MwLLColor; typedef struct _MwLLColor* MwLLColor;
@ -44,6 +45,17 @@ enum MwLLKey {
MwLLKeyDown MwLLKeyDown
}; };
enum MwLLMouse {
MwLLMouseLeft = 1,
MwLLMouseMiddle = 2,
MwLLMouseRight = 3,
};
struct _MwLLMouse {
MwPoint point;
int button;
};
struct _MwLLHandler { struct _MwLLHandler {
void (*draw)(MwLL handle, void* data); void (*draw)(MwLL handle, void* data);
void (*up)(MwLL handle, void* data); void (*up)(MwLL handle, void* data);

View file

@ -29,6 +29,7 @@ typedef void (*MwHandler)(MwWidget handle);
typedef int (*MwHandler2)(MwWidget handle); typedef int (*MwHandler2)(MwWidget handle);
typedef void (*MwHandler3)(MwWidget handle, const char* key); typedef void (*MwHandler3)(MwWidget handle, const char* key);
typedef void (*MwHandler4)(MwWidget handle, int 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 (*MwUserHandler)(MwWidget handle, void* user_data, void* call_data);
typedef void (*MwErrorHandler)(int code, const char* message, void* user_data); typedef void (*MwErrorHandler)(int code, const char* message, void* user_data);
@ -133,8 +134,8 @@ struct _MwClass {
MwHandler parent_resize; MwHandler parent_resize;
MwHandler3 prop_change; MwHandler3 prop_change;
MwHandler mouse_move; MwHandler mouse_move;
MwHandler mouse_up; MwHandler5 mouse_up;
MwHandler mouse_down; MwHandler5 mouse_down;
MwHandler4 key; MwHandler4 key;
void* reserved1; void* reserved1;
void* reserved2; void* reserved2;

View file

@ -163,23 +163,33 @@ void MwLLNextEvent(MwLL handle) {
if(ev.type == Expose) { if(ev.type == Expose) {
render = 1; render = 1;
} else if(ev.type == ButtonPress) { } else if(ev.type == ButtonPress) {
MwLLMouse p;
p.point.x = ev.xbutton.x;
p.point.y = ev.xbutton.y;
if(ev.xbutton.button == Button1) { if(ev.xbutton.button == Button1) {
MwPoint p; p.button = MwLLMouseLeft;
p.x = ev.xbutton.x; } else if(ev.xbutton.button == Button2) {
p.y = ev.xbutton.y; p.button = MwLLMouseMiddle;
} else if(ev.xbutton.button == Button3) {
p.button = MwLLMouseRight;
}
MwLLDispatch(handle, down, &p); MwLLDispatch(handle, down, &p);
XSetInputFocus(handle->display, handle->window, RevertToNone, CurrentTime); XSetInputFocus(handle->display, handle->window, RevertToNone, CurrentTime);
}
} else if(ev.type == ButtonRelease) { } else if(ev.type == ButtonRelease) {
MwLLMouse p;
p.point.x = ev.xbutton.x;
p.point.y = ev.xbutton.y;
if(ev.xbutton.button == Button1) { if(ev.xbutton.button == Button1) {
MwPoint p; p.button = MwLLMouseLeft;
p.x = ev.xbutton.x; } else if(ev.xbutton.button == Button2) {
p.y = ev.xbutton.y; p.button = MwLLMouseMiddle;
} else if(ev.xbutton.button == Button3) {
p.button = MwLLMouseRight;
}
MwLLDispatch(handle, up, &p); MwLLDispatch(handle, up, &p);
}
} else if(ev.type == ConfigureNotify) { } else if(ev.type == ConfigureNotify) {
MwLLDispatch(handle, resize, NULL); MwLLDispatch(handle, resize, NULL);

View file

@ -13,27 +13,25 @@ static void lldrawhandler(MwLL handle, void* data) {
static void lluphandler(MwLL handle, void* data) { static void lluphandler(MwLL handle, void* data) {
MwWidget h = (MwWidget)handle->user; MwWidget h = (MwWidget)handle->user;
MwPoint* p = data; 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; if(p->button == MwLLMouseLeft) MwDispatch(h, click);
h->mouse_point.x = p->x; MwDispatch3(h, mouse_up, data);
h->mouse_point.y = p->y;
MwDispatch(h, click);
MwDispatch(h, mouse_up);
MwDispatchUserHandler(h, MwNmouseUpHandler, data); MwDispatchUserHandler(h, MwNmouseUpHandler, data);
} }
static void lldownhandler(MwLL handle, void* data) { static void lldownhandler(MwLL handle, void* data) {
MwWidget h = (MwWidget)handle->user; MwWidget h = (MwWidget)handle->user;
MwPoint* p = data; MwLLMouse* p = data;
h->pressed = 1; if(p->button == MwLLMouseLeft) h->pressed = 1;
h->mouse_point.x = p->x; h->mouse_point.x = p->point.x;
h->mouse_point.y = p->y; h->mouse_point.y = p->point.y;
MwDispatch(h, mouse_down); MwDispatch3(h, mouse_down, data);
MwDispatchUserHandler(h, MwNmouseDownHandler, data); MwDispatchUserHandler(h, MwNmouseDownHandler, data);
} }
@ -384,3 +382,9 @@ void MwGetBeforeStep(MwWidget handle, jmp_buf* jmpbuf) {
void MwForceRender(MwWidget handle) { void MwForceRender(MwWidget handle) {
MwLLForceRender(handle->lowlevel); MwLLForceRender(handle->lowlevel);
} }
void MwForceRender2(MwWidget handle, void* ptr) {
(void)ptr;
MwForceRender(handle);
}

View file

@ -73,8 +73,8 @@ MwClassRec MwButtonClassRec = {
NULL, /* parent_resize */ NULL, /* parent_resize */
prop_change, /* prop_change */ prop_change, /* prop_change */
NULL, /* mouse_move */ NULL, /* mouse_move */
MwForceRender, /* mouse_up */ MwForceRender2, /* mouse_up */
MwForceRender, /* mouse_down */ MwForceRender2, /* mouse_down */
NULL, /* key */ NULL, /* key */
NULL, NULL,
NULL, NULL,

View file

@ -45,8 +45,8 @@ MwClassRec MwCheckBoxClassRec = {
NULL, /* parent_resize */ NULL, /* parent_resize */
prop_change, /* prop_change */ prop_change, /* prop_change */
NULL, /* mouse_move */ NULL, /* mouse_move */
MwForceRender, /* mouse_up */ MwForceRender2, /* mouse_up */
MwForceRender, /* mouse_down */ MwForceRender2, /* mouse_down */
NULL, /* key */ NULL, /* key */
NULL, NULL,
NULL, NULL,

View file

@ -125,8 +125,8 @@ MwClassRec MwEntryClassRec = {
NULL, /* parent_resize */ NULL, /* parent_resize */
NULL, /* prop_change */ NULL, /* prop_change */
NULL, /* mouse_move */ NULL, /* mouse_move */
MwForceRender, /* mouse_up */ MwForceRender2, /* mouse_up */
MwForceRender, /* mouse_down */ MwForceRender2, /* mouse_down */
key, /* key */ key, /* key */
NULL, NULL,
NULL, NULL,

View file

@ -122,8 +122,11 @@ static void parent_resize(MwWidget handle) {
set_xywh(handle); set_xywh(handle);
} }
static void mouse_down(MwWidget handle) { static void mouse_down(MwWidget handle, void* ptr) {
MENU_LOOP_DECL; MENU_LOOP_DECL;
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
BEGIN_MENU_LOOP; BEGIN_MENU_LOOP;
if(in_area) { if(in_area) {
if(m->sub[i]->wsub == NULL && arrlen(m->sub[i]->sub) > 0) { if(m->sub[i]->wsub == NULL && arrlen(m->sub[i]->sub) > 0) {
@ -151,8 +154,11 @@ static void mouse_down(MwWidget handle) {
MwForceRender(handle); MwForceRender(handle);
} }
static void mouse_up(MwWidget handle) { static void mouse_up(MwWidget handle, void* ptr) {
MENU_LOOP_DECL; MENU_LOOP_DECL;
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
BEGIN_MENU_LOOP; BEGIN_MENU_LOOP;
if(in_area && m->sub[i]->wsub != NULL) { if(in_area && m->sub[i]->wsub != NULL) {
m->sub[i]->keep = 1; m->sub[i]->keep = 1;

View file

@ -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; MwEntry e = handle->internal;
int w = MwGetInteger(handle, MwNwidth); int w = MwGetInteger(handle, MwNwidth);
int h = MwGetInteger(handle, MwNheight); int h = MwGetInteger(handle, MwNheight);
const char* str = MwGetText(handle, MwNtext); const char* str = MwGetText(handle, MwNtext);
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
if(e->mouse.x >= (w - e->right)) { if(e->mouse.x >= (w - e->right)) {
char s[512]; char s[512];
if(e->mouse.y >= (h / 2)) { if(e->mouse.y >= (h / 2)) {
@ -114,9 +116,11 @@ static void mouse_up(MwWidget handle) {
MwForceRender(handle); MwForceRender(handle);
} }
static void mouse_down(MwWidget handle) { static void mouse_down(MwWidget handle, void* ptr) {
MwEntry e = handle->internal; MwEntry e = handle->internal;
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
e->mouse = handle->mouse_point; e->mouse = handle->mouse_point;
MwForceRender(handle); MwForceRender(handle);

View file

@ -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 ww = MwGetInteger(handle, MwNwidth);
int wh = MwGetInteger(handle, MwNheight); int wh = MwGetInteger(handle, MwNheight);
int or = MwGetInteger(handle, MwNorientation); int or = MwGetInteger(handle, MwNorientation);
scrollbar_t* scr = handle->internal; scrollbar_t* scr = handle->internal;
if(((MwLLMouse*)ptr)->button != MwLLMouseLeft) return;
scr->point = handle->mouse_point; scr->point = handle->mouse_point;
scr->drag = 0; scr->drag = 0;
if(or == MwVERTICAL) { if(or == MwVERTICAL) {
@ -193,7 +195,7 @@ MwClassRec MwScrollBarClassRec = {
NULL, /* parent_resize */ NULL, /* parent_resize */
prop_change, /* prop_change */ prop_change, /* prop_change */
mouse_move, /* mouse_move */ mouse_move, /* mouse_move */
MwForceRender, /* mouse_up */ MwForceRender2, /* mouse_up */
mouse_down, /* mouse_down */ mouse_down, /* mouse_down */
NULL, /* key */ NULL, /* key */
NULL, NULL,

View file

@ -154,8 +154,8 @@ MwClassRec MwSubMenuClassRec = {
NULL, /* parent_resize */ NULL, /* parent_resize */
NULL, /* prop_change */ NULL, /* prop_change */
NULL, /* mouse_move */ NULL, /* mouse_move */
MwForceRender, /* mouse_up */ MwForceRender2, /* mouse_up */
MwForceRender, /* mouse_down */ MwForceRender2, /* mouse_down */
NULL, /* key */ NULL, /* key */
NULL, NULL,
NULL, NULL,