X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4af4dec65ff21d5fc107312bd8708562bff4a148..591cc82deb5bc6dd0af6047ecfa7ce7cf4f8d859:/src/msw/button.cpp diff --git a/src/msw/button.cpp b/src/msw/button.cpp index 893dd88301..ef43802277 100644 --- a/src/msw/button.cpp +++ b/src/msw/button.cpp @@ -72,6 +72,10 @@ #define ODS_NOFOCUSRECT 0x0200 #endif +#ifndef DT_HIDEPREFIX + #define DT_HIDEPREFIX 0x00100000 +#endif + // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- @@ -163,33 +167,41 @@ void wxMSWButton::UpdateMultilineStyle(HWND hwnd, const wxString& label) ::SetWindowLong(hwnd, GWL_STYLE, styleNew); } +wxSize wxMSWButton::GetFittingSize(wxWindow *win, const wxSize& sizeLabel) +{ + // FIXME: this is pure guesswork, need to retrieve the real button margins + wxSize sizeBtn = sizeLabel; + + sizeBtn.x += 3*win->GetCharWidth(); + sizeBtn.y = 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(sizeLabel.y)/10; + + return sizeBtn; +} + wxSize wxMSWButton::ComputeBestSize(wxControl *btn) { wxClientDC dc(btn); - wxCoord wBtn, - hBtn; - dc.GetMultiLineTextExtent(btn->GetLabelText(), &wBtn, &hBtn); + wxSize sizeBtn; + dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y); - // FIXME: this is pure guesswork, need to retrieve the real button margins - wBtn += 3*btn->GetCharWidth(); - hBtn = 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(hBtn)/10; + sizeBtn = GetFittingSize(btn, sizeBtn); // all buttons have at least the standard size unless the user explicitly // wants them to be of smaller size and used wxBU_EXACTFIT style when // creating the button if ( !btn->HasFlag(wxBU_EXACTFIT) ) { - wxSize sz = wxButton::GetDefaultSize(); - if ( wBtn < sz.x ) - wBtn = sz.x; - if ( hBtn < sz.y ) - hBtn = sz.y; + wxSize sizeDef = wxButton::GetDefaultSize(); + if ( sizeBtn.x < sizeDef.x ) + sizeBtn.x = sizeDef.x; + if ( sizeBtn.y < sizeDef.y ) + sizeBtn.y = sizeDef.y; } - wxSize best(wBtn, hBtn); - btn->CacheBestSize(best); - return best; + btn->CacheBestSize(sizeBtn); + + return sizeBtn; } // ---------------------------------------------------------------------------- @@ -293,7 +305,7 @@ void wxButton::SetLabel(const wxString& label) wxSize wxButton::DoGetBestSize() const { - return wxMSWButton::ComputeBestSize(wx_const_cast(wxButton *, this)); + return wxMSWButton::ComputeBestSize(const_cast(this)); } /* static */ @@ -582,11 +594,15 @@ WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) static void DrawButtonText(HDC hdc, RECT *pRect, const wxString& text, - COLORREF col) + COLORREF col, + int flags) { COLORREF colOld = SetTextColor(hdc, col); int modeOld = SetBkMode(hdc, TRANSPARENT); + // center text horizontally in any case + flags |= DT_CENTER; + if ( text.find(_T('\n')) != wxString::npos ) { // draw multiline label @@ -605,13 +621,14 @@ static void DrawButtonText(HDC hdc, rc.top = (pRect->bottom - pRect->top)/2 - h/2; rc.bottom = rc.top+h; - ::DrawText(hdc, text.wx_str(), text.length(), &rc, DT_CENTER); + ::DrawText(hdc, text.wx_str(), text.length(), &rc, flags); } else // single line label { - // Note: we must have DT_SINGLELINE for DT_VCENTER to work. + // centre text vertically too (notice that we must have DT_SINGLELINE + // for DT_VCENTER to work) ::DrawText(hdc, text.wx_str(), text.length(), pRect, - DT_SINGLELINE | DT_CENTER | DT_VCENTER); + flags | DT_SINGLELINE | DT_VCENTER); } SetBkMode(hdc, modeOld); @@ -890,11 +907,15 @@ bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis) } } - COLORREF colFg = wxColourToRGB(GetForegroundColour()); - if ( state & ODS_DISABLED ) colFg = GetSysColor(COLOR_GRAYTEXT) ; - wxString label = GetLabel(); - if ( state & ODS_NOACCEL ) label = GetLabelText() ; - DrawButtonText(hdc, &rectBtn, label, colFg); + COLORREF colFg = state & ODS_DISABLED + ? ::GetSysColor(COLOR_GRAYTEXT) + : wxColourToRGB(GetForegroundColour()); + + // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000) systems + // but by happy coincidence ODS_NOACCEL is not used under them neither so + // DT_HIDEPREFIX should never be used there + DrawButtonText(hdc, &rectBtn, GetLabel(), colFg, + state & ODS_NOACCEL ? DT_HIDEPREFIX : 0); return true; }