+ wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
+
+ wxButtonBase::SetLabel(label);
+}
+
+// ----------------------------------------------------------------------------
+// size management including autosizing
+// ----------------------------------------------------------------------------
+
+wxSize wxButton::DoGetBestSize() const
+{
+ wxSize size;
+
+ // account for the text part
+ if ( ShowsLabel() )
+ {
+ size = wxMSWButton::ComputeBestSize(const_cast<wxButton *>(this));
+ }
+
+ if ( m_imageData )
+ {
+ // account for the bitmap size
+ const wxSize sizeBmp = m_imageData->GetBitmap(State_Normal).GetSize();
+ const wxDirection dirBmp = m_imageData->GetBitmapPosition();
+ if ( dirBmp == wxLEFT || dirBmp == wxRIGHT )
+ {
+ size.x += sizeBmp.x;
+ if ( sizeBmp.y > size.y )
+ size.y = sizeBmp.y;
+ }
+ else // bitmap on top/below the text
+ {
+ size.y += sizeBmp.y;
+ if ( sizeBmp.x > size.x )
+ size.x = sizeBmp.x;
+ }
+
+ // account for the user-specified margins
+ size += 2*m_imageData->GetBitmapMargins();
+
+ // and also for the margins we always add internally (unless we have no
+ // border at all in which case the button has exactly the same size as
+ // bitmap and so no margins should be used)
+ if ( !HasFlag(wxBORDER_NONE) )
+ {
+ int marginH = 0,
+ marginV = 0;
+#if wxUSE_UXTHEME
+ if ( wxUxThemeEngine::GetIfActive() )
+ {
+ wxUxThemeHandle theme(const_cast<wxButton *>(this), L"BUTTON");
+
+ MARGINS margins;
+ wxUxThemeEngine::Get()->GetThemeMargins(theme, NULL,
+ BP_PUSHBUTTON,
+ PBS_NORMAL,
+ TMT_CONTENTMARGINS,
+ NULL,
+ &margins);
+
+ // XP doesn't draw themed buttons correctly when the client
+ // area is smaller than 8x8 - enforce this minimum size for
+ // small bitmaps
+ size.IncTo(wxSize(8, 8));
+
+ marginH = margins.cxLeftWidth + margins.cxRightWidth
+ + 2*XP_BUTTON_EXTRA_MARGIN;
+ marginV = margins.cyTopHeight + margins.cyBottomHeight
+ + 2*XP_BUTTON_EXTRA_MARGIN;
+ }
+ else
+#endif // wxUSE_UXTHEME
+ {
+ marginH =
+ marginV = OD_BUTTON_MARGIN;
+ }
+
+ size.IncBy(marginH, marginV);
+ }
+
+ CacheBestSize(size);
+ }
+
+ return size;
+}
+
+/* static */
+wxSize wxButtonBase::GetDefaultSize()
+{
+ static wxSize s_sizeBtn;
+
+ if ( s_sizeBtn.x == 0 )
+ {
+ wxScreenDC dc;
+ dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
+
+ // the size of a standard button in the dialog units is 50x14,
+ // translate this to pixels
+ // NB1: the multipliers come from the Windows convention
+ // NB2: the extra +1/+2 were needed to get the size be the same as the
+ // size of the buttons in the standard dialog - I don't know how
+ // this happens, but on my system this size is 75x23 in pixels and
+ // 23*8 isn't even divisible by 14... Would be nice to understand
+ // why these constants are needed though!
+ s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
+ s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
+ }
+
+ return s_sizeBtn;
+}
+
+// ----------------------------------------------------------------------------
+// default button handling
+// ----------------------------------------------------------------------------
+
+/*
+ "Everything you ever wanted to know about the default buttons" or "Why do we
+ have to do all this?"
+
+ In MSW the default button should be activated when the user presses Enter
+ and the current control doesn't process Enter itself somehow. This is
+ handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
+ Another aspect of "defaultness" is that the default button has different
+ appearance: this is due to BS_DEFPUSHBUTTON style which is completely
+ separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
+ be unset if our parent window is not active so it should be unset whenever
+ we lose activation and set back when we regain it.
+
+ Final complication is that when a button is active, it should be the default
+ one, i.e. pressing Enter on a button always activates it and not another
+ one.
+
+ We handle this by maintaining a permanent and a temporary default items in
+ wxControlContainer (both may be NULL). When a button becomes the current
+ control (i.e. gets focus) it sets itself as the temporary default which
+ ensures that it has the right appearance and that Enter will be redirected
+ to it. When the button loses focus, it unsets the temporary default and so
+ the default item will be the permanent default -- that is the default button
+ if any had been set or none otherwise, which is just what we want.
+
+ NB: all this is quite complicated by now and the worst is that normally
+ it shouldn't be necessary at all as for the normal Windows programs
+ DefWindowProc() and IsDialogMessage() take care of all this
+ automatically -- however in wxWidgets programs this doesn't work for
+ nested hierarchies (i.e. a notebook inside a notebook) for unknown
+ reason and so we have to reproduce all this code ourselves. It would be
+ very nice if we could avoid doing it.
+ */
+
+// set this button as the (permanently) default one in its panel
+wxWindow *wxButton::SetDefault()
+{
+ // set this one as the default button both for wxWidgets ...
+ wxWindow *winOldDefault = wxButtonBase::SetDefault();
+
+ // ... and Windows
+ SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
+ SetDefaultStyle(this, true);
+
+ return winOldDefault;
+}
+
+// return the top level parent window if it's not being deleted yet, otherwise
+// return NULL
+static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win)
+{
+ for ( ;; )
+ {
+ // IsTopLevel() will return false for a wxTLW being deleted, so we also
+ // need the parent test for this case
+ wxWindow * const parent = win->GetParent();
+ if ( !parent || win->IsTopLevel() )
+ {
+ if ( win->IsBeingDeleted() )
+ return NULL;
+
+ break;
+ }
+
+ win = parent;
+ }
+
+ wxASSERT_MSG( win, wxT("button without top level parent?") );
+
+ wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow);
+ wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
+
+ return tlw;
+}
+
+// set this button as being currently default
+void wxButton::SetTmpDefault()
+{
+ wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
+ if ( !tlw )
+ return;
+
+ wxWindow *winOldDefault = tlw->GetDefaultItem();
+ tlw->SetTmpDefaultItem(this);
+
+ SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
+ SetDefaultStyle(this, true);
+}
+
+// unset this button as currently default, it may still stay permanent default
+void wxButton::UnsetTmpDefault()
+{
+ wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
+ if ( !tlw )
+ return;
+
+ tlw->SetTmpDefaultItem(NULL);
+
+ wxWindow *winOldDefault = tlw->GetDefaultItem();
+
+ SetDefaultStyle(this, false);
+ SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true);
+}
+
+/* static */
+void
+wxButton::SetDefaultStyle(wxButton *btn, bool on)
+{
+ // we may be called with NULL pointer -- simpler to do the check here than
+ // in the caller which does wxDynamicCast()
+ if ( !btn )
+ return;
+
+ // first, let DefDlgProc() know about the new default button
+ if ( on )
+ {
+ // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
+ // focus at all any more
+ if ( !wxTheApp->IsActive() )
+ return;
+
+ wxWindow * const tlw = wxGetTopLevelParent(btn);
+ wxCHECK_RET( tlw, wxT("button without top level window?") );
+
+ ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L);
+
+ // sending DM_SETDEFID also changes the button style to
+ // BS_DEFPUSHBUTTON so there is nothing more to do
+ }
+
+ // then also change the style as needed
+ long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
+ if ( !(style & BS_DEFPUSHBUTTON) == on )
+ {
+ // don't do it with the owner drawn buttons because it will
+ // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
+ // BS_DEFPUSHBUTTON != 0)!
+ if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
+ {
+ ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
+ on ? style | BS_DEFPUSHBUTTON
+ : style & ~BS_DEFPUSHBUTTON,
+ 1L /* redraw */);
+ }
+ else // owner drawn
+ {
+ // redraw the button - it will notice itself that it's
+ // [not] the default one [any longer]
+ btn->Refresh();
+ }
+ }
+ //else: already has correct style
+}
+
+// ----------------------------------------------------------------------------
+// helpers
+// ----------------------------------------------------------------------------
+
+bool wxButton::SendClickEvent()
+{
+ wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
+ event.SetEventObject(this);
+
+ return ProcessCommand(event);
+}
+
+void wxButton::Command(wxCommandEvent & event)
+{
+ ProcessCommand(event);
+}
+
+// ----------------------------------------------------------------------------
+// event/message handlers
+// ----------------------------------------------------------------------------
+
+bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
+{
+ bool processed = false;
+ switch ( param )
+ {
+ // NOTE: Apparently older versions (NT 4?) of the common controls send
+ // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
+ // buttons, so in order to send two EVT_BUTTON events we should
+ // catch both types. Currently (Feb 2003) up-to-date versions of
+ // win98, win2k and winXP all send two BN_CLICKED messages for
+ // all button types, so we don't catch BN_DOUBLECLICKED anymore
+ // in order to not get 3 EVT_BUTTON events. If this is a problem
+ // then we need to figure out which version of the comctl32 changed
+ // this behaviour and test for it.
+
+ case 1: // message came from an accelerator
+ case BN_CLICKED: // normal buttons send this
+ processed = SendClickEvent();
+ break;
+ }
+
+ return processed;
+}
+
+WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+ // when we receive focus, we want to temporarily become the default button in
+ // our parent panel so that pressing "Enter" would activate us -- and when
+ // losing it we should restore the previous default button as well
+ if ( nMsg == WM_SETFOCUS )
+ {
+ SetTmpDefault();
+
+ // let the default processing take place too
+ }
+ else if ( nMsg == WM_KILLFOCUS )
+ {
+ UnsetTmpDefault();
+ }
+ else if ( nMsg == WM_LBUTTONDBLCLK )
+ {
+ // emulate a click event to force an owner-drawn button to change its
+ // appearance - without this, it won't do it
+ (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam);
+
+ // and continue with processing the message normally as well
+ }
+#if wxUSE_UXTHEME
+ else if ( nMsg == WM_THEMECHANGED )
+ {
+ // need to recalculate the best size here
+ // as the theme size might have changed
+ InvalidateBestSize();
+ }
+#endif // wxUSE_UXTHEME
+ // must use m_mouseInWindow here instead of IsMouseInWindow()
+ // since we need to know the first time the mouse enters the window
+ // and IsMouseInWindow() would return true in this case
+ else if ( (nMsg == WM_MOUSEMOVE && !m_mouseInWindow) ||
+ nMsg == WM_MOUSELEAVE )
+ {
+ if (
+ IsEnabled() &&
+ (
+#if wxUSE_UXTHEME
+ wxUxThemeEngine::GetIfActive() ||
+#endif // wxUSE_UXTHEME
+ m_imageData && m_imageData->GetBitmap(State_Current).IsOk()
+ )
+ )
+ {
+ Refresh();
+ }
+ }
+
+ // let the base class do all real processing
+ return wxControl::MSWWindowProc(nMsg, wParam, lParam);
+}
+
+// ----------------------------------------------------------------------------
+// button images
+// ----------------------------------------------------------------------------
+
+wxBitmap wxButton::DoGetBitmap(State which) const
+{
+ return m_imageData ? m_imageData->GetBitmap(which) : wxBitmap();
+}
+
+void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
+{
+ // allocate the image data when the first bitmap is set
+ if ( !m_imageData )
+ {
+#if wxUSE_UXTHEME
+ // using image list doesn't work correctly if we don't have any label
+ // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
+ // BS_BITMAP style), at least under Windows 2003 so use owner drawn
+ // strategy for bitmap-only buttons
+ if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
+ {
+ m_imageData = new wxXPButtonImageData(this, bitmap);
+ }
+ else
+#endif // wxUSE_UXTHEME
+ {
+ m_imageData = new wxODButtonImageData(this, bitmap);
+ MakeOwnerDrawn();
+ }
+ }
+ else
+ {
+ m_imageData->SetBitmap(bitmap, which);
+ }
+
+ // it should be enough to only invalidate the best size when the normal
+ // bitmap changes as all bitmaps assigned to the button should be of the
+ // same size anyhow
+ if ( which == State_Normal )
+ InvalidateBestSize();
+
+ Refresh();
+}
+
+wxSize wxButton::DoGetBitmapMargins() const
+{
+ return m_imageData ? m_imageData->GetBitmapMargins() : wxSize(0, 0);
+}
+
+void wxButton::DoSetBitmapMargins(wxCoord x, wxCoord y)
+{
+ wxCHECK_RET( m_imageData, "SetBitmap() must be called first" );
+
+ m_imageData->SetBitmapMargins(x, y);
+}
+
+void wxButton::DoSetBitmapPosition(wxDirection dir)
+{
+ wxCHECK_RET( m_imageData, "SetBitmap() must be called first" );
+
+ m_imageData->SetBitmapPosition(dir);
+}
+
+// ----------------------------------------------------------------------------
+// owner-drawn buttons support
+// ----------------------------------------------------------------------------
+
+// drawing helpers
+namespace
+{
+
+// return the button state using both the ODS_XXX flags specified in state
+// parameter and the current button state
+wxButton::State GetButtonState(wxButton *btn, UINT state)
+{
+ if ( state & ODS_DISABLED )
+ return wxButton::State_Disabled;
+
+ if ( state & ODS_SELECTED )
+ return wxButton::State_Pressed;
+
+ if ( btn->HasCapture() || btn->IsMouseInWindow() )
+ return wxButton::State_Current;
+
+ if ( state & ODS_FOCUS )
+ return wxButton::State_Focused;
+
+ return wxButton::State_Normal;
+}
+
+void DrawButtonText(HDC hdc,
+ RECT *pRect,
+ const wxString& text,
+ COLORREF col,
+ int flags)
+{
+ wxTextColoursChanger changeFg(hdc, col, CLR_INVALID);
+ wxBkModeChanger changeBkMode(hdc, wxBRUSHSTYLE_TRANSPARENT);
+
+ // center text horizontally in any case
+ flags |= DT_CENTER;
+
+ if ( text.find(wxT('\n')) != wxString::npos )
+ {
+ // draw multiline label
+
+ // first we need to compute its bounding rect
+ RECT rc;
+ ::CopyRect(&rc, pRect);
+ ::DrawText(hdc, text.wx_str(), 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.wx_str(), text.length(), &rc, flags);
+ }
+ else // single line label
+ {
+ // centre text vertically too (notice that we must have DT_SINGLELINE
+ // for DT_VCENTER to work)
+ ::DrawText(hdc, text.wx_str(), text.length(), pRect,
+ flags | DT_SINGLELINE | DT_VCENTER);
+ }