1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/anybutton.cpp
3 // Purpose: wxAnyButton
4 // Author: Julian Smart
5 // Created: 1998-01-04 (extracted from button.cpp)
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #ifdef wxHAS_ANY_BUTTON
28 #include "wx/anybutton.h"
34 #include "wx/bmpbuttn.h"
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
37 #include "wx/dcclient.h"
38 #include "wx/toplevel.h"
39 #include "wx/msw/wrapcctl.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/missing.h"
44 #include "wx/imaglist.h"
45 #include "wx/stockitem.h"
46 #include "wx/msw/private/button.h"
47 #include "wx/msw/private/dc.h"
48 #include "wx/private/window.h"
51 #include "wx/generic/private/markuptext.h"
52 #endif // wxUSE_MARKUP
54 using namespace wxMSWImpl
;
57 #include "wx/msw/uxtheme.h"
59 // no need to include tmschema.h
61 #define BP_PUSHBUTTON 1
66 #define PBS_DISABLED 4
67 #define PBS_DEFAULTED 5
69 #define TMT_CONTENTMARGINS 3602
72 // provide the necessary declarations ourselves if they're missing from
74 #ifndef BCM_SETIMAGELIST
75 #define BCM_SETIMAGELIST 0x1602
76 #define BCM_SETTEXTMARGIN 0x1604
80 BUTTON_IMAGELIST_ALIGN_LEFT
,
81 BUTTON_IMAGELIST_ALIGN_RIGHT
,
82 BUTTON_IMAGELIST_ALIGN_TOP
,
83 BUTTON_IMAGELIST_ALIGN_BOTTOM
86 struct BUTTON_IMAGELIST
93 #endif // wxUSE_UXTHEME
95 #ifndef WM_THEMECHANGED
96 #define WM_THEMECHANGED 0x031A
100 #define ODS_NOACCEL 0x0100
103 #ifndef ODS_NOFOCUSRECT
104 #define ODS_NOFOCUSRECT 0x0200
107 #ifndef DT_HIDEPREFIX
108 #define DT_HIDEPREFIX 0x00100000
112 extern wxWindowMSW
*wxWindowBeingErased
; // From src/msw/window.cpp
113 #endif // wxUSE_UXTHEME
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 // we use different data classes for owner drawn buttons and for themed XP ones
121 class wxButtonImageData
124 wxButtonImageData() { }
125 virtual ~wxButtonImageData() { }
127 virtual wxBitmap
GetBitmap(wxAnyButton::State which
) const = 0;
128 virtual void SetBitmap(const wxBitmap
& bitmap
, wxAnyButton::State which
) = 0;
130 virtual wxSize
GetBitmapMargins() const = 0;
131 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
) = 0;
133 virtual wxDirection
GetBitmapPosition() const = 0;
134 virtual void SetBitmapPosition(wxDirection dir
) = 0;
137 wxDECLARE_NO_COPY_CLASS(wxButtonImageData
);
143 // the gap between button edge and the interior area used by Windows for the
145 const int OD_BUTTON_MARGIN
= 4;
147 class wxODButtonImageData
: public wxButtonImageData
150 wxODButtonImageData(wxAnyButton
*btn
, const wxBitmap
& bitmap
)
152 SetBitmap(bitmap
, wxAnyButton::State_Normal
);
154 SetBitmap(bitmap
.ConvertToDisabled(), wxAnyButton::State_Disabled
);
158 // we use margins when we have both bitmap and text, but when we have
159 // only the bitmap it should take up the entire button area
160 if ( btn
->ShowsLabel() )
162 m_margin
.x
= btn
->GetCharWidth();
163 m_margin
.y
= btn
->GetCharHeight() / 2;
167 virtual wxBitmap
GetBitmap(wxAnyButton::State which
) const
169 return m_bitmaps
[which
];
172 virtual void SetBitmap(const wxBitmap
& bitmap
, wxAnyButton::State which
)
174 m_bitmaps
[which
] = bitmap
;
177 virtual wxSize
GetBitmapMargins() const
182 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
184 m_margin
= wxSize(x
, y
);
187 virtual wxDirection
GetBitmapPosition() const
192 virtual void SetBitmapPosition(wxDirection dir
)
198 // just store the values passed to us to be able to retrieve them later
199 // from the drawing code
200 wxBitmap m_bitmaps
[wxAnyButton::State_Max
];
204 wxDECLARE_NO_COPY_CLASS(wxODButtonImageData
);
209 // somehow the margin is one pixel greater than the value returned by
210 // GetThemeMargins() call
211 const int XP_BUTTON_EXTRA_MARGIN
= 1;
213 class wxXPButtonImageData
: public wxButtonImageData
216 // we must be constructed with the size of our images as we need to create
218 wxXPButtonImageData(wxAnyButton
*btn
, const wxBitmap
& bitmap
)
219 : m_iml(bitmap
.GetWidth(), bitmap
.GetHeight(), true /* use mask */,
220 wxAnyButton::State_Max
),
221 m_hwndBtn(GetHwndOf(btn
))
223 // initialize all bitmaps except for the disabled one to normal state
224 for ( int n
= 0; n
< wxAnyButton::State_Max
; n
++ )
227 m_iml
.Add(n
== wxAnyButton::State_Disabled
? bitmap
.ConvertToDisabled()
234 m_data
.himl
= GetHimagelistOf(&m_iml
);
236 // no margins by default
238 m_data
.margin
.right
=
240 m_data
.margin
.bottom
= 0;
242 // use default alignment
243 m_data
.uAlign
= BUTTON_IMAGELIST_ALIGN_LEFT
;
248 virtual wxBitmap
GetBitmap(wxAnyButton::State which
) const
250 return m_iml
.GetBitmap(which
);
253 virtual void SetBitmap(const wxBitmap
& bitmap
, wxAnyButton::State which
)
255 m_iml
.Replace(which
, bitmap
);
260 virtual wxSize
GetBitmapMargins() const
262 return wxSize(m_data
.margin
.left
, m_data
.margin
.top
);
265 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
267 RECT
& margin
= m_data
.margin
;
273 if ( !::SendMessage(m_hwndBtn
, BCM_SETTEXTMARGIN
, 0, (LPARAM
)&margin
) )
275 wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
279 virtual wxDirection
GetBitmapPosition() const
281 switch ( m_data
.uAlign
)
284 wxFAIL_MSG( "invalid image alignment" );
287 case BUTTON_IMAGELIST_ALIGN_LEFT
:
290 case BUTTON_IMAGELIST_ALIGN_RIGHT
:
293 case BUTTON_IMAGELIST_ALIGN_TOP
:
296 case BUTTON_IMAGELIST_ALIGN_BOTTOM
:
301 virtual void SetBitmapPosition(wxDirection dir
)
307 wxFAIL_MSG( "invalid direction" );
311 alignNew
= BUTTON_IMAGELIST_ALIGN_LEFT
;
315 alignNew
= BUTTON_IMAGELIST_ALIGN_RIGHT
;
319 alignNew
= BUTTON_IMAGELIST_ALIGN_TOP
;
323 alignNew
= BUTTON_IMAGELIST_ALIGN_BOTTOM
;
327 if ( alignNew
!= m_data
.uAlign
)
329 m_data
.uAlign
= alignNew
;
335 void UpdateImageInfo()
337 if ( !::SendMessage(m_hwndBtn
, BCM_SETIMAGELIST
, 0, (LPARAM
)&m_data
) )
339 wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
343 // we store image list separately to be able to use convenient wxImageList
344 // methods instead of working with raw HIMAGELIST
347 // store the rest of the data in BCM_SETIMAGELIST-friendly form
348 BUTTON_IMAGELIST m_data
;
350 // the button we're associated with
351 const HWND m_hwndBtn
;
354 wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData
);
357 #endif // wxUSE_UXTHEME
359 } // anonymous namespace
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 // ============================================================================
367 // ============================================================================
369 // ----------------------------------------------------------------------------
370 // helper functions from wx/msw/private/button.h
371 // ----------------------------------------------------------------------------
373 void wxMSWButton::UpdateMultilineStyle(HWND hwnd
, const wxString
& label
)
375 // update BS_MULTILINE style depending on the new label (resetting it
376 // doesn't seem to do anything very useful but it shouldn't hurt and we do
377 // have to set it whenever the label becomes multi line as otherwise it
378 // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
379 // the control unless it already has new lines in its label)
380 long styleOld
= ::GetWindowLong(hwnd
, GWL_STYLE
),
382 if ( label
.find(wxT('\n')) != wxString::npos
)
383 styleNew
= styleOld
| BS_MULTILINE
;
385 styleNew
= styleOld
& ~BS_MULTILINE
;
387 if ( styleNew
!= styleOld
)
388 ::SetWindowLong(hwnd
, GWL_STYLE
, styleNew
);
391 wxSize
wxMSWButton::GetFittingSize(wxWindow
*win
,
392 const wxSize
& sizeLabel
,
395 wxSize sizeBtn
= sizeLabel
;
397 // FIXME: The numbers here are pure guesswork, no idea how should the
398 // button margins be really calculated.
399 if ( flags
& Size_ExactFit
)
401 // We still need some margin or the text would be overwritten, just
402 // make it as small as possible.
403 sizeBtn
.x
+= (3*win
->GetCharWidth());
407 sizeBtn
.x
+= 3*win
->GetCharWidth();
408 sizeBtn
.y
+= win
->GetCharHeight()/2;
411 // account for the shield UAC icon if we have it
412 if ( flags
& Size_AuthNeeded
)
413 sizeBtn
.x
+= wxSystemSettings::GetMetric(wxSYS_SMALLICON_X
);
418 wxSize
wxMSWButton::ComputeBestFittingSize(wxControl
*btn
, int flags
)
423 dc
.GetMultiLineTextExtent(btn
->GetLabelText(), &sizeBtn
.x
, &sizeBtn
.y
);
425 return GetFittingSize(btn
, sizeBtn
, flags
);
428 wxSize
wxMSWButton::IncreaseToStdSizeAndCache(wxControl
*btn
, const wxSize
& size
)
430 wxSize
sizeBtn(size
);
432 // The 50x14 button size is documented in the "Recommended sizing and
433 // spacing" section of MSDN layout article.
435 // Note that we intentionally don't use GetDefaultSize() here, because
436 // it's inexact -- dialog units depend on this dialog's font.
437 const wxSize sizeDef
= btn
->ConvertDialogToPixels(wxSize(50, 14));
439 // All buttons should have at least the standard size, unless the user
440 // explicitly wants them to be as small as possible and used wxBU_EXACTFIT
441 // style to indicate this.
442 const bool incToStdSize
= !btn
->HasFlag(wxBU_EXACTFIT
);
445 if ( sizeBtn
.x
< sizeDef
.x
)
446 sizeBtn
.x
= sizeDef
.x
;
449 // Notice that we really want to make all buttons with text label equally
450 // high, otherwise they look ugly and the existing code using wxBU_EXACTFIT
451 // only uses it to control width and not height.
452 if ( incToStdSize
|| !btn
->GetLabel().empty() )
454 if ( sizeBtn
.y
< sizeDef
.y
)
455 sizeBtn
.y
= sizeDef
.y
;
458 btn
->CacheBestSize(sizeBtn
);
463 // ----------------------------------------------------------------------------
464 // creation/destruction
465 // ----------------------------------------------------------------------------
467 wxAnyButton::~wxAnyButton()
472 #endif // wxUSE_MARKUP
475 void wxAnyButton::SetLabel(const wxString
& label
)
477 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
479 wxAnyButtonBase::SetLabel(label
);
482 // If we have a plain text label, we shouldn't be using markup any longer.
488 // Unfortunately we don't really know whether we can reset the button
489 // to be non-owner-drawn or not: if we had made it owner-drawn just
490 // because of a call to SetLabelMarkup(), we could, but not if there
491 // were [also] calls to Set{Fore,Back}groundColour(). If it's really a
492 // problem to have button remain owner-drawn forever just because it
493 // had markup label once, we should record the reason for our current
494 // owner-drawnness and check it here.
496 #endif // wxUSE_MARKUP
499 // ----------------------------------------------------------------------------
500 // size management including autosizing
501 // ----------------------------------------------------------------------------
503 void wxAnyButton::AdjustForBitmapSize(wxSize
&size
) const
505 wxCHECK_RET( m_imageData
, wxT("shouldn't be called if no image") );
507 // account for the bitmap size
508 const wxSize sizeBmp
= m_imageData
->GetBitmap(State_Normal
).GetSize();
509 const wxDirection dirBmp
= m_imageData
->GetBitmapPosition();
510 if ( dirBmp
== wxLEFT
|| dirBmp
== wxRIGHT
)
513 if ( sizeBmp
.y
> size
.y
)
516 else // bitmap on top/below the text
519 if ( sizeBmp
.x
> size
.x
)
523 // account for the user-specified margins
524 size
+= 2*m_imageData
->GetBitmapMargins();
526 // and also for the margins we always add internally (unless we have no
527 // border at all in which case the button has exactly the same size as
528 // bitmap and so no margins should be used)
529 if ( !HasFlag(wxBORDER_NONE
) )
534 if ( wxUxThemeEngine::GetIfActive() )
536 wxUxThemeHandle
theme(const_cast<wxAnyButton
*>(this), L
"BUTTON");
539 wxUxThemeEngine::Get()->GetThemeMargins(theme
, NULL
,
546 // XP doesn't draw themed buttons correctly when the client
547 // area is smaller than 8x8 - enforce this minimum size for
549 size
.IncTo(wxSize(8, 8));
551 marginH
= margins
.cxLeftWidth
+ margins
.cxRightWidth
552 + 2*XP_BUTTON_EXTRA_MARGIN
;
553 marginV
= margins
.cyTopHeight
+ margins
.cyBottomHeight
554 + 2*XP_BUTTON_EXTRA_MARGIN
;
557 #endif // wxUSE_UXTHEME
560 marginV
= OD_BUTTON_MARGIN
;
563 size
.IncBy(marginH
, marginV
);
567 wxSize
wxAnyButton::DoGetBestSize() const
569 wxAnyButton
* const self
= const_cast<wxAnyButton
*>(this);
573 // Account for the text part if we have it.
577 if ( HasFlag(wxBU_EXACTFIT
) )
578 flags
|= wxMSWButton::Size_ExactFit
;
579 if ( DoGetAuthNeeded() )
580 flags
|= wxMSWButton::Size_AuthNeeded
;
586 size
= wxMSWButton::GetFittingSize(self
,
587 m_markupText
->Measure(dc
),
590 else // Normal plain text (but possibly multiline) label.
591 #endif // wxUSE_MARKUP
593 size
= wxMSWButton::ComputeBestFittingSize(self
, flags
);
598 AdjustForBitmapSize(size
);
600 return wxMSWButton::IncreaseToStdSizeAndCache(self
, size
);
603 // ----------------------------------------------------------------------------
604 // event/message handlers
605 // ----------------------------------------------------------------------------
607 WXLRESULT
wxAnyButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
609 if ( nMsg
== WM_LBUTTONDBLCLK
)
611 // emulate a click event to force an owner-drawn button to change its
612 // appearance - without this, it won't do it
613 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
615 // and continue with processing the message normally as well
618 else if ( nMsg
== WM_THEMECHANGED
)
620 // need to recalculate the best size here
621 // as the theme size might have changed
622 InvalidateBestSize();
624 #endif // wxUSE_UXTHEME
625 // must use m_mouseInWindow here instead of IsMouseInWindow()
626 // since we need to know the first time the mouse enters the window
627 // and IsMouseInWindow() would return true in this case
628 else if ( (nMsg
== WM_MOUSEMOVE
&& !m_mouseInWindow
) ||
629 nMsg
== WM_MOUSELEAVE
)
635 wxUxThemeEngine::GetIfActive() ||
636 #endif // wxUSE_UXTHEME
637 (m_imageData
&& m_imageData
->GetBitmap(State_Current
).IsOk())
645 // let the base class do all real processing
646 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
649 // ----------------------------------------------------------------------------
651 // ----------------------------------------------------------------------------
653 wxBitmap
wxAnyButton::DoGetBitmap(State which
) const
655 return m_imageData
? m_imageData
->GetBitmap(which
) : wxBitmap();
658 void wxAnyButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
661 wxXPButtonImageData
*oldData
= NULL
;
662 #endif // wxUSE_UXTHEME
664 // Check if we already had bitmaps of different size.
666 bitmap
.GetSize() != m_imageData
->GetBitmap(State_Normal
).GetSize() )
668 wxASSERT_MSG( (which
== State_Normal
) || bitmap
.IsNull(),
669 "Must set normal bitmap with the new size first" );
672 if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
674 // We can't change the size of the images stored in wxImageList
675 // in wxXPButtonImageData::m_iml so force recreating it below but
676 // keep the current data to copy its values into the new one.
677 oldData
= static_cast<wxXPButtonImageData
*>(m_imageData
);
680 #endif // wxUSE_UXTHEME
681 //else: wxODButtonImageData doesn't require anything special
684 // allocate the image data when the first bitmap is set
688 // using image list doesn't work correctly if we don't have any label
689 // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
690 // BS_BITMAP style), at least under Windows 2003 so use owner drawn
691 // strategy for bitmap-only buttons
692 if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
694 m_imageData
= new wxXPButtonImageData(this, bitmap
);
698 // Preserve the old values in case the user changed them.
699 m_imageData
->SetBitmapPosition(oldData
->GetBitmapPosition());
701 const wxSize oldMargins
= oldData
->GetBitmapMargins();
702 m_imageData
->SetBitmapMargins(oldMargins
.x
, oldMargins
.y
);
704 // No need to preserve the bitmaps though as they were of wrong
711 #endif // wxUSE_UXTHEME
713 m_imageData
= new wxODButtonImageData(this, bitmap
);
719 m_imageData
->SetBitmap(bitmap
, which
);
722 // it should be enough to only invalidate the best size when the normal
723 // bitmap changes as all bitmaps assigned to the button should be of the
725 if ( which
== State_Normal
)
726 InvalidateBestSize();
731 wxSize
wxAnyButton::DoGetBitmapMargins() const
733 return m_imageData
? m_imageData
->GetBitmapMargins() : wxSize(0, 0);
736 void wxAnyButton::DoSetBitmapMargins(wxCoord x
, wxCoord y
)
738 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
740 m_imageData
->SetBitmapMargins(x
, y
);
741 InvalidateBestSize();
744 void wxAnyButton::DoSetBitmapPosition(wxDirection dir
)
746 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
748 m_imageData
->SetBitmapPosition(dir
);
749 InvalidateBestSize();
752 // ----------------------------------------------------------------------------
754 // ----------------------------------------------------------------------------
758 bool wxAnyButton::DoSetLabelMarkup(const wxString
& markup
)
760 if ( !wxAnyButtonBase::DoSetLabelMarkup(markup
) )
765 m_markupText
= new wxMarkupText(markup
);
770 // We are already owner-drawn so just update the text.
771 m_markupText
->SetMarkup(markup
);
779 #endif // wxUSE_MARKUP
781 // ----------------------------------------------------------------------------
782 // owner-drawn buttons support
783 // ----------------------------------------------------------------------------
789 // return the button state using both the ODS_XXX flags specified in state
790 // parameter and the current button state
791 wxAnyButton::State
GetButtonState(wxAnyButton
*btn
, UINT state
)
793 if ( state
& ODS_DISABLED
)
794 return wxAnyButton::State_Disabled
;
796 if ( state
& ODS_SELECTED
)
797 return wxAnyButton::State_Pressed
;
799 if ( btn
->HasCapture() || btn
->IsMouseInWindow() )
800 return wxAnyButton::State_Current
;
802 if ( state
& ODS_FOCUS
)
803 return wxAnyButton::State_Focused
;
805 return btn
->GetNormalState();
808 void DrawButtonText(HDC hdc
,
813 const wxString text
= btn
->GetLabel();
815 if ( text
.find(wxT('\n')) != wxString::npos
)
817 // draw multiline label
819 // center text horizontally in any case
822 // first we need to compute its bounding rect
824 ::CopyRect(&rc
, pRect
);
825 ::DrawText(hdc
, text
.t_str(), text
.length(), &rc
,
826 DT_CENTER
| DT_CALCRECT
);
828 // now center this rect inside the entire button area
829 const LONG w
= rc
.right
- rc
.left
;
830 const LONG h
= rc
.bottom
- rc
.top
;
831 rc
.left
= pRect
->left
+ (pRect
->right
- pRect
->left
)/2 - w
/2;
832 rc
.right
= rc
.left
+w
;
833 rc
.top
= pRect
->top
+ (pRect
->bottom
- pRect
->top
)/2 - h
/2;
834 rc
.bottom
= rc
.top
+h
;
836 ::DrawText(hdc
, text
.t_str(), text
.length(), &rc
, flags
);
838 else // single line label
840 // translate wx button flags to alignment flags for DrawText()
841 if ( btn
->HasFlag(wxBU_RIGHT
) )
845 else if ( !btn
->HasFlag(wxBU_LEFT
) )
849 //else: DT_LEFT is the default anyhow (and its value is 0 too)
851 if ( btn
->HasFlag(wxBU_BOTTOM
) )
855 else if ( !btn
->HasFlag(wxBU_TOP
) )
859 //else: as above, DT_TOP is the default
861 // notice that we must have DT_SINGLELINE for vertical alignment flags
863 ::DrawText(hdc
, text
.t_str(), text
.length(), pRect
,
864 flags
| DT_SINGLELINE
);
868 void DrawRect(HDC hdc
, const RECT
& r
)
870 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
871 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
872 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
873 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
877 The button frame looks like this normally:
880 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
881 WH GB H = light grey (LIGHT)
882 WH GB G = dark grey (SHADOW)
883 WH GB B = black (DKSHADOW)
888 When the button is selected, the button becomes like this (the total button
889 size doesn't change):
900 When the button is pushed (while selected) it is like:
911 void DrawButtonFrame(HDC hdc
, RECT
& rectBtn
,
912 bool selected
, bool pushed
)
915 CopyRect(&r
, &rectBtn
);
917 AutoHPEN
hpenBlack(GetSysColor(COLOR_3DDKSHADOW
)),
918 hpenGrey(GetSysColor(COLOR_3DSHADOW
)),
919 hpenLightGr(GetSysColor(COLOR_3DLIGHT
)),
920 hpenWhite(GetSysColor(COLOR_3DHILIGHT
));
922 SelectInHDC
selectPen(hdc
, hpenBlack
);
931 (void)SelectObject(hdc
, hpenGrey
);
932 ::InflateRect(&r
, -1, -1);
942 ::InflateRect(&r
, -1, -1);
945 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
946 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
948 (void)SelectObject(hdc
, hpenWhite
);
949 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
950 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
952 (void)SelectObject(hdc
, hpenLightGr
);
953 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
954 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
956 (void)SelectObject(hdc
, hpenGrey
);
957 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
958 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
961 InflateRect(&rectBtn
, -OD_BUTTON_MARGIN
, -OD_BUTTON_MARGIN
);
965 void DrawXPBackground(wxAnyButton
*button
, HDC hdc
, RECT
& rectBtn
, UINT state
)
967 wxUxThemeHandle
theme(button
, L
"BUTTON");
969 // this array is indexed by wxAnyButton::State values and so must be kept in
971 static const int uxStates
[] =
973 PBS_NORMAL
, PBS_HOT
, PBS_PRESSED
, PBS_DISABLED
, PBS_DEFAULTED
976 int iState
= uxStates
[GetButtonState(button
, state
)];
978 wxUxThemeEngine
* const engine
= wxUxThemeEngine::Get();
980 // draw parent background if needed
981 if ( engine
->IsThemeBackgroundPartiallyTransparent
988 // Set this button as the one whose background is being erased: this
989 // allows our WM_ERASEBKGND handler used by DrawThemeParentBackground()
990 // to correctly align the background brush with this window instead of
991 // the parent window to which WM_ERASEBKGND is sent. Notice that this
992 // doesn't work with custom user-defined EVT_ERASE_BACKGROUND handlers
993 // as they won't be aligned but unfortunately all the attempts to fix
994 // it by shifting DC origin before calling DrawThemeParentBackground()
995 // failed to work so we at least do this, even though this is far from
996 // being the perfect solution.
997 wxWindowBeingErased
= button
;
999 engine
->DrawThemeParentBackground(GetHwndOf(button
), hdc
, &rectBtn
);
1001 wxWindowBeingErased
= NULL
;
1005 engine
->DrawThemeBackground(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1008 // calculate content area margins
1010 engine
->GetThemeMargins(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1011 TMT_CONTENTMARGINS
, &rectBtn
, &margins
);
1012 ::InflateRect(&rectBtn
, -margins
.cxLeftWidth
, -margins
.cyTopHeight
);
1013 ::InflateRect(&rectBtn
, -XP_BUTTON_EXTRA_MARGIN
, -XP_BUTTON_EXTRA_MARGIN
);
1015 if ( button
->UseBgCol() )
1017 COLORREF colBg
= wxColourToRGB(button
->GetBackgroundColour());
1018 AutoHBRUSH
hbrushBackground(colBg
);
1020 // don't overwrite the focus rect
1022 ::CopyRect(&rectClient
, &rectBtn
);
1023 ::InflateRect(&rectClient
, -1, -1);
1024 FillRect(hdc
, &rectClient
, hbrushBackground
);
1027 #endif // wxUSE_UXTHEME
1029 } // anonymous namespace
1031 // ----------------------------------------------------------------------------
1032 // owner drawn buttons support
1033 // ----------------------------------------------------------------------------
1035 void wxAnyButton::MakeOwnerDrawn()
1037 if ( !IsOwnerDrawn() )
1040 // note that BS_OWNERDRAW is not independent from other style bits
1041 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
1042 style
&= ~(BS_3STATE
| BS_AUTO3STATE
| BS_AUTOCHECKBOX
| BS_AUTORADIOBUTTON
| BS_CHECKBOX
| BS_DEFPUSHBUTTON
| BS_GROUPBOX
| BS_PUSHBUTTON
| BS_RADIOBUTTON
| BS_PUSHLIKE
);
1043 style
|= BS_OWNERDRAW
;
1044 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
1048 bool wxAnyButton::IsOwnerDrawn() const
1050 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
1051 return ( (style
& BS_OWNERDRAW
) == BS_OWNERDRAW
);
1054 bool wxAnyButton::SetBackgroundColour(const wxColour
&colour
)
1056 if ( !wxControl::SetBackgroundColour(colour
) )
1069 bool wxAnyButton::SetForegroundColour(const wxColour
&colour
)
1071 if ( !wxControl::SetForegroundColour(colour
) )
1084 bool wxAnyButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
1086 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
1087 HDC hdc
= lpDIS
->hDC
;
1089 UINT state
= lpDIS
->itemState
;
1090 switch ( GetButtonState(this, state
) )
1092 case State_Disabled
:
1093 state
|= ODS_DISABLED
;
1096 state
|= ODS_SELECTED
;
1105 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
1108 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
1110 // draw the button background
1111 if ( !HasFlag(wxBORDER_NONE
) )
1114 if ( wxUxThemeEngine::GetIfActive() )
1116 DrawXPBackground(this, hdc
, rectBtn
, state
);
1119 #endif // wxUSE_UXTHEME
1121 COLORREF colBg
= wxColourToRGB(GetBackgroundColour());
1123 // first, draw the background
1124 AutoHBRUSH
hbrushBackground(colBg
);
1125 FillRect(hdc
, &rectBtn
, hbrushBackground
);
1127 // draw the border for the current state
1128 bool selected
= (state
& ODS_SELECTED
) != 0;
1132 tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
1135 selected
= tlw
->GetDefaultItem() == this;
1139 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
1142 // draw the focus rectangle if we need it
1143 if ( (state
& ODS_FOCUS
) && !(state
& ODS_NOFOCUSRECT
) )
1145 DrawFocusRect(hdc
, &rectBtn
);
1148 if ( !wxUxThemeEngine::GetIfActive() )
1149 #endif // wxUSE_UXTHEME
1153 // the label is shifted by 1 pixel to create "pushed" effect
1154 OffsetRect(&rectBtn
, 1, 1);
1161 // draw the image, if any
1164 wxBitmap bmp
= m_imageData
->GetBitmap(GetButtonState(this, state
));
1166 bmp
= m_imageData
->GetBitmap(State_Normal
);
1168 const wxSize sizeBmp
= bmp
.GetSize();
1169 const wxSize margin
= m_imageData
->GetBitmapMargins();
1170 const wxSize
sizeBmpWithMargins(sizeBmp
+ 2*margin
);
1171 wxRect
rectButton(wxRectFromRECT(rectBtn
));
1173 // for simplicity, we start with centred rectangle and then move it to
1174 // the appropriate edge
1175 wxRect rectBitmap
= wxRect(sizeBmp
).CentreIn(rectButton
);
1177 // move bitmap only if we have a label, otherwise keep it centered
1180 switch ( m_imageData
->GetBitmapPosition() )
1183 wxFAIL_MSG( "invalid direction" );
1187 rectBitmap
.x
= rectButton
.x
+ margin
.x
;
1188 rectButton
.x
+= sizeBmpWithMargins
.x
;
1189 rectButton
.width
-= sizeBmpWithMargins
.x
;
1193 rectBitmap
.x
= rectButton
.GetRight() - sizeBmp
.x
- margin
.x
;
1194 rectButton
.width
-= sizeBmpWithMargins
.x
;
1198 rectBitmap
.y
= rectButton
.y
+ margin
.y
;
1199 rectButton
.y
+= sizeBmpWithMargins
.y
;
1200 rectButton
.height
-= sizeBmpWithMargins
.y
;
1204 rectBitmap
.y
= rectButton
.GetBottom() - sizeBmp
.y
- margin
.y
;
1205 rectButton
.height
-= sizeBmpWithMargins
.y
;
1210 wxDCTemp
dst((WXHDC
)hdc
);
1211 dst
.DrawBitmap(bmp
, rectBitmap
.GetPosition(), true);
1213 wxCopyRectToRECT(rectButton
, rectBtn
);
1217 // finally draw the label
1220 COLORREF colFg
= state
& ODS_DISABLED
1221 ? ::GetSysColor(COLOR_GRAYTEXT
)
1222 : wxColourToRGB(GetForegroundColour());
1224 wxTextColoursChanger
changeFg(hdc
, colFg
, CLR_INVALID
);
1225 wxBkModeChanger
changeBkMode(hdc
, wxBRUSHSTYLE_TRANSPARENT
);
1230 wxDCTemp
dc((WXHDC
)hdc
);
1231 dc
.SetTextForeground(wxColour(colFg
));
1232 dc
.SetFont(GetFont());
1234 m_markupText
->Render(dc
, wxRectFromRECT(rectBtn
),
1236 ? wxMarkupText::Render_Default
1237 : wxMarkupText::Render_ShowAccels
);
1239 else // Plain text label
1240 #endif // wxUSE_MARKUP
1242 // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000)
1243 // systems but by happy coincidence ODS_NOACCEL is not used under
1244 // them neither so DT_HIDEPREFIX should never be used there
1245 DrawButtonText(hdc
, &rectBtn
, this,
1246 state
& ODS_NOACCEL
? DT_HIDEPREFIX
: 0);
1253 #endif // wxHAS_ANY_BUTTON