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"
32 #include "wx/button.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
40 #include "wx/msw/private.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
48 // this macro tries to adjust the default button height to a reasonable value
49 // using the char height as the base
50 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
57 // creation/destruction
58 // ----------------------------------------------------------------------------
60 bool wxButton::Create(wxWindow
*parent
,
62 const wxString
& label
,
66 const wxValidator
& validator
,
69 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
72 parent
->AddChild((wxButton
*)this);
74 m_backgroundColour
= parent
->GetBackgroundColour();
75 m_foregroundColour
= parent
->GetForegroundColour();
78 m_hWnd
= (WXHWND
)CreateWindowEx
80 MakeExtendedStyle(m_windowStyle
),
83 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
,
91 // Subclass again for purposes of dialog editing mode
94 SetFont(parent
->GetFont());
96 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
98 // bad hack added by Robert to make buttons at least
99 // 80 pixels wide. There are probably better ways...
101 wxSize
nsize( GetSize() );
102 if ((nsize
.x
< 80) || (nsize
.y
< 23))
104 if ((size
.x
== -1) && (nsize
.x
< 80))
106 if ((size
.y
== -1) && (nsize
.y
< 23))
114 wxButton::~wxButton()
116 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
119 if ( panel
->GetDefaultItem() == this )
121 // don't leave the panel with invalid default item
122 panel
->SetDefaultItem(NULL
);
127 // ----------------------------------------------------------------------------
128 // size management including autosizing
129 // ----------------------------------------------------------------------------
131 wxSize
wxButton::DoGetBestSize() const
133 wxString label
= wxGetWindowText(GetHWND());
135 GetTextExtent(label
, &wBtn
, NULL
);
138 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
140 // add a margin - the button is wider than just its label
143 // the button height is proportional to the height of the font used
144 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
146 return wxSize(wBtn
, hBtn
);
150 wxSize
wxButton::GetDefaultSize()
152 static wxSize s_sizeBtn
;
154 if ( s_sizeBtn
.x
== 0 )
157 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
159 // the size of a standard button in the dialog units is 50x14,
160 // translate this to pixels
161 // NB1: the multipliers come from the Windows convention
162 // NB2: the extra +1/+2 were needed to get the size be the same as the
163 // size of the buttons in the standard dialog - I don't know how
164 // this happens, but on my system this size is 75x23 in pixels and
165 // 23*8 isn't even divisible by 14... Would be nice to understand
166 // why these constants are needed though!
167 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
168 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
174 // ----------------------------------------------------------------------------
175 // set this button as the default one in its panel
176 // ----------------------------------------------------------------------------
178 void wxButton::SetDefault()
180 wxWindow
*parent
= GetParent();
181 wxButton
*btnOldDefault
= NULL
;
182 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
185 btnOldDefault
= panel
->GetDefaultItem();
186 panel
->SetDefaultItem(this);
191 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
196 // remove the BS_DEFPUSHBUTTON style from the other button
197 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
199 // don't do it with the owner drawn buttons because it will reset
200 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
201 if ( !(style
& BS_OWNERDRAW
) )
203 style
&= ~BS_DEFPUSHBUTTON
;
204 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
208 // set this button as the default
209 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
210 style
|= BS_DEFPUSHBUTTON
;
211 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxButton::SendClickEvent()
220 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
221 event
.SetEventObject(this);
223 return ProcessCommand(event
);
226 void wxButton::Command(wxCommandEvent
& event
)
228 ProcessCommand(event
);
231 // ----------------------------------------------------------------------------
232 // event/message handlers
233 // ----------------------------------------------------------------------------
235 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
237 bool processed
= FALSE
;
240 case 1: // means that the message came from an accelerator
242 processed
= SendClickEvent();
249 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
251 // make sure that we won't have BS_DEFPUSHBUTTON style any more if the
252 // focus is being transfered to another button with the same parent -
253 // otherwise, we could finish with 2 default buttons inside one panel
254 if ( (nMsg
== WM_KILLFOCUS
) &&
255 (GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_DEFPUSHBUTTON
) )
257 wxWindow
*parent
= GetParent();
258 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)wParam
);
259 if ( win
&& win
->GetParent() == parent
)
261 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
264 panel
->SetDefaultItem(this);
266 // else: I don't know what to do - we'll still have the problem
267 // with multiple default buttons in a dialog...
271 // let the base class do all real processing
272 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
275 // ----------------------------------------------------------------------------
276 // owner-drawn buttons support
277 // ----------------------------------------------------------------------------
283 static void DrawButtonText(HDC hdc
,
285 const wxString
& text
,
288 COLORREF colOld
= SetTextColor(hdc
, col
);
289 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
291 DrawText(hdc
, text
, text
.length(), pRect
,
292 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
294 SetBkMode(hdc
, modeOld
);
295 SetTextColor(hdc
, colOld
);
298 static void DrawRect(HDC hdc
, const RECT
& r
)
300 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
301 LineTo(hdc
, r
.right
, r
.top
);
302 LineTo(hdc
, r
.right
, r
.bottom
);
303 LineTo(hdc
, r
.left
, r
.bottom
);
304 LineTo(hdc
, r
.left
, r
.top
);
307 void wxButton::MakeOwnerDrawn()
309 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
310 if ( !(style
& BS_OWNERDRAW
) )
313 style
|= BS_OWNERDRAW
;
314 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
318 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
320 if ( !wxControl::SetBackgroundColour(colour
) )
333 bool wxButton::SetForegroundColour(const wxColour
&colour
)
335 if ( !wxControl::SetForegroundColour(colour
) )
349 The button frame looks like this normally:
354 W GB where W, G, B are white, grey and black pixels
359 When the button is selected, the button becomes like this (the total button
360 size doesn't change):
370 When the button is pushed (while selected) it is like:
381 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
382 bool selected
, bool pushed
)
385 CopyRect(&r
, &rectBtn
);
387 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
388 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
389 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
391 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
400 (void)SelectObject(hdc
, hpenGrey
);
401 InflateRect(&r
, -1, -1);
411 InflateRect(&r
, -1, -1);
414 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
415 LineTo(hdc
, r
.right
, r
.bottom
);
416 LineTo(hdc
, r
.right
, r
.top
- 1);
418 (void)SelectObject(hdc
, hpenWhite
);
419 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
420 LineTo(hdc
, r
.left
, r
.top
);
421 LineTo(hdc
, r
.right
, r
.top
);
423 (void)SelectObject(hdc
, hpenGrey
);
424 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
425 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
426 LineTo(hdc
, r
.right
- 1, r
.top
);
429 (void)SelectObject(hdc
, hpenOld
);
430 DeleteObject(hpenWhite
);
431 DeleteObject(hpenGrey
);
432 DeleteObject(hpenBlack
);
435 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
437 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
440 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
442 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
443 colFg
= wxColourToRGB(GetForegroundColour());
445 HDC hdc
= lpDIS
->hDC
;
446 UINT state
= lpDIS
->itemState
;
448 // first, draw the background
449 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
451 FillRect(hdc
, &rectBtn
, hbrushBackground
);
453 // draw the border for the current state
454 bool selected
= (state
& ODS_SELECTED
) != 0;
457 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
460 selected
= panel
->GetDefaultItem() == this;
463 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
465 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
467 // draw the focus rect if needed
468 if ( state
& ODS_FOCUS
)
471 CopyRect(&rectFocus
, &rectBtn
);
473 // I don't know where does this constant come from, but this is how
474 // Windows draws them
475 InflateRect(&rectFocus
, -4, -4);
477 DrawFocusRect(hdc
, &rectFocus
);
480 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
481 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
484 ::DeleteObject(hbrushBackground
);
487 wxString s
= "button state: ";
492 if ( state
& ODS_FOCUS
)