1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/anybutton.cpp
3 // Purpose: wxAnyButton
4 // Author: Julian Smart
5 // Created: 1998-01-04 (extracted from button.cpp)
6 // Copyright: (c) Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #ifdef wxHAS_ANY_BUTTON
27 #include "wx/anybutton.h"
33 #include "wx/bmpbuttn.h"
34 #include "wx/settings.h"
35 #include "wx/dcscreen.h"
36 #include "wx/dcclient.h"
37 #include "wx/toplevel.h"
38 #include "wx/msw/wrapcctl.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/missing.h"
43 #include "wx/imaglist.h"
44 #include "wx/stockitem.h"
45 #include "wx/msw/private/button.h"
46 #include "wx/msw/private/dc.h"
47 #include "wx/private/window.h"
50 #include "wx/generic/private/markuptext.h"
51 #endif // wxUSE_MARKUP
53 using namespace wxMSWImpl
;
56 #include "wx/msw/uxtheme.h"
58 // no need to include tmschema.h
60 #define BP_PUSHBUTTON 1
65 #define PBS_DISABLED 4
66 #define PBS_DEFAULTED 5
68 #define TMT_CONTENTMARGINS 3602
71 // provide the necessary declarations ourselves if they're missing from
73 #ifndef BCM_SETIMAGELIST
74 #define BCM_SETIMAGELIST 0x1602
75 #define BCM_SETTEXTMARGIN 0x1604
79 BUTTON_IMAGELIST_ALIGN_LEFT
,
80 BUTTON_IMAGELIST_ALIGN_RIGHT
,
81 BUTTON_IMAGELIST_ALIGN_TOP
,
82 BUTTON_IMAGELIST_ALIGN_BOTTOM
85 struct BUTTON_IMAGELIST
92 #endif // wxUSE_UXTHEME
94 #ifndef WM_THEMECHANGED
95 #define WM_THEMECHANGED 0x031A
99 #define ODS_NOACCEL 0x0100
102 #ifndef ODS_NOFOCUSRECT
103 #define ODS_NOFOCUSRECT 0x0200
106 #ifndef DT_HIDEPREFIX
107 #define DT_HIDEPREFIX 0x00100000
111 extern wxWindowMSW
*wxWindowBeingErased
; // From src/msw/window.cpp
112 #endif // wxUSE_UXTHEME
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // we use different data classes for owner drawn buttons and for themed XP ones
120 class wxButtonImageData
123 wxButtonImageData() { }
124 virtual ~wxButtonImageData() { }
126 virtual wxBitmap
GetBitmap(wxAnyButton::State which
) const = 0;
127 virtual void SetBitmap(const wxBitmap
& bitmap
, wxAnyButton::State which
) = 0;
129 virtual wxSize
GetBitmapMargins() const = 0;
130 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
) = 0;
132 virtual wxDirection
GetBitmapPosition() const = 0;
133 virtual void SetBitmapPosition(wxDirection dir
) = 0;
136 wxDECLARE_NO_COPY_CLASS(wxButtonImageData
);
142 // the gap between button edge and the interior area used by Windows for the
144 const int OD_BUTTON_MARGIN
= 4;
146 class wxODButtonImageData
: public wxButtonImageData
149 wxODButtonImageData(wxAnyButton
*btn
, const wxBitmap
& bitmap
)
151 SetBitmap(bitmap
, wxAnyButton::State_Normal
);
153 SetBitmap(bitmap
.ConvertToDisabled(), wxAnyButton::State_Disabled
);
157 // we use margins when we have both bitmap and text, but when we have
158 // only the bitmap it should take up the entire button area
159 if ( btn
->ShowsLabel() )
161 m_margin
.x
= btn
->GetCharWidth();
162 m_margin
.y
= btn
->GetCharHeight() / 2;
166 virtual wxBitmap
GetBitmap(wxAnyButton::State which
) const
168 return m_bitmaps
[which
];
171 virtual void SetBitmap(const wxBitmap
& bitmap
, wxAnyButton::State which
)
173 m_bitmaps
[which
] = bitmap
;
176 virtual wxSize
GetBitmapMargins() const
181 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
183 m_margin
= wxSize(x
, y
);
186 virtual wxDirection
GetBitmapPosition() const
191 virtual void SetBitmapPosition(wxDirection dir
)
197 // just store the values passed to us to be able to retrieve them later
198 // from the drawing code
199 wxBitmap m_bitmaps
[wxAnyButton::State_Max
];
203 wxDECLARE_NO_COPY_CLASS(wxODButtonImageData
);
208 // somehow the margin is one pixel greater than the value returned by
209 // GetThemeMargins() call
210 const int XP_BUTTON_EXTRA_MARGIN
= 1;
212 class wxXPButtonImageData
: public wxButtonImageData
215 // we must be constructed with the size of our images as we need to create
217 wxXPButtonImageData(wxAnyButton
*btn
, const wxBitmap
& bitmap
)
218 : m_iml(bitmap
.GetWidth(), bitmap
.GetHeight(), true /* use mask */,
219 wxAnyButton::State_Max
+ 1 /* see "pulse" comment below */),
220 m_hwndBtn(GetHwndOf(btn
))
222 // initialize all bitmaps except for the disabled one to normal state
223 for ( int n
= 0; n
< wxAnyButton::State_Max
; n
++ )
226 m_iml
.Add(n
== wxAnyButton::State_Disabled
? bitmap
.ConvertToDisabled()
233 // In addition to the states supported by wxWidgets such as normal,
234 // hot, pressed, disabled and focused, we need to add bitmap for
235 // another state when running under Windows 7 -- the so called "stylus
236 // hot" state corresponding to PBS_STYLUSHOT constant. While it's
237 // documented in MSDN as being only used with tablets, it is a lie as
238 // a focused button actually alternates between the image list elements
239 // with PBS_DEFAULTED and PBS_STYLUSHOT indices and, in particular,
240 // just disappears during half of the time if the latter is not set so
241 // we absolutely must set it.
243 // This also explains why we need to allocate an extra slot in the
244 // image list ctor above, the slot State_Max is used for this one.
247 m_data
.himl
= GetHimagelistOf(&m_iml
);
249 // no margins by default
251 m_data
.margin
.right
=
253 m_data
.margin
.bottom
= 0;
255 // use default alignment
256 m_data
.uAlign
= BUTTON_IMAGELIST_ALIGN_LEFT
;
261 virtual wxBitmap
GetBitmap(wxAnyButton::State which
) const
263 return m_iml
.GetBitmap(which
);
266 virtual void SetBitmap(const wxBitmap
& bitmap
, wxAnyButton::State which
)
268 m_iml
.Replace(which
, bitmap
);
270 // As we want the focused button to always show its bitmap, we need to
271 // update the "stylus hot" one to match it to avoid any pulsing.
272 if ( which
== wxAnyButton::State_Focused
)
273 m_iml
.Replace(wxAnyButton::State_Max
, bitmap
);
278 virtual wxSize
GetBitmapMargins() const
280 return wxSize(m_data
.margin
.left
, m_data
.margin
.top
);
283 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
285 RECT
& margin
= m_data
.margin
;
291 if ( !::SendMessage(m_hwndBtn
, BCM_SETTEXTMARGIN
, 0, (LPARAM
)&margin
) )
293 wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
297 virtual wxDirection
GetBitmapPosition() const
299 switch ( m_data
.uAlign
)
302 wxFAIL_MSG( "invalid image alignment" );
305 case BUTTON_IMAGELIST_ALIGN_LEFT
:
308 case BUTTON_IMAGELIST_ALIGN_RIGHT
:
311 case BUTTON_IMAGELIST_ALIGN_TOP
:
314 case BUTTON_IMAGELIST_ALIGN_BOTTOM
:
319 virtual void SetBitmapPosition(wxDirection dir
)
325 wxFAIL_MSG( "invalid direction" );
329 alignNew
= BUTTON_IMAGELIST_ALIGN_LEFT
;
333 alignNew
= BUTTON_IMAGELIST_ALIGN_RIGHT
;
337 alignNew
= BUTTON_IMAGELIST_ALIGN_TOP
;
341 alignNew
= BUTTON_IMAGELIST_ALIGN_BOTTOM
;
345 if ( alignNew
!= m_data
.uAlign
)
347 m_data
.uAlign
= alignNew
;
353 void UpdateImageInfo()
355 if ( !::SendMessage(m_hwndBtn
, BCM_SETIMAGELIST
, 0, (LPARAM
)&m_data
) )
357 wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
361 // we store image list separately to be able to use convenient wxImageList
362 // methods instead of working with raw HIMAGELIST
365 // store the rest of the data in BCM_SETIMAGELIST-friendly form
366 BUTTON_IMAGELIST m_data
;
368 // the button we're associated with
369 const HWND m_hwndBtn
;
372 wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData
);
375 #endif // wxUSE_UXTHEME
377 } // anonymous namespace
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 // ============================================================================
385 // ============================================================================
387 // ----------------------------------------------------------------------------
388 // helper functions from wx/msw/private/button.h
389 // ----------------------------------------------------------------------------
391 void wxMSWButton::UpdateMultilineStyle(HWND hwnd
, const wxString
& label
)
393 // update BS_MULTILINE style depending on the new label (resetting it
394 // doesn't seem to do anything very useful but it shouldn't hurt and we do
395 // have to set it whenever the label becomes multi line as otherwise it
396 // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
397 // the control unless it already has new lines in its label)
398 long styleOld
= ::GetWindowLong(hwnd
, GWL_STYLE
),
400 if ( label
.find(wxT('\n')) != wxString::npos
)
401 styleNew
= styleOld
| BS_MULTILINE
;
403 styleNew
= styleOld
& ~BS_MULTILINE
;
405 if ( styleNew
!= styleOld
)
406 ::SetWindowLong(hwnd
, GWL_STYLE
, styleNew
);
409 wxSize
wxMSWButton::GetFittingSize(wxWindow
*win
,
410 const wxSize
& sizeLabel
,
413 wxSize sizeBtn
= sizeLabel
;
415 // FIXME: The numbers here are pure guesswork, no idea how should the
416 // button margins be really calculated.
417 if ( flags
& Size_ExactFit
)
419 // We still need some margin or the text would be overwritten, just
420 // make it as small as possible.
421 sizeBtn
.x
+= (3*win
->GetCharWidth());
425 sizeBtn
.x
+= 3*win
->GetCharWidth();
426 sizeBtn
.y
+= win
->GetCharHeight()/2;
429 // account for the shield UAC icon if we have it
430 if ( flags
& Size_AuthNeeded
)
431 sizeBtn
.x
+= wxSystemSettings::GetMetric(wxSYS_SMALLICON_X
);
436 wxSize
wxMSWButton::ComputeBestFittingSize(wxControl
*btn
, int flags
)
441 dc
.GetMultiLineTextExtent(btn
->GetLabelText(), &sizeBtn
.x
, &sizeBtn
.y
);
443 return GetFittingSize(btn
, sizeBtn
, flags
);
446 wxSize
wxMSWButton::IncreaseToStdSizeAndCache(wxControl
*btn
, const wxSize
& size
)
448 wxSize
sizeBtn(size
);
450 // The 50x14 button size is documented in the "Recommended sizing and
451 // spacing" section of MSDN layout article.
453 // Note that we intentionally don't use GetDefaultSize() here, because
454 // it's inexact -- dialog units depend on this dialog's font.
455 const wxSize sizeDef
= btn
->ConvertDialogToPixels(wxSize(50, 14));
457 // All buttons should have at least the standard size, unless the user
458 // explicitly wants them to be as small as possible and used wxBU_EXACTFIT
459 // style to indicate this.
460 const bool incToStdSize
= !btn
->HasFlag(wxBU_EXACTFIT
);
463 if ( sizeBtn
.x
< sizeDef
.x
)
464 sizeBtn
.x
= sizeDef
.x
;
467 // Notice that we really want to make all buttons with text label equally
468 // high, otherwise they look ugly and the existing code using wxBU_EXACTFIT
469 // only uses it to control width and not height.
470 if ( incToStdSize
|| !btn
->GetLabel().empty() )
472 if ( sizeBtn
.y
< sizeDef
.y
)
473 sizeBtn
.y
= sizeDef
.y
;
476 btn
->CacheBestSize(sizeBtn
);
481 // ----------------------------------------------------------------------------
482 // creation/destruction
483 // ----------------------------------------------------------------------------
485 wxAnyButton::~wxAnyButton()
490 #endif // wxUSE_MARKUP
493 void wxAnyButton::SetLabel(const wxString
& label
)
495 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
497 wxAnyButtonBase::SetLabel(label
);
500 // If we have a plain text label, we shouldn't be using markup any longer.
506 // Unfortunately we don't really know whether we can reset the button
507 // to be non-owner-drawn or not: if we had made it owner-drawn just
508 // because of a call to SetLabelMarkup(), we could, but not if there
509 // were [also] calls to Set{Fore,Back}groundColour(). If it's really a
510 // problem to have button remain owner-drawn forever just because it
511 // had markup label once, we should record the reason for our current
512 // owner-drawnness and check it here.
514 #endif // wxUSE_MARKUP
517 // ----------------------------------------------------------------------------
518 // size management including autosizing
519 // ----------------------------------------------------------------------------
521 void wxAnyButton::AdjustForBitmapSize(wxSize
&size
) const
523 wxCHECK_RET( m_imageData
, wxT("shouldn't be called if no image") );
525 // account for the bitmap size
526 const wxSize sizeBmp
= m_imageData
->GetBitmap(State_Normal
).GetSize();
527 const wxDirection dirBmp
= m_imageData
->GetBitmapPosition();
528 if ( dirBmp
== wxLEFT
|| dirBmp
== wxRIGHT
)
531 if ( sizeBmp
.y
> size
.y
)
534 else // bitmap on top/below the text
537 if ( sizeBmp
.x
> size
.x
)
541 // account for the user-specified margins
542 size
+= 2*m_imageData
->GetBitmapMargins();
544 // and also for the margins we always add internally (unless we have no
545 // border at all in which case the button has exactly the same size as
546 // bitmap and so no margins should be used)
547 if ( !HasFlag(wxBORDER_NONE
) )
552 if ( wxUxThemeEngine::GetIfActive() )
554 wxUxThemeHandle
theme(const_cast<wxAnyButton
*>(this), L
"BUTTON");
557 wxUxThemeEngine::Get()->GetThemeMargins(theme
, NULL
,
564 // XP doesn't draw themed buttons correctly when the client
565 // area is smaller than 8x8 - enforce this minimum size for
567 size
.IncTo(wxSize(8, 8));
569 marginH
= margins
.cxLeftWidth
+ margins
.cxRightWidth
570 + 2*XP_BUTTON_EXTRA_MARGIN
;
571 marginV
= margins
.cyTopHeight
+ margins
.cyBottomHeight
572 + 2*XP_BUTTON_EXTRA_MARGIN
;
575 #endif // wxUSE_UXTHEME
578 marginV
= OD_BUTTON_MARGIN
;
581 size
.IncBy(marginH
, marginV
);
585 wxSize
wxAnyButton::DoGetBestSize() const
587 wxAnyButton
* const self
= const_cast<wxAnyButton
*>(this);
591 // Account for the text part if we have it.
595 if ( HasFlag(wxBU_EXACTFIT
) )
596 flags
|= wxMSWButton::Size_ExactFit
;
597 if ( DoGetAuthNeeded() )
598 flags
|= wxMSWButton::Size_AuthNeeded
;
604 size
= wxMSWButton::GetFittingSize(self
,
605 m_markupText
->Measure(dc
),
608 else // Normal plain text (but possibly multiline) label.
609 #endif // wxUSE_MARKUP
611 size
= wxMSWButton::ComputeBestFittingSize(self
, flags
);
616 AdjustForBitmapSize(size
);
618 return wxMSWButton::IncreaseToStdSizeAndCache(self
, size
);
621 // ----------------------------------------------------------------------------
622 // event/message handlers
623 // ----------------------------------------------------------------------------
625 WXLRESULT
wxAnyButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
627 if ( nMsg
== WM_LBUTTONDBLCLK
)
629 // emulate a click event to force an owner-drawn button to change its
630 // appearance - without this, it won't do it
631 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
633 // and continue with processing the message normally as well
636 else if ( nMsg
== WM_THEMECHANGED
)
638 // need to recalculate the best size here
639 // as the theme size might have changed
640 InvalidateBestSize();
642 #endif // wxUSE_UXTHEME
643 // must use m_mouseInWindow here instead of IsMouseInWindow()
644 // since we need to know the first time the mouse enters the window
645 // and IsMouseInWindow() would return true in this case
646 else if ( (nMsg
== WM_MOUSEMOVE
&& !m_mouseInWindow
) ||
647 nMsg
== WM_MOUSELEAVE
)
653 wxUxThemeEngine::GetIfActive() ||
654 #endif // wxUSE_UXTHEME
655 (m_imageData
&& m_imageData
->GetBitmap(State_Current
).IsOk())
663 // let the base class do all real processing
664 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
667 // ----------------------------------------------------------------------------
669 // ----------------------------------------------------------------------------
671 wxBitmap
wxAnyButton::DoGetBitmap(State which
) const
673 return m_imageData
? m_imageData
->GetBitmap(which
) : wxBitmap();
676 void wxAnyButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
679 wxXPButtonImageData
*oldData
= NULL
;
680 #endif // wxUSE_UXTHEME
682 // Check if we already had bitmaps of different size.
684 bitmap
.GetSize() != m_imageData
->GetBitmap(State_Normal
).GetSize() )
686 wxASSERT_MSG( (which
== State_Normal
) || bitmap
.IsNull(),
687 "Must set normal bitmap with the new size first" );
690 if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
692 // We can't change the size of the images stored in wxImageList
693 // in wxXPButtonImageData::m_iml so force recreating it below but
694 // keep the current data to copy its values into the new one.
695 oldData
= static_cast<wxXPButtonImageData
*>(m_imageData
);
698 #endif // wxUSE_UXTHEME
699 //else: wxODButtonImageData doesn't require anything special
702 // allocate the image data when the first bitmap is set
706 // using image list doesn't work correctly if we don't have any label
707 // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
708 // BS_BITMAP style), at least under Windows 2003 so use owner drawn
709 // strategy for bitmap-only buttons
710 if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
712 m_imageData
= new wxXPButtonImageData(this, bitmap
);
716 // Preserve the old values in case the user changed them.
717 m_imageData
->SetBitmapPosition(oldData
->GetBitmapPosition());
719 const wxSize oldMargins
= oldData
->GetBitmapMargins();
720 m_imageData
->SetBitmapMargins(oldMargins
.x
, oldMargins
.y
);
722 // No need to preserve the bitmaps though as they were of wrong
729 #endif // wxUSE_UXTHEME
731 m_imageData
= new wxODButtonImageData(this, bitmap
);
737 m_imageData
->SetBitmap(bitmap
, which
);
740 // it should be enough to only invalidate the best size when the normal
741 // bitmap changes as all bitmaps assigned to the button should be of the
743 if ( which
== State_Normal
)
744 InvalidateBestSize();
749 wxSize
wxAnyButton::DoGetBitmapMargins() const
751 return m_imageData
? m_imageData
->GetBitmapMargins() : wxSize(0, 0);
754 void wxAnyButton::DoSetBitmapMargins(wxCoord x
, wxCoord y
)
756 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
758 m_imageData
->SetBitmapMargins(x
, y
);
759 InvalidateBestSize();
762 void wxAnyButton::DoSetBitmapPosition(wxDirection dir
)
764 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
766 m_imageData
->SetBitmapPosition(dir
);
767 InvalidateBestSize();
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
776 bool wxAnyButton::DoSetLabelMarkup(const wxString
& markup
)
778 if ( !wxAnyButtonBase::DoSetLabelMarkup(markup
) )
783 m_markupText
= new wxMarkupText(markup
);
788 // We are already owner-drawn so just update the text.
789 m_markupText
->SetMarkup(markup
);
797 #endif // wxUSE_MARKUP
799 // ----------------------------------------------------------------------------
800 // owner-drawn buttons support
801 // ----------------------------------------------------------------------------
807 // return the button state using both the ODS_XXX flags specified in state
808 // parameter and the current button state
809 wxAnyButton::State
GetButtonState(wxAnyButton
*btn
, UINT state
)
811 if ( state
& ODS_DISABLED
)
812 return wxAnyButton::State_Disabled
;
814 if ( state
& ODS_SELECTED
)
815 return wxAnyButton::State_Pressed
;
817 if ( btn
->HasCapture() || btn
->IsMouseInWindow() )
818 return wxAnyButton::State_Current
;
820 if ( state
& ODS_FOCUS
)
821 return wxAnyButton::State_Focused
;
823 return btn
->GetNormalState();
826 void DrawButtonText(HDC hdc
,
831 const wxString text
= btn
->GetLabel();
833 if ( text
.find(wxT('\n')) != wxString::npos
)
835 // draw multiline label
837 // center text horizontally in any case
840 // first we need to compute its bounding rect
842 ::CopyRect(&rc
, pRect
);
843 ::DrawText(hdc
, text
.t_str(), text
.length(), &rc
,
844 DT_CENTER
| DT_CALCRECT
);
846 // now center this rect inside the entire button area
847 const LONG w
= rc
.right
- rc
.left
;
848 const LONG h
= rc
.bottom
- rc
.top
;
849 rc
.left
= pRect
->left
+ (pRect
->right
- pRect
->left
)/2 - w
/2;
850 rc
.right
= rc
.left
+w
;
851 rc
.top
= pRect
->top
+ (pRect
->bottom
- pRect
->top
)/2 - h
/2;
852 rc
.bottom
= rc
.top
+h
;
854 ::DrawText(hdc
, text
.t_str(), text
.length(), &rc
, flags
);
856 else // single line label
858 // translate wx button flags to alignment flags for DrawText()
859 if ( btn
->HasFlag(wxBU_RIGHT
) )
863 else if ( !btn
->HasFlag(wxBU_LEFT
) )
867 //else: DT_LEFT is the default anyhow (and its value is 0 too)
869 if ( btn
->HasFlag(wxBU_BOTTOM
) )
873 else if ( !btn
->HasFlag(wxBU_TOP
) )
877 //else: as above, DT_TOP is the default
879 // notice that we must have DT_SINGLELINE for vertical alignment flags
881 ::DrawText(hdc
, text
.t_str(), text
.length(), pRect
,
882 flags
| DT_SINGLELINE
);
886 void DrawRect(HDC hdc
, const RECT
& r
)
888 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
889 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
890 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
891 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
895 The button frame looks like this normally:
898 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
899 WH GB H = light grey (LIGHT)
900 WH GB G = dark grey (SHADOW)
901 WH GB B = black (DKSHADOW)
906 When the button is selected, the button becomes like this (the total button
907 size doesn't change):
918 When the button is pushed (while selected) it is like:
929 void DrawButtonFrame(HDC hdc
, RECT
& rectBtn
,
930 bool selected
, bool pushed
)
933 CopyRect(&r
, &rectBtn
);
935 AutoHPEN
hpenBlack(GetSysColor(COLOR_3DDKSHADOW
)),
936 hpenGrey(GetSysColor(COLOR_3DSHADOW
)),
937 hpenLightGr(GetSysColor(COLOR_3DLIGHT
)),
938 hpenWhite(GetSysColor(COLOR_3DHILIGHT
));
940 SelectInHDC
selectPen(hdc
, hpenBlack
);
949 (void)SelectObject(hdc
, hpenGrey
);
950 ::InflateRect(&r
, -1, -1);
960 ::InflateRect(&r
, -1, -1);
963 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
964 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
966 (void)SelectObject(hdc
, hpenWhite
);
967 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
968 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
970 (void)SelectObject(hdc
, hpenLightGr
);
971 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
972 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
974 (void)SelectObject(hdc
, hpenGrey
);
975 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
976 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
979 InflateRect(&rectBtn
, -OD_BUTTON_MARGIN
, -OD_BUTTON_MARGIN
);
983 void DrawXPBackground(wxAnyButton
*button
, HDC hdc
, RECT
& rectBtn
, UINT state
)
985 wxUxThemeHandle
theme(button
, L
"BUTTON");
987 // this array is indexed by wxAnyButton::State values and so must be kept in
989 static const int uxStates
[] =
991 PBS_NORMAL
, PBS_HOT
, PBS_PRESSED
, PBS_DISABLED
, PBS_DEFAULTED
994 int iState
= uxStates
[GetButtonState(button
, state
)];
996 wxUxThemeEngine
* const engine
= wxUxThemeEngine::Get();
998 // draw parent background if needed
999 if ( engine
->IsThemeBackgroundPartiallyTransparent
1006 // Set this button as the one whose background is being erased: this
1007 // allows our WM_ERASEBKGND handler used by DrawThemeParentBackground()
1008 // to correctly align the background brush with this window instead of
1009 // the parent window to which WM_ERASEBKGND is sent. Notice that this
1010 // doesn't work with custom user-defined EVT_ERASE_BACKGROUND handlers
1011 // as they won't be aligned but unfortunately all the attempts to fix
1012 // it by shifting DC origin before calling DrawThemeParentBackground()
1013 // failed to work so we at least do this, even though this is far from
1014 // being the perfect solution.
1015 wxWindowBeingErased
= button
;
1017 engine
->DrawThemeParentBackground(GetHwndOf(button
), hdc
, &rectBtn
);
1019 wxWindowBeingErased
= NULL
;
1023 engine
->DrawThemeBackground(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1026 // calculate content area margins
1028 engine
->GetThemeMargins(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1029 TMT_CONTENTMARGINS
, &rectBtn
, &margins
);
1030 ::InflateRect(&rectBtn
, -margins
.cxLeftWidth
, -margins
.cyTopHeight
);
1031 ::InflateRect(&rectBtn
, -XP_BUTTON_EXTRA_MARGIN
, -XP_BUTTON_EXTRA_MARGIN
);
1033 if ( button
->UseBgCol() )
1035 COLORREF colBg
= wxColourToRGB(button
->GetBackgroundColour());
1036 AutoHBRUSH
hbrushBackground(colBg
);
1038 // don't overwrite the focus rect
1040 ::CopyRect(&rectClient
, &rectBtn
);
1041 ::InflateRect(&rectClient
, -1, -1);
1042 FillRect(hdc
, &rectClient
, hbrushBackground
);
1045 #endif // wxUSE_UXTHEME
1047 } // anonymous namespace
1049 // ----------------------------------------------------------------------------
1050 // owner drawn buttons support
1051 // ----------------------------------------------------------------------------
1053 void wxAnyButton::MakeOwnerDrawn()
1055 if ( !IsOwnerDrawn() )
1058 // note that BS_OWNERDRAW is not independent from other style bits
1059 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
1060 style
&= ~(BS_3STATE
| BS_AUTO3STATE
| BS_AUTOCHECKBOX
| BS_AUTORADIOBUTTON
| BS_CHECKBOX
| BS_DEFPUSHBUTTON
| BS_GROUPBOX
| BS_PUSHBUTTON
| BS_RADIOBUTTON
| BS_PUSHLIKE
);
1061 style
|= BS_OWNERDRAW
;
1062 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
1066 bool wxAnyButton::IsOwnerDrawn() const
1068 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
1069 return ( (style
& BS_OWNERDRAW
) == BS_OWNERDRAW
);
1072 bool wxAnyButton::SetBackgroundColour(const wxColour
&colour
)
1074 if ( !wxControl::SetBackgroundColour(colour
) )
1087 bool wxAnyButton::SetForegroundColour(const wxColour
&colour
)
1089 if ( !wxControl::SetForegroundColour(colour
) )
1102 bool wxAnyButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
1104 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
1105 HDC hdc
= lpDIS
->hDC
;
1107 UINT state
= lpDIS
->itemState
;
1108 switch ( GetButtonState(this, state
) )
1110 case State_Disabled
:
1111 state
|= ODS_DISABLED
;
1114 state
|= ODS_SELECTED
;
1123 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
1126 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
1128 // draw the button background
1129 if ( !HasFlag(wxBORDER_NONE
) )
1132 if ( wxUxThemeEngine::GetIfActive() )
1134 DrawXPBackground(this, hdc
, rectBtn
, state
);
1137 #endif // wxUSE_UXTHEME
1139 COLORREF colBg
= wxColourToRGB(GetBackgroundColour());
1141 // first, draw the background
1142 AutoHBRUSH
hbrushBackground(colBg
);
1143 FillRect(hdc
, &rectBtn
, hbrushBackground
);
1145 // draw the border for the current state
1146 bool selected
= (state
& ODS_SELECTED
) != 0;
1150 tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
1153 selected
= tlw
->GetDefaultItem() == this;
1157 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
1160 // draw the focus rectangle if we need it
1161 if ( (state
& ODS_FOCUS
) && !(state
& ODS_NOFOCUSRECT
) )
1163 DrawFocusRect(hdc
, &rectBtn
);
1166 if ( !wxUxThemeEngine::GetIfActive() )
1167 #endif // wxUSE_UXTHEME
1171 // the label is shifted by 1 pixel to create "pushed" effect
1172 OffsetRect(&rectBtn
, 1, 1);
1179 // draw the image, if any
1182 wxBitmap bmp
= m_imageData
->GetBitmap(GetButtonState(this, state
));
1184 bmp
= m_imageData
->GetBitmap(State_Normal
);
1186 const wxSize sizeBmp
= bmp
.GetSize();
1187 const wxSize margin
= m_imageData
->GetBitmapMargins();
1188 const wxSize
sizeBmpWithMargins(sizeBmp
+ 2*margin
);
1189 wxRect
rectButton(wxRectFromRECT(rectBtn
));
1191 // for simplicity, we start with centred rectangle and then move it to
1192 // the appropriate edge
1193 wxRect rectBitmap
= wxRect(sizeBmp
).CentreIn(rectButton
);
1195 // move bitmap only if we have a label, otherwise keep it centered
1198 switch ( m_imageData
->GetBitmapPosition() )
1201 wxFAIL_MSG( "invalid direction" );
1205 rectBitmap
.x
= rectButton
.x
+ margin
.x
;
1206 rectButton
.x
+= sizeBmpWithMargins
.x
;
1207 rectButton
.width
-= sizeBmpWithMargins
.x
;
1211 rectBitmap
.x
= rectButton
.GetRight() - sizeBmp
.x
- margin
.x
;
1212 rectButton
.width
-= sizeBmpWithMargins
.x
;
1216 rectBitmap
.y
= rectButton
.y
+ margin
.y
;
1217 rectButton
.y
+= sizeBmpWithMargins
.y
;
1218 rectButton
.height
-= sizeBmpWithMargins
.y
;
1222 rectBitmap
.y
= rectButton
.GetBottom() - sizeBmp
.y
- margin
.y
;
1223 rectButton
.height
-= sizeBmpWithMargins
.y
;
1228 wxDCTemp
dst((WXHDC
)hdc
);
1229 dst
.DrawBitmap(bmp
, rectBitmap
.GetPosition(), true);
1231 wxCopyRectToRECT(rectButton
, rectBtn
);
1235 // finally draw the label
1238 COLORREF colFg
= state
& ODS_DISABLED
1239 ? ::GetSysColor(COLOR_GRAYTEXT
)
1240 : wxColourToRGB(GetForegroundColour());
1242 wxTextColoursChanger
changeFg(hdc
, colFg
, CLR_INVALID
);
1243 wxBkModeChanger
changeBkMode(hdc
, wxBRUSHSTYLE_TRANSPARENT
);
1248 wxDCTemp
dc((WXHDC
)hdc
);
1249 dc
.SetTextForeground(wxColour(colFg
));
1250 dc
.SetFont(GetFont());
1252 m_markupText
->Render(dc
, wxRectFromRECT(rectBtn
),
1254 ? wxMarkupText::Render_Default
1255 : wxMarkupText::Render_ShowAccels
);
1257 else // Plain text label
1258 #endif // wxUSE_MARKUP
1260 // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000)
1261 // systems but by happy coincidence ODS_NOACCEL is not used under
1262 // them neither so DT_HIDEPREFIX should never be used there
1263 DrawButtonText(hdc
, &rectBtn
, this,
1264 state
& ODS_NOACCEL
? DT_HIDEPREFIX
: 0);
1271 #endif // wxHAS_ANY_BUTTON