]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/button.cpp
Include wx/pen.h according to precompiled headers of wx/wx.h (with other minor cleaning).
[wxWidgets.git] / src / msw / button.cpp
index 422386516934a68d9c7a98db2ca9413d1d604cd5..ad0fbede215350c8bc4e9ec09b0ce99d43c72379 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        msw/button.cpp
+// Name:        src/msw/button.cpp
 // Purpose:     wxButton
 // Author:      Julian Smart
 // Modified by:
 // 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"
 
 
 #if wxUSE_BUTTON
 
+#include "wx/button.h"
+
 #ifndef WX_PRECOMP
     #include "wx/app.h"
-    #include "wx/button.h"
     #include "wx/brush.h"
     #include "wx/panel.h"
     #include "wx/bmpbuttn.h"
     #include "wx/settings.h"
     #include "wx/dcscreen.h"
+    #include "wx/dcclient.h"
 #endif
 
+#include "wx/stockitem.h"
+#include "wx/tokenzr.h"
 #include "wx/msw/private.h"
 
 // ----------------------------------------------------------------------------
@@ -121,13 +121,28 @@ IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
 
 bool wxButton::Create(wxWindow *parent,
                       wxWindowID id,
-                      const wxString& label,
+                      const wxString& lbl,
                       const wxPoint& pos,
                       const wxSize& size,
                       long style,
                       const wxValidator& validator,
                       const wxString& name)
 {
+    wxString label(lbl);
+    if (label.empty() && wxIsStockID(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;
 
@@ -199,17 +214,16 @@ WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
 
 wxSize wxButton::DoGetBestSize() const
 {
-    int wBtn;
-    GetTextExtent(wxGetWindowText(GetHWND()), &wBtn, NULL);
+    wxClientDC dc(wx_const_cast(wxButton *, this));
+    dc.SetFont(GetFont());
 
-    int wChar, hChar;
-    wxGetCharSize(GetHWND(), &wChar, &hChar, GetFont());
+    wxCoord wBtn,
+            hBtn;
+    dc.GetMultiLineTextExtent(wxStripMenuCodes(GetLabel()), &wBtn, &hBtn);
 
     // add a margin -- the button is wider than just its label
-    wBtn += 3*wChar;
-
-    // the button height is proportional to the height of the font used
-    int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
+    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
@@ -225,7 +239,9 @@ wxSize wxButton::DoGetBestSize() const
         return sz;
     }
 
-    return wxSize(wBtn, hBtn);
+    wxSize best(wBtn, hBtn);
+    CacheBestSize(best);
+    return best;
 }
 
 /* static */
@@ -479,8 +495,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);
@@ -691,4 +730,3 @@ bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
 #endif // __WIN32__
 
 #endif // wxUSE_BUTTON
-