1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/button.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "button.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/button.h"
38 #include "wx/bmpbuttn.h"
39 #include "wx/settings.h"
40 #include "wx/dcscreen.h"
43 #include "wx/msw/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if wxUSE_EXTENDED_RTTI
51 enum wxButtonStyleBits
53 wxButtonExactFitBit
= 0 ,
56 wxButtonRightBit
= 8 ,
57 wxButtonBottomBit
= 9 ,
59 // wxNoFullRepaintOnResizeBit = 16 ,
60 // wxPopUpWindowBit = 17 ,
62 // wxTabTraversalBit = 19 ,
64 wxTransparentWindowBit
= 20 ,
65 wxBorderNoneBit
= 21 ,
66 // wxClipChildrenBit = 22 ,
67 // wxAlwaysShowScrollBarsBit = 23 ,
69 wxBorderStaticBit
= 24 ,
70 wxBorderSimpleBit
= 25 ,
71 wxBorderRaisedBit
= 26 ,
72 wxBorderSunkenBit
= 27 ,
74 wxBorderDoubleBit
= 28 ,
75 // wxCaptionBit = 29 ,
76 // wxClipSiblingsBit = 29 , // caption not used for non toplevel
78 // wxVScrollBit = 31 ,
81 typedef wxFlags
<wxButtonStyleBits
> wxButtonStyleFlags
;
83 WX_BEGIN_ENUM( wxButtonStyleBits
)
84 WX_ENUM_MEMBER( wxButtonExactFitBit
)
85 WX_ENUM_MEMBER( wxButtonLeftBit
)
86 WX_ENUM_MEMBER( wxButtonTopBit
)
87 WX_ENUM_MEMBER( wxButtonRightBit
)
88 WX_ENUM_MEMBER( wxButtonBottomBit
)
89 WX_ENUM_MEMBER( wxWantCharsBit
)
90 WX_ENUM_MEMBER( wxTransparentWindowBit
)
91 WX_ENUM_MEMBER( wxBorderNoneBit
)
92 WX_ENUM_MEMBER( wxBorderStaticBit
)
93 WX_ENUM_MEMBER( wxBorderSimpleBit
)
94 WX_ENUM_MEMBER( wxBorderRaisedBit
)
95 WX_ENUM_MEMBER( wxBorderSunkenBit
)
96 WX_ENUM_MEMBER( wxBorderDoubleBit
)
97 WX_END_ENUM( wxButtonStyleBits
)
99 WX_IMPLEMENT_SET_STREAMING( wxButtonStyleFlags
, wxButtonStyleBits
)
101 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton
, wxControl
,"wx/button.h")
103 WX_BEGIN_PROPERTIES_TABLE(wxButton
)
104 WX_DELEGATE( OnClick
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEvent
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
106 WX_PROPERTY( Font
, wxFont
, SetFont
, GetFont
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
107 WX_PROPERTY( Label
, wxString
, SetLabel
, GetLabel
, wxEmptyString
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
109 WX_PROPERTY_FLAGS( WindowStyle
, wxButtonStyleFlags
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
111 WX_END_PROPERTIES_TABLE()
113 WX_BEGIN_HANDLERS_TABLE(wxButton
)
114 WX_END_HANDLERS_TABLE()
116 WX_CONSTRUCTOR_6( wxButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
120 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
123 // this macro tries to adjust the default button height to a reasonable value
124 // using the char height as the base
125 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
132 // creation/destruction
133 // ----------------------------------------------------------------------------
135 bool wxButton::Create(wxWindow
*parent
,
137 const wxString
& label
,
141 const wxValidator
& validator
,
142 const wxString
& name
)
144 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
148 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
151 // if the label contains several lines we must explicitly tell the button
152 // about it or it wouldn't draw it correctly ("\n"s would just appear as
155 // NB: we do it here and not in MSWGetStyle() because we need the label
156 // value and m_label is not set yet when MSWGetStyle() is called;
157 // besides changing BS_MULTILINE during run-time is pointless anyhow
158 if ( label
.find(_T('\n')) != wxString::npos
)
160 msStyle
|= BS_MULTILINE
;
164 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
167 wxButton::~wxButton()
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
177 // buttons never have an external border, they draw their own one
178 WXDWORD msStyle
= wxControl::MSWGetStyle
180 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
183 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
184 // each other in any resizeable dialog which has more than one button in
186 msStyle
|= WS_CLIPSIBLINGS
;
189 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
190 // and wxBU_RIGHT to get BS_CENTER!
191 if ( style
& wxBU_LEFT
)
193 if ( style
& wxBU_RIGHT
)
195 if ( style
& wxBU_TOP
)
197 if ( style
& wxBU_BOTTOM
)
198 msStyle
|= BS_BOTTOM
;
204 // ----------------------------------------------------------------------------
205 // size management including autosizing
206 // ----------------------------------------------------------------------------
208 wxSize
wxButton::DoGetBestSize() const
211 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn
, NULL
);
214 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
216 // add a margin -- the button is wider than just its label
219 // the button height is proportional to the height of the font used
220 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
222 // all buttons have at least the standard size unless the user explicitly
223 // wants them to be of smaller size and used wxBU_EXACTFIT style when
224 // creating the button
225 if ( !HasFlag(wxBU_EXACTFIT
) )
227 wxSize sz
= GetDefaultSize();
236 return wxSize(wBtn
, hBtn
);
240 wxSize
wxButtonBase::GetDefaultSize()
242 static wxSize s_sizeBtn
;
244 if ( s_sizeBtn
.x
== 0 )
247 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
249 // the size of a standard button in the dialog units is 50x14,
250 // translate this to pixels
251 // NB1: the multipliers come from the Windows convention
252 // NB2: the extra +1/+2 were needed to get the size be the same as the
253 // size of the buttons in the standard dialog - I don't know how
254 // this happens, but on my system this size is 75x23 in pixels and
255 // 23*8 isn't even divisible by 14... Would be nice to understand
256 // why these constants are needed though!
257 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
258 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
264 // ----------------------------------------------------------------------------
265 // default button handling
266 // ----------------------------------------------------------------------------
269 "Everything you ever wanted to know about the default buttons" or "Why do we
270 have to do all this?"
272 In MSW the default button should be activated when the user presses Enter
273 and the current control doesn't process Enter itself somehow. This is
274 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
275 Another aspect of "defaultness" is that the default button has different
276 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
277 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
278 be unset if our parent window is not active so it should be unset whenever
279 we lose activation and set back when we regain it.
281 Final complication is that when a button is active, it should be the default
282 one, i.e. pressing Enter on a button always activates it and not another
285 We handle this by maintaining a permanent and a temporary default items in
286 wxControlContainer (both may be NULL). When a button becomes the current
287 control (i.e. gets focus) it sets itself as the temporary default which
288 ensures that it has the right appearance and that Enter will be redirected
289 to it. When the button loses focus, it unsets the temporary default and so
290 the default item will be the permanent default -- that is the default button
291 if any had been set or none otherwise, which is just what we want.
293 NB: all this is quite complicated by now and the worst is that normally
294 it shouldn't be necessary at all as for the normal Windows programs
295 DefWindowProc() and IsDialogMessage() take care of all this
296 automatically -- however in wxWindows programs this doesn't work for
297 nested hierarchies (i.e. a notebook inside a notebook) for unknown
298 reason and so we have to reproduce all this code ourselves. It would be
299 very nice if we could avoid doing it.
302 // set this button as the (permanently) default one in its panel
303 void wxButton::SetDefault()
305 wxWindow
*parent
= GetParent();
307 wxCHECK_RET( parent
, _T("button without parent?") );
309 // set this one as the default button both for wxWindows ...
310 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
313 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
314 SetDefaultStyle(this, TRUE
);
317 // set this button as being currently default
318 void wxButton::SetTmpDefault()
320 wxWindow
*parent
= GetParent();
322 wxCHECK_RET( parent
, _T("button without parent?") );
324 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
325 parent
->SetTmpDefaultItem(this);
327 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
328 SetDefaultStyle(this, TRUE
);
331 // unset this button as currently default, it may still stay permanent default
332 void wxButton::UnsetTmpDefault()
334 wxWindow
*parent
= GetParent();
336 wxCHECK_RET( parent
, _T("button without parent?") );
338 parent
->SetTmpDefaultItem(NULL
);
340 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
342 SetDefaultStyle(this, FALSE
);
343 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), TRUE
);
348 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
350 // we may be called with NULL pointer -- simpler to do the check here than
351 // in the caller which does wxDynamicCast()
355 // first, let DefDlgProc() know about the new default button
358 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
359 // focus at all any more
360 if ( !wxTheApp
->IsActive() )
363 // look for a panel-like window
364 wxWindow
*win
= btn
->GetParent();
365 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
366 win
= win
->GetParent();
370 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
372 // sending DM_SETDEFID also changes the button style to
373 // BS_DEFPUSHBUTTON so there is nothing more to do
377 // then also change the style as needed
378 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
379 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
381 // don't do it with the owner drawn buttons because it will
382 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
383 // BS_DEFPUSHBUTTON != 0)!
384 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
386 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
387 on
? style
| BS_DEFPUSHBUTTON
388 : style
& ~BS_DEFPUSHBUTTON
,
393 // redraw the button - it will notice itself that it's
394 // [not] the default one [any longer]
398 //else: already has correct style
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 bool wxButton::SendClickEvent()
407 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
408 event
.SetEventObject(this);
410 return ProcessCommand(event
);
413 void wxButton::Command(wxCommandEvent
& event
)
415 ProcessCommand(event
);
418 // ----------------------------------------------------------------------------
419 // event/message handlers
420 // ----------------------------------------------------------------------------
422 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
424 bool processed
= FALSE
;
427 // NOTE: Apparently older versions (NT 4?) of the common controls send
428 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
429 // buttons, so in order to send two EVET_BUTTON events we should
430 // catch both types. Currently (Feb 2003) up-to-date versions of
431 // win98, win2k and winXP all send two BN_CLICKED messages for
432 // all button types, so we don't catch BN_DOUBLECLICKED anymore
433 // in order to not get 3 EVT_BUTTON events. If this is a problem
434 // then we need to figure out which version of the comctl32 changed
435 // this behaviour and test for it.
437 case 1: // message came from an accelerator
438 case BN_CLICKED
: // normal buttons send this
439 processed
= SendClickEvent();
446 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
448 // when we receive focus, we want to temporary become the default button in
449 // our parent panel so that pressing "Enter" would activate us -- and when
450 // losing it we should restore the previous default button as well
451 if ( nMsg
== WM_SETFOCUS
)
455 // let the default processing take place too
457 else if ( nMsg
== WM_KILLFOCUS
)
461 else if ( nMsg
== WM_LBUTTONDBLCLK
)
463 // emulate a click event to force an owner-drawn button to change its
464 // appearance - without this, it won't do it
465 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
467 // and continue with processing the message normally as well
470 // let the base class do all real processing
471 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
474 // ----------------------------------------------------------------------------
475 // owner-drawn buttons support
476 // ----------------------------------------------------------------------------
482 static void DrawButtonText(HDC hdc
,
484 const wxString
& text
,
487 COLORREF colOld
= SetTextColor(hdc
, col
);
488 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
490 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
491 ::DrawText(hdc
, text
, text
.length(), pRect
, DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
493 SetBkMode(hdc
, modeOld
);
494 SetTextColor(hdc
, colOld
);
497 static void DrawRect(HDC hdc
, const RECT
& r
)
499 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
500 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
501 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
502 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
505 void wxButton::MakeOwnerDrawn()
507 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
508 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
511 style
|= BS_OWNERDRAW
;
512 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
516 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
518 if ( !wxControl::SetBackgroundColour(colour
) )
531 bool wxButton::SetForegroundColour(const wxColour
&colour
)
533 if ( !wxControl::SetForegroundColour(colour
) )
547 The button frame looks like this normally:
550 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
551 WH GB H = light grey (LIGHT)
552 WH GB G = dark grey (SHADOW)
553 WH GB B = black (DKSHADOW)
558 When the button is selected, the button becomes like this (the total button
559 size doesn't change):
570 When the button is pushed (while selected) it is like:
582 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
583 bool selected
, bool pushed
)
586 CopyRect(&r
, &rectBtn
);
588 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
589 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
590 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
591 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
593 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
602 (void)SelectObject(hdc
, hpenGrey
);
603 InflateRect(&r
, -1, -1);
613 InflateRect(&r
, -1, -1);
616 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
617 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
619 (void)SelectObject(hdc
, hpenWhite
);
620 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
621 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
623 (void)SelectObject(hdc
, hpenLightGr
);
624 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
625 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
627 (void)SelectObject(hdc
, hpenGrey
);
628 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
629 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
632 (void)SelectObject(hdc
, hpenOld
);
633 DeleteObject(hpenWhite
);
634 DeleteObject(hpenLightGr
);
635 DeleteObject(hpenGrey
);
636 DeleteObject(hpenBlack
);
639 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
641 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
644 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
646 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
647 colFg
= wxColourToRGB(GetForegroundColour());
649 HDC hdc
= lpDIS
->hDC
;
650 UINT state
= lpDIS
->itemState
;
652 // first, draw the background
653 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
655 FillRect(hdc
, &rectBtn
, hbrushBackground
);
657 // draw the border for the current state
658 bool selected
= (state
& ODS_SELECTED
) != 0;
661 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
664 selected
= panel
->GetDefaultItem() == this;
667 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
669 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
671 // draw the focus rect if needed
672 if ( state
& ODS_FOCUS
)
675 CopyRect(&rectFocus
, &rectBtn
);
677 // I don't know where does this constant come from, but this is how
678 // Windows draws them
679 InflateRect(&rectFocus
, -4, -4);
681 DrawFocusRect(hdc
, &rectFocus
);
686 // the label is shifted by 1 pixel to create "pushed" effect
687 OffsetRect(&rectBtn
, 1, 1);
690 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
691 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
694 ::DeleteObject(hbrushBackground
);
701 #endif // wxUSE_BUTTON