From 29b344f831f42c77ec2291806ded30c46ad06f3c Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Mon, 29 Sep 2025 05:58:28 +0000 Subject: [PATCH] map window points git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@58 b9cfdab3-6d41-4d17-bbe4-086880011989 --- examples/example.c | 45 ++++++++++++++++++++++++++++++++++++----- include/Mw/LowLevel.h | 1 + include/Mw/StringDefs.h | 1 + src/core.c | 15 ++++++++++---- src/gdi.c | 5 +++++ src/x11.c | 4 +++- 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/examples/example.c b/examples/example.c index 3dcc69b..62a680b 100644 --- a/examples/example.c +++ b/examples/example.c @@ -2,6 +2,8 @@ #include +MwWidget window, button, button2, button3, button4; + void handler(MwWidget handle, void* user_data, void* call_data){ (void)handle; (void)user_data; @@ -10,26 +12,59 @@ void handler(MwWidget handle, void* user_data, void* call_data){ printf("hello world!\n"); } +void resize(MwWidget handle, void* user_data, void* call_data){ + unsigned int w, h; + w = MwGetInteger(handle, MwNwidth); + h = MwGetInteger(handle, MwNheight); + + (void)user_data; + (void)call_data; + + MwVaApply(button, + MwNwidth, w - 50 * 2, + MwNheight, h - 125 - 50 * 3, + NULL); + + MwVaApply(button2, + MwNx, 50 + (w - 50 * 2) / 3 * 0, + MwNy, h - 50 - 125, + MwNwidth, (w - 50 * 2) / 3, + NULL); + + MwVaApply(button3, + MwNx, 50 + (w - 50 * 2) / 3 * 1, + MwNy, h - 50 - 125, + MwNwidth, (w - 50 * 2) / 3, + NULL); + + MwVaApply(button4, + MwNx, 50 + (w - 50 * 2) / 3 * 2, + MwNy, h - 50 - 125, + MwNwidth, (w - 50 * 2) / 3, + NULL); +} + int main(){ - MwWidget window = MwVaCreateWidget(MwWindowClass, "main", NULL, 0, 0, 400, 400, + window = MwVaCreateWidget(MwWindowClass, "main", NULL, 0, 0, 400, 400, MwNtitle, "hello world", NULL); - MwWidget button = MwVaCreateWidget(MwButtonClass, "button", window, 50, 50, 300, 125, + button = MwVaCreateWidget(MwButtonClass, "button", window, 50, 50, 300, 125, MwNtext, "lorem ipsum", NULL); - MwWidget button2 = MwVaCreateWidget(MwButtonClass, "button", window, 50, 225, 100, 125, + button2 = MwVaCreateWidget(MwButtonClass, "button", window, 50, 225, 100, 125, MwNtext, "lorem ipsum", MwNbackground, "#f66", NULL); - MwWidget button3 = MwVaCreateWidget(MwButtonClass, "button", window, 150, 225, 100, 125, + button3 = MwVaCreateWidget(MwButtonClass, "button", window, 150, 225, 100, 125, MwNtext, "lorem ipsum", MwNbackground, "#6f6", NULL); - MwWidget button4 = MwVaCreateWidget(MwButtonClass, "button", window, 250, 225, 100, 125, + button4 = MwVaCreateWidget(MwButtonClass, "button", window, 250, 225, 100, 125, MwNtext, "lorem ipsum", MwNbackground, "#66f", NULL); + MwAddUserHandler(window, MwNresizeHandler, resize, NULL); MwAddUserHandler(button, MwNactivateHandler, handler, NULL); MwAddUserHandler(button2, MwNactivateHandler, handler, NULL); MwAddUserHandler(button3, MwNactivateHandler, handler, NULL); diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 1c22f7e..3e83595 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -30,6 +30,7 @@ struct _MwLLHandler { void (*draw)(MwLL handle); void (*up)(MwLL handle); void (*down)(MwLL handle); + void (*resize)(MwLL handle); }; #ifdef __cplusplus diff --git a/include/Mw/StringDefs.h b/include/Mw/StringDefs.h index 0adebde..0dd7678 100644 --- a/include/Mw/StringDefs.h +++ b/include/Mw/StringDefs.h @@ -13,5 +13,6 @@ #define MwNforeground "Sforeground" #define MwNactivateHandler "Cactivate" +#define MwNresizeHandler "Cresize" #endif diff --git a/src/core.c b/src/core.c index f960dfc..31f8540 100644 --- a/src/core.c +++ b/src/core.c @@ -20,6 +20,12 @@ static void lldownhandler(MwLL handle) { h->pressed = 1; } +static void llresizehandler(MwLL handle) { + MwWidget h = (MwWidget)handle->user; + + MwDispatchUserHandler(h, MwNresizeHandler, NULL); +} + MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent, int x, int y, unsigned int width, unsigned int height) { MwWidget h = malloc(sizeof(*h)); @@ -32,10 +38,11 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent, h->widget_class = widget_class; h->pressed = 0; - h->lowlevel->user = h; - h->lowlevel->handler->draw = lldrawhandler; - h->lowlevel->handler->up = lluphandler; - h->lowlevel->handler->down = lldownhandler; + h->lowlevel->user = h; + h->lowlevel->handler->draw = lldrawhandler; + h->lowlevel->handler->up = lluphandler; + h->lowlevel->handler->down = lldownhandler; + h->lowlevel->handler->resize = llresizehandler; if(parent != NULL) arrput(parent->children, h); diff --git a/src/gdi.c b/src/gdi.c index f49d6a3..fb90895 100644 --- a/src/gdi.c +++ b/src/gdi.c @@ -24,6 +24,8 @@ static LRESULT CALLBACK wndproc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { SetCapture(NULL); MwLLDispatch(u->ll, up); InvalidateRect(hWnd, NULL, FALSE); + } else if(msg == WM_SIZE) { + MwLLDispatch(u->ll, resize); } else if(msg == WM_ERASEBKGND) { } else if(msg == WM_NCHITTEST) { return HTCLIENT; @@ -140,6 +142,7 @@ void MwLLGetXYWH(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) RECT rc; GetClientRect(handle->hWnd, &rc); + MapWindowPoints(handle->hWnd, GetParent(handle->hWnd), (LPPOINT)&rc, 2); *x = rc.left; *y = rc.top; @@ -149,10 +152,12 @@ void MwLLGetXYWH(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) void MwLLSetXY(MwLL handle, int x, int y) { SetWindowPos(handle->hWnd, NULL, x, y, 0, 0, SWP_NOSIZE); + InvalidateRect(handle->hWnd, NULL, FALSE); } void MwLLSetWH(MwLL handle, int w, int h) { SetWindowPos(handle->hWnd, NULL, 0, 0, w, h, SWP_NOMOVE); + InvalidateRect(handle->hWnd, NULL, FALSE); } void MwLLSetTitle(MwLL handle, const char* title) { diff --git a/src/x11.c b/src/x11.c index fea7074..fafbb58 100644 --- a/src/x11.c +++ b/src/x11.c @@ -105,7 +105,7 @@ int MwLLPending(MwLL handle) { void MwLLNextEvent(MwLL handle) { XEvent ev; - if(XCheckWindowEvent(handle->display, handle->window, mask, &ev)) { + while(XCheckWindowEvent(handle->display, handle->window, mask, &ev)) { if(ev.type == Expose) { MwLLDispatch(handle, draw); } else if(ev.type == ButtonPress) { @@ -118,6 +118,8 @@ void MwLLNextEvent(MwLL handle) { MwLLDispatch(handle, up); MwLLDispatch(handle, draw); } + } else if(ev.type == ConfigureNotify) { + MwLLDispatch(handle, resize); } } }