+#ifndef ODS_NOACCEL
+ #define ODS_NOACCEL 0x0100
+#endif
+
+#ifndef ODS_NOFOCUSRECT
+ #define ODS_NOFOCUSRECT 0x0200
+#endif
+
+#ifndef DT_HIDEPREFIX
+ #define DT_HIDEPREFIX 0x00100000
+#endif
+
+// set the value for BCM_SETSHIELD (for the UAC shield) if it's not defined in
+// the header
+#ifndef BCM_SETSHIELD
+ #define BCM_SETSHIELD 0x160c
+#endif
+
+// ----------------------------------------------------------------------------
+// button image data
+// ----------------------------------------------------------------------------
+
+// we use different data classes for owner drawn buttons and for themed XP ones
+
+class wxButtonImageData
+{
+public:
+ wxButtonImageData() { }
+ virtual ~wxButtonImageData() { }
+
+ virtual wxBitmap GetBitmap(wxButton::State which) const = 0;
+ virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which) = 0;
+
+ virtual wxSize GetBitmapMargins() const = 0;
+ virtual void SetBitmapMargins(wxCoord x, wxCoord y) = 0;
+
+ virtual wxDirection GetBitmapPosition() const = 0;
+ virtual void SetBitmapPosition(wxDirection dir) = 0;
+
+private:
+ wxDECLARE_NO_COPY_CLASS(wxButtonImageData);
+};
+
+namespace
+{
+
+// the gap between button edge and the interior area used by Windows for the
+// standard buttons
+const int OD_BUTTON_MARGIN = 4;
+
+class wxODButtonImageData : public wxButtonImageData
+{
+public:
+ wxODButtonImageData(wxButton *btn, const wxBitmap& bitmap)
+ {
+ SetBitmap(bitmap, wxButton::State_Normal);
+
+ m_dir = wxLEFT;
+
+ // we use margins when we have both bitmap and text, but when we have
+ // only the bitmap it should take up the entire button area
+ if ( btn->ShowsLabel() )
+ {
+ m_margin.x = btn->GetCharWidth();
+ m_margin.y = btn->GetCharHeight() / 2;
+ }
+ }
+
+ virtual wxBitmap GetBitmap(wxButton::State which) const
+ {
+ return m_bitmaps[which];
+ }
+
+ virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which)
+ {
+ m_bitmaps[which] = bitmap;
+ }
+
+ virtual wxSize GetBitmapMargins() const
+ {
+ return m_margin;
+ }
+
+ virtual void SetBitmapMargins(wxCoord x, wxCoord y)
+ {
+ m_margin = wxSize(x, y);
+ }
+
+ virtual wxDirection GetBitmapPosition() const
+ {
+ return m_dir;
+ }
+
+ virtual void SetBitmapPosition(wxDirection dir)
+ {
+ m_dir = dir;
+ }
+
+private:
+ // just store the values passed to us to be able to retrieve them later
+ // from the drawing code
+ wxBitmap m_bitmaps[wxButton::State_Max];
+ wxSize m_margin;
+ wxDirection m_dir;
+
+ wxDECLARE_NO_COPY_CLASS(wxODButtonImageData);
+};
+
+#if wxUSE_UXTHEME
+
+// somehow the margin is one pixel greater than the value returned by
+// GetThemeMargins() call
+const int XP_BUTTON_EXTRA_MARGIN = 1;
+
+class wxXPButtonImageData : public wxButtonImageData
+{
+public:
+ // we must be constructed with the size of our images as we need to create
+ // the image list
+ wxXPButtonImageData(wxButton *btn, const wxBitmap& bitmap)
+ : m_iml(bitmap.GetWidth(), bitmap.GetHeight(), true /* use mask */,
+ wxButton::State_Max),
+ m_hwndBtn(GetHwndOf(btn))
+ {
+ // initialize all bitmaps to normal state
+ for ( int n = 0; n < wxButton::State_Max; n++ )
+ {
+ m_iml.Add(bitmap);
+ }
+
+ m_data.himl = GetHimagelistOf(&m_iml);
+
+ // use default margins
+ m_data.margin.left =
+ m_data.margin.right = btn->GetCharWidth();
+ m_data.margin.top =
+ m_data.margin.bottom = btn->GetCharHeight() / 2;
+
+ // and default alignment
+ m_data.uAlign = BUTTON_IMAGELIST_ALIGN_LEFT;
+
+ UpdateImageInfo();
+ }
+
+ virtual wxBitmap GetBitmap(wxButton::State which) const
+ {
+ return m_iml.GetBitmap(which);
+ }
+
+ virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which)
+ {
+ m_iml.Replace(which, bitmap);
+
+ UpdateImageInfo();
+ }
+
+ virtual wxSize GetBitmapMargins() const
+ {
+ return wxSize(m_data.margin.left, m_data.margin.top);
+ }
+
+ virtual void SetBitmapMargins(wxCoord x, wxCoord y)
+ {
+ RECT& margin = m_data.margin;
+ margin.left =
+ margin.right = x;
+ margin.top =
+ margin.bottom = y;
+
+ if ( !::SendMessage(m_hwndBtn, BCM_SETTEXTMARGIN, 0, (LPARAM)&margin) )
+ {
+ wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
+ }
+ }
+
+ virtual wxDirection GetBitmapPosition() const
+ {
+ switch ( m_data.uAlign )
+ {
+ default:
+ wxFAIL_MSG( "invalid image alignment" );
+ // fall through
+
+ case BUTTON_IMAGELIST_ALIGN_LEFT:
+ return wxLEFT;
+
+ case BUTTON_IMAGELIST_ALIGN_RIGHT:
+ return wxRIGHT;
+
+ case BUTTON_IMAGELIST_ALIGN_TOP:
+ return wxTOP;
+
+ case BUTTON_IMAGELIST_ALIGN_BOTTOM:
+ return wxBOTTOM;
+ }
+ }
+
+ virtual void SetBitmapPosition(wxDirection dir)
+ {
+ UINT alignNew;
+ switch ( dir )
+ {
+ default:
+ wxFAIL_MSG( "invalid direction" );
+ // fall through
+
+ case wxLEFT:
+ alignNew = BUTTON_IMAGELIST_ALIGN_LEFT;
+ break;
+
+ case wxRIGHT:
+ alignNew = BUTTON_IMAGELIST_ALIGN_RIGHT;
+ break;
+
+ case wxTOP:
+ alignNew = BUTTON_IMAGELIST_ALIGN_TOP;
+ break;
+
+ case wxBOTTOM:
+ alignNew = BUTTON_IMAGELIST_ALIGN_BOTTOM;
+ break;
+ }
+
+ if ( alignNew != m_data.uAlign )
+ {
+ m_data.uAlign = alignNew;
+ UpdateImageInfo();
+ }
+ }
+
+private:
+ void UpdateImageInfo()
+ {
+ if ( !::SendMessage(m_hwndBtn, BCM_SETIMAGELIST, 0, (LPARAM)&m_data) )
+ {
+ wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
+ }
+ }
+
+ // we store image list separately to be able to use convenient wxImageList
+ // methods instead of working with raw HIMAGELIST
+ wxImageList m_iml;
+
+ // store the rest of the data in BCM_SETIMAGELIST-friendly form
+ BUTTON_IMAGELIST m_data;
+
+ // the button we're associated with
+ const HWND m_hwndBtn;
+
+
+ wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData);
+};
+
+#endif // wxUSE_UXTHEME
+
+} // anonymous namespace
+
+// ----------------------------------------------------------------------------
+// macros
+// ----------------------------------------------------------------------------
+
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// helper functions from wx/msw/private/button.h
+// ----------------------------------------------------------------------------
+
+void wxMSWButton::UpdateMultilineStyle(HWND hwnd, const wxString& label)
+{
+ // update BS_MULTILINE style depending on the new label (resetting it
+ // doesn't seem to do anything very useful but it shouldn't hurt and we do
+ // have to set it whenever the label becomes multi line as otherwise it
+ // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
+ // the control unless it already has new lines in its label)
+ long styleOld = ::GetWindowLong(hwnd, GWL_STYLE),
+ styleNew;
+ if ( label.find(wxT('\n')) != wxString::npos )
+ styleNew = styleOld | BS_MULTILINE;
+ else
+ styleNew = styleOld & ~BS_MULTILINE;
+
+ if ( styleNew != styleOld )
+ ::SetWindowLong(hwnd, GWL_STYLE, styleNew);
+}
+
+wxSize wxMSWButton::GetFittingSize(wxWindow *win,
+ const wxSize& sizeLabel,
+ int flags)
+{
+ // 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;
+
+ // account for the shield UAC icon if we have it
+ if ( flags & Size_AuthNeeded )
+ sizeBtn.x += wxSystemSettings::GetMetric(wxSYS_SMALLICON_X);
+
+ return sizeBtn;
+}
+
+wxSize wxMSWButton::ComputeBestSize(wxControl *btn, int flags)
+{
+ wxClientDC dc(btn);
+
+ wxSize sizeBtn;
+ dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y);
+
+ sizeBtn = GetFittingSize(btn, sizeBtn, flags);
+
+ // 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) )
+ {
+ // The size of a standard button in the dialog units is 50x14, use it.
+ // Note that we intentionally don't use GetDefaultSize() here, because
+ // it's inexact -- dialog units depend on this dialog's font.
+ wxSize sizeDef = btn->ConvertDialogToPixels(wxSize(50, 14));
+ if ( sizeBtn.x < sizeDef.x )
+ sizeBtn.x = sizeDef.x;
+ if ( sizeBtn.y < sizeDef.y )
+ sizeBtn.y = sizeDef.y;
+ }
+
+ btn->CacheBestSize(sizeBtn);
+
+ return sizeBtn;
+}
+
+// ----------------------------------------------------------------------------
+// creation/destruction
+// ----------------------------------------------------------------------------
+
+bool wxButton::Create(wxWindow *parent,
+ wxWindowID id,
+ const wxString& lbl,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxValidator& validator,
+ const wxString& name)
+{
+ m_authNeeded = false;
+
+ wxString label(lbl);
+ if (label.empty() && wxIsStockID(id))
+ {
+ // On Windows, some buttons aren't supposed to have mnemonics
+ label = wxGetStockLabel
+ (
+ id,
+ id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE
+ ? wxSTOCK_NOFLAGS
+ : wxSTOCK_WITH_MNEMONIC
+ );
+ }
+
+ if ( !CreateControl(parent, id, pos, size, style, validator, name) )
+ return false;
+
+ WXDWORD exstyle;
+ WXDWORD msStyle = MSWGetStyle(style, &exstyle);
+
+ // if the label contains several lines we must explicitly tell the button
+ // about it or it wouldn't draw it correctly ("\n"s would just appear as
+ // black boxes)
+ //
+ // NB: we do it here and not in MSWGetStyle() because we need the label
+ // value and the label is not set yet when MSWGetStyle() is called
+ msStyle |= wxMSWButton::GetMultilineStyle(label);
+
+ return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
+}
+
+wxButton::~wxButton()
+{
+ wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
+ if ( tlw && tlw->GetTmpDefaultItem() == this )
+ {
+ UnsetTmpDefault();
+ }
+
+ delete m_imageData;
+}
+
+// ----------------------------------------------------------------------------
+// flags
+// ----------------------------------------------------------------------------
+
+WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
+{
+ // buttons never have an external border, they draw their own one
+ WXDWORD msStyle = wxControl::MSWGetStyle
+ (
+ (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
+ );
+
+ // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
+ // each other in any resizeable dialog which has more than one button in
+ // the bottom
+ msStyle |= WS_CLIPSIBLINGS;
+
+ // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
+ // and wxBU_RIGHT to get BS_CENTER!
+ if ( style & wxBU_LEFT )
+ msStyle |= BS_LEFT;
+ if ( style & wxBU_RIGHT )
+ msStyle |= BS_RIGHT;
+ if ( style & wxBU_TOP )
+ msStyle |= BS_TOP;
+ if ( style & wxBU_BOTTOM )
+ msStyle |= BS_BOTTOM;
+#ifndef __WXWINCE__
+ // flat 2d buttons
+ if ( style & wxNO_BORDER )
+ msStyle |= BS_FLAT;
+#endif // __WXWINCE__
+
+ return msStyle;
+}
+
+void wxButton::SetLabel(const wxString& label)
+{
+ wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
+
+ wxButtonBase::SetLabel(label);
+}
+
+// ----------------------------------------------------------------------------
+// size management including autosizing
+// ----------------------------------------------------------------------------
+
+void wxButton::AdjustForBitmapSize(wxSize &size) const
+{
+ if ( !m_imageData )
+ return;
+
+ // 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);
+ }
+}
+
+wxSize wxButton::DoGetBestSize() const
+{
+ wxSize size;
+
+ // account for the text part if we have it or if we don't have any image at
+ // all (buttons initially created with empty label should still have a non
+ // zero size)
+ if ( ShowsLabel() || !m_imageData )
+ {
+ int flags = 0;
+ if ( GetAuthNeeded() )
+ flags |= wxMSWButton::Size_AuthNeeded;
+
+ size = wxMSWButton::ComputeBestSize(const_cast<wxButton *>(this), flags);
+ }
+
+ if ( m_imageData )
+ {
+ AdjustForBitmapSize(size);
+
+ 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));