From 1680c2828447938c1fae48d36f8a052f215d2721 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 7 May 2018 13:44:15 +0200 Subject: [PATCH] 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. --- docs/changes.txt | 1 + src/msw/anybutton.cpp | 44 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 29956046fc..e6551fa719 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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) diff --git a/src/msw/anybutton.cpp b/src/msw/anybutton.cpp index 2202432834..85a7a79062 100644 --- a/src/msw/anybutton.cpp +++ b/src/msw/anybutton.cpp @@ -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; - rc.left = pRect->left + (pRect->right - pRect->left)/2 - w/2; + + 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; + + 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.top = pRect->top + (pRect->bottom - pRect->top)/2 - h/2; rc.bottom = rc.top+h; ::DrawText(hdc, text.t_str(), text.length(), &rc, flags);