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 WX_BEGIN_FLAGS( wxButtonStyle
)
54 // new style border flags, we put them first to
55 // use them for streaming out
56 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
57 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
58 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
59 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
60 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
61 WX_FLAGS_MEMBER(wxBORDER_NONE
)
63 // old style border flags
64 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
65 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
66 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
67 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
68 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
69 WX_FLAGS_MEMBER(wxNO_BORDER
)
71 // standard window styles
72 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
73 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
74 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
75 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
76 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
77 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
78 WX_FLAGS_MEMBER(wxVSCROLL
)
79 WX_FLAGS_MEMBER(wxHSCROLL
)
81 WX_FLAGS_MEMBER(wxBU_LEFT
)
82 WX_FLAGS_MEMBER(wxBU_RIGHT
)
83 WX_FLAGS_MEMBER(wxBU_TOP
)
84 WX_FLAGS_MEMBER(wxBU_BOTTOM
)
85 WX_FLAGS_MEMBER(wxBU_EXACTFIT
)
86 WX_END_FLAGS( wxButtonStyle
)
88 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton
, wxControl
,"wx/button.h")
90 WX_BEGIN_PROPERTIES_TABLE(wxButton
)
91 WX_DELEGATE( OnClick
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEvent
)
93 WX_PROPERTY( Font
, wxFont
, SetFont
, GetFont
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
94 WX_PROPERTY( Label
, wxString
, SetLabel
, GetLabel
, wxEmptyString
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
96 WX_PROPERTY_FLAGS( WindowStyle
, wxButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
98 WX_END_PROPERTIES_TABLE()
100 WX_BEGIN_HANDLERS_TABLE(wxButton
)
101 WX_END_HANDLERS_TABLE()
103 WX_CONSTRUCTOR_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
,
124 const wxString
& label
,
128 const wxValidator
& validator
,
129 const wxString
& name
)
131 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
135 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
138 // if the label contains several lines we must explicitly tell the button
139 // about it or it wouldn't draw it correctly ("\n"s would just appear as
142 // NB: we do it here and not in MSWGetStyle() because we need the label
143 // value and m_label is not set yet when MSWGetStyle() is called;
144 // besides changing BS_MULTILINE during run-time is pointless anyhow
145 if ( label
.find(_T('\n')) != wxString::npos
)
147 msStyle
|= BS_MULTILINE
;
151 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
154 wxButton::~wxButton()
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
164 // buttons never have an external border, they draw their own one
165 WXDWORD msStyle
= wxControl::MSWGetStyle
167 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
170 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
171 // each other in any resizeable dialog which has more than one button in
173 msStyle
|= WS_CLIPSIBLINGS
;
176 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
177 // and wxBU_RIGHT to get BS_CENTER!
178 if ( style
& wxBU_LEFT
)
180 if ( style
& wxBU_RIGHT
)
182 if ( style
& wxBU_TOP
)
184 if ( style
& wxBU_BOTTOM
)
185 msStyle
|= BS_BOTTOM
;
191 // ----------------------------------------------------------------------------
192 // size management including autosizing
193 // ----------------------------------------------------------------------------
195 wxSize
wxButton::DoGetBestSize() const
198 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn
, NULL
);
201 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
203 // add a margin -- the button is wider than just its label
206 // the button height is proportional to the height of the font used
207 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
209 // all buttons have at least the standard size unless the user explicitly
210 // wants them to be of smaller size and used wxBU_EXACTFIT style when
211 // creating the button
212 if ( !HasFlag(wxBU_EXACTFIT
) )
214 wxSize sz
= GetDefaultSize();
223 return wxSize(wBtn
, hBtn
);
227 wxSize
wxButtonBase::GetDefaultSize()
229 static wxSize s_sizeBtn
;
231 if ( s_sizeBtn
.x
== 0 )
234 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
236 // the size of a standard button in the dialog units is 50x14,
237 // translate this to pixels
238 // NB1: the multipliers come from the Windows convention
239 // NB2: the extra +1/+2 were needed to get the size be the same as the
240 // size of the buttons in the standard dialog - I don't know how
241 // this happens, but on my system this size is 75x23 in pixels and
242 // 23*8 isn't even divisible by 14... Would be nice to understand
243 // why these constants are needed though!
244 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
245 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
251 // ----------------------------------------------------------------------------
252 // default button handling
253 // ----------------------------------------------------------------------------
256 "Everything you ever wanted to know about the default buttons" or "Why do we
257 have to do all this?"
259 In MSW the default button should be activated when the user presses Enter
260 and the current control doesn't process Enter itself somehow. This is
261 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
262 Another aspect of "defaultness" is that the default button has different
263 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
264 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
265 be unset if our parent window is not active so it should be unset whenever
266 we lose activation and set back when we regain it.
268 Final complication is that when a button is active, it should be the default
269 one, i.e. pressing Enter on a button always activates it and not another
272 We handle this by maintaining a permanent and a temporary default items in
273 wxControlContainer (both may be NULL). When a button becomes the current
274 control (i.e. gets focus) it sets itself as the temporary default which
275 ensures that it has the right appearance and that Enter will be redirected
276 to it. When the button loses focus, it unsets the temporary default and so
277 the default item will be the permanent default -- that is the default button
278 if any had been set or none otherwise, which is just what we want.
280 NB: all this is quite complicated by now and the worst is that normally
281 it shouldn't be necessary at all as for the normal Windows programs
282 DefWindowProc() and IsDialogMessage() take care of all this
283 automatically -- however in wxWindows programs this doesn't work for
284 nested hierarchies (i.e. a notebook inside a notebook) for unknown
285 reason and so we have to reproduce all this code ourselves. It would be
286 very nice if we could avoid doing it.
289 // set this button as the (permanently) default one in its panel
290 void wxButton::SetDefault()
292 wxWindow
*parent
= GetParent();
294 wxCHECK_RET( parent
, _T("button without parent?") );
296 // set this one as the default button both for wxWindows ...
297 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
300 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
301 SetDefaultStyle(this, TRUE
);
304 // set this button as being currently default
305 void wxButton::SetTmpDefault()
307 wxWindow
*parent
= GetParent();
309 wxCHECK_RET( parent
, _T("button without parent?") );
311 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
312 parent
->SetTmpDefaultItem(this);
314 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
315 SetDefaultStyle(this, TRUE
);
318 // unset this button as currently default, it may still stay permanent default
319 void wxButton::UnsetTmpDefault()
321 wxWindow
*parent
= GetParent();
323 wxCHECK_RET( parent
, _T("button without parent?") );
325 parent
->SetTmpDefaultItem(NULL
);
327 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
329 SetDefaultStyle(this, FALSE
);
330 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), TRUE
);
335 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
337 // we may be called with NULL pointer -- simpler to do the check here than
338 // in the caller which does wxDynamicCast()
342 // first, let DefDlgProc() know about the new default button
345 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
346 // focus at all any more
347 if ( !wxTheApp
->IsActive() )
350 // look for a panel-like window
351 wxWindow
*win
= btn
->GetParent();
352 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
353 win
= win
->GetParent();
357 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
359 // sending DM_SETDEFID also changes the button style to
360 // BS_DEFPUSHBUTTON so there is nothing more to do
364 // then also change the style as needed
365 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
366 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
368 // don't do it with the owner drawn buttons because it will
369 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
370 // BS_DEFPUSHBUTTON != 0)!
371 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
373 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
374 on
? style
| BS_DEFPUSHBUTTON
375 : style
& ~BS_DEFPUSHBUTTON
,
380 // redraw the button - it will notice itself that it's
381 // [not] the default one [any longer]
385 //else: already has correct style
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 bool wxButton::SendClickEvent()
394 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
395 event
.SetEventObject(this);
397 return ProcessCommand(event
);
400 void wxButton::Command(wxCommandEvent
& event
)
402 ProcessCommand(event
);
405 // ----------------------------------------------------------------------------
406 // event/message handlers
407 // ----------------------------------------------------------------------------
409 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
411 bool processed
= FALSE
;
414 // NOTE: Apparently older versions (NT 4?) of the common controls send
415 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
416 // buttons, so in order to send two EVET_BUTTON events we should
417 // catch both types. Currently (Feb 2003) up-to-date versions of
418 // win98, win2k and winXP all send two BN_CLICKED messages for
419 // all button types, so we don't catch BN_DOUBLECLICKED anymore
420 // in order to not get 3 EVT_BUTTON events. If this is a problem
421 // then we need to figure out which version of the comctl32 changed
422 // this behaviour and test for it.
424 case 1: // message came from an accelerator
425 case BN_CLICKED
: // normal buttons send this
426 processed
= SendClickEvent();
433 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
435 // when we receive focus, we want to temporary become the default button in
436 // our parent panel so that pressing "Enter" would activate us -- and when
437 // losing it we should restore the previous default button as well
438 if ( nMsg
== WM_SETFOCUS
)
442 // let the default processing take place too
444 else if ( nMsg
== WM_KILLFOCUS
)
448 else if ( nMsg
== WM_LBUTTONDBLCLK
)
450 // emulate a click event to force an owner-drawn button to change its
451 // appearance - without this, it won't do it
452 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
454 // and continue with processing the message normally as well
457 // let the base class do all real processing
458 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
461 // ----------------------------------------------------------------------------
462 // owner-drawn buttons support
463 // ----------------------------------------------------------------------------
469 static void DrawButtonText(HDC hdc
,
471 const wxString
& text
,
474 COLORREF colOld
= SetTextColor(hdc
, col
);
475 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
477 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
478 ::DrawText(hdc
, text
, text
.length(), pRect
, DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
480 SetBkMode(hdc
, modeOld
);
481 SetTextColor(hdc
, colOld
);
484 static void DrawRect(HDC hdc
, const RECT
& r
)
486 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
487 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
488 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
489 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
492 void wxButton::MakeOwnerDrawn()
494 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
495 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
498 style
|= BS_OWNERDRAW
;
499 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
503 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
505 if ( !wxControl::SetBackgroundColour(colour
) )
518 bool wxButton::SetForegroundColour(const wxColour
&colour
)
520 if ( !wxControl::SetForegroundColour(colour
) )
534 The button frame looks like this normally:
537 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
538 WH GB H = light grey (LIGHT)
539 WH GB G = dark grey (SHADOW)
540 WH GB B = black (DKSHADOW)
545 When the button is selected, the button becomes like this (the total button
546 size doesn't change):
557 When the button is pushed (while selected) it is like:
569 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
570 bool selected
, bool pushed
)
573 CopyRect(&r
, &rectBtn
);
575 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
576 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
577 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
578 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
580 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
589 (void)SelectObject(hdc
, hpenGrey
);
590 InflateRect(&r
, -1, -1);
600 InflateRect(&r
, -1, -1);
603 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
604 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
606 (void)SelectObject(hdc
, hpenWhite
);
607 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
608 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
610 (void)SelectObject(hdc
, hpenLightGr
);
611 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
612 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
614 (void)SelectObject(hdc
, hpenGrey
);
615 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
616 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
619 (void)SelectObject(hdc
, hpenOld
);
620 DeleteObject(hpenWhite
);
621 DeleteObject(hpenLightGr
);
622 DeleteObject(hpenGrey
);
623 DeleteObject(hpenBlack
);
626 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
628 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
631 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
633 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
634 colFg
= wxColourToRGB(GetForegroundColour());
636 HDC hdc
= lpDIS
->hDC
;
637 UINT state
= lpDIS
->itemState
;
639 // first, draw the background
640 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
642 FillRect(hdc
, &rectBtn
, hbrushBackground
);
644 // draw the border for the current state
645 bool selected
= (state
& ODS_SELECTED
) != 0;
648 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
651 selected
= panel
->GetDefaultItem() == this;
654 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
656 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
658 // draw the focus rect if needed
659 if ( state
& ODS_FOCUS
)
662 CopyRect(&rectFocus
, &rectBtn
);
664 // I don't know where does this constant come from, but this is how
665 // Windows draws them
666 InflateRect(&rectFocus
, -4, -4);
668 DrawFocusRect(hdc
, &rectFocus
);
673 // the label is shifted by 1 pixel to create "pushed" effect
674 OffsetRect(&rectBtn
, 1, 1);
677 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
678 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
681 ::DeleteObject(hbrushBackground
);
688 #endif // wxUSE_BUTTON