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"
41 #include "wx/stockitem.h"
42 #include "wx/tokenzr.h"
43 #include "wx/msw/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if wxUSE_EXTENDED_RTTI
51 WX_DEFINE_FLAGS( wxButtonStyle
)
53 wxBEGIN_FLAGS( wxButtonStyle
)
54 // new style border flags, we put them first to
55 // use them for streaming out
56 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
57 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
58 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
59 wxFLAGS_MEMBER(wxBORDER_RAISED
)
60 wxFLAGS_MEMBER(wxBORDER_STATIC
)
61 wxFLAGS_MEMBER(wxBORDER_NONE
)
63 // old style border flags
64 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
65 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
66 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
67 wxFLAGS_MEMBER(wxRAISED_BORDER
)
68 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
69 wxFLAGS_MEMBER(wxBORDER
)
71 // standard window styles
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
75 wxFLAGS_MEMBER(wxWANTS_CHARS
)
76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
77 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
78 wxFLAGS_MEMBER(wxVSCROLL
)
79 wxFLAGS_MEMBER(wxHSCROLL
)
81 wxFLAGS_MEMBER(wxBU_LEFT
)
82 wxFLAGS_MEMBER(wxBU_RIGHT
)
83 wxFLAGS_MEMBER(wxBU_TOP
)
84 wxFLAGS_MEMBER(wxBU_BOTTOM
)
85 wxFLAGS_MEMBER(wxBU_EXACTFIT
)
86 wxEND_FLAGS( wxButtonStyle
)
88 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton
, wxControl
,"wx/button.h")
90 wxBEGIN_PROPERTIES_TABLE(wxButton
)
91 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEvent
)
93 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
94 wxPROPERTY( Label
, wxString
, SetLabel
, GetLabel
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
96 wxPROPERTY_FLAGS( WindowStyle
, wxButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
98 wxEND_PROPERTIES_TABLE()
100 wxBEGIN_HANDLERS_TABLE(wxButton
)
101 wxEND_HANDLERS_TABLE()
103 wxCONSTRUCTOR_6( wxButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
107 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
110 // this macro tries to adjust the default button height to a reasonable value
111 // using the char height as the base
112 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
119 // creation/destruction
120 // ----------------------------------------------------------------------------
122 bool wxButton::Create(wxWindow
*parent
,
128 const wxValidator
& validator
,
129 const wxString
& name
)
132 if (label
.empty() && wxIsStockID(id
))
134 // On Windows, some buttons aren't supposed to have
135 // mnemonics, so strip them out.
137 label
= wxGetStockLabel(id
138 #if defined(__WXMSW__) || defined(__WXWINCE__)
146 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
150 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
153 // if the label contains several lines we must explicitly tell the button
154 // about it or it wouldn't draw it correctly ("\n"s would just appear as
157 // NB: we do it here and not in MSWGetStyle() because we need the label
158 // value and m_label is not set yet when MSWGetStyle() is called;
159 // besides changing BS_MULTILINE during run-time is pointless anyhow
160 if ( label
.find(_T('\n')) != wxString::npos
)
162 msStyle
|= BS_MULTILINE
;
166 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
169 wxButton::~wxButton()
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
179 // buttons never have an external border, they draw their own one
180 WXDWORD msStyle
= wxControl::MSWGetStyle
182 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
185 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
186 // each other in any resizeable dialog which has more than one button in
188 msStyle
|= WS_CLIPSIBLINGS
;
191 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
192 // and wxBU_RIGHT to get BS_CENTER!
193 if ( style
& wxBU_LEFT
)
195 if ( style
& wxBU_RIGHT
)
197 if ( style
& wxBU_TOP
)
199 if ( style
& wxBU_BOTTOM
)
200 msStyle
|= BS_BOTTOM
;
203 if ( style
& wxNO_BORDER
)
205 #endif // __WXWINCE__
211 // ----------------------------------------------------------------------------
212 // size management including autosizing
213 // ----------------------------------------------------------------------------
215 wxSize
wxButton::DoGetBestSize() const
217 wxClientDC
dc(wx_const_cast(wxButton
*, this));
218 dc
.SetFont(GetFont());
222 dc
.GetMultiLineTextExtent(wxStripMenuCodes(GetLabel()), &wBtn
, &hBtn
);
224 // add a margin -- the button is wider than just its label
225 wBtn
+= 3*GetCharWidth();
226 hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hBtn
);
228 // all buttons have at least the standard size unless the user explicitly
229 // wants them to be of smaller size and used wxBU_EXACTFIT style when
230 // creating the button
231 if ( !HasFlag(wxBU_EXACTFIT
) )
233 wxSize sz
= GetDefaultSize();
242 wxSize
best(wBtn
, hBtn
);
248 wxSize
wxButtonBase::GetDefaultSize()
250 static wxSize s_sizeBtn
;
252 if ( s_sizeBtn
.x
== 0 )
255 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
257 // the size of a standard button in the dialog units is 50x14,
258 // translate this to pixels
259 // NB1: the multipliers come from the Windows convention
260 // NB2: the extra +1/+2 were needed to get the size be the same as the
261 // size of the buttons in the standard dialog - I don't know how
262 // this happens, but on my system this size is 75x23 in pixels and
263 // 23*8 isn't even divisible by 14... Would be nice to understand
264 // why these constants are needed though!
265 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
266 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
272 // ----------------------------------------------------------------------------
273 // default button handling
274 // ----------------------------------------------------------------------------
277 "Everything you ever wanted to know about the default buttons" or "Why do we
278 have to do all this?"
280 In MSW the default button should be activated when the user presses Enter
281 and the current control doesn't process Enter itself somehow. This is
282 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
283 Another aspect of "defaultness" is that the default button has different
284 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
285 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
286 be unset if our parent window is not active so it should be unset whenever
287 we lose activation and set back when we regain it.
289 Final complication is that when a button is active, it should be the default
290 one, i.e. pressing Enter on a button always activates it and not another
293 We handle this by maintaining a permanent and a temporary default items in
294 wxControlContainer (both may be NULL). When a button becomes the current
295 control (i.e. gets focus) it sets itself as the temporary default which
296 ensures that it has the right appearance and that Enter will be redirected
297 to it. When the button loses focus, it unsets the temporary default and so
298 the default item will be the permanent default -- that is the default button
299 if any had been set or none otherwise, which is just what we want.
301 NB: all this is quite complicated by now and the worst is that normally
302 it shouldn't be necessary at all as for the normal Windows programs
303 DefWindowProc() and IsDialogMessage() take care of all this
304 automatically -- however in wxWidgets programs this doesn't work for
305 nested hierarchies (i.e. a notebook inside a notebook) for unknown
306 reason and so we have to reproduce all this code ourselves. It would be
307 very nice if we could avoid doing it.
310 // set this button as the (permanently) default one in its panel
311 void wxButton::SetDefault()
313 wxWindow
*parent
= GetParent();
315 wxCHECK_RET( parent
, _T("button without parent?") );
317 // set this one as the default button both for wxWidgets ...
318 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
321 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
322 SetDefaultStyle(this, true);
325 // set this button as being currently default
326 void wxButton::SetTmpDefault()
328 wxWindow
*parent
= GetParent();
330 wxCHECK_RET( parent
, _T("button without parent?") );
332 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
333 parent
->SetTmpDefaultItem(this);
335 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
336 SetDefaultStyle(this, true);
339 // unset this button as currently default, it may still stay permanent default
340 void wxButton::UnsetTmpDefault()
342 wxWindow
*parent
= GetParent();
344 wxCHECK_RET( parent
, _T("button without parent?") );
346 parent
->SetTmpDefaultItem(NULL
);
348 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
350 SetDefaultStyle(this, false);
351 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), true);
356 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
358 // we may be called with NULL pointer -- simpler to do the check here than
359 // in the caller which does wxDynamicCast()
363 // first, let DefDlgProc() know about the new default button
366 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
367 // focus at all any more
368 if ( !wxTheApp
->IsActive() )
371 // look for a panel-like window
372 wxWindow
*win
= btn
->GetParent();
373 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
374 win
= win
->GetParent();
378 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
380 // sending DM_SETDEFID also changes the button style to
381 // BS_DEFPUSHBUTTON so there is nothing more to do
385 // then also change the style as needed
386 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
387 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
389 // don't do it with the owner drawn buttons because it will
390 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
391 // BS_DEFPUSHBUTTON != 0)!
392 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
394 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
395 on
? style
| BS_DEFPUSHBUTTON
396 : style
& ~BS_DEFPUSHBUTTON
,
401 // redraw the button - it will notice itself that it's
402 // [not] the default one [any longer]
406 //else: already has correct style
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 bool wxButton::SendClickEvent()
415 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
416 event
.SetEventObject(this);
418 return ProcessCommand(event
);
421 void wxButton::Command(wxCommandEvent
& event
)
423 ProcessCommand(event
);
426 // ----------------------------------------------------------------------------
427 // event/message handlers
428 // ----------------------------------------------------------------------------
430 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
432 bool processed
= false;
435 // NOTE: Apparently older versions (NT 4?) of the common controls send
436 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
437 // buttons, so in order to send two EVT_BUTTON events we should
438 // catch both types. Currently (Feb 2003) up-to-date versions of
439 // win98, win2k and winXP all send two BN_CLICKED messages for
440 // all button types, so we don't catch BN_DOUBLECLICKED anymore
441 // in order to not get 3 EVT_BUTTON events. If this is a problem
442 // then we need to figure out which version of the comctl32 changed
443 // this behaviour and test for it.
445 case 1: // message came from an accelerator
446 case BN_CLICKED
: // normal buttons send this
447 processed
= SendClickEvent();
454 WXLRESULT
wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
456 // when we receive focus, we want to temporarily become the default button in
457 // our parent panel so that pressing "Enter" would activate us -- and when
458 // losing it we should restore the previous default button as well
459 if ( nMsg
== WM_SETFOCUS
)
463 // let the default processing take place too
465 else if ( nMsg
== WM_KILLFOCUS
)
469 else if ( nMsg
== WM_LBUTTONDBLCLK
)
471 // emulate a click event to force an owner-drawn button to change its
472 // appearance - without this, it won't do it
473 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
475 // and continue with processing the message normally as well
478 // let the base class do all real processing
479 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
482 // ----------------------------------------------------------------------------
483 // owner-drawn buttons support
484 // ----------------------------------------------------------------------------
490 static void DrawButtonText(HDC hdc
,
492 const wxString
& text
,
495 COLORREF colOld
= SetTextColor(hdc
, col
);
496 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
498 if ( text
.find(_T('\n')) != wxString::npos
)
500 // draw multiline label
502 // first we need to compute its bounding rect
504 ::CopyRect(&rc
, pRect
);
505 ::DrawText(hdc
, text
, text
.length(), &rc
, DT_CENTER
| DT_CALCRECT
);
507 // now center this rect inside the entire button area
508 const LONG w
= rc
.right
- rc
.left
;
509 const LONG h
= rc
.bottom
- rc
.top
;
510 rc
.left
= (pRect
->right
- pRect
->left
)/2 - w
/2;
511 rc
.right
= rc
.left
+w
;
512 rc
.top
= (pRect
->bottom
- pRect
->top
)/2 - h
/2;
513 rc
.bottom
= rc
.top
+h
;
515 ::DrawText(hdc
, text
, text
.length(), &rc
, DT_CENTER
);
517 else // single line label
519 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
520 ::DrawText(hdc
, text
, text
.length(), pRect
,
521 DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
524 SetBkMode(hdc
, modeOld
);
525 SetTextColor(hdc
, colOld
);
528 static void DrawRect(HDC hdc
, const RECT
& r
)
530 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
531 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
532 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
533 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
536 void wxButton::MakeOwnerDrawn()
538 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
539 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
542 style
|= BS_OWNERDRAW
;
543 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
547 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
549 if ( !wxControl::SetBackgroundColour(colour
) )
562 bool wxButton::SetForegroundColour(const wxColour
&colour
)
564 if ( !wxControl::SetForegroundColour(colour
) )
578 The button frame looks like this normally:
581 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
582 WH GB H = light grey (LIGHT)
583 WH GB G = dark grey (SHADOW)
584 WH GB B = black (DKSHADOW)
589 When the button is selected, the button becomes like this (the total button
590 size doesn't change):
601 When the button is pushed (while selected) it is like:
613 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
614 bool selected
, bool pushed
)
617 CopyRect(&r
, &rectBtn
);
619 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
620 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
621 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
622 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
624 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
633 (void)SelectObject(hdc
, hpenGrey
);
634 ::InflateRect(&r
, -1, -1);
644 ::InflateRect(&r
, -1, -1);
647 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
648 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
650 (void)SelectObject(hdc
, hpenWhite
);
651 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
652 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
654 (void)SelectObject(hdc
, hpenLightGr
);
655 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
656 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
658 (void)SelectObject(hdc
, hpenGrey
);
659 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
660 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
663 (void)SelectObject(hdc
, hpenOld
);
664 DeleteObject(hpenWhite
);
665 DeleteObject(hpenLightGr
);
666 DeleteObject(hpenGrey
);
667 DeleteObject(hpenBlack
);
670 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
672 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
675 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
677 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
678 colFg
= wxColourToRGB(GetForegroundColour());
680 HDC hdc
= lpDIS
->hDC
;
681 UINT state
= lpDIS
->itemState
;
683 // first, draw the background
684 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
686 FillRect(hdc
, &rectBtn
, hbrushBackground
);
688 // draw the border for the current state
689 bool selected
= (state
& ODS_SELECTED
) != 0;
692 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
695 selected
= panel
->GetDefaultItem() == this;
698 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
700 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
702 // draw the focus rect if needed
703 if ( state
& ODS_FOCUS
)
706 CopyRect(&rectFocus
, &rectBtn
);
708 // I don't know where does this constant come from, but this is how
709 // Windows draws them
710 InflateRect(&rectFocus
, -4, -4);
712 DrawFocusRect(hdc
, &rectFocus
);
717 // the label is shifted by 1 pixel to create "pushed" effect
718 OffsetRect(&rectBtn
, 1, 1);
721 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
722 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
725 ::DeleteObject(hbrushBackground
);
732 #endif // wxUSE_BUTTON