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 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
, , 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
, , 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 wxButtonBase::wxButtonBase()
126 bool wxButton::Create(wxWindow
*parent
,
128 const wxString
& label
,
132 const wxValidator
& validator
,
133 const wxString
& name
)
135 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
139 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
142 // if the label contains several lines we must explicitly tell the button
143 // about it or it wouldn't draw it correctly ("\n"s would just appear as
146 // NB: we do it here and not in MSWGetStyle() because we need the label
147 // value and m_label is not set yet when MSWGetStyle() is called;
148 // besides changing BS_MULTILINE during run-time is pointless anyhow
149 if ( label
.find(_T('\n')) != wxString::npos
)
151 msStyle
|= BS_MULTILINE
;
155 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
158 wxButton::~wxButton()
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
168 // buttons never have an external border, they draw their own one
169 WXDWORD msStyle
= wxControl::MSWGetStyle
171 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
174 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
175 // each other in any resizeable dialog which has more than one button in
177 msStyle
|= WS_CLIPSIBLINGS
;
180 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
181 // and wxBU_RIGHT to get BS_CENTER!
182 if ( style
& wxBU_LEFT
)
184 if ( style
& wxBU_RIGHT
)
186 if ( style
& wxBU_TOP
)
188 if ( style
& wxBU_BOTTOM
)
189 msStyle
|= BS_BOTTOM
;
191 if ( style
& wxNO_BORDER
)
198 // ----------------------------------------------------------------------------
199 // size management including autosizing
200 // ----------------------------------------------------------------------------
202 wxSize
wxButton::DoGetBestSize() const
205 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn
, NULL
);
208 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
210 // add a margin -- the button is wider than just its label
213 // the button height is proportional to the height of the font used
214 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
216 // all buttons have at least the standard size unless the user explicitly
217 // wants them to be of smaller size and used wxBU_EXACTFIT style when
218 // creating the button
219 if ( !HasFlag(wxBU_EXACTFIT
) )
221 wxSize sz
= GetDefaultSize();
230 return wxSize(wBtn
, hBtn
);
234 wxSize
wxButtonBase::GetDefaultSize()
236 static wxSize s_sizeBtn
;
238 if ( s_sizeBtn
.x
== 0 )
241 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
243 // the size of a standard button in the dialog units is 50x14,
244 // translate this to pixels
245 // NB1: the multipliers come from the Windows convention
246 // NB2: the extra +1/+2 were needed to get the size be the same as the
247 // size of the buttons in the standard dialog - I don't know how
248 // this happens, but on my system this size is 75x23 in pixels and
249 // 23*8 isn't even divisible by 14... Would be nice to understand
250 // why these constants are needed though!
251 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
252 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
258 // ----------------------------------------------------------------------------
259 // default button handling
260 // ----------------------------------------------------------------------------
263 "Everything you ever wanted to know about the default buttons" or "Why do we
264 have to do all this?"
266 In MSW the default button should be activated when the user presses Enter
267 and the current control doesn't process Enter itself somehow. This is
268 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
269 Another aspect of "defaultness" is that the default button has different
270 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
271 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
272 be unset if our parent window is not active so it should be unset whenever
273 we lose activation and set back when we regain it.
275 Final complication is that when a button is active, it should be the default
276 one, i.e. pressing Enter on a button always activates it and not another
279 We handle this by maintaining a permanent and a temporary default items in
280 wxControlContainer (both may be NULL). When a button becomes the current
281 control (i.e. gets focus) it sets itself as the temporary default which
282 ensures that it has the right appearance and that Enter will be redirected
283 to it. When the button loses focus, it unsets the temporary default and so
284 the default item will be the permanent default -- that is the default button
285 if any had been set or none otherwise, which is just what we want.
287 NB: all this is quite complicated by now and the worst is that normally
288 it shouldn't be necessary at all as for the normal Windows programs
289 DefWindowProc() and IsDialogMessage() take care of all this
290 automatically -- however in wxWindows programs this doesn't work for
291 nested hierarchies (i.e. a notebook inside a notebook) for unknown
292 reason and so we have to reproduce all this code ourselves. It would be
293 very nice if we could avoid doing it.
296 // set this button as the (permanently) default one in its panel
297 void wxButton::SetDefault()
299 wxWindow
*parent
= GetParent();
301 wxCHECK_RET( parent
, _T("button without parent?") );
303 // set this one as the default button both for wxWindows ...
304 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
307 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
308 SetDefaultStyle(this, TRUE
);
311 // set this button as being currently default
312 void wxButton::SetTmpDefault()
314 wxWindow
*parent
= GetParent();
316 wxCHECK_RET( parent
, _T("button without parent?") );
318 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
319 parent
->SetTmpDefaultItem(this);
321 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
322 SetDefaultStyle(this, TRUE
);
325 // unset this button as currently default, it may still stay permanent default
326 void wxButton::UnsetTmpDefault()
328 wxWindow
*parent
= GetParent();
330 wxCHECK_RET( parent
, _T("button without parent?") );
332 parent
->SetTmpDefaultItem(NULL
);
334 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
336 SetDefaultStyle(this, FALSE
);
337 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), TRUE
);
342 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
344 // we may be called with NULL pointer -- simpler to do the check here than
345 // in the caller which does wxDynamicCast()
349 // first, let DefDlgProc() know about the new default button
352 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
353 // focus at all any more
354 if ( !wxTheApp
->IsActive() )
357 // look for a panel-like window
358 wxWindow
*win
= btn
->GetParent();
359 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
360 win
= win
->GetParent();
364 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
366 // sending DM_SETDEFID also changes the button style to
367 // BS_DEFPUSHBUTTON so there is nothing more to do
371 // then also change the style as needed
372 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
373 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
375 // don't do it with the owner drawn buttons because it will
376 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
377 // BS_DEFPUSHBUTTON != 0)!
378 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
380 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
381 on
? style
| BS_DEFPUSHBUTTON
382 : style
& ~BS_DEFPUSHBUTTON
,
387 // redraw the button - it will notice itself that it's
388 // [not] the default one [any longer]
392 //else: already has correct style
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 bool wxButton::SendClickEvent()
401 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
402 event
.SetEventObject(this);
404 return ProcessCommand(event
);
407 void wxButton::Command(wxCommandEvent
& event
)
409 ProcessCommand(event
);
412 // ----------------------------------------------------------------------------
413 // event/message handlers
414 // ----------------------------------------------------------------------------
416 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
418 bool processed
= FALSE
;
421 // NOTE: Apparently older versions (NT 4?) of the common controls send
422 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
423 // buttons, so in order to send two EVET_BUTTON events we should
424 // catch both types. Currently (Feb 2003) up-to-date versions of
425 // win98, win2k and winXP all send two BN_CLICKED messages for
426 // all button types, so we don't catch BN_DOUBLECLICKED anymore
427 // in order to not get 3 EVT_BUTTON events. If this is a problem
428 // then we need to figure out which version of the comctl32 changed
429 // this behaviour and test for it.
431 case 1: // message came from an accelerator
432 case BN_CLICKED
: // normal buttons send this
433 processed
= SendClickEvent();
440 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
442 // when we receive focus, we want to temporary become the default button in
443 // our parent panel so that pressing "Enter" would activate us -- and when
444 // losing it we should restore the previous default button as well
445 if ( nMsg
== WM_SETFOCUS
)
449 // let the default processing take place too
451 else if ( nMsg
== WM_KILLFOCUS
)
455 else if ( nMsg
== WM_LBUTTONDBLCLK
)
457 // emulate a click event to force an owner-drawn button to change its
458 // appearance - without this, it won't do it
459 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
461 // and continue with processing the message normally as well
464 // let the base class do all real processing
465 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
468 // ----------------------------------------------------------------------------
469 // owner-drawn buttons support
470 // ----------------------------------------------------------------------------
476 static void DrawButtonText(HDC hdc
,
478 const wxString
& text
,
481 COLORREF colOld
= SetTextColor(hdc
, col
);
482 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
484 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
485 ::DrawText(hdc
, text
, text
.length(), pRect
, DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
487 SetBkMode(hdc
, modeOld
);
488 SetTextColor(hdc
, colOld
);
491 static void DrawRect(HDC hdc
, const RECT
& r
)
493 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
494 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
495 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
496 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
499 void wxButton::MakeOwnerDrawn()
501 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
502 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
505 style
|= BS_OWNERDRAW
;
506 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
510 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
512 if ( !wxControl::SetBackgroundColour(colour
) )
525 bool wxButton::SetForegroundColour(const wxColour
&colour
)
527 if ( !wxControl::SetForegroundColour(colour
) )
541 The button frame looks like this normally:
544 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
545 WH GB H = light grey (LIGHT)
546 WH GB G = dark grey (SHADOW)
547 WH GB B = black (DKSHADOW)
552 When the button is selected, the button becomes like this (the total button
553 size doesn't change):
564 When the button is pushed (while selected) it is like:
576 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
577 bool selected
, bool pushed
)
580 CopyRect(&r
, &rectBtn
);
582 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
583 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
584 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
585 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
587 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
596 (void)SelectObject(hdc
, hpenGrey
);
597 InflateRect(&r
, -1, -1);
607 InflateRect(&r
, -1, -1);
610 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
611 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
613 (void)SelectObject(hdc
, hpenWhite
);
614 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
615 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
617 (void)SelectObject(hdc
, hpenLightGr
);
618 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
619 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
621 (void)SelectObject(hdc
, hpenGrey
);
622 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
623 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
626 (void)SelectObject(hdc
, hpenOld
);
627 DeleteObject(hpenWhite
);
628 DeleteObject(hpenLightGr
);
629 DeleteObject(hpenGrey
);
630 DeleteObject(hpenBlack
);
633 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
635 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
638 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
640 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
641 colFg
= wxColourToRGB(GetForegroundColour());
643 HDC hdc
= lpDIS
->hDC
;
644 UINT state
= lpDIS
->itemState
;
646 // first, draw the background
647 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
649 FillRect(hdc
, &rectBtn
, hbrushBackground
);
651 // draw the border for the current state
652 bool selected
= (state
& ODS_SELECTED
) != 0;
655 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
658 selected
= panel
->GetDefaultItem() == this;
661 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
663 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
665 // draw the focus rect if needed
666 if ( state
& ODS_FOCUS
)
669 CopyRect(&rectFocus
, &rectBtn
);
671 // I don't know where does this constant come from, but this is how
672 // Windows draws them
673 InflateRect(&rectFocus
, -4, -4);
675 DrawFocusRect(hdc
, &rectFocus
);
680 // the label is shifted by 1 pixel to create "pushed" effect
681 OffsetRect(&rectBtn
, 1, 1);
684 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
685 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
688 ::DeleteObject(hbrushBackground
);
695 #endif // wxUSE_BUTTON