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
);
101 wxButton::~wxButton()
103 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
106 if ( panel
->GetDefaultItem() == this )
108 // don't leave the panel with invalid default item
109 panel
->SetDefaultItem(NULL
);
114 // ----------------------------------------------------------------------------
115 // size management including autosizing
116 // ----------------------------------------------------------------------------
118 wxSize
wxButton::DoGetBestSize() const
120 wxString label
= wxGetWindowText(GetHWND());
122 GetTextExtent(label
, &wBtn
, NULL
);
125 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
127 // add a margin - the button is wider than just its label
130 // the button height is proportional to the height of the font used
131 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
133 wxSize sz
= GetDefaultSize();
134 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
135 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
141 wxSize
wxButton::GetDefaultSize()
143 static wxSize s_sizeBtn
;
145 if ( s_sizeBtn
.x
== 0 )
148 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
150 // the size of a standard button in the dialog units is 50x14,
151 // translate this to pixels
152 // NB1: the multipliers come from the Windows convention
153 // NB2: the extra +1/+2 were needed to get the size be the same as the
154 // size of the buttons in the standard dialog - I don't know how
155 // this happens, but on my system this size is 75x23 in pixels and
156 // 23*8 isn't even divisible by 14... Would be nice to understand
157 // why these constants are needed though!
158 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
159 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
165 // ----------------------------------------------------------------------------
166 // set this button as the default one in its panel
167 // ----------------------------------------------------------------------------
169 void wxButton::SetDefault()
171 wxWindow
*parent
= GetParent();
172 wxButton
*btnOldDefault
= NULL
;
173 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
176 btnOldDefault
= panel
->GetDefaultItem();
177 panel
->SetDefaultItem(this);
182 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
187 // remove the BS_DEFPUSHBUTTON style from the other button
188 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
190 // don't do it with the owner drawn buttons because it will reset
191 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
192 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
194 style
&= ~BS_DEFPUSHBUTTON
;
195 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
199 // redraw the button - it will notice itself that it's not the
200 // default one any longer
201 btnOldDefault
->Refresh();
205 // set this button as the default
206 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
207 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
209 style
|= BS_DEFPUSHBUTTON
;
210 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: // message came from an accelerator
241 case BN_CLICKED
: // normal buttons send this
242 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
243 processed
= SendClickEvent();
250 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
252 // when we receive focus, we want to become the default button in our
254 if ( nMsg
== WM_SETFOCUS
)
258 // let the default processign take place too
260 else if ( nMsg
== WM_LBUTTONDBLCLK
)
262 // trick the base class into thinking that this was just a click
263 nMsg
= WM_LBUTTONDOWN
;
266 // let the base class do all real processing
267 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
270 // ----------------------------------------------------------------------------
271 // owner-drawn buttons support
272 // ----------------------------------------------------------------------------
278 static void DrawButtonText(HDC hdc
,
280 const wxString
& text
,
283 COLORREF colOld
= SetTextColor(hdc
, col
);
284 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
286 DrawText(hdc
, text
, text
.length(), pRect
,
287 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
289 SetBkMode(hdc
, modeOld
);
290 SetTextColor(hdc
, colOld
);
293 static void DrawRect(HDC hdc
, const RECT
& r
)
295 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
296 LineTo(hdc
, r
.right
, r
.top
);
297 LineTo(hdc
, r
.right
, r
.bottom
);
298 LineTo(hdc
, r
.left
, r
.bottom
);
299 LineTo(hdc
, r
.left
, r
.top
);
302 void wxButton::MakeOwnerDrawn()
304 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
305 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
308 style
|= BS_OWNERDRAW
;
309 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
313 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
315 if ( !wxControl::SetBackgroundColour(colour
) )
328 bool wxButton::SetForegroundColour(const wxColour
&colour
)
330 if ( !wxControl::SetForegroundColour(colour
) )
344 The button frame looks like this normally:
347 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
348 WH GB H = light grey (LIGHT)
349 WH GB G = dark grey (SHADOW)
350 WH GB B = black (DKSHADOW)
355 When the button is selected, the button becomes like this (the total button
356 size doesn't change):
367 When the button is pushed (while selected) it is like:
379 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
380 bool selected
, bool pushed
)
383 CopyRect(&r
, &rectBtn
);
385 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
386 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
387 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
388 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
390 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
399 (void)SelectObject(hdc
, hpenGrey
);
400 InflateRect(&r
, -1, -1);
410 InflateRect(&r
, -1, -1);
413 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
414 LineTo(hdc
, r
.right
, r
.bottom
);
415 LineTo(hdc
, r
.right
, r
.top
- 1);
417 (void)SelectObject(hdc
, hpenWhite
);
418 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
419 LineTo(hdc
, r
.left
, r
.top
);
420 LineTo(hdc
, r
.right
, r
.top
);
422 (void)SelectObject(hdc
, hpenLightGr
);
423 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
424 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
425 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
427 (void)SelectObject(hdc
, hpenGrey
);
428 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
429 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
430 LineTo(hdc
, r
.right
- 1, r
.top
);
433 (void)SelectObject(hdc
, hpenOld
);
434 DeleteObject(hpenWhite
);
435 DeleteObject(hpenLightGr
);
436 DeleteObject(hpenGrey
);
437 DeleteObject(hpenBlack
);
440 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
442 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
445 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
447 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
448 colFg
= wxColourToRGB(GetForegroundColour());
450 HDC hdc
= lpDIS
->hDC
;
451 UINT state
= lpDIS
->itemState
;
453 // first, draw the background
454 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
456 FillRect(hdc
, &rectBtn
, hbrushBackground
);
458 // draw the border for the current state
459 bool selected
= (state
& ODS_SELECTED
) != 0;
462 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
465 selected
= panel
->GetDefaultItem() == this;
468 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
470 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
472 // draw the focus rect if needed
473 if ( state
& ODS_FOCUS
)
476 CopyRect(&rectFocus
, &rectBtn
);
478 // I don't know where does this constant come from, but this is how
479 // Windows draws them
480 InflateRect(&rectFocus
, -4, -4);
482 DrawFocusRect(hdc
, &rectFocus
);
487 // the label is shifted by 1 pixel to create "pushed" effect
488 OffsetRect(&rectBtn
, 1, 1);
491 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
492 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
495 ::DeleteObject(hbrushBackground
);