From 17709f1fc7979b9b37f03963770b175b7c32a62c Mon Sep 17 00:00:00 2001 From: Jonas Kümmerlin Date: Tue, 26 May 2020 23:27:31 +0200 Subject: focus and default button handling: fixes and some documenting comments --- alf/alf.cpp | 4 ++-- alf/alf.h | 19 ++++++++++++++++++- alf/alfwindow.cpp | 16 +++++----------- widgetfactory.cpp | 1 + 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/alf/alf.cpp b/alf/alf.cpp index 717bd18..8929a5b 100644 --- a/alf/alf.cpp +++ b/alf/alf.cpp @@ -523,7 +523,7 @@ void ALF_SetFocus(HWND target) { if (GetWindowStyle(target) & WS_CHILD) - PostMessage(GetParent(target), WM_NEXTDLGCTL, (WPARAM)target, TRUE); + SendMessage(GetParent(target), WM_NEXTDLGCTL, (WPARAM)target, TRUE); else - PostMessage(target, WM_NEXTDLGCTL, (WPARAM)target, TRUE); + SendMessage(target, WM_NEXTDLGCTL, (WPARAM)target, TRUE); } diff --git a/alf/alf.h b/alf/alf.h index 2e07736..e1ad26e 100644 --- a/alf/alf.h +++ b/alf/alf.h @@ -8,7 +8,7 @@ extern "C" { #endif typedef struct { - void (*create)(void * /*closure*/, HWND /*window*/); + BOOL (*initialize)(void * /*closure*/, HWND /*window*/); // return FALSE if you set the focus, TRUE if you want default initial focus void (*destroy)(void * /*closure*/, HWND /*window*/); BOOL (*close)(void * /*closure*/, HWND /*window*/); void (*postdestroy)(void * /*closure*/); @@ -189,6 +189,23 @@ ALF_AddEdit(HWND win, WORD id, int x, int y, const TCHAR *text); HWND ALF_AddButton(HWND win, WORD id, int x, int y, const TCHAR *text); +// NOTE about default buttons: +// +// - If no default button is specified, the default button will be the button +// with the id IDOK. A WM_COMMAND message on pressing the return key will be +// sent even if no widget with the default id can be found. +// - In NT 3.51, the default button must be a direct child of the window. Otherwise +// the default button effect will not work. +// - In Win32s on Win3.1, the default button should be a direct child of the window. +// Otherwise the default button effect will be buggy when setting the focus +// via WM_NEXTDLGCTL or ALF_SetFocus(), but it will work correctly when using +// the TAB key. +// - Changing the default button will only fully take effect after a focus change. +// It might temporarily lead to artifacts such as having two or no default buttons. +// +// Recommendation: Set the default button during window initialization, and then +// immediately set the focus via ALF_SetFocus(). If you want to support NT 3.51 +// or Win32s, ensure that the default button is a direct child of the toplevel window. void ALF_SetDefaultButton(HWND win, WORD id); diff --git a/alf/alfwindow.cpp b/alf/alfwindow.cpp index 8e765ea..15b635b 100644 --- a/alf/alfwindow.cpp +++ b/alf/alfwindow.cpp @@ -117,18 +117,12 @@ ALF_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) int dpi = (int)ALF_Compat_GetDpiForWindow(hwnd); SendMessage(hwnd, ALF_WM_DPICHANGE, 0, (LPARAM)dpi); // will also update fonts - if (priv->vtbl && priv->vtbl->create) { - priv->vtbl->create(priv->closure, hwnd); + // TODO: after initializing, update window size and layout + if (priv->vtbl && priv->vtbl->initialize) { + return priv->vtbl->initialize(priv->closure, hwnd); + } else { + return TRUE; } - - // FIXME! still necessary? - /*BOOL alwaysUnderline = FALSE; - SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &alwaysUnderline, 0); - if (!alwaysUnderline) { - SendMessage(hwnd, WM_UPDATEUISTATE, MAKEWPARAM(UIS_INITIALIZE, 0), 0); - }*/ - - ALF_Layout_Apply(&priv->layout, hwnd); } ALFWindowPriv *priv = (ALFWindowPriv*)(void*)GetWindowLongPtr(hwnd, DLGWINDOWEXTRA); diff --git a/widgetfactory.cpp b/widgetfactory.cpp index 315a95c..db720ac 100644 --- a/widgetfactory.cpp +++ b/widgetfactory.cpp @@ -865,6 +865,7 @@ WinMain ALF_LayoutSetColumnExpandNumerator(win, 4, 1); ALF_SetDefaultButton(win, ID_HELLO); + ALF_SetFocus(GetNextDlgTabItem(win, win, FALSE)); ALF_ResizeWindow(win, 1, 1); -- cgit v1.2.3