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
) != BS_OWNERDRAW
)
203 style
&= ~BS_DEFPUSHBUTTON
;
204 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
208 // redraw the button - it will notice itself that it's not the
209 // default one any longer
210 btnOldDefault
->Refresh();
214 // set this button as the default
215 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
216 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
218 style
|= BS_DEFPUSHBUTTON
;
219 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 bool wxButton::SendClickEvent()
229 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
230 event
.SetEventObject(this);
232 return ProcessCommand(event
);
235 void wxButton::Command(wxCommandEvent
& event
)
237 ProcessCommand(event
);
240 // ----------------------------------------------------------------------------
241 // event/message handlers
242 // ----------------------------------------------------------------------------
244 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
246 bool processed
= FALSE
;
249 case 1: // means that the message came from an accelerator
251 processed
= SendClickEvent();
258 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
260 // when we receive focus, we want to become the default button in our
262 if ( nMsg
== WM_SETFOCUS
)
266 // let the default processign take place too
269 // let the base class do all real processing
270 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
273 // ----------------------------------------------------------------------------
274 // owner-drawn buttons support
275 // ----------------------------------------------------------------------------
281 static void DrawButtonText(HDC hdc
,
283 const wxString
& text
,
286 COLORREF colOld
= SetTextColor(hdc
, col
);
287 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
289 DrawText(hdc
, text
, text
.length(), pRect
,
290 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
292 SetBkMode(hdc
, modeOld
);
293 SetTextColor(hdc
, colOld
);
296 static void DrawRect(HDC hdc
, const RECT
& r
)
298 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
299 LineTo(hdc
, r
.right
, r
.top
);
300 LineTo(hdc
, r
.right
, r
.bottom
);
301 LineTo(hdc
, r
.left
, r
.bottom
);
302 LineTo(hdc
, r
.left
, r
.top
);
305 void wxButton::MakeOwnerDrawn()
307 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
308 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
311 style
|= BS_OWNERDRAW
;
312 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
316 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
318 if ( !wxControl::SetBackgroundColour(colour
) )
331 bool wxButton::SetForegroundColour(const wxColour
&colour
)
333 if ( !wxControl::SetForegroundColour(colour
) )
347 The button frame looks like this normally:
352 W GB where W, G, B are white, grey and black pixels
357 When the button is selected, the button becomes like this (the total button
358 size doesn't change):
368 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 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
389 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
398 (void)SelectObject(hdc
, hpenGrey
);
399 InflateRect(&r
, -1, -1);
409 InflateRect(&r
, -1, -1);
412 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
413 LineTo(hdc
, r
.right
, r
.bottom
);
414 LineTo(hdc
, r
.right
, r
.top
- 1);
416 (void)SelectObject(hdc
, hpenWhite
);
417 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
418 LineTo(hdc
, r
.left
, r
.top
);
419 LineTo(hdc
, r
.right
, r
.top
);
421 (void)SelectObject(hdc
, hpenGrey
);
422 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
423 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
424 LineTo(hdc
, r
.right
- 1, r
.top
);
427 (void)SelectObject(hdc
, hpenOld
);
428 DeleteObject(hpenWhite
);
429 DeleteObject(hpenGrey
);
430 DeleteObject(hpenBlack
);
433 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
435 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
438 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
440 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
441 colFg
= wxColourToRGB(GetForegroundColour());
443 HDC hdc
= lpDIS
->hDC
;
444 UINT state
= lpDIS
->itemState
;
446 // first, draw the background
447 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
449 FillRect(hdc
, &rectBtn
, hbrushBackground
);
451 // draw the border for the current state
452 bool selected
= (state
& ODS_SELECTED
) != 0;
455 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
458 selected
= panel
->GetDefaultItem() == this;
461 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
463 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
465 // draw the focus rect if needed
466 if ( state
& ODS_FOCUS
)
469 CopyRect(&rectFocus
, &rectBtn
);
471 // I don't know where does this constant come from, but this is how
472 // Windows draws them
473 InflateRect(&rectFocus
, -4, -4);
475 DrawFocusRect(hdc
, &rectFocus
);
480 // the label is shifted by 1 pixel to create "pushed" effect
481 OffsetRect(&rectBtn
, 1, 1);
484 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
485 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
488 ::DeleteObject(hbrushBackground
);