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/stockitem.h"
44 #include "wx/msw/private.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #if wxUSE_EXTENDED_RTTI
52 WX_DEFINE_FLAGS( wxButtonStyle
)
54 wxBEGIN_FLAGS( wxButtonStyle
)
55 // new style border flags, we put them first to
56 // use them for streaming out
57 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
58 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
59 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
60 wxFLAGS_MEMBER(wxBORDER_RAISED
)
61 wxFLAGS_MEMBER(wxBORDER_STATIC
)
62 wxFLAGS_MEMBER(wxBORDER_NONE
)
64 // old style border flags
65 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
66 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
67 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
68 wxFLAGS_MEMBER(wxRAISED_BORDER
)
69 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
70 wxFLAGS_MEMBER(wxBORDER
)
72 // standard window styles
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
76 wxFLAGS_MEMBER(wxWANTS_CHARS
)
77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
78 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
79 wxFLAGS_MEMBER(wxVSCROLL
)
80 wxFLAGS_MEMBER(wxHSCROLL
)
82 wxFLAGS_MEMBER(wxBU_LEFT
)
83 wxFLAGS_MEMBER(wxBU_RIGHT
)
84 wxFLAGS_MEMBER(wxBU_TOP
)
85 wxFLAGS_MEMBER(wxBU_BOTTOM
)
86 wxFLAGS_MEMBER(wxBU_EXACTFIT
)
87 wxEND_FLAGS( wxButtonStyle
)
89 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton
, wxControl
,"wx/button.h")
91 wxBEGIN_PROPERTIES_TABLE(wxButton
)
92 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEvent
)
94 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
95 wxPROPERTY( Label
, wxString
, SetLabel
, GetLabel
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
97 wxPROPERTY_FLAGS( WindowStyle
, wxButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
99 wxEND_PROPERTIES_TABLE()
101 wxBEGIN_HANDLERS_TABLE(wxButton
)
102 wxEND_HANDLERS_TABLE()
104 wxCONSTRUCTOR_6( wxButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
108 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
111 // this macro tries to adjust the default button height to a reasonable value
112 // using the char height as the base
113 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // creation/destruction
121 // ----------------------------------------------------------------------------
123 bool wxButton::Create(wxWindow
*parent
,
129 const wxValidator
& validator
,
130 const wxString
& name
)
133 if (label
.empty() && wxIsStockID(id
))
134 label
= wxGetStockLabel(id
);
136 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
140 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
143 // if the label contains several lines we must explicitly tell the button
144 // about it or it wouldn't draw it correctly ("\n"s would just appear as
147 // NB: we do it here and not in MSWGetStyle() because we need the label
148 // value and m_label is not set yet when MSWGetStyle() is called;
149 // besides changing BS_MULTILINE during run-time is pointless anyhow
150 if ( label
.find(_T('\n')) != wxString::npos
)
152 msStyle
|= BS_MULTILINE
;
156 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
159 wxButton::~wxButton()
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
169 // buttons never have an external border, they draw their own one
170 WXDWORD msStyle
= wxControl::MSWGetStyle
172 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
175 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
176 // each other in any resizeable dialog which has more than one button in
178 msStyle
|= WS_CLIPSIBLINGS
;
181 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
182 // and wxBU_RIGHT to get BS_CENTER!
183 if ( style
& wxBU_LEFT
)
185 if ( style
& wxBU_RIGHT
)
187 if ( style
& wxBU_TOP
)
189 if ( style
& wxBU_BOTTOM
)
190 msStyle
|= BS_BOTTOM
;
193 if ( style
& wxNO_BORDER
)
195 #endif // __WXWINCE__
201 // ----------------------------------------------------------------------------
202 // size management including autosizing
203 // ----------------------------------------------------------------------------
205 wxSize
wxButton::DoGetBestSize() const
208 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn
, NULL
);
211 wxGetCharSize(GetHWND(), &wChar
, &hChar
, GetFont());
213 // add a margin -- the button is wider than just its label
216 // the button height is proportional to the height of the font used
217 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
219 // all buttons have at least the standard size unless the user explicitly
220 // wants them to be of smaller size and used wxBU_EXACTFIT style when
221 // creating the button
222 if ( !HasFlag(wxBU_EXACTFIT
) )
224 wxSize sz
= GetDefaultSize();
233 return wxSize(wBtn
, hBtn
);
237 wxSize
wxButtonBase::GetDefaultSize()
239 static wxSize s_sizeBtn
;
241 if ( s_sizeBtn
.x
== 0 )
244 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
246 // the size of a standard button in the dialog units is 50x14,
247 // translate this to pixels
248 // NB1: the multipliers come from the Windows convention
249 // NB2: the extra +1/+2 were needed to get the size be the same as the
250 // size of the buttons in the standard dialog - I don't know how
251 // this happens, but on my system this size is 75x23 in pixels and
252 // 23*8 isn't even divisible by 14... Would be nice to understand
253 // why these constants are needed though!
254 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
255 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
261 // ----------------------------------------------------------------------------
262 // default button handling
263 // ----------------------------------------------------------------------------
266 "Everything you ever wanted to know about the default buttons" or "Why do we
267 have to do all this?"
269 In MSW the default button should be activated when the user presses Enter
270 and the current control doesn't process Enter itself somehow. This is
271 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
272 Another aspect of "defaultness" is that the default button has different
273 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
274 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
275 be unset if our parent window is not active so it should be unset whenever
276 we lose activation and set back when we regain it.
278 Final complication is that when a button is active, it should be the default
279 one, i.e. pressing Enter on a button always activates it and not another
282 We handle this by maintaining a permanent and a temporary default items in
283 wxControlContainer (both may be NULL). When a button becomes the current
284 control (i.e. gets focus) it sets itself as the temporary default which
285 ensures that it has the right appearance and that Enter will be redirected
286 to it. When the button loses focus, it unsets the temporary default and so
287 the default item will be the permanent default -- that is the default button
288 if any had been set or none otherwise, which is just what we want.
290 NB: all this is quite complicated by now and the worst is that normally
291 it shouldn't be necessary at all as for the normal Windows programs
292 DefWindowProc() and IsDialogMessage() take care of all this
293 automatically -- however in wxWidgets programs this doesn't work for
294 nested hierarchies (i.e. a notebook inside a notebook) for unknown
295 reason and so we have to reproduce all this code ourselves. It would be
296 very nice if we could avoid doing it.
299 // set this button as the (permanently) default one in its panel
300 void wxButton::SetDefault()
302 wxWindow
*parent
= GetParent();
304 wxCHECK_RET( parent
, _T("button without parent?") );
306 // set this one as the default button both for wxWidgets ...
307 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
310 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
311 SetDefaultStyle(this, true);
314 // set this button as being currently default
315 void wxButton::SetTmpDefault()
317 wxWindow
*parent
= GetParent();
319 wxCHECK_RET( parent
, _T("button without parent?") );
321 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
322 parent
->SetTmpDefaultItem(this);
324 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), false);
325 SetDefaultStyle(this, true);
328 // unset this button as currently default, it may still stay permanent default
329 void wxButton::UnsetTmpDefault()
331 wxWindow
*parent
= GetParent();
333 wxCHECK_RET( parent
, _T("button without parent?") );
335 parent
->SetTmpDefaultItem(NULL
);
337 wxWindow
*winOldDefault
= parent
->GetDefaultItem();
339 SetDefaultStyle(this, false);
340 SetDefaultStyle(wxDynamicCast(winOldDefault
, wxButton
), true);
345 wxButton::SetDefaultStyle(wxButton
*btn
, bool on
)
347 // we may be called with NULL pointer -- simpler to do the check here than
348 // in the caller which does wxDynamicCast()
352 // first, let DefDlgProc() know about the new default button
355 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
356 // focus at all any more
357 if ( !wxTheApp
->IsActive() )
360 // look for a panel-like window
361 wxWindow
*win
= btn
->GetParent();
362 while ( win
&& !win
->HasFlag(wxTAB_TRAVERSAL
) )
363 win
= win
->GetParent();
367 ::SendMessage(GetHwndOf(win
), DM_SETDEFID
, btn
->GetId(), 0L);
369 // sending DM_SETDEFID also changes the button style to
370 // BS_DEFPUSHBUTTON so there is nothing more to do
374 // then also change the style as needed
375 long style
= ::GetWindowLong(GetHwndOf(btn
), GWL_STYLE
);
376 if ( !(style
& BS_DEFPUSHBUTTON
) == on
)
378 // don't do it with the owner drawn buttons because it will
379 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
380 // BS_DEFPUSHBUTTON != 0)!
381 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
383 ::SendMessage(GetHwndOf(btn
), BM_SETSTYLE
,
384 on
? style
| BS_DEFPUSHBUTTON
385 : style
& ~BS_DEFPUSHBUTTON
,
390 // redraw the button - it will notice itself that it's
391 // [not] the default one [any longer]
395 //else: already has correct style
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 bool wxButton::SendClickEvent()
404 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
405 event
.SetEventObject(this);
407 return ProcessCommand(event
);
410 void wxButton::Command(wxCommandEvent
& event
)
412 ProcessCommand(event
);
415 // ----------------------------------------------------------------------------
416 // event/message handlers
417 // ----------------------------------------------------------------------------
419 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
421 bool processed
= false;
424 // NOTE: Apparently older versions (NT 4?) of the common controls send
425 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
426 // buttons, so in order to send two EVT_BUTTON events we should
427 // catch both types. Currently (Feb 2003) up-to-date versions of
428 // win98, win2k and winXP all send two BN_CLICKED messages for
429 // all button types, so we don't catch BN_DOUBLECLICKED anymore
430 // in order to not get 3 EVT_BUTTON events. If this is a problem
431 // then we need to figure out which version of the comctl32 changed
432 // this behaviour and test for it.
434 case 1: // message came from an accelerator
435 case BN_CLICKED
: // normal buttons send this
436 processed
= SendClickEvent();
443 WXLRESULT
wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
445 // when we receive focus, we want to temporarily become the default button in
446 // our parent panel so that pressing "Enter" would activate us -- and when
447 // losing it we should restore the previous default button as well
448 if ( nMsg
== WM_SETFOCUS
)
452 // let the default processing take place too
454 else if ( nMsg
== WM_KILLFOCUS
)
458 else if ( nMsg
== WM_LBUTTONDBLCLK
)
460 // emulate a click event to force an owner-drawn button to change its
461 // appearance - without this, it won't do it
462 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
464 // and continue with processing the message normally as well
467 // let the base class do all real processing
468 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
471 // ----------------------------------------------------------------------------
472 // owner-drawn buttons support
473 // ----------------------------------------------------------------------------
479 static void DrawButtonText(HDC hdc
,
481 const wxString
& text
,
484 COLORREF colOld
= SetTextColor(hdc
, col
);
485 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
487 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
488 ::DrawText(hdc
, text
, text
.length(), pRect
, DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
490 SetBkMode(hdc
, modeOld
);
491 SetTextColor(hdc
, colOld
);
494 static void DrawRect(HDC hdc
, const RECT
& r
)
496 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
497 wxDrawLine(hdc
, r
.right
, r
.top
, r
.right
, r
.bottom
);
498 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.left
, r
.bottom
);
499 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.left
, r
.top
);
502 void wxButton::MakeOwnerDrawn()
504 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
505 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
508 style
|= BS_OWNERDRAW
;
509 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
513 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
515 if ( !wxControl::SetBackgroundColour(colour
) )
528 bool wxButton::SetForegroundColour(const wxColour
&colour
)
530 if ( !wxControl::SetForegroundColour(colour
) )
544 The button frame looks like this normally:
547 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
548 WH GB H = light grey (LIGHT)
549 WH GB G = dark grey (SHADOW)
550 WH GB B = black (DKSHADOW)
555 When the button is selected, the button becomes like this (the total button
556 size doesn't change):
567 When the button is pushed (while selected) it is like:
579 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
580 bool selected
, bool pushed
)
583 CopyRect(&r
, &rectBtn
);
585 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
586 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
587 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
588 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
590 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
599 (void)SelectObject(hdc
, hpenGrey
);
600 ::InflateRect(&r
, -1, -1);
610 ::InflateRect(&r
, -1, -1);
613 wxDrawLine(hdc
, r
.left
, r
.bottom
, r
.right
, r
.bottom
);
614 wxDrawLine(hdc
, r
.right
, r
.bottom
, r
.right
, r
.top
- 1);
616 (void)SelectObject(hdc
, hpenWhite
);
617 wxDrawLine(hdc
, r
.left
, r
.bottom
- 1, r
.left
, r
.top
);
618 wxDrawLine(hdc
, r
.left
, r
.top
, r
.right
, r
.top
);
620 (void)SelectObject(hdc
, hpenLightGr
);
621 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 2, r
.left
+ 1, r
.top
+ 1);
622 wxDrawLine(hdc
, r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.top
+ 1);
624 (void)SelectObject(hdc
, hpenGrey
);
625 wxDrawLine(hdc
, r
.left
+ 1, r
.bottom
- 1, r
.right
- 1, r
.bottom
- 1);
626 wxDrawLine(hdc
, r
.right
- 1, r
.bottom
- 1, r
.right
- 1, r
.top
);
629 (void)SelectObject(hdc
, hpenOld
);
630 DeleteObject(hpenWhite
);
631 DeleteObject(hpenLightGr
);
632 DeleteObject(hpenGrey
);
633 DeleteObject(hpenBlack
);
636 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
638 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
641 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
643 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
644 colFg
= wxColourToRGB(GetForegroundColour());
646 HDC hdc
= lpDIS
->hDC
;
647 UINT state
= lpDIS
->itemState
;
649 // first, draw the background
650 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
652 FillRect(hdc
, &rectBtn
, hbrushBackground
);
654 // draw the border for the current state
655 bool selected
= (state
& ODS_SELECTED
) != 0;
658 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
661 selected
= panel
->GetDefaultItem() == this;
664 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
666 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
668 // draw the focus rect if needed
669 if ( state
& ODS_FOCUS
)
672 CopyRect(&rectFocus
, &rectBtn
);
674 // I don't know where does this constant come from, but this is how
675 // Windows draws them
676 InflateRect(&rectFocus
, -4, -4);
678 DrawFocusRect(hdc
, &rectFocus
);
683 // the label is shifted by 1 pixel to create "pushed" effect
684 OffsetRect(&rectBtn
, 1, 1);
687 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
688 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
691 ::DeleteObject(hbrushBackground
);
698 #endif // wxUSE_BUTTON