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/imaglist.h"
43 #include "wx/stockitem.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/private/button.h"
46 #include "wx/msw/private/dc.h"
48 using namespace wxMSWImpl
;
51 #include "wx/msw/uxtheme.h"
53 // no need to include tmschema.h
55 #define BP_PUSHBUTTON 1
60 #define PBS_DISABLED 4
61 #define PBS_DEFAULTED 5
63 #define TMT_CONTENTMARGINS 3602
66 // provide the necessary declarations ourselves if they're missing from
68 #ifndef BCM_SETIMAGELIST
69 #define BCM_SETIMAGELIST 0x1602
70 #define BCM_SETTEXTMARGIN 0x1604
74 BUTTON_IMAGELIST_ALIGN_LEFT
,
75 BUTTON_IMAGELIST_ALIGN_RIGHT
,
76 BUTTON_IMAGELIST_ALIGN_TOP
,
77 BUTTON_IMAGELIST_ALIGN_BOTTOM
80 struct BUTTON_IMAGELIST
87 #endif // wxUSE_UXTHEME
89 #ifndef WM_THEMECHANGED
90 #define WM_THEMECHANGED 0x031A
94 #define ODS_NOACCEL 0x0100
97 #ifndef ODS_NOFOCUSRECT
98 #define ODS_NOFOCUSRECT 0x0200
101 #ifndef DT_HIDEPREFIX
102 #define DT_HIDEPREFIX 0x00100000
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // we use different data classes for owner drawn buttons and for themed XP ones
111 class wxButtonImageData
114 wxButtonImageData() { }
115 virtual ~wxButtonImageData() { }
117 virtual wxBitmap
GetBitmap(wxButton::State which
) const = 0;
118 virtual void SetBitmap(const wxBitmap
& bitmap
, wxButton::State which
) = 0;
120 virtual wxSize
GetBitmapMargins() const = 0;
121 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
) = 0;
123 virtual wxDirection
GetBitmapPosition() const = 0;
124 virtual void SetBitmapPosition(wxDirection dir
) = 0;
127 wxDECLARE_NO_COPY_CLASS(wxButtonImageData
);
133 // the gap between button edge and the interior area used by Windows for the
135 const int OD_BUTTON_MARGIN
= 4;
137 class wxODButtonImageData
: public wxButtonImageData
140 wxODButtonImageData(wxButton
*btn
, const wxBitmap
& bitmap
)
142 SetBitmap(bitmap
, wxButton::State_Normal
);
146 // we use margins when we have both bitmap and text, but when we have
147 // only the bitmap it should take up the entire button area
148 if ( !btn
->GetLabel().empty() )
150 m_margin
.x
= btn
->GetCharWidth();
151 m_margin
.y
= btn
->GetCharHeight() / 2;
155 virtual wxBitmap
GetBitmap(wxButton::State which
) const
157 return m_bitmaps
[which
];
160 virtual void SetBitmap(const wxBitmap
& bitmap
, wxButton::State which
)
162 m_bitmaps
[which
] = bitmap
;
165 virtual wxSize
GetBitmapMargins() const
170 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
172 m_margin
= wxSize(x
, y
);
175 virtual wxDirection
GetBitmapPosition() const
180 virtual void SetBitmapPosition(wxDirection dir
)
186 // just store the values passed to us to be able to retrieve them later
187 // from the drawing code
188 wxBitmap m_bitmaps
[wxButton::State_Max
];
192 wxDECLARE_NO_COPY_CLASS(wxODButtonImageData
);
197 // somehow the margin is one pixel greater than the value returned by
198 // GetThemeMargins() call
199 const int XP_BUTTON_EXTRA_MARGIN
= 1;
201 class wxXPButtonImageData
: public wxButtonImageData
204 // we must be constructed with the size of our images as we need to create
206 wxXPButtonImageData(wxButton
*btn
, const wxBitmap
& bitmap
)
207 : m_iml(bitmap
.GetWidth(), bitmap
.GetHeight(), true /* use mask */,
208 wxButton::State_Max
),
209 m_hwndBtn(GetHwndOf(btn
))
211 // initialize all bitmaps to normal state
212 for ( int n
= 0; n
< wxButton::State_Max
; n
++ )
217 m_data
.himl
= GetHimagelistOf(&m_iml
);
219 // use default margins
221 m_data
.margin
.right
= btn
->GetCharWidth();
223 m_data
.margin
.bottom
= btn
->GetCharHeight() / 2;
225 // and default alignment
226 m_data
.uAlign
= BUTTON_IMAGELIST_ALIGN_LEFT
;
231 virtual wxBitmap
GetBitmap(wxButton::State which
) const
233 return m_iml
.GetBitmap(which
);
236 virtual void SetBitmap(const wxBitmap
& bitmap
, wxButton::State which
)
238 m_iml
.Replace(which
, bitmap
);
243 virtual wxSize
GetBitmapMargins() const
245 return wxSize(m_data
.margin
.left
, m_data
.margin
.top
);
248 virtual void SetBitmapMargins(wxCoord x
, wxCoord y
)
250 RECT
& margin
= m_data
.margin
;
256 if ( !::SendMessage(m_hwndBtn
, BCM_SETTEXTMARGIN
, 0, (LPARAM
)&margin
) )
258 wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
262 virtual wxDirection
GetBitmapPosition() const
264 switch ( m_data
.uAlign
)
267 wxFAIL_MSG( "invalid image alignment" );
270 case BUTTON_IMAGELIST_ALIGN_LEFT
:
273 case BUTTON_IMAGELIST_ALIGN_RIGHT
:
276 case BUTTON_IMAGELIST_ALIGN_TOP
:
279 case BUTTON_IMAGELIST_ALIGN_BOTTOM
:
284 virtual void SetBitmapPosition(wxDirection dir
)
290 wxFAIL_MSG( "invalid direction" );
294 alignNew
= BUTTON_IMAGELIST_ALIGN_LEFT
;
298 alignNew
= BUTTON_IMAGELIST_ALIGN_RIGHT
;
302 alignNew
= BUTTON_IMAGELIST_ALIGN_TOP
;
306 alignNew
= BUTTON_IMAGELIST_ALIGN_BOTTOM
;
310 if ( alignNew
!= m_data
.uAlign
)
312 m_data
.uAlign
= alignNew
;
318 void UpdateImageInfo()
320 if ( !::SendMessage(m_hwndBtn
, BCM_SETIMAGELIST
, 0, (LPARAM
)&m_data
) )
322 wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
326 // we store image list separately to be able to use convenient wxImageList
327 // methods instead of working with raw HIMAGELIST
330 // store the rest of the data in BCM_SETIMAGELIST-friendly form
331 BUTTON_IMAGELIST m_data
;
333 // the button we're associated with
334 const HWND m_hwndBtn
;
337 wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData
);
340 #endif // wxUSE_UXTHEME
342 } // anonymous namespace
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 #if wxUSE_EXTENDED_RTTI
350 WX_DEFINE_FLAGS( wxButtonStyle
)
352 wxBEGIN_FLAGS( wxButtonStyle
)
353 // new style border flags, we put them first to
354 // use them for streaming out
355 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
356 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
357 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
358 wxFLAGS_MEMBER(wxBORDER_RAISED
)
359 wxFLAGS_MEMBER(wxBORDER_STATIC
)
360 wxFLAGS_MEMBER(wxBORDER_NONE
)
362 // old style border flags
363 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
364 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
365 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
366 wxFLAGS_MEMBER(wxRAISED_BORDER
)
367 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
368 wxFLAGS_MEMBER(wxBORDER
)
370 // standard window styles
371 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
372 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
373 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
374 wxFLAGS_MEMBER(wxWANTS_CHARS
)
375 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
376 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
377 wxFLAGS_MEMBER(wxVSCROLL
)
378 wxFLAGS_MEMBER(wxHSCROLL
)
380 wxFLAGS_MEMBER(wxBU_LEFT
)
381 wxFLAGS_MEMBER(wxBU_RIGHT
)
382 wxFLAGS_MEMBER(wxBU_TOP
)
383 wxFLAGS_MEMBER(wxBU_BOTTOM
)
384 wxFLAGS_MEMBER(wxBU_EXACTFIT
)
385 wxEND_FLAGS( wxButtonStyle
)
387 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton
, wxControl
,"wx/button.h")
389 wxBEGIN_PROPERTIES_TABLE(wxButton
)
390 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEvent
)
392 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
393 wxPROPERTY( Label
, wxString
, SetLabel
, GetLabel
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
395 wxPROPERTY_FLAGS( WindowStyle
, wxButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
397 wxEND_PROPERTIES_TABLE()
399 wxBEGIN_HANDLERS_TABLE(wxButton
)
400 wxEND_HANDLERS_TABLE()
402 wxCONSTRUCTOR_6( wxButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
406 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
409 // ============================================================================
411 // ============================================================================
413 // ----------------------------------------------------------------------------
414 // helper functions from wx/msw/private/button.h
415 // ----------------------------------------------------------------------------
417 void wxMSWButton::UpdateMultilineStyle(HWND hwnd
, const wxString
& label
)
419 // update BS_MULTILINE style depending on the new label (resetting it
420 // doesn't seem to do anything very useful but it shouldn't hurt and we do
421 // have to set it whenever the label becomes multi line as otherwise it
422 // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
423 // the control unless it already has new lines in its label)
424 long styleOld
= ::GetWindowLong(hwnd
, GWL_STYLE
),
426 if ( label
.find(_T('\n')) != wxString::npos
)
427 styleNew
= styleOld
| BS_MULTILINE
;
429 styleNew
= styleOld
& ~BS_MULTILINE
;
431 if ( styleNew
!= styleOld
)
432 ::SetWindowLong(hwnd
, GWL_STYLE
, styleNew
);
435 wxSize
wxMSWButton::GetFittingSize(wxWindow
*win
, const wxSize
& sizeLabel
)
437 // FIXME: this is pure guesswork, need to retrieve the real button margins
438 wxSize sizeBtn
= sizeLabel
;
440 sizeBtn
.x
+= 3*win
->GetCharWidth();
441 sizeBtn
.y
= 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(sizeLabel
.y
)/10;
446 wxSize
wxMSWButton::ComputeBestSize(wxControl
*btn
)
451 dc
.GetMultiLineTextExtent(btn
->GetLabelText(), &sizeBtn
.x
, &sizeBtn
.y
);
453 sizeBtn
= GetFittingSize(btn
, sizeBtn
);
455 // all buttons have at least the standard size unless the user explicitly
456 // wants them to be of smaller size and used wxBU_EXACTFIT style when
457 // creating the button
458 if ( !btn
->HasFlag(wxBU_EXACTFIT
) )
460 wxSize sizeDef
= wxButton::GetDefaultSize();
461 if ( sizeBtn
.x
< sizeDef
.x
)
462 sizeBtn
.x
= sizeDef
.x
;
463 if ( sizeBtn
.y
< sizeDef
.y
)
464 sizeBtn
.y
= sizeDef
.y
;
467 btn
->CacheBestSize(sizeBtn
);
472 // ----------------------------------------------------------------------------
473 // creation/destruction
474 // ----------------------------------------------------------------------------
476 bool wxButton::Create(wxWindow
*parent
,
482 const wxValidator
& validator
,
483 const wxString
& name
)
486 if (label
.empty() && wxIsStockID(id
))
488 // On Windows, some buttons aren't supposed to have mnemonics
489 label
= wxGetStockLabel
492 id
== wxID_OK
|| id
== wxID_CANCEL
|| id
== wxID_CLOSE
494 : wxSTOCK_WITH_MNEMONIC
498 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
502 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
504 // if the label contains several lines we must explicitly tell the button
505 // about it or it wouldn't draw it correctly ("\n"s would just appear as
508 // NB: we do it here and not in MSWGetStyle() because we need the label
509 // value and the label is not set yet when MSWGetStyle() is called
510 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
512 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
515 wxButton::~wxButton()
517 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
518 if ( tlw
&& tlw
->GetTmpDefaultItem() == this )
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
530 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
532 // buttons never have an external border, they draw their own one
533 WXDWORD msStyle
= wxControl::MSWGetStyle
535 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
538 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
539 // each other in any resizeable dialog which has more than one button in
541 msStyle
|= WS_CLIPSIBLINGS
;
543 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
544 // and wxBU_RIGHT to get BS_CENTER!
545 if ( style
& wxBU_LEFT
)
547 if ( style
& wxBU_RIGHT
)
549 if ( style
& wxBU_TOP
)
551 if ( style
& wxBU_BOTTOM
)
552 msStyle
|= BS_BOTTOM
;
555 if ( style
& wxNO_BORDER
)
557 #endif // __WXWINCE__
562 void wxButton::SetLabel(const wxString
& label
)
564 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
566 wxButtonBase::SetLabel(label
);
569 // ----------------------------------------------------------------------------
570 // size management including autosizing
571 // ----------------------------------------------------------------------------
573 wxSize
wxButton::DoGetBestSize() const
577 // account for the text part
578 if ( !GetLabel().empty() )
580 size
= wxMSWButton::ComputeBestSize(const_cast<wxButton
*>(this));
585 // account for the bitmap size
586 const wxSize sizeBmp
= m_imageData
->GetBitmap(State_Normal
).GetSize();
587 const wxDirection dirBmp
= m_imageData
->GetBitmapPosition();
588 if ( dirBmp
== wxLEFT
|| dirBmp
== wxRIGHT
)
591 if ( sizeBmp
.y
> size
.y
)
594 else // bitmap on top/below the text
597 if ( sizeBmp
.x
> size
.x
)
601 // account for the user-specified margins
602 size
+= 2*m_imageData
->GetBitmapMargins();
604 // and also for the margins we always add internally
608 if ( wxUxThemeEngine::GetIfActive() )
610 wxUxThemeHandle
theme(const_cast<wxButton
*>(this), L
"BUTTON");
613 wxUxThemeEngine::Get()->GetThemeMargins(theme
, NULL
,
614 BP_PUSHBUTTON
, PBS_NORMAL
,
615 TMT_CONTENTMARGINS
, NULL
,
618 // XP doesn't draw themed buttons correctly when the client area is
619 // smaller than 8x8 - enforce this minimum size for small bitmaps
620 size
.IncTo(wxSize(8, 8));
622 // don't add margins for the borderless buttons, they don't need
623 // them and it just makes them appear larger than needed
624 if ( !HasFlag(wxBORDER_NONE
) )
626 marginH
= margins
.cxLeftWidth
+ margins
.cxRightWidth
627 + 2*XP_BUTTON_EXTRA_MARGIN
;
628 marginV
= margins
.cyTopHeight
+ margins
.cyBottomHeight
629 + 2*XP_BUTTON_EXTRA_MARGIN
;
633 #endif // wxUSE_UXTHEME
636 marginV
= OD_BUTTON_MARGIN
;
639 size
.IncBy(marginH
, marginV
);
648 wxSize
wxButtonBase::GetDefaultSize()
650 static wxSize s_sizeBtn
;
652 if ( s_sizeBtn
.x
== 0 )
655 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
657 // the size of a standard button in the dialog units is 50x14,
658 // translate this to pixels
659 // NB1: the multipliers come from the Windows convention
660 // NB2: the extra +1/+2 were needed to get the size be the same as the
661 // size of the buttons in the standard dialog - I don't know how
662 // this happens, but on my system this size is 75x23 in pixels and
663 // 23*8 isn't even divisible by 14... Would be nice to understand
664 // why these constants are needed though!
665 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
666 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
672 // ----------------------------------------------------------------------------
673 // default button handling
674 // ----------------------------------------------------------------------------
677 "Everything you ever wanted to know about the default buttons" or "Why do we
678 have to do all this?"
680 In MSW the default button should be activated when the user presses Enter
681 and the current control doesn't process Enter itself somehow. This is
682 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
683 Another aspect of "defaultness" is that the default button has different
684 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
685 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
686 be unset if our parent window is not active so it should be unset whenever
687 we lose activation and set back when we regain it.
689 Final complication is that when a button is active, it should be the default
690 one, i.e. pressing Enter on a button always activates it and not another
693 We handle this by maintaining a permanent and a temporary default items in
694 wxControlContainer (both may be NULL). When a button becomes the current
695 control (i.e. gets focus) it sets itself as the temporary default which
696 ensures that it has the right appearance and that Enter will be redirected
697 to it. When the button loses focus, it unsets the temporary default and so
698 the default item will be the permanent default -- that is the default button
699 if any had been set or none otherwise, which is just what we want.
701 NB: all this is quite complicated by now and the worst is that normally
702 it shouldn't be necessary at all as for the normal Windows programs
703 DefWindowProc() and IsDialogMessage() take care of all this
704 automatically -- however in wxWidgets programs this doesn't work for
705 nested hierarchies (i.e. a notebook inside a notebook) for unknown
706 reason and so we have to reproduce all this code ourselves. It would be
707 very nice if we could avoid doing it.
710 // set this button as the (permanently) default one in its panel
711 wxWindow
*wxButton::SetDefault()
713 // set this one as the default button both for wxWidgets ...
714 wxWindow
*winOldDefault
= wxButtonBase::SetDefault();
717 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
718 SetDefaultStyle(this, true);
720 return winOldDefault
;
723 // return the top level parent window if it's not being deleted yet, otherwise
725 static wxTopLevelWindow
*GetTLWParentIfNotBeingDeleted(wxWindow
*win
)
729 // IsTopLevel() will return false for a wxTLW being deleted, so we also
730 // need the parent test for this case
731 wxWindow
* const parent
= win
->GetParent();
732 if ( !parent
|| win
->IsTopLevel() )
734 if ( win
->IsBeingDeleted() )
743 wxASSERT_MSG( win
, _T("button without top level parent?") );
745 wxTopLevelWindow
* const tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
746 wxASSERT_MSG( tlw
, _T("logic error in GetTLWParentIfNotBeingDeleted()") );
751 // set this button as being currently default
752 void wxButton::SetTmpDefault()
754 wxTopLevelWindow
* const tlw
= GetTLWParentIfNotBeingDeleted(GetParent());
758 wxWindow
*winOldDefault
= tlw
->GetDefaultItem();
759 tlw
->SetTmpDefaultItem(this);
761 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
762 SetDefaultStyle(this, true);
765 // unset this button as currently default, it may still stay permanent default
766 void wxButton::UnsetTmpDefault()
768 wxTopLevelWindow
* const tlw
= GetTLWParentIfNotBeingDeleted(GetParent());
772 tlw
->SetTmpDefaultItem(NULL
);
774 wxWindow
*winOldDefault
= tlw
->GetDefaultItem();
776 SetDefaultStyle(this, false);
777 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), true);
782 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
784 // we may be called with NULL pointer -- simpler to do the check here than
785 // in the caller which does wxDynamicCast()
789 // first, let DefDlgProc() know about the new default button
792 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
793 // focus at all any more
794 if ( !wxTheApp
->IsActive() )
797 wxWindow
* const tlw
= wxGetTopLevelParent(btn
);
798 wxCHECK_RET( tlw
, _T("button without top level window?") );
800 ::SendMessage(GetHwndOf(tlw
), DM_SETDEFID
, btn
->GetId(), 0L);
802 // sending DM_SETDEFID also changes the button style to
803 // BS_DEFPUSHBUTTON so there is nothing more to do
806 // then also change the style as needed
807 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
808 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
810 // don't do it with the owner drawn buttons because it will
811 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
812 // BS_DEFPUSHBUTTON != 0)!
813 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
815 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
816 on
? style
| BS_DEFPUSHBUTTON
817 : style
& ~BS_DEFPUSHBUTTON
,
822 // redraw the button - it will notice itself that it's
823 // [not] the default one [any longer]
827 //else: already has correct style
830 // ----------------------------------------------------------------------------
832 // ----------------------------------------------------------------------------
834 bool wxButton::SendClickEvent()
836 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
837 event
.SetEventObject(this);
839 return ProcessCommand(event
);
842 void wxButton::Command(wxCommandEvent
& event
)
844 ProcessCommand(event
);
847 // ----------------------------------------------------------------------------
848 // event/message handlers
849 // ----------------------------------------------------------------------------
851 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
853 bool processed
= false;
856 // NOTE: Apparently older versions (NT 4?) of the common controls send
857 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
858 // buttons, so in order to send two EVT_BUTTON events we should
859 // catch both types. Currently (Feb 2003) up-to-date versions of
860 // win98, win2k and winXP all send two BN_CLICKED messages for
861 // all button types, so we don't catch BN_DOUBLECLICKED anymore
862 // in order to not get 3 EVT_BUTTON events. If this is a problem
863 // then we need to figure out which version of the comctl32 changed
864 // this behaviour and test for it.
866 case 1: // message came from an accelerator
867 case BN_CLICKED
: // normal buttons send this
868 processed
= SendClickEvent();
875 WXLRESULT
wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
877 // when we receive focus, we want to temporarily become the default button in
878 // our parent panel so that pressing "Enter" would activate us -- and when
879 // losing it we should restore the previous default button as well
880 if ( nMsg
== WM_SETFOCUS
)
884 // let the default processing take place too
886 else if ( nMsg
== WM_KILLFOCUS
)
890 else if ( nMsg
== WM_LBUTTONDBLCLK
)
892 // emulate a click event to force an owner-drawn button to change its
893 // appearance - without this, it won't do it
894 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
896 // and continue with processing the message normally as well
899 else if ( nMsg
== WM_THEMECHANGED
)
901 // need to recalculate the best size here
902 // as the theme size might have changed
903 InvalidateBestSize();
905 #endif // wxUSE_UXTHEME
906 // must use m_mouseInWindow here instead of IsMouseInWindow()
907 // since we need to know the first time the mouse enters the window
908 // and IsMouseInWindow() would return true in this case
909 else if ( (nMsg
== WM_MOUSEMOVE
&& !m_mouseInWindow
) ||
910 nMsg
== WM_MOUSELEAVE
)
916 wxUxThemeEngine::GetIfActive() ||
917 #endif // wxUSE_UXTHEME
918 m_imageData
&& m_imageData
->GetBitmap(State_Current
).IsOk()
926 // let the base class do all real processing
927 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
930 // ----------------------------------------------------------------------------
932 // ----------------------------------------------------------------------------
934 wxBitmap
wxButton::DoGetBitmap(State which
) const
936 return m_imageData
? m_imageData
->GetBitmap(which
) : wxBitmap();
939 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
941 // allocate the image data when the first bitmap is set
945 // using image list doesn't work correctly if we don't have any label
946 // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
947 // BS_BITMAP style), at least under Windows 2003 so use owner drawn
948 // strategy for bitmap-only buttons
949 if ( !GetLabel().empty() && wxUxThemeEngine::GetIfActive() )
951 m_imageData
= new wxXPButtonImageData(this, bitmap
);
954 #endif // wxUSE_UXTHEME
956 m_imageData
= new wxODButtonImageData(this, bitmap
);
960 // if a bitmap was assigned to the bitmap, its best size must be
961 // changed to account for it
962 InvalidateBestSize();
966 m_imageData
->SetBitmap(bitmap
, which
);
972 wxSize
wxButton::DoGetBitmapMargins() const
974 return m_imageData
? m_imageData
->GetBitmapMargins() : wxSize(0, 0);
977 void wxButton::DoSetBitmapMargins(wxCoord x
, wxCoord y
)
979 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
981 m_imageData
->SetBitmapMargins(x
, y
);
984 void wxButton::DoSetBitmapPosition(wxDirection dir
)
986 wxCHECK_RET( m_imageData
, "SetBitmap() must be called first" );
988 m_imageData
->SetBitmapPosition(dir
);
991 // ----------------------------------------------------------------------------
992 // owner-drawn buttons support
993 // ----------------------------------------------------------------------------
999 // return the button state using both the ODS_XXX flags specified in state
1000 // parameter and the current button state
1001 wxButton::State
GetButtonState(wxButton
*btn
, UINT state
)
1003 if ( state
& ODS_DISABLED
)
1004 return wxButton::State_Disabled
;
1006 if ( state
& ODS_SELECTED
)
1007 return wxButton::State_Pressed
;
1009 if ( btn
->HasCapture() || btn
->IsMouseInWindow() )
1010 return wxButton::State_Current
;
1012 if ( state
& ODS_FOCUS
)
1013 return wxButton::State_Focused
;
1015 return wxButton::State_Normal
;
1018 void DrawButtonText(HDC hdc
,
1020 const wxString
& text
,
1024 wxTextColoursChanger
changeFg(hdc
, col
, CLR_INVALID
);
1025 wxBkModeChanger
changeBkMode(hdc
, wxBRUSHSTYLE_TRANSPARENT
);
1027 // center text horizontally in any case
1030 if ( text
.find(_T('\n')) != wxString::npos
)
1032 // draw multiline label
1034 // first we need to compute its bounding rect
1036 ::CopyRect(&rc
, pRect
);
1037 ::DrawText(hdc
, text
.wx_str(), text
.length(), &rc
,
1038 DT_CENTER
| DT_CALCRECT
);
1040 // now center this rect inside the entire button area
1041 const LONG w
= rc
.right
- rc
.left
;
1042 const LONG h
= rc
.bottom
- rc
.top
;
1043 rc
.left
= (pRect
->right
- pRect
->left
)/2 - w
/2;
1044 rc
.right
= rc
.left
+w
;
1045 rc
.top
= (pRect
->bottom
- pRect
->top
)/2 - h
/2;
1046 rc
.bottom
= rc
.top
+h
;
1048 ::DrawText(hdc
, text
.wx_str(), text
.length(), &rc
, flags
);
1050 else // single line label
1052 // centre text vertically too (notice that we must have DT_SINGLELINE
1053 // for DT_VCENTER to work)
1054 ::DrawText(hdc
, text
.wx_str(), text
.length(), pRect
,
1055 flags
| DT_SINGLELINE
| DT_VCENTER
);
1059 void DrawRect(HDC hdc
, const RECT
& r
)
1061 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
1062 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
1063 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
1064 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
1068 The button frame looks like this normally:
1071 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
1072 WH GB H = light grey (LIGHT)
1073 WH GB G = dark grey (SHADOW)
1074 WH GB B = black (DKSHADOW)
1079 When the button is selected, the button becomes like this (the total button
1080 size doesn't change):
1091 When the button is pushed (while selected) it is like:
1102 void DrawButtonFrame(HDC hdc
, RECT
& rectBtn
,
1103 bool selected
, bool pushed
)
1106 CopyRect(&r
, &rectBtn
);
1108 AutoHPEN
hpenBlack(GetSysColor(COLOR_3DDKSHADOW
)),
1109 hpenGrey(GetSysColor(COLOR_3DSHADOW
)),
1110 hpenLightGr(GetSysColor(COLOR_3DLIGHT
)),
1111 hpenWhite(GetSysColor(COLOR_3DHILIGHT
));
1113 SelectInHDC
selectPen(hdc
, hpenBlack
);
1122 (void)SelectObject(hdc
, hpenGrey
);
1123 ::InflateRect(&r
, -1, -1);
1133 ::InflateRect(&r
, -1, -1);
1136 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
1137 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
1139 (void)SelectObject(hdc
, hpenWhite
);
1140 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
1141 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
1143 (void)SelectObject(hdc
, hpenLightGr
);
1144 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
1145 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
1147 (void)SelectObject(hdc
, hpenGrey
);
1148 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
1149 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
1152 InflateRect(&rectBtn
, -OD_BUTTON_MARGIN
, -OD_BUTTON_MARGIN
);
1156 void DrawXPBackground(wxButton
*button
, HDC hdc
, RECT
& rectBtn
, UINT state
)
1158 wxUxThemeHandle
theme(button
, L
"BUTTON");
1160 // this array is indexed by wxButton::State values and so must be kept in
1162 static const int uxStates
[] =
1164 PBS_NORMAL
, PBS_HOT
, PBS_PRESSED
, PBS_DISABLED
, PBS_DEFAULTED
1167 int iState
= uxStates
[GetButtonState(button
, state
)];
1169 wxUxThemeEngine
* const engine
= wxUxThemeEngine::Get();
1171 // draw parent background if needed
1172 if ( engine
->IsThemeBackgroundPartiallyTransparent
1179 engine
->DrawThemeParentBackground(GetHwndOf(button
), hdc
, &rectBtn
);
1183 engine
->DrawThemeBackground(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1186 // calculate content area margins
1188 engine
->GetThemeMargins(theme
, hdc
, BP_PUSHBUTTON
, iState
,
1189 TMT_CONTENTMARGINS
, &rectBtn
, &margins
);
1190 ::InflateRect(&rectBtn
, -margins
.cxLeftWidth
, -margins
.cyTopHeight
);
1191 ::InflateRect(&rectBtn
, -XP_BUTTON_EXTRA_MARGIN
, -XP_BUTTON_EXTRA_MARGIN
);
1193 if ( button
->UseBgCol() )
1195 COLORREF colBg
= wxColourToRGB(button
->GetBackgroundColour());
1196 AutoHBRUSH
hbrushBackground(colBg
);
1198 // don't overwrite the focus rect
1200 ::CopyRect(&rectClient
, &rectBtn
);
1201 ::InflateRect(&rectClient
, -1, -1);
1202 FillRect(hdc
, &rectClient
, hbrushBackground
);
1205 #endif // wxUSE_UXTHEME
1207 } // anonymous namespace
1209 // ----------------------------------------------------------------------------
1210 // owner drawn buttons support
1211 // ----------------------------------------------------------------------------
1213 void wxButton::MakeOwnerDrawn()
1215 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
1216 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
1219 style
|= BS_OWNERDRAW
;
1220 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
1224 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
1226 if ( !wxControl::SetBackgroundColour(colour
) )
1239 bool wxButton::SetForegroundColour(const wxColour
&colour
)
1241 if ( !wxControl::SetForegroundColour(colour
) )
1254 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
1256 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
1257 HDC hdc
= lpDIS
->hDC
;
1259 UINT state
= lpDIS
->itemState
;
1260 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
1263 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
1265 const wxString label
= GetLabel();
1267 // draw the button background
1269 if ( wxUxThemeEngine::GetIfActive() )
1271 DrawXPBackground(this, hdc
, rectBtn
, state
);
1274 #endif // wxUSE_UXTHEME
1276 COLORREF colBg
= wxColourToRGB(GetBackgroundColour());
1278 // first, draw the background
1279 AutoHBRUSH
hbrushBackground(colBg
);
1280 FillRect(hdc
, &rectBtn
, hbrushBackground
);
1282 // draw the border for the current state
1283 bool selected
= (state
& ODS_SELECTED
) != 0;
1287 tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
1290 selected
= tlw
->GetDefaultItem() == this;
1294 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
1297 // draw the focus rectangle if we need it
1298 if ( (state
& ODS_FOCUS
) && !(state
& ODS_NOFOCUSRECT
) )
1300 DrawFocusRect(hdc
, &rectBtn
);
1303 if ( !wxUxThemeEngine::GetIfActive() )
1304 #endif // wxUSE_UXTHEME
1308 // the label is shifted by 1 pixel to create "pushed" effect
1309 OffsetRect(&rectBtn
, 1, 1);
1315 // draw the image, if any
1318 wxBitmap bmp
= m_imageData
->GetBitmap(GetButtonState(this, state
));
1320 bmp
= m_imageData
->GetBitmap(State_Normal
);
1322 const wxSize sizeBmp
= bmp
.GetSize();
1323 const wxSize margin
= m_imageData
->GetBitmapMargins();
1324 const wxSize
sizeBmpWithMargins(sizeBmp
+ 2*margin
);
1325 wxRect
rectButton(wxRectFromRECT(rectBtn
));
1327 // for simplicity, we start with centred rectangle and then move it to
1328 // the appropriate edge
1329 wxRect rectBitmap
= wxRect(sizeBmp
).CentreIn(rectButton
);
1330 switch ( m_imageData
->GetBitmapPosition() )
1333 wxFAIL_MSG( "invalid direction" );
1337 rectBitmap
.x
= rectButton
.x
+ margin
.x
;
1338 rectButton
.x
+= sizeBmpWithMargins
.x
;
1339 rectButton
.width
-= sizeBmpWithMargins
.x
;
1343 rectBitmap
.x
= rectButton
.GetRight() - sizeBmp
.x
- margin
.x
;
1344 rectButton
.width
-= sizeBmpWithMargins
.x
;
1348 rectBitmap
.y
= rectButton
.y
+ margin
.y
;
1349 rectButton
.y
+= sizeBmpWithMargins
.y
;
1350 rectButton
.height
-= sizeBmpWithMargins
.y
;
1354 rectBitmap
.y
= rectButton
.GetBottom() - sizeBmp
.y
- margin
.y
;
1355 rectButton
.height
-= sizeBmpWithMargins
.y
;
1359 wxDCTemp
dst((WXHDC
)hdc
);
1360 dst
.DrawBitmap(bmp
, rectBitmap
.GetPosition(), true);
1362 wxCopyRectToRECT(rectButton
, rectBtn
);
1366 // finally draw the label
1367 if ( !label
.empty() )
1369 COLORREF colFg
= state
& ODS_DISABLED
1370 ? ::GetSysColor(COLOR_GRAYTEXT
)
1371 : wxColourToRGB(GetForegroundColour());
1373 // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000)
1374 // systems but by happy coincidence ODS_NOACCEL is not used under them
1375 // neither so DT_HIDEPREFIX should never be used there
1376 DrawButtonText(hdc
, &rectBtn
, label
, colFg
,
1377 state
& ODS_NOACCEL
? DT_HIDEPREFIX
: 0);
1383 #endif // wxUSE_BUTTON