From a06c7ff02ce50243cd1e4bef8bad25bd1978bcda Mon Sep 17 00:00:00 2001 From: Jonas Kümmerlin Date: Mon, 25 May 2020 21:35:28 +0200 Subject: add explicit functions for adding native buttons --- alf/alflayout.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'alf/alflayout.cpp') diff --git a/alf/alflayout.cpp b/alf/alflayout.cpp index 470f728..974cd78 100644 --- a/alf/alflayout.cpp +++ b/alf/alflayout.cpp @@ -256,6 +256,46 @@ ALF_Layout_CalcCheckboxSize(HWND hwndWindow, ALFLayout *layout, HWND hwndCheckbo ReleaseDC(hwndCheckbox, hDC); } +static void +ALF_Layout_CalcButtonSize(HWND hwndWindow, ALFLayout *layout, HWND hwndButton, SIZE *pSize) +{ + (void)hwndWindow; + + HDC hdc = GetDC(hwndButton); + HFONT oldFont = SelectFont(hdc, (HFONT)SendMessage(hwndButton, WM_GETFONT, 0, 0)); + + // calc drawtext style + UINT format = DT_LEFT | DT_EXPANDTABS | DT_CALCRECT; + + if (!(GetWindowLong(hwndButton, GWL_STYLE) & BS_MULTILINE)) + format |= DT_SINGLELINE; + + RECT r = { 0, 0, 0x7FFFFFFF, 100 }; + + TCHAR *textbuf = ALF_Text(hwndButton); + DrawText(hdc, textbuf, -1, &r, format); + ALF_Free(textbuf); + + int xpadding = ALF_Compat_GetSystemMetricsForDpi(SM_CXEDGE, + (UINT)layout->dpi) * 2 + 6; + int ypadding = ALF_Compat_GetSystemMetricsForDpi(SM_CYEDGE, + (UINT)layout->dpi) * 2 + 4; + + if (pSize->cx < r.right - r.left + xpadding) { + pSize->cx = r.right - r.left + xpadding; + } + if (pSize->cy < r.bottom - r.top + ypadding) { + pSize->cy = r.bottom - r.top + ypadding; + } + if (pSize->cx < pSize->cy) { + pSize->cx = pSize->cy; + } + + SelectFont(hdc, oldFont); + + ReleaseDC(hwndButton, hdc); +} + static void ALF_Layout_CalcMinWidgetSize(ALFLayout *layout, ALFWidgetPriv *c, HWND window, SIZE *s) { @@ -280,6 +320,9 @@ ALF_Layout_CalcMinWidgetSize(ALFLayout *layout, ALFWidgetPriv *c, HWND window, S case ALF_LAYOUT_SIZE_CHECKBOX: ALF_Layout_CalcCheckboxSize(window, layout, c->hwnd, s); break; + case ALF_LAYOUT_SIZE_PUSHBUTTON: + ALF_Layout_CalcButtonSize(window, layout, c->hwnd, s); + break; default: // FIXME! unimplemented break; -- cgit v1.2.3