1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/button.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
51 // this macro tries to adjust the default button height to a reasonable value
52 // using the char height as the base
53 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // creation/destruction
61 // ----------------------------------------------------------------------------
63 bool wxButton::Create(wxWindow
*parent
,
65 const wxString
& label
,
69 const wxValidator
& validator
,
72 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
76 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
79 // if the label contains several lines we must explicitly tell the button
80 // about it or it wouldn't draw it correctly ("\n"s would just appear as
83 // NB: we do it here and not in MSWGetStyle() because we need the label
84 // value and m_label is not set yet when MSWGetStyle() is called;
85 // besides changing BS_MULTILINE during run-time is pointless anyhow
86 if ( label
.find(_T('\n')) != wxString::npos
)
88 msStyle
|= BS_MULTILINE
;
92 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
105 // buttons never have an external border, they draw their own one
106 WXDWORD msStyle
= wxControl::MSWGetStyle
108 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
111 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
112 // each other in any resizeable dialog which has more than one button in
114 msStyle
|= WS_CLIPSIBLINGS
;
117 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
118 // and wxBU_RIGHT to get BS_CENTER!
119 if ( style
& wxBU_LEFT
)
121 if ( style
& wxBU_RIGHT
)
123 if ( style
& wxBU_TOP
)
125 if ( style
& wxBU_BOTTOM
)
126 msStyle
|= BS_BOTTOM
;
132 // ----------------------------------------------------------------------------
133 // size management including autosizing
134 // ----------------------------------------------------------------------------
136 wxSize
wxButton::DoGetBestSize() const
139 GetTextExtent(wxGetWindowText(GetHWND()), &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 // all buttons have at least the standard size unless the user explicitly
151 // wants them to be of smaller size and used wxBU_EXACTFIT style when
152 // creating the button
153 if ( !HasFlag(wxBU_EXACTFIT
) )
155 wxSize sz
= GetDefaultSize();
164 return wxSize(wBtn
, hBtn
);
168 wxSize
wxButtonBase::GetDefaultSize()
170 static wxSize s_sizeBtn
;
172 if ( s_sizeBtn
.x
== 0 )
175 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
177 // the size of a standard button in the dialog units is 50x14,
178 // translate this to pixels
179 // NB1: the multipliers come from the Windows convention
180 // NB2: the extra +1/+2 were needed to get the size be the same as the
181 // size of the buttons in the standard dialog - I don't know how
182 // this happens, but on my system this size is 75x23 in pixels and
183 // 23*8 isn't even divisible by 14... Would be nice to understand
184 // why these constants are needed though!
185 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
186 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
192 // ----------------------------------------------------------------------------
193 // default button handling
194 // ----------------------------------------------------------------------------
197 "Everything you ever wanted to know about the default buttons" or "Why do we
198 have to do all this?"
200 In MSW the default button should be activated when the user presses Enter
201 and the current control doesn't process Enter itself somehow. This is
202 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
203 Another aspect of "defaultness" is that the default button has different
204 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
205 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
206 be unset if our parent window is not active so it should be unset whenever
207 we lose activation and set back when we regain it.
209 Final complication is that when a button is active, it should be the default
210 one, i.e. pressing Enter on a button always activates it and not another
213 We handle this by maintaining a permanent and a temporary default items in
214 wxControlContainer (both may be NULL). When a button becomes the current
215 control (i.e. gets focus) it sets itself as the temporary default which
216 ensures that it has the right appearance and that Enter will be redirected
217 to it. When the button loses focus, it unsets the temporary default and so
218 the default item will be the permanent default -- that is the default button
219 if any had been set or none otherwise, which is just what we want.
221 NB: all this is quite complicated by now and the worst is that normally
222 it shouldn't be necessary at all as for the normal Windows programs
223 DefWindowProc() and IsDialogMessage() take care of all this
224 automatically -- however in wxWindows programs this doesn't work for
225 nested hierarchies (i.e. a notebook inside a notebook) for unknown
226 reason and so we have to reproduce all this code ourselves. It would be
227 very nice if we could avoid doing it.
230 // set this button as the (permanently) default one in its panel
231 void wxButton::SetDefault()
233 wxWindow
*parent
= GetParent();
235 wxCHECK_RET( parent
, _T("button without parent?") );
237 // set this one as the default button both for wxWindows ...
238 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
241 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
242 SetDefaultStyle(this, TRUE
);
245 // set this button as being currently default
246 void wxButton::SetTmpDefault()
248 wxWindow
*parent
= GetParent();
250 wxCHECK_RET( parent
, _T("button without parent?") );
252 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
253 parent
->SetTmpDefaultItem(this);
255 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), FALSE
);
256 SetDefaultStyle(this, TRUE
);
259 // unset this button as currently default, it may still stay permanent default
260 void wxButton::UnsetTmpDefault()
262 wxWindow
*parent
= GetParent();
264 wxCHECK_RET( parent
, _T("button without parent?") );
266 parent
->SetTmpDefaultItem(NULL
);
268 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
270 SetDefaultStyle(this, FALSE
);
271 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), TRUE
);
276 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
278 // we may be called with NULL pointer -- simpler to do the check here than
279 // in the caller which does wxDynamicCast()
283 // first, let DefDlgProc() know about the new default button
286 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
287 // focus at all any more
288 if ( !wxTheApp
->IsActive() )
291 // look for a panel-like window
292 wxWindow
*win
= btn
->GetParent();
293 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
294 win
= win
->GetParent();
298 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
300 // sending DM_SETDEFID also changes the button style to
301 // BS_DEFPUSHBUTTON so there is nothing more to do
305 // then also change the style as needed
306 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
307 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
309 // don't do it with the owner drawn buttons because it will
310 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
311 // BS_DEFPUSHBUTTON != 0)!
312 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
314 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
315 on
? style
| BS_DEFPUSHBUTTON
316 : style
& ~BS_DEFPUSHBUTTON
,
321 // redraw the button - it will notice itself that it's
322 // [not] the default one [any longer]
326 //else: already has correct style
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 bool wxButton::SendClickEvent()
335 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
336 event
.SetEventObject(this);
338 return ProcessCommand(event
);
341 void wxButton::Command(wxCommandEvent
& event
)
343 ProcessCommand(event
);
346 // ----------------------------------------------------------------------------
347 // event/message handlers
348 // ----------------------------------------------------------------------------
350 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
352 bool processed
= FALSE
;
355 // NOTE: Apparently older versions (NT 4?) of the common controls send
356 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
357 // buttons, so in order to send two EVET_BUTTON events we should
358 // catch both types. Currently (Feb 2003) up-to-date versions of
359 // win98, win2k and winXP all send two BN_CLICKED messages for
360 // all button types, so we don't catch BN_DOUBLECLICKED anymore
361 // in order to not get 3 EVT_BUTTON events. If this is a problem
362 // then we need to figure out which version of the comctl32 changed
363 // this behaviour and test for it.
365 case 1: // message came from an accelerator
366 case BN_CLICKED
: // normal buttons send this
367 processed
= SendClickEvent();
374 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
376 // when we receive focus, we want to temporary become the default button in
377 // our parent panel so that pressing "Enter" would activate us -- and when
378 // losing it we should restore the previous default button as well
379 if ( nMsg
== WM_SETFOCUS
)
383 // let the default processing take place too
385 else if ( nMsg
== WM_KILLFOCUS
)
389 else if ( nMsg
== WM_LBUTTONDBLCLK
)
391 // emulate a click event to force an owner-drawn button to change its
392 // appearance - without this, it won't do it
393 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
395 // and continue with processing the message normally as well
398 // let the base class do all real processing
399 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
402 // ----------------------------------------------------------------------------
403 // owner-drawn buttons support
404 // ----------------------------------------------------------------------------
410 static void DrawButtonText(HDC hdc
,
412 const wxString
& text
,
415 COLORREF colOld
= SetTextColor(hdc
, col
);
416 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
418 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
419 ::DrawText(hdc
, text
, text
.length(), pRect
, DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
421 SetBkMode(hdc
, modeOld
);
422 SetTextColor(hdc
, colOld
);
425 static void DrawRect(HDC hdc
, const RECT
& r
)
427 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
428 LineTo(hdc
, r
.right
, r
.top
);
429 LineTo(hdc
, r
.right
, r
.bottom
);
430 LineTo(hdc
, r
.left
, r
.bottom
);
431 LineTo(hdc
, r
.left
, r
.top
);
434 void wxButton::MakeOwnerDrawn()
436 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
437 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
440 style
|= BS_OWNERDRAW
;
441 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
445 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
447 if ( !wxControl::SetBackgroundColour(colour
) )
460 bool wxButton::SetForegroundColour(const wxColour
&colour
)
462 if ( !wxControl::SetForegroundColour(colour
) )
476 The button frame looks like this normally:
479 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
480 WH GB H = light grey (LIGHT)
481 WH GB G = dark grey (SHADOW)
482 WH GB B = black (DKSHADOW)
487 When the button is selected, the button becomes like this (the total button
488 size doesn't change):
499 When the button is pushed (while selected) it is like:
511 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
512 bool selected
, bool pushed
)
515 CopyRect(&r
, &rectBtn
);
517 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
518 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
519 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
520 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
522 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
531 (void)SelectObject(hdc
, hpenGrey
);
532 InflateRect(&r
, -1, -1);
542 InflateRect(&r
, -1, -1);
545 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
546 LineTo(hdc
, r
.right
, r
.bottom
);
547 LineTo(hdc
, r
.right
, r
.top
- 1);
549 (void)SelectObject(hdc
, hpenWhite
);
550 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
551 LineTo(hdc
, r
.left
, r
.top
);
552 LineTo(hdc
, r
.right
, r
.top
);
554 (void)SelectObject(hdc
, hpenLightGr
);
555 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
556 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
557 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
559 (void)SelectObject(hdc
, hpenGrey
);
560 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
561 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
562 LineTo(hdc
, r
.right
- 1, r
.top
);
565 (void)SelectObject(hdc
, hpenOld
);
566 DeleteObject(hpenWhite
);
567 DeleteObject(hpenLightGr
);
568 DeleteObject(hpenGrey
);
569 DeleteObject(hpenBlack
);
572 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
574 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
577 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
579 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
580 colFg
= wxColourToRGB(GetForegroundColour());
582 HDC hdc
= lpDIS
->hDC
;
583 UINT state
= lpDIS
->itemState
;
585 // first, draw the background
586 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
588 FillRect(hdc
, &rectBtn
, hbrushBackground
);
590 // draw the border for the current state
591 bool selected
= (state
& ODS_SELECTED
) != 0;
594 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
597 selected
= panel
->GetDefaultItem() == this;
600 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
602 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
604 // draw the focus rect if needed
605 if ( state
& ODS_FOCUS
)
608 CopyRect(&rectFocus
, &rectBtn
);
610 // I don't know where does this constant come from, but this is how
611 // Windows draws them
612 InflateRect(&rectFocus
, -4, -4);
614 DrawFocusRect(hdc
, &rectFocus
);
619 // the label is shifted by 1 pixel to create "pushed" effect
620 OffsetRect(&rectBtn
, 1, 1);
623 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
624 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
627 ::DeleteObject(hbrushBackground
);
634 #endif // wxUSE_BUTTON