X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2376eee2483911ac454f16313a48da5740457765..25e6a4a698b15d72874bd1dbf4d260163a60f005:/src/msw/button.cpp diff --git a/src/msw/button.cpp b/src/msw/button.cpp index ba86e1650b..6b6f67aa65 100644 --- a/src/msw/button.cpp +++ b/src/msw/button.cpp @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "button.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -38,6 +34,7 @@ #include "wx/bmpbuttn.h" #include "wx/settings.h" #include "wx/dcscreen.h" + #include "wx/dcclient.h" #endif #include "wx/stockitem.h" @@ -132,7 +129,18 @@ bool wxButton::Create(wxWindow *parent, { wxString label(lbl); if (label.empty() && wxIsStockID(id)) - label = wxGetStockLabel(id); + { + // On Windows, some buttons aren't supposed to have + // mnemonics, so strip them out. + + label = wxGetStockLabel(id +#if defined(__WXMSW__) || defined(__WXWINCE__) + , ( id != wxID_OK && + id != wxID_CANCEL && + id != wxID_CLOSE ) +#endif + ); + } if ( !CreateControl(parent, id, pos, size, style, validator, name) ) return false; @@ -205,38 +213,16 @@ WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const wxSize wxButton::DoGetBestSize() const { - int wBtn = 0; - int wChar, hChar, hBtn; - wxGetCharSize(GetHWND(), &wChar, &hChar, GetFont()); + wxClientDC dc(wx_const_cast(wxButton *, this)); + dc.SetFont(GetFont()); - wxString label = wxGetWindowText(GetHWND()); - if ( label.find(_T('\n')) != wxString::npos ) - { - wxStringTokenizer tokens( label, wxT("\n") ); + wxCoord wBtn, + hBtn; + dc.GetMultiLineTextExtent(wxStripMenuCodes(GetLabel()), &wBtn, &hBtn); - // the button height is proportional to the height of the font used - hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar); - hBtn += hChar*(tokens.CountTokens()-1); - - while (tokens.HasMoreTokens()) - { - wxString sub = tokens.GetNextToken(); - int w; - GetTextExtent( sub, &w, NULL); - if (w > wBtn) - wBtn = w; - } - } - else - { - GetTextExtent( label, &wBtn, NULL); - - // the button height is proportional to the height of the font used - hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar); - } - // add a margin -- the button is wider than just its label - wBtn += 3*wChar; + wBtn += 3*GetCharWidth(); + hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hBtn); // 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 @@ -252,7 +238,9 @@ wxSize wxButton::DoGetBestSize() const return sz; } - return wxSize(wBtn, hBtn); + wxSize best(wBtn, hBtn); + CacheBestSize(best); + return best; } /* static */ @@ -506,8 +494,31 @@ static void DrawButtonText(HDC hdc, COLORREF colOld = SetTextColor(hdc, col); int modeOld = SetBkMode(hdc, TRANSPARENT); - // Note: we must have DT_SINGLELINE for DT_VCENTER to work. - ::DrawText(hdc, text, text.length(), pRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); + if ( text.find(_T('\n')) != wxString::npos ) + { + // draw multiline label + + // first we need to compute its bounding rect + RECT rc; + ::CopyRect(&rc, pRect); + ::DrawText(hdc, text, text.length(), &rc, DT_CENTER | DT_CALCRECT); + + // now center this rect inside the entire button area + const LONG w = rc.right - rc.left; + const LONG h = rc.bottom - rc.top; + rc.left = (pRect->right - pRect->left)/2 - w/2; + rc.right = rc.left+w; + rc.top = (pRect->bottom - pRect->top)/2 - h/2; + rc.bottom = rc.top+h; + + ::DrawText(hdc, text, text.length(), &rc, DT_CENTER); + } + else // single line label + { + // Note: we must have DT_SINGLELINE for DT_VCENTER to work. + ::DrawText(hdc, text, text.length(), pRect, + DT_SINGLELINE | DT_CENTER | DT_VCENTER); + } SetBkMode(hdc, modeOld); SetTextColor(hdc, colOld);