1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/button.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/button.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
38 #include "wx/dcclient.h"
39 #include "wx/toplevel.h"
40 #include "wx/msw/wrapcctl.h"
41 #include "wx/msw/private.h"
42 #include "wx/msw/missing.h"
45 #include "wx/imaglist.h"
46 #include "wx/stockitem.h"
47 #include "wx/msw/private/button.h"
48 #include "wx/msw/private/dc.h"
49 #include "wx/private/window.h"
51 using namespace wxMSWImpl
;
54 #include "wx/msw/uxtheme.h"
56 // no need to include tmschema.h
58 #define BP_PUSHBUTTON 1
63 #define PBS_DISABLED 4
64 #define PBS_DEFAULTED 5
66 #define TMT_CONTENTMARGINS 3602
69 // provide the necessary declarations ourselves if they're missing from
71 #ifndef BCM_SETIMAGELIST
72 #define BCM_SETIMAGELIST 0x1602
73 #define BCM_SETTEXTMARGIN 0x1604
77 BUTTON_IMAGELIST_ALIGN_LEFT
,
78 BUTTON_IMAGELIST_ALIGN_RIGHT
,
79 BUTTON_IMAGELIST_ALIGN_TOP
,
80 BUTTON_IMAGELIST_ALIGN_BOTTOM
83 struct BUTTON_IMAGELIST
90 #endif // wxUSE_UXTHEME
92 #ifndef WM_THEMECHANGED
93 #define WM_THEMECHANGED 0x031A
97 #define ODS_NOACCEL 0x0100
100 #ifndef ODS_NOFOCUSRECT
101 #define ODS_NOFOCUSRECT 0x0200
104 #ifndef DT_HIDEPREFIX
105 #define DT_HIDEPREFIX 0x00100000
108 // set the value for BCM_SETSHIELD (for the UAC shield) if it's not defined in
110 #ifndef BCM_SETSHIELD
111 #define BCM_SETSHIELD 0x160c
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(wxButton
::State which
) const = 0;
127 virtual void SetBitmap(const wxBitmap
& bitmap
, wxButton
::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(wxButton
*btn
, const wxBitmap
& bitmap
)
151 SetBitmap(bitmap
, wxButton
::State_Normal
);
155 // we use margins when we have both bitmap and text, but when we have
156 // only the bitmap it should take up the entire button area
157 if ( btn
->ShowsLabel() )
159 m_margin
.x
= btn
->GetCharWidth();
160 m_margin
.y
= btn
->GetCharHeight() / 2;
164 virtual wxBitmap
GetBitmap(wxButton
::State which
) const
166 return m_bitmaps
[which
];
169 virtual void SetBitmap(const wxBitmap
& bitmap
, wxButton
::State which
)
171 m_bitmaps
[which
] = bitmap
;
174 virtual wxSize
GetBitmapMargins() const
179 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
181 m_margin
= wxSize(x
, y
);
184 virtual wxDirection
GetBitmapPosition() const
189 virtual void SetBitmapPosition(wxDirection dir
)
195 // just store the values passed to us to be able to retrieve them later
196 // from the drawing code
197 wxBitmap m_bitmaps
[wxButton
::State_Max
];
201 wxDECLARE_NO_COPY_CLASS(wxODButtonImageData
);
206 // somehow the margin is one pixel greater than the value returned by
207 // GetThemeMargins() call
208 const int XP_BUTTON_EXTRA_MARGIN
= 1;
210 class wxXPButtonImageData
: public wxButtonImageData
213 // we must be constructed with the size of our images as we need to create
215 wxXPButtonImageData(wxButton
*btn
, const wxBitmap
& bitmap
)
216 : m_iml(bitmap
.GetWidth(), bitmap
.GetHeight(), true /* use mask */,
217 wxButton
::State_Max
),
218 m_hwndBtn(GetHwndOf(btn
))
220 // initialize all bitmaps to normal state
221 for ( int n
= 0; n
< wxButton
::State_Max
; n
++ )
226 m_data
.himl
= GetHimagelistOf(&m_iml
);
228 // no margins by default
230 m_data
.margin
.right
=
232 m_data
.margin
.bottom
= 0;
234 // use default alignment
235 m_data
.uAlign
= BUTTON_IMAGELIST_ALIGN_LEFT
;
240 virtual wxBitmap
GetBitmap(wxButton
::State which
) const
242 return m_iml
.GetBitmap(which
);
245 virtual void SetBitmap(const wxBitmap
& bitmap
, wxButton
::State which
)
247 m_iml
.Replace(which
, bitmap
);
252 virtual wxSize
GetBitmapMargins() const
254 return wxSize(m_data
.margin
.left
, m_data
.margin
.top
);
257 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
259 RECT
& margin
= m_data
.margin
;
265 if ( !::SendMessage(m_hwndBtn
, BCM_SETTEXTMARGIN
, 0, (LPARAM
)&margin
) )
267 wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
271 virtual wxDirection
GetBitmapPosition() const
273 switch ( m_data
.uAlign
)
276 wxFAIL_MSG( "invalid image alignment" );
279 case BUTTON_IMAGELIST_ALIGN_LEFT
:
282 case BUTTON_IMAGELIST_ALIGN_RIGHT
:
285 case BUTTON_IMAGELIST_ALIGN_TOP
:
288 case BUTTON_IMAGELIST_ALIGN_BOTTOM
:
293 virtual void SetBitmapPosition(wxDirection dir
)
299 wxFAIL_MSG( "invalid direction" );
303 alignNew
= BUTTON_IMAGELIST_ALIGN_LEFT
;
307 alignNew
= BUTTON_IMAGELIST_ALIGN_RIGHT
;
311 alignNew
= BUTTON_IMAGELIST_ALIGN_TOP
;
315 alignNew
= BUTTON_IMAGELIST_ALIGN_BOTTOM
;
319 if ( alignNew
!= m_data
.uAlign
)
321 m_data
.uAlign
= alignNew
;
327 void UpdateImageInfo()
329 if ( !::SendMessage(m_hwndBtn
, BCM_SETIMAGELIST
, 0, (LPARAM
)&m_data
) )
331 wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
335 // we store image list separately to be able to use convenient wxImageList
336 // methods instead of working with raw HIMAGELIST
339 // store the rest of the data in BCM_SETIMAGELIST-friendly form
340 BUTTON_IMAGELIST m_data
;
342 // the button we're associated with
343 const HWND m_hwndBtn
;
346 wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData
);
349 #endif // wxUSE_UXTHEME
351 } // anonymous namespace
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 // ============================================================================
359 // ============================================================================
361 // ----------------------------------------------------------------------------
362 // helper functions from wx/msw/private/button.h
363 // ----------------------------------------------------------------------------
365 void wxMSWButton
::UpdateMultilineStyle(HWND hwnd
, const wxString
& label
)
367 // update BS_MULTILINE style depending on the new label (resetting it
368 // doesn't seem to do anything very useful but it shouldn't hurt and we do
369 // have to set it whenever the label becomes multi line as otherwise it
370 // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
371 // the control unless it already has new lines in its label)
372 long styleOld
= ::GetWindowLong(hwnd
, GWL_STYLE
),
374 if ( label
.find(wxT('\n')) != wxString
::npos
)
375 styleNew
= styleOld
| BS_MULTILINE
;
377 styleNew
= styleOld
& ~BS_MULTILINE
;
379 if ( styleNew
!= styleOld
)
380 ::SetWindowLong(hwnd
, GWL_STYLE
, styleNew
);
383 wxSize wxMSWButton
::GetFittingSize(wxWindow
*win
,
384 const wxSize
& sizeLabel
,
387 // FIXME: this is pure guesswork, need to retrieve the real button margins
388 wxSize sizeBtn
= sizeLabel
;
390 sizeBtn
.x
+= 3*win
->GetCharWidth();
391 sizeBtn
.y
= 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(sizeLabel
.y
)/10;
393 // account for the shield UAC icon if we have it
394 if ( flags
& Size_AuthNeeded
)
395 sizeBtn
.x
+= wxSystemSettings
::GetMetric(wxSYS_SMALLICON_X
);
400 wxSize wxMSWButton
::ComputeBestFittingSize(wxControl
*btn
, int flags
)
405 dc
.GetMultiLineTextExtent(btn
->GetLabelText(), &sizeBtn
.x
, &sizeBtn
.y
);
407 return GetFittingSize(btn
, sizeBtn
, flags
);
410 wxSize wxMSWButton
::IncreaseToStdSizeAndCache(wxControl
*btn
, const wxSize
& size
)
412 wxSize
sizeBtn(size
);
414 // all buttons have at least the standard size unless the user explicitly
415 // wants them to be of smaller size and used wxBU_EXACTFIT style when
416 // creating the button
417 if ( !btn
->HasFlag(wxBU_EXACTFIT
) )
419 // The size of a standard button in the dialog units is 50x14, use it.
420 // Note that we intentionally don't use GetDefaultSize() here, because
421 // it's inexact -- dialog units depend on this dialog's font.
422 wxSize sizeDef
= btn
->ConvertDialogToPixels(wxSize(50, 14));
423 if ( sizeBtn
.x
< sizeDef
.x
)
424 sizeBtn
.x
= sizeDef
.x
;
425 if ( sizeBtn
.y
< sizeDef
.y
)
426 sizeBtn
.y
= sizeDef
.y
;
429 btn
->CacheBestSize(sizeBtn
);
434 // ----------------------------------------------------------------------------
435 // creation/destruction
436 // ----------------------------------------------------------------------------
438 bool wxButton
::Create(wxWindow
*parent
,
444 const wxValidator
& validator
,
445 const wxString
& name
)
447 m_authNeeded
= false;
450 if (label
.empty() && wxIsStockID(id
))
452 // On Windows, some buttons aren't supposed to have mnemonics
453 label
= wxGetStockLabel
456 id
== wxID_OK
|| id
== wxID_CANCEL
|| id
== wxID_CLOSE
458 : wxSTOCK_WITH_MNEMONIC
462 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
466 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
468 // if the label contains several lines we must explicitly tell the button
469 // about it or it wouldn't draw it correctly ("\n"s would just appear as
472 // NB: we do it here and not in MSWGetStyle() because we need the label
473 // value and the label is not set yet when MSWGetStyle() is called
474 msStyle
|= wxMSWButton
::GetMultilineStyle(label
);
476 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
479 wxButton
::~wxButton()
481 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
482 if ( tlw
&& tlw
->GetTmpDefaultItem() == this )
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 WXDWORD wxButton
::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
496 // buttons never have an external border, they draw their own one
497 WXDWORD msStyle
= wxControl
::MSWGetStyle
499 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
502 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
503 // each other in any resizeable dialog which has more than one button in
505 msStyle
|= WS_CLIPSIBLINGS
;
507 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
508 // and wxBU_RIGHT to get BS_CENTER!
509 if ( style
& wxBU_LEFT
)
511 if ( style
& wxBU_RIGHT
)
513 if ( style
& wxBU_TOP
)
515 if ( style
& wxBU_BOTTOM
)
516 msStyle
|= BS_BOTTOM
;
519 if ( style
& wxNO_BORDER
)
521 #endif // __WXWINCE__
526 void wxButton
::SetLabel(const wxString
& label
)
528 wxMSWButton
::UpdateMultilineStyle(GetHwnd(), label
);
530 wxButtonBase
::SetLabel(label
);
533 // ----------------------------------------------------------------------------
534 // size management including autosizing
535 // ----------------------------------------------------------------------------
537 void wxButton
::AdjustForBitmapSize(wxSize
&size
) const
539 wxCHECK_RET( m_imageData
, wxT("shouldn't be called if no image") );
541 // account for the bitmap size
542 const wxSize sizeBmp
= m_imageData
->GetBitmap(State_Normal
).GetSize();
543 const wxDirection dirBmp
= m_imageData
->GetBitmapPosition();
544 if ( dirBmp
== wxLEFT
|| dirBmp
== wxRIGHT
)
547 if ( sizeBmp
.y
> size
.y
)
550 else // bitmap on top/below the text
553 if ( sizeBmp
.x
> size
.x
)
557 // account for the user-specified margins
558 size
+= 2*m_imageData
->GetBitmapMargins();
560 // and also for the margins we always add internally (unless we have no
561 // border at all in which case the button has exactly the same size as
562 // bitmap and so no margins should be used)
563 if ( !HasFlag(wxBORDER_NONE
) )
568 if ( wxUxThemeEngine
::GetIfActive() )
570 wxUxThemeHandle
theme(const_cast<wxButton
*>(this), L
"BUTTON");
573 wxUxThemeEngine
::Get()->GetThemeMargins(theme
, NULL
,
580 // XP doesn't draw themed buttons correctly when the client
581 // area is smaller than 8x8 - enforce this minimum size for
583 size
.IncTo(wxSize(8, 8));
585 marginH
= margins
.cxLeftWidth
+ margins
.cxRightWidth
586 + 2*XP_BUTTON_EXTRA_MARGIN
;
587 marginV
= margins
.cyTopHeight
+ margins
.cyBottomHeight
588 + 2*XP_BUTTON_EXTRA_MARGIN
;
591 #endif // wxUSE_UXTHEME
594 marginV
= OD_BUTTON_MARGIN
;
597 size
.IncBy(marginH
, marginV
);
601 wxSize wxButton
::DoGetBestSize() const
603 wxButton
* const self
= const_cast<wxButton
*>(this);
607 // account for the text part if we have it or if we don't have any image at
608 // all (buttons initially created with empty label should still have a non
610 if ( ShowsLabel() || !m_imageData
)
613 if ( GetAuthNeeded() )
614 flags
|= wxMSWButton
::Size_AuthNeeded
;
616 size
= wxMSWButton
::ComputeBestFittingSize(self
, flags
);
620 AdjustForBitmapSize(size
);
622 return wxMSWButton
::IncreaseToStdSizeAndCache(self
, size
);
626 wxSize wxButtonBase
::GetDefaultSize()
628 static wxSize s_sizeBtn
;
630 if ( s_sizeBtn
.x
== 0 )
633 dc
.SetFont(wxSystemSettings
::GetFont(wxSYS_DEFAULT_GUI_FONT
));
635 // The size of a standard button in the dialog units is 50x14,
636 // translate this to pixels.
638 // Windows' computes dialog units using average character width over
639 // upper- and lower-case ASCII alphabet and not using the average
640 // character width metadata stored in the font; see
641 // http://support.microsoft.com/default.aspx/kb/145994 for detailed
644 // NB: wxMulDivInt32() is used, because it correctly rounds the result
646 const wxSize base
= wxPrivate
::GetAverageASCIILetterSize(dc
);
647 s_sizeBtn
.x
= wxMulDivInt32(50, base
.x
, 4);
648 s_sizeBtn
.y
= wxMulDivInt32(14, base
.y
, 8);
654 // ----------------------------------------------------------------------------
655 // default button handling
656 // ----------------------------------------------------------------------------
659 The comment below and all this code is probably due to not using WM_NEXTDLGCTL
660 message when changing focus (but just SetFocus() which is not enough), see
661 http://blogs.msdn.com/oldnewthing/archive/2004/08/02/205624.aspx for the
664 TODO: Do use WM_NEXTDLGCTL and get rid of all this code.
667 "Everything you ever wanted to know about the default buttons" or "Why do we
668 have to do all this?"
670 In MSW the default button should be activated when the user presses Enter
671 and the current control doesn't process Enter itself somehow. This is
672 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
673 Another aspect of "defaultness" is that the default button has different
674 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
675 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
676 be unset if our parent window is not active so it should be unset whenever
677 we lose activation and set back when we regain it.
679 Final complication is that when a button is active, it should be the default
680 one, i.e. pressing Enter on a button always activates it and not another
683 We handle this by maintaining a permanent and a temporary default items in
684 wxControlContainer (both may be NULL). When a button becomes the current
685 control (i.e. gets focus) it sets itself as the temporary default which
686 ensures that it has the right appearance and that Enter will be redirected
687 to it. When the button loses focus, it unsets the temporary default and so
688 the default item will be the permanent default -- that is the default button
689 if any had been set or none otherwise, which is just what we want.
691 NB: all this is quite complicated by now and the worst is that normally
692 it shouldn't be necessary at all as for the normal Windows programs
693 DefWindowProc() and IsDialogMessage() take care of all this
694 automatically -- however in wxWidgets programs this doesn't work for
695 nested hierarchies (i.e. a notebook inside a notebook) for unknown
696 reason and so we have to reproduce all this code ourselves. It would be
697 very nice if we could avoid doing it.
700 // set this button as the (permanently) default one in its panel
701 wxWindow
*wxButton
::SetDefault()
703 // set this one as the default button both for wxWidgets ...
704 wxWindow
*winOldDefault
= wxButtonBase
::SetDefault();
707 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
708 SetDefaultStyle(this, true);
710 return winOldDefault
;
713 // return the top level parent window if it's not being deleted yet, otherwise
715 static wxTopLevelWindow
*GetTLWParentIfNotBeingDeleted(wxWindow
*win
)
719 // IsTopLevel() will return false for a wxTLW being deleted, so we also
720 // need the parent test for this case
721 wxWindow
* const parent
= win
->GetParent();
722 if ( !parent
|| win
->IsTopLevel() )
724 if ( win
->IsBeingDeleted() )
733 wxASSERT_MSG( win
, wxT("button without top level parent?") );
735 wxTopLevelWindow
* const tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
736 wxASSERT_MSG( tlw
, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
741 // set this button as being currently default
742 void wxButton
::SetTmpDefault()
744 wxTopLevelWindow
* const tlw
= GetTLWParentIfNotBeingDeleted(GetParent());
748 wxWindow
*winOldDefault
= tlw
->GetDefaultItem();
749 tlw
->SetTmpDefaultItem(this);
751 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
752 SetDefaultStyle(this, true);
755 // unset this button as currently default, it may still stay permanent default
756 void wxButton
::UnsetTmpDefault()
758 wxTopLevelWindow
* const tlw
= GetTLWParentIfNotBeingDeleted(GetParent());
762 tlw
->SetTmpDefaultItem(NULL
);
764 wxWindow
*winOldDefault
= tlw
->GetDefaultItem();
766 SetDefaultStyle(this, false);
767 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), true);
772 wxButton
::SetDefaultStyle(wxButton
*btn
, bool on
)
774 // we may be called with NULL pointer -- simpler to do the check here than
775 // in the caller which does wxDynamicCast()
779 // first, let DefDlgProc() know about the new default button
782 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
783 // focus at all any more
784 if ( !wxTheApp
->IsActive() )
787 wxWindow
* const tlw
= wxGetTopLevelParent(btn
);
788 wxCHECK_RET( tlw
, wxT("button without top level window?") );
790 ::SendMessage(GetHwndOf(tlw
), DM_SETDEFID
, btn
->GetId(), 0L);
792 // sending DM_SETDEFID also changes the button style to
793 // BS_DEFPUSHBUTTON so there is nothing more to do
796 // then also change the style as needed
797 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
798 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
800 // don't do it with the owner drawn buttons because it will
801 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
802 // BS_DEFPUSHBUTTON != 0)!
803 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
805 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
806 on ? style
| BS_DEFPUSHBUTTON
807 : style
& ~BS_DEFPUSHBUTTON
,
812 // redraw the button - it will notice itself that it's
813 // [not] the default one [any longer]
817 //else: already has correct style
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
824 bool wxButton
::SendClickEvent()
826 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
827 event
.SetEventObject(this);
829 return ProcessCommand(event
);
832 void wxButton
::Command(wxCommandEvent
& event
)
834 ProcessCommand(event
);
837 // ----------------------------------------------------------------------------
838 // event/message handlers
839 // ----------------------------------------------------------------------------
841 bool wxButton
::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
843 bool processed
= false;
846 // NOTE: Apparently older versions (NT 4?) of the common controls send
847 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
848 // buttons, so in order to send two EVT_BUTTON events we should
849 // catch both types. Currently (Feb 2003) up-to-date versions of
850 // win98, win2k and winXP all send two BN_CLICKED messages for
851 // all button types, so we don't catch BN_DOUBLECLICKED anymore
852 // in order to not get 3 EVT_BUTTON events. If this is a problem
853 // then we need to figure out which version of the comctl32 changed
854 // this behaviour and test for it.
856 case 1: // message came from an accelerator
857 case BN_CLICKED
: // normal buttons send this
858 processed
= SendClickEvent();
865 WXLRESULT wxButton
::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
867 // when we receive focus, we want to temporarily become the default button in
868 // our parent panel so that pressing "Enter" would activate us -- and when
869 // losing it we should restore the previous default button as well
870 if ( nMsg
== WM_SETFOCUS
)
874 // let the default processing take place too
876 else if ( nMsg
== WM_KILLFOCUS
)
880 else if ( nMsg
== WM_LBUTTONDBLCLK
)
882 // emulate a click event to force an owner-drawn button to change its
883 // appearance - without this, it won't do it
884 (void)wxControl
::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
886 // and continue with processing the message normally as well
889 else if ( nMsg
== WM_THEMECHANGED
)
891 // need to recalculate the best size here
892 // as the theme size might have changed
893 InvalidateBestSize();
895 #endif // wxUSE_UXTHEME
896 // must use m_mouseInWindow here instead of IsMouseInWindow()
897 // since we need to know the first time the mouse enters the window
898 // and IsMouseInWindow() would return true in this case
899 else if ( (nMsg
== WM_MOUSEMOVE
&& !m_mouseInWindow
) ||
900 nMsg
== WM_MOUSELEAVE
)
906 wxUxThemeEngine
::GetIfActive() ||
907 #endif // wxUSE_UXTHEME
908 (m_imageData
&& m_imageData
->GetBitmap(State_Current
).IsOk())
916 // let the base class do all real processing
917 return wxControl
::MSWWindowProc(nMsg
, wParam
, lParam
);
920 // ----------------------------------------------------------------------------
921 // authentication needed handling
922 // ----------------------------------------------------------------------------
924 bool wxButton
::DoGetAuthNeeded() const
929 void wxButton
::DoSetAuthNeeded(bool show
)
931 // show/hide UAC symbol on Windows Vista and later
932 if ( wxGetWinVersion() >= wxWinVersion_6
)
935 ::SendMessage(GetHwnd(), BCM_SETSHIELD
, 0, show
);
936 InvalidateBestSize();
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
944 wxBitmap wxButton
::DoGetBitmap(State which
) const
946 return m_imageData ? m_imageData
->GetBitmap(which
) : wxBitmap();
949 void wxButton
::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
951 // allocate the image data when the first bitmap is set
955 // using image list doesn't work correctly if we don't have any label
956 // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
957 // BS_BITMAP style), at least under Windows 2003 so use owner drawn
958 // strategy for bitmap-only buttons
959 if ( ShowsLabel() && wxUxThemeEngine
::GetIfActive() )
961 m_imageData
= new wxXPButtonImageData(this, bitmap
);
964 #endif // wxUSE_UXTHEME
966 m_imageData
= new wxODButtonImageData(this, bitmap
);
972 m_imageData
->SetBitmap(bitmap
, which
);
975 // it should be enough to only invalidate the best size when the normal
976 // bitmap changes as all bitmaps assigned to the button should be of the
978 if ( which
== State_Normal
)
979 InvalidateBestSize();
984 wxSize wxButton
::DoGetBitmapMargins() const
986 return m_imageData ? m_imageData
->GetBitmapMargins() : wxSize(0, 0);
989 void wxButton
::DoSetBitmapMargins(wxCoord x
, wxCoord y
)
991 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
993 m_imageData
->SetBitmapMargins(x
, y
);
994 InvalidateBestSize();
997 void wxButton
::DoSetBitmapPosition(wxDirection dir
)
999 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
1001 m_imageData
->SetBitmapPosition(dir
);
1002 InvalidateBestSize();
1005 // ----------------------------------------------------------------------------
1006 // owner-drawn buttons support
1007 // ----------------------------------------------------------------------------
1013 // return the button state using both the ODS_XXX flags specified in state
1014 // parameter and the current button state
1015 wxButton
::State
GetButtonState(wxButton
*btn
, UINT state
)
1017 if ( state
& ODS_DISABLED
)
1018 return wxButton
::State_Disabled
;
1020 if ( state
& ODS_SELECTED
)
1021 return wxButton
::State_Pressed
;
1023 if ( btn
->HasCapture() || btn
->IsMouseInWindow() )
1024 return wxButton
::State_Current
;
1026 if ( state
& ODS_FOCUS
)
1027 return wxButton
::State_Focused
;
1029 return wxButton
::State_Normal
;
1032 void DrawButtonText(HDC hdc
,
1034 const wxString
& text
,
1038 wxTextColoursChanger
changeFg(hdc
, col
, CLR_INVALID
);
1039 wxBkModeChanger
changeBkMode(hdc
, wxBRUSHSTYLE_TRANSPARENT
);
1041 // center text horizontally in any case
1044 if ( text
.find(wxT('\n')) != wxString
::npos
)
1046 // draw multiline label
1048 // first we need to compute its bounding rect
1050 ::CopyRect(&rc
, pRect
);
1051 ::DrawText(hdc
, text
.wx_str(), text
.length(), &rc
,
1052 DT_CENTER
| DT_CALCRECT
);
1054 // now center this rect inside the entire button area
1055 const LONG w
= rc
.right
- rc
.left
;
1056 const LONG h
= rc
.bottom
- rc
.top
;
1057 rc
.left
= (pRect
->right
- pRect
->left
)/2 - w
/2;
1058 rc
.right
= rc
.left
+w
;
1059 rc
.top
= (pRect
->bottom
- pRect
->top
)/2 - h
/2;
1060 rc
.bottom
= rc
.top
+h
;
1062 ::DrawText(hdc
, text
.wx_str(), text
.length(), &rc
, flags
);
1064 else // single line label
1066 // centre text vertically too (notice that we must have DT_SINGLELINE
1067 // for DT_VCENTER to work)
1068 ::DrawText(hdc
, text
.wx_str(), text
.length(), pRect
,
1069 flags
| DT_SINGLELINE
| DT_VCENTER
);
1073 void DrawRect(HDC hdc
, const RECT
& r
)
1075 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
1076 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
1077 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
1078 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
1082 The button frame looks like this normally:
1085 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
1086 WH GB H = light grey (LIGHT)
1087 WH GB G = dark grey (SHADOW)
1088 WH GB B = black (DKSHADOW)
1093 When the button is selected, the button becomes like this (the total button
1094 size doesn't change):
1105 When the button is pushed (while selected) it is like:
1116 void DrawButtonFrame(HDC hdc
, RECT
& rectBtn
,
1117 bool selected
, bool pushed
)
1120 CopyRect(&r
, &rectBtn
);
1122 AutoHPEN
hpenBlack(GetSysColor(COLOR_3DDKSHADOW
)),
1123 hpenGrey(GetSysColor(COLOR_3DSHADOW
)),
1124 hpenLightGr(GetSysColor(COLOR_3DLIGHT
)),
1125 hpenWhite(GetSysColor(COLOR_3DHILIGHT
));
1127 SelectInHDC
selectPen(hdc
, hpenBlack
);
1136 (void)SelectObject(hdc
, hpenGrey
);
1137 ::InflateRect(&r
, -1, -1);
1147 ::InflateRect(&r
, -1, -1);
1150 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
1151 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
1153 (void)SelectObject(hdc
, hpenWhite
);
1154 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
1155 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
1157 (void)SelectObject(hdc
, hpenLightGr
);
1158 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
1159 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
1161 (void)SelectObject(hdc
, hpenGrey
);
1162 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
1163 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
1166 InflateRect(&rectBtn
, -OD_BUTTON_MARGIN
, -OD_BUTTON_MARGIN
);
1170 void DrawXPBackground(wxButton
*button
, HDC hdc
, RECT
& rectBtn
, UINT state
)
1172 wxUxThemeHandle
theme(button
, L
"BUTTON");
1174 // this array is indexed by wxButton::State values and so must be kept in
1176 static const int uxStates
[] =
1178 PBS_NORMAL
, PBS_HOT
, PBS_PRESSED
, PBS_DISABLED
, PBS_DEFAULTED
1181 int iState
= uxStates
[GetButtonState(button
, state
)];
1183 wxUxThemeEngine
* const engine
= wxUxThemeEngine
::Get();
1185 // draw parent background if needed
1186 if ( engine
->IsThemeBackgroundPartiallyTransparent
1193 engine
->DrawThemeParentBackground(GetHwndOf(button
), hdc
, &rectBtn
);
1197 engine
->DrawThemeBackground(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1200 // calculate content area margins
1202 engine
->GetThemeMargins(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1203 TMT_CONTENTMARGINS
, &rectBtn
, &margins
);
1204 ::InflateRect(&rectBtn
, -margins
.cxLeftWidth
, -margins
.cyTopHeight
);
1205 ::InflateRect(&rectBtn
, -XP_BUTTON_EXTRA_MARGIN
, -XP_BUTTON_EXTRA_MARGIN
);
1207 if ( button
->UseBgCol() )
1209 COLORREF colBg
= wxColourToRGB(button
->GetBackgroundColour());
1210 AutoHBRUSH
hbrushBackground(colBg
);
1212 // don't overwrite the focus rect
1214 ::CopyRect(&rectClient
, &rectBtn
);
1215 ::InflateRect(&rectClient
, -1, -1);
1216 FillRect(hdc
, &rectClient
, hbrushBackground
);
1219 #endif // wxUSE_UXTHEME
1221 } // anonymous namespace
1223 // ----------------------------------------------------------------------------
1224 // owner drawn buttons support
1225 // ----------------------------------------------------------------------------
1227 void wxButton
::MakeOwnerDrawn()
1229 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
1230 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
1233 style
|= BS_OWNERDRAW
;
1234 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
1238 bool wxButton
::SetBackgroundColour(const wxColour
&colour
)
1240 if ( !wxControl
::SetBackgroundColour(colour
) )
1253 bool wxButton
::SetForegroundColour(const wxColour
&colour
)
1255 if ( !wxControl
::SetForegroundColour(colour
) )
1268 bool wxButton
::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
1270 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
1271 HDC hdc
= lpDIS
->hDC
;
1273 UINT state
= lpDIS
->itemState
;
1274 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
1277 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
1279 // draw the button background
1280 if ( !HasFlag(wxBORDER_NONE
) )
1283 if ( wxUxThemeEngine
::GetIfActive() )
1285 DrawXPBackground(this, hdc
, rectBtn
, state
);
1288 #endif // wxUSE_UXTHEME
1290 COLORREF colBg
= wxColourToRGB(GetBackgroundColour());
1292 // first, draw the background
1293 AutoHBRUSH
hbrushBackground(colBg
);
1294 FillRect(hdc
, &rectBtn
, hbrushBackground
);
1296 // draw the border for the current state
1297 bool selected
= (state
& ODS_SELECTED
) != 0;
1301 tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
1304 selected
= tlw
->GetDefaultItem() == this;
1308 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
1311 // draw the focus rectangle if we need it
1312 if ( (state
& ODS_FOCUS
) && !(state
& ODS_NOFOCUSRECT
) )
1314 DrawFocusRect(hdc
, &rectBtn
);
1317 if ( !wxUxThemeEngine
::GetIfActive() )
1318 #endif // wxUSE_UXTHEME
1322 // the label is shifted by 1 pixel to create "pushed" effect
1323 OffsetRect(&rectBtn
, 1, 1);
1330 // draw the image, if any
1333 wxBitmap bmp
= m_imageData
->GetBitmap(GetButtonState(this, state
));
1335 bmp
= m_imageData
->GetBitmap(State_Normal
);
1337 const wxSize sizeBmp
= bmp
.GetSize();
1338 const wxSize margin
= m_imageData
->GetBitmapMargins();
1339 const wxSize
sizeBmpWithMargins(sizeBmp
+ 2*margin
);
1340 wxRect
rectButton(wxRectFromRECT(rectBtn
));
1342 // for simplicity, we start with centred rectangle and then move it to
1343 // the appropriate edge
1344 wxRect rectBitmap
= wxRect(sizeBmp
).CentreIn(rectButton
);
1346 // move bitmap only if we have a label, otherwise keep it centered
1349 switch ( m_imageData
->GetBitmapPosition() )
1352 wxFAIL_MSG( "invalid direction" );
1356 rectBitmap
.x
= rectButton
.x
+ margin
.x
;
1357 rectButton
.x
+= sizeBmpWithMargins
.x
;
1358 rectButton
.width
-= sizeBmpWithMargins
.x
;
1362 rectBitmap
.x
= rectButton
.GetRight() - sizeBmp
.x
- margin
.x
;
1363 rectButton
.width
-= sizeBmpWithMargins
.x
;
1367 rectBitmap
.y
= rectButton
.y
+ margin
.y
;
1368 rectButton
.y
+= sizeBmpWithMargins
.y
;
1369 rectButton
.height
-= sizeBmpWithMargins
.y
;
1373 rectBitmap
.y
= rectButton
.GetBottom() - sizeBmp
.y
- margin
.y
;
1374 rectButton
.height
-= sizeBmpWithMargins
.y
;
1379 wxDCTemp
dst((WXHDC
)hdc
);
1380 dst
.DrawBitmap(bmp
, rectBitmap
.GetPosition(), true);
1382 wxCopyRectToRECT(rectButton
, rectBtn
);
1386 // finally draw the label
1389 COLORREF colFg
= state
& ODS_DISABLED
1390 ?
::GetSysColor(COLOR_GRAYTEXT
)
1391 : wxColourToRGB(GetForegroundColour());
1393 // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000)
1394 // systems but by happy coincidence ODS_NOACCEL is not used under them
1395 // neither so DT_HIDEPREFIX should never be used there
1396 DrawButtonText(hdc
, &rectBtn
, GetLabel(), colFg
,
1397 state
& ODS_NOACCEL ? DT_HIDEPREFIX
: 0);
1403 #endif // wxUSE_BUTTON