From 73ac4e4160eed3a564a9cc4128b16c49e94c06a7 Mon Sep 17 00:00:00 2001 From: Jonas Kümmerlin Date: Sat, 5 Jan 2019 23:24:33 +0100 Subject: changed my mind about how fonts should be applied to new widgets --- alf/alf.cpp | 34 +++++++++++++++++++--------------- alf/alf.h | 23 ++++++++++++++++++----- alf/alflayout.cpp | 13 +++---------- alf/alflayout.h | 2 +- alf/alfpriv.h | 3 +++ widgetfactory.cpp | 1 + 6 files changed, 45 insertions(+), 31 deletions(-) diff --git a/alf/alf.cpp b/alf/alf.cpp index 5ee591d..f36dda3 100644 --- a/alf/alf.cpp +++ b/alf/alf.cpp @@ -42,6 +42,16 @@ ALF_UpdateFonts(HWND win) SendMessage(win, ALF_WM_UPDATEFONTS, 0, 0); } +void +ALF_ApplyFontsPriv(HWND win, ALFWindowPriv *priv) +{ + ALF_Layout_ApplyFonts(&priv->layout, win, &priv->fonts); + + if (priv->vtbl->applyfonts) { + priv->vtbl->applyfonts(priv->closure, win, &priv->fonts); + } +} + void ALF_UpdateFontsPriv(HWND win, ALFWindowPriv *priv) { @@ -74,11 +84,13 @@ ALF_UpdateFontsPriv(HWND win, ALFWindowPriv *priv) priv->fonts.hMessageFont = CreateFontIndirect(&priv->fonts.lfMessageFont); } - ALF_Layout_UpdateFonts(&priv->layout, win, &priv->fonts); + ALF_ApplyFontsPriv(win, priv); +} - if (priv->vtbl->updatefonts) { - priv->vtbl->updatefonts(priv->closure, win, &priv->fonts); - } +void +ALF_ApplyFonts(HWND win) +{ + SendMessage(win, ALF_WM_APPLYFONTS, 0, 0); } void @@ -167,9 +179,9 @@ ALF_DefWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) return 0; } - if (msg == ALF_WM_GETWINDOWFONTS) { - *((ALFWindowFonts *)lparam) = priv->fonts; - return TRUE; + if (msg == ALF_WM_APPLYFONTS) { + ALF_ApplyFontsPriv(hwnd, priv); + return 0; } if (msg == WM_COMMAND) { @@ -709,11 +721,3 @@ ALF_WidgetAtLayoutPosition(HWND parent, UINT x, UINT y) return (HWND)SendMessage(parent, ALF_WM_GETWIDGETATPOS, 0, (LPARAM)&xy); } - - -BOOL -ALF_WindowFonts(HWND window, ALFWindowFonts *fonts) -{ - return (BOOL)SendMessage(window, ALF_WM_GETWINDOWFONTS, 0, (LPARAM)fonts); -} - diff --git a/alf/alf.h b/alf/alf.h index b60ae2a..3388b48 100644 --- a/alf/alf.h +++ b/alf/alf.h @@ -18,7 +18,7 @@ typedef struct { void (*destroy)(void * /*closure*/, HWND /*window*/); BOOL (*close)(void * /*closure*/, HWND /*window*/); void (*postdestroy)(void * /*closure*/); - void (*updatefonts)(void * /*closure*/, HWND /*window*/, const ALFWindowFonts *fonts); + void (*applyfonts)(void * /*closure*/, HWND /*window*/, const ALFWindowFonts *fonts); LRESULT (*message)(void * /*closure*/, HWND, UINT, WPARAM, LPARAM); LRESULT (*command)(void * /*closure*/, HWND /*window*/, WORD /*notificationcode*/, WORD /*sourceid*/, HWND /*control*/); LRESULT (*notify)(void * /*closure*/, HWND /*window*/, WPARAM /*sourceid*/, NMHDR *); @@ -48,7 +48,7 @@ typedef struct { #define ALF_WM_GETLAYOUTPARAMS (ALF_WM__BASE + 13) #define ALF_WM_SETLAYOUTPARAMS (ALF_WM__BASE + 14) #define ALF_WM_GETWIDGETATPOS (ALF_WM__BASE + 15) -#define ALF_WM_GETWINDOWFONTS (ALF_WM__BASE + 16) +#define ALF_WM_APPLYFONTS (ALF_WM__BASE + 16) typedef struct { const TCHAR *className; @@ -131,9 +131,25 @@ ALF_WidgetHwndById(HWND win, WORD id); void ALF_RecalculateLayout(HWND win); +// Recalculates the window's DPI and all fonts and applies them to child widgets. +// ALF does this automatically on a DPI or settings change so you shouldn't have +// to call ALF_UpdateFonts(). If you have added new widgets and want to set their +// fonts, call ALF_ApplyFonts() instead. +// +// Since widget sizes depend on the assigned font, you should probably +// call ALF_RecalculateLayout() afterwards. void ALF_UpdateFonts(HWND win); +// Applies window fonts to all children (depending on their layout flags). +// also calls the applyfonts() virtual function. +// +// Call this after you have added new child widgets. +// Since widget sizes depend on the assigned font, you should probably +// call ALF_RecalculateLayout() afterwards. +void +ALF_ApplyFonts(HWND win); + void ALF_ResizeWindow(HWND win, int cptWidth, int cptHeight); @@ -194,9 +210,6 @@ ALF_SetWidgetLayoutSize(HWND parent, HWND widget, UINT cptWidth, UINT cptHeight) HWND ALF_WidgetAtLayoutPosition(HWND parent, UINT x, UINT y); -BOOL -ALF_WindowFonts(HWND window, ALFWindowFonts *fonts); - // combo box HWND diff --git a/alf/alflayout.cpp b/alf/alflayout.cpp index 8d03a6b..7251ca6 100644 --- a/alf/alflayout.cpp +++ b/alf/alflayout.cpp @@ -18,7 +18,7 @@ ALF_Layout_Clear(ALFLayout *layout) static void -ALF_UpdateFontForWidget(ALFWidgetPriv *widget, const ALFWindowFonts *fonts) +ALF_ApplyFontForWidget(ALFWidgetPriv *widget, const ALFWindowFonts *fonts) { if (widget->hwnd && (widget->flags & ALF_MESSAGEFONT) == ALF_MESSAGEFONT) { SendMessage(widget->hwnd, WM_SETFONT, (WPARAM)fonts->hMessageFont, (LPARAM)NULL); @@ -30,12 +30,12 @@ ALF_UpdateFontForWidget(ALFWidgetPriv *widget, const ALFWindowFonts *fonts) } void -ALF_Layout_UpdateFonts(ALFLayout *layout, HWND window, const ALFWindowFonts *fonts) +ALF_Layout_ApplyFonts(ALFLayout *layout, HWND window, const ALFWindowFonts *fonts) { (void)window; ALF_FOR_LIST(ALFWidgetPriv, list, &layout->widgets, i) { - ALF_UpdateFontForWidget(i, fonts); + ALF_ApplyFontForWidget(i, fonts); } } @@ -255,13 +255,6 @@ ALF_Layout_AddWidget(ALFLayout* layout, HWND window, const ALFWidgetLayoutParams SetParent(w->hwnd, window); ALF_ListInsert(layout->widgets.prev, &w->list); - - if (w->flags & ALF_MESSAGEFONT) { - ALFWindowFonts fonts; - ZeroMemory(&fonts, sizeof(fonts)); - ALF_WindowFonts(window, &fonts); - ALF_UpdateFontForWidget(w, &fonts); - } } BOOL diff --git a/alf/alflayout.h b/alf/alflayout.h index 4787550..8772908 100644 --- a/alf/alflayout.h +++ b/alf/alflayout.h @@ -67,4 +67,4 @@ HWND ALF_Layout_WidgetAtPos(ALFLayout *layout, UINT x, UINT y); void -ALF_Layout_UpdateFonts(ALFLayout *layout, HWND window, const ALFWindowFonts *fonts); +ALF_Layout_ApplyFonts(ALFLayout *layout, HWND window, const ALFWindowFonts *fonts); diff --git a/alf/alfpriv.h b/alf/alfpriv.h index acea53a..c41e24f 100644 --- a/alf/alfpriv.h +++ b/alf/alfpriv.h @@ -40,6 +40,9 @@ ALF_CentipointsToPxPriv(ALFWindowPriv *priv, int cptValue); void ALF_UpdateFontsPriv(HWND hwnd, ALFWindowPriv *priv); +void +ALF_ApplyFontsPriv(HWND win, ALFWindowPriv *priv); + void ALF_RegisterComboClass(ALFAPP app); diff --git a/widgetfactory.cpp b/widgetfactory.cpp index 2f5da69..f147982 100644 --- a/widgetfactory.cpp +++ b/widgetfactory.cpp @@ -151,6 +151,7 @@ WinMain //ALF_AddWidgetLayoutFlag(win, ALF_WidgetHwndById(win, ID_LBL6), ALF_VEXPAND); //ALF_AddWidgetLayoutFlag(win, ALF_WidgetHwndById(win, ID_B1), ALF_HEXPAND); + ALF_ApplyFonts(win); ALF_RecalculateLayout(win); ALF_SetDefaultButton(win, ID_B2); -- cgit v1.2.3