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:
NishiOwO
2025-10-11 13:01:41 +00:00
parent a5dae3ffd9
commit d03ccea517
12 changed files with 133 additions and 87 deletions

View File

@@ -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

View File

@@ -10,6 +10,7 @@
#include <Mw/MachDep.h>
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);

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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;

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;
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);

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 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,

View File

@@ -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,