1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/button.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "button.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/button.h"
37 #include "wx/bmpbuttn.h"
38 #include "wx/settings.h"
39 #include "wx/dcscreen.h"
42 #include "wx/msw/private.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
50 // this macro tries to adjust the default button height to a reasonable value
51 // using the char height as the base
52 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // creation/destruction
60 // ----------------------------------------------------------------------------
62 bool wxButton::Create(wxWindow
*parent
,
64 const wxString
& label
,
68 const wxValidator
& validator
,
71 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
75 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
78 // if the label contains several lines we must explicitly tell the button
79 // about it or it wouldn't draw it correctly ("\n"s would just appear as
82 // NB: we do it here and not in MSWGetStyle() because we need the label
83 // value and m_label is not set yet when MSWGetStyle() is called;
84 // besides changing BS_MULTILINE during run-time is pointless anyhow
85 if ( label
.find(_T('\n')) != wxString::npos
)
87 msStyle
|= BS_MULTILINE
;
91 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
104 // buttons never have an external border, they draw their own one
105 WXDWORD msStyle
= wxControl::MSWGetStyle
107 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
110 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
111 // each other in any resizeable dialog which has more than one button in
113 msStyle
|= WS_CLIPSIBLINGS
;
116 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
117 // and wxBU_RIGHT to get BS_CENTER!
118 if ( style
& wxBU_LEFT
)
120 if ( style
& wxBU_RIGHT
)
122 if ( style
& wxBU_TOP
)
124 if ( style
& wxBU_BOTTOM
)
125 msStyle
|= BS_BOTTOM
;
131 // ----------------------------------------------------------------------------
132 // size management including autosizing
133 // ----------------------------------------------------------------------------
135 wxSize
wxButton::DoGetBestSize() const
138 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn
, NULL
);
141 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
143 // add a margin -- the button is wider than just its label
146 // the button height is proportional to the height of the font used
147 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
149 // all buttons have at least the standard size unless the user explicitly
150 // wants them to be of smaller size and used wxBU_EXACTFIT style when
151 // creating the button
152 if ( !HasFlag(wxBU_EXACTFIT
) )
154 wxSize sz
= GetDefaultSize();
163 return wxSize(wBtn
, hBtn
);
167 wxSize
wxButtonBase::GetDefaultSize()
169 static wxSize s_sizeBtn
;
171 if ( s_sizeBtn
.x
== 0 )
174 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
176 // the size of a standard button in the dialog units is 50x14,
177 // translate this to pixels
178 // NB1: the multipliers come from the Windows convention
179 // NB2: the extra +1/+2 were needed to get the size be the same as the
180 // size of the buttons in the standard dialog - I don't know how
181 // this happens, but on my system this size is 75x23 in pixels and
182 // 23*8 isn't even divisible by 14... Would be nice to understand
183 // why these constants are needed though!
184 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
185 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
191 // ----------------------------------------------------------------------------
192 // default button handling
193 // ----------------------------------------------------------------------------
196 "Everything you ever wanted to know about the default buttons" or "Why do we
197 have to do all this?"
199 In MSW the default button should be activated when the user presses Enter
200 and the current control doesn't process Enter itself somehow. This is
201 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
202 Another aspect of "defaultness" is that the default button has different
203 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
204 separate from DM_SETDEFID stuff (!).
206 Final complication is that when a button is active, it should be the default
207 one, i.e. pressing Enter on a button always activates it and not another
210 We handle this by maintaining a permanent and a temporary default items in
211 wxControlContainer (both may be NULL). When a button becomes the current
212 control (i.e. gets focus) it sets itself as the temporary default which
213 ensures that it has the right appearance and that Enter will be redirected
214 to it. When the button loses focus, it unsets the temporary default and so
215 the default item will be the permanent default -- that is the default button
216 if any had been set or none otherwise, which is just what we want.
218 Remark that we probably don't need to send DM_SETDEFID as we don't use
219 ::IsDialogMessage() (which relies on it) any longer but OTOH it probably
220 doesn't hurt neither.
223 // set this button as the (permanently) default one in its panel
224 void wxButton::SetDefault()
226 wxWindow
*parent
= GetParent();
228 wxCHECK_RET( parent
, _T("button without parent?") );
230 // set this one as the default button both for wxWindows and Windows
231 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
232 ::SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
234 UpdateDefaultStyle(this, winOldDefault
);
237 void wxButton::SetTmpDefault()
239 wxWindow
*parent
= GetParent();
241 wxCHECK_RET( parent
, _T("button without parent?") );
243 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
244 parent
->SetTmpDefaultItem(this);
245 if ( winOldDefault
!= this )
247 UpdateDefaultStyle(this, winOldDefault
);
249 //else: no styles to update
252 void wxButton::UnsetTmpDefault()
254 wxWindow
*parent
= GetParent();
256 wxCHECK_RET( parent
, _T("button without parent?") );
258 parent
->SetTmpDefaultItem(NULL
);
260 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
261 if ( winOldDefault
!= this )
263 UpdateDefaultStyle(winOldDefault
, this);
265 //else: we had been default before anyhow
270 wxButton::UpdateDefaultStyle(wxWindow
*winDefault
, wxWindow
*winOldDefault
)
272 // clear the BS_DEFPUSHBUTTON for the old default button
273 wxButton
*btnOldDefault
= wxDynamicCast(winOldDefault
, wxButton
);
274 if ( btnOldDefault
&& btnOldDefault
!= winDefault
)
276 // remove the BS_DEFPUSHBUTTON style from the other button
277 long style
= ::GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
279 // don't do it with the owner drawn buttons because it will reset
280 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
281 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
283 style
&= ~BS_DEFPUSHBUTTON
;
284 ::SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
288 // redraw the button - it will notice itself that it's not the
289 // default one any longer
290 btnOldDefault
->Refresh();
294 // and set BS_DEFPUSHBUTTON for this button
295 wxButton
*btnDefault
= wxDynamicCast(winDefault
, wxButton
);
298 long style
= ::GetWindowLong(GetHwndOf(btnDefault
), GWL_STYLE
);
299 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
301 style
|= BS_DEFPUSHBUTTON
;
302 ::SendMessage(GetHwndOf(btnDefault
), BM_SETSTYLE
, style
, 1L);
306 btnDefault
->Refresh();
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 bool wxButton::SendClickEvent()
317 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
318 event
.SetEventObject(this);
320 return ProcessCommand(event
);
323 void wxButton::Command(wxCommandEvent
& event
)
325 ProcessCommand(event
);
328 // ----------------------------------------------------------------------------
329 // event/message handlers
330 // ----------------------------------------------------------------------------
332 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
334 bool processed
= FALSE
;
337 case 1: // message came from an accelerator
338 case BN_CLICKED
: // normal buttons send this
339 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
340 processed
= SendClickEvent();
347 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
349 // when we receive focus, we want to temporary become the default button in
350 // our parent panel so that pressing "Enter" would activate us -- and when
351 // losing it we should restore the previous default button as well
352 if ( nMsg
== WM_SETFOCUS
)
356 // let the default processing take place too
358 else if ( nMsg
== WM_KILLFOCUS
)
362 else if ( nMsg
== WM_LBUTTONDBLCLK
)
364 // emulate a click event to force an owner-drawn button to change its
365 // appearance - without this, it won't do it
366 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
368 // and continue with processing the message normally as well
371 // let the base class do all real processing
372 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
375 // ----------------------------------------------------------------------------
376 // owner-drawn buttons support
377 // ----------------------------------------------------------------------------
383 static void DrawButtonText(HDC hdc
,
385 const wxString
& text
,
388 COLORREF colOld
= SetTextColor(hdc
, col
);
389 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
391 DrawText(hdc
, text
, text
.length(), pRect
,
392 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
394 SetBkMode(hdc
, modeOld
);
395 SetTextColor(hdc
, colOld
);
398 static void DrawRect(HDC hdc
, const RECT
& r
)
400 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
401 LineTo(hdc
, r
.right
, r
.top
);
402 LineTo(hdc
, r
.right
, r
.bottom
);
403 LineTo(hdc
, r
.left
, r
.bottom
);
404 LineTo(hdc
, r
.left
, r
.top
);
407 void wxButton::MakeOwnerDrawn()
409 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
410 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
413 style
|= BS_OWNERDRAW
;
414 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
418 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
420 if ( !wxControl::SetBackgroundColour(colour
) )
433 bool wxButton::SetForegroundColour(const wxColour
&colour
)
435 if ( !wxControl::SetForegroundColour(colour
) )
449 The button frame looks like this normally:
452 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
453 WH GB H = light grey (LIGHT)
454 WH GB G = dark grey (SHADOW)
455 WH GB B = black (DKSHADOW)
460 When the button is selected, the button becomes like this (the total button
461 size doesn't change):
472 When the button is pushed (while selected) it is like:
484 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
485 bool selected
, bool pushed
)
488 CopyRect(&r
, &rectBtn
);
490 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
491 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
492 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
493 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
495 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
504 (void)SelectObject(hdc
, hpenGrey
);
505 InflateRect(&r
, -1, -1);
515 InflateRect(&r
, -1, -1);
518 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
519 LineTo(hdc
, r
.right
, r
.bottom
);
520 LineTo(hdc
, r
.right
, r
.top
- 1);
522 (void)SelectObject(hdc
, hpenWhite
);
523 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
524 LineTo(hdc
, r
.left
, r
.top
);
525 LineTo(hdc
, r
.right
, r
.top
);
527 (void)SelectObject(hdc
, hpenLightGr
);
528 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
529 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
530 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
532 (void)SelectObject(hdc
, hpenGrey
);
533 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
534 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
535 LineTo(hdc
, r
.right
- 1, r
.top
);
538 (void)SelectObject(hdc
, hpenOld
);
539 DeleteObject(hpenWhite
);
540 DeleteObject(hpenLightGr
);
541 DeleteObject(hpenGrey
);
542 DeleteObject(hpenBlack
);
545 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
547 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
550 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
552 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
553 colFg
= wxColourToRGB(GetForegroundColour());
555 HDC hdc
= lpDIS
->hDC
;
556 UINT state
= lpDIS
->itemState
;
558 // first, draw the background
559 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
561 FillRect(hdc
, &rectBtn
, hbrushBackground
);
563 // draw the border for the current state
564 bool selected
= (state
& ODS_SELECTED
) != 0;
567 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
570 selected
= panel
->GetDefaultItem() == this;
573 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
575 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
577 // draw the focus rect if needed
578 if ( state
& ODS_FOCUS
)
581 CopyRect(&rectFocus
, &rectBtn
);
583 // I don't know where does this constant come from, but this is how
584 // Windows draws them
585 InflateRect(&rectFocus
, -4, -4);
587 DrawFocusRect(hdc
, &rectFocus
);
592 // the label is shifted by 1 pixel to create "pushed" effect
593 OffsetRect(&rectBtn
, 1, 1);
596 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
597 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
600 ::DeleteObject(hbrushBackground
);
607 #endif // wxUSE_BUTTON