Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiline text label #465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions windows/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ int uiWindowsWindowTextWidth(HWND hwnd)

size.cx = 0;
size.cy = 0;
//save the max width of multiline text
auto maxWidth = size.cx;

text = windowTextAndLen(hwnd, &len);
WCHAR *start = text, *end = text;
if (len == 0) // no text; nothing to do
goto noTextOrError;

Expand All @@ -66,20 +69,39 @@ int uiWindowsWindowTextWidth(HWND hwnd)
ReleaseDC(hwnd, dc);
goto noTextOrError;
}
if (GetTextExtentPoint32W(dc, text, len, &size) == 0) {
logLastError(L"error getting text extent point");
// continue anyway, assuming size is 0
size.cx = 0;
size.cy = 0;

// calculate width of each line
while(start != text + len) {
while(*start == '\n' && start != text + len) {
start++;
}
if(start == text + len) {
break;
}
end = start + 1;
while(*end != '\n' && end != text + len){
end++;
}
if (GetTextExtentPoint32W(dc, start, end - start, &size) == 0) {
logLastError(L"error getting text extent point");
// continue anyway, assuming size is 0
size.cx = 0;
size.cy = 0;
}
if(size.cx > maxWidth) {
maxWidth = size.cx;
}
start = end;
}

// continue on errors; we got what we want
if (SelectObject(dc, prevfont) != hMessageFont)
logLastError(L"error restoring previous font into device context");
if (ReleaseDC(hwnd, dc) == 0)
logLastError(L"error releasing DC");

uiprivFree(text);
return size.cx;
return maxWidth;

noTextOrError:
uiprivFree(text);
Expand Down