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
137 wxString label
= wxGetWindowText(GetHWND());
139 GetTextExtent(label
, &wBtn
, NULL
);
142 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
144 // add a margin - the button is wider than just its label
147 // the button height is proportional to the height of the font used
148 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
150 if (!HasFlag(wxBU_EXACTFIT
))
152 wxSize sz
= GetDefaultSize();
153 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
154 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
158 return wxSize(wBtn
, hBtn
);
163 wxSize
wxButtonBase::GetDefaultSize()
165 static wxSize s_sizeBtn
;
167 if ( s_sizeBtn
.x
== 0 )
170 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
172 // the size of a standard button in the dialog units is 50x14,
173 // translate this to pixels
174 // NB1: the multipliers come from the Windows convention
175 // NB2: the extra +1/+2 were needed to get the size be the same as the
176 // size of the buttons in the standard dialog - I don't know how
177 // this happens, but on my system this size is 75x23 in pixels and
178 // 23*8 isn't even divisible by 14... Would be nice to understand
179 // why these constants are needed though!
180 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
181 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
187 // ----------------------------------------------------------------------------
188 // default button handling
189 // ----------------------------------------------------------------------------
192 "Everything you ever wanted to know about the default buttons" or "Why do we
193 have to do all this?"
195 In MSW the default button should be activated when the user presses Enter
196 and the current control doesn't process Enter itself somehow. This is
197 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
198 Another aspect of "defaultness" is that the default button has different
199 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
200 separate from DM_SETDEFID stuff (!).
202 Final complication is that when a button is active, it should be the default
203 one, i.e. pressing Enter on a button always activates it and not another
206 We handle this by maintaining a permanent and a temporary default items in
207 wxControlContainer (both may be NULL). When a button becomes the current
208 control (i.e. gets focus) it sets itself as the temporary default which
209 ensures that it has the right appearance and that Enter will be redirected
210 to it. When the button loses focus, it unsets the temporary default and so
211 the default item will be the permanent default -- that is the default button
212 if any had been set or none otherwise, which is just what we want.
214 Remark that we probably don't need to send DM_SETDEFID as we don't use
215 ::IsDialogMessage() (which relies on it) any longer but OTOH it probably
216 doesn't hurt neither.
219 // set this button as the (permanently) default one in its panel
220 void wxButton::SetDefault()
222 wxWindow
*parent
= GetParent();
224 wxCHECK_RET( parent
, _T("button without parent?") );
226 // set this one as the default button both for wxWindows and Windows
227 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
228 ::SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
230 UpdateDefaultStyle(this, winOldDefault
);
233 void wxButton::SetTmpDefault()
235 wxWindow
*parent
= GetParent();
237 wxCHECK_RET( parent
, _T("button without parent?") );
239 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
240 parent
->SetTmpDefaultItem(this);
241 if ( winOldDefault
!= this )
243 UpdateDefaultStyle(this, winOldDefault
);
245 //else: no styles to update
248 void wxButton::UnsetTmpDefault()
250 wxWindow
*parent
= GetParent();
252 wxCHECK_RET( parent
, _T("button without parent?") );
254 parent
->SetTmpDefaultItem(NULL
);
256 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
257 if ( winOldDefault
!= this )
259 UpdateDefaultStyle(winOldDefault
, this);
261 //else: we had been default before anyhow
266 wxButton::UpdateDefaultStyle(wxWindow
*winDefault
, wxWindow
*winOldDefault
)
268 // clear the BS_DEFPUSHBUTTON for the old default button
269 wxButton
*btnOldDefault
= wxDynamicCast(winOldDefault
, wxButton
);
270 if ( btnOldDefault
&& btnOldDefault
!= winDefault
)
272 // remove the BS_DEFPUSHBUTTON style from the other button
273 long style
= ::GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
275 // don't do it with the owner drawn buttons because it will reset
276 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
277 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
279 style
&= ~BS_DEFPUSHBUTTON
;
280 ::SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
284 // redraw the button - it will notice itself that it's not the
285 // default one any longer
286 btnOldDefault
->Refresh();
290 // and set BS_DEFPUSHBUTTON for this button
291 wxButton
*btnDefault
= wxDynamicCast(winDefault
, wxButton
);
294 long style
= ::GetWindowLong(GetHwndOf(btnDefault
), GWL_STYLE
);
295 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
297 style
|= BS_DEFPUSHBUTTON
;
298 ::SendMessage(GetHwndOf(btnDefault
), BM_SETSTYLE
, style
, 1L);
302 btnDefault
->Refresh();
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 bool wxButton::SendClickEvent()
313 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
314 event
.SetEventObject(this);
316 return ProcessCommand(event
);
319 void wxButton::Command(wxCommandEvent
& event
)
321 ProcessCommand(event
);
324 // ----------------------------------------------------------------------------
325 // event/message handlers
326 // ----------------------------------------------------------------------------
328 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
330 bool processed
= FALSE
;
333 case 1: // message came from an accelerator
334 case BN_CLICKED
: // normal buttons send this
335 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
336 processed
= SendClickEvent();
343 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
345 // when we receive focus, we want to temporary become the default button in
346 // our parent panel so that pressing "Enter" would activate us -- and when
347 // losing it we should restore the previous default button as well
348 if ( nMsg
== WM_SETFOCUS
)
352 // let the default processing take place too
354 else if ( nMsg
== WM_KILLFOCUS
)
358 else if ( nMsg
== WM_LBUTTONDBLCLK
)
360 // emulate a click event to force an owner-drawn button to change its
361 // appearance - without this, it won't do it
362 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
364 // and continue with processing the message normally as well
367 // let the base class do all real processing
368 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
371 // ----------------------------------------------------------------------------
372 // owner-drawn buttons support
373 // ----------------------------------------------------------------------------
379 static void DrawButtonText(HDC hdc
,
381 const wxString
& text
,
384 COLORREF colOld
= SetTextColor(hdc
, col
);
385 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
387 DrawText(hdc
, text
, text
.length(), pRect
,
388 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
390 SetBkMode(hdc
, modeOld
);
391 SetTextColor(hdc
, colOld
);
394 static void DrawRect(HDC hdc
, const RECT
& r
)
396 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
397 LineTo(hdc
, r
.right
, r
.top
);
398 LineTo(hdc
, r
.right
, r
.bottom
);
399 LineTo(hdc
, r
.left
, r
.bottom
);
400 LineTo(hdc
, r
.left
, r
.top
);
403 void wxButton::MakeOwnerDrawn()
405 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
406 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
409 style
|= BS_OWNERDRAW
;
410 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
414 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
416 if ( !wxControl::SetBackgroundColour(colour
) )
429 bool wxButton::SetForegroundColour(const wxColour
&colour
)
431 if ( !wxControl::SetForegroundColour(colour
) )
445 The button frame looks like this normally:
448 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
449 WH GB H = light grey (LIGHT)
450 WH GB G = dark grey (SHADOW)
451 WH GB B = black (DKSHADOW)
456 When the button is selected, the button becomes like this (the total button
457 size doesn't change):
468 When the button is pushed (while selected) it is like:
480 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
481 bool selected
, bool pushed
)
484 CopyRect(&r
, &rectBtn
);
486 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
487 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
488 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
489 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
491 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
500 (void)SelectObject(hdc
, hpenGrey
);
501 InflateRect(&r
, -1, -1);
511 InflateRect(&r
, -1, -1);
514 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
515 LineTo(hdc
, r
.right
, r
.bottom
);
516 LineTo(hdc
, r
.right
, r
.top
- 1);
518 (void)SelectObject(hdc
, hpenWhite
);
519 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
520 LineTo(hdc
, r
.left
, r
.top
);
521 LineTo(hdc
, r
.right
, r
.top
);
523 (void)SelectObject(hdc
, hpenLightGr
);
524 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
525 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
526 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
528 (void)SelectObject(hdc
, hpenGrey
);
529 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
530 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
531 LineTo(hdc
, r
.right
- 1, r
.top
);
534 (void)SelectObject(hdc
, hpenOld
);
535 DeleteObject(hpenWhite
);
536 DeleteObject(hpenLightGr
);
537 DeleteObject(hpenGrey
);
538 DeleteObject(hpenBlack
);
541 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
543 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
546 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
548 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
549 colFg
= wxColourToRGB(GetForegroundColour());
551 HDC hdc
= lpDIS
->hDC
;
552 UINT state
= lpDIS
->itemState
;
554 // first, draw the background
555 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
557 FillRect(hdc
, &rectBtn
, hbrushBackground
);
559 // draw the border for the current state
560 bool selected
= (state
& ODS_SELECTED
) != 0;
563 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
566 selected
= panel
->GetDefaultItem() == this;
569 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
571 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
573 // draw the focus rect if needed
574 if ( state
& ODS_FOCUS
)
577 CopyRect(&rectFocus
, &rectBtn
);
579 // I don't know where does this constant come from, but this is how
580 // Windows draws them
581 InflateRect(&rectFocus
, -4, -4);
583 DrawFocusRect(hdc
, &rectFocus
);
588 // the label is shifted by 1 pixel to create "pushed" effect
589 OffsetRect(&rectBtn
, 1, 1);
592 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
593 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
596 ::DeleteObject(hbrushBackground
);
603 #endif // wxUSE_BUTTON