From b0b0e97aa5a06b22768bb9c9ea5e8caf383d78a4 Mon Sep 17 00:00:00 2001 From: Jonas Kümmerlin Date: Thu, 27 Dec 2018 20:50:39 +0100 Subject: split into multiple files --- alf/alflabel.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 alf/alflabel.cpp (limited to 'alf/alflabel.cpp') diff --git a/alf/alflabel.cpp b/alf/alflabel.cpp new file mode 100644 index 0000000..e9ae550 --- /dev/null +++ b/alf/alflabel.cpp @@ -0,0 +1,83 @@ +#include "alfpriv.h" + +/* LABEL */ + +static LRESULT CALLBACK +ALF__LabelSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) +{ + (void)uIdSubclass; + (void)dwRefData; + + ALFAPP app = (ALFAPP)dwRefData; + + if (uMsg == ALF_WM_QUERYSIZE) { + HDC hdcLabel = GetDC(hwnd); + HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); + HFONT oldFont = 0; + if (font) + oldFont = SelectFont(hdcLabel, font); + + // calc drawtext style + DWORD style = GetWindowLong(hwnd, GWL_STYLE); + UINT format = DT_LEFT | DT_EXPANDTABS | DT_CALCRECT; + if (style & SS_NOPREFIX) + format |= DT_NOPREFIX; + if (style & SS_EDITCONTROL) + format |= DT_EDITCONTROL; + if (style & SS_ENDELLIPSIS || style & SS_PATHELLIPSIS || style & SS_WORDELLIPSIS) + format |= DT_SINGLELINE; + + RECT r = { 0, 0, 100, 100 }; + + int textlen = GetWindowTextLength(hwnd); + TCHAR *textbuf = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, (textlen + 1)*sizeof(TCHAR)); + GetWindowText(hwnd, textbuf, textlen+1); + + DrawText(hdcLabel, textbuf, -1, &r, format); + + HeapFree(GetProcessHeap(), 0, textbuf); + + SIZE *pSize = (SIZE*)(void*)lParam; + if (pSize->cx == 0) + pSize->cx = r.right - r.left; + if (pSize->cy == 0) + pSize->cy = r.bottom - r.top; + + if (font) + SelectFont(hdcLabel, oldFont); + + ReleaseDC(hwnd, hdcLabel); + } + + return app->compatFn->DefSubclassProc(hwnd, uMsg, wParam, lParam); +} + +HWND +ALF_AddLabel(HWND win, WORD id, UINT x, UINT y, const WCHAR *text) +{ + HWND hwndLabel = CreateWindow(L"STATIC", + text, + WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, + 0, 0, 100, 100, + win, + (HMENU)(int)id, + (HINSTANCE)GetWindowLongPtr(win, GWLP_HINSTANCE), + NULL); + + ALFAPP app = ALF_ApplicationFromWindow(win); + app->compatFn->SetWindowSubclass(hwndLabel, ALF__LabelSubclassProc, 0, (DWORD_PTR)app); + + ALFAddWidgetParams p; + ZeroMemory(&p, sizeof(p)); + p.hwnd = hwndLabel; + p.x = x; + p.y = y; + p.width = 0; + p.height = 0; + p.flags = ALF_QUERYSIZE | ALF_MESSAGEFONT; + p.margins[0] = 300; // TODO: pixel perfect margins from system metrics + + ALF_AddWidgetEx(win, &p); + + return hwndLabel; +} -- cgit v1.2.3