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
50 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton
, wxControl
,"wx/button.h")
52 WX_BEGIN_PROPERTIES_TABLE(wxButton
)
53 WX_DELEGATE( OnClick
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEvent
)
55 WX_PROPERTY( Font
, wxFont
, SetFont
, GetFont
, )
56 WX_PROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxT("") )
57 WX_END_PROPERTIES_TABLE()
59 WX_BEGIN_HANDLERS_TABLE(wxButton
)
60 WX_END_HANDLERS_TABLE()
62 WX_CONSTRUCTOR_6( wxButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
66 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
69 // this macro tries to adjust the default button height to a reasonable value
70 // using the char height as the base
71 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
78 // creation/destruction
79 // ----------------------------------------------------------------------------
81 bool wxButton::Create(wxWindow
*parent
,
83 const wxString
& label
,
87 const wxValidator
& validator
,
90 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
94 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
97 // if the label contains several lines we must explicitly tell the button
98 // about it or it wouldn't draw it correctly ("\n"s would just appear as
101 // NB: we do it here and not in MSWGetStyle() because we need the label
102 // value and m_label is not set yet when MSWGetStyle() is called;
103 // besides changing BS_MULTILINE during run-time is pointless anyhow
104 if ( label
.find(_T('\n')) != wxString::npos
)
106 msStyle
|= BS_MULTILINE
;
110 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
113 wxButton::~wxButton()
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
123 // buttons never have an external border, they draw their own one
124 WXDWORD msStyle
= wxControl::MSWGetStyle
126 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
129 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
130 // each other in any resizeable dialog which has more than one button in
132 msStyle
|= WS_CLIPSIBLINGS
;
135 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
136 // and wxBU_RIGHT to get BS_CENTER!
137 if ( style
& wxBU_LEFT
)
139 if ( style
& wxBU_RIGHT
)
141 if ( style
& wxBU_TOP
)
143 if ( style
& wxBU_BOTTOM
)
144 msStyle
|= BS_BOTTOM
;
150 // ----------------------------------------------------------------------------
151 // size management including autosizing
152 // ----------------------------------------------------------------------------
154 wxSize
wxButton::DoGetBestSize() const
157 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn
, NULL
);
160 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
162 // add a margin -- the button is wider than just its label
165 // the button height is proportional to the height of the font used
166 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
168 // all buttons have at least the standard size unless the user explicitly
169 // wants them to be of smaller size and used wxBU_EXACTFIT style when
170 // creating the button
171 if ( !HasFlag(wxBU_EXACTFIT
) )
173 wxSize sz
= GetDefaultSize();
182 return wxSize(wBtn
, hBtn
);
186 wxSize
wxButtonBase::GetDefaultSize()
188 static wxSize s_sizeBtn
;
190 if ( s_sizeBtn
.x
== 0 )
193 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
195 // the size of a standard button in the dialog units is 50x14,
196 // translate this to pixels
197 // NB1: the multipliers come from the Windows convention
198 // NB2: the extra +1/+2 were needed to get the size be the same as the
199 // size of the buttons in the standard dialog - I don't know how
200 // this happens, but on my system this size is 75x23 in pixels and
201 // 23*8 isn't even divisible by 14... Would be nice to understand
202 // why these constants are needed though!
203 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
204 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
210 // ----------------------------------------------------------------------------
211 // default button handling
212 // ----------------------------------------------------------------------------
215 "Everything you ever wanted to know about the default buttons" or "Why do we
216 have to do all this?"
218 In MSW the default button should be activated when the user presses Enter
219 and the current control doesn't process Enter itself somehow. This is
220 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
221 Another aspect of "defaultness" is that the default button has different
222 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
223 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
224 be unset if our parent window is not active so it should be unset whenever
225 we lose activation and set back when we regain it.
227 Final complication is that when a button is active, it should be the default
228 one, i.e. pressing Enter on a button always activates it and not another
231 We handle this by maintaining a permanent and a temporary default items in
232 wxControlContainer (both may be NULL). When a button becomes the current
233 control (i.e. gets focus) it sets itself as the temporary default which
234 ensures that it has the right appearance and that Enter will be redirected
235 to it. When the button loses focus, it unsets the temporary default and so
236 the default item will be the permanent default -- that is the default button
237 if any had been set or none otherwise, which is just what we want.
239 NB: all this is quite complicated by now and the worst is that normally
240 it shouldn't be necessary at all as for the normal Windows programs
241 DefWindowProc() and IsDialogMessage() take care of all this
242 automatically -- however in wxWindows programs this doesn't work for
243 nested hierarchies (i.e. a notebook inside a notebook) for unknown
244 reason and so we have to reproduce all this code ourselves. It would be
245 very nice if we could avoid doing it.
248 // set this button as the (permanently) default one in its panel
249 void wxButton::SetDefault()
251 wxWindow
*parent
= GetParent();
253 wxCHECK_RET( parent
, _T("button without parent?") );
255 // set this one as the default button both for wxWindows ...
256 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
259 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
260 SetDefaultStyle(this, TRUE
);
263 // set this button as being currently default
264 void wxButton::SetTmpDefault()
266 wxWindow
*parent
= GetParent();
268 wxCHECK_RET( parent
, _T("button without parent?") );
270 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
271 parent
->SetTmpDefaultItem(this);
273 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
274 SetDefaultStyle(this, TRUE
);
277 // unset this button as currently default, it may still stay permanent default
278 void wxButton::UnsetTmpDefault()
280 wxWindow
*parent
= GetParent();
282 wxCHECK_RET( parent
, _T("button without parent?") );
284 parent
->SetTmpDefaultItem(NULL
);
286 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
288 SetDefaultStyle(this, FALSE
);
289 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), TRUE
);
294 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
296 // we may be called with NULL pointer -- simpler to do the check here than
297 // in the caller which does wxDynamicCast()
301 // first, let DefDlgProc() know about the new default button
304 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
305 // focus at all any more
306 if ( !wxTheApp
->IsActive() )
309 // look for a panel-like window
310 wxWindow
*win
= btn
->GetParent();
311 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
312 win
= win
->GetParent();
316 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
318 // sending DM_SETDEFID also changes the button style to
319 // BS_DEFPUSHBUTTON so there is nothing more to do
323 // then also change the style as needed
324 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
325 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
327 // don't do it with the owner drawn buttons because it will
328 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
329 // BS_DEFPUSHBUTTON != 0)!
330 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
332 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
333 on
? style
| BS_DEFPUSHBUTTON
334 : style
& ~BS_DEFPUSHBUTTON
,
339 // redraw the button - it will notice itself that it's
340 // [not] the default one [any longer]
344 //else: already has correct style
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 bool wxButton::SendClickEvent()
353 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
354 event
.SetEventObject(this);
356 return ProcessCommand(event
);
359 void wxButton::Command(wxCommandEvent
& event
)
361 ProcessCommand(event
);
364 // ----------------------------------------------------------------------------
365 // event/message handlers
366 // ----------------------------------------------------------------------------
368 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
370 bool processed
= FALSE
;
373 // NOTE: Apparently older versions (NT 4?) of the common controls send
374 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
375 // buttons, so in order to send two EVET_BUTTON events we should
376 // catch both types. Currently (Feb 2003) up-to-date versions of
377 // win98, win2k and winXP all send two BN_CLICKED messages for
378 // all button types, so we don't catch BN_DOUBLECLICKED anymore
379 // in order to not get 3 EVT_BUTTON events. If this is a problem
380 // then we need to figure out which version of the comctl32 changed
381 // this behaviour and test for it.
383 case 1: // message came from an accelerator
384 case BN_CLICKED
: // normal buttons send this
385 processed
= SendClickEvent();
392 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
394 // when we receive focus, we want to temporary become the default button in
395 // our parent panel so that pressing "Enter" would activate us -- and when
396 // losing it we should restore the previous default button as well
397 if ( nMsg
== WM_SETFOCUS
)
401 // let the default processing take place too
403 else if ( nMsg
== WM_KILLFOCUS
)
407 else if ( nMsg
== WM_LBUTTONDBLCLK
)
409 // emulate a click event to force an owner-drawn button to change its
410 // appearance - without this, it won't do it
411 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
413 // and continue with processing the message normally as well
416 // let the base class do all real processing
417 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
420 // ----------------------------------------------------------------------------
421 // owner-drawn buttons support
422 // ----------------------------------------------------------------------------
428 static void DrawButtonText(HDC hdc
,
430 const wxString
& text
,
433 COLORREF colOld
= SetTextColor(hdc
, col
);
434 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
436 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
437 ::DrawText(hdc
, text
, text
.length(), pRect
, DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
439 SetBkMode(hdc
, modeOld
);
440 SetTextColor(hdc
, colOld
);
443 static void DrawRect(HDC hdc
, const RECT
& r
)
445 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
446 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
447 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
448 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
451 void wxButton::MakeOwnerDrawn()
453 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
454 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
457 style
|= BS_OWNERDRAW
;
458 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
462 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
464 if ( !wxControl::SetBackgroundColour(colour
) )
477 bool wxButton::SetForegroundColour(const wxColour
&colour
)
479 if ( !wxControl::SetForegroundColour(colour
) )
493 The button frame looks like this normally:
496 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
497 WH GB H = light grey (LIGHT)
498 WH GB G = dark grey (SHADOW)
499 WH GB B = black (DKSHADOW)
504 When the button is selected, the button becomes like this (the total button
505 size doesn't change):
516 When the button is pushed (while selected) it is like:
528 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
529 bool selected
, bool pushed
)
532 CopyRect(&r
, &rectBtn
);
534 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
535 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
536 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
537 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
539 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
548 (void)SelectObject(hdc
, hpenGrey
);
549 InflateRect(&r
, -1, -1);
559 InflateRect(&r
, -1, -1);
562 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
563 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
565 (void)SelectObject(hdc
, hpenWhite
);
566 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
567 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
569 (void)SelectObject(hdc
, hpenLightGr
);
570 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
571 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
573 (void)SelectObject(hdc
, hpenGrey
);
574 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
575 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
578 (void)SelectObject(hdc
, hpenOld
);
579 DeleteObject(hpenWhite
);
580 DeleteObject(hpenLightGr
);
581 DeleteObject(hpenGrey
);
582 DeleteObject(hpenBlack
);
585 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
587 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
590 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
592 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
593 colFg
= wxColourToRGB(GetForegroundColour());
595 HDC hdc
= lpDIS
->hDC
;
596 UINT state
= lpDIS
->itemState
;
598 // first, draw the background
599 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
601 FillRect(hdc
, &rectBtn
, hbrushBackground
);
603 // draw the border for the current state
604 bool selected
= (state
& ODS_SELECTED
) != 0;
607 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
610 selected
= panel
->GetDefaultItem() == this;
613 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
615 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
617 // draw the focus rect if needed
618 if ( state
& ODS_FOCUS
)
621 CopyRect(&rectFocus
, &rectBtn
);
623 // I don't know where does this constant come from, but this is how
624 // Windows draws them
625 InflateRect(&rectFocus
, -4, -4);
627 DrawFocusRect(hdc
, &rectFocus
);
632 // the label is shifted by 1 pixel to create "pushed" effect
633 OffsetRect(&rectBtn
, 1, 1);
636 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
637 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
640 ::DeleteObject(hbrushBackground
);
647 #endif // wxUSE_BUTTON