Author: gadamopoulos Date: Mon May 15 16:05:14 2017 New Revision: 74552 URL: http://svn.reactos.org/svn/reactos?rev=74552&view=rev Log: [UXTHEME] -ThemeDrawCaptionText: Try to avoid a heap allocation when getting the window caption.
Modified: trunk/reactos/dll/win32/uxtheme/nonclient.c Modified: trunk/reactos/dll/win32/uxtheme/nonclient.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/uxtheme/nonclient.c?rev=74552&r1=74551&r2=74552&view=diff ============================================================================== --- trunk/reactos/dll/win32/uxtheme/nonclient.c [iso-8859-1] (original) +++ trunk/reactos/dll/win32/uxtheme/nonclient.c [iso-8859-1] Mon May 15 16:05:14 2017 @@ -100,16 +100,7 @@ return hIcon; } -WCHAR *UserGetWindowCaption(HWND hwnd) -{ - INT len = 512; - WCHAR *text; - text = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); - if (text) InternalGetWindowText(hwnd, text, len); - return text; -} - -HRESULT WINAPI ThemeDrawCaptionText(PDRAW_CONTEXT pcontext, RECT* pRect, int iPartId, int iStateId, LPCWSTR pszText) +HRESULT WINAPI ThemeDrawCaptionText(PDRAW_CONTEXT pcontext, RECT* pRect, int iPartId, int iStateId) { HRESULT hr; HFONT hFont = NULL; @@ -117,6 +108,25 @@ LOGFONTW logfont; COLORREF textColor; COLORREF oldTextColor; + + WCHAR buffer[50]; + WCHAR *pszText = buffer; + INT len; + + len = InternalGetWindowText(pcontext->hWnd, NULL, 0); + if (!len) + return S_OK; + + len++; /* From now on this is the size of the buffer so include the null */ + + if (len > 50) + { + pszText = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); + if (!pszText) + return E_FAIL; + } + + InternalGetWindowText(pcontext->hWnd, pszText, len); hr = GetThemeSysFont(0,TMT_CAPTIONFONT,&logfont); if(SUCCEEDED(hr)) @@ -136,16 +146,20 @@ iPartId, iStateId, pszText, - lstrlenW(pszText), + len - 1, DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS, 0, pRect); SetTextColor(pcontext->hDC, oldTextColor); - if(hFont) + if (hFont) { SelectObject(pcontext->hDC, oldFont); DeleteObject(hFont); + } + if (pszText != buffer) + { + HeapFree(GetProcessHeap(), 0, pszText); } return S_OK; } @@ -320,7 +334,6 @@ RECT rcPart; int iPart, iState; HICON hIcon; - WCHAR *CaptionText; // See also win32ss/user/ntuser/nonclient.c!UserDrawCaptionBar // and win32ss/user/ntuser/nonclient.c!UserDrawCaption @@ -328,8 +341,6 @@ hIcon = UserGetWindowIcon(pcontext); else hIcon = NULL; - - CaptionText = UserGetWindowCaption(pcontext->hWnd); /* Get the caption part and state id */ if (pcontext->wi.dwStyle & WS_MINIMIZE) @@ -378,11 +389,7 @@ rcPart.right -= 4; /* Draw the caption */ - if (CaptionText) - { - ThemeDrawCaptionText(pcontext, &rcPart, iPart, iState, CaptionText); - HeapFree(GetProcessHeap(), 0, CaptionText); - } + ThemeDrawCaptionText(pcontext, &rcPart, iPart, iState); } static void