From 7b0173e7c493d3627174de67f2e0204d75c91aa0 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Wed, 8 Oct 2025 18:03:46 +0000 Subject: [PATCH] fix sloppy focus git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@231 b9cfdab3-6d41-4d17-bbe4-086880011989 --- include/Mw/LowLevel.h | 1 + include/Mw/MachDep.h | 1 + include/Mw/TypeDefs.h | 7 +++++++ src/backend/gdi.c | 5 ----- src/backend/x11.c | 17 ++++++++++++++++- src/core.c | 8 ++++++++ src/widget/button.c | 9 +++++++-- src/widget/checkbox.c | 9 +++++++-- src/widget/frame.c | 9 +++++++-- src/widget/image.c | 9 +++++++-- src/widget/label.c | 9 +++++++-- src/widget/menu.c | 9 +++++++-- src/widget/opengl.c | 9 +++++++-- src/widget/scrollbar.c | 9 +++++++-- src/widget/submenu.c | 9 +++++++-- src/widget/text.c | 25 +++++++++++++++---------- src/widget/vulkan.c | 9 +++++++-- src/widget/window.c | 9 +++++++-- 18 files changed, 125 insertions(+), 38 deletions(-) diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 018fe79..4aebbe6 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -43,6 +43,7 @@ struct _MwLLHandler { void (*resize)(MwLL handle, void* data); void (*close)(MwLL handle, void* data); void (*move)(MwLL handle, void* data); + void (*key)(MwLL handle, void* data); }; #ifdef __cplusplus diff --git a/include/Mw/MachDep.h b/include/Mw/MachDep.h index 8c8d2cd..fa143a9 100644 --- a/include/Mw/MachDep.h +++ b/include/Mw/MachDep.h @@ -14,6 +14,7 @@ #include #include #include +#include #ifndef _WIN32 #include #include diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index b6b83cc..ce4567b 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -26,6 +26,7 @@ typedef void* MwWidget; 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 (*MwUserHandler)(MwWidget handle, void* user_data, void* call_data); typedef void (*MwErrorHandler)(int code, const char* message, void* user_data); @@ -118,6 +119,12 @@ struct _MwClass { MwHandler mouse_move; MwHandler mouse_up; MwHandler mouse_down; + MwHandler4 key; + void* reserved1; + void* reserved2; + void* reserved3; + void* reserved4; + void* reserved5; }; struct _MwFont { diff --git a/src/backend/gdi.c b/src/backend/gdi.c index f585854..a6c2027 100644 --- a/src/backend/gdi.c +++ b/src/backend/gdi.c @@ -389,8 +389,3 @@ void MwLLDetach(MwLL handle, MwPoint* point) { SetWindowLongPtr(handle->hWnd, GWL_STYLE, style); } - -void MwLLShow(MwLL handle, int show) { - ShowWindow(handle->hWnd, show ? SW_NORMAL : SW_HIDE); - if(show) SetFocus(handle->hWnd); -} diff --git a/src/backend/x11.c b/src/backend/x11.c index 08b55a1..b9c3aea 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -1,7 +1,7 @@ /* $Id$ */ #include -static unsigned long mask = ExposureMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask; +static unsigned long mask = ExposureMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | KeymapNotify; static void create_pixmap(MwLL handle) { XWindowAttributes attr; @@ -150,6 +150,8 @@ void MwLLNextEvent(MwLL handle) { p.y = ev.xbutton.y; MwLLDispatch(handle, down, &p); + + XSetInputFocus(handle->display, handle->window, RevertToNone, CurrentTime); } } else if(ev.type == ButtonRelease) { if(ev.xbutton.button == Button1) { @@ -179,6 +181,19 @@ void MwLLNextEvent(MwLL handle) { p.y = ev.xmotion.y; MwLLDispatch(handle, move, &p); + } else if(ev.type == KeyPress) { + int n; + KeySym sym = XLookupKeysym(&ev.xkey, 0); + char* str = XKeysymToString(sym); + char s = str == NULL ? 0 : str[0]; + + if(ev.xkey.state & (ShiftMask | LockMask)) { + n = toupper((int)s); + } else { + n = s; + } + + MwLLDispatch(handle, key, &n); } if(render) { int x, y; diff --git a/src/core.c b/src/core.c index b79a8fe..bdbb5ab 100644 --- a/src/core.c +++ b/src/core.c @@ -66,6 +66,13 @@ static void llmovehandler(MwLL handle, void* data) { MwDispatch(h, mouse_move); } +static void llkeyhandler(MwLL handle, void* data) { + MwWidget h = (MwWidget)handle->user; + int key = *(int*)data; + + MwDispatch3(h, key, key); +} + 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)); @@ -92,6 +99,7 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent, h->lowlevel->handler->resize = llresizehandler; h->lowlevel->handler->close = llclosehandler; h->lowlevel->handler->move = llmovehandler; + h->lowlevel->handler->key = llkeyhandler; if(parent != NULL) arrput(parent->children, h); diff --git a/src/widget/button.c b/src/widget/button.c index 89d7127..f3d343f 100644 --- a/src/widget/button.c +++ b/src/widget/button.c @@ -74,6 +74,11 @@ MwClassRec MwButtonClassRec = { prop_change, /* prop_change */ NULL, /* mouse_move */ MwForceRender, /* mouse_up */ - MwForceRender /* mouse_down */ -}; + MwForceRender, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwButtonClass = &MwButtonClassRec; diff --git a/src/widget/checkbox.c b/src/widget/checkbox.c index 570f691..222d04e 100644 --- a/src/widget/checkbox.c +++ b/src/widget/checkbox.c @@ -46,6 +46,11 @@ MwClassRec MwCheckBoxClassRec = { prop_change, /* prop_change */ NULL, /* mouse_move */ MwForceRender, /* mouse_up */ - MwForceRender /* mouse_down */ -}; + MwForceRender, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwCheckBoxClass = &MwCheckBoxClassRec; diff --git a/src/widget/frame.c b/src/widget/frame.c index 4f5a324..d612cb4 100644 --- a/src/widget/frame.c +++ b/src/widget/frame.c @@ -32,6 +32,11 @@ MwClassRec MwFrameClassRec = { NULL, /* prop_change */ NULL, /* mouse_move */ NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + NULL, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwFrameClass = &MwFrameClassRec; diff --git a/src/widget/image.c b/src/widget/image.c index 23a942c..b7df424 100644 --- a/src/widget/image.c +++ b/src/widget/image.c @@ -33,6 +33,11 @@ MwClassRec MwImageClassRec = { prop_change, /* prop_change */ NULL, /* mouse_move */ NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + NULL, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwImageClass = &MwImageClassRec; diff --git a/src/widget/label.c b/src/widget/label.c index f5a828d..cfd4f1b 100644 --- a/src/widget/label.c +++ b/src/widget/label.c @@ -53,6 +53,11 @@ MwClassRec MwLabelClassRec = { prop_change, /* prop_change */ NULL, /* mouse_move */ NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + NULL, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwLabelClass = &MwLabelClassRec; diff --git a/src/widget/menu.c b/src/widget/menu.c index f1533a1..88d1280 100644 --- a/src/widget/menu.c +++ b/src/widget/menu.c @@ -175,8 +175,13 @@ MwClassRec MwMenuClassRec = { NULL, /* prop_change */ NULL, /* mouse_move */ mouse_up, /* mouse_up */ - mouse_down /* mouse_down */ -}; + mouse_down, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwMenuClass = &MwMenuClassRec; MwMenu MwMenuAdd(MwWidget handle, MwMenu menu, const char* name) { diff --git a/src/widget/opengl.c b/src/widget/opengl.c index d958c01..668b172 100644 --- a/src/widget/opengl.c +++ b/src/widget/opengl.c @@ -137,8 +137,13 @@ MwClassRec MwOpenGLClassRec = { NULL, /* prop_change */ NULL, /* mouse_move */ NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + NULL, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwOpenGLClass = &MwOpenGLClassRec; void MwOpenGLMakeCurrent(MwWidget handle) { diff --git a/src/widget/scrollbar.c b/src/widget/scrollbar.c index f12ced0..9af89d8 100644 --- a/src/widget/scrollbar.c +++ b/src/widget/scrollbar.c @@ -194,8 +194,13 @@ MwClassRec MwScrollBarClassRec = { prop_change, /* prop_change */ mouse_move, /* mouse_move */ MwForceRender, /* mouse_up */ - mouse_down /* mouse_down */ -}; + mouse_down, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwScrollBarClass = &MwScrollBarClassRec; int MwScrollBarGetVisibleLength(MwWidget handle) { diff --git a/src/widget/submenu.c b/src/widget/submenu.c index 78a485a..895962d 100644 --- a/src/widget/submenu.c +++ b/src/widget/submenu.c @@ -155,8 +155,13 @@ MwClassRec MwSubMenuClassRec = { NULL, /* prop_change */ NULL, /* mouse_move */ MwForceRender, /* mouse_up */ - MwForceRender /* mouse_down */ -}; + MwForceRender, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwSubMenuClass = &MwSubMenuClassRec; void MwSubMenuAppear(MwWidget handle, MwMenu menu, MwPoint* point) { diff --git a/src/widget/text.c b/src/widget/text.c index 7eaed71..bf8aecb 100644 --- a/src/widget/text.c +++ b/src/widget/text.c @@ -25,14 +25,19 @@ static void draw(MwWidget handle) { } MwClassRec MwTextClassRec = { - create, /* create */ - NULL, /* destroy */ - draw, /* draw */ - NULL, /* click */ - NULL, /* parent_resize */ - NULL, /* prop_change */ - NULL, /* mouse_move */ - NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + create, /* create */ + NULL, /* destroy */ + draw, /* draw */ + NULL, /* click */ + NULL, /* parent_resize */ + NULL, /* prop_change */ + NULL, /* mouse_move */ + MwForceRender, /* mouse_up */ + MwForceRender, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwTextClass = &MwTextClassRec; diff --git a/src/widget/vulkan.c b/src/widget/vulkan.c index 7f1154c..5cbdd06 100644 --- a/src/widget/vulkan.c +++ b/src/widget/vulkan.c @@ -512,6 +512,11 @@ MwClassRec MwVulkanClassRec = { NULL, /* prop_change */ NULL, /* mouse_move */ NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + NULL, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwVulkanClass = &MwVulkanClassRec; diff --git a/src/widget/window.c b/src/widget/window.c index f0a7be8..462f856 100644 --- a/src/widget/window.c +++ b/src/widget/window.c @@ -30,8 +30,13 @@ MwClassRec MwWindowClassRec = { NULL, /* prop_change */ NULL, /* mouse_move */ NULL, /* mouse_up */ - NULL /* mouse_down */ -}; + NULL, /* mouse_down */ + NULL, /* key */ + NULL, + NULL, + NULL, + NULL, + NULL}; MwClass MwWindowClass = &MwWindowClassRec; void MwWindowSetIcon(MwWidget handle, MwLLPixmap pixmap) {