Fix alignment for multiline buttons using custom colours in wxMSW

Owner-drawn buttons with multiline labels were always centered.

Fix this by handling their alignment explicitly when drawing them, as
::DrawText() doesn't do it for multiline strings.

Closes #18131.
This commit is contained in:
Vadim Zeitlin
2018-05-07 13:44:15 +02:00
parent 41a920cf72
commit 1680c28284
2 changed files with 38 additions and 7 deletions

View File

@@ -92,6 +92,7 @@ wxMSW:
- Fix saving/restoring window position for maximized windows.
- Fix stack corruption when using wxStackWalker (srfisk).
- Fix positioning windows at positions >= SHORT_MAX (Cătălin Răceanu).
- Honour alignment flags for multiline buttons using custom colours too.
3.1.1: (released 2018-02-19)

View File

@@ -930,21 +930,51 @@ void DrawButtonText(HDC hdc,
{
// draw multiline label
// center text horizontally in any case
flags |= DT_CENTER;
// first we need to compute its bounding rect
RECT rc;
::CopyRect(&rc, pRect);
::DrawText(hdc, text.t_str(), text.length(), &rc,
DT_CENTER | DT_CALCRECT);
flags | DT_CALCRECT);
// now center this rect inside the entire button area
// now position this rect inside the entire button area: notice
// that DrawText() doesn't respect alignment flags for multiline
// text, which is why we have to do it on our own (but still use
// the horizontal alignment flags for the individual lines to be
// aligned correctly)
const LONG w = rc.right - rc.left;
const LONG h = rc.bottom - rc.top;
if ( btn->HasFlag(wxBU_RIGHT) )
{
rc.left = pRect->right - w;
flags |= DT_RIGHT;
}
else if ( !btn->HasFlag(wxBU_LEFT) )
{
rc.left = pRect->left + (pRect->right - pRect->left)/2 - w/2;
rc.right = rc.left+w;
flags |= DT_CENTER;
}
else // wxBU_LEFT
{
rc.left = pRect->left;
}
if ( btn->HasFlag(wxBU_BOTTOM) )
{
rc.top = pRect->bottom - h;
}
else if ( !btn->HasFlag(wxBU_TOP) )
{
rc.top = pRect->top + (pRect->bottom - pRect->top)/2 - h/2;
}
else // wxBU_TOP
{
rc.top = pRect->top;
}
rc.right = rc.left+w;
rc.bottom = rc.top+h;
::DrawText(hdc, text.t_str(), text.length(), &rc, flags);