From 960533ab1f3cc701e7e9991ec65478785918a193 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 22 Apr 2016 14:36:21 -0400 Subject: [PATCH] Abstracted out the message filter stuff so we can add a dialog message filter to allow tab navigation anywhere next. --- windows/areaevents.c | 36 ++++----------------------------- windows/init.cpp | 6 ++++-- windows/main.cpp | 37 ++++++++++++++++++++++++++++++++++ windows/uipriv_windows_new.hpp | 4 ++++ 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/windows/areaevents.c b/windows/areaevents.c index 42e001bf..8913d223 100644 --- a/windows/areaevents.c +++ b/windows/areaevents.c @@ -393,23 +393,17 @@ BOOL areaDoEvents(uiArea *a, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *l return FALSE; } -static HHOOK areaFilter; - // TODO affect visibility properly // TODO what did this mean -static LRESULT CALLBACK areaFilterProc(int code, WPARAM wParam, LPARAM lParam) +static BOOL areaFilter(MSG *msg) { - MSG *msg = (MSG *) lParam; LRESULT handled; - if (code < 0) - goto callNext; - // is the recipient an area? if (msg->hwnd == NULL) // this can happen; for example, WM_TIMER - goto callNext; + return FALSE; if (windowClassOf(msg->hwnd, areaClass, NULL) != 0) - goto callNext; // nope + return FALSE; // nope handled = 0; switch (msg->message) { @@ -423,27 +417,5 @@ static LRESULT CALLBACK areaFilterProc(int code, WPARAM wParam, LPARAM lParam) break; // otherwise handled remains 0, as we didn't handle this } - if (!handled) - goto callNext; - - // we handled it; discard the message so the dialog manager doesn't see it - return 1; - -callNext: - return CallNextHookEx(areaFilter, code, wParam, lParam); -} - -int registerAreaFilter(void) -{ - areaFilter = SetWindowsHookExW(WH_MSGFILTER, - areaFilterProc, - hInstance, - GetCurrentThreadId()); - return areaFilter != NULL; -} - -void unregisterAreaFilter(void) -{ - if (UnhookWindowsHookEx(areaFilter) == 0) - logLastError("error unregistering uiArea message filter in unregisterAreaFilter()"); + return (BOOL) handled; } diff --git a/windows/init.cpp b/windows/init.cpp index 22e33986..a2492e0d 100644 --- a/windows/init.cpp +++ b/windows/init.cpp @@ -126,8 +126,9 @@ const char *uiInit(uiInitOptions *o) if (registerAreaClass(hDefaultIcon, hDefaultCursor) == 0) return ieLastErr("registering uiArea window class"); - if (registerAreaFilter() == 0) - return ieLastErr("registering uiArea message filter"); + + if (registerMessageFilter() == 0) + return ieLastErr("registering libui message filter"); if (registerD2DScratchClass(hDefaultIcon, hDefaultCursor) == 0) return ieLastErr("initializing D2D scratch window class"); @@ -139,6 +140,7 @@ void uiUninit(void) { uninitMenus(); unregisterD2DScratchClass(); + unregisterMessageFilter(); unregisterArea(); uninitDrawText(); uninitDraw(); diff --git a/windows/main.cpp b/windows/main.cpp index ed7102ca..6d4054a5 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -1,6 +1,43 @@ // 6 april 2015 #include "uipriv_windows.hpp" +static HHOOK filter; + +static LRESULT CALLBACK filterProc(int code, WPARAM wParam, LPARAM lParam) +{ + MSG *msg = (MSG *) lParam; + BOOL discard; + + if (code < 0) + goto callNext; + + discard = !areaFilter(msg); + + if (discard) + goto callNext; + + // we handled it; discard the message so the dialog manager doesn't see it + return 1; + +callNext: + return CallNextHookEx(areaFilter, code, wParam, lParam); +} + +int registerMessageFilter(void) +{ + filter = SetWindowsHookExW(WH_MSGFILTER, + filterProc, + hInstance, + GetCurrentThreadId()); + return filter != NULL; +} + +void unregisterMessageFilter(void) +{ + if (UnhookWindowsHookEx(areaFilter) == 0) + logLastError(L"error unregistering libui message filter"); +} + // TODO http://blogs.msdn.com/b/oldnewthing/archive/2005/04/08/406509.aspx when adding accelerators, TranslateAccelerators() before IsDialogMessage() void uiMain(void) diff --git a/windows/uipriv_windows_new.hpp b/windows/uipriv_windows_new.hpp index f8797dad..c52768a7 100644 --- a/windows/uipriv_windows_new.hpp +++ b/windows/uipriv_windows_new.hpp @@ -59,3 +59,7 @@ extern uiInitOptions options; extern HWND utilWindow; extern const char *initUtilWindow(HICON hDefaultIcon, HCURSOR hDefaultCursor); extern void uninitUtilWindow(void); + +// main.cpp +extern void registerMessageFilter(void); +extern void unregisterMessageFilter(void);