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 wxSize sz
= GetDefaultSize();
151 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
152 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
158 wxSize
wxButtonBase::GetDefaultSize()
160 static wxSize s_sizeBtn
;
162 if ( s_sizeBtn
.x
== 0 )
165 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
167 // the size of a standard button in the dialog units is 50x14,
168 // translate this to pixels
169 // NB1: the multipliers come from the Windows convention
170 // NB2: the extra +1/+2 were needed to get the size be the same as the
171 // size of the buttons in the standard dialog - I don't know how
172 // this happens, but on my system this size is 75x23 in pixels and
173 // 23*8 isn't even divisible by 14... Would be nice to understand
174 // why these constants are needed though!
175 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
176 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
182 // ----------------------------------------------------------------------------
183 // set this button as the default one in its panel
184 // ----------------------------------------------------------------------------
186 void wxButton::SetDefault()
188 wxWindow
*parent
= GetParent();
189 wxButton
*btnOldDefault
;
192 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
193 btnOldDefault
= wxDynamicCast(winOldDefault
, wxButton
);
195 ::SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
197 else // is a button without parent really normal?
199 btnOldDefault
= NULL
;
202 if ( btnOldDefault
&& btnOldDefault
!= this )
204 // remove the BS_DEFPUSHBUTTON style from the other button
205 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
207 // don't do it with the owner drawn buttons because it will reset
208 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
209 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
211 style
&= ~BS_DEFPUSHBUTTON
;
212 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
216 // redraw the button - it will notice itself that it's not the
217 // default one any longer
218 btnOldDefault
->Refresh();
222 // set this button as the default
223 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
224 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
226 style
|= BS_DEFPUSHBUTTON
;
227 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 bool wxButton::SendClickEvent()
237 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
238 event
.SetEventObject(this);
240 return ProcessCommand(event
);
243 void wxButton::Command(wxCommandEvent
& event
)
245 ProcessCommand(event
);
248 // ----------------------------------------------------------------------------
249 // event/message handlers
250 // ----------------------------------------------------------------------------
252 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
254 bool processed
= FALSE
;
257 case 1: // message came from an accelerator
258 case BN_CLICKED
: // normal buttons send this
259 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
260 processed
= SendClickEvent();
267 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
269 // when we receive focus, we want to become the default button in our
271 if ( nMsg
== WM_SETFOCUS
)
275 // let the default processign take place too
277 else if ( nMsg
== WM_LBUTTONDBLCLK
)
279 // emulate a click event to force an owner-drawn button to change its
280 // appearance - without this, it won't do it
281 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
283 // and conitnue with processing the message normally as well
286 // let the base class do all real processing
287 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
290 // ----------------------------------------------------------------------------
291 // owner-drawn buttons support
292 // ----------------------------------------------------------------------------
298 static void DrawButtonText(HDC hdc
,
300 const wxString
& text
,
303 COLORREF colOld
= SetTextColor(hdc
, col
);
304 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
306 DrawText(hdc
, text
, text
.length(), pRect
,
307 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
309 SetBkMode(hdc
, modeOld
);
310 SetTextColor(hdc
, colOld
);
313 static void DrawRect(HDC hdc
, const RECT
& r
)
315 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
316 LineTo(hdc
, r
.right
, r
.top
);
317 LineTo(hdc
, r
.right
, r
.bottom
);
318 LineTo(hdc
, r
.left
, r
.bottom
);
319 LineTo(hdc
, r
.left
, r
.top
);
322 void wxButton::MakeOwnerDrawn()
324 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
325 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
328 style
|= BS_OWNERDRAW
;
329 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
333 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
335 if ( !wxControl::SetBackgroundColour(colour
) )
348 bool wxButton::SetForegroundColour(const wxColour
&colour
)
350 if ( !wxControl::SetForegroundColour(colour
) )
364 The button frame looks like this normally:
367 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
368 WH GB H = light grey (LIGHT)
369 WH GB G = dark grey (SHADOW)
370 WH GB B = black (DKSHADOW)
375 When the button is selected, the button becomes like this (the total button
376 size doesn't change):
387 When the button is pushed (while selected) it is like:
399 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
400 bool selected
, bool pushed
)
403 CopyRect(&r
, &rectBtn
);
405 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
406 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
407 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
408 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
410 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
419 (void)SelectObject(hdc
, hpenGrey
);
420 InflateRect(&r
, -1, -1);
430 InflateRect(&r
, -1, -1);
433 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
434 LineTo(hdc
, r
.right
, r
.bottom
);
435 LineTo(hdc
, r
.right
, r
.top
- 1);
437 (void)SelectObject(hdc
, hpenWhite
);
438 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
439 LineTo(hdc
, r
.left
, r
.top
);
440 LineTo(hdc
, r
.right
, r
.top
);
442 (void)SelectObject(hdc
, hpenLightGr
);
443 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
444 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
445 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
447 (void)SelectObject(hdc
, hpenGrey
);
448 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
449 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
450 LineTo(hdc
, r
.right
- 1, r
.top
);
453 (void)SelectObject(hdc
, hpenOld
);
454 DeleteObject(hpenWhite
);
455 DeleteObject(hpenLightGr
);
456 DeleteObject(hpenGrey
);
457 DeleteObject(hpenBlack
);
460 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
462 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
465 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
467 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
468 colFg
= wxColourToRGB(GetForegroundColour());
470 HDC hdc
= lpDIS
->hDC
;
471 UINT state
= lpDIS
->itemState
;
473 // first, draw the background
474 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
476 FillRect(hdc
, &rectBtn
, hbrushBackground
);
478 // draw the border for the current state
479 bool selected
= (state
& ODS_SELECTED
) != 0;
482 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
485 selected
= panel
->GetDefaultItem() == this;
488 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
490 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
492 // draw the focus rect if needed
493 if ( state
& ODS_FOCUS
)
496 CopyRect(&rectFocus
, &rectBtn
);
498 // I don't know where does this constant come from, but this is how
499 // Windows draws them
500 InflateRect(&rectFocus
, -4, -4);
502 DrawFocusRect(hdc
, &rectFocus
);
507 // the label is shifted by 1 pixel to create "pushed" effect
508 OffsetRect(&rectBtn
, 1, 1);
511 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
512 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
515 ::DeleteObject(hbrushBackground
);
522 #endif // wxUSE_BUTTON